cmac.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* crypto/cmac/cmac.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2010 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. */
  53. #define OPENSSL_FIPSAPI
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include "cryptlib.h"
  58. #include <openssl/cmac.h>
  59. struct CMAC_CTX_st
  60. {
  61. /* Cipher context to use */
  62. EVP_CIPHER_CTX cctx;
  63. /* Keys k1 and k2 */
  64. unsigned char k1[EVP_MAX_BLOCK_LENGTH];
  65. unsigned char k2[EVP_MAX_BLOCK_LENGTH];
  66. /* Temporary block */
  67. unsigned char tbl[EVP_MAX_BLOCK_LENGTH];
  68. /* Last (possibly partial) block */
  69. unsigned char last_block[EVP_MAX_BLOCK_LENGTH];
  70. /* Number of bytes in last block: -1 means context not initialised */
  71. int nlast_block;
  72. };
  73. /* Make temporary keys K1 and K2 */
  74. static void make_kn(unsigned char *k1, unsigned char *l, int bl)
  75. {
  76. int i;
  77. /* Shift block to left, including carry */
  78. for (i = 0; i < bl; i++)
  79. {
  80. k1[i] = l[i] << 1;
  81. if (i < bl - 1 && l[i + 1] & 0x80)
  82. k1[i] |= 1;
  83. }
  84. /* If MSB set fixup with R */
  85. if (l[0] & 0x80)
  86. k1[bl - 1] ^= bl == 16 ? 0x87 : 0x1b;
  87. }
  88. CMAC_CTX *CMAC_CTX_new(void)
  89. {
  90. CMAC_CTX *ctx;
  91. ctx = OPENSSL_malloc(sizeof(CMAC_CTX));
  92. if (!ctx)
  93. return NULL;
  94. EVP_CIPHER_CTX_init(&ctx->cctx);
  95. ctx->nlast_block = -1;
  96. return ctx;
  97. }
  98. void CMAC_CTX_cleanup(CMAC_CTX *ctx)
  99. {
  100. EVP_CIPHER_CTX_cleanup(&ctx->cctx);
  101. OPENSSL_cleanse(ctx->tbl, EVP_MAX_BLOCK_LENGTH);
  102. OPENSSL_cleanse(ctx->k1, EVP_MAX_BLOCK_LENGTH);
  103. OPENSSL_cleanse(ctx->k2, EVP_MAX_BLOCK_LENGTH);
  104. OPENSSL_cleanse(ctx->last_block, EVP_MAX_BLOCK_LENGTH);
  105. ctx->nlast_block = -1;
  106. }
  107. EVP_CIPHER_CTX *CMAC_CTX_get0_cipher_ctx(CMAC_CTX *ctx)
  108. {
  109. return &ctx->cctx;
  110. }
  111. void CMAC_CTX_free(CMAC_CTX *ctx)
  112. {
  113. CMAC_CTX_cleanup(ctx);
  114. OPENSSL_free(ctx);
  115. }
  116. int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in)
  117. {
  118. int bl;
  119. if (in->nlast_block == -1)
  120. return 0;
  121. if (!EVP_CIPHER_CTX_copy(&out->cctx, &in->cctx))
  122. return 0;
  123. bl = M_EVP_CIPHER_CTX_block_size(&in->cctx);
  124. memcpy(out->k1, in->k1, bl);
  125. memcpy(out->k2, in->k2, bl);
  126. memcpy(out->tbl, in->tbl, bl);
  127. memcpy(out->last_block, in->last_block, bl);
  128. out->nlast_block = in->nlast_block;
  129. return 1;
  130. }
  131. int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
  132. const EVP_CIPHER *cipher, ENGINE *impl)
  133. {
  134. static unsigned char zero_iv[EVP_MAX_BLOCK_LENGTH];
  135. /* All zeros means restart */
  136. if (!key && !cipher && !impl && keylen == 0)
  137. {
  138. /* Not initialised */
  139. if (ctx->nlast_block == -1)
  140. return 0;
  141. if (!M_EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, NULL, zero_iv))
  142. return 0;
  143. return 1;
  144. }
  145. /* Initialiase context */
  146. if (cipher && !M_EVP_EncryptInit_ex(&ctx->cctx, cipher, impl, NULL, NULL))
  147. return 0;
  148. /* Non-NULL key means initialisation complete */
  149. if (key)
  150. {
  151. int bl;
  152. if (!M_EVP_CIPHER_CTX_cipher(&ctx->cctx))
  153. return 0;
  154. if (!EVP_CIPHER_CTX_set_key_length(&ctx->cctx, keylen))
  155. return 0;
  156. if (!M_EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, key, zero_iv))
  157. return 0;
  158. bl = M_EVP_CIPHER_CTX_block_size(&ctx->cctx);
  159. if (!EVP_Cipher(&ctx->cctx, ctx->tbl, zero_iv, bl))
  160. return 0;
  161. make_kn(ctx->k1, ctx->tbl, bl);
  162. make_kn(ctx->k2, ctx->k1, bl);
  163. OPENSSL_cleanse(ctx->tbl, bl);
  164. /* Reset context again ready for first data block */
  165. if (!M_EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, NULL, zero_iv))
  166. return 0;
  167. /* Zero tbl so resume works */
  168. memset(ctx->tbl, 0, bl);
  169. ctx->nlast_block = 0;
  170. }
  171. return 1;
  172. }
  173. int CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen)
  174. {
  175. const unsigned char *data = in;
  176. size_t bl;
  177. if (ctx->nlast_block == -1)
  178. return 0;
  179. if (dlen == 0)
  180. return 1;
  181. bl = M_EVP_CIPHER_CTX_block_size(&ctx->cctx);
  182. /* Copy into partial block if we need to */
  183. if (ctx->nlast_block > 0)
  184. {
  185. size_t nleft;
  186. nleft = bl - ctx->nlast_block;
  187. if (dlen < nleft)
  188. nleft = dlen;
  189. memcpy(ctx->last_block + ctx->nlast_block, data, nleft);
  190. dlen -= nleft;
  191. ctx->nlast_block += nleft;
  192. /* If no more to process return */
  193. if (dlen == 0)
  194. return 1;
  195. data += nleft;
  196. /* Else not final block so encrypt it */
  197. if (!EVP_Cipher(&ctx->cctx, ctx->tbl, ctx->last_block,bl))
  198. return 0;
  199. }
  200. /* Encrypt all but one of the complete blocks left */
  201. while(dlen > bl)
  202. {
  203. if (!EVP_Cipher(&ctx->cctx, ctx->tbl, data, bl))
  204. return 0;
  205. dlen -= bl;
  206. data += bl;
  207. }
  208. /* Copy any data left to last block buffer */
  209. memcpy(ctx->last_block, data, dlen);
  210. ctx->nlast_block = dlen;
  211. return 1;
  212. }
  213. int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen)
  214. {
  215. int i, bl, lb;
  216. if (ctx->nlast_block == -1)
  217. return 0;
  218. bl = M_EVP_CIPHER_CTX_block_size(&ctx->cctx);
  219. *poutlen = (size_t)bl;
  220. if (!out)
  221. return 1;
  222. lb = ctx->nlast_block;
  223. /* Is last block complete? */
  224. if (lb == bl)
  225. {
  226. for (i = 0; i < bl; i++)
  227. out[i] = ctx->last_block[i] ^ ctx->k1[i];
  228. }
  229. else
  230. {
  231. ctx->last_block[lb] = 0x80;
  232. if (bl - lb > 1)
  233. memset(ctx->last_block + lb + 1, 0, bl - lb - 1);
  234. for (i = 0; i < bl; i++)
  235. out[i] = ctx->last_block[i] ^ ctx->k2[i];
  236. }
  237. if (!EVP_Cipher(&ctx->cctx, out, out, bl))
  238. {
  239. OPENSSL_cleanse(out, bl);
  240. return 0;
  241. }
  242. return 1;
  243. }
  244. int CMAC_resume(CMAC_CTX *ctx)
  245. {
  246. if (ctx->nlast_block == -1)
  247. return 0;
  248. /* The buffer "tbl" containes the last fully encrypted block
  249. * which is the last IV (or all zeroes if no last encrypted block).
  250. * The last block has not been modified since CMAC_final().
  251. * So reinitliasing using the last decrypted block will allow
  252. * CMAC to continue after calling CMAC_Final().
  253. */
  254. return M_EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, NULL, ctx->tbl);
  255. }