blob: 9dda1b0c2e2987c6e8664a4f9a5f10a856b80969 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <openssl/aead.h>
16
17#include <string.h>
18
19#include <openssl/chacha.h>
20#include <openssl/cipher.h>
21#include <openssl/err.h>
22#include <openssl/mem.h>
23#include <openssl/poly1305.h>
24
25#include "internal.h"
26
27
28#define POLY1305_TAG_LEN 16
Kenny Root03bcf612015-11-05 20:20:27 +000029#define CHACHA20_NONCE_LEN 8
Adam Langleyd9e397b2015-01-22 14:27:53 -080030
31struct aead_chacha20_poly1305_ctx {
32 unsigned char key[32];
33 unsigned char tag_len;
34};
35
36static int aead_chacha20_poly1305_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
37 size_t key_len, size_t tag_len) {
38 struct aead_chacha20_poly1305_ctx *c20_ctx;
39
40 if (tag_len == 0) {
41 tag_len = POLY1305_TAG_LEN;
42 }
43
44 if (tag_len > POLY1305_TAG_LEN) {
Kenny Rootb8494592015-09-25 02:29:14 +000045 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
Adam Langleyd9e397b2015-01-22 14:27:53 -080046 return 0;
47 }
48
49 if (key_len != sizeof(c20_ctx->key)) {
50 return 0; /* internal error - EVP_AEAD_CTX_init should catch this. */
51 }
52
53 c20_ctx = OPENSSL_malloc(sizeof(struct aead_chacha20_poly1305_ctx));
54 if (c20_ctx == NULL) {
55 return 0;
56 }
57
58 memcpy(c20_ctx->key, key, key_len);
59 c20_ctx->tag_len = tag_len;
60 ctx->aead_state = c20_ctx;
61
62 return 1;
63}
64
65static void aead_chacha20_poly1305_cleanup(EVP_AEAD_CTX *ctx) {
66 struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
67 OPENSSL_cleanse(c20_ctx->key, sizeof(c20_ctx->key));
68 OPENSSL_free(c20_ctx);
69}
70
Kenny Root03bcf612015-11-05 20:20:27 +000071static void poly1305_update_with_length(poly1305_state *poly1305,
72 const uint8_t *data, size_t data_len) {
73 size_t j = data_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -080074 uint8_t length_bytes[8];
75 unsigned i;
76
77 for (i = 0; i < sizeof(length_bytes); i++) {
Kenny Root03bcf612015-11-05 20:20:27 +000078 length_bytes[i] = j;
79 j >>= 8;
Adam Langleyd9e397b2015-01-22 14:27:53 -080080 }
81
Kenny Root03bcf612015-11-05 20:20:27 +000082 CRYPTO_poly1305_update(poly1305, data, data_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -080083 CRYPTO_poly1305_update(poly1305, length_bytes, sizeof(length_bytes));
84}
85
86#if defined(__arm__)
87#define ALIGNED __attribute__((aligned(16)))
88#else
89#define ALIGNED
90#endif
91
Kenny Root03bcf612015-11-05 20:20:27 +000092static int aead_chacha20_poly1305_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
93 size_t *out_len, size_t max_out_len,
94 const uint8_t *nonce, size_t nonce_len,
95 const uint8_t *in, size_t in_len,
96 const uint8_t *ad, size_t ad_len) {
Adam Langleyfdeb4882015-10-30 13:15:30 -070097 const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
Kenny Root03bcf612015-11-05 20:20:27 +000098 uint8_t poly1305_key[32] ALIGNED;
99 poly1305_state poly1305;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800100 const uint64_t in_len_64 = in_len;
101
Kenny Root03bcf612015-11-05 20:20:27 +0000102 /* The underlying ChaCha implementation may not overflow the block
103 * counter into the second counter word. Therefore we disallow
Adam Langleyd9e397b2015-01-22 14:27:53 -0800104 * individual operations that work on more than 256GB at a time.
105 * |in_len_64| is needed because, on 32-bit platforms, size_t is only
106 * 32-bits and this produces a warning because it's always false.
107 * Casting to uint64_t inside the conditional is not sufficient to stop
108 * the warning. */
109 if (in_len_64 >= (1ull << 32) * 64 - 64) {
Kenny Rootb8494592015-09-25 02:29:14 +0000110 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800111 return 0;
112 }
113
114 if (in_len + c20_ctx->tag_len < in_len) {
Kenny Rootb8494592015-09-25 02:29:14 +0000115 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800116 return 0;
117 }
118
119 if (max_out_len < in_len + c20_ctx->tag_len) {
Kenny Rootb8494592015-09-25 02:29:14 +0000120 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800121 return 0;
122 }
123
Kenny Root03bcf612015-11-05 20:20:27 +0000124 if (nonce_len != CHACHA20_NONCE_LEN) {
125 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_IV_TOO_LARGE);
126 return 0;
127 }
128
129 memset(poly1305_key, 0, sizeof(poly1305_key));
130 CRYPTO_chacha_20(poly1305_key, poly1305_key, sizeof(poly1305_key),
131 c20_ctx->key, nonce, 0);
132
133 CRYPTO_poly1305_init(&poly1305, poly1305_key);
134 poly1305_update_with_length(&poly1305, ad, ad_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800135 CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1);
Kenny Root03bcf612015-11-05 20:20:27 +0000136 poly1305_update_with_length(&poly1305, out, in_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800137
138 uint8_t tag[POLY1305_TAG_LEN] ALIGNED;
Kenny Root03bcf612015-11-05 20:20:27 +0000139 CRYPTO_poly1305_finish(&poly1305, tag);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800140 memcpy(out + in_len, tag, c20_ctx->tag_len);
141 *out_len = in_len + c20_ctx->tag_len;
142 return 1;
143}
144
Kenny Root03bcf612015-11-05 20:20:27 +0000145static int aead_chacha20_poly1305_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
146 size_t *out_len, size_t max_out_len,
147 const uint8_t *nonce, size_t nonce_len,
148 const uint8_t *in, size_t in_len,
149 const uint8_t *ad, size_t ad_len) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800150 const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
Kenny Root03bcf612015-11-05 20:20:27 +0000151 uint8_t mac[POLY1305_TAG_LEN];
152 uint8_t poly1305_key[32] ALIGNED;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800153 size_t plaintext_len;
Kenny Root03bcf612015-11-05 20:20:27 +0000154 poly1305_state poly1305;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800155 const uint64_t in_len_64 = in_len;
156
157 if (in_len < c20_ctx->tag_len) {
Kenny Rootb8494592015-09-25 02:29:14 +0000158 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800159 return 0;
160 }
161
Kenny Root03bcf612015-11-05 20:20:27 +0000162 /* The underlying ChaCha implementation may not overflow the block
163 * counter into the second counter word. Therefore we disallow
Adam Langleyd9e397b2015-01-22 14:27:53 -0800164 * individual operations that work on more than 256GB at a time.
165 * |in_len_64| is needed because, on 32-bit platforms, size_t is only
166 * 32-bits and this produces a warning because it's always false.
167 * Casting to uint64_t inside the conditional is not sufficient to stop
168 * the warning. */
169 if (in_len_64 >= (1ull << 32) * 64 - 64) {
Kenny Rootb8494592015-09-25 02:29:14 +0000170 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800171 return 0;
172 }
173
Kenny Root03bcf612015-11-05 20:20:27 +0000174 if (nonce_len != CHACHA20_NONCE_LEN) {
175 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_IV_TOO_LARGE);
176 return 0;
177 }
178
Adam Langleyd9e397b2015-01-22 14:27:53 -0800179 plaintext_len = in_len - c20_ctx->tag_len;
Kenny Root03bcf612015-11-05 20:20:27 +0000180
181 if (max_out_len < plaintext_len) {
182 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
183 return 0;
184 }
185
186 memset(poly1305_key, 0, sizeof(poly1305_key));
187 CRYPTO_chacha_20(poly1305_key, poly1305_key, sizeof(poly1305_key),
188 c20_ctx->key, nonce, 0);
189
190 CRYPTO_poly1305_init(&poly1305, poly1305_key);
191 poly1305_update_with_length(&poly1305, ad, ad_len);
192 poly1305_update_with_length(&poly1305, in, plaintext_len);
193 CRYPTO_poly1305_finish(&poly1305, mac);
194
195 if (CRYPTO_memcmp(mac, in + plaintext_len, c20_ctx->tag_len) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000196 OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800197 return 0;
198 }
199
200 CRYPTO_chacha_20(out, in, plaintext_len, c20_ctx->key, nonce, 1);
201 *out_len = plaintext_len;
202 return 1;
203}
204
205static const EVP_AEAD aead_chacha20_poly1305 = {
206 32, /* key len */
Kenny Root03bcf612015-11-05 20:20:27 +0000207 CHACHA20_NONCE_LEN, /* nonce len */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800208 POLY1305_TAG_LEN, /* overhead */
209 POLY1305_TAG_LEN, /* max tag length */
Adam Langleye9ada862015-05-11 17:20:37 -0700210 aead_chacha20_poly1305_init,
211 NULL, /* init_with_direction */
212 aead_chacha20_poly1305_cleanup,
213 aead_chacha20_poly1305_seal,
214 aead_chacha20_poly1305_open,
215 NULL, /* get_rc4_state */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800216};
217
Adam Langleyfdeb4882015-10-30 13:15:30 -0700218const EVP_AEAD *EVP_aead_chacha20_poly1305(void) {
Kenny Root03bcf612015-11-05 20:20:27 +0000219 return &aead_chacha20_poly1305;
Adam Langleyfdeb4882015-10-30 13:15:30 -0700220}