cmac.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* $OpenBSD: cmac.c,v 1.2 2011/01/11 15:42:05 deraadt Exp $ */
  2. /*-
  3. * Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * This code implements the CMAC (Cipher-based Message Authentication)
  19. * algorithm described in FIPS SP800-38B using the AES-128 cipher.
  20. */
  21. #include <sys/param.h>
  22. #include <sys/systm.h>
  23. #include <crypto/rijndael.h>
  24. #include <crypto/cmac.h>
  25. #define LSHIFT(v, r) do { \
  26. int i; \
  27. for (i = 0; i < 15; i++) \
  28. (r)[i] = (v)[i] << 1 | (v)[i + 1] >> 7; \
  29. (r)[15] = (v)[15] << 1; \
  30. } while (0)
  31. #define XOR(v, r) do { \
  32. int i; \
  33. for (i = 0; i < 16; i++) \
  34. (r)[i] ^= (v)[i]; \
  35. } while (0)
  36. void
  37. AES_CMAC_Init(AES_CMAC_CTX *ctx)
  38. {
  39. memset(ctx->X, 0, sizeof ctx->X);
  40. ctx->M_n = 0;
  41. }
  42. void
  43. AES_CMAC_SetKey(AES_CMAC_CTX *ctx, const u_int8_t key[AES_CMAC_KEY_LENGTH])
  44. {
  45. rijndael_set_key_enc_only(&ctx->rijndael, key, 128);
  46. }
  47. void
  48. AES_CMAC_Update(AES_CMAC_CTX *ctx, const u_int8_t *data, u_int len)
  49. {
  50. u_int mlen;
  51. if (ctx->M_n > 0) {
  52. mlen = MIN(16 - ctx->M_n, len);
  53. memcpy(ctx->M_last + ctx->M_n, data, mlen);
  54. ctx->M_n += mlen;
  55. if (ctx->M_n < 16 || len == mlen)
  56. return;
  57. XOR(ctx->M_last, ctx->X);
  58. rijndael_encrypt(&ctx->rijndael, ctx->X, ctx->X);
  59. data += mlen;
  60. len -= mlen;
  61. }
  62. while (len > 16) { /* not last block */
  63. XOR(data, ctx->X);
  64. rijndael_encrypt(&ctx->rijndael, ctx->X, ctx->X);
  65. data += 16;
  66. len -= 16;
  67. }
  68. /* potential last block, save it */
  69. memcpy(ctx->M_last, data, len);
  70. ctx->M_n = len;
  71. }
  72. void
  73. AES_CMAC_Final(u_int8_t digest[AES_CMAC_DIGEST_LENGTH], AES_CMAC_CTX *ctx)
  74. {
  75. u_int8_t K[16];
  76. /* generate subkey K1 */
  77. memset(K, 0, sizeof K);
  78. rijndael_encrypt(&ctx->rijndael, K, K);
  79. if (K[0] & 0x80) {
  80. LSHIFT(K, K);
  81. K[15] ^= 0x87;
  82. } else
  83. LSHIFT(K, K);
  84. if (ctx->M_n == 16) {
  85. /* last block was a complete block */
  86. XOR(K, ctx->M_last);
  87. } else {
  88. /* generate subkey K2 */
  89. if (K[0] & 0x80) {
  90. LSHIFT(K, K);
  91. K[15] ^= 0x87;
  92. } else
  93. LSHIFT(K, K);
  94. /* padding(M_last) */
  95. ctx->M_last[ctx->M_n] = 0x80;
  96. while (++ctx->M_n < 16)
  97. ctx->M_last[ctx->M_n] = 0;
  98. XOR(K, ctx->M_last);
  99. }
  100. XOR(ctx->M_last, ctx->X);
  101. rijndael_encrypt(&ctx->rijndael, ctx->X, digest);
  102. explicit_bzero(K, sizeof K);
  103. }