blake_large.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* blake_large.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 blake_large.c
  18. * \author Daniel Otte
  19. * \email daniel.otte@rub.de
  20. * \date 2009-05-08
  21. * \license GPLv3 or later
  22. *
  23. */
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <crypto/memxor.h>
  27. #include <crypto/blake_large.h>
  28. #include <crypto/blake_common.h>
  29. static const
  30. uint64_t blake_c[] = {
  31. 0x243F6A8885A308D3LL, 0x13198A2E03707344LL,
  32. 0xA4093822299F31D0LL, 0x082EFA98EC4E6C89LL,
  33. 0x452821E638D01377LL, 0xBE5466CF34E90C6CLL,
  34. 0xC0AC29B7C97C50DDLL, 0x3F84D5B5B5470917LL,
  35. 0x9216D5D98979FB1BLL, 0xD1310BA698DFB5ACLL,
  36. 0x2FFD72DBD01ADFB7LL, 0xB8E1AFED6A267E96LL,
  37. 0xBA7C9045F12C7F99LL, 0x24A19947B3916CF7LL,
  38. 0x0801F2E2858EFC16LL, 0x636920D871574E69LL
  39. };
  40. #define ROTL64(a, n) (((a)<<(n))|((a)>>(64-(n))))
  41. #define ROTR64(a, n) (((a)>>(n))|((a)<<(64-(n))))
  42. #define CHANGE_ENDIAN32(a) (((a)<<24)| \
  43. ((0x0000ff00&(a))<<8)| \
  44. ((0x00ff0000&(a))>>8)| \
  45. (a)>>24 )
  46. static
  47. void blake_large_expand(uint64_t* v, const blake_large_ctx_t* ctx){
  48. uint8_t i;
  49. memcpy(v, ctx->h, 8*8);
  50. for(i=0; i<8; ++i){
  51. v[8+i] = blake_c[i];
  52. }
  53. memxor((uint8_t*)v+8, ctx->s, 4*8);
  54. }
  55. static
  56. void blake_large_changeendian(void* dest, const void* src){
  57. uint8_t i;
  58. uint32_t tmp;
  59. for(i=0; i<32; i+=2){
  60. tmp = CHANGE_ENDIAN32(((uint32_t*)src)[i]);
  61. ((uint32_t*)dest)[i] = CHANGE_ENDIAN32(((uint32_t*)src)[i+1]);
  62. ((uint32_t*)dest)[i+1] = tmp;
  63. }
  64. }
  65. #define A (v[idx.v8[0]])
  66. #define B (v[idx.v8[1]])
  67. #define C (v[idx.v8[2]])
  68. #define D (v[idx.v8[3]])
  69. static
  70. void blake_large_compress(uint64_t* v,const void* m){
  71. uint8_t r,i;
  72. uint16_t s, *p=(uint16_t*)blake_sigma;
  73. union {
  74. uint32_t v32;
  75. uint8_t v8[4];
  76. }idx;
  77. for(r=0; r<16; ++r){
  78. for(i=0; i<8; ++i){
  79. idx.v32 = ((uint32_t*)blake_index_lut)[i];
  80. s = *p++;
  81. if(p==(uint16_t*)(blake_sigma + 160)){
  82. p=(uint16_t*)blake_sigma;
  83. }
  84. A += B + (((uint64_t*)m)[s&0xff] ^ blake_c[s>>8]);
  85. D = ROTR64(D^A, 32);
  86. C += D;
  87. B = ROTR64(B^C, 25);
  88. A += B + (((uint64_t*)m)[s>>8] ^ blake_c[s&0xff]);
  89. D = ROTR64(D^A, 16);
  90. C += D;
  91. B = ROTR64(B^C, 11);
  92. }
  93. }
  94. }
  95. static
  96. void blake_large_collapse(blake_large_ctx_t* ctx, uint64_t* v){
  97. uint8_t i;
  98. for(i=0; i<8; ++i){
  99. ctx->h[i] ^= ctx->s[i%4] ^ v[i] ^ v[8+i];
  100. }
  101. }
  102. void blake_large_nextBlock(blake_large_ctx_t* ctx, const void* msg){
  103. uint64_t v[16];
  104. uint64_t m[16];
  105. union {
  106. uint64_t v64;
  107. uint32_t v32[2];
  108. }ctr;
  109. blake_large_expand(v,ctx);
  110. ctx->counter++;
  111. ctr.v64 = ctx->counter*1024;
  112. v[12] ^= ctr.v64;
  113. v[13] ^= ctr.v64;
  114. blake_large_changeendian(m, msg);
  115. blake_large_compress(v, m);
  116. blake_large_collapse(ctx, v);
  117. }
  118. void blake_large_lastBlock(blake_large_ctx_t* ctx, const void* msg, uint16_t length_b){
  119. while(length_b>=BLAKE_LARGE_BLOCKSIZE){
  120. blake_large_nextBlock(ctx, msg);
  121. msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
  122. length_b -= BLAKE_LARGE_BLOCKSIZE;
  123. }
  124. union {
  125. uint8_t v8[128];
  126. uint64_t v64[ 16];
  127. } buffer;
  128. uint64_t v[16];
  129. uint64_t ctr;
  130. ctr = ctx->counter*1024+length_b;
  131. memset(buffer.v8, 0, 128);
  132. memcpy(buffer.v8, msg, (length_b+7)/8);
  133. buffer.v8[length_b/8] |= 0x80 >> (length_b&0x7);
  134. blake_large_changeendian(buffer.v8, buffer.v8);
  135. blake_large_expand(v, ctx);
  136. if(length_b>1024-128-2){
  137. v[12] ^= ctr;
  138. v[13] ^= ctr;
  139. blake_large_compress(v, buffer.v8);
  140. blake_large_collapse(ctx, v);
  141. memset(buffer.v8, 0, 128-8);
  142. blake_large_expand(v, ctx);
  143. } else {
  144. if(length_b){
  145. v[12] ^= ctr;
  146. v[13] ^= ctr;
  147. }
  148. }
  149. if(ctx->appendone)
  150. buffer.v8[128-16-8] |= 0x01;
  151. buffer.v64[15] = ctr;
  152. blake_large_compress(v, buffer.v8);
  153. blake_large_collapse(ctx, v);
  154. }
  155. static const
  156. uint64_t blake512_iv[] = {
  157. 0x6A09E667F3BCC908LL, 0xBB67AE8584CAA73BLL,
  158. 0x3C6EF372FE94F82BLL, 0xA54FF53A5F1D36F1LL,
  159. 0x510E527FADE682D1LL, 0x9B05688C2B3E6C1FLL,
  160. 0x1F83D9ABFB41BD6BLL, 0x5BE0CD19137E2179LL
  161. };
  162. void blake512_init(blake512_ctx_t* ctx){
  163. uint8_t i;
  164. for(i=0; i<8; ++i){
  165. ctx->h[i] = blake512_iv[i];
  166. }
  167. memset(ctx->s, 0, 4*8);
  168. ctx->counter = 0;
  169. ctx->appendone = 1;
  170. }
  171. static const
  172. uint64_t blake384_iv[] = {
  173. 0xCBBB9D5DC1059ED8LL, 0x629A292A367CD507LL,
  174. 0x9159015A3070DD17LL, 0x152FECD8F70E5939LL,
  175. 0x67332667FFC00B31LL, 0x8EB44A8768581511LL,
  176. 0xDB0C2E0D64F98FA7LL, 0x47B5481DBEFA4FA4LL
  177. };
  178. void blake384_init(blake384_ctx_t* ctx){
  179. uint8_t i;
  180. for(i=0; i<8; ++i){
  181. ctx->h[i] = blake384_iv[i];
  182. }
  183. memset(ctx->s, 0, 4*8);
  184. ctx->counter = 0;
  185. ctx->appendone = 0;
  186. }
  187. void blake512_ctx2hash(void* dest, const blake512_ctx_t* ctx){
  188. uint8_t i;
  189. for(i=0; i<8; ++i){
  190. ((uint32_t*)dest)[2*i+0] = CHANGE_ENDIAN32((ctx->h[i])>>32);
  191. ((uint32_t*)dest)[2*i+1] = CHANGE_ENDIAN32((uint32_t)ctx->h[i]);
  192. }
  193. }
  194. void blake384_ctx2hash(void* dest, const blake384_ctx_t* ctx){
  195. uint8_t i;
  196. for(i=0; i<6; ++i){
  197. ((uint32_t*)dest)[2*i+0] = CHANGE_ENDIAN32((ctx->h[i])>>32);
  198. ((uint32_t*)dest)[2*i+1] = CHANGE_ENDIAN32((uint32_t)ctx->h[i]);
  199. }
  200. }
  201. void blake512_nextBlock(blake512_ctx_t* ctx, const void* block){
  202. blake_large_nextBlock(ctx, block);
  203. }
  204. void blake384_nextBlock(blake384_ctx_t* ctx, const void* block){
  205. blake_large_nextBlock(ctx, block);
  206. }
  207. void blake512_lastBlock(blake512_ctx_t* ctx, const void* block, uint16_t length_b){
  208. blake_large_lastBlock(ctx, block, length_b);
  209. }
  210. void blake384_lastBlock(blake384_ctx_t* ctx, const void* block, uint16_t length_b){
  211. blake_large_lastBlock(ctx, block, length_b);
  212. }
  213. void blake512(void* dest, const void* msg, uint32_t length_b){
  214. blake_large_ctx_t ctx;
  215. blake512_init(&ctx);
  216. while(length_b>=BLAKE_LARGE_BLOCKSIZE){
  217. blake_large_nextBlock(&ctx, msg);
  218. msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
  219. length_b -= BLAKE_LARGE_BLOCKSIZE;
  220. }
  221. blake_large_lastBlock(&ctx, msg, length_b);
  222. blake512_ctx2hash(dest, &ctx);
  223. }
  224. void blake384(void* dest, const void* msg, uint32_t length_b){
  225. blake_large_ctx_t ctx;
  226. blake384_init(&ctx);
  227. while(length_b>=BLAKE_LARGE_BLOCKSIZE){
  228. blake_large_nextBlock(&ctx, msg);
  229. msg = (uint8_t*)msg + BLAKE_LARGE_BLOCKSIZE_B;
  230. length_b -= BLAKE_LARGE_BLOCKSIZE;
  231. }
  232. blake_large_lastBlock(&ctx, msg, length_b);
  233. blake384_ctx2hash(dest, &ctx);
  234. }