aes_keyschedule.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* aes_keyschedule.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_keyschedule.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/memxor.h>
  27. #include <crypto/aes.h>
  28. #include <crypto/aes_keyschedule.h>
  29. #include <crypto/aes_sbox.h>
  30. /*
  31. static
  32. void aes_rotword(void* a){
  33. uint8_t t;
  34. t=((uint8_t*)a)[0];
  35. ((uint8_t*)a)[0] = ((uint8_t*)a)[1];
  36. ((uint8_t*)a)[1] = ((uint8_t*)a)[2];
  37. ((uint8_t*)a)[2] = ((uint8_t*)a)[3];
  38. ((uint8_t*)a)[3] = t;
  39. }
  40. */
  41. const uint8_t rc_tab[] = {
  42. 0x01, 0x02, 0x04, 0x08,
  43. 0x10, 0x20, 0x40, 0x80,
  44. 0x1b, 0x36 };
  45. void aes_init(const void* key, uint16_t keysize_b, aes_genctx_t* ctx){
  46. uint8_t hi,i,nk, next_nk;
  47. uint8_t rc=0;
  48. union __attribute__((packed)) {
  49. uint32_t v32;
  50. uint8_t v8[4];
  51. } tmp;
  52. nk = keysize_b >> 5; /* 4, 6, 8 */
  53. hi = 4 * (nk + 6 + 1);
  54. memcpy(ctx, key, keysize_b/8);
  55. next_nk = nk;
  56. for(i=nk; i<hi; ++i){
  57. /* tmp.v32 = ((uint32_t*)(ctx->key[0].ks))[i-1]; */
  58. memcpy(tmp.v8, ctx->key[0].ks + (i - 1) * 4, 4);
  59. if(i != next_nk){
  60. if(nk == 8 && i % 8 == 4){
  61. tmp.v8[0] = aes_sbox[tmp.v8[0]];
  62. tmp.v8[1] = aes_sbox[tmp.v8[1]];
  63. tmp.v8[2] = aes_sbox[tmp.v8[2]];
  64. tmp.v8[3] = aes_sbox[tmp.v8[3]];
  65. }
  66. } else {
  67. next_nk += nk;
  68. /* aes_rotword(&(tmp.v32)); */
  69. tmp.v32 = (tmp.v32 >> 8) | (tmp.v32 << 24);
  70. tmp.v8[0] = aes_sbox[tmp.v8[0]];
  71. tmp.v8[1] = aes_sbox[tmp.v8[1]];
  72. tmp.v8[2] = aes_sbox[tmp.v8[2]];
  73. tmp.v8[3] = aes_sbox[tmp.v8[3]];
  74. tmp.v8[0] ^= rc_tab[rc];
  75. rc++;
  76. }
  77. memcpy(ctx->key[0].ks + 4 * i, ctx->key[0].ks + (i - nk) * 4, 4);
  78. memxor(ctx->key[0].ks + 4 * i, tmp.v8, 4);
  79. /*
  80. ((uint32_t*)(ctx->key[0].ks))[i] = ((uint32_t*)(ctx->key[0].ks))[i-nk]
  81. ^ tmp.v32;
  82. */
  83. }
  84. }
  85. void aes128_init(const void* key, aes128_ctx_t* ctx){
  86. aes_init(key, 128, (aes_genctx_t*)ctx);
  87. }
  88. void aes192_init(const void* key, aes192_ctx_t* ctx){
  89. aes_init(key, 192, (aes_genctx_t*)ctx);
  90. }
  91. void aes256_init(const void* key, aes256_ctx_t* ctx){
  92. aes_init(key, 256, (aes_genctx_t*)ctx);
  93. }