seed_c.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* seed_c.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 seed_c.c
  18. * \author Daniel Otte
  19. * \date 2007-06-1
  20. * \brief SEED parts in C for ARM
  21. * \par License
  22. * GPL
  23. *
  24. */
  25. #include <stdint.h>
  26. #include <string.h>
  27. #include <crypto/seed.h>
  28. #include <crypto/seed_sbox.h>
  29. static
  30. uint32_t g_function(uint32_t x);
  31. /******************************************************************************/
  32. static
  33. void changeendian32(uint32_t * a){
  34. *a = (*a & 0x000000FF) << 24 |
  35. (*a & 0x0000FF00) << 8 |
  36. (*a & 0x00FF0000) >> 8 |
  37. (*a & 0xFF000000) >> 24;
  38. }
  39. /******************************************************************************/
  40. static
  41. uint32_t bigendian_sum32(uint32_t a, uint32_t b){
  42. changeendian32(&a);
  43. changeendian32(&b);
  44. a += b;
  45. changeendian32(&a);
  46. return a;
  47. }
  48. /******************************************************************************/
  49. static
  50. uint32_t bigendian_sub32(uint32_t a, uint32_t b){
  51. changeendian32(&a);
  52. changeendian32(&b);
  53. a -= b;
  54. changeendian32(&a);
  55. return a;
  56. }
  57. /******************************************************************************/
  58. static inline
  59. uint64_t bigendian_rotl8_64(uint64_t a){
  60. /*
  61. changeendian64(&a);
  62. a = (a<<8) | (a>>(64-8));
  63. changeendian64(&a);
  64. */
  65. a = (a>>8) | (a<<(64-8));
  66. return a;
  67. }
  68. /******************************************************************************/
  69. static inline
  70. uint64_t bigendian_rotr8_64(uint64_t a){
  71. /*
  72. changeendian64(&a);
  73. a = (a>>8) | (a<<(64-8));
  74. changeendian64(&a);
  75. */
  76. a = (a<<8) | (a>>(64-8));
  77. return a;
  78. }
  79. /******************************************************************************/
  80. static
  81. uint64_t f_function(const uint64_t* a, uint32_t k0, uint32_t k1){
  82. uint32_t c,d;
  83. c = *a & 0x00000000FFFFFFFFLL;
  84. d = (*a>>32) & 0x00000000FFFFFFFFLL;
  85. c ^= k0; d ^= k1;
  86. d ^= c;
  87. d = g_function(d);
  88. c = bigendian_sum32(c,d);
  89. c = g_function(c);
  90. d = bigendian_sum32(c,d);
  91. d = g_function(d);
  92. c = bigendian_sum32(c,d);
  93. return ((uint64_t)d << 32) | c;
  94. }
  95. /******************************************************************************/
  96. #define M0 0xfc
  97. #define M1 0xf3
  98. #define M2 0xcf
  99. #define M3 0x3f
  100. #define X3 (((uint8_t*)(&x))[0])
  101. #define X2 (((uint8_t*)(&x))[1])
  102. #define X1 (((uint8_t*)(&x))[2])
  103. #define X0 (((uint8_t*)(&x))[3])
  104. #define Z3 (((uint8_t*)(&z))[0])
  105. #define Z2 (((uint8_t*)(&z))[1])
  106. #define Z1 (((uint8_t*)(&z))[2])
  107. #define Z0 (((uint8_t*)(&z))[3])
  108. static
  109. uint32_t g_function(uint32_t x){
  110. uint32_t z;
  111. /* sbox substitution */
  112. X3 = seed_sbox2[X3];
  113. X2 = seed_sbox1[X2];
  114. X1 = seed_sbox2[X1];
  115. X0 = seed_sbox1[X0];
  116. /* now the permutation */
  117. Z0 = (X0 & M0) ^ (X1 & M1) ^ (X2 & M2) ^ (X3 & M3);
  118. Z1 = (X0 & M1) ^ (X1 & M2) ^ (X2 & M3) ^ (X3 & M0);
  119. Z2 = (X0 & M2) ^ (X1 & M3) ^ (X2 & M0) ^ (X3 & M1);
  120. Z3 = (X0 & M3) ^ (X1 & M0) ^ (X2 & M1) ^ (X3 & M2);
  121. return z;
  122. }
  123. /******************************************************************************/
  124. typedef struct {
  125. uint32_t k0, k1;
  126. } keypair_t;
  127. keypair_t getnextkeys(uint32_t *keystate, uint8_t curround){
  128. keypair_t ret;
  129. if (curround>15){
  130. /* ERROR */
  131. ret.k0 = ret.k1 = 0;
  132. } else {
  133. /* ret.k0 = g_function(keystate[0] + keystate[2] - pgm_read_dword(&(seed_kc[curround])));
  134. ret.k1 = g_function(keystate[1] - keystate[3] + pgm_read_dword(&(seed_kc[curround]))); */
  135. ret.k0 = bigendian_sum32(keystate[0], keystate[2]);
  136. ret.k0 = bigendian_sub32(ret.k0, seed_kc[curround]);
  137. ret.k0 = g_function(ret.k0);
  138. ret.k1 = bigendian_sub32(keystate[1], keystate[3]);
  139. ret.k1 = bigendian_sum32(ret.k1, seed_kc[curround]);
  140. ret.k1 = g_function(ret.k1);
  141. if (curround & 1){
  142. /* odd round (1,3,5, ...) */
  143. ((uint64_t*)keystate)[1] = bigendian_rotl8_64( ((uint64_t*)keystate)[1] );
  144. } else {
  145. /* even round (0,2,4, ...) */
  146. ((uint64_t*)keystate)[0] = bigendian_rotr8_64(((uint64_t*)keystate)[0]);
  147. }
  148. }
  149. return ret;
  150. }
  151. /******************************************************************************/
  152. keypair_t getprevkeys(uint32_t *keystate, uint8_t curround){
  153. keypair_t ret;
  154. if (curround>15){
  155. /* ERROR */
  156. ret.k0 = ret.k1 = 0;
  157. } else {
  158. if (curround & 1){
  159. /* odd round (1,3,5, ..., 15) */
  160. ((uint64_t*)keystate)[1] = bigendian_rotr8_64( ((uint64_t*)keystate)[1] );
  161. } else {
  162. /* even round (0,2,4, ..., 14) */
  163. ((uint64_t*)keystate)[0] = bigendian_rotl8_64(((uint64_t*)keystate)[0]);
  164. }
  165. /* ret.k0 = g_function(keystate[0] + keystate[2] - pgm_read_dword(&(seed_kc[curround])));
  166. ret.k1 = g_function(keystate[1] - keystate[3] + pgm_read_dword(&(seed_kc[curround]))); */
  167. ret.k0 = bigendian_sum32(keystate[0], keystate[2]);
  168. ret.k0 = bigendian_sub32(ret.k0, seed_kc[curround]);
  169. ret.k0 = g_function(ret.k0);
  170. ret.k1 = bigendian_sub32(keystate[1], keystate[3]);
  171. ret.k1 = bigendian_sum32(ret.k1, seed_kc[curround]);
  172. ret.k1 = g_function(ret.k1);
  173. }
  174. return ret;
  175. }
  176. /******************************************************************************/
  177. void seed_init(const void * key, seed_ctx_t * ctx){
  178. memcpy(ctx->k, key, 128/8);
  179. }
  180. /******************************************************************************/
  181. #define L (((uint64_t*)buffer)[0])
  182. #define R (((uint64_t*)buffer)[1])
  183. void seed_enc(void * buffer, const seed_ctx_t * ctx){
  184. uint8_t r;
  185. keypair_t k;
  186. for(r=0; r<8; ++r){
  187. k = getnextkeys(((seed_ctx_t*)ctx)->k, 2*r);
  188. /*
  189. DEBUG_S("\r\n\tDBG ka,0: "); cli_hexdump(&k.k0, 4);
  190. DEBUG_S("\r\n\tDBG ka,1: "); cli_hexdump(&k.k1, 4);
  191. DEBUG_S("\r\n\t DBG L: "); cli_hexdump((uint8_t*)buffer+0, 8);
  192. DEBUG_S("\r\n\t DBG R: "); cli_hexdump((uint8_t*)buffer+8, 8);
  193. */
  194. L ^= f_function(&R,k.k0,k.k1);
  195. k = getnextkeys(((seed_ctx_t*)ctx)->k, 2*r+1);
  196. /*
  197. DEBUG_S("\r\n\tDBG kb,0: "); cli_hexdump(&k.k0, 4);
  198. DEBUG_S("\r\n\tDBG kb,1: "); cli_hexdump(&k.k1, 4);
  199. DEBUG_S("\r\n\t DBG L: "); cli_hexdump((uint8_t*)buffer+8, 8);
  200. DEBUG_S("\r\n\t DBG R: "); cli_hexdump((uint8_t*)buffer+0, 8);
  201. */
  202. R ^= f_function(&L,k.k0,k.k1);
  203. }
  204. /* just an exchange without temp. variable */
  205. L ^= R;
  206. R ^= L;
  207. L ^= R;
  208. }
  209. /******************************************************************************/
  210. #define L (((uint64_t*)buffer)[0])
  211. #define R (((uint64_t*)buffer)[1])
  212. void seed_dec(void * buffer, const seed_ctx_t * ctx){
  213. int8_t r;
  214. keypair_t k;
  215. for(r=7; r>=0; --r){
  216. k = getprevkeys(((seed_ctx_t*)ctx)->k, 2*r+1);
  217. /*
  218. DEBUG_S("\r\n\tDBG ka,0: "); cli_hexdump(&k.k0, 4);
  219. DEBUG_S("\r\n\tDBG ka,1: "); cli_hexdump(&k.k1, 4);
  220. DEBUG_S("\r\n\t DBG L: "); cli_hexdump((uint8_t*)buffer+0, 8);
  221. DEBUG_S("\r\n\t DBG R: "); cli_hexdump((uint8_t*)buffer+8, 8);
  222. */
  223. L ^= f_function(&R,k.k0,k.k1);
  224. k = getprevkeys(((seed_ctx_t*)ctx)->k, 2*r+0);
  225. /*
  226. DEBUG_S("\r\n\tDBG kb,0: "); cli_hexdump(&k.k0, 4);
  227. DEBUG_S("\r\n\tDBG kb,1: "); cli_hexdump(&k.k1, 4);
  228. DEBUG_S("\r\n\t DBG L: "); cli_hexdump((uint8_t*)buffer+8, 8);
  229. DEBUG_S("\r\n\t DBG R: "); cli_hexdump((uint8_t*)buffer+0, 8);
  230. */
  231. R ^= f_function(&L,k.k0,k.k1);
  232. }
  233. /* just an exchange without temp. variable */
  234. L ^= R;
  235. R ^= L;
  236. L ^= R;
  237. }