aes_enc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* aes_enc.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. /**
  17. * \file aes_enc.c
  18. * \email daniel.otte@rub.de
  19. * \author Daniel Otte
  20. * \date 2008-12-30
  21. * \license GPLv3 or later
  22. *
  23. */
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <crypto/aes.h>
  27. #include <crypto/gf256mul.h>
  28. #include <crypto/aes_sbox.h>
  29. #include <crypto/aes_enc.h>
  30. void aes_shiftcol(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[(shift+0)&3];
  37. ((uint8_t*)data)[ 4] = tmp[(shift+1)&3];
  38. ((uint8_t*)data)[ 8] = tmp[(shift+2)&3];
  39. ((uint8_t*)data)[12] = tmp[(shift+3)&3];
  40. }
  41. #define GF256MUL_1(a) (a)
  42. #define GF256MUL_2(a) (gf256mul(2, (a), 0x1b))
  43. #define GF256MUL_3(a) (gf256mul(3, (a), 0x1b))
  44. static
  45. void aes_enc_round(aes_cipher_state_t* state, const aes_roundkey_t* k){
  46. uint8_t tmp[16], t;
  47. uint8_t i;
  48. /* subBytes */
  49. for(i=0; i<16; ++i){
  50. tmp[i] = aes_sbox[state->s[i]];
  51. }
  52. /* shiftRows */
  53. aes_shiftcol(tmp+1, 1);
  54. aes_shiftcol(tmp+2, 2);
  55. aes_shiftcol(tmp+3, 3);
  56. /* mixColums */
  57. for(i=0; i<4; ++i){
  58. t = tmp[4*i+0] ^ tmp[4*i+1] ^ tmp[4*i+2] ^ tmp[4*i+3];
  59. state->s[4*i+0] =
  60. GF256MUL_2(tmp[4*i+0]^tmp[4*i+1])
  61. ^ tmp[4*i+0]
  62. ^ t;
  63. state->s[4*i+1] =
  64. GF256MUL_2(tmp[4*i+1]^tmp[4*i+2])
  65. ^ tmp[4*i+1]
  66. ^ t;
  67. state->s[4*i+2] =
  68. GF256MUL_2(tmp[4*i+2]^tmp[4*i+3])
  69. ^ tmp[4*i+2]
  70. ^ t;
  71. state->s[4*i+3] =
  72. GF256MUL_2(tmp[4*i+3]^tmp[4*i+0])
  73. ^ tmp[4*i+3]
  74. ^ t;
  75. }
  76. /* addKey */
  77. for(i=0; i<16; ++i){
  78. state->s[i] ^= k->ks[i];
  79. }
  80. }
  81. static
  82. void aes_enc_lastround(aes_cipher_state_t* state,const aes_roundkey_t* k){
  83. uint8_t i;
  84. /* subBytes */
  85. for(i=0; i<16; ++i){
  86. state->s[i] = aes_sbox[state->s[i]];
  87. }
  88. /* shiftRows */
  89. aes_shiftcol(state->s+1, 1);
  90. aes_shiftcol(state->s+2, 2);
  91. aes_shiftcol(state->s+3, 3);
  92. /* keyAdd */
  93. for(i=0; i<16; ++i){
  94. state->s[i] ^= k->ks[i];
  95. }
  96. }
  97. void aes_encrypt_core(aes_cipher_state_t* state, const aes_genctx_t* ks, uint8_t rounds){
  98. uint8_t i;
  99. for(i=0; i<16; ++i){
  100. state->s[i] ^= ks->key[0].ks[i];
  101. }
  102. i=1;
  103. for(;rounds>1;--rounds){
  104. aes_enc_round(state, &(ks->key[i]));
  105. ++i;
  106. }
  107. aes_enc_lastround(state, &(ks->key[i]));
  108. }