cmac.c 5.4 KB

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