keccak.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* keecak.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 <stdlib.h>
  18. #include <string.h>
  19. #include <crypto/memxor.h>
  20. #include <crypto/keccak.h>
  21. #ifdef DEBUG
  22. # undef DEBUG
  23. #endif
  24. #define DEBUG 0
  25. #if DEBUG
  26. #include <crypto/cli.h>
  27. void keccak_dump_state(uint64_t a[5][5]){
  28. uint8_t i,j;
  29. for(i=0; i<5; ++i){
  30. cli_putstr("\r\n");
  31. cli_putc('0'+i);
  32. cli_putstr(": ");
  33. for(j=0; j<5; ++j){
  34. cli_hexdump_rev(&(a[i][j]), 8);
  35. cli_putc(' ');
  36. }
  37. }
  38. }
  39. void keccak_dump_ctx(keccak_ctx_t* ctx){
  40. keccak_dump_state(ctx->a);
  41. cli_putstr("\r\nDBG: r: ");
  42. cli_hexdump_rev(&(ctx->r), 2);
  43. cli_putstr("\t c: ");
  44. cli_hexdump_rev(&(ctx->c), 2);
  45. cli_putstr("\t d: ");
  46. cli_hexdump(&(ctx->d), 1);
  47. cli_putstr("\t bs: ");
  48. cli_hexdump(&(ctx->bs), 1);
  49. }
  50. #endif
  51. static const uint64_t rc[] = {
  52. 0x0000000000000001LL, 0x0000000000008082LL,
  53. 0x800000000000808ALL, 0x8000000080008000LL,
  54. 0x000000000000808BLL, 0x0000000080000001LL,
  55. 0x8000000080008081LL, 0x8000000000008009LL,
  56. 0x000000000000008ALL, 0x0000000000000088LL,
  57. 0x0000000080008009LL, 0x000000008000000ALL,
  58. 0x000000008000808BLL, 0x800000000000008BLL,
  59. 0x8000000000008089LL, 0x8000000000008003LL,
  60. 0x8000000000008002LL, 0x8000000000000080LL,
  61. 0x000000000000800ALL, 0x800000008000000ALL,
  62. 0x8000000080008081LL, 0x8000000000008080LL,
  63. 0x0000000080000001LL, 0x8000000080008008LL
  64. };
  65. static inline
  66. uint64_t rotl64(uint64_t a, uint8_t r){
  67. return (a<<r)|(a>>(64-r));
  68. }
  69. static const uint8_t r[5][5] = {
  70. { 0, 36, 3, 41, 18 },
  71. { 1, 44, 10, 45, 2 },
  72. { 62, 6, 43, 15, 61 },
  73. { 28, 55, 25, 21, 56 },
  74. { 27, 20, 39, 8, 14 }
  75. };
  76. void keccak_round(uint64_t a[5][5], uint8_t rci){
  77. uint64_t b[5][5];
  78. uint8_t i,j;
  79. /* theta */
  80. for(i=0; i<5; ++i){
  81. b[i][0] = a[0][i] ^ a[1][i] ^ a[2][i] ^ a[3][i] ^ a[4][i];
  82. }
  83. for(i=0; i<5; ++i){
  84. b[i][1] = b[(4+i)%5][0] ^ rotl64(b[(i+1)%5][0], 1);
  85. for(j=0; j<5; ++j){
  86. a[j][i] ^= b[i][1];
  87. }
  88. }
  89. #if DEBUG
  90. cli_putstr("\r\nAfter theta:");
  91. keccak_dump_state(a);
  92. #endif
  93. /* rho & pi */
  94. for(i=0; i<5; ++i){
  95. for(j=0; j<5; ++j){
  96. b[(2*i+3*j)%5][j] = rotl64(a[j][i], r[i][j]);
  97. }
  98. }
  99. #if DEBUG
  100. cli_putstr("\r\n--- after rho & pi ---");
  101. keccak_dump_state(a);
  102. #endif
  103. /* chi */
  104. for(i=0; i<5; ++i){
  105. for(j=0; j<5; ++j){
  106. a[j][i] = b[j][i] ^ ((~(b[j][(i+1)%5]))&(b[j][(i+2)%5]));
  107. }
  108. }
  109. #if DEBUG
  110. cli_putstr("\r\nAfter chi:");
  111. keccak_dump_state(a);
  112. #endif
  113. /* iota */
  114. uint64_t t;
  115. t= rc[rci];
  116. a[0][0] ^= t;
  117. #if DEBUG
  118. cli_putstr("\r\nAfter iota:");
  119. keccak_dump_state(a);
  120. #endif
  121. }
  122. void keccak_f1600(uint64_t a[5][5]){
  123. uint8_t i=0;
  124. do{
  125. #if DEBUG
  126. cli_putstr("\r\n\r\n--- Round ");
  127. cli_hexdump(&i, 1);
  128. cli_putstr(" ---");
  129. #endif
  130. keccak_round(a, i);
  131. }while(++i<24);
  132. }
  133. void keccak_nextBlock(keccak_ctx_t* ctx, const void* block){
  134. memxor(ctx->a, block, ctx->bs);
  135. keccak_f1600(ctx->a);
  136. }
  137. void keccak_lastBlock(keccak_ctx_t* ctx, const void* block, uint16_t length_b){
  138. while(length_b>=ctx->r){
  139. keccak_nextBlock(ctx, block);
  140. block = (uint8_t*)block + ctx->bs;
  141. length_b -= ctx->r;
  142. }
  143. uint8_t tmp[ctx->bs];
  144. uint8_t pad[3];
  145. memset(tmp, 0x00, ctx->bs);
  146. memcpy(tmp, block, (length_b+7)/8);
  147. /* appand 1 */
  148. if(length_b&7){
  149. /* we have some single bits */
  150. uint8_t t;
  151. t = tmp[length_b/8]>>(8-(length_b&7));
  152. t |= 0x01<<(length_b&7);
  153. tmp[length_b/8] = t;
  154. }else{
  155. tmp[length_b/8] = 0x01;
  156. }
  157. pad[0] = ctx->d;
  158. pad[1] = ctx->bs;
  159. pad[2] = 0x01;
  160. if(length_b/8+1+3<=ctx->bs){
  161. memcpy(tmp+length_b/8+1, pad, 3);
  162. }else{
  163. if(length_b/8+1+2<=ctx->bs){
  164. memcpy(tmp+length_b/8+1, pad, 2);
  165. keccak_nextBlock(ctx, tmp);
  166. memset(tmp, 0x00, ctx->bs);
  167. tmp[0]=0x01;
  168. }else{
  169. if(length_b/8+1+1<=ctx->bs){
  170. memcpy(tmp+length_b/8+1, pad, 1);
  171. keccak_nextBlock(ctx, tmp);
  172. memset(tmp, 0x00, ctx->bs);
  173. tmp[0] = ctx->bs;
  174. tmp[1] = 0x01;
  175. }else{
  176. keccak_nextBlock(ctx, tmp);
  177. memset(tmp, 0x00, ctx->bs);
  178. tmp[0] = ctx->d;
  179. tmp[1] = ctx->bs;
  180. tmp[2] = 0x01;
  181. }
  182. }
  183. }
  184. keccak_nextBlock(ctx, tmp);
  185. }
  186. void keccak_ctx2hash(void* dest, uint16_t length_b, keccak_ctx_t* ctx){
  187. while(length_b>=ctx->r){
  188. memcpy(dest, ctx->a, ctx->bs);
  189. dest = (uint8_t*)dest + ctx->bs;
  190. length_b -= ctx->r;
  191. keccak_f1600(ctx->a);
  192. }
  193. memcpy(dest, ctx->a, (length_b+7)/8);
  194. }
  195. void keccak224_ctx2hash(void* dest, keccak_ctx_t* ctx){
  196. keccak_ctx2hash(dest, 224, ctx);
  197. }
  198. void keccak256_ctx2hash(void* dest, keccak_ctx_t* ctx){
  199. keccak_ctx2hash(dest, 256, ctx);
  200. }
  201. void keccak384_ctx2hash(void* dest, keccak_ctx_t* ctx){
  202. keccak_ctx2hash(dest, 384, ctx);
  203. }
  204. void keccak512_ctx2hash(void* dest, keccak_ctx_t* ctx){
  205. keccak_ctx2hash(dest, 512, ctx);
  206. }
  207. /*
  208. 1. SHA3-224: ⌊Keccak[r = 1152, c = 448, d = 28]⌋224
  209. 2. SHA3-256: ⌊Keccak[r = 1088, c = 512, d = 32]⌋256
  210. 3. SHA3-384: ⌊Keccak[r = 832, c = 768, d = 48]⌋384
  211. 4. SHA3-512: ⌊Keccak[r = 576, c = 1024, d = 64]⌋512
  212. */
  213. void keccak_init(uint16_t r, uint16_t c, uint8_t d, keccak_ctx_t* ctx){
  214. memset(ctx->a, 0x00, 5*5*8);
  215. ctx->r = r;
  216. ctx->c = c;
  217. ctx->d = d;
  218. ctx->bs = (uint8_t)(r/8);
  219. }
  220. void keccak224_init(keccak_ctx_t* ctx){
  221. keccak_init(1152, 448, 28, ctx);
  222. }
  223. void keccak256_init(keccak_ctx_t* ctx){
  224. keccak_init(1088, 512, 32, ctx);
  225. }
  226. void keccak384_init(keccak_ctx_t* ctx){
  227. keccak_init( 832, 768, 48, ctx);
  228. }
  229. void keccak512_init(keccak_ctx_t* ctx){
  230. keccak_init( 576, 1024, 64, ctx);
  231. }