Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | /***********************************************************************
|
| 2 | **
|
| 3 | ** Implementation of the Skein block functions.
|
| 4 | **
|
| 5 | ** Source code author: Doug Whiting, 2008.
|
| 6 | **
|
| 7 | ** This algorithm and source code is released to the public domain.
|
| 8 | **
|
| 9 | ** Compile-time switches:
|
| 10 | **
|
| 11 | ** SKEIN_USE_ASM -- set bits (256/512/1024) to select which
|
| 12 | ** versions use ASM code for block processing
|
| 13 | ** [default: use C for all block sizes]
|
| 14 | **
|
| 15 | ************************************************************************/
|
| 16 |
|
| 17 | #include <string.h>
|
Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 18 | #include <crypto/skein.h>
|
Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 19 |
|
| 20 | #ifndef SKEIN_USE_ASM
|
| 21 | #define SKEIN_USE_ASM (0) /* default is all C code (no ASM) */
|
| 22 | #endif
|
| 23 |
|
| 24 | #ifndef SKEIN_LOOP
|
| 25 | #define SKEIN_LOOP 001 /* default: unroll 256 and 512, but not 1024 */
|
| 26 | #endif
|
| 27 |
|
| 28 | #define BLK_BITS (WCNT*64) /* some useful definitions for code here */
|
| 29 | #define KW_TWK_BASE (0)
|
| 30 | #define KW_KEY_BASE (3)
|
| 31 | #define ks (kw + KW_KEY_BASE)
|
| 32 | #define ts (kw + KW_TWK_BASE)
|
| 33 |
|
| 34 | #ifdef SKEIN_DEBUG
|
| 35 | #define DebugSaveTweak(ctx) { ctx->h.T[0] = ts[0]; ctx->h.T[1] = ts[1]; }
|
| 36 | #else
|
| 37 | #define DebugSaveTweak(ctx)
|
| 38 | #endif
|
| 39 |
|
| 40 | /***************************** Skein_256 ******************************/
|
| 41 | #if !(SKEIN_USE_ASM & 256)
|
| 42 | void Skein_256_Process_Block(Skein_256_Ctxt_t *ctx,const u08b_t *blkPtr,size_t blkCnt,size_t byteCntAdd)
|
| 43 | { /* do it in C */
|
| 44 | enum
|
| 45 | {
|
| 46 | WCNT = SKEIN_256_STATE_WORDS
|
| 47 | };
|
| 48 | #undef RCNT
|
| 49 | #define RCNT (SKEIN_256_ROUNDS_TOTAL/8)
|
| 50 |
|
| 51 | #ifdef SKEIN_LOOP /* configure how much to unroll the loop */
|
| 52 | #define SKEIN_UNROLL_256 (((SKEIN_LOOP)/100)%10)
|
| 53 | #else
|
| 54 | #define SKEIN_UNROLL_256 (0)
|
| 55 | #endif
|
| 56 |
|
| 57 | #if SKEIN_UNROLL_256
|
| 58 | #if (RCNT % SKEIN_UNROLL_256)
|
| 59 | #error "Invalid SKEIN_UNROLL_256" /* sanity check on unroll count */
|
| 60 | #endif
|
| 61 | size_t r;
|
| 62 | u64b_t kw[WCNT+4+RCNT*2]; /* key schedule words : chaining vars + tweak + "rotation"*/
|
| 63 | #else
|
| 64 | u64b_t kw[WCNT+4]; /* key schedule words : chaining vars + tweak */
|
| 65 | #endif
|
| 66 | u64b_t X0,X1,X2,X3; /* local copy of context vars, for speed */
|
| 67 | u64b_t w [WCNT]; /* local copy of input block */
|
| 68 | #ifdef SKEIN_DEBUG
|
| 69 | const u64b_t *Xptr[4]; /* use for debugging (help compiler put Xn in registers) */
|
| 70 | Xptr[0] = &X0; Xptr[1] = &X1; Xptr[2] = &X2; Xptr[3] = &X3;
|
| 71 | #endif
|
| 72 | Skein_assert(blkCnt != 0); /* never call with blkCnt == 0! */
|
| 73 | ts[0] = ctx->h.T[0];
|
| 74 | ts[1] = ctx->h.T[1];
|
| 75 | do {
|
| 76 | /* this implementation only supports 2**64 input bytes (no carry out here) */
|
| 77 | ts[0] += byteCntAdd; /* update processed length */
|
| 78 |
|
| 79 | /* precompute the key schedule for this block */
|
| 80 | ks[0] = ctx->X[0];
|
| 81 | ks[1] = ctx->X[1];
|
| 82 | ks[2] = ctx->X[2];
|
| 83 | ks[3] = ctx->X[3];
|
| 84 | ks[4] = ks[0] ^ ks[1] ^ ks[2] ^ ks[3] ^ SKEIN_KS_PARITY;
|
| 85 |
|
| 86 | ts[2] = ts[0] ^ ts[1];
|
| 87 |
|
| 88 | Skein_Get64_LSB_First(w,blkPtr,WCNT); /* get input block in little-endian format */
|
| 89 | DebugSaveTweak(ctx);
|
| 90 | Skein_Show_Block(BLK_BITS,&ctx->h,ctx->X,blkPtr,w,ks,ts);
|
| 91 |
|
| 92 | X0 = w[0] + ks[0]; /* do the first full key injection */
|
| 93 | X1 = w[1] + ks[1] + ts[0];
|
| 94 | X2 = w[2] + ks[2] + ts[1];
|
| 95 | X3 = w[3] + ks[3];
|
| 96 |
|
| 97 | Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INITIAL,Xptr); /* show starting state values */
|
| 98 |
|
| 99 | blkPtr += SKEIN_256_BLOCK_BYTES;
|
| 100 |
|
| 101 | /* run the rounds */
|
| 102 |
|
| 103 | #define Round256(p0,p1,p2,p3,ROT,rNum) \
|
| 104 | X##p0 += X##p1; X##p1 = RotL_64(X##p1,ROT##_0); X##p1 ^= X##p0; \
|
| 105 | X##p2 += X##p3; X##p3 = RotL_64(X##p3,ROT##_1); X##p3 ^= X##p2; \
|
| 106 |
|
| 107 | #if SKEIN_UNROLL_256 == 0
|
| 108 | #define R256(p0,p1,p2,p3,ROT,rNum) /* fully unrolled */ \
|
| 109 | Round256(p0,p1,p2,p3,ROT,rNum) \
|
| 110 | Skein_Show_R_Ptr(BLK_BITS,&ctx->h,rNum,Xptr);
|
| 111 |
|
| 112 | #define I256(R) \
|
| 113 | X0 += ks[((R)+1) % 5]; /* inject the key schedule value */ \
|
| 114 | X1 += ks[((R)+2) % 5] + ts[((R)+1) % 3]; \
|
| 115 | X2 += ks[((R)+3) % 5] + ts[((R)+2) % 3]; \
|
| 116 | X3 += ks[((R)+4) % 5] + (R)+1; \
|
| 117 | Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
|
| 118 | #else /* looping version */
|
| 119 | #define R256(p0,p1,p2,p3,ROT,rNum) \
|
| 120 | Round256(p0,p1,p2,p3,ROT,rNum) \
|
| 121 | Skein_Show_R_Ptr(BLK_BITS,&ctx->h,4*(r-1)+rNum,Xptr);
|
| 122 |
|
| 123 | #define I256(R) \
|
| 124 | X0 += ks[r+(R)+0]; /* inject the key schedule value */ \
|
| 125 | X1 += ks[r+(R)+1] + ts[r+(R)+0]; \
|
| 126 | X2 += ks[r+(R)+2] + ts[r+(R)+1]; \
|
| 127 | X3 += ks[r+(R)+3] + r+(R) ; \
|
| 128 | ks[r + (R)+4 ] = ks[r+(R)-1]; /* rotate key schedule */\
|
| 129 | ts[r + (R)+2 ] = ts[r+(R)-1]; \
|
| 130 | Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
|
| 131 |
|
| 132 | for (r=1;r < 2*RCNT;r+=2*SKEIN_UNROLL_256) /* loop thru it */
|
| 133 | #endif
|
| 134 | {
|
| 135 | #define R256_8_rounds(R) \
|
| 136 | R256(0,1,2,3,R_256_0,8*(R) + 1); \
|
| 137 | R256(0,3,2,1,R_256_1,8*(R) + 2); \
|
| 138 | R256(0,1,2,3,R_256_2,8*(R) + 3); \
|
| 139 | R256(0,3,2,1,R_256_3,8*(R) + 4); \
|
| 140 | I256(2*(R)); \
|
| 141 | R256(0,1,2,3,R_256_4,8*(R) + 5); \
|
| 142 | R256(0,3,2,1,R_256_5,8*(R) + 6); \
|
| 143 | R256(0,1,2,3,R_256_6,8*(R) + 7); \
|
| 144 | R256(0,3,2,1,R_256_7,8*(R) + 8); \
|
| 145 | I256(2*(R)+1);
|
| 146 |
|
| 147 | R256_8_rounds( 0);
|
| 148 |
|
| 149 | #define R256_Unroll_R(NN) ((SKEIN_UNROLL_256 == 0 && SKEIN_256_ROUNDS_TOTAL/8 > (NN)) || (SKEIN_UNROLL_256 > (NN)))
|
| 150 |
|
| 151 | #if R256_Unroll_R( 1)
|
| 152 | R256_8_rounds( 1);
|
| 153 | #endif
|
| 154 | #if R256_Unroll_R( 2)
|
| 155 | R256_8_rounds( 2);
|
| 156 | #endif
|
| 157 | #if R256_Unroll_R( 3)
|
| 158 | R256_8_rounds( 3);
|
| 159 | #endif
|
| 160 | #if R256_Unroll_R( 4)
|
| 161 | R256_8_rounds( 4);
|
| 162 | #endif
|
| 163 | #if R256_Unroll_R( 5)
|
| 164 | R256_8_rounds( 5);
|
| 165 | #endif
|
| 166 | #if R256_Unroll_R( 6)
|
| 167 | R256_8_rounds( 6);
|
| 168 | #endif
|
| 169 | #if R256_Unroll_R( 7)
|
| 170 | R256_8_rounds( 7);
|
| 171 | #endif
|
| 172 | #if R256_Unroll_R( 8)
|
| 173 | R256_8_rounds( 8);
|
| 174 | #endif
|
| 175 | #if R256_Unroll_R( 9)
|
| 176 | R256_8_rounds( 9);
|
| 177 | #endif
|
| 178 | #if R256_Unroll_R(10)
|
| 179 | R256_8_rounds(10);
|
| 180 | #endif
|
| 181 | #if R256_Unroll_R(11)
|
| 182 | R256_8_rounds(11);
|
| 183 | #endif
|
| 184 | #if R256_Unroll_R(12)
|
| 185 | R256_8_rounds(12);
|
| 186 | #endif
|
| 187 | #if R256_Unroll_R(13)
|
| 188 | R256_8_rounds(13);
|
| 189 | #endif
|
| 190 | #if R256_Unroll_R(14)
|
| 191 | R256_8_rounds(14);
|
| 192 | #endif
|
| 193 | #if (SKEIN_UNROLL_256 > 14)
|
| 194 | #error "need more unrolling in Skein_256_Process_Block"
|
| 195 | #endif
|
| 196 | }
|
| 197 | /* do the final "feedforward" xor, update context chaining vars */
|
| 198 | ctx->X[0] = X0 ^ w[0];
|
| 199 | ctx->X[1] = X1 ^ w[1];
|
| 200 | ctx->X[2] = X2 ^ w[2];
|
| 201 | ctx->X[3] = X3 ^ w[3];
|
| 202 |
|
| 203 | Skein_Show_Round(BLK_BITS,&ctx->h,SKEIN_RND_FEED_FWD,ctx->X);
|
| 204 |
|
| 205 | ts[1] &= ~SKEIN_T1_FLAG_FIRST;
|
| 206 | }
|
| 207 | while (--blkCnt);
|
| 208 | ctx->h.T[0] = ts[0];
|
| 209 | ctx->h.T[1] = ts[1];
|
| 210 | }
|
| 211 |
|
| 212 | #if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF)
|
| 213 | size_t Skein_256_Process_Block_CodeSize(void)
|
| 214 | {
|
| 215 | return ((u08b_t *) Skein_256_Process_Block_CodeSize) -
|
| 216 | ((u08b_t *) Skein_256_Process_Block);
|
| 217 | }
|
| 218 | uint_t Skein_256_Unroll_Cnt(void)
|
| 219 | {
|
| 220 | return SKEIN_UNROLL_256;
|
| 221 | }
|
| 222 | #endif
|
| 223 | #endif
|
| 224 |
|
| 225 | /***************************** Skein_512 ******************************/
|
| 226 | #if !(SKEIN_USE_ASM & 512)
|
| 227 | void Skein_512_Process_Block(Skein_512_Ctxt_t *ctx,const u08b_t *blkPtr,size_t blkCnt,size_t byteCntAdd)
|
| 228 | { /* do it in C */
|
| 229 | enum
|
| 230 | {
|
| 231 | WCNT = SKEIN_512_STATE_WORDS
|
| 232 | };
|
| 233 | #undef RCNT
|
| 234 | #define RCNT (SKEIN_512_ROUNDS_TOTAL/8)
|
| 235 |
|
| 236 | #ifdef SKEIN_LOOP /* configure how much to unroll the loop */
|
| 237 | #define SKEIN_UNROLL_512 (((SKEIN_LOOP)/10)%10)
|
| 238 | #else
|
| 239 | #define SKEIN_UNROLL_512 (0)
|
| 240 | #endif
|
| 241 |
|
| 242 | #if SKEIN_UNROLL_512
|
| 243 | #if (RCNT % SKEIN_UNROLL_512)
|
| 244 | #error "Invalid SKEIN_UNROLL_512" /* sanity check on unroll count */
|
| 245 | #endif
|
| 246 | size_t r;
|
| 247 | u64b_t kw[WCNT+4+RCNT*2]; /* key schedule words : chaining vars + tweak + "rotation"*/
|
| 248 | #else
|
| 249 | u64b_t kw[WCNT+4]; /* key schedule words : chaining vars + tweak */
|
| 250 | #endif
|
| 251 | u64b_t X0,X1,X2,X3,X4,X5,X6,X7; /* local copy of vars, for speed */
|
| 252 | u64b_t w [WCNT]; /* local copy of input block */
|
| 253 | #ifdef SKEIN_DEBUG
|
| 254 | const u64b_t *Xptr[8]; /* use for debugging (help compiler put Xn in registers) */
|
| 255 | Xptr[0] = &X0; Xptr[1] = &X1; Xptr[2] = &X2; Xptr[3] = &X3;
|
| 256 | Xptr[4] = &X4; Xptr[5] = &X5; Xptr[6] = &X6; Xptr[7] = &X7;
|
| 257 | #endif
|
| 258 |
|
| 259 | Skein_assert(blkCnt != 0); /* never call with blkCnt == 0! */
|
| 260 | ts[0] = ctx->h.T[0];
|
| 261 | ts[1] = ctx->h.T[1];
|
| 262 | do {
|
| 263 | /* this implementation only supports 2**64 input bytes (no carry out here) */
|
| 264 | ts[0] += byteCntAdd; /* update processed length */
|
| 265 |
|
| 266 | /* precompute the key schedule for this block */
|
| 267 | ks[0] = ctx->X[0];
|
| 268 | ks[1] = ctx->X[1];
|
| 269 | ks[2] = ctx->X[2];
|
| 270 | ks[3] = ctx->X[3];
|
| 271 | ks[4] = ctx->X[4];
|
| 272 | ks[5] = ctx->X[5];
|
| 273 | ks[6] = ctx->X[6];
|
| 274 | ks[7] = ctx->X[7];
|
| 275 | ks[8] = ks[0] ^ ks[1] ^ ks[2] ^ ks[3] ^
|
| 276 | ks[4] ^ ks[5] ^ ks[6] ^ ks[7] ^ SKEIN_KS_PARITY;
|
| 277 |
|
| 278 | ts[2] = ts[0] ^ ts[1];
|
| 279 |
|
| 280 | Skein_Get64_LSB_First(w,blkPtr,WCNT); /* get input block in little-endian format */
|
| 281 | DebugSaveTweak(ctx);
|
| 282 | Skein_Show_Block(BLK_BITS,&ctx->h,ctx->X,blkPtr,w,ks,ts);
|
| 283 |
|
| 284 | X0 = w[0] + ks[0]; /* do the first full key injection */
|
| 285 | X1 = w[1] + ks[1];
|
| 286 | X2 = w[2] + ks[2];
|
| 287 | X3 = w[3] + ks[3];
|
| 288 | X4 = w[4] + ks[4];
|
| 289 | X5 = w[5] + ks[5] + ts[0];
|
| 290 | X6 = w[6] + ks[6] + ts[1];
|
| 291 | X7 = w[7] + ks[7];
|
| 292 |
|
| 293 | blkPtr += SKEIN_512_BLOCK_BYTES;
|
| 294 |
|
| 295 | Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INITIAL,Xptr);
|
| 296 | /* run the rounds */
|
| 297 | #define Round512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \
|
| 298 | X##p0 += X##p1; X##p1 = RotL_64(X##p1,ROT##_0); X##p1 ^= X##p0; \
|
| 299 | X##p2 += X##p3; X##p3 = RotL_64(X##p3,ROT##_1); X##p3 ^= X##p2; \
|
| 300 | X##p4 += X##p5; X##p5 = RotL_64(X##p5,ROT##_2); X##p5 ^= X##p4; \
|
| 301 | X##p6 += X##p7; X##p7 = RotL_64(X##p7,ROT##_3); X##p7 ^= X##p6; \
|
| 302 |
|
| 303 | #if SKEIN_UNROLL_512 == 0
|
| 304 | #define R512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) /* unrolled */ \
|
| 305 | Round512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \
|
| 306 | Skein_Show_R_Ptr(BLK_BITS,&ctx->h,rNum,Xptr);
|
| 307 |
|
| 308 | #define I512(R) \
|
| 309 | X0 += ks[((R)+1) % 9]; /* inject the key schedule value */ \
|
| 310 | X1 += ks[((R)+2) % 9]; \
|
| 311 | X2 += ks[((R)+3) % 9]; \
|
| 312 | X3 += ks[((R)+4) % 9]; \
|
| 313 | X4 += ks[((R)+5) % 9]; \
|
| 314 | X5 += ks[((R)+6) % 9] + ts[((R)+1) % 3]; \
|
| 315 | X6 += ks[((R)+7) % 9] + ts[((R)+2) % 3]; \
|
| 316 | X7 += ks[((R)+8) % 9] + (R)+1; \
|
| 317 | Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
|
| 318 | #else /* looping version */
|
| 319 | #define R512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \
|
| 320 | Round512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \
|
| 321 | Skein_Show_R_Ptr(BLK_BITS,&ctx->h,4*(r-1)+rNum,Xptr);
|
| 322 |
|
| 323 | #define I512(R) \
|
| 324 | X0 += ks[r+(R)+0]; /* inject the key schedule value */ \
|
| 325 | X1 += ks[r+(R)+1]; \
|
| 326 | X2 += ks[r+(R)+2]; \
|
| 327 | X3 += ks[r+(R)+3]; \
|
| 328 | X4 += ks[r+(R)+4]; \
|
| 329 | X5 += ks[r+(R)+5] + ts[r+(R)+0]; \
|
| 330 | X6 += ks[r+(R)+6] + ts[r+(R)+1]; \
|
| 331 | X7 += ks[r+(R)+7] + r+(R) ; \
|
| 332 | ks[r + (R)+8] = ks[r+(R)-1]; /* rotate key schedule */ \
|
| 333 | ts[r + (R)+2] = ts[r+(R)-1]; \
|
| 334 | Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
|
| 335 |
|
| 336 | for (r=1;r < 2*RCNT;r+=2*SKEIN_UNROLL_512) /* loop thru it */
|
| 337 | #endif /* end of looped code definitions */
|
| 338 | {
|
| 339 | #define R512_8_rounds(R) /* do 8 full rounds */ \
|
| 340 | R512(0,1,2,3,4,5,6,7,R_512_0,8*(R)+ 1); \
|
| 341 | R512(2,1,4,7,6,5,0,3,R_512_1,8*(R)+ 2); \
|
| 342 | R512(4,1,6,3,0,5,2,7,R_512_2,8*(R)+ 3); \
|
| 343 | R512(6,1,0,7,2,5,4,3,R_512_3,8*(R)+ 4); \
|
| 344 | I512(2*(R)); \
|
| 345 | R512(0,1,2,3,4,5,6,7,R_512_4,8*(R)+ 5); \
|
| 346 | R512(2,1,4,7,6,5,0,3,R_512_5,8*(R)+ 6); \
|
| 347 | R512(4,1,6,3,0,5,2,7,R_512_6,8*(R)+ 7); \
|
| 348 | R512(6,1,0,7,2,5,4,3,R_512_7,8*(R)+ 8); \
|
| 349 | I512(2*(R)+1); /* and key injection */
|
| 350 |
|
| 351 | R512_8_rounds( 0);
|
| 352 |
|
| 353 | #define R512_Unroll_R(NN) ((SKEIN_UNROLL_512 == 0 && SKEIN_512_ROUNDS_TOTAL/8 > (NN)) || (SKEIN_UNROLL_512 > (NN)))
|
| 354 |
|
| 355 | #if R512_Unroll_R( 1)
|
| 356 | R512_8_rounds( 1);
|
| 357 | #endif
|
| 358 | #if R512_Unroll_R( 2)
|
| 359 | R512_8_rounds( 2);
|
| 360 | #endif
|
| 361 | #if R512_Unroll_R( 3)
|
| 362 | R512_8_rounds( 3);
|
| 363 | #endif
|
| 364 | #if R512_Unroll_R( 4)
|
| 365 | R512_8_rounds( 4);
|
| 366 | #endif
|
| 367 | #if R512_Unroll_R( 5)
|
| 368 | R512_8_rounds( 5);
|
| 369 | #endif
|
| 370 | #if R512_Unroll_R( 6)
|
| 371 | R512_8_rounds( 6);
|
| 372 | #endif
|
| 373 | #if R512_Unroll_R( 7)
|
| 374 | R512_8_rounds( 7);
|
| 375 | #endif
|
| 376 | #if R512_Unroll_R( 8)
|
| 377 | R512_8_rounds( 8);
|
| 378 | #endif
|
| 379 | #if R512_Unroll_R( 9)
|
| 380 | R512_8_rounds( 9);
|
| 381 | #endif
|
| 382 | #if R512_Unroll_R(10)
|
| 383 | R512_8_rounds(10);
|
| 384 | #endif
|
| 385 | #if R512_Unroll_R(11)
|
| 386 | R512_8_rounds(11);
|
| 387 | #endif
|
| 388 | #if R512_Unroll_R(12)
|
| 389 | R512_8_rounds(12);
|
| 390 | #endif
|
| 391 | #if R512_Unroll_R(13)
|
| 392 | R512_8_rounds(13);
|
| 393 | #endif
|
| 394 | #if R512_Unroll_R(14)
|
| 395 | R512_8_rounds(14);
|
| 396 | #endif
|
| 397 | #if (SKEIN_UNROLL_512 > 14)
|
| 398 | #error "need more unrolling in Skein_512_Process_Block"
|
| 399 | #endif
|
| 400 | }
|
| 401 |
|
| 402 | /* do the final "feedforward" xor, update context chaining vars */
|
| 403 | ctx->X[0] = X0 ^ w[0];
|
| 404 | ctx->X[1] = X1 ^ w[1];
|
| 405 | ctx->X[2] = X2 ^ w[2];
|
| 406 | ctx->X[3] = X3 ^ w[3];
|
| 407 | ctx->X[4] = X4 ^ w[4];
|
| 408 | ctx->X[5] = X5 ^ w[5];
|
| 409 | ctx->X[6] = X6 ^ w[6];
|
| 410 | ctx->X[7] = X7 ^ w[7];
|
| 411 | Skein_Show_Round(BLK_BITS,&ctx->h,SKEIN_RND_FEED_FWD,ctx->X);
|
| 412 |
|
| 413 | ts[1] &= ~SKEIN_T1_FLAG_FIRST;
|
| 414 | }
|
| 415 | while (--blkCnt);
|
| 416 | ctx->h.T[0] = ts[0];
|
| 417 | ctx->h.T[1] = ts[1];
|
| 418 | }
|
| 419 |
|
| 420 | #if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF)
|
| 421 | size_t Skein_512_Process_Block_CodeSize(void)
|
| 422 | {
|
| 423 | return ((u08b_t *) Skein_512_Process_Block_CodeSize) -
|
| 424 | ((u08b_t *) Skein_512_Process_Block);
|
| 425 | }
|
| 426 | uint_t Skein_512_Unroll_Cnt(void)
|
| 427 | {
|
| 428 | return SKEIN_UNROLL_512;
|
| 429 | }
|
| 430 | #endif
|
| 431 | #endif
|
| 432 |
|
| 433 | /***************************** Skein1024 ******************************/
|
| 434 | #if !(SKEIN_USE_ASM & 1024)
|
| 435 | void Skein1024_Process_Block(Skein1024_Ctxt_t *ctx,const u08b_t *blkPtr,size_t blkCnt,size_t byteCntAdd)
|
| 436 | { /* do it in C, always looping (unrolled is bigger AND slower!) */
|
| 437 | enum
|
| 438 | {
|
| 439 | WCNT = SKEIN1024_STATE_WORDS
|
| 440 | };
|
| 441 | #undef RCNT
|
| 442 | #define RCNT (SKEIN1024_ROUNDS_TOTAL/8)
|
| 443 |
|
| 444 | #ifdef SKEIN_LOOP /* configure how much to unroll the loop */
|
| 445 | #define SKEIN_UNROLL_1024 ((SKEIN_LOOP)%10)
|
| 446 | #else
|
| 447 | #define SKEIN_UNROLL_1024 (0)
|
| 448 | #endif
|
| 449 |
|
| 450 | #if (SKEIN_UNROLL_1024 != 0)
|
| 451 | #if (RCNT % SKEIN_UNROLL_1024)
|
| 452 | #error "Invalid SKEIN_UNROLL_1024" /* sanity check on unroll count */
|
| 453 | #endif
|
| 454 | size_t r;
|
| 455 | u64b_t kw[WCNT+4+RCNT*2]; /* key schedule words : chaining vars + tweak + "rotation"*/
|
| 456 | #else
|
| 457 | u64b_t kw[WCNT+4]; /* key schedule words : chaining vars + tweak */
|
| 458 | #endif
|
| 459 |
|
| 460 | u64b_t X00,X01,X02,X03,X04,X05,X06,X07, /* local copy of vars, for speed */
|
| 461 | X08,X09,X10,X11,X12,X13,X14,X15;
|
| 462 | u64b_t w [WCNT]; /* local copy of input block */
|
| 463 | #ifdef SKEIN_DEBUG
|
| 464 | const u64b_t *Xptr[16]; /* use for debugging (help compiler put Xn in registers) */
|
| 465 | Xptr[ 0] = &X00; Xptr[ 1] = &X01; Xptr[ 2] = &X02; Xptr[ 3] = &X03;
|
| 466 | Xptr[ 4] = &X04; Xptr[ 5] = &X05; Xptr[ 6] = &X06; Xptr[ 7] = &X07;
|
| 467 | Xptr[ 8] = &X08; Xptr[ 9] = &X09; Xptr[10] = &X10; Xptr[11] = &X11;
|
| 468 | Xptr[12] = &X12; Xptr[13] = &X13; Xptr[14] = &X14; Xptr[15] = &X15;
|
| 469 | #endif
|
| 470 |
|
| 471 | Skein_assert(blkCnt != 0); /* never call with blkCnt == 0! */
|
| 472 | ts[0] = ctx->h.T[0];
|
| 473 | ts[1] = ctx->h.T[1];
|
| 474 | do {
|
| 475 | /* this implementation only supports 2**64 input bytes (no carry out here) */
|
| 476 | ts[0] += byteCntAdd; /* update processed length */
|
| 477 |
|
| 478 | /* precompute the key schedule for this block */
|
| 479 | ks[ 0] = ctx->X[ 0];
|
| 480 | ks[ 1] = ctx->X[ 1];
|
| 481 | ks[ 2] = ctx->X[ 2];
|
| 482 | ks[ 3] = ctx->X[ 3];
|
| 483 | ks[ 4] = ctx->X[ 4];
|
| 484 | ks[ 5] = ctx->X[ 5];
|
| 485 | ks[ 6] = ctx->X[ 6];
|
| 486 | ks[ 7] = ctx->X[ 7];
|
| 487 | ks[ 8] = ctx->X[ 8];
|
| 488 | ks[ 9] = ctx->X[ 9];
|
| 489 | ks[10] = ctx->X[10];
|
| 490 | ks[11] = ctx->X[11];
|
| 491 | ks[12] = ctx->X[12];
|
| 492 | ks[13] = ctx->X[13];
|
| 493 | ks[14] = ctx->X[14];
|
| 494 | ks[15] = ctx->X[15];
|
| 495 | ks[16] = ks[ 0] ^ ks[ 1] ^ ks[ 2] ^ ks[ 3] ^
|
| 496 | ks[ 4] ^ ks[ 5] ^ ks[ 6] ^ ks[ 7] ^
|
| 497 | ks[ 8] ^ ks[ 9] ^ ks[10] ^ ks[11] ^
|
| 498 | ks[12] ^ ks[13] ^ ks[14] ^ ks[15] ^ SKEIN_KS_PARITY;
|
| 499 |
|
| 500 | ts[2] = ts[0] ^ ts[1];
|
| 501 |
|
| 502 | Skein_Get64_LSB_First(w,blkPtr,WCNT); /* get input block in little-endian format */
|
| 503 | DebugSaveTweak(ctx);
|
| 504 | Skein_Show_Block(BLK_BITS,&ctx->h,ctx->X,blkPtr,w,ks,ts);
|
| 505 |
|
| 506 | X00 = w[ 0] + ks[ 0]; /* do the first full key injection */
|
| 507 | X01 = w[ 1] + ks[ 1];
|
| 508 | X02 = w[ 2] + ks[ 2];
|
| 509 | X03 = w[ 3] + ks[ 3];
|
| 510 | X04 = w[ 4] + ks[ 4];
|
| 511 | X05 = w[ 5] + ks[ 5];
|
| 512 | X06 = w[ 6] + ks[ 6];
|
| 513 | X07 = w[ 7] + ks[ 7];
|
| 514 | X08 = w[ 8] + ks[ 8];
|
| 515 | X09 = w[ 9] + ks[ 9];
|
| 516 | X10 = w[10] + ks[10];
|
| 517 | X11 = w[11] + ks[11];
|
| 518 | X12 = w[12] + ks[12];
|
| 519 | X13 = w[13] + ks[13] + ts[0];
|
| 520 | X14 = w[14] + ks[14] + ts[1];
|
| 521 | X15 = w[15] + ks[15];
|
| 522 |
|
| 523 | Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INITIAL,Xptr);
|
| 524 |
|
| 525 | #define Round1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rNum) \
|
| 526 | X##p0 += X##p1; X##p1 = RotL_64(X##p1,ROT##_0); X##p1 ^= X##p0; \
|
| 527 | X##p2 += X##p3; X##p3 = RotL_64(X##p3,ROT##_1); X##p3 ^= X##p2; \
|
| 528 | X##p4 += X##p5; X##p5 = RotL_64(X##p5,ROT##_2); X##p5 ^= X##p4; \
|
| 529 | X##p6 += X##p7; X##p7 = RotL_64(X##p7,ROT##_3); X##p7 ^= X##p6; \
|
| 530 | X##p8 += X##p9; X##p9 = RotL_64(X##p9,ROT##_4); X##p9 ^= X##p8; \
|
| 531 | X##pA += X##pB; X##pB = RotL_64(X##pB,ROT##_5); X##pB ^= X##pA; \
|
| 532 | X##pC += X##pD; X##pD = RotL_64(X##pD,ROT##_6); X##pD ^= X##pC; \
|
| 533 | X##pE += X##pF; X##pF = RotL_64(X##pF,ROT##_7); X##pF ^= X##pE; \
|
| 534 |
|
| 535 | #if SKEIN_UNROLL_1024 == 0
|
| 536 | #define R1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rn) \
|
| 537 | Round1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rn) \
|
| 538 | Skein_Show_R_Ptr(BLK_BITS,&ctx->h,rn,Xptr);
|
| 539 |
|
| 540 | #define I1024(R) \
|
| 541 | X00 += ks[((R)+ 1) % 17]; /* inject the key schedule value */ \
|
| 542 | X01 += ks[((R)+ 2) % 17]; \
|
| 543 | X02 += ks[((R)+ 3) % 17]; \
|
| 544 | X03 += ks[((R)+ 4) % 17]; \
|
| 545 | X04 += ks[((R)+ 5) % 17]; \
|
| 546 | X05 += ks[((R)+ 6) % 17]; \
|
| 547 | X06 += ks[((R)+ 7) % 17]; \
|
| 548 | X07 += ks[((R)+ 8) % 17]; \
|
| 549 | X08 += ks[((R)+ 9) % 17]; \
|
| 550 | X09 += ks[((R)+10) % 17]; \
|
| 551 | X10 += ks[((R)+11) % 17]; \
|
| 552 | X11 += ks[((R)+12) % 17]; \
|
| 553 | X12 += ks[((R)+13) % 17]; \
|
| 554 | X13 += ks[((R)+14) % 17] + ts[((R)+1) % 3]; \
|
| 555 | X14 += ks[((R)+15) % 17] + ts[((R)+2) % 3]; \
|
| 556 | X15 += ks[((R)+16) % 17] + (R)+1; \
|
| 557 | Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
|
| 558 | #else /* looping version */
|
| 559 | #define R1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rn) \
|
| 560 | Round1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rn) \
|
| 561 | Skein_Show_R_Ptr(BLK_BITS,&ctx->h,4*(r-1)+rn,Xptr);
|
| 562 |
|
| 563 | #define I1024(R) \
|
| 564 | X00 += ks[r+(R)+ 0]; /* inject the key schedule value */ \
|
| 565 | X01 += ks[r+(R)+ 1]; \
|
| 566 | X02 += ks[r+(R)+ 2]; \
|
| 567 | X03 += ks[r+(R)+ 3]; \
|
| 568 | X04 += ks[r+(R)+ 4]; \
|
| 569 | X05 += ks[r+(R)+ 5]; \
|
| 570 | X06 += ks[r+(R)+ 6]; \
|
| 571 | X07 += ks[r+(R)+ 7]; \
|
| 572 | X08 += ks[r+(R)+ 8]; \
|
| 573 | X09 += ks[r+(R)+ 9]; \
|
| 574 | X10 += ks[r+(R)+10]; \
|
| 575 | X11 += ks[r+(R)+11]; \
|
| 576 | X12 += ks[r+(R)+12]; \
|
| 577 | X13 += ks[r+(R)+13] + ts[r+(R)+0]; \
|
| 578 | X14 += ks[r+(R)+14] + ts[r+(R)+1]; \
|
| 579 | X15 += ks[r+(R)+15] + r+(R) ; \
|
| 580 | ks[r + (R)+16] = ks[r+(R)-1]; /* rotate key schedule */ \
|
| 581 | ts[r + (R)+ 2] = ts[r+(R)-1]; \
|
| 582 | Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr);
|
| 583 |
|
| 584 | for (r=1;r <= 2*RCNT;r+=2*SKEIN_UNROLL_1024) /* loop thru it */
|
| 585 | #endif
|
| 586 | {
|
| 587 | #define R1024_8_rounds(R) /* do 8 full rounds */ \
|
| 588 | R1024(00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,R1024_0,8*(R) + 1); \
|
| 589 | R1024(00,09,02,13,06,11,04,15,10,07,12,03,14,05,08,01,R1024_1,8*(R) + 2); \
|
| 590 | R1024(00,07,02,05,04,03,06,01,12,15,14,13,08,11,10,09,R1024_2,8*(R) + 3); \
|
| 591 | R1024(00,15,02,11,06,13,04,09,14,01,08,05,10,03,12,07,R1024_3,8*(R) + 4); \
|
| 592 | I1024(2*(R)); \
|
| 593 | R1024(00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,R1024_4,8*(R) + 5); \
|
| 594 | R1024(00,09,02,13,06,11,04,15,10,07,12,03,14,05,08,01,R1024_5,8*(R) + 6); \
|
| 595 | R1024(00,07,02,05,04,03,06,01,12,15,14,13,08,11,10,09,R1024_6,8*(R) + 7); \
|
| 596 | R1024(00,15,02,11,06,13,04,09,14,01,08,05,10,03,12,07,R1024_7,8*(R) + 8); \
|
| 597 | I1024(2*(R)+1);
|
| 598 |
|
| 599 | R1024_8_rounds( 0);
|
| 600 |
|
| 601 | #define R1024_Unroll_R(NN) ((SKEIN_UNROLL_1024 == 0 && SKEIN1024_ROUNDS_TOTAL/8 > (NN)) || (SKEIN_UNROLL_1024 > (NN)))
|
| 602 |
|
| 603 | #if R1024_Unroll_R( 1)
|
| 604 | R1024_8_rounds( 1);
|
| 605 | #endif
|
| 606 | #if R1024_Unroll_R( 2)
|
| 607 | R1024_8_rounds( 2);
|
| 608 | #endif
|
| 609 | #if R1024_Unroll_R( 3)
|
| 610 | R1024_8_rounds( 3);
|
| 611 | #endif
|
| 612 | #if R1024_Unroll_R( 4)
|
| 613 | R1024_8_rounds( 4);
|
| 614 | #endif
|
| 615 | #if R1024_Unroll_R( 5)
|
| 616 | R1024_8_rounds( 5);
|
| 617 | #endif
|
| 618 | #if R1024_Unroll_R( 6)
|
| 619 | R1024_8_rounds( 6);
|
| 620 | #endif
|
| 621 | #if R1024_Unroll_R( 7)
|
| 622 | R1024_8_rounds( 7);
|
| 623 | #endif
|
| 624 | #if R1024_Unroll_R( 8)
|
| 625 | R1024_8_rounds( 8);
|
| 626 | #endif
|
| 627 | #if R1024_Unroll_R( 9)
|
| 628 | R1024_8_rounds( 9);
|
| 629 | #endif
|
| 630 | #if R1024_Unroll_R(10)
|
| 631 | R1024_8_rounds(10);
|
| 632 | #endif
|
| 633 | #if R1024_Unroll_R(11)
|
| 634 | R1024_8_rounds(11);
|
| 635 | #endif
|
| 636 | #if R1024_Unroll_R(12)
|
| 637 | R1024_8_rounds(12);
|
| 638 | #endif
|
| 639 | #if R1024_Unroll_R(13)
|
| 640 | R1024_8_rounds(13);
|
| 641 | #endif
|
| 642 | #if R1024_Unroll_R(14)
|
| 643 | R1024_8_rounds(14);
|
| 644 | #endif
|
| 645 | #if (SKEIN_UNROLL_1024 > 14)
|
| 646 | #error "need more unrolling in Skein_1024_Process_Block"
|
| 647 | #endif
|
| 648 | }
|
| 649 | /* do the final "feedforward" xor, update context chaining vars */
|
| 650 |
|
| 651 | ctx->X[ 0] = X00 ^ w[ 0];
|
| 652 | ctx->X[ 1] = X01 ^ w[ 1];
|
| 653 | ctx->X[ 2] = X02 ^ w[ 2];
|
| 654 | ctx->X[ 3] = X03 ^ w[ 3];
|
| 655 | ctx->X[ 4] = X04 ^ w[ 4];
|
| 656 | ctx->X[ 5] = X05 ^ w[ 5];
|
| 657 | ctx->X[ 6] = X06 ^ w[ 6];
|
| 658 | ctx->X[ 7] = X07 ^ w[ 7];
|
| 659 | ctx->X[ 8] = X08 ^ w[ 8];
|
| 660 | ctx->X[ 9] = X09 ^ w[ 9];
|
| 661 | ctx->X[10] = X10 ^ w[10];
|
| 662 | ctx->X[11] = X11 ^ w[11];
|
| 663 | ctx->X[12] = X12 ^ w[12];
|
| 664 | ctx->X[13] = X13 ^ w[13];
|
| 665 | ctx->X[14] = X14 ^ w[14];
|
| 666 | ctx->X[15] = X15 ^ w[15];
|
| 667 |
|
| 668 | Skein_Show_Round(BLK_BITS,&ctx->h,SKEIN_RND_FEED_FWD,ctx->X);
|
| 669 |
|
| 670 | ts[1] &= ~SKEIN_T1_FLAG_FIRST;
|
| 671 | blkPtr += SKEIN1024_BLOCK_BYTES;
|
| 672 | }
|
| 673 | while (--blkCnt);
|
| 674 | ctx->h.T[0] = ts[0];
|
| 675 | ctx->h.T[1] = ts[1];
|
| 676 | }
|
| 677 |
|
| 678 | #if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF)
|
| 679 | size_t Skein1024_Process_Block_CodeSize(void)
|
| 680 | {
|
| 681 | return ((u08b_t *) Skein1024_Process_Block_CodeSize) -
|
| 682 | ((u08b_t *) Skein1024_Process_Block);
|
| 683 | }
|
| 684 | uint_t Skein1024_Unroll_Cnt(void)
|
| 685 | {
|
| 686 | return SKEIN_UNROLL_1024;
|
| 687 | }
|
| 688 | #endif
|
| 689 | #endif
|