summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorschwarze <>2024-11-29 12:05:06 +0000
committerschwarze <>2024-11-29 12:05:06 +0000
commit7bf674fac288e15425988251ece54449f398a71c (patch)
tree4a56b8e434e1dc9af2c9250b8d168eb98c0257e7 /src
parented5a19b8d4937430721c54a137750602df0e201f (diff)
downloadopenbsd-7bf674fac288e15425988251ece54449f398a71c.tar.gz
openbsd-7bf674fac288e15425988251ece54449f398a71c.tar.bz2
openbsd-7bf674fac288e15425988251ece54449f398a71c.zip
Provide an example of signing with HMAC-SHA256 or Ed25519
because that makes it easier to see the big picture of how EVP_PKEY_new_raw_private_key(3) is supposed to be used. Feedback and OK tb@.
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/man/EVP_PKEY_new.370
1 files changed, 67 insertions, 3 deletions
diff --git a/src/lib/libcrypto/man/EVP_PKEY_new.3 b/src/lib/libcrypto/man/EVP_PKEY_new.3
index aae1ab3f91..0705c8432a 100644
--- a/src/lib/libcrypto/man/EVP_PKEY_new.3
+++ b/src/lib/libcrypto/man/EVP_PKEY_new.3
@@ -1,10 +1,10 @@
1.\" $OpenBSD: EVP_PKEY_new.3,v 1.21 2024/11/12 20:15:24 schwarze Exp $ 1.\" $OpenBSD: EVP_PKEY_new.3,v 1.22 2024/11/29 12:05:06 schwarze Exp $
2.\" full merge up to: OpenSSL 4dcfdfce May 27 11:50:05 2020 +0100 2.\" full merge up to: OpenSSL 4dcfdfce May 27 11:50:05 2020 +0100
3.\" 3.\"
4.\" This file is a derived work. 4.\" This file is a derived work.
5.\" The changes are covered by the following Copyright and license: 5.\" The changes are covered by the following Copyright and license:
6.\" 6.\"
7.\" Copyright (c) 2022 Ingo Schwarze <schwarze@openbsd.org> 7.\" Copyright (c) 2022, 2024 Ingo Schwarze <schwarze@openbsd.org>
8.\" 8.\"
9.\" Permission to use, copy, modify, and distribute this software for any 9.\" Permission to use, copy, modify, and distribute this software for any
10.\" purpose with or without fee is hereby granted, provided that the above 10.\" purpose with or without fee is hereby granted, provided that the above
@@ -66,7 +66,7 @@
66.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 66.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
67.\" OF THE POSSIBILITY OF SUCH DAMAGE. 67.\" OF THE POSSIBILITY OF SUCH DAMAGE.
68.\" 68.\"
69.Dd $Mdocdate: November 12 2024 $ 69.Dd $Mdocdate: November 29 2024 $
70.Dt EVP_PKEY_NEW 3 70.Dt EVP_PKEY_NEW 3
71.Os 71.Os
72.Sh NAME 72.Sh NAME
@@ -242,6 +242,70 @@ if an error occurred.
242and 242and
243.Fn EVP_PKEY_get_raw_public_key 243.Fn EVP_PKEY_get_raw_public_key
244return 1 for success or 0 for failure. 244return 1 for success or 0 for failure.
245.Sh EXAMPLES
246The following code digests a message with HMAC-SHA256:
247.Bd -literal -offset indent
248/* Bogus key: would normally be set from another source */
249const unsigned char *key = "key";
250const size_t key_len = strlen(key);
251
252const char *msg = "The quick brown fox jumps over the lazy dog";
253const size_t msg_len = strlen(msg);
254
255unsigned char *out_mac;
256size_t out_len, i;
257
258EVP_PKEY *pkey;
259EVP_MD_CTX *md_ctx;
260
261pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
262 key, key_len);
263if (pkey == NULL)
264 err(1, "EVP_PKEY_new_raw_private_key");
265
266md_ctx = EVP_MD_CTX_new();
267if (md_ctx == NULL)
268 err(1, "EVP_MD_CTX_new");
269
270if (EVP_DigestSignInit(md_ctx, NULL, EVP_sha256(), NULL, pkey) == 0)
271 err(1, "EVP_DigestSignInit");
272if (EVP_DigestSign(md_ctx, NULL, &out_len, msg, msg_len) == 0)
273 err(1, "EVP_DigestSign(NULL)");
274if ((out_mac = calloc(1, out_len)) == NULL)
275 err(1, "calloc");
276if (EVP_DigestSign(md_ctx, out_mac, &out_len, msg, msg_len) == 0)
277 err(1, "EVP_DigestSign(MAC)");
278
279EVP_MD_CTX_free(md_ctx);
280EVP_PKEY_free(pkey);
281
282printf(" MAC = ");
283for (i = 0; i < out_len; i++)
284 printf("%02x", out_mac[i]);
285printf("\en");
286free(out_mac);
287.Ed
288.Pp
289Even though the type name
290.Vt EVP_PKEY
291was originally intended to stand for
292.Dq private key
293and the
294.Xr EVP_DigestSignInit 3
295API was designed for digital signatures in the context of public key
296cryptography, both are also used here because a MAC also requires a key,
297even though that is a symmetric key.
298.Pp
299The same code can be used for signing with Ed25519 by making the key
300.Dv ED25519_PRIVATE_KEY_LENGTH No = 32
301bytes long, replacing
302.Dv EVP_PKEY_HMAC
303with
304.Dv EVP_PKEY_ED25519 ,
305and replacing the call to
306.Xr EVP_sha256 3
307with
308.Dv NULL .
245.Sh SEE ALSO 309.Sh SEE ALSO
246.Xr CMAC_Init 3 , 310.Xr CMAC_Init 3 ,
247.Xr d2i_PrivateKey 3 , 311.Xr d2i_PrivateKey 3 ,