Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

282 wiersze
9.6 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. struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = ctx->aead_state;
  86. OPENSSL_cleanse(aes_ctx, sizeof(struct aead_aes_ctr_hmac_sha256_ctx));
  87. OPENSSL_free(aes_ctx);
  88. }
  89. static void hmac_update_uint64(SHA256_CTX *sha256, uint64_t value) {
  90. unsigned i;
  91. uint8_t bytes[8];
  92. for (i = 0; i < sizeof(bytes); i++) {
  93. bytes[i] = value & 0xff;
  94. value >>= 8;
  95. }
  96. SHA256_Update(sha256, bytes, sizeof(bytes));
  97. }
  98. static void hmac_calculate(uint8_t out[SHA256_DIGEST_LENGTH],
  99. const SHA256_CTX *inner_init_state,
  100. const SHA256_CTX *outer_init_state,
  101. const uint8_t *ad, size_t ad_len,
  102. const uint8_t *nonce, const uint8_t *ciphertext,
  103. size_t ciphertext_len) {
  104. SHA256_CTX sha256;
  105. OPENSSL_memcpy(&sha256, inner_init_state, sizeof(sha256));
  106. hmac_update_uint64(&sha256, ad_len);
  107. hmac_update_uint64(&sha256, ciphertext_len);
  108. SHA256_Update(&sha256, nonce, EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN);
  109. SHA256_Update(&sha256, ad, ad_len);
  110. /* Pad with zeros to the end of the SHA-256 block. */
  111. const unsigned num_padding =
  112. (SHA256_CBLOCK - ((sizeof(uint64_t)*2 +
  113. EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN + ad_len) %
  114. SHA256_CBLOCK)) %
  115. SHA256_CBLOCK;
  116. uint8_t padding[SHA256_CBLOCK];
  117. OPENSSL_memset(padding, 0, num_padding);
  118. SHA256_Update(&sha256, padding, num_padding);
  119. SHA256_Update(&sha256, ciphertext, ciphertext_len);
  120. uint8_t inner_digest[SHA256_DIGEST_LENGTH];
  121. SHA256_Final(inner_digest, &sha256);
  122. OPENSSL_memcpy(&sha256, outer_init_state, sizeof(sha256));
  123. SHA256_Update(&sha256, inner_digest, sizeof(inner_digest));
  124. SHA256_Final(out, &sha256);
  125. }
  126. static void aead_aes_ctr_hmac_sha256_crypt(
  127. const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx, uint8_t *out,
  128. const uint8_t *in, size_t len, const uint8_t *nonce) {
  129. /* Since the AEAD operation is one-shot, keeping a buffer of unused keystream
  130. * bytes is pointless. However, |CRYPTO_ctr128_encrypt| requires it. */
  131. uint8_t partial_block_buffer[AES_BLOCK_SIZE];
  132. unsigned partial_block_offset = 0;
  133. OPENSSL_memset(partial_block_buffer, 0, sizeof(partial_block_buffer));
  134. uint8_t counter[AES_BLOCK_SIZE];
  135. OPENSSL_memcpy(counter, nonce, EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN);
  136. OPENSSL_memset(counter + EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN, 0, 4);
  137. if (aes_ctx->ctr) {
  138. CRYPTO_ctr128_encrypt_ctr32(in, out, len, &aes_ctx->ks.ks, counter,
  139. partial_block_buffer, &partial_block_offset,
  140. aes_ctx->ctr);
  141. } else {
  142. CRYPTO_ctr128_encrypt(in, out, len, &aes_ctx->ks.ks, counter,
  143. partial_block_buffer, &partial_block_offset,
  144. aes_ctx->block);
  145. }
  146. }
  147. static int aead_aes_ctr_hmac_sha256_seal_scatter(
  148. const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
  149. size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
  150. size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
  151. size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
  152. const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = ctx->aead_state;
  153. const uint64_t in_len_64 = in_len;
  154. if (in_len_64 >= (UINT64_C(1) << 32) * AES_BLOCK_SIZE) {
  155. /* This input is so large it would overflow the 32-bit block counter. */
  156. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  157. return 0;
  158. }
  159. if (max_out_tag_len < ctx->tag_len) {
  160. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  161. return 0;
  162. }
  163. if (nonce_len != EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN) {
  164. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  165. return 0;
  166. }
  167. aead_aes_ctr_hmac_sha256_crypt(aes_ctx, out, in, in_len, nonce);
  168. uint8_t hmac_result[SHA256_DIGEST_LENGTH];
  169. hmac_calculate(hmac_result, &aes_ctx->inner_init_state,
  170. &aes_ctx->outer_init_state, ad, ad_len, nonce, out, in_len);
  171. OPENSSL_memcpy(out_tag, hmac_result, ctx->tag_len);
  172. *out_tag_len = ctx->tag_len;
  173. return 1;
  174. }
  175. static int aead_aes_ctr_hmac_sha256_open_gather(
  176. const EVP_AEAD_CTX *ctx, uint8_t *out, const uint8_t *nonce,
  177. size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *in_tag,
  178. size_t in_tag_len, const uint8_t *ad, size_t ad_len) {
  179. const struct aead_aes_ctr_hmac_sha256_ctx *aes_ctx = ctx->aead_state;
  180. if (in_tag_len != ctx->tag_len) {
  181. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  182. return 0;
  183. }
  184. if (nonce_len != EVP_AEAD_AES_CTR_HMAC_SHA256_NONCE_LEN) {
  185. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_NONCE_SIZE);
  186. return 0;
  187. }
  188. uint8_t hmac_result[SHA256_DIGEST_LENGTH];
  189. hmac_calculate(hmac_result, &aes_ctx->inner_init_state,
  190. &aes_ctx->outer_init_state, ad, ad_len, nonce, in,
  191. in_len);
  192. if (CRYPTO_memcmp(hmac_result, in_tag, ctx->tag_len) != 0) {
  193. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  194. return 0;
  195. }
  196. aead_aes_ctr_hmac_sha256_crypt(aes_ctx, out, in, in_len, nonce);
  197. return 1;
  198. }
  199. static const EVP_AEAD aead_aes_128_ctr_hmac_sha256 = {
  200. 16 /* AES key */ + 32 /* HMAC key */,
  201. 12, /* nonce length */
  202. EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, /* overhead */
  203. EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, /* max tag length */
  204. 0, /* seal_scatter_supports_extra_in */
  205. aead_aes_ctr_hmac_sha256_init,
  206. NULL /* init_with_direction */,
  207. aead_aes_ctr_hmac_sha256_cleanup,
  208. NULL /* open */,
  209. aead_aes_ctr_hmac_sha256_seal_scatter,
  210. aead_aes_ctr_hmac_sha256_open_gather,
  211. NULL /* get_iv */,
  212. };
  213. static const EVP_AEAD aead_aes_256_ctr_hmac_sha256 = {
  214. 32 /* AES key */ + 32 /* HMAC key */,
  215. 12, /* nonce length */
  216. EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, /* overhead */
  217. EVP_AEAD_AES_CTR_HMAC_SHA256_TAG_LEN, /* max tag length */
  218. 0, /* seal_scatter_supports_extra_in */
  219. aead_aes_ctr_hmac_sha256_init,
  220. NULL /* init_with_direction */,
  221. aead_aes_ctr_hmac_sha256_cleanup,
  222. NULL /* open */,
  223. aead_aes_ctr_hmac_sha256_seal_scatter,
  224. aead_aes_ctr_hmac_sha256_open_gather,
  225. NULL /* get_iv */,
  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. }