diff options
author | schwarze <> | 2024-11-29 12:05:06 +0000 |
---|---|---|
committer | schwarze <> | 2024-11-29 12:05:06 +0000 |
commit | 7bf674fac288e15425988251ece54449f398a71c (patch) | |
tree | 4a56b8e434e1dc9af2c9250b8d168eb98c0257e7 /src | |
parent | ed5a19b8d4937430721c54a137750602df0e201f (diff) | |
download | openbsd-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.3 | 70 |
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. | |||
242 | and | 242 | and |
243 | .Fn EVP_PKEY_get_raw_public_key | 243 | .Fn EVP_PKEY_get_raw_public_key |
244 | return 1 for success or 0 for failure. | 244 | return 1 for success or 0 for failure. |
245 | .Sh EXAMPLES | ||
246 | The following code digests a message with HMAC-SHA256: | ||
247 | .Bd -literal -offset indent | ||
248 | /* Bogus key: would normally be set from another source */ | ||
249 | const unsigned char *key = "key"; | ||
250 | const size_t key_len = strlen(key); | ||
251 | |||
252 | const char *msg = "The quick brown fox jumps over the lazy dog"; | ||
253 | const size_t msg_len = strlen(msg); | ||
254 | |||
255 | unsigned char *out_mac; | ||
256 | size_t out_len, i; | ||
257 | |||
258 | EVP_PKEY *pkey; | ||
259 | EVP_MD_CTX *md_ctx; | ||
260 | |||
261 | pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, | ||
262 | key, key_len); | ||
263 | if (pkey == NULL) | ||
264 | err(1, "EVP_PKEY_new_raw_private_key"); | ||
265 | |||
266 | md_ctx = EVP_MD_CTX_new(); | ||
267 | if (md_ctx == NULL) | ||
268 | err(1, "EVP_MD_CTX_new"); | ||
269 | |||
270 | if (EVP_DigestSignInit(md_ctx, NULL, EVP_sha256(), NULL, pkey) == 0) | ||
271 | err(1, "EVP_DigestSignInit"); | ||
272 | if (EVP_DigestSign(md_ctx, NULL, &out_len, msg, msg_len) == 0) | ||
273 | err(1, "EVP_DigestSign(NULL)"); | ||
274 | if ((out_mac = calloc(1, out_len)) == NULL) | ||
275 | err(1, "calloc"); | ||
276 | if (EVP_DigestSign(md_ctx, out_mac, &out_len, msg, msg_len) == 0) | ||
277 | err(1, "EVP_DigestSign(MAC)"); | ||
278 | |||
279 | EVP_MD_CTX_free(md_ctx); | ||
280 | EVP_PKEY_free(pkey); | ||
281 | |||
282 | printf(" MAC = "); | ||
283 | for (i = 0; i < out_len; i++) | ||
284 | printf("%02x", out_mac[i]); | ||
285 | printf("\en"); | ||
286 | free(out_mac); | ||
287 | .Ed | ||
288 | .Pp | ||
289 | Even though the type name | ||
290 | .Vt EVP_PKEY | ||
291 | was originally intended to stand for | ||
292 | .Dq private key | ||
293 | and the | ||
294 | .Xr EVP_DigestSignInit 3 | ||
295 | API was designed for digital signatures in the context of public key | ||
296 | cryptography, both are also used here because a MAC also requires a key, | ||
297 | even though that is a symmetric key. | ||
298 | .Pp | ||
299 | The same code can be used for signing with Ed25519 by making the key | ||
300 | .Dv ED25519_PRIVATE_KEY_LENGTH No = 32 | ||
301 | bytes long, replacing | ||
302 | .Dv EVP_PKEY_HMAC | ||
303 | with | ||
304 | .Dv EVP_PKEY_ED25519 , | ||
305 | and replacing the call to | ||
306 | .Xr EVP_sha256 3 | ||
307 | with | ||
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 , |