aes_dec.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* aes.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <crypto/gf256mul.h>
  19. #include <crypto/aes.h>
  20. #include <crypto/aes_invsbox.h>
  21. #include <crypto/aes_dec.h>
  22. void aes_invshiftrow(void* data, uint8_t shift){
  23. uint8_t tmp[4];
  24. tmp[0] = ((uint8_t*)data)[(4+0-shift)&3];
  25. tmp[1] = ((uint8_t*)data)[(4+1-shift)&3];
  26. tmp[2] = ((uint8_t*)data)[(4+2-shift)&3];
  27. tmp[3] = ((uint8_t*)data)[(4+3-shift)&3];
  28. memcpy(data, tmp, 4);
  29. }
  30. void aes_invshiftcol(void* data, uint8_t shift){
  31. uint8_t tmp[4];
  32. tmp[0] = ((uint8_t*)data)[ 0];
  33. tmp[1] = ((uint8_t*)data)[ 4];
  34. tmp[2] = ((uint8_t*)data)[ 8];
  35. tmp[3] = ((uint8_t*)data)[12];
  36. ((uint8_t*)data)[ 0] = tmp[(4-shift+0)&3];
  37. ((uint8_t*)data)[ 4] = tmp[(4-shift+1)&3];
  38. ((uint8_t*)data)[ 8] = tmp[(4-shift+2)&3];
  39. ((uint8_t*)data)[12] = tmp[(4-shift+3)&3];
  40. }
  41. static
  42. void aes_dec_round(aes_cipher_state_t* state, const aes_roundkey_t* k){
  43. uint8_t tmp[16];
  44. uint8_t i;
  45. uint8_t t,u,v,w;
  46. /* keyAdd */
  47. for(i=0; i<16; ++i){
  48. tmp[i] = state->s[i] ^ k->ks[i];
  49. }
  50. /* mixColums */
  51. for(i=0; i<4; ++i){
  52. t = tmp[4*i+3] ^ tmp[4*i+2];
  53. u = tmp[4*i+1] ^ tmp[4*i+0];
  54. v = t ^ u;
  55. v = gf256mul(0x09, v, 0x1b);
  56. w = v ^ gf256mul(0x04, tmp[4*i+2] ^ tmp[4*i+0], 0x1b);
  57. v = v ^ gf256mul(0x04, tmp[4*i+3] ^ tmp[4*i+1], 0x1b);
  58. state->s[4*i+3] = tmp[4*i+3] ^ v ^ gf256mul(0x02, tmp[4*i+0] ^ tmp[4*i+3], 0x1b);
  59. state->s[4*i+2] = tmp[4*i+2] ^ w ^ gf256mul(0x02, t, 0x1b);
  60. state->s[4*i+1] = tmp[4*i+1] ^ v ^ gf256mul(0x02, tmp[4*i+2] ^ tmp[4*i+1], 0x1b);
  61. state->s[4*i+0] = tmp[4*i+0] ^ w ^ gf256mul(0x02, u, 0x1b);
  62. /*
  63. state->s[4*i+0] =
  64. gf256mul(0xe, tmp[4*i+0], 0x1b)
  65. ^ gf256mul(0xb, tmp[4*i+1], 0x1b)
  66. ^ gf256mul(0xd, tmp[4*i+2], 0x1b)
  67. ^ gf256mul(0x9, tmp[4*i+3], 0x1b);
  68. state->s[4*i+1] =
  69. gf256mul(0x9, tmp[4*i+0], 0x1b)
  70. ^ gf256mul(0xe, tmp[4*i+1], 0x1b)
  71. ^ gf256mul(0xb, tmp[4*i+2], 0x1b)
  72. ^ gf256mul(0xd, tmp[4*i+3], 0x1b);
  73. state->s[4*i+2] =
  74. gf256mul(0xd, tmp[4*i+0], 0x1b)
  75. ^ gf256mul(0x9, tmp[4*i+1], 0x1b)
  76. ^ gf256mul(0xe, tmp[4*i+2], 0x1b)
  77. ^ gf256mul(0xb, tmp[4*i+3], 0x1b);
  78. state->s[4*i+3] =
  79. gf256mul(0xb, tmp[4*i+0], 0x1b)
  80. ^ gf256mul(0xd, tmp[4*i+1], 0x1b)
  81. ^ gf256mul(0x9, tmp[4*i+2], 0x1b)
  82. ^ gf256mul(0xe, tmp[4*i+3], 0x1b);
  83. */
  84. }
  85. /* shiftRows */
  86. aes_invshiftcol(state->s+1, 1);
  87. aes_invshiftcol(state->s+2, 2);
  88. aes_invshiftcol(state->s+3, 3);
  89. /* subBytes */
  90. for(i=0; i<16; ++i){
  91. state->s[i] = aes_invsbox[state->s[i]];
  92. }
  93. }
  94. static
  95. void aes_dec_firstround(aes_cipher_state_t* state, const aes_roundkey_t* k){
  96. uint8_t i;
  97. /* keyAdd */
  98. for(i=0; i<16; ++i){
  99. state->s[i] ^= k->ks[i];
  100. }
  101. /* shiftRows */
  102. aes_invshiftcol(state->s+1, 1);
  103. aes_invshiftcol(state->s+2, 2);
  104. aes_invshiftcol(state->s+3, 3);
  105. /* subBytes */
  106. for(i=0; i<16; ++i){
  107. state->s[i] = aes_invsbox[state->s[i]];
  108. }
  109. }
  110. void aes_decrypt_core(aes_cipher_state_t* state, const aes_genctx_t* ks, uint8_t rounds){
  111. uint8_t i;
  112. aes_dec_firstround(state, &(ks->key[i=rounds]));
  113. for(;rounds>1;--rounds){
  114. --i;
  115. aes_dec_round(state, &(ks->key[i]));
  116. }
  117. for(i=0; i<16; ++i){
  118. state->s[i] ^= ks->key[0].ks[i];
  119. }
  120. }