cmac.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright 2010-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "internal/cryptlib.h"
  13. #include <openssl/cmac.h>
  14. struct CMAC_CTX_st {
  15. /* Cipher context to use */
  16. EVP_CIPHER_CTX *cctx;
  17. /* Keys k1 and k2 */
  18. unsigned char k1[EVP_MAX_BLOCK_LENGTH];
  19. unsigned char k2[EVP_MAX_BLOCK_LENGTH];
  20. /* Temporary block */
  21. unsigned char tbl[EVP_MAX_BLOCK_LENGTH];
  22. /* Last (possibly partial) block */
  23. unsigned char last_block[EVP_MAX_BLOCK_LENGTH];
  24. /* Number of bytes in last block: -1 means context not initialised */
  25. int nlast_block;
  26. };
  27. /* Make temporary keys K1 and K2 */
  28. static void make_kn(unsigned char *k1, const unsigned char *l, int bl)
  29. {
  30. int i;
  31. unsigned char c = l[0], carry = c >> 7, cnext;
  32. /* Shift block to left, including carry */
  33. for (i = 0; i < bl - 1; i++, c = cnext)
  34. k1[i] = (c << 1) | ((cnext = l[i + 1]) >> 7);
  35. /* If MSB set fixup with R */
  36. k1[i] = (c << 1) ^ ((0 - carry) & (bl == 16 ? 0x87 : 0x1b));
  37. }
  38. CMAC_CTX *CMAC_CTX_new(void)
  39. {
  40. CMAC_CTX *ctx;
  41. ctx = OPENSSL_malloc(sizeof(*ctx));
  42. if (ctx == NULL)
  43. return NULL;
  44. ctx->cctx = EVP_CIPHER_CTX_new();
  45. if (ctx->cctx == NULL) {
  46. OPENSSL_free(ctx);
  47. return NULL;
  48. }
  49. ctx->nlast_block = -1;
  50. return ctx;
  51. }
  52. void CMAC_CTX_cleanup(CMAC_CTX *ctx)
  53. {
  54. EVP_CIPHER_CTX_reset(ctx->cctx);
  55. OPENSSL_cleanse(ctx->tbl, EVP_MAX_BLOCK_LENGTH);
  56. OPENSSL_cleanse(ctx->k1, EVP_MAX_BLOCK_LENGTH);
  57. OPENSSL_cleanse(ctx->k2, EVP_MAX_BLOCK_LENGTH);
  58. OPENSSL_cleanse(ctx->last_block, EVP_MAX_BLOCK_LENGTH);
  59. ctx->nlast_block = -1;
  60. }
  61. EVP_CIPHER_CTX *CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx)
  62. {
  63. return ctx->cctx;
  64. }
  65. void CMAC_CTX_free(CMAC_CTX *ctx)
  66. {
  67. if (!ctx)
  68. return;
  69. CMAC_CTX_cleanup(ctx);
  70. EVP_CIPHER_CTX_free(ctx->cctx);
  71. OPENSSL_free(ctx);
  72. }
  73. int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in)
  74. {
  75. int bl;
  76. if (in->nlast_block == -1)
  77. return 0;
  78. if (!EVP_CIPHER_CTX_copy(out->cctx, in->cctx))
  79. return 0;
  80. bl = EVP_CIPHER_CTX_block_size(in->cctx);
  81. memcpy(out->k1, in->k1, bl);
  82. memcpy(out->k2, in->k2, bl);
  83. memcpy(out->tbl, in->tbl, bl);
  84. memcpy(out->last_block, in->last_block, bl);
  85. out->nlast_block = in->nlast_block;
  86. return 1;
  87. }
  88. int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
  89. const EVP_CIPHER *cipher, ENGINE *impl)
  90. {
  91. static const unsigned char zero_iv[EVP_MAX_BLOCK_LENGTH] = { 0 };
  92. /* All zeros means restart */
  93. if (!key && !cipher && !impl && keylen == 0) {
  94. /* Not initialised */
  95. if (ctx->nlast_block == -1)
  96. return 0;
  97. if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
  98. return 0;
  99. memset(ctx->tbl, 0, EVP_CIPHER_CTX_block_size(ctx->cctx));
  100. ctx->nlast_block = 0;
  101. return 1;
  102. }
  103. /* Initialise context */
  104. if (cipher && !EVP_EncryptInit_ex(ctx->cctx, cipher, impl, NULL, NULL))
  105. return 0;
  106. /* Non-NULL key means initialisation complete */
  107. if (key) {
  108. int bl;
  109. if (!EVP_CIPHER_CTX_cipher(ctx->cctx))
  110. return 0;
  111. if (!EVP_CIPHER_CTX_set_key_length(ctx->cctx, keylen))
  112. return 0;
  113. if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, key, zero_iv))
  114. return 0;
  115. bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
  116. if (!EVP_Cipher(ctx->cctx, ctx->tbl, zero_iv, bl))
  117. return 0;
  118. make_kn(ctx->k1, ctx->tbl, bl);
  119. make_kn(ctx->k2, ctx->k1, bl);
  120. OPENSSL_cleanse(ctx->tbl, bl);
  121. /* Reset context again ready for first data block */
  122. if (!EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, zero_iv))
  123. return 0;
  124. /* Zero tbl so resume works */
  125. memset(ctx->tbl, 0, bl);
  126. ctx->nlast_block = 0;
  127. }
  128. return 1;
  129. }
  130. int CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen)
  131. {
  132. const unsigned char *data = in;
  133. size_t bl;
  134. if (ctx->nlast_block == -1)
  135. return 0;
  136. if (dlen == 0)
  137. return 1;
  138. bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
  139. /* Copy into partial block if we need to */
  140. if (ctx->nlast_block > 0) {
  141. size_t nleft;
  142. nleft = bl - ctx->nlast_block;
  143. if (dlen < nleft)
  144. nleft = dlen;
  145. memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
  146. dlen -= nleft;
  147. ctx->nlast_block += nleft;
  148. /* If no more to process return */
  149. if (dlen == 0)
  150. return 1;
  151. data += nleft;
  152. /* Else not final block so encrypt it */
  153. if (!EVP_Cipher(ctx->cctx, ctx->tbl, ctx->last_block, bl))
  154. return 0;
  155. }
  156. /* Encrypt all but one of the complete blocks left */
  157. while (dlen > bl) {
  158. if (!EVP_Cipher(ctx->cctx, ctx->tbl, data, bl))
  159. return 0;
  160. dlen -= bl;
  161. data += bl;
  162. }
  163. /* Copy any data left to last block buffer */
  164. memcpy(ctx->last_block, data, dlen);
  165. ctx->nlast_block = dlen;
  166. return 1;
  167. }
  168. int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen)
  169. {
  170. int i, bl, lb;
  171. if (ctx->nlast_block == -1)
  172. return 0;
  173. bl = EVP_CIPHER_CTX_block_size(ctx->cctx);
  174. *poutlen = (size_t)bl;
  175. if (!out)
  176. return 1;
  177. lb = ctx->nlast_block;
  178. /* Is last block complete? */
  179. if (lb == bl) {
  180. for (i = 0; i < bl; i++)
  181. out[i] = ctx->last_block[i] ^ ctx->k1[i];
  182. } else {
  183. ctx->last_block[lb] = 0x80;
  184. if (bl - lb > 1)
  185. memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
  186. for (i = 0; i < bl; i++)
  187. out[i] = ctx->last_block[i] ^ ctx->k2[i];
  188. }
  189. if (!EVP_Cipher(ctx->cctx, out, out, bl)) {
  190. OPENSSL_cleanse(out, bl);
  191. return 0;
  192. }
  193. return 1;
  194. }
  195. int CMAC_resume(CMAC_CTX *ctx)
  196. {
  197. if (ctx->nlast_block == -1)
  198. return 0;
  199. /*
  200. * The buffer "tbl" contains the last fully encrypted block which is the
  201. * last IV (or all zeroes if no last encrypted block). The last block has
  202. * not been modified since CMAC_final(). So reinitialising using the last
  203. * decrypted block will allow CMAC to continue after calling
  204. * CMAC_Final().
  205. */
  206. return EVP_EncryptInit_ex(ctx->cctx, NULL, NULL, NULL, ctx->tbl);
  207. }