diff options
author | tb <> | 2023-09-28 11:29:10 +0000 |
---|---|---|
committer | tb <> | 2023-09-28 11:29:10 +0000 |
commit | 92da04b3fa82724d71f293483fb9284e6da2fa37 (patch) | |
tree | 3522ab66513de919b4cb23f4f9a24a55b2df8066 /src/lib/libcrypto/evp/e_aes.c | |
parent | a95a07cea06fc8d354ce9de8b80ef4f91fbfe23c (diff) | |
download | openbsd-92da04b3fa82724d71f293483fb9284e6da2fa37.tar.gz openbsd-92da04b3fa82724d71f293483fb9284e6da2fa37.tar.bz2 openbsd-92da04b3fa82724d71f293483fb9284e6da2fa37.zip |
Fix EVP_CIPHER_CTX_iv_length()
In today's episode of "curly nonsense from EVP land" we deal with a quite
harmless oversight and a not too bad suboptimal fix, relatively speaking.
At some point EVP_CIPHER_{CCM,GCM}_SET_IVLEN was added. It modified some
object hanging off of EVP_CIPHER. However, EVP_CIPHER_CTX_iv_length() wasn't
taught about this and kept returning the hardcoded default value on the
EVP_CIPHER. Once it transpired that a doc fix isn't going to cut it, this
was fixed. And of course it's easy to fix: you only have to dive through
about three layers of EVP, test and set a flag and handle a control in a
couple methods.
The upstream fix was done poorly and we begrudgingly have to match the API:
the caller is expected to pass a raw pointer next to a 0 length along with
EVP_CIPHER_GET_IV_LENGTH and the control handler goes *(int *)ptr = length
in full YOLO mode. That's never going to be an issue because of course the
caller will always pass a properly aligned pointer backing a sufficient
amount of memory. Yes, unlikely to be a real issue, but it could have been
done with proper semantics and checks without complicating the code. But
why do I even bother to complain? We're used to this.
Of note here is that there was some pushback painting other corners of a
bikeshed until the reviewer gave up with a resigned
That kind of changes the semantics and is one extra complexity level,
but [shrug] ok...
Anyway, the reason this matters now after so many years is that rust-openssl
has an assert, notably added in a +758 -84 commit with the awesome message
"Docs" that gets triggered by recent tests added to py-cryptography.
Thanks to Alex Gaynor for reporting this. Let me take the opportunity to
point out that pyca contributed to improve rust-openssl, in particular its
libressl support, quite a bit. That's much appreciated and very noticeable.
Regress coverage to follow in subsequent commits.
Based on OpenSSL PR #9499 and issue #8330.
ok beck jsing
PS: A few macros were kept internal for now to avoid impact on the release
cycle that is about to finish. They will be exposed after release.
Diffstat (limited to 'src/lib/libcrypto/evp/e_aes.c')
-rw-r--r-- | src/lib/libcrypto/evp/e_aes.c | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/src/lib/libcrypto/evp/e_aes.c b/src/lib/libcrypto/evp/e_aes.c index 3d3b1a9d6c..3d357f0119 100644 --- a/src/lib/libcrypto/evp/e_aes.c +++ b/src/lib/libcrypto/evp/e_aes.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: e_aes.c,v 1.53 2023/07/07 19:37:53 beck Exp $ */ | 1 | /* $OpenBSD: e_aes.c,v 1.54 2023/09/28 11:29:10 tb Exp $ */ |
2 | /* ==================================================================== | 2 | /* ==================================================================== |
3 | * Copyright (c) 2001-2011 The OpenSSL Project. All rights reserved. | 3 | * Copyright (c) 2001-2011 The OpenSSL Project. All rights reserved. |
4 | * | 4 | * |
@@ -1305,7 +1305,11 @@ aes_gcm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |||
1305 | gctx->tls_aad_len = -1; | 1305 | gctx->tls_aad_len = -1; |
1306 | return 1; | 1306 | return 1; |
1307 | 1307 | ||
1308 | case EVP_CTRL_GCM_SET_IVLEN: | 1308 | case EVP_CTRL_AEAD_GET_IVLEN: |
1309 | *(int *)ptr = gctx->ivlen; | ||
1310 | return 1; | ||
1311 | |||
1312 | case EVP_CTRL_AEAD_SET_IVLEN: | ||
1309 | if (arg <= 0) | 1313 | if (arg <= 0) |
1310 | return 0; | 1314 | return 0; |
1311 | /* Allocate memory for IV if needed */ | 1315 | /* Allocate memory for IV if needed */ |
@@ -1631,6 +1635,7 @@ aes_gcm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |||
1631 | 1635 | ||
1632 | #define CUSTOM_FLAGS \ | 1636 | #define CUSTOM_FLAGS \ |
1633 | ( EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV | \ | 1637 | ( EVP_CIPH_FLAG_DEFAULT_ASN1 | EVP_CIPH_CUSTOM_IV | \ |
1638 | EVP_CIPH_FLAG_CUSTOM_IV_LENGTH | \ | ||
1634 | EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT | \ | 1639 | EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT | \ |
1635 | EVP_CIPH_CTRL_INIT | EVP_CIPH_CUSTOM_COPY ) | 1640 | EVP_CIPH_CTRL_INIT | EVP_CIPH_CUSTOM_COPY ) |
1636 | 1641 | ||
@@ -1968,7 +1973,11 @@ aes_ccm_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |||
1968 | cctx->len_set = 0; | 1973 | cctx->len_set = 0; |
1969 | return 1; | 1974 | return 1; |
1970 | 1975 | ||
1971 | case EVP_CTRL_CCM_SET_IVLEN: | 1976 | case EVP_CTRL_AEAD_GET_IVLEN: |
1977 | *(int *)ptr = 15 - cctx->L; | ||
1978 | return 1; | ||
1979 | |||
1980 | case EVP_CTRL_AEAD_SET_IVLEN: | ||
1972 | arg = 15 - arg; | 1981 | arg = 15 - arg; |
1973 | 1982 | ||
1974 | case EVP_CTRL_CCM_SET_L: | 1983 | case EVP_CTRL_CCM_SET_L: |