Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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> |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 21 | #include <openssl/cpu.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 22 | #include <openssl/err.h> |
| 23 | #include <openssl/mem.h> |
| 24 | #include <openssl/poly1305.h> |
| 25 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 26 | #include "../fipsmodule/cipher/internal.h" |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 27 | #include "../internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 28 | |
| 29 | |
| 30 | #define POLY1305_TAG_LEN 16 |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 31 | |
| 32 | struct aead_chacha20_poly1305_ctx { |
| 33 | unsigned char key[32]; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 34 | }; |
| 35 | |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 36 | #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \ |
| 37 | !defined(OPENSSL_WINDOWS) |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 38 | static int asm_capable(void) { |
| 39 | const int sse41_capable = (OPENSSL_ia32cap_P[1] & (1 << 19)) != 0; |
| 40 | return sse41_capable; |
| 41 | } |
| 42 | |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 43 | // chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It |
| 44 | // decrypts |plaintext_len| bytes from |ciphertext| and writes them to |
| 45 | // |out_plaintext|. On entry, |aead_data| must contain the final 48 bytes of |
| 46 | // the initial ChaCha20 block, i.e. the key, followed by four zeros, followed |
| 47 | // by the nonce. On exit, it will contain the calculated tag value, which the |
| 48 | // caller must check. |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 49 | extern void chacha20_poly1305_open(uint8_t *out_plaintext, |
| 50 | const uint8_t *ciphertext, |
| 51 | size_t plaintext_len, const uint8_t *ad, |
| 52 | size_t ad_len, uint8_t *aead_data); |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 53 | |
| 54 | // chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It |
| 55 | // encrypts |plaintext_len| bytes from |plaintext| and writes them to |
| 56 | // |out_ciphertext|. On entry, |aead_data| must contain the final 48 bytes of |
| 57 | // the initial ChaCha20 block, i.e. the key, followed by four zeros, followed |
| 58 | // by the nonce. On exit, it will contain the calculated tag value, which the |
| 59 | // caller must append to the ciphertext. |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 60 | extern void chacha20_poly1305_seal(uint8_t *out_ciphertext, |
| 61 | const uint8_t *plaintext, |
| 62 | size_t plaintext_len, const uint8_t *ad, |
| 63 | size_t ad_len, uint8_t *aead_data); |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 64 | #else |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 65 | static int asm_capable(void) { |
| 66 | return 0; |
| 67 | } |
| 68 | |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 69 | |
| 70 | static void chacha20_poly1305_open(uint8_t *out_plaintext, |
| 71 | const uint8_t *ciphertext, |
| 72 | size_t plaintext_len, const uint8_t *ad, |
| 73 | size_t ad_len, uint8_t *aead_data) {} |
| 74 | |
| 75 | static void chacha20_poly1305_seal(uint8_t *out_ciphertext, |
| 76 | const uint8_t *plaintext, |
| 77 | size_t plaintext_len, const uint8_t *ad, |
| 78 | size_t ad_len, uint8_t *aead_data) {} |
| 79 | #endif |
| 80 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 81 | static int aead_chacha20_poly1305_init(EVP_AEAD_CTX *ctx, const uint8_t *key, |
| 82 | size_t key_len, size_t tag_len) { |
| 83 | struct aead_chacha20_poly1305_ctx *c20_ctx; |
| 84 | |
| 85 | if (tag_len == 0) { |
| 86 | tag_len = POLY1305_TAG_LEN; |
| 87 | } |
| 88 | |
| 89 | if (tag_len > POLY1305_TAG_LEN) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 90 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | if (key_len != sizeof(c20_ctx->key)) { |
| 95 | return 0; /* internal error - EVP_AEAD_CTX_init should catch this. */ |
| 96 | } |
| 97 | |
| 98 | c20_ctx = OPENSSL_malloc(sizeof(struct aead_chacha20_poly1305_ctx)); |
| 99 | if (c20_ctx == NULL) { |
| 100 | return 0; |
| 101 | } |
| 102 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 103 | OPENSSL_memcpy(c20_ctx->key, key, key_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 104 | ctx->aead_state = c20_ctx; |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 105 | ctx->tag_len = tag_len; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 106 | |
| 107 | return 1; |
| 108 | } |
| 109 | |
| 110 | static void aead_chacha20_poly1305_cleanup(EVP_AEAD_CTX *ctx) { |
| 111 | struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state; |
| 112 | OPENSSL_cleanse(c20_ctx->key, sizeof(c20_ctx->key)); |
| 113 | OPENSSL_free(c20_ctx); |
| 114 | } |
| 115 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 116 | static void poly1305_update_length(poly1305_state *poly1305, size_t data_len) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 117 | uint8_t length_bytes[8]; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 118 | |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 119 | for (unsigned i = 0; i < sizeof(length_bytes); i++) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 120 | length_bytes[i] = data_len; |
| 121 | data_len >>= 8; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 124 | CRYPTO_poly1305_update(poly1305, length_bytes, sizeof(length_bytes)); |
| 125 | } |
| 126 | |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 127 | static void poly1305_update_padded_16(poly1305_state *poly1305, |
| 128 | const uint8_t *data, size_t data_len) { |
| 129 | static const uint8_t padding[16] = { 0 }; /* Padding is all zeros. */ |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 130 | |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 131 | CRYPTO_poly1305_update(poly1305, data, data_len); |
| 132 | if (data_len % 16 != 0) { |
| 133 | CRYPTO_poly1305_update(poly1305, padding, |
| 134 | sizeof(padding) - (data_len % 16)); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* calc_tag fills |tag| with the authentication tag for the given inputs. */ |
| 139 | static void calc_tag(uint8_t tag[POLY1305_TAG_LEN], |
| 140 | const struct aead_chacha20_poly1305_ctx *c20_ctx, |
| 141 | const uint8_t nonce[12], const uint8_t *ad, size_t ad_len, |
| 142 | const uint8_t *ciphertext, size_t ciphertext_len) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 143 | alignas(16) uint8_t poly1305_key[32]; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 144 | OPENSSL_memset(poly1305_key, 0, sizeof(poly1305_key)); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 145 | CRYPTO_chacha_20(poly1305_key, poly1305_key, sizeof(poly1305_key), |
| 146 | c20_ctx->key, nonce, 0); |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 147 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 148 | poly1305_state ctx; |
| 149 | CRYPTO_poly1305_init(&ctx, poly1305_key); |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 150 | poly1305_update_padded_16(&ctx, ad, ad_len); |
| 151 | poly1305_update_padded_16(&ctx, ciphertext, ciphertext_len); |
| 152 | poly1305_update_length(&ctx, ad_len); |
| 153 | poly1305_update_length(&ctx, ciphertext_len); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 154 | CRYPTO_poly1305_finish(&ctx, tag); |
| 155 | } |
| 156 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 157 | static int aead_chacha20_poly1305_seal_scatter( |
| 158 | const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag, |
| 159 | size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce, |
Robert Sloan | 927a495 | 2017-07-03 11:25:09 -0700 | [diff] [blame^] | 160 | size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in, |
| 161 | size_t extra_in_len, const uint8_t *ad, size_t ad_len) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 162 | const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 163 | |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 164 | if (nonce_len != 12) { |
| 165 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 166 | return 0; |
| 167 | } |
| 168 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 169 | /* |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 170 | * individual operations that work on more than 256GB at a time. |
| 171 | * |in_len_64| is needed because, on 32-bit platforms, size_t is only |
| 172 | * 32-bits and this produces a warning because it's always false. |
| 173 | * Casting to uint64_t inside the conditional is not sufficient to stop |
| 174 | * the warning. */ |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 175 | const uint64_t in_len_64 = in_len; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 176 | if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 177 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 181 | if (max_out_tag_len < ctx->tag_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 182 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 186 | alignas(16) uint8_t tag[48]; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 187 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 188 | if (asm_capable()) { |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 189 | OPENSSL_memcpy(tag, c20_ctx->key, 32); |
| 190 | OPENSSL_memset(tag + 32, 0, 4); |
| 191 | OPENSSL_memcpy(tag + 32 + 4, nonce, 12); |
| 192 | chacha20_poly1305_seal(out, in, in_len, ad, ad_len, tag); |
| 193 | } else { |
| 194 | CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1); |
| 195 | calc_tag(tag, c20_ctx, nonce, ad, ad_len, out, in_len); |
| 196 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 197 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 198 | OPENSSL_memcpy(out_tag, tag, ctx->tag_len); |
| 199 | *out_tag_len = ctx->tag_len; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 200 | return 1; |
| 201 | } |
| 202 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 203 | static int aead_chacha20_poly1305_open_gather( |
| 204 | const EVP_AEAD_CTX *ctx, uint8_t *out, const uint8_t *nonce, |
| 205 | size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *in_tag, |
| 206 | size_t in_tag_len, const uint8_t *ad, size_t ad_len) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 207 | const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 208 | |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 209 | if (nonce_len != 12) { |
| 210 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE); |
| 211 | return 0; |
| 212 | } |
| 213 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 214 | if (in_tag_len != ctx->tag_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 215 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 219 | /* |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 220 | * individual operations that work on more than 256GB at a time. |
| 221 | * |in_len_64| is needed because, on 32-bit platforms, size_t is only |
| 222 | * 32-bits and this produces a warning because it's always false. |
| 223 | * Casting to uint64_t inside the conditional is not sufficient to stop |
| 224 | * the warning. */ |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 225 | const uint64_t in_len_64 = in_len; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 226 | if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 227 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 231 | alignas(16) uint8_t tag[48]; |
| 232 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 233 | if (asm_capable()) { |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 234 | OPENSSL_memcpy(tag, c20_ctx->key, 32); |
| 235 | OPENSSL_memset(tag + 32, 0, 4); |
| 236 | OPENSSL_memcpy(tag + 32 + 4, nonce, 12); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 237 | chacha20_poly1305_open(out, in, in_len, ad, ad_len, tag); |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 238 | } else { |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 239 | calc_tag(tag, c20_ctx, nonce, ad, ad_len, in, in_len); |
| 240 | CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1); |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 241 | } |
| 242 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 243 | if (CRYPTO_memcmp(tag, in_tag, ctx->tag_len) != 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 244 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 248 | return 1; |
| 249 | } |
| 250 | |
| 251 | static const EVP_AEAD aead_chacha20_poly1305 = { |
Robert Sloan | 927a495 | 2017-07-03 11:25:09 -0700 | [diff] [blame^] | 252 | 32, /* key len */ |
| 253 | 12, /* nonce len */ |
| 254 | POLY1305_TAG_LEN, /* overhead */ |
| 255 | POLY1305_TAG_LEN, /* max tag length */ |
| 256 | 0, /* seal_scatter_supports_extra_in */ |
| 257 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 258 | aead_chacha20_poly1305_init, |
| 259 | NULL, /* init_with_direction */ |
| 260 | aead_chacha20_poly1305_cleanup, |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 261 | NULL /* open */, |
| 262 | aead_chacha20_poly1305_seal_scatter, |
| 263 | aead_chacha20_poly1305_open_gather, |
Robert Sloan | 927a495 | 2017-07-03 11:25:09 -0700 | [diff] [blame^] | 264 | NULL, /* get_iv */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 265 | }; |
| 266 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 267 | const EVP_AEAD *EVP_aead_chacha20_poly1305(void) { |
Kenny Root | 03bcf61 | 2015-11-05 20:20:27 +0000 | [diff] [blame] | 268 | return &aead_chacha20_poly1305; |
Adam Langley | fdeb488 | 2015-10-30 13:15:30 -0700 | [diff] [blame] | 269 | } |