grain.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* grain.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 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. /**
  17. *
  18. * author: Daniel Otte
  19. * email: daniel.otte@rub.de
  20. * license: GPLv3 or later
  21. *
  22. */
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include <crypto/grain.h>
  26. #define GRAIN_REVERSEKEY
  27. /* s0, s1, s2, ..., s78, s79 */
  28. #define S(i) ((ctx->lfsr[9-((i)/8)])>>(7-((i)%8)))
  29. /* b0, b1, b2, ..., b78, b79 */
  30. #define B(i) ((ctx->nfsr[9-((i)/8)])>>(7-((i)%8)))
  31. #define _B(i) (((ctx->nfsr[9-((i)/8)])>>(7-((i)%8)))&1)
  32. uint8_t h_lut[4] = {0x4C, 0xB6, 0xD3, 0x26};
  33. #ifdef GRAIN_BADOPTIMISATION
  34. uint8_t g_lut[128] = {
  35. 0xF0, 0xA5, 0x0F, 0x5A, 0x0F, 0x5A, 0xF0, 0xA5, 0x0F, 0x5A, 0xF0, 0xA5, 0xF0, 0x5A, 0x0F, 0x0F,
  36. 0xC3, 0x96, 0x3C, 0x69, 0x3C, 0x69, 0xC3, 0x96, 0x9C, 0xC9, 0x63, 0x36, 0x63, 0xC9, 0x9C, 0x9C,
  37. 0x0F, 0x5A, 0x0F, 0x5A, 0xF0, 0xA5, 0xF0, 0x5A, 0xF0, 0xA5, 0xF0, 0xA5, 0x0F, 0xA5, 0x0F, 0xF0,
  38. 0x3C, 0x69, 0x3C, 0x69, 0xC3, 0x96, 0xC3, 0x69, 0x63, 0x36, 0x63, 0x36, 0x9C, 0x36, 0x9C, 0x63,
  39. 0x0F, 0xD2, 0xF0, 0x2D, 0xF0, 0x2D, 0x0F, 0xD2, 0xF0, 0x2D, 0x0F, 0xD2, 0x0F, 0x2D, 0xF0, 0x78,
  40. 0x3C, 0xE1, 0xC3, 0x1E, 0xC3, 0x1E, 0x3C, 0xE1, 0x63, 0xBE, 0x9C, 0x41, 0x9C, 0xBE, 0x63, 0xEB,
  41. 0x00, 0xDD, 0x00, 0xDD, 0xFF, 0x22, 0xFF, 0xDD, 0xFF, 0x22, 0xFF, 0x22, 0x00, 0x22, 0xF0, 0x87,
  42. 0xF3, 0x2E, 0xF3, 0x2E, 0x0C, 0xD1, 0x0C, 0x2E, 0xAC, 0x71, 0xAC, 0x71, 0x53, 0x71, 0xA3, 0xD4 };
  43. #endif
  44. uint8_t grain_enc(grain_ctx_t* ctx){
  45. uint8_t s80, s0, c1, c2;
  46. uint8_t i;
  47. /* clock the LFSR */
  48. s0=S(0);
  49. s80 =S(62) ^ S(51) ^ S(38) ^ S(23) ^ S(13) ^ s0;
  50. s80 &= 1;
  51. c1 = s80;
  52. for(i=0; i<10; ++i){
  53. c2 = (ctx->lfsr[i])>>7;
  54. ctx->lfsr[i] = ((ctx->lfsr[i])<<1) | c1;
  55. c1 = c2;
  56. }
  57. /* clock the NFSR */
  58. uint8_t b80;
  59. /* 778 Byte in this variant / 617 clks enc_time */
  60. #ifndef GRAIN_BADOPTIMISATION
  61. uint8_t a,b,d,e;
  62. b80 = B(62) ^ B(60) ^ B(52) ^ B(45) ^
  63. B(37) ^ B(33) ^ B(28) ^ B(21) ^
  64. B(14) ^ B( 9) ^ B( 0) ^ s0;
  65. b80 ^= (a = B(63) & B(60));
  66. b80 ^= (b = B(37) & B(33));
  67. b80 ^= B(15) & B( 9); // c
  68. b80 ^= (d = B(60) & B(52) & B(45));
  69. b80 ^= (e = B(33) & B(28) & B(21));
  70. b80 ^= B(63) & B(45) & B(28) & B(9); // f
  71. /* -- */
  72. b80 ^= b & B(60) & B(52); // g
  73. b80 ^= a & B(21) & B(15); // h
  74. b80 ^= d & B(63) & B(37); // i
  75. b80 ^= e & B(15) & B( 9); // j
  76. b80 ^= e & B(52) & B(45) & B(37); // k
  77. #else
  78. /* let's reorder the bits */
  79. uint16_t x;
  80. /*
  81. x = _B(21); x<<=1;
  82. x |= _B(33); x<<=1;
  83. x |= _B(9) ; x<<=1;
  84. x |= _B(45); x<<=1;
  85. x |= _B(52); x<<=1;
  86. x |= _B(37); x<<=1;
  87. x |= _B(60); x<<=1;
  88. x |= _B(28); x<<=1;
  89. x |= _B(15); x<<=1;
  90. x |= _B(63);
  91. */
  92. x = ((ctx->nfsr[8])&0x41)<<1; // B15 & B09
  93. x |= ((ctx->nfsr[2])&0x09); // B63 & B60
  94. // x |= ((ctx->nfsr[4])&0x04)<<4; // B45
  95. x |= (((ctx->nfsr[5])&0x44) |
  96. ((ctx->nfsr[3])&0x08) |
  97. (((((ctx->nfsr[7])&0x04)<<3) |((ctx->nfsr[4])&0x04))<<2) )<<2; // B37 & B33
  98. // x |= ((ctx->nfsr[3])&0x08)<<2; // B52
  99. x |= ((ctx->nfsr[6])&0x08)>>1; // B28
  100. // x |= ((ctx->nfsr[7])&0x04)<<7; // B21
  101. b80 = (g_lut[x/8])>>(x%8);
  102. b80 ^= s0 ^ B(62) ^ B(14) ^ B(0);
  103. #endif
  104. c1 = b80 & 1;
  105. for(i=0; i<10; ++i){
  106. c2 = (ctx->nfsr[i])>>7;
  107. ctx->nfsr[i] = ((ctx->nfsr[i])<<1) | c1;
  108. c1 = c2;
  109. }
  110. /* now the h function */
  111. uint8_t h;
  112. i = (S(2)&1) |
  113. ((S(24)&1) << 1) |
  114. ((S(45)&1) << 2) |
  115. ((S(63)&1) << 3) |
  116. ((B(62)&1) << 4);
  117. h = (h_lut[i/8])>>(i%8);
  118. h ^= B(0) ^ B(1) ^ B(3) ^ B(9) ^ B(30) ^ B(42) ^ B(55);
  119. return (h&1);
  120. }
  121. uint8_t grain_getbyte(grain_ctx_t* ctx){
  122. uint8_t i=0;
  123. uint8_t r=0;
  124. do{
  125. r >>= 1;
  126. r |= grain_enc(ctx)?0x80:0x00;
  127. }while(++i<8);
  128. return r;
  129. }
  130. #ifdef GRAIN_REVERSEKEY
  131. static
  132. uint8_t reverse_bits(uint8_t a){
  133. uint8_t lut[16] = {
  134. 0x0, 0x8, 0x4, 0xC, /* 0000 1000 0100 1100 */
  135. 0x2, 0xA, 0x6, 0xE, /* 0010 1010 0110 1110 */
  136. 0x1, 0x9, 0x5, 0xD, /* 0001 1001 0101 1101 */
  137. 0x3, 0xB, 0x7, 0xF }; /* 0011 1011 0111 1111 */
  138. uint8_t x;
  139. x = ((lut[a&0xf]) << 4) | lut[a>>4];
  140. return x;
  141. }
  142. #else
  143. #define reverse_bits(a) (a)
  144. #endif
  145. void grain_init(const void* key, const void* iv, grain_ctx_t* ctx){
  146. uint8_t i,t;
  147. /* load the 80bit key */
  148. for(i=0; i<10; ++i){
  149. ctx->nfsr[9-i] = reverse_bits(((uint8_t*)key)[i]);
  150. }
  151. /* load the 64bit iv */
  152. for(i=0; i<8; ++i){
  153. ctx->lfsr[9-i] = reverse_bits(((uint8_t*)iv)[i]);
  154. }
  155. /* set the other bits of iv to 1 */
  156. ctx->lfsr[0] = ctx->lfsr[1] = 0xFF;
  157. /* run it 160 times */
  158. for(i=0; i<160; ++i){
  159. t = grain_enc(ctx);
  160. (ctx->lfsr[0]) ^= t;
  161. (ctx->nfsr[0]) ^= t;
  162. }
  163. }