cmac.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #line __LINE__ "cmac.c"
  2. /**************************************************************************
  3. Copyright (C) 2009 Lander Casado, Philippas Tsigas
  4. All rights reserved.
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files
  7. (the "Software"), to deal with the Software without restriction, including
  8. without limitation the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the Software, and to
  10. permit persons to whom the Software is furnished to do so, subject to
  11. the following conditions:
  12. Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimers. Redistributions in
  14. binary form must reproduce the above copyright notice, this list of
  15. conditions and the following disclaimers in the documentation and/or
  16. other materials provided with the distribution.
  17. In no event shall the authors or copyright holders be liable for any special,
  18. incidental, indirect or consequential damages of any kind, or any damages
  19. whatsoever resulting from loss of use, data or profits, whether or not
  20. advised of the possibility of damage, and on any theory of liability,
  21. arising out of or in connection with the use or performance of this software.
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  23. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  28. DEALINGS WITH THE SOFTWARE
  29. *****************************************************************************/
  30. //#include <sys/param.h>
  31. //#include <sys/systm.h>
  32. #include "aes.h"
  33. #include "cmac.h"
  34. #include <string.h>
  35. //#include <memory.h>
  36. #define LSHIFT(v, r) do { \
  37. int i; \
  38. for (i = 0; i < 15; i++) \
  39. (r)[i] = (v)[i] << 1 | (v)[i + 1] >> 7; \
  40. (r)[15] = (v)[15] << 1; \
  41. } while (0)
  42. #define XOR(v, r) do { \
  43. int i; \
  44. for (i = 0; i < 16; i++) \
  45. { \
  46. (r)[i] = (r)[i] ^ (v)[i]; \
  47. } \
  48. } while (0) \
  49. #define MIN(a,b) (((a)<(b))?(a):(b))
  50. void AES_CMAC_Init(AES_CMAC_CTX *ctx)
  51. {
  52. memset(ctx->X, 0, sizeof ctx->X);
  53. ctx->M_n = 0;
  54. memset(ctx->rijndael.ksch, '\0', sizeof(ctx->rijndael.ksch));
  55. }
  56. void AES_CMAC_SetKey(AES_CMAC_CTX *ctx, const u_int8_t key[AES_CMAC_KEY_LENGTH])
  57. {
  58. //rijndael_set_key_enc_only(&ctx->rijndael, key, 128);
  59. aes_set_key( key, AES_CMAC_KEY_LENGTH, &ctx->rijndael);
  60. }
  61. void AES_CMAC_Update(AES_CMAC_CTX *ctx, const u_int8_t *data, u_int len)
  62. {
  63. u_int mlen;
  64. unsigned char in[16];
  65. if (ctx->M_n > 0) {
  66. mlen = MIN(16 - ctx->M_n, len);
  67. memcpy(ctx->M_last + ctx->M_n, data, mlen);
  68. ctx->M_n += mlen;
  69. if (ctx->M_n < 16 || len == mlen)
  70. return;
  71. XOR(ctx->M_last, ctx->X);
  72. //rijndael_encrypt(&ctx->rijndael, ctx->X, ctx->X);
  73. aes_encrypt( ctx->X, ctx->X, &ctx->rijndael);
  74. data += mlen;
  75. len -= mlen;
  76. }
  77. while (len > 16) { /* not last block */
  78. XOR(data, ctx->X);
  79. //rijndael_encrypt(&ctx->rijndael, ctx->X, ctx->X);
  80. memcpy(in, &ctx->X[0], 16); //Bestela ez du ondo iten
  81. aes_encrypt( in, in, &ctx->rijndael);
  82. memcpy(&ctx->X[0], in, 16);
  83. data += 16;
  84. len -= 16;
  85. }
  86. /* potential last block, save it */
  87. memcpy(ctx->M_last, data, len);
  88. ctx->M_n = len;
  89. }
  90. void AES_CMAC_Final(u_int8_t digest[AES_CMAC_DIGEST_LENGTH], AES_CMAC_CTX *ctx)
  91. {
  92. u_int8_t K[16];
  93. unsigned char in[16];
  94. /* generate subkey K1 */
  95. memset(K, '\0', 16);
  96. //rijndael_encrypt(&ctx->rijndael, K, K);
  97. aes_encrypt( K, K, &ctx->rijndael);
  98. if (K[0] & 0x80) {
  99. LSHIFT(K, K);
  100. K[15] ^= 0x87;
  101. } else
  102. LSHIFT(K, K);
  103. if (ctx->M_n == 16) {
  104. /* last block was a complete block */
  105. XOR(K, ctx->M_last);
  106. } else {
  107. /* generate subkey K2 */
  108. if (K[0] & 0x80) {
  109. LSHIFT(K, K);
  110. K[15] ^= 0x87;
  111. } else
  112. LSHIFT(K, K);
  113. /* padding(M_last) */
  114. ctx->M_last[ctx->M_n] = 0x80;
  115. while (++ctx->M_n < 16)
  116. ctx->M_last[ctx->M_n] = 0;
  117. XOR(K, ctx->M_last);
  118. }
  119. XOR(ctx->M_last, ctx->X);
  120. //rijndael_encrypt(&ctx->rijndael, ctx->X, digest);
  121. memcpy(in, &ctx->X[0], 16); //Bestela ez du ondo iten
  122. aes_encrypt(in, digest, &ctx->rijndael);
  123. memset(K, 0, sizeof K);
  124. }