Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a NULL dereference in chacha20_poly1305_init_key() #1049

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 13 additions & 9 deletions crypto/evp/e_chacha20_poly1305.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ static int chacha20_poly1305_init_key(EVP_CIPHER_CTX *ctx,
const unsigned char *iv, int enc)
{
EVP_CHACHA_AEAD_CTX *actx = aead_data(ctx);
unsigned char temp[CHACHA_CTR_SIZE];

if (!inkey && !iv)
return 1;
Expand All @@ -216,16 +215,21 @@ static int chacha20_poly1305_init_key(EVP_CIPHER_CTX *ctx,
actx->mac_inited = 0;
actx->tls_payload_length = NO_TLS_PAYLOAD_LENGTH;

/* pad on the left */
memset(temp, 0, sizeof(temp));
if (actx->nonce_len <= CHACHA_CTR_SIZE)
memcpy(temp + CHACHA_CTR_SIZE - actx->nonce_len, iv, actx->nonce_len);
if (iv != NULL) {
unsigned char temp[CHACHA_CTR_SIZE] = { 0 };

chacha_init_key(ctx, inkey, temp, enc);
/* pad on the left */
if (actx->nonce_len <= CHACHA_CTR_SIZE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of reindenting everything, a more minimal change would be:

if (iv == NULL) {
  chacha_init_key(ctx, inkey, NULL, enc);
  return 1;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but I wanted to limit the scope of temp. If smaller diff is preferred I can change this :)

memcpy(temp + CHACHA_CTR_SIZE - actx->nonce_len, iv, actx->nonce_len);

actx->nonce[0] = actx->key.counter[1];
actx->nonce[1] = actx->key.counter[2];
actx->nonce[2] = actx->key.counter[3];
chacha_init_key(ctx, inkey, temp, enc);

actx->nonce[0] = actx->key.counter[1];
actx->nonce[1] = actx->key.counter[2];
actx->nonce[2] = actx->key.counter[3];
} else {
chacha_init_key(ctx, inkey, NULL, enc);
}

return 1;
}
Expand Down