| 1 |
/* 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 |
#include "../internal.h" |
| 27 |
|
| 28 |
|
| 29 |
#define POLY1305_TAG_LEN 16 |
| 30 |
|
| 31 |
struct aead_chacha20_poly1305_ctx { |
| 32 |
unsigned char key[32]; |
| 33 |
unsigned char tag_len; |
| 34 |
}; |
| 35 |
|
| 36 |
static 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) { |
| 45 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 46 |
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 |
|
| 65 |
static 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 |
|
| 71 |
static void poly1305_update_length(poly1305_state *poly1305, size_t data_len) { |
| 72 |
uint8_t length_bytes[8]; |
| 73 |
unsigned i; |
| 74 |
|
| 75 |
for (i = 0; i < sizeof(length_bytes); i++) { |
| 76 |
length_bytes[i] = data_len; |
| 77 |
data_len >>= 8; |
| 78 |
} |
| 79 |
|
| 80 |
CRYPTO_poly1305_update(poly1305, length_bytes, sizeof(length_bytes)); |
| 81 |
} |
| 82 |
|
| 83 |
typedef void (*aead_poly1305_update)(poly1305_state *ctx, const uint8_t *ad, |
| 84 |
size_t ad_len, const uint8_t *ciphertext, |
| 85 |
size_t ciphertext_len); |
| 86 |
|
| 87 |
/* aead_poly1305 fills |tag| with the authentication tag for the given |
| 88 |
* inputs, using |update| to control the order and format that the inputs are |
| 89 |
* signed/authenticated. */ |
| 90 |
static void aead_poly1305(aead_poly1305_update update, |
| 91 |
uint8_t tag[POLY1305_TAG_LEN], |
| 92 |
const struct aead_chacha20_poly1305_ctx *c20_ctx, |
| 93 |
const uint8_t nonce[12], const uint8_t *ad, |
| 94 |
size_t ad_len, const uint8_t *ciphertext, |
| 95 |
size_t ciphertext_len) { |
| 96 |
alignas(16) uint8_t poly1305_key[32]; |
| 97 |
memset(poly1305_key, 0, sizeof(poly1305_key)); |
| 98 |
CRYPTO_chacha_20(poly1305_key, poly1305_key, sizeof(poly1305_key), |
| 99 |
c20_ctx->key, nonce, 0); |
| 100 |
poly1305_state ctx; |
| 101 |
CRYPTO_poly1305_init(&ctx, poly1305_key); |
| 102 |
update(&ctx, ad, ad_len, ciphertext, ciphertext_len); |
| 103 |
CRYPTO_poly1305_finish(&ctx, tag); |
| 104 |
} |
| 105 |
|
| 106 |
static int seal_impl(aead_poly1305_update poly1305_update, |
| 107 |
const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len, |
| 108 |
size_t max_out_len, const uint8_t nonce[12], |
| 109 |
const uint8_t *in, size_t in_len, const uint8_t *ad, |
| 110 |
size_t ad_len) { |
| 111 |
const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state; |
| 112 |
const uint64_t in_len_64 = in_len; |
| 113 |
|
| 114 |
/* |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow |
| 115 |
* individual operations that work on more than 256GB at a time. |
| 116 |
* |in_len_64| is needed because, on 32-bit platforms, size_t is only |
| 117 |
* 32-bits and this produces a warning because it's always false. |
| 118 |
* Casting to uint64_t inside the conditional is not sufficient to stop |
| 119 |
* the warning. */ |
| 120 |
if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) { |
| 121 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
if (in_len + c20_ctx->tag_len < in_len) { |
| 126 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
if (max_out_len < in_len + c20_ctx->tag_len) { |
| 131 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1); |
| 136 |
|
| 137 |
alignas(16) uint8_t tag[POLY1305_TAG_LEN]; |
| 138 |
aead_poly1305(poly1305_update, tag, c20_ctx, nonce, ad, ad_len, out, in_len); |
| 139 |
|
| 140 |
memcpy(out + in_len, tag, c20_ctx->tag_len); |
| 141 |
*out_len = in_len + c20_ctx->tag_len; |
| 142 |
return 1; |
| 143 |
} |
| 144 |
|
| 145 |
static int open_impl(aead_poly1305_update poly1305_update, |
| 146 |
const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len, |
| 147 |
size_t max_out_len, const uint8_t nonce[12], |
| 148 |
const uint8_t *in, size_t in_len, const uint8_t *ad, |
| 149 |
size_t ad_len) { |
| 150 |
const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state; |
| 151 |
size_t plaintext_len; |
| 152 |
const uint64_t in_len_64 = in_len; |
| 153 |
|
| 154 |
if (in_len < c20_ctx->tag_len) { |
| 155 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
/* |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow |
| 160 |
* individual operations that work on more than 256GB at a time. |
| 161 |
* |in_len_64| is needed because, on 32-bit platforms, size_t is only |
| 162 |
* 32-bits and this produces a warning because it's always false. |
| 163 |
* Casting to uint64_t inside the conditional is not sufficient to stop |
| 164 |
* the warning. */ |
| 165 |
if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) { |
| 166 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
plaintext_len = in_len - c20_ctx->tag_len; |
| 171 |
alignas(16) uint8_t tag[POLY1305_TAG_LEN]; |
| 172 |
aead_poly1305(poly1305_update, tag, c20_ctx, nonce, ad, ad_len, in, |
| 173 |
plaintext_len); |
| 174 |
if (CRYPTO_memcmp(tag, in + plaintext_len, c20_ctx->tag_len) != 0) { |
| 175 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
CRYPTO_chacha_20(out, in, plaintext_len, c20_ctx->key, nonce, 1); |
| 180 |
*out_len = plaintext_len; |
| 181 |
return 1; |
| 182 |
} |
| 183 |
|
| 184 |
static void poly1305_update_padded_16(poly1305_state *poly1305, |
| 185 |
const uint8_t *data, size_t data_len) { |
| 186 |
static const uint8_t padding[16] = { 0 }; /* Padding is all zeros. */ |
| 187 |
|
| 188 |
CRYPTO_poly1305_update(poly1305, data, data_len); |
| 189 |
if (data_len % 16 != 0) { |
| 190 |
CRYPTO_poly1305_update(poly1305, padding, sizeof(padding) - (data_len % 16)); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
static void poly1305_update(poly1305_state *ctx, const uint8_t *ad, |
| 195 |
size_t ad_len, const uint8_t *ciphertext, |
| 196 |
size_t ciphertext_len) { |
| 197 |
poly1305_update_padded_16(ctx, ad, ad_len); |
| 198 |
poly1305_update_padded_16(ctx, ciphertext, ciphertext_len); |
| 199 |
poly1305_update_length(ctx, ad_len); |
| 200 |
poly1305_update_length(ctx, ciphertext_len); |
| 201 |
} |
| 202 |
|
| 203 |
static int aead_chacha20_poly1305_seal(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 204 |
size_t *out_len, size_t max_out_len, |
| 205 |
const uint8_t *nonce, size_t nonce_len, |
| 206 |
const uint8_t *in, size_t in_len, |
| 207 |
const uint8_t *ad, size_t ad_len) { |
| 208 |
if (nonce_len != 12) { |
| 209 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 210 |
return 0; |
| 211 |
} |
| 212 |
return seal_impl(poly1305_update, ctx, out, out_len, max_out_len, nonce, in, |
| 213 |
in_len, ad, ad_len); |
| 214 |
} |
| 215 |
|
| 216 |
static int aead_chacha20_poly1305_open(const EVP_AEAD_CTX *ctx, uint8_t *out, |
| 217 |
size_t *out_len, size_t max_out_len, |
| 218 |
const uint8_t *nonce, size_t nonce_len, |
| 219 |
const uint8_t *in, size_t in_len, |
| 220 |
const uint8_t *ad, size_t ad_len) { |
| 221 |
if (nonce_len != 12) { |
| 222 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 223 |
return 0; |
| 224 |
} |
| 225 |
return open_impl(poly1305_update, ctx, out, out_len, max_out_len, nonce, in, |
| 226 |
in_len, ad, ad_len); |
| 227 |
} |
| 228 |
|
| 229 |
static const EVP_AEAD aead_chacha20_poly1305 = { |
| 230 |
32, /* key len */ |
| 231 |
12, /* nonce len */ |
| 232 |
POLY1305_TAG_LEN, /* overhead */ |
| 233 |
POLY1305_TAG_LEN, /* max tag length */ |
| 234 |
aead_chacha20_poly1305_init, |
| 235 |
NULL, /* init_with_direction */ |
| 236 |
aead_chacha20_poly1305_cleanup, |
| 237 |
aead_chacha20_poly1305_seal, |
| 238 |
aead_chacha20_poly1305_open, |
| 239 |
NULL, /* get_rc4_state */ |
| 240 |
NULL, /* get_iv */ |
| 241 |
}; |
| 242 |
|
| 243 |
const EVP_AEAD *EVP_aead_chacha20_poly1305(void) { |
| 244 |
return &aead_chacha20_poly1305; |
| 245 |
} |
| 246 |
|
| 247 |
static void poly1305_update_old(poly1305_state *ctx, const uint8_t *ad, |
| 248 |
size_t ad_len, const uint8_t *ciphertext, |
| 249 |
size_t ciphertext_len) { |
| 250 |
CRYPTO_poly1305_update(ctx, ad, ad_len); |
| 251 |
poly1305_update_length(ctx, ad_len); |
| 252 |
CRYPTO_poly1305_update(ctx, ciphertext, ciphertext_len); |
| 253 |
poly1305_update_length(ctx, ciphertext_len); |
| 254 |
} |
| 255 |
|
| 256 |
static int aead_chacha20_poly1305_old_seal( |
| 257 |
const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len, |
| 258 |
const uint8_t *nonce, size_t nonce_len, const uint8_t *in, size_t in_len, |
| 259 |
const uint8_t *ad, size_t ad_len) { |
| 260 |
if (nonce_len != 8) { |
| 261 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 262 |
return 0; |
| 263 |
} |
| 264 |
uint8_t nonce_96[12]; |
| 265 |
memset(nonce_96, 0, 4); |
| 266 |
memcpy(nonce_96 + 4, nonce, 8); |
| 267 |
return seal_impl(poly1305_update_old, ctx, out, out_len, max_out_len, |
| 268 |
nonce_96, in, in_len, ad, ad_len); |
| 269 |
} |
| 270 |
|
| 271 |
static int aead_chacha20_poly1305_old_open( |
| 272 |
const EVP_AEAD_CTX *ctx, uint8_t *out, size_t *out_len, size_t max_out_len, |
| 273 |
const uint8_t *nonce, size_t nonce_len, const uint8_t *in, size_t in_len, |
| 274 |
const uint8_t *ad, size_t ad_len) { |
| 275 |
if (nonce_len != 8) { |
| 276 |
OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 277 |
return 0; |
| 278 |
} |
| 279 |
uint8_t nonce_96[12]; |
| 280 |
memset(nonce_96, 0, 4); |
| 281 |
memcpy(nonce_96 + 4, nonce, 8); |
| 282 |
return open_impl(poly1305_update_old, ctx, out, out_len, max_out_len, |
| 283 |
nonce_96, in, in_len, ad, ad_len); |
| 284 |
} |
| 285 |
|
| 286 |
static const EVP_AEAD aead_chacha20_poly1305_old = { |
| 287 |
32, /* key len */ |
| 288 |
8, /* nonce len */ |
| 289 |
POLY1305_TAG_LEN, /* overhead */ |
| 290 |
POLY1305_TAG_LEN, /* max tag length */ |
| 291 |
aead_chacha20_poly1305_init, |
| 292 |
NULL, /* init_with_direction */ |
| 293 |
aead_chacha20_poly1305_cleanup, |
| 294 |
aead_chacha20_poly1305_old_seal, |
| 295 |
aead_chacha20_poly1305_old_open, |
| 296 |
NULL, /* get_rc4_state */ |
| 297 |
NULL, /* get_iv */ |
| 298 |
}; |
| 299 |
|
| 300 |
const EVP_AEAD *EVP_aead_chacha20_poly1305_old(void) { |
| 301 |
return &aead_chacha20_poly1305_old; |
| 302 |
} |