diff options
-rw-r--r-- | src/lib/libcrypto/evp/e_chacha.c | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/src/lib/libcrypto/evp/e_chacha.c b/src/lib/libcrypto/evp/e_chacha.c index b63f586bba..bc496241e6 100644 --- a/src/lib/libcrypto/evp/e_chacha.c +++ b/src/lib/libcrypto/evp/e_chacha.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: e_chacha.c,v 1.5 2014/08/04 04:16:11 miod Exp $ */ | 1 | /* $OpenBSD: e_chacha.c,v 1.6 2020/01/26 02:39:58 tb Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> |
4 | * | 4 | * |
@@ -34,8 +34,9 @@ static const EVP_CIPHER chacha20_cipher = { | |||
34 | .nid = NID_chacha20, | 34 | .nid = NID_chacha20, |
35 | .block_size = 1, | 35 | .block_size = 1, |
36 | .key_len = 32, | 36 | .key_len = 32, |
37 | .iv_len = 8, | 37 | .iv_len = 16, /* OpenSSL has 8 byte counter followed by 8 byte iv */ |
38 | .flags = EVP_CIPH_STREAM_CIPHER, | 38 | .flags = EVP_CIPH_STREAM_CIPHER | EVP_CIPH_ALWAYS_CALL_INIT | |
39 | EVP_CIPH_CUSTOM_IV, | ||
39 | .init = chacha_init, | 40 | .init = chacha_init, |
40 | .do_cipher = chacha_cipher, | 41 | .do_cipher = chacha_cipher, |
41 | .ctx_size = sizeof(ChaCha_ctx) | 42 | .ctx_size = sizeof(ChaCha_ctx) |
@@ -51,10 +52,16 @@ static int | |||
51 | chacha_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 52 | chacha_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
52 | const unsigned char *iv, int enc) | 53 | const unsigned char *iv, int enc) |
53 | { | 54 | { |
54 | ChaCha_set_key((ChaCha_ctx *)ctx->cipher_data, key, | 55 | if (key != NULL) |
55 | EVP_CIPHER_CTX_key_length(ctx) * 8); | 56 | ChaCha_set_key((ChaCha_ctx *)ctx->cipher_data, key, |
56 | if (iv != NULL) | 57 | EVP_CIPHER_CTX_key_length(ctx) * 8); |
57 | ChaCha_set_iv((ChaCha_ctx *)ctx->cipher_data, iv, NULL); | 58 | if (iv != NULL) { |
59 | const unsigned char *openssl_iv = iv + 8; | ||
60 | const unsigned char *counter = iv; | ||
61 | |||
62 | ChaCha_set_iv((ChaCha_ctx *)ctx->cipher_data, openssl_iv, | ||
63 | counter); | ||
64 | } | ||
58 | return 1; | 65 | return 1; |
59 | } | 66 | } |
60 | 67 | ||