Home | History | Annotate | Download | only in cipher_extra
      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/cpu.h>
     22 #include <openssl/err.h>
     23 #include <openssl/mem.h>
     24 #include <openssl/poly1305.h>
     25 #include <openssl/type_check.h>
     26 
     27 #include "../fipsmodule/cipher/internal.h"
     28 #include "../internal.h"
     29 
     30 
     31 #define POLY1305_TAG_LEN 16
     32 
     33 struct aead_chacha20_poly1305_ctx {
     34   uint8_t key[32];
     35 };
     36 
     37 // For convenience (the x86_64 calling convention allows only six parameters in
     38 // registers), the final parameter for the assembly functions is both an input
     39 // and output parameter.
     40 union open_data {
     41   struct {
     42     alignas(16) uint8_t key[32];
     43     uint32_t counter;
     44     uint8_t nonce[12];
     45   } in;
     46   struct {
     47     uint8_t tag[POLY1305_TAG_LEN];
     48   } out;
     49 };
     50 
     51 union seal_data {
     52   struct {
     53     alignas(16) uint8_t key[32];
     54     uint32_t counter;
     55     uint8_t nonce[12];
     56     const uint8_t *extra_ciphertext;
     57     size_t extra_ciphertext_len;
     58   } in;
     59   struct {
     60     uint8_t tag[POLY1305_TAG_LEN];
     61   } out;
     62 };
     63 
     64 #if defined(OPENSSL_X86_64) && !defined(OPENSSL_NO_ASM) && \
     65     !defined(OPENSSL_WINDOWS)
     66 static int asm_capable(void) {
     67   const int sse41_capable = (OPENSSL_ia32cap_P[1] & (1 << 19)) != 0;
     68   return sse41_capable;
     69 }
     70 
     71 OPENSSL_COMPILE_ASSERT(sizeof(union open_data) == 48, wrong_open_data_size);
     72 OPENSSL_COMPILE_ASSERT(sizeof(union seal_data) == 48 + 8 + 8,
     73                        wrong_seal_data_size);
     74 
     75 // chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It decrypts
     76 // |plaintext_len| bytes from |ciphertext| and writes them to |out_plaintext|.
     77 // Additional input parameters are passed in |aead_data->in|. On exit, it will
     78 // write calculated tag value to |aead_data->out.tag|, which the caller must
     79 // check.
     80 extern void chacha20_poly1305_open(uint8_t *out_plaintext,
     81                                    const uint8_t *ciphertext,
     82                                    size_t plaintext_len, const uint8_t *ad,
     83                                    size_t ad_len, union open_data *aead_data);
     84 
     85 // chacha20_poly1305_open is defined in chacha20_poly1305_x86_64.pl. It encrypts
     86 // |plaintext_len| bytes from |plaintext| and writes them to |out_ciphertext|.
     87 // Additional input parameters are passed in |aead_data->in|. The calculated tag
     88 // value is over the computed ciphertext concatenated with |extra_ciphertext|
     89 // and written to |aead_data->out.tag|.
     90 extern void chacha20_poly1305_seal(uint8_t *out_ciphertext,
     91                                    const uint8_t *plaintext,
     92                                    size_t plaintext_len, const uint8_t *ad,
     93                                    size_t ad_len, union seal_data *aead_data);
     94 #else
     95 static int asm_capable(void) { return 0; }
     96 
     97 
     98 static void chacha20_poly1305_open(uint8_t *out_plaintext,
     99                                    const uint8_t *ciphertext,
    100                                    size_t plaintext_len, const uint8_t *ad,
    101                                    size_t ad_len, union open_data *aead_data) {}
    102 
    103 static void chacha20_poly1305_seal(uint8_t *out_ciphertext,
    104                                    const uint8_t *plaintext,
    105                                    size_t plaintext_len, const uint8_t *ad,
    106                                    size_t ad_len, union seal_data *aead_data) {}
    107 #endif
    108 
    109 static int aead_chacha20_poly1305_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
    110                                        size_t key_len, size_t tag_len) {
    111   struct aead_chacha20_poly1305_ctx *c20_ctx;
    112 
    113   if (tag_len == 0) {
    114     tag_len = POLY1305_TAG_LEN;
    115   }
    116 
    117   if (tag_len > POLY1305_TAG_LEN) {
    118     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
    119     return 0;
    120   }
    121 
    122   if (key_len != sizeof(c20_ctx->key)) {
    123     return 0;  // internal error - EVP_AEAD_CTX_init should catch this.
    124   }
    125 
    126   c20_ctx = OPENSSL_malloc(sizeof(struct aead_chacha20_poly1305_ctx));
    127   if (c20_ctx == NULL) {
    128     return 0;
    129   }
    130 
    131   OPENSSL_memcpy(c20_ctx->key, key, key_len);
    132   ctx->aead_state = c20_ctx;
    133   ctx->tag_len = tag_len;
    134 
    135   return 1;
    136 }
    137 
    138 static void aead_chacha20_poly1305_cleanup(EVP_AEAD_CTX *ctx) {
    139   OPENSSL_free(ctx->aead_state);
    140 }
    141 
    142 static void poly1305_update_length(poly1305_state *poly1305, size_t data_len) {
    143   uint8_t length_bytes[8];
    144 
    145   for (unsigned i = 0; i < sizeof(length_bytes); i++) {
    146     length_bytes[i] = data_len;
    147     data_len >>= 8;
    148   }
    149 
    150   CRYPTO_poly1305_update(poly1305, length_bytes, sizeof(length_bytes));
    151 }
    152 
    153 // calc_tag fills |tag| with the authentication tag for the given inputs.
    154 static void calc_tag(uint8_t tag[POLY1305_TAG_LEN],
    155                      const struct aead_chacha20_poly1305_ctx *c20_ctx,
    156                      const uint8_t nonce[12], const uint8_t *ad, size_t ad_len,
    157                      const uint8_t *ciphertext, size_t ciphertext_len,
    158                      const uint8_t *ciphertext_extra,
    159                      size_t ciphertext_extra_len) {
    160   alignas(16) uint8_t poly1305_key[32];
    161   OPENSSL_memset(poly1305_key, 0, sizeof(poly1305_key));
    162   CRYPTO_chacha_20(poly1305_key, poly1305_key, sizeof(poly1305_key),
    163                    c20_ctx->key, nonce, 0);
    164 
    165   static const uint8_t padding[16] = { 0 };  // Padding is all zeros.
    166   poly1305_state ctx;
    167   CRYPTO_poly1305_init(&ctx, poly1305_key);
    168   CRYPTO_poly1305_update(&ctx, ad, ad_len);
    169   if (ad_len % 16 != 0) {
    170     CRYPTO_poly1305_update(&ctx, padding, sizeof(padding) - (ad_len % 16));
    171   }
    172   CRYPTO_poly1305_update(&ctx, ciphertext, ciphertext_len);
    173   CRYPTO_poly1305_update(&ctx, ciphertext_extra, ciphertext_extra_len);
    174   const size_t ciphertext_total = ciphertext_len + ciphertext_extra_len;
    175   if (ciphertext_total % 16 != 0) {
    176     CRYPTO_poly1305_update(&ctx, padding,
    177                            sizeof(padding) - (ciphertext_total % 16));
    178   }
    179   poly1305_update_length(&ctx, ad_len);
    180   poly1305_update_length(&ctx, ciphertext_total);
    181   CRYPTO_poly1305_finish(&ctx, tag);
    182 }
    183 
    184 static int aead_chacha20_poly1305_seal_scatter(
    185     const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
    186     size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
    187     size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
    188     size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
    189   const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
    190 
    191   if (extra_in_len + ctx->tag_len < ctx->tag_len) {
    192     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
    193     return 0;
    194   }
    195   if (max_out_tag_len < ctx->tag_len + extra_in_len) {
    196     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
    197     return 0;
    198   }
    199   if (nonce_len != 12) {
    200     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
    201     return 0;
    202   }
    203 
    204   // |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow
    205   // individual operations that work on more than 256GB at a time.
    206   // |in_len_64| is needed because, on 32-bit platforms, size_t is only
    207   // 32-bits and this produces a warning because it's always false.
    208   // Casting to uint64_t inside the conditional is not sufficient to stop
    209   // the warning.
    210   const uint64_t in_len_64 = in_len;
    211   if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) {
    212     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
    213     return 0;
    214   }
    215 
    216   if (max_out_tag_len < ctx->tag_len) {
    217     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
    218     return 0;
    219   }
    220 
    221   // The the extra input is given, it is expected to be very short and so is
    222   // encrypted byte-by-byte first.
    223   if (extra_in_len) {
    224     static const size_t kChaChaBlockSize = 64;
    225     uint32_t block_counter = 1 + (in_len / kChaChaBlockSize);
    226     size_t offset = in_len % kChaChaBlockSize;
    227     uint8_t block[64 /* kChaChaBlockSize */];
    228 
    229     for (size_t done = 0; done < extra_in_len; block_counter++) {
    230       memset(block, 0, sizeof(block));
    231       CRYPTO_chacha_20(block, block, sizeof(block), c20_ctx->key, nonce,
    232                        block_counter);
    233       for (size_t i = offset; i < sizeof(block) && done < extra_in_len;
    234            i++, done++) {
    235         out_tag[done] = extra_in[done] ^ block[i];
    236       }
    237       offset = 0;
    238     }
    239   }
    240 
    241   union seal_data data;
    242   if (asm_capable()) {
    243     OPENSSL_memcpy(data.in.key, c20_ctx->key, 32);
    244     data.in.counter = 0;
    245     OPENSSL_memcpy(data.in.nonce, nonce, 12);
    246     data.in.extra_ciphertext = out_tag;
    247     data.in.extra_ciphertext_len = extra_in_len;
    248     chacha20_poly1305_seal(out, in, in_len, ad, ad_len, &data);
    249   } else {
    250     CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1);
    251     calc_tag(data.out.tag, c20_ctx, nonce, ad, ad_len, out, in_len, out_tag,
    252              extra_in_len);
    253   }
    254 
    255   OPENSSL_memcpy(out_tag + extra_in_len, data.out.tag, ctx->tag_len);
    256   *out_tag_len = extra_in_len + ctx->tag_len;
    257   return 1;
    258 }
    259 
    260 static int aead_chacha20_poly1305_open_gather(
    261     const EVP_AEAD_CTX *ctx, uint8_t *out, const uint8_t *nonce,
    262     size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *in_tag,
    263     size_t in_tag_len, const uint8_t *ad, size_t ad_len) {
    264   const struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
    265 
    266   if (nonce_len != 12) {
    267     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
    268     return 0;
    269   }
    270 
    271   if (in_tag_len != ctx->tag_len) {
    272     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
    273     return 0;
    274   }
    275 
    276   // |CRYPTO_chacha_20| uses a 32-bit block counter. Therefore we disallow
    277   // individual operations that work on more than 256GB at a time.
    278   // |in_len_64| is needed because, on 32-bit platforms, size_t is only
    279   // 32-bits and this produces a warning because it's always false.
    280   // Casting to uint64_t inside the conditional is not sufficient to stop
    281   // the warning.
    282   const uint64_t in_len_64 = in_len;
    283   if (in_len_64 >= (UINT64_C(1) << 32) * 64 - 64) {
    284     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
    285     return 0;
    286   }
    287 
    288   union open_data data;
    289   if (asm_capable()) {
    290     OPENSSL_memcpy(data.in.key, c20_ctx->key, 32);
    291     data.in.counter = 0;
    292     OPENSSL_memcpy(data.in.nonce, nonce, 12);
    293     chacha20_poly1305_open(out, in, in_len, ad, ad_len, &data);
    294   } else {
    295     calc_tag(data.out.tag, c20_ctx, nonce, ad, ad_len, in, in_len, NULL, 0);
    296     CRYPTO_chacha_20(out, in, in_len, c20_ctx->key, nonce, 1);
    297   }
    298 
    299   if (CRYPTO_memcmp(data.out.tag, in_tag, ctx->tag_len) != 0) {
    300     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
    301     return 0;
    302   }
    303 
    304   return 1;
    305 }
    306 
    307 static const EVP_AEAD aead_chacha20_poly1305 = {
    308     32,                // key len
    309     12,                // nonce len
    310     POLY1305_TAG_LEN,  // overhead
    311     POLY1305_TAG_LEN,  // max tag length
    312     1,                 // seal_scatter_supports_extra_in
    313 
    314     aead_chacha20_poly1305_init,
    315     NULL,  // init_with_direction
    316     aead_chacha20_poly1305_cleanup,
    317     NULL /* open */,
    318     aead_chacha20_poly1305_seal_scatter,
    319     aead_chacha20_poly1305_open_gather,
    320     NULL,  // get_iv
    321     NULL,  // tag_len
    322 };
    323 
    324 const EVP_AEAD *EVP_aead_chacha20_poly1305(void) {
    325   return &aead_chacha20_poly1305;
    326 }
    327