summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschwarze <>2024-12-29 12:27:28 +0000
committerschwarze <>2024-12-29 12:27:28 +0000
commit1890d262ab9bcbf87c2f69f6e2156d61ad9fbf2f (patch)
treeea808f1ae9b32c375d8dd9a547db249833960ab5
parent2a801fad92517eff7ef232be41af1b086dbb5a50 (diff)
downloadopenbsd-1890d262ab9bcbf87c2f69f6e2156d61ad9fbf2f.tar.gz
openbsd-1890d262ab9bcbf87c2f69f6e2156d61ad9fbf2f.tar.bz2
openbsd-1890d262ab9bcbf87c2f69f6e2156d61ad9fbf2f.zip
Prefer the constants EVP_CTRL_AEAD_* over EVP_CTRL_CCM_* and EVP_CTRL_GCM_*
because that's what OpenSSL 1.1 suggests. Even though that "unification" doesn't really simplify anything but is more akin to repainting the bikeshed, at least it doesn't cause any additional harm, so keeping recommendations consistent may reduce the risk of code breaking in the future. Provide an example of decryption with AES-CCM in addition to the example of encryption already in place, because there are a number of subtle and non-obvious differences that users have to pay attention to. Both ideas originally suggested by tb@.
-rw-r--r--src/lib/libcrypto/man/EVP_aes_128_ccm.3105
-rw-r--r--src/lib/libcrypto/man/EVP_aes_128_gcm.317
2 files changed, 110 insertions, 12 deletions
diff --git a/src/lib/libcrypto/man/EVP_aes_128_ccm.3 b/src/lib/libcrypto/man/EVP_aes_128_ccm.3
index c1faf9e325..e9023a5b67 100644
--- a/src/lib/libcrypto/man/EVP_aes_128_ccm.3
+++ b/src/lib/libcrypto/man/EVP_aes_128_ccm.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: EVP_aes_128_ccm.3,v 1.4 2024/12/23 22:06:00 jsg Exp $ 1.\" $OpenBSD: EVP_aes_128_ccm.3,v 1.5 2024/12/29 12:27:28 schwarze Exp $
2.\" full merge up to: 2.\" full merge up to:
3.\" OpenSSL EVP_EncryptInit.pod 0874d7f2 Oct 11 13:13:47 2022 +0100 3.\" OpenSSL EVP_EncryptInit.pod 0874d7f2 Oct 11 13:13:47 2022 +0100
4.\" OpenSSL EVP_aes.pod a1ec85c1 Apr 21 10:49:12 2020 +0100 4.\" OpenSSL EVP_aes.pod a1ec85c1 Apr 21 10:49:12 2020 +0100
@@ -67,7 +67,7 @@
67.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 67.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
68.\" OF THE POSSIBILITY OF SUCH DAMAGE. 68.\" OF THE POSSIBILITY OF SUCH DAMAGE.
69.\" 69.\"
70.Dd $Mdocdate: December 23 2024 $ 70.Dd $Mdocdate: December 29 2024 $
71.Dt EVP_AES_128_CCM 3 71.Dt EVP_AES_128_CCM 3
72.Os 72.Os
73.Sh NAME 73.Sh NAME
@@ -105,6 +105,11 @@ For CCM mode ciphers, the behaviour of the EVP interface is subtly
105altered and several additional 105altered and several additional
106.Xr EVP_CIPHER_CTX_ctrl 3 106.Xr EVP_CIPHER_CTX_ctrl 3
107operations are required to function correctly. 107operations are required to function correctly.
108Some of the
109.Dv EVP_CTRL_CCM_*
110control commands are older aliases for corresponding
111.Dv EVP_CTRL_AEAD_*
112constants as indicated below.
108.Pp 113.Pp
109The less cumbersome and less error-prone 114The less cumbersome and less error-prone
110.Xr EVP_AEAD_CTX_new 3 115.Xr EVP_AEAD_CTX_new 3
@@ -145,7 +150,7 @@ for the length field reduces des maximum size of messages that can be sent,
145but in return allows transmitting more messages with the same key. 150but in return allows transmitting more messages with the same key.
146It is an error to pass less than 2 or more than the default value of 8 for 151It is an error to pass less than 2 or more than the default value of 8 for
147.Fa arg . 152.Fa arg .
148.It Dv EVP_CTRL_CCM_SET_IVLEN 153.It Dv EVP_CTRL_AEAD_SET_IVLEN Pq == Dv EVP_CTRL_CCM_SET_IVLEN
149Set the size of the nonce to 154Set the size of the nonce to
150.Fa arg 155.Fa arg
151bytes and the size 156bytes and the size
@@ -170,7 +175,7 @@ argument, with the other two arguments pointing to the desired AES key
170and to the desired nonce. 175and to the desired nonce.
171.Ss Encryption controls 176.Ss Encryption controls
172.Bl -tag -width Ds 177.Bl -tag -width Ds
173.It Dv EVP_CTRL_CCM_SET_TAG 178.It Dv EVP_CTRL_AEAD_SET_TAG Pq == Dv EVP_CTRL_CCM_SET_TAG
174If the 179If the
175.Fa ptr 180.Fa ptr
176argument is 181argument is
@@ -195,7 +200,7 @@ when
195is not configured for encrypting. 200is not configured for encrypting.
196Issuing this control command when an encryption key is already configured 201Issuing this control command when an encryption key is already configured
197silently causes data corruption. 202silently causes data corruption.
198.It Dv EVP_CTRL_CCM_GET_TAG 203.It Dv EVP_CTRL_AEAD_GET_TAG Pq == Dv EVP_CTRL_CCM_GET_TAG
199Store the 204Store the
200.Fa arg 205.Fa arg
201bytes of the tag in the memory provided by the caller starting at 206bytes of the tag in the memory provided by the caller starting at
@@ -248,7 +253,7 @@ argument set to
248.Dv NULL . 253.Dv NULL .
249.Ss Decryption controls 254.Ss Decryption controls
250.Bl -tag -width Ds 255.Bl -tag -width Ds
251.It Dv EVP_CTRL_CCM_SET_TAG 256.It Dv EVP_CTRL_AEAD_SET_TAG Pq == Dv EVP_CTRL_CCM_SET_TAG
252If the 257If the
253.Fa ptr 258.Fa ptr
254argument is not 259argument is not
@@ -456,6 +461,94 @@ putchar('\en');
456 461
457EVP_CIPHER_CTX_free(ctx); 462EVP_CIPHER_CTX_free(ctx);
458.Ed 463.Ed
464.Pp
465The reverse operation for the same test vector,
466i.e. decrypting and comparing the digest,
467is implemented by the following code.
468.Pp
469The variable declarations and definitions up to the call of
470.Xr EVP_CIPHER_CTX_new 3
471are the same as above.
472The chief differences are:
473.Bl -dash -width 1n -compact
474.It
475The tag is not part of the output,
476so the total output length is shorter.
477.It
478No
479.Xr memcmp 3
480of the tag takes place.
481Instead, the control command
482.Dv EVP_CTRL_CCM_SET_TAG
483requires the tag that is going to be verified as an additional argument.
484.It
485While
486.Xr EVP_EncryptFinal 3
487is an optional no-op,
488.Xr EVP_DecryptFinal 3
489is not called and would fail.
490.El
491.Bd -literal -offset indent
492const int out_len = aad_len + text_len;
493
494/* configuration */
495ctx = EVP_CIPHER_CTX_new();
496if (ctx == NULL)
497 err(1, "EVP_CIPHER_CTX_new");
498
499if (EVP_DecryptInit(ctx, EVP_aes_128_ccm(), NULL, NULL) != 1)
500 err(1, "EVP_DecryptInit(NULL)");
501
502if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, size_len, NULL) <= 0)
503 err(1, "EVP_CTRL_CCM_SET_L(%d)", size_len);
504
505if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG,
506 tag_len, (void *)wanted_tag) <= 0)
507 err(1, "EVP_CTRL_CCM_SET_TAG(%d)", tag_len);
508
509/* process input data */
510if (EVP_DecryptInit(ctx, NULL, key, nonce) != 1)
511 err(1, "EVP_DecryptInit(key, nonce)");
512
513if (EVP_DecryptUpdate(ctx, NULL, &irv, NULL, text_len) != 1)
514 err(1, "EVP_DecryptUpdate(len = %d)", text_len);
515if (irv != text_len)
516 errx(1, "text length: want %d, got %d", text_len, irv);
517
518irv = -1;
519if (EVP_DecryptUpdate(ctx, NULL, &irv, aad, aad_len) != 1)
520 err(1, "EVP_DecryptUpdate(AAD)");
521memcpy(out_p, aad, aad_len);
522out_p += aad_len;
523
524irv = -1;
525if (EVP_DecryptUpdate(ctx, out_p, &irv, ciphertext, text_len) != 1)
526 err(1, "EVP_DecryptUpdate(ciphertext)");
527if (irv != text_len)
528 errx(1, "text_len: want %d, got %d", text_len, irv);
529out_p += irv;
530
531/* Do not call EVP_DecryptFinal(3); it would fail and do nothing. */
532
533/* check output data */
534if (memcmp(out_buf + aad_len, plaintext, text_len) != 0)
535 errx(1, "plaintext mismatch");
536if (out_p != out_end)
537 errx(1, "end of output: want %p, got %p", out_end, out_p);
538
539printf("Total packet length = %d.", out_len);
540printf(" [Decrypted and Authenticated Input]");
541for (i = 0; i < out_len; i++) {
542 if (i % 16 == 0)
543 printf("\n ");
544 if (i % 4 == 0)
545 putchar(' ');
546 printf(" %02X", out_buf[i]);
547}
548putchar('\n');
549
550EVP_CIPHER_CTX_free(ctx);
551.Ed
459.Sh SEE ALSO 552.Sh SEE ALSO
460.Xr AES_encrypt 3 , 553.Xr AES_encrypt 3 ,
461.Xr evp 3 , 554.Xr evp 3 ,
diff --git a/src/lib/libcrypto/man/EVP_aes_128_gcm.3 b/src/lib/libcrypto/man/EVP_aes_128_gcm.3
index 1e15cb79aa..53c41ea162 100644
--- a/src/lib/libcrypto/man/EVP_aes_128_gcm.3
+++ b/src/lib/libcrypto/man/EVP_aes_128_gcm.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: EVP_aes_128_gcm.3,v 1.1 2024/12/17 18:11:44 schwarze Exp $ 1.\" $OpenBSD: EVP_aes_128_gcm.3,v 1.2 2024/12/29 12:27:28 schwarze Exp $
2.\" full merge up to: 2.\" full merge up to:
3.\" OpenSSL EVP_EncryptInit.pod 0874d7f2 Oct 11 13:13:47 2022 +0100 3.\" OpenSSL EVP_EncryptInit.pod 0874d7f2 Oct 11 13:13:47 2022 +0100
4.\" OpenSSL EVP_aes.pod a1ec85c1 Apr 21 10:49:12 2020 +0100 4.\" OpenSSL EVP_aes.pod a1ec85c1 Apr 21 10:49:12 2020 +0100
@@ -67,7 +67,7 @@
67.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 67.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
68.\" OF THE POSSIBILITY OF SUCH DAMAGE. 68.\" OF THE POSSIBILITY OF SUCH DAMAGE.
69.\" 69.\"
70.Dd $Mdocdate: December 17 2024 $ 70.Dd $Mdocdate: December 29 2024 $
71.Dt EVP_AES_128_GCM 3 71.Dt EVP_AES_128_GCM 3
72.Os 72.Os
73.Sh NAME 73.Sh NAME
@@ -97,6 +97,11 @@ For GCM mode ciphers, the behaviour of the EVP interface is subtly
97altered and several additional 97altered and several additional
98.Xr EVP_CIPHER_CTX_ctrl 3 98.Xr EVP_CIPHER_CTX_ctrl 3
99operations are required to function correctly. 99operations are required to function correctly.
100Some of the
101.Dv EVP_CTRL_GCM_*
102control commands are older aliases for corresponding
103.Dv EVP_CTRL_AEAD_*
104constants as indicated below.
100.Pp 105.Pp
101To avoid using the cumbersome and error-prone API documented 106To avoid using the cumbersome and error-prone API documented
102in the present manual page, consider using the functions documented in 107in the present manual page, consider using the functions documented in
@@ -109,7 +114,7 @@ instead.
109.\" EVP_GCM_TLS_EXPLICIT_IV_LEN and EVP_GCM_TLS_TAG_LEN (used internally 114.\" EVP_GCM_TLS_EXPLICIT_IV_LEN and EVP_GCM_TLS_TAG_LEN (used internally
110.\" only in aes_gcm_tls_cipher(), which is unused) 115.\" only in aes_gcm_tls_cipher(), which is unused)
111.Bl -tag -width Ds 116.Bl -tag -width Ds
112.It Dv EVP_CTRL_GCM_SET_IVLEN 117.It Dv EVP_CTRL_AEAD_SET_IVLEN Pq == Dv EVP_CTRL_GCM_SET_IVLEN
113Set the length of the initialization vector to 118Set the length of the initialization vector to
114.Fa arg 119.Fa arg
115bytes; the 120bytes; the
@@ -124,7 +129,7 @@ Using this control command is discouraged because section 5.2.1.1 of the
124specification explicitly recommends that implementations of GCM restrict 129specification explicitly recommends that implementations of GCM restrict
125support to the default IV length of 12 bytes for interoperability, 130support to the default IV length of 12 bytes for interoperability,
126efficiency, and simplicity of design. 131efficiency, and simplicity of design.
127.It Dv EVP_CTRL_GCM_SET_IV_FIXED 132.It Dv EVP_CTRL_AEAD_SET_IV_FIXED Pq == Dv EVP_CTRL_GCM_SET_IV_FIXED
128Usually, \-1 is passed for 133Usually, \-1 is passed for
129.Fa arg . 134.Fa arg .
130In that case, the complete initialization vector is copied from 135In that case, the complete initialization vector is copied from
@@ -158,7 +163,7 @@ that may have been configured earlier and always operates on the
158last eight bytes of the initialization vector. 163last eight bytes of the initialization vector.
159It is an error to issue this command 164It is an error to issue this command
160when no key or no initialization vector is set. 165when no key or no initialization vector is set.
161.It Dv EVP_CTRL_GCM_GET_TAG 166.It Dv EVP_CTRL_AEAD_GET_TAG Pq == Dv EVP_CTRL_GCM_GET_TAG
162Write 167Write
163.Fa arg 168.Fa arg
164bytes of the tag value to the location pointed to by 169bytes of the tag value to the location pointed to by
@@ -194,7 +199,7 @@ When default lengths are in use, pass 8 for
194In that case, this control command sets the invocation field. 199In that case, this control command sets the invocation field.
195It is an error to issue this command 200It is an error to issue this command
196when no key or no initialization vector is set, or when encrypting. 201when no key or no initialization vector is set, or when encrypting.
197.It Dv EVP_CTRL_GCM_SET_TAG 202.It Dv EVP_CTRL_AEAD_SET_TAG Pq == Dv EVP_CTRL_GCM_SET_TAG
198Set the expected tag to the 203Set the expected tag to the
199.Fa arg 204.Fa arg
200bytes located at 205bytes located at