mickey128.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* mickey128.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2006-2011 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/memxor.h>
  19. #include <crypto/mickey128.h>
  20. /*
  21. RTAPS = { 0,4,5, 8,10,11,14 ,16,20, 25,30, 32,35,36,38,
  22. 42,43,46, 50,51,53,54,55, 56,57,60,61,62,63,
  23. 65,66,69, 73,74,76,79, 80,81,82,85,86, 90,91,92,95,
  24. 97,100,101, 105,106,107,108,109,111, 112,113,115,116,117,
  25. 127, 128,129,130,131,133,135, 136,137,140,142, 145,148,150,
  26. 152,153,154,156,157 }
  27. 0011.0001 0100.1101 0001.0001 0100.0010 0101.1001
  28. 0100.1100 1110.1100 1111.0011
  29. 0010.0110 1001.0110 0110.0111 1001.1100
  30. 0011.0010 1011.1110 0011.1011
  31. 1000.0000 1010.1111 0101.0011 0101.0010
  32. 0011.0111
  33. 1000110010110010100010000100001010011010001100100011011111001111011001
  34. 0001101001111001100011100101001100011111011101110000000001111101011100
  35. 101001001010111011
  36. 1000.1100 1011.0010 1000.1000 0100.0010 1001.1010 0011.0010
  37. 0011.0111 1100.1111 0110.0100 0110.1001 1110.0110 0011.1001
  38. 0100.1100 0111.1101 1101.1100 0000.0001 1111.0101 1100.1010
  39. 0100.1010 1110.11
  40. */
  41. uint8_t rtaps[] = {
  42. 0x31, 0x4D, 0x11, 0x42,
  43. 0x59, 0x4C, 0xEC, 0xF3,
  44. 0x26, 0x96, 0x67, 0x9C,
  45. 0x32, 0xBE, 0x3B, 0x80,
  46. 0xAF, 0x53, 0x52, 0x37
  47. };
  48. #define SHL0(a) c1=((a)>>7); (a)=(((a)<<1)|c0)
  49. #define SHL1(a) c0=((a)>>7); (a)=(((a)<<1)|c1)
  50. #define SHLX0(a) c1=((a)>>7); (a)^=(((a)<<1)|c0)
  51. #define SHLX1(a) c0=((a)>>7); (a)^=(((a)<<1)|c1)
  52. static
  53. void clock_r(uint8_t* r, uint8_t ibit, uint8_t cbit){
  54. uint8_t i,c0=0,c1=0; /* carry */
  55. ibit ^= ((r[159/8])>>(159%8))&1; /* ibit is now the same as feedback_bit */
  56. if(cbit){
  57. for(i=0; i<10; ++i){
  58. SHLX0(r[2*i+0]);
  59. SHLX1(r[2*i+1]);
  60. }
  61. } else {
  62. for(i=0; i<10; ++i){
  63. SHL0(r[2*i+0]);
  64. SHL1(r[2*i+1]);
  65. }
  66. }
  67. if(ibit){
  68. memxor(r, rtaps, 20);
  69. }
  70. }
  71. /* comp0 (filling spaces with zero) (one at each side)
  72. * 0101.1110 1111.0010 1101.0110 0101.1101
  73. * 0101.0101 0000.1001 0010.0110 0111.1001
  74. * 0110.0010 0111.0000 0000.0000 0111.1001
  75. * 0011.0001 1101.1001 1010.1111 0011.0111
  76. * 1011.1110 0000.0110 1011.1110 0000.1111
  77. * --
  78. * 5E F2 D6 5D
  79. * 55 09 26 79
  80. * 62 70 00 79
  81. * 31 D9 AF 37
  82. * BE 06 BE 0F
  83. */
  84. uint8_t comp0[] = {
  85. 0x5E, 0xF2, 0xD6, 0x5D,
  86. 0x55, 0x09, 0x26, 0x79,
  87. 0x62, 0x70, 0x00, 0x79,
  88. 0x31, 0xD9, 0xAF, 0x37,
  89. 0xBE, 0x06, 0xBE, 0x0F
  90. };
  91. /* comp1 (shifting one bit right to make calculation easier, so inserting two zeros)
  92. * 0110.0000 0011.1110 0011.0010 1111.1010
  93. * 0011.0000 0111.1001 0110.1100 1111.1101
  94. * 1100.0001 1000.0111 0000.0001 1111.1000
  95. * 1000.1010 1100.0110 1100.0001 1100.1100
  96. * 0110.1010 1011.0111 1110.1000 1111.1111
  97. * --
  98. * 60 3E 32 FA
  99. * 30 79 6C FD
  100. * C1 87 01 F8
  101. * 8A C6 C1 CC
  102. * 6A B7 E8 FF
  103. */
  104. /*
  105. uint8_t comp1[] PROGMEM = {
  106. 0x60, 0x3E, 0x32, 0xFA, 0x30, 0x79, 0x6C, 0xFD, 0xC1, 0x87,
  107. 0x01, 0xF8, 0x8A, 0xC6, 0xC1, 0xCC, 0x6A, 0xB7, 0xE8, 0xFF
  108. };
  109. */
  110. /* comp1
  111. * 0000.1100 1111.1000 1001.1000 1011.1110
  112. * 0001.1001 0011.1100 0110.1101 0111.1111
  113. * 0000.0111 1100.0011 0000.0000 0011.1110
  114. * 1010.0010 1100.0111 0000.0110 0110.0110
  115. * 1010.1101 1101.1010 0010.1111 1111.1110
  116. * --
  117. * 0C F8 98 BE
  118. * 19 3C 6D 7F
  119. * 07 C3 00 3E
  120. * A2 C7 06 66
  121. * AD DA 2F FE
  122. */
  123. /*
  124. uint8_t comp1[] PROGMEM = {
  125. 0x0C, 0xF8, 0x98, 0xBE, 0x19, 0x3C, 0x6D, 0x7F, 0x07, 0xC3,
  126. 0x00, 0x3E, 0xA2, 0xC7, 0x06, 0x66, 0xAD, 0xDA, 0x2F, 0xFE
  127. };
  128. */
  129. /* comp1
  130. * 0011.0000 0001.1111 0001.1001 0111.1101
  131. * 1001.1000 0011.1100 1011.0110 1111.1110
  132. * 1110.0000 1100.0011 0000.0000 0111.1100
  133. * 0100.0101 1110.0011 0110.0000 0110.0110
  134. * 1011.0101 0101.1011 1111.0100 0111.1111
  135. * --
  136. * 30 1F 19 7D
  137. * 98 3C B6 FE
  138. * E0 C3 00 7C
  139. * 45 E3 60 66
  140. * B5 5B F4 7F
  141. */
  142. uint8_t comp1[] = {
  143. 0x30, 0x1F, 0x19, 0x7D,
  144. 0x98, 0x3C, 0xB6, 0xFE,
  145. 0xE0, 0xC3, 0x00, 0x7C,
  146. 0x45, 0xE3, 0x60, 0x66,
  147. 0xB5, 0x5B, 0xF4, 0x7F
  148. };
  149. /* fb0
  150. * 1010.1111 0001.1111 0011.1100 1100.0100
  151. * 0010.0010 1010.0011 0010.1111 0000.1110
  152. * 1000.0001 0100.1101 1110.0101 0110.0110
  153. * 1001.0001 0100.1011 0101.0100 1101.0100
  154. * 1100.0001 0000.1011 0110.0011 1000.0011
  155. * --
  156. * AF 1F 3C C4
  157. * 22 A3 2F 0E
  158. * 81 4D E5 66
  159. * 91 4B 54 D4
  160. * C1 0B 63 83
  161. */
  162. uint8_t fb0[] = {
  163. 0xAF, 0x1F, 0x3C, 0xC4,
  164. 0x22, 0xA3, 0x2F, 0x0E,
  165. 0x81, 0x4D, 0xE5, 0x66,
  166. 0x91, 0x4B, 0x54, 0xD4,
  167. 0xC1, 0x0B, 0x63, 0x83
  168. };
  169. /* fb1
  170. * 1010.1011 0111.0111 1111.0100 1001.1011
  171. * 1001.0000 1000.1100 0111.1001 0111.0000
  172. * 1011.0110 0001.1000 1001.1010 0110.1111
  173. * 1110.0111 0111.1110 0100.1011 0110.1100
  174. * 1110.1111 1000.0000 1010.0111 0001.0001
  175. * --
  176. * AB 77 F4 9B
  177. * 90 8C 79 70
  178. * B6 18 9A 6F
  179. * E7 7E 4B 6C
  180. * EF 80 A7 11
  181. */
  182. uint8_t fb1[] = {
  183. 0xAB, 0x77, 0xF4, 0x9B,
  184. 0x90, 0x8C, 0x79, 0x70,
  185. 0xB6, 0x18, 0x9A, 0x6F,
  186. 0xE7, 0x7E, 0x4B, 0x6C,
  187. 0xEF, 0x80, 0xA7, 0x11
  188. };
  189. static
  190. void clock_s(uint8_t* s, uint8_t ibit, uint8_t cbit){
  191. uint8_t s0[20], s1[20];
  192. uint8_t i,c=0, c2=0;
  193. ibit ^= (s[19])>>7;
  194. memcpy(s0,s,20);
  195. memxor(s0, comp0, 20);
  196. for(i=0; i<20; ++i){
  197. s1[19-i]= c|((s[19-i])>>1);
  198. c = (s[19-i])<<7;
  199. }
  200. memxor(s1, comp1, 20);
  201. c=0;
  202. for(i=0; i<20; ++i){
  203. c2=(s[i])>>7;
  204. s[i]=((s[i])<<1) ^ ((s0[i])&(s1[i])) ^ c;
  205. c=c2;
  206. }
  207. s[0] &= 0xFE;
  208. if(ibit){
  209. memxor(s, cbit?fb1:fb0, 20);
  210. }
  211. }
  212. static
  213. void clock_kg(uint8_t* r, uint8_t* s, uint8_t mixing, uint8_t input){
  214. uint8_t rb, sb;
  215. rb = ((s[ 54/8])>>(( 54%8))) ^ ((r[106/8])>>(((106%8))));
  216. sb = ((s[106/8])>>((106%8))) ^ ((r[ 53/8])>>((( 53%8))));
  217. rb &= 1;
  218. sb &= 1;
  219. mixing = input ^ (mixing & ((s[80/8]>>((80%8))) & 1));
  220. clock_r(r, mixing, rb);
  221. clock_s(s, input, sb);
  222. }
  223. void mickey128_init(void* key, uint16_t keysize_b,
  224. void* iv, uint16_t ivsize_b,
  225. mickey128_ctx_t* ctx){
  226. uint16_t i;
  227. memset(ctx->r, 0, 20);
  228. memset(ctx->s, 0, 20);
  229. for(i=0; i<ivsize_b; ++i){
  230. clock_kg(ctx->r, ctx->s, 1, 1&((((uint8_t*)iv)[i/8])>>(7-(i%8))));
  231. }
  232. for(i=0; i<keysize_b; ++i){
  233. clock_kg(ctx->r, ctx->s, 1, 1&((((uint8_t*)key)[i/8])>>(7-(i%8))));
  234. }
  235. for(i=0; i<160; ++i){
  236. clock_kg(ctx->r, ctx->s, 1, 0);
  237. }
  238. }
  239. uint8_t mickey128_getbit(mickey128_ctx_t* ctx){
  240. uint8_t ret;
  241. ret = 1&(*(ctx->r) ^ *(ctx->s));
  242. clock_kg(ctx->r, ctx->s, 0, 0);
  243. return ret;
  244. }
  245. uint8_t mickey128_getbyte(mickey128_ctx_t* ctx){
  246. uint8_t i,ret=0;
  247. for(i=0; i<8; ++i){
  248. ret<<=1;
  249. ret |= 1&(((ctx->r)[0]) ^ ((ctx->s)[0]));
  250. clock_kg(ctx->r, ctx->s, 0, 0);
  251. }
  252. return ret;
  253. }