Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

204 linhas
6.1 KiB

  1. /* Copyright (c) 2018, 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 <assert.h>
  16. #include <openssl/cipher.h>
  17. #include <openssl/err.h>
  18. #include <openssl/mem.h>
  19. #include "../fipsmodule/cipher/internal.h"
  20. #define EVP_AEAD_AES_CCM_MAX_TAG_LEN 16
  21. struct aead_aes_ccm_ctx {
  22. union {
  23. double align;
  24. AES_KEY ks;
  25. } ks;
  26. CCM128_CONTEXT ccm;
  27. };
  28. static int aead_aes_ccm_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  29. size_t key_len, size_t tag_len, unsigned M,
  30. unsigned L) {
  31. assert(M == EVP_AEAD_max_overhead(ctx->aead));
  32. assert(M == EVP_AEAD_max_tag_len(ctx->aead));
  33. assert(15 - L == EVP_AEAD_nonce_length(ctx->aead));
  34. if (key_len != EVP_AEAD_key_length(ctx->aead)) {
  35. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
  36. return 0; // EVP_AEAD_CTX_init should catch this.
  37. }
  38. if (tag_len == EVP_AEAD_DEFAULT_TAG_LENGTH) {
  39. tag_len = M;
  40. }
  41. if (tag_len != M) {
  42. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TAG_TOO_LARGE);
  43. return 0;
  44. }
  45. struct aead_aes_ccm_ctx *ccm_ctx =
  46. OPENSSL_malloc(sizeof(struct aead_aes_ccm_ctx));
  47. if (ccm_ctx == NULL) {
  48. OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
  49. return 0;
  50. }
  51. block128_f block;
  52. ctr128_f ctr = aes_ctr_set_key(&ccm_ctx->ks.ks, NULL, &block, key, key_len);
  53. ctx->tag_len = tag_len;
  54. if (!CRYPTO_ccm128_init(&ccm_ctx->ccm, &ccm_ctx->ks.ks, block, ctr, M, L)) {
  55. OPENSSL_PUT_ERROR(CIPHER, ERR_R_INTERNAL_ERROR);
  56. OPENSSL_free(ccm_ctx);
  57. return 0;
  58. }
  59. ctx->aead_state = ccm_ctx;
  60. return 1;
  61. }
  62. static void aead_aes_ccm_cleanup(EVP_AEAD_CTX *ctx) {
  63. OPENSSL_free(ctx->aead_state);
  64. }
  65. static int aead_aes_ccm_seal_scatter(
  66. const EVP_AEAD_CTX *ctx, uint8_t *out, uint8_t *out_tag,
  67. size_t *out_tag_len, size_t max_out_tag_len, const uint8_t *nonce,
  68. size_t nonce_len, const uint8_t *in, size_t in_len, const uint8_t *extra_in,
  69. size_t extra_in_len, const uint8_t *ad, size_t ad_len) {
  70. const struct aead_aes_ccm_ctx *ccm_ctx = ctx->aead_state;
  71. if (in_len > CRYPTO_ccm128_max_input(&ccm_ctx->ccm)) {
  72. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  73. return 0;
  74. }
  75. if (max_out_tag_len < ctx->tag_len) {
  76. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
  77. return 0;
  78. }
  79. if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
  80. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
  81. return 0;
  82. }
  83. if (!CRYPTO_ccm128_encrypt(&ccm_ctx->ccm, &ccm_ctx->ks.ks, out, out_tag,
  84. ctx->tag_len, nonce, nonce_len, in, in_len, ad,
  85. ad_len)) {
  86. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  87. return 0;
  88. }
  89. *out_tag_len = ctx->tag_len;
  90. return 1;
  91. }
  92. static int aead_aes_ccm_open_gather(const EVP_AEAD_CTX *ctx, uint8_t *out,
  93. const uint8_t *nonce, size_t nonce_len,
  94. const uint8_t *in, size_t in_len,
  95. const uint8_t *in_tag, size_t in_tag_len,
  96. const uint8_t *ad, size_t ad_len) {
  97. const struct aead_aes_ccm_ctx *ccm_ctx = ctx->aead_state;
  98. if (in_len > CRYPTO_ccm128_max_input(&ccm_ctx->ccm)) {
  99. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  100. return 0;
  101. }
  102. if (nonce_len != EVP_AEAD_nonce_length(ctx->aead)) {
  103. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_NONCE_SIZE);
  104. return 0;
  105. }
  106. if (in_tag_len != ctx->tag_len) {
  107. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  108. return 0;
  109. }
  110. uint8_t tag[EVP_AEAD_AES_CCM_MAX_TAG_LEN];
  111. assert(ctx->tag_len <= EVP_AEAD_AES_CCM_MAX_TAG_LEN);
  112. if (!CRYPTO_ccm128_decrypt(&ccm_ctx->ccm, &ccm_ctx->ks.ks, out, tag,
  113. ctx->tag_len, nonce, nonce_len, in, in_len, ad,
  114. ad_len)) {
  115. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
  116. return 0;
  117. }
  118. if (CRYPTO_memcmp(tag, in_tag, ctx->tag_len) != 0) {
  119. OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
  120. return 0;
  121. }
  122. return 1;
  123. }
  124. static int aead_aes_ccm_bluetooth_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  125. size_t key_len, size_t tag_len) {
  126. return aead_aes_ccm_init(ctx, key, key_len, tag_len, 4, 2);
  127. }
  128. static const EVP_AEAD aead_aes_128_ccm_bluetooth = {
  129. 16, // key length (AES-128)
  130. 13, // nonce length
  131. 4, // overhead
  132. 4, // max tag length
  133. 0, // seal_scatter_supports_extra_in
  134. aead_aes_ccm_bluetooth_init,
  135. NULL /* init_with_direction */,
  136. aead_aes_ccm_cleanup,
  137. NULL /* open */,
  138. aead_aes_ccm_seal_scatter,
  139. aead_aes_ccm_open_gather,
  140. NULL /* get_iv */,
  141. NULL /* tag_len */,
  142. };
  143. const EVP_AEAD *EVP_aead_aes_128_ccm_bluetooth(void) {
  144. return &aead_aes_128_ccm_bluetooth;
  145. }
  146. static int aead_aes_ccm_bluetooth_8_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
  147. size_t key_len, size_t tag_len) {
  148. return aead_aes_ccm_init(ctx, key, key_len, tag_len, 8, 2);
  149. }
  150. static const EVP_AEAD aead_aes_128_ccm_bluetooth_8 = {
  151. 16, // key length (AES-128)
  152. 13, // nonce length
  153. 8, // overhead
  154. 8, // max tag length
  155. 0, // seal_scatter_supports_extra_in
  156. aead_aes_ccm_bluetooth_8_init,
  157. NULL /* init_with_direction */,
  158. aead_aes_ccm_cleanup,
  159. NULL /* open */,
  160. aead_aes_ccm_seal_scatter,
  161. aead_aes_ccm_open_gather,
  162. NULL /* get_iv */,
  163. NULL /* tag_len */,
  164. };
  165. const EVP_AEAD *EVP_aead_aes_128_ccm_bluetooth_8(void) {
  166. return &aead_aes_128_ccm_bluetooth_8;
  167. }