Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

279 řádky
8.5 KiB

  1. /* ====================================================================
  2. * Copyright (c) 2010 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * licensing@OpenSSL.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ==================================================================== */
  48. #include <openssl/cmac.h>
  49. #include <assert.h>
  50. #include <string.h>
  51. #include <openssl/aes.h>
  52. #include <openssl/cipher.h>
  53. #include <openssl/mem.h>
  54. #include "../internal.h"
  55. struct cmac_ctx_st {
  56. EVP_CIPHER_CTX cipher_ctx;
  57. // k1 and k2 are the CMAC subkeys. See
  58. // https://tools.ietf.org/html/rfc4493#section-2.3
  59. uint8_t k1[AES_BLOCK_SIZE];
  60. uint8_t k2[AES_BLOCK_SIZE];
  61. // Last (possibly partial) scratch
  62. uint8_t block[AES_BLOCK_SIZE];
  63. // block_used contains the number of valid bytes in |block|.
  64. unsigned block_used;
  65. };
  66. static void CMAC_CTX_init(CMAC_CTX *ctx) {
  67. EVP_CIPHER_CTX_init(&ctx->cipher_ctx);
  68. }
  69. static void CMAC_CTX_cleanup(CMAC_CTX *ctx) {
  70. EVP_CIPHER_CTX_cleanup(&ctx->cipher_ctx);
  71. OPENSSL_cleanse(ctx->k1, sizeof(ctx->k1));
  72. OPENSSL_cleanse(ctx->k2, sizeof(ctx->k2));
  73. OPENSSL_cleanse(ctx->block, sizeof(ctx->block));
  74. }
  75. int AES_CMAC(uint8_t out[16], const uint8_t *key, size_t key_len,
  76. const uint8_t *in, size_t in_len) {
  77. const EVP_CIPHER *cipher;
  78. switch (key_len) {
  79. case 16:
  80. cipher = EVP_aes_128_cbc();
  81. break;
  82. case 32:
  83. cipher = EVP_aes_256_cbc();
  84. break;
  85. default:
  86. return 0;
  87. }
  88. size_t scratch_out_len;
  89. CMAC_CTX ctx;
  90. CMAC_CTX_init(&ctx);
  91. const int ok = CMAC_Init(&ctx, key, key_len, cipher, NULL /* engine */) &&
  92. CMAC_Update(&ctx, in, in_len) &&
  93. CMAC_Final(&ctx, out, &scratch_out_len);
  94. CMAC_CTX_cleanup(&ctx);
  95. return ok;
  96. }
  97. CMAC_CTX *CMAC_CTX_new(void) {
  98. CMAC_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
  99. if (ctx != NULL) {
  100. CMAC_CTX_init(ctx);
  101. }
  102. return ctx;
  103. }
  104. void CMAC_CTX_free(CMAC_CTX *ctx) {
  105. if (ctx == NULL) {
  106. return;
  107. }
  108. CMAC_CTX_cleanup(ctx);
  109. OPENSSL_free(ctx);
  110. }
  111. int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in) {
  112. if (!EVP_CIPHER_CTX_copy(&out->cipher_ctx, &in->cipher_ctx)) {
  113. return 0;
  114. }
  115. OPENSSL_memcpy(out->k1, in->k1, AES_BLOCK_SIZE);
  116. OPENSSL_memcpy(out->k2, in->k2, AES_BLOCK_SIZE);
  117. OPENSSL_memcpy(out->block, in->block, AES_BLOCK_SIZE);
  118. out->block_used = in->block_used;
  119. return 1;
  120. }
  121. // binary_field_mul_x_128 treats the 128 bits at |in| as an element of GF(2¹²⁸)
  122. // with a hard-coded reduction polynomial and sets |out| as x times the input.
  123. //
  124. // See https://tools.ietf.org/html/rfc4493#section-2.3
  125. static void binary_field_mul_x_128(uint8_t out[16], const uint8_t in[16]) {
  126. unsigned i;
  127. // Shift |in| to left, including carry.
  128. for (i = 0; i < 15; i++) {
  129. out[i] = (in[i] << 1) | (in[i+1] >> 7);
  130. }
  131. // If MSB set fixup with R.
  132. const uint8_t carry = in[0] >> 7;
  133. out[i] = (in[i] << 1) ^ ((0 - carry) & 0x87);
  134. }
  135. // binary_field_mul_x_64 behaves like |binary_field_mul_x_128| but acts on an
  136. // element of GF(2⁶⁴).
  137. //
  138. // See https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38b.pdf
  139. static void binary_field_mul_x_64(uint8_t out[8], const uint8_t in[8]) {
  140. unsigned i;
  141. // Shift |in| to left, including carry.
  142. for (i = 0; i < 7; i++) {
  143. out[i] = (in[i] << 1) | (in[i+1] >> 7);
  144. }
  145. // If MSB set fixup with R.
  146. const uint8_t carry = in[0] >> 7;
  147. out[i] = (in[i] << 1) ^ ((0 - carry) & 0x1b);
  148. }
  149. static const uint8_t kZeroIV[AES_BLOCK_SIZE] = {0};
  150. int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t key_len,
  151. const EVP_CIPHER *cipher, ENGINE *engine) {
  152. uint8_t scratch[AES_BLOCK_SIZE];
  153. size_t block_size = EVP_CIPHER_block_size(cipher);
  154. if ((block_size != AES_BLOCK_SIZE && block_size != 8 /* 3-DES */) ||
  155. EVP_CIPHER_key_length(cipher) != key_len ||
  156. !EVP_EncryptInit_ex(&ctx->cipher_ctx, cipher, NULL, key, kZeroIV) ||
  157. !EVP_Cipher(&ctx->cipher_ctx, scratch, kZeroIV, block_size) ||
  158. // Reset context again ready for first data.
  159. !EVP_EncryptInit_ex(&ctx->cipher_ctx, NULL, NULL, NULL, kZeroIV)) {
  160. return 0;
  161. }
  162. if (block_size == AES_BLOCK_SIZE) {
  163. binary_field_mul_x_128(ctx->k1, scratch);
  164. binary_field_mul_x_128(ctx->k2, ctx->k1);
  165. } else {
  166. binary_field_mul_x_64(ctx->k1, scratch);
  167. binary_field_mul_x_64(ctx->k2, ctx->k1);
  168. }
  169. ctx->block_used = 0;
  170. return 1;
  171. }
  172. int CMAC_Reset(CMAC_CTX *ctx) {
  173. ctx->block_used = 0;
  174. return EVP_EncryptInit_ex(&ctx->cipher_ctx, NULL, NULL, NULL, kZeroIV);
  175. }
  176. int CMAC_Update(CMAC_CTX *ctx, const uint8_t *in, size_t in_len) {
  177. size_t block_size = EVP_CIPHER_CTX_block_size(&ctx->cipher_ctx);
  178. assert(block_size <= AES_BLOCK_SIZE);
  179. uint8_t scratch[AES_BLOCK_SIZE];
  180. if (ctx->block_used > 0) {
  181. size_t todo = block_size - ctx->block_used;
  182. if (in_len < todo) {
  183. todo = in_len;
  184. }
  185. OPENSSL_memcpy(ctx->block + ctx->block_used, in, todo);
  186. in += todo;
  187. in_len -= todo;
  188. ctx->block_used += todo;
  189. // If |in_len| is zero then either |ctx->block_used| is less than
  190. // |block_size|, in which case we can stop here, or |ctx->block_used| is
  191. // exactly |block_size| but there's no more data to process. In the latter
  192. // case we don't want to process this block now because it might be the last
  193. // block and that block is treated specially.
  194. if (in_len == 0) {
  195. return 1;
  196. }
  197. assert(ctx->block_used == block_size);
  198. if (!EVP_Cipher(&ctx->cipher_ctx, scratch, ctx->block, block_size)) {
  199. return 0;
  200. }
  201. }
  202. // Encrypt all but one of the remaining blocks.
  203. while (in_len > block_size) {
  204. if (!EVP_Cipher(&ctx->cipher_ctx, scratch, in, block_size)) {
  205. return 0;
  206. }
  207. in += block_size;
  208. in_len -= block_size;
  209. }
  210. OPENSSL_memcpy(ctx->block, in, in_len);
  211. ctx->block_used = in_len;
  212. return 1;
  213. }
  214. int CMAC_Final(CMAC_CTX *ctx, uint8_t *out, size_t *out_len) {
  215. size_t block_size = EVP_CIPHER_CTX_block_size(&ctx->cipher_ctx);
  216. assert(block_size <= AES_BLOCK_SIZE);
  217. *out_len = block_size;
  218. if (out == NULL) {
  219. return 1;
  220. }
  221. const uint8_t *mask = ctx->k1;
  222. if (ctx->block_used != block_size) {
  223. // If the last block is incomplete, terminate it with a single 'one' bit
  224. // followed by zeros.
  225. ctx->block[ctx->block_used] = 0x80;
  226. OPENSSL_memset(ctx->block + ctx->block_used + 1, 0,
  227. block_size - (ctx->block_used + 1));
  228. mask = ctx->k2;
  229. }
  230. for (unsigned i = 0; i < block_size; i++) {
  231. out[i] = ctx->block[i] ^ mask[i];
  232. }
  233. return EVP_Cipher(&ctx->cipher_ctx, out, out, block_size);
  234. }