Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

282 lignes
9.5 KiB

  1. /* Copyright (c) 2017, 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. #include <openssl/aead.h>
  15. #include <openssl/cipher.h>
  16. #include <openssl/crypto.h>
  17. #include <openssl/err.h>
  18. #include <openssl/sha.h>
  19. #include "../fipsmodule/cipher/internal.h"
  20. #define EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN SHA256_DIGEST_LENGTH
  21. #define EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN 12
  22. struct aead_aes_ctr_hmac_sha256_ctx {
  23. union {
  24. double align;
  25. AES_KEY ks;
  26. } ks;
  27. ctr128_f ctr;
  28. block128_f block;
  29. SHA256_CTX inner_init_state;
  30. SHA256_CTX outer_init_state;
  31. };
  32. static void hmac_init(SHA256_CTX *out_inner, SHA256_CTX *out_outer,
  33. const uint8_t hmac_key[32]) {
  34. static const size_t hmac_key_len = 32;
  35. uint8_t block[SHA256_CBLOCK];
  36. OPENSSL_memcpy(block, hmac_key, hmac_key_len);
  37. OPENSSL_memset(block + hmac_key_len, 0x36, sizeof(block) - hmac_key_len);
  38. unsigned i;
  39. for (i = 0; i < hmac_key_len; i++) {
  40. block[i] ^= 0x36;
  41. }
  42. SHA256_Init(out_inner);
  43. SHA256_Update(out_inner, block, sizeof(block));
  44. OPENSSL_memset(block + hmac_key_len, 0x5c, sizeof(block) - hmac_key_len);
  45. for (i = 0; i < hmac_key_len; i++) {
  46. block[i] ^= (0x36 ^ 0x5c);
  47. }
  48. SHA256_Init(out_outer);
  49. SHA256_Update(out_outer, block, sizeof(block));
  50. }
  51. static int aead_aes_ctr_hmac_sha256_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  52. size_t key_len, size_t tag_len) {
  53. struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx;
  54. static const size_t hmac_key_len = 32;
  55. if (key_len < hmac_key_len) {
  56. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
  57. return 0; // EVP_AEAD_CTX_init should catch this.
  58. }
  59. const size_t aes_key_len = key_len - hmac_key_len;
  60. if (aes_key_len != 16 && aes_key_len != 32) {
  61. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
  62. return 0; // EVP_AEAD_CTX_init should catch this.
  63. }
  64. if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) {
  65. tag_len = EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN;
  66. }
  67. if (tag_len > EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN) {
  68. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE);
  69. return 0;
  70. }
  71. aes_ctx = OPENSSL_malloc(sizeof(struct aead_aes_ctr_hmac_sha256_ctx));
  72. if (aes_ctx == NULL) {
  73. OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
  74. return 0;
  75. }
  76. aes_ctx->ctr =
  77. aes_ctr_set_key(&aes_ctx->ks.ks, NULL, &aes_ctx->block, key, aes_key_len);
  78. ctx->tag_len = tag_len;
  79. hmac_init(&aes_ctx->inner_init_state, &aes_ctx->outer_init_state,
  80. key + aes_key_len);
  81. ctx->aead_state = aes_ctx;
  82. return 1;
  83. }
  84. static void aead_aes_ctr_hmac_sha256_cleanup(EVP_AEAD_CTX *ctx) {
  85. OPENSSL_free(ctx->aead_state);
  86. }
  87. static void hmac_update_uint64(SHA256_CTX *sha256, uint64_t value) {
  88. unsigned i;
  89. uint8_t bytes[8];
  90. for (i = 0; i < sizeof(bytes); i++) {
  91. bytes[i] = value & 0xff;
  92. value >>= 8;
  93. }
  94. SHA256_Update(sha256, bytes, sizeof(bytes));
  95. }
  96. static void hmac_calculate(uint8_t out[SHA256_DIGEST_LENGTH],
  97. const SHA256_CTX *inner_init_state,
  98. const SHA256_CTX *outer_init_state,
  99. const uint8_t *ad, size_t ad_len,
  100. const uint8_t *nonce, const uint8_t *ciphertext,
  101. size_t ciphertext_len) {
  102. SHA256_CTX sha256;
  103. OPENSSL_memcpy(&sha256, inner_init_state, sizeof(sha256));
  104. hmac_update_uint64(&sha256, ad_len);
  105. hmac_update_uint64(&sha256, ciphertext_len);
  106. SHA256_Update(&sha256, nonce, EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN);
  107. SHA256_Update(&sha256, ad, ad_len);
  108. // Pad with zeros to the end of the SHA-256 block.
  109. const unsigned num_padding =
  110. (SHA256_CBLOCK - ((sizeof(uint64_t)*2 +
  111. EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN + ad_len) %
  112. SHA256_CBLOCK)) %
  113. SHA256_CBLOCK;
  114. uint8_t padding[SHA256_CBLOCK];
  115. OPENSSL_memset(padding, 0, num_padding);
  116. SHA256_Update(&sha256, padding, num_padding);
  117. SHA256_Update(&sha256, ciphertext, ciphertext_len);
  118. uint8_t inner_digest[SHA256_DIGEST_LENGTH];
  119. SHA256_Final(inner_digest, &sha256);
  120. OPENSSL_memcpy(&sha256, outer_init_state, sizeof(sha256));
  121. SHA256_Update(&sha256, inner_digest, sizeof(inner_digest));
  122. SHA256_Final(out, &sha256);
  123. }
  124. static void aead_aes_ctr_hmac_sha256_crypt(
  125. const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx, uint8_t *out,
  126. const uint8_t *in, size_t len, const uint8_t *nonce) {
  127. // Since the AEAD operation is one-shot, keeping a buffer of unused keystream
  128. // bytes is pointless. However, |CRYPTO_ctr128_encrypt| requires it.
  129. uint8_t partial_block_buffer[AES_BLOCK_SIZE];
  130. unsigned partial_block_offset = 0;
  131. OPENSSL_memset(partial_block_buffer, 0, sizeof(partial_block_buffer));
  132. uint8_t counter[AES_BLOCK_SIZE];
  133. OPENSSL_memcpy(counter, nonce, EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN);
  134. OPENSSL_memset(counter + EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN, 0, 4);
  135. if (aes_ctx->ctr) {
  136. CRYPTO_ctr128_encrypt_ctr32(in, out, len, &aes_ctx->ks.ks, counter,
  137. partial_block_buffer, &partial_block_offset,
  138. aes_ctx->ctr);
  139. } else {
  140. CRYPTO_ctr128_encrypt(in, out, len, &aes_ctx->ks.ks, counter,
  141. partial_block_buffer, &partial_block_offset,
  142. aes_ctx->block);
  143. }
  144. }
  145. static int aead_aes_ctr_hmac_sha256_seal_scatter(
  146. const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
  147. size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
  148. size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
  149. size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
  150. const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = ctx->aead_state;
  151. const uint64_t in_len_64 = in_len;
  152. if (in_len_64 >= (UINT64_C(1) << 32) * AES_BLOCK_SIZE) {
  153. // This input is so large it would overflow the 32-bit block counter.
  154. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  155. return 0;
  156. }
  157. if (max_out_tag_len < ctx->tag_len) {
  158. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  159. return 0;
  160. }
  161. if (nonce_len != EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN) {
  162. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  163. return 0;
  164. }
  165. aead_aes_ctr_hmac_sha256_crypt(aes_ctx, out, in, in_len, nonce);
  166. uint8_t hmac_result[SHA256_DIGEST_LENGTH];
  167. hmac_calculate(hmac_result, &aes_ctx->inner_init_state,
  168. &aes_ctx->outer_init_state, ad, ad_len, nonce, out, in_len);
  169. OPENSSL_memcpy(out_tag, hmac_result, ctx->tag_len);
  170. *out_tag_len = ctx->tag_len;
  171. return 1;
  172. }
  173. static int aead_aes_ctr_hmac_sha256_open_gather(
  174. const EVP_AEAD_CTX *ctx, uint8_t *out, const uint8_t *nonce,
  175. size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *in_tag,
  176. size_t in_tag_len, const uint8_t *ad, size_t ad_len) {
  177. const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = ctx->aead_state;
  178. if (in_tag_len != ctx->tag_len) {
  179. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  180. return 0;
  181. }
  182. if (nonce_len != EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN) {
  183. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  184. return 0;
  185. }
  186. uint8_t hmac_result[SHA256_DIGEST_LENGTH];
  187. hmac_calculate(hmac_result, &aes_ctx->inner_init_state,
  188. &aes_ctx->outer_init_state, ad, ad_len, nonce, in,
  189. in_len);
  190. if (CRYPTO_memcmp(hmac_result, in_tag, ctx->tag_len) != 0) {
  191. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  192. return 0;
  193. }
  194. aead_aes_ctr_hmac_sha256_crypt(aes_ctx, out, in, in_len, nonce);
  195. return 1;
  196. }
  197. static const EVP_AEAD aead_aes_128_ctr_hmac_sha256 = {
  198. 16 /* AES key */ + 32 /* HMAC key */,
  199. 12, // nonce length
  200. EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, // overhead
  201. EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, // max tag length
  202. 0, // seal_scatter_supports_extra_in
  203. aead_aes_ctr_hmac_sha256_init,
  204. NULL /* init_with_direction */,
  205. aead_aes_ctr_hmac_sha256_cleanup,
  206. NULL /* open */,
  207. aead_aes_ctr_hmac_sha256_seal_scatter,
  208. aead_aes_ctr_hmac_sha256_open_gather,
  209. NULL /* get_iv */,
  210. NULL /* tag_len */,
  211. };
  212. static const EVP_AEAD aead_aes_256_ctr_hmac_sha256 = {
  213. 32 /* AES key */ + 32 /* HMAC key */,
  214. 12, // nonce length
  215. EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, // overhead
  216. EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, // max tag length
  217. 0, // seal_scatter_supports_extra_in
  218. aead_aes_ctr_hmac_sha256_init,
  219. NULL /* init_with_direction */,
  220. aead_aes_ctr_hmac_sha256_cleanup,
  221. NULL /* open */,
  222. aead_aes_ctr_hmac_sha256_seal_scatter,
  223. aead_aes_ctr_hmac_sha256_open_gather,
  224. NULL /* get_iv */,
  225. NULL /* tag_len */,
  226. };
  227. const EVP_AEAD *EVP_aead_aes_128_ctr_hmac_sha256(void) {
  228. return &aead_aes_128_ctr_hmac_sha256;
  229. }
  230. const EVP_AEAD *EVP_aead_aes_256_ctr_hmac_sha256(void) {
  231. return &aead_aes_256_ctr_hmac_sha256;
  232. }