aes-internal-dec.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * AES (Rijndael) cipher - decrypt
  3. *
  4. * Modifications to public domain implementation:
  5. * - support only 128-bit keys
  6. * - cleanup
  7. * - use C pre-processor to make it easier to change S table access
  8. * - added option (AES_SMALL_TABLES) for reducing code size by about 8 kB at
  9. * cost of reduced throughput (quite small difference on Pentium 4,
  10. * 10-25% when using -O1 or -O2 optimization)
  11. *
  12. * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. *
  18. * Alternatively, this software may be distributed under the terms of BSD
  19. * license.
  20. *
  21. * See README and COPYING for more details.
  22. */
  23. #include "includes.h"
  24. #include "common.h"
  25. #include "crypto.h"
  26. #include "aes_i.h"
  27. /**
  28. * Expand the cipher key into the decryption key schedule.
  29. *
  30. * @return the number of rounds for the given cipher key size.
  31. */
  32. static void rijndaelKeySetupDec(u32 rk[/*44*/], const u8 cipherKey[])
  33. {
  34. int Nr = 10, i, j;
  35. u32 temp;
  36. /* expand the cipher key: */
  37. rijndaelKeySetupEnc(rk, cipherKey);
  38. /* invert the order of the round keys: */
  39. for (i = 0, j = 4*Nr; i < j; i += 4, j -= 4) {
  40. temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp;
  41. temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp;
  42. temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp;
  43. temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp;
  44. }
  45. /* apply the inverse MixColumn transform to all round keys but the
  46. * first and the last: */
  47. for (i = 1; i < Nr; i++) {
  48. rk += 4;
  49. for (j = 0; j < 4; j++) {
  50. rk[j] = TD0_(TE4((rk[j] >> 24) )) ^
  51. TD1_(TE4((rk[j] >> 16) & 0xff)) ^
  52. TD2_(TE4((rk[j] >> 8) & 0xff)) ^
  53. TD3_(TE4((rk[j] ) & 0xff));
  54. }
  55. }
  56. }
  57. void * aes_decrypt_init(const u8 *key, size_t len)
  58. {
  59. u32 *rk;
  60. if (len != 16)
  61. return NULL;
  62. rk = os_malloc(AES_PRIV_SIZE);
  63. if (rk == NULL)
  64. return NULL;
  65. rijndaelKeySetupDec(rk, key);
  66. return rk;
  67. }
  68. static void rijndaelDecrypt(const u32 rk[/*44*/], const u8 ct[16], u8 pt[16])
  69. {
  70. u32 s0, s1, s2, s3, t0, t1, t2, t3;
  71. const int Nr = 10;
  72. #ifndef FULL_UNROLL
  73. int r;
  74. #endif /* ?FULL_UNROLL */
  75. /*
  76. * map byte array block to cipher state
  77. * and add initial round key:
  78. */
  79. s0 = GETU32(ct ) ^ rk[0];
  80. s1 = GETU32(ct + 4) ^ rk[1];
  81. s2 = GETU32(ct + 8) ^ rk[2];
  82. s3 = GETU32(ct + 12) ^ rk[3];
  83. #define ROUND(i,d,s) \
  84. d##0 = TD0(s##0) ^ TD1(s##3) ^ TD2(s##2) ^ TD3(s##1) ^ rk[4 * i]; \
  85. d##1 = TD0(s##1) ^ TD1(s##0) ^ TD2(s##3) ^ TD3(s##2) ^ rk[4 * i + 1]; \
  86. d##2 = TD0(s##2) ^ TD1(s##1) ^ TD2(s##0) ^ TD3(s##3) ^ rk[4 * i + 2]; \
  87. d##3 = TD0(s##3) ^ TD1(s##2) ^ TD2(s##1) ^ TD3(s##0) ^ rk[4 * i + 3]
  88. #ifdef FULL_UNROLL
  89. ROUND(1,t,s);
  90. ROUND(2,s,t);
  91. ROUND(3,t,s);
  92. ROUND(4,s,t);
  93. ROUND(5,t,s);
  94. ROUND(6,s,t);
  95. ROUND(7,t,s);
  96. ROUND(8,s,t);
  97. ROUND(9,t,s);
  98. rk += Nr << 2;
  99. #else /* !FULL_UNROLL */
  100. /* Nr - 1 full rounds: */
  101. r = Nr >> 1;
  102. for (;;) {
  103. ROUND(1,t,s);
  104. rk += 8;
  105. if (--r == 0)
  106. break;
  107. ROUND(0,s,t);
  108. }
  109. #endif /* ?FULL_UNROLL */
  110. #undef ROUND
  111. /*
  112. * apply last round and
  113. * map cipher state to byte array block:
  114. */
  115. s0 = TD41(t0) ^ TD42(t3) ^ TD43(t2) ^ TD44(t1) ^ rk[0];
  116. PUTU32(pt , s0);
  117. s1 = TD41(t1) ^ TD42(t0) ^ TD43(t3) ^ TD44(t2) ^ rk[1];
  118. PUTU32(pt + 4, s1);
  119. s2 = TD41(t2) ^ TD42(t1) ^ TD43(t0) ^ TD44(t3) ^ rk[2];
  120. PUTU32(pt + 8, s2);
  121. s3 = TD41(t3) ^ TD42(t2) ^ TD43(t1) ^ TD44(t0) ^ rk[3];
  122. PUTU32(pt + 12, s3);
  123. }
  124. void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
  125. {
  126. rijndaelDecrypt(ctx, crypt, plain);
  127. }
  128. void aes_decrypt_deinit(void *ctx)
  129. {
  130. os_memset(ctx, 0, AES_PRIV_SIZE);
  131. os_free(ctx);
  132. }