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> |
| 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 |
| 29 | #define CHACHA20_NONCE_LEN 8 |
| 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) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 45 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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_with_length(poly1305_state *poly1305, |
| 72 | const uint8_t *data, size_t data_len) { |
| 73 | size_t j = data_len; |
| 74 | uint8_t length_bytes[8]; |
| 75 | unsigned i; |
| 76 | |
| 77 | for (i = 0; i < sizeof(length_bytes); i++) { |
| 78 | length_bytes[i] = j; |
| 79 | j >>= 8; |
| 80 | } |
| 81 | |
| 82 | CRYPTO_poly1305_update(poly1305, data, data_len); |
| 83 | 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 | |
| 92 | static 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) { |
| 97 | const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state; |
| 98 | uint8_t poly1305_key[32] ALIGNED; |
| 99 | poly1305_state poly1305; |
| 100 | const uint64_t in_len_64 = in_len; |
| 101 | |
| 102 | /* The underlying ChaCha implementation may not overflow the block |
| 103 | * counter into the second counter word. Therefore we disallow |
| 104 | * 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 Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 110 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | if (in_len + c20_ctx->tag_len < in_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 115 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | if (max_out_len < in_len + c20_ctx->tag_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 120 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | if (nonce_len != CHACHA20_NONCE_LEN) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 125 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_IV_TOO_LARGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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); |
| 135 | CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1); |
| 136 | poly1305_update_with_length(&poly1305, out, in_len); |
| 137 | |
| 138 | uint8_t tag[POLY1305_TAG_LEN] ALIGNED; |
| 139 | CRYPTO_poly1305_finish(&poly1305, tag); |
| 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 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) { |
| 150 | const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state; |
| 151 | uint8_t mac[POLY1305_TAG_LEN]; |
| 152 | uint8_t poly1305_key[32] ALIGNED; |
| 153 | size_t plaintext_len; |
| 154 | poly1305_state poly1305; |
| 155 | const uint64_t in_len_64 = in_len; |
| 156 | |
| 157 | if (in_len < c20_ctx->tag_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 158 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | /* The underlying ChaCha implementation may not overflow the block |
| 163 | * counter into the second counter word. Therefore we disallow |
| 164 | * 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 Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 170 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | if (nonce_len != CHACHA20_NONCE_LEN) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 175 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_IV_TOO_LARGE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | plaintext_len = in_len - c20_ctx->tag_len; |
| 180 | |
| 181 | if (max_out_len < plaintext_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 | |
| 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 Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 196 | OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 197 | 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 | |
| 205 | static const EVP_AEAD aead_chacha20_poly1305 = { |
| 206 | 32, /* key len */ |
| 207 | CHACHA20_NONCE_LEN, /* nonce len */ |
| 208 | POLY1305_TAG_LEN, /* overhead */ |
| 209 | POLY1305_TAG_LEN, /* max tag length */ |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 210 | 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 Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | const EVP_AEAD *EVP_aead_chacha20_poly1305(void) { |
| 219 | return &aead_chacha20_poly1305; |
| 220 | } |