diff options
author | vnugent <public@vaughnnugent.com> | 2024-08-05 18:01:03 -0400 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2024-08-05 18:01:03 -0400 |
commit | e949ae5aa1fd25d4d11fe31e30b7d82ae7778dc2 (patch) | |
tree | dd538646175a6d51b52eb0cf1bd1b25920fa05eb /src/providers/openssl.c | |
parent | a60a3e1ca1d99d655c0cfc96e3952c371e8a8677 (diff) |
fix: Find and fix openssl encryption bug
Diffstat (limited to 'src/providers/openssl.c')
-rw-r--r-- | src/providers/openssl.c | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/src/providers/openssl.c b/src/providers/openssl.c index 7f405ef..90f2fa9 100644 --- a/src/providers/openssl.c +++ b/src/providers/openssl.c @@ -2,7 +2,7 @@ * Copyright (c) 2024 Vaughn Nugent * * Package: noscrypt -* File: impl/openssl.c +* File: providers/openssl.c * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -290,7 +290,10 @@ DEBUG_ASSERT2(ncSpanGetSize(output) <= ncSpanGetSizeC(input), "Output buffer must be equal or larger than the input buffer"); DEBUG_ASSERT(cipher != NULL); - result = CSTATUS_FAIL; + DEBUG_ASSERT((uint32_t)EVP_CIPHER_get_key_length(cipher) == ncSpanGetSizeC(key)); + DEBUG_ASSERT((uint32_t)EVP_CIPHER_iv_length(cipher) == ncSpanGetSizeC(iv)); + + result = CSTATUS_FAIL; ctx = EVP_CIPHER_CTX_new(); @@ -356,13 +359,25 @@ { cstatus_t result; EVP_CIPHER* cipher; + uint8_t chaChaIv[CHACHA_NONCE_SIZE + 4]; cspan_t keySpan, nonceSpan, inputSpan; span_t outputSpan; result = CSTATUS_FAIL; + /* + * RFC 7539 ChaCha20 requires a 16 byte initialization vector. A + * counter value is preprended to the nonce to make up the 16 byte + * size. + * + * The counter is always set to 0 for the nonce. + */ + + ncCryptoSecureZero(chaChaIv, sizeof(chaChaIv)); + MEMMOV(chaChaIv + 4, nonce, CHACHA_NONCE_SIZE); + ncSpanInitC(&keySpan, key, CHACHA_KEY_SIZE); - ncSpanInitC(&nonceSpan, nonce, CHACHA_NONCE_SIZE); + ncSpanInitC(&nonceSpan, chaChaIv, sizeof(chaChaIv)); ncSpanInitC(&inputSpan, input, dataLen); ncSpanInit(&outputSpan, output, dataLen); |