aes_enc_round.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* aes-round.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 <crypto/aes_enc_round.h>
  18. #include <crypto/gf256mul.h>
  19. #include <crypto/aes_sbox.h>
  20. static
  21. void aes_shiftcol(void* data, uint8_t shift){
  22. uint8_t tmp[4];
  23. tmp[0] = ((uint8_t*)data)[ 0];
  24. tmp[1] = ((uint8_t*)data)[ 4];
  25. tmp[2] = ((uint8_t*)data)[ 8];
  26. tmp[3] = ((uint8_t*)data)[12];
  27. ((uint8_t*)data)[ 0] = tmp[(shift+0)&3];
  28. ((uint8_t*)data)[ 4] = tmp[(shift+1)&3];
  29. ((uint8_t*)data)[ 8] = tmp[(shift+2)&3];
  30. ((uint8_t*)data)[12] = tmp[(shift+3)&3];
  31. }
  32. #define GF256MUL_1(a) (a)
  33. #define GF256MUL_2(a) (gf256mul(2, (a), 0x1b))
  34. #define GF256MUL_3(a) (gf256mul(3, (a), 0x1b))
  35. void aes_enc_round(aes_cipher_state_t* state, const aes_roundkey_t* k){
  36. uint8_t tmp[16], t;
  37. uint8_t i;
  38. /* subBytes */
  39. for(i=0; i<16; ++i){
  40. tmp[i] = aes_sbox[state->s[i]];
  41. }
  42. /* shiftRows */
  43. aes_shiftcol(tmp+1, 1);
  44. aes_shiftcol(tmp+2, 2);
  45. aes_shiftcol(tmp+3, 3);
  46. /* mixColums */
  47. for(i=0; i<4; ++i){
  48. t = tmp[4*i+0] ^ tmp[4*i+1] ^ tmp[4*i+2] ^ tmp[4*i+3];
  49. state->s[4*i+0] =
  50. GF256MUL_2(tmp[4*i+0]^tmp[4*i+1])
  51. ^ tmp[4*i+0]
  52. ^ t;
  53. state->s[4*i+1] =
  54. GF256MUL_2(tmp[4*i+1]^tmp[4*i+2])
  55. ^ tmp[4*i+1]
  56. ^ t;
  57. state->s[4*i+2] =
  58. GF256MUL_2(tmp[4*i+2]^tmp[4*i+3])
  59. ^ tmp[4*i+2]
  60. ^ t;
  61. state->s[4*i+3] =
  62. GF256MUL_2(tmp[4*i+3]^tmp[4*i+0])
  63. ^ tmp[4*i+3]
  64. ^ t;
  65. }
  66. /* addKey */
  67. for(i=0; i<16; ++i){
  68. state->s[i] ^= k->ks[i];
  69. }
  70. }