linux/drivers/staging/skein/skein_block.h
<<
>>
Prefs
   1/*
   2 ***********************************************************************
   3 *
   4 * Implementation of the Skein hash function.
   5 *
   6 * Source code author: Doug Whiting, 2008.
   7 *
   8 * This algorithm and source code is released to the public domain.
   9 *
  10 ***********************************************************************
  11 */
  12#ifndef _SKEIN_BLOCK_H_
  13#define _SKEIN_BLOCK_H_
  14
  15#include "skein_base.h" /* get the Skein API definitions   */
  16
  17#ifndef SKEIN_USE_ASM
  18#define SKEIN_USE_ASM   (0) /* default is all C code (no ASM) */
  19#endif
  20
  21#ifndef SKEIN_LOOP
  22#define SKEIN_LOOP 001 /* default: unroll 256 and 512, but not 1024 */
  23#endif
  24
  25#define BLK_BITS        (WCNT * 64) /* some useful definitions for code here */
  26#define KW_TWK_BASE     (0)
  27#define KW_KEY_BASE     (3)
  28#define ks              (kw + KW_KEY_BASE)
  29#define ts              (kw + KW_TWK_BASE)
  30
  31#ifdef SKEIN_DEBUG
  32#define debug_save_tweak(ctx)       \
  33{                                   \
  34        ctx->h.tweak[0] = ts[0];    \
  35        ctx->h.tweak[1] = ts[1];    \
  36}
  37#else
  38#define debug_save_tweak(ctx)
  39#endif
  40
  41#if !(SKEIN_USE_ASM & 256)
  42#undef  RCNT
  43#define RCNT (SKEIN_256_ROUNDS_TOTAL / 8)
  44#ifdef SKEIN_LOOP /* configure how much to unroll the loop */
  45#define SKEIN_UNROLL_256 (((SKEIN_LOOP) / 100) % 10)
  46#else
  47#define SKEIN_UNROLL_256 (0)
  48#endif
  49
  50#if SKEIN_UNROLL_256
  51#if (RCNT % SKEIN_UNROLL_256)
  52#error "Invalid SKEIN_UNROLL_256" /* sanity check on unroll count */
  53#endif
  54#endif
  55#define ROUND256(p0, p1, p2, p3, ROT, r_num)         \
  56        do {                                         \
  57                X##p0 += X##p1;                      \
  58                X##p1 = rol64(X##p1, ROT##_0);       \
  59                X##p1 ^= X##p0;                      \
  60                X##p2 += X##p3;                      \
  61                X##p3 = rol64(X##p3, ROT##_1);       \
  62                X##p3 ^= X##p2;                      \
  63        } while (0)
  64
  65#if SKEIN_UNROLL_256 == 0
  66#define R256(p0, p1, p2, p3, ROT, r_num) /* fully unrolled */ \
  67        ROUND256(p0, p1, p2, p3, ROT, r_num)
  68
  69#define I256(R)                                                         \
  70        do {                                                            \
  71                /* inject the key schedule value */                     \
  72                X0   += ks[((R) + 1) % 5];                              \
  73                X1   += ks[((R) + 2) % 5] + ts[((R) + 1) % 3];          \
  74                X2   += ks[((R) + 3) % 5] + ts[((R) + 2) % 3];          \
  75                X3   += ks[((R) + 4) % 5] + (R) + 1;                    \
  76        } while (0)
  77#else
  78/* looping version */
  79#define R256(p0, p1, p2, p3, ROT, r_num) ROUND256(p0, p1, p2, p3, ROT, r_num)
  80
  81#define I256(R)                                         \
  82        do {                                            \
  83                /* inject the key schedule value */     \
  84                X0 += ks[r + (R) + 0];                  \
  85                X1 += ks[r + (R) + 1] + ts[r + (R) + 0];\
  86                X2 += ks[r + (R) + 2] + ts[r + (R) + 1];\
  87                X3 += ks[r + (R) + 3] + r + (R);        \
  88                /* rotate key schedule */               \
  89                ks[r + (R) + 4] = ks[r + (R) - 1];      \
  90                ts[r + (R) + 2] = ts[r + (R) - 1];      \
  91        } while (0)
  92#endif
  93#define R256_8_ROUNDS(R)                                \
  94        do {                                            \
  95                R256(0, 1, 2, 3, R_256_0, 8 * (R) + 1); \
  96                R256(0, 3, 2, 1, R_256_1, 8 * (R) + 2); \
  97                R256(0, 1, 2, 3, R_256_2, 8 * (R) + 3); \
  98                R256(0, 3, 2, 1, R_256_3, 8 * (R) + 4); \
  99                I256(2 * (R));                          \
 100                R256(0, 1, 2, 3, R_256_4, 8 * (R) + 5); \
 101                R256(0, 3, 2, 1, R_256_5, 8 * (R) + 6); \
 102                R256(0, 1, 2, 3, R_256_6, 8 * (R) + 7); \
 103                R256(0, 3, 2, 1, R_256_7, 8 * (R) + 8); \
 104                I256(2 * (R) + 1);                      \
 105        } while (0)
 106
 107#define R256_UNROLL_R(NN)                     \
 108        ((SKEIN_UNROLL_256 == 0 &&            \
 109        SKEIN_256_ROUNDS_TOTAL / 8 > (NN)) || \
 110        (SKEIN_UNROLL_256 > (NN)))
 111
 112#if  (SKEIN_UNROLL_256 > 14)
 113#error  "need more unrolling in skein_256_process_block"
 114#endif
 115#endif
 116
 117#if !(SKEIN_USE_ASM & 512)
 118#undef  RCNT
 119#define RCNT  (SKEIN_512_ROUNDS_TOTAL / 8)
 120
 121#ifdef SKEIN_LOOP /* configure how much to unroll the loop */
 122#define SKEIN_UNROLL_512 (((SKEIN_LOOP) / 10) % 10)
 123#else
 124#define SKEIN_UNROLL_512 (0)
 125#endif
 126
 127#if SKEIN_UNROLL_512
 128#if (RCNT % SKEIN_UNROLL_512)
 129#error "Invalid SKEIN_UNROLL_512" /* sanity check on unroll count */
 130#endif
 131#endif
 132#define ROUND512(p0, p1, p2, p3, p4, p5, p6, p7, ROT, r_num)    \
 133        do {                                                    \
 134                X##p0 += X##p1;                                 \
 135                X##p1 = rol64(X##p1, ROT##_0);                  \
 136                X##p1 ^= X##p0;                                 \
 137                X##p2 += X##p3;                                 \
 138                X##p3 = rol64(X##p3, ROT##_1);                  \
 139                X##p3 ^= X##p2;                                 \
 140                X##p4 += X##p5;                                 \
 141                X##p5 = rol64(X##p5, ROT##_2);                  \
 142                X##p5 ^= X##p4;                                 \
 143                X##p6 += X##p7;                                 \
 144                X##p7 = rol64(X##p7, ROT##_3);                  \
 145                X##p7 ^= X##p6;                                 \
 146        } while (0)
 147
 148#if SKEIN_UNROLL_512 == 0
 149#define R512(p0, p1, p2, p3, p4, p5, p6, p7, ROT, r_num) /* unrolled */ \
 150        ROUND512(p0, p1, p2, p3, p4, p5, p6, p7, ROT, r_num)
 151
 152#define I512(R)                                                         \
 153        do {                                                            \
 154                /* inject the key schedule value */                     \
 155                X0   += ks[((R) + 1) % 9];                              \
 156                X1   += ks[((R) + 2) % 9];                              \
 157                X2   += ks[((R) + 3) % 9];                              \
 158                X3   += ks[((R) + 4) % 9];                              \
 159                X4   += ks[((R) + 5) % 9];                              \
 160                X5   += ks[((R) + 6) % 9] + ts[((R) + 1) % 3];          \
 161                X6   += ks[((R) + 7) % 9] + ts[((R) + 2) % 3];          \
 162                X7   += ks[((R) + 8) % 9] + (R) + 1;                    \
 163        } while (0)
 164
 165#else /* looping version */
 166#define R512(p0, p1, p2, p3, p4, p5, p6, p7, ROT, r_num)                 \
 167        ROUND512(p0, p1, p2, p3, p4, p5, p6, p7, ROT, r_num)             \
 168
 169#define I512(R)                                                         \
 170        do {                                                            \
 171                /* inject the key schedule value */                     \
 172                X0   += ks[r + (R) + 0];                                \
 173                X1   += ks[r + (R) + 1];                                \
 174                X2   += ks[r + (R) + 2];                                \
 175                X3   += ks[r + (R) + 3];                                \
 176                X4   += ks[r + (R) + 4];                                \
 177                X5   += ks[r + (R) + 5] + ts[r + (R) + 0];              \
 178                X6   += ks[r + (R) + 6] + ts[r + (R) + 1];              \
 179                X7   += ks[r + (R) + 7] + r + (R);                      \
 180                /* rotate key schedule */                               \
 181                ks[r + (R) + 8] = ks[r + (R) - 1];                      \
 182                ts[r + (R) + 2] = ts[r + (R) - 1];                      \
 183        } while (0)
 184#endif /* end of looped code definitions */
 185#define R512_8_ROUNDS(R)  /* do 8 full rounds */                        \
 186        do {                                                            \
 187                R512(0, 1, 2, 3, 4, 5, 6, 7, R_512_0, 8 * (R) + 1);     \
 188                R512(2, 1, 4, 7, 6, 5, 0, 3, R_512_1, 8 * (R) + 2);     \
 189                R512(4, 1, 6, 3, 0, 5, 2, 7, R_512_2, 8 * (R) + 3);     \
 190                R512(6, 1, 0, 7, 2, 5, 4, 3, R_512_3, 8 * (R) + 4);     \
 191                I512(2 * (R));                                          \
 192                R512(0, 1, 2, 3, 4, 5, 6, 7, R_512_4, 8 * (R) + 5);     \
 193                R512(2, 1, 4, 7, 6, 5, 0, 3, R_512_5, 8 * (R) + 6);     \
 194                R512(4, 1, 6, 3, 0, 5, 2, 7, R_512_6, 8 * (R) + 7);     \
 195                R512(6, 1, 0, 7, 2, 5, 4, 3, R_512_7, 8 * (R) + 8);     \
 196                I512(2 * (R) + 1); /* and key injection */              \
 197        } while (0)
 198#define R512_UNROLL_R(NN)                             \
 199                ((SKEIN_UNROLL_512 == 0 &&            \
 200                SKEIN_512_ROUNDS_TOTAL / 8 > (NN)) || \
 201                (SKEIN_UNROLL_512 > (NN)))
 202
 203#if  (SKEIN_UNROLL_512 > 14)
 204#error  "need more unrolling in skein_512_process_block"
 205#endif
 206#endif
 207
 208#if !(SKEIN_USE_ASM & 1024)
 209#undef  RCNT
 210#define RCNT  (SKEIN_1024_ROUNDS_TOTAL / 8)
 211#ifdef SKEIN_LOOP /* configure how much to unroll the loop */
 212#define SKEIN_UNROLL_1024 ((SKEIN_LOOP) % 10)
 213#else
 214#define SKEIN_UNROLL_1024 (0)
 215#endif
 216
 217#if (SKEIN_UNROLL_1024 != 0)
 218#if (RCNT % SKEIN_UNROLL_1024)
 219#error "Invalid SKEIN_UNROLL_1024" /* sanity check on unroll count */
 220#endif
 221#endif
 222#define ROUND1024(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, pA, pB, pC, pD, pE, \
 223                  pF, ROT, r_num)                                             \
 224        do {                                                                  \
 225                X##p0 += X##p1;                                               \
 226                X##p1 = rol64(X##p1, ROT##_0);                                \
 227                X##p1 ^= X##p0;                                               \
 228                X##p2 += X##p3;                                               \
 229                X##p3 = rol64(X##p3, ROT##_1);                                \
 230                X##p3 ^= X##p2;                                               \
 231                X##p4 += X##p5;                                               \
 232                X##p5 = rol64(X##p5, ROT##_2);                                \
 233                X##p5 ^= X##p4;                                               \
 234                X##p6 += X##p7;                                               \
 235                X##p7 = rol64(X##p7, ROT##_3);                                \
 236                X##p7 ^= X##p6;                                               \
 237                X##p8 += X##p9;                                               \
 238                X##p9 = rol64(X##p9, ROT##_4);                                \
 239                X##p9 ^= X##p8;                                               \
 240                X##pA += X##pB;                                               \
 241                X##pB = rol64(X##pB, ROT##_5);                                \
 242                X##pB ^= X##pA;                                               \
 243                X##pC += X##pD;                                               \
 244                X##pD = rol64(X##pD, ROT##_6);                                \
 245                X##pD ^= X##pC;                                               \
 246                X##pE += X##pF;                                               \
 247                X##pF = rol64(X##pF, ROT##_7);                                \
 248                X##pF ^= X##pE;                                               \
 249        } while (0)
 250
 251#if SKEIN_UNROLL_1024 == 0
 252#define R1024(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, pA, pB, pC, pD, pE, pF, \
 253              ROT, rn)                                                        \
 254        ROUND1024(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, pA, pB, pC, pD, pE, \
 255                  pF, ROT, rn)                                                \
 256
 257#define I1024(R)                                                \
 258        do {                                                    \
 259                /* inject the key schedule value */             \
 260                X00 += ks[((R) + 1) % 17];                      \
 261                X01 += ks[((R) + 2) % 17];                      \
 262                X02 += ks[((R) + 3) % 17];                      \
 263                X03 += ks[((R) + 4) % 17];                      \
 264                X04 += ks[((R) + 5) % 17];                      \
 265                X05 += ks[((R) + 6) % 17];                      \
 266                X06 += ks[((R) + 7) % 17];                      \
 267                X07 += ks[((R) + 8) % 17];                      \
 268                X08 += ks[((R) + 9) % 17];                      \
 269                X09 += ks[((R) + 10) % 17];                     \
 270                X10 += ks[((R) + 11) % 17];                     \
 271                X11 += ks[((R) + 12) % 17];                     \
 272                X12 += ks[((R) + 13) % 17];                     \
 273                X13 += ks[((R) + 14) % 17] + ts[((R) + 1) % 3]; \
 274                X14 += ks[((R) + 15) % 17] + ts[((R) + 2) % 3]; \
 275                X15 += ks[((R) + 16) % 17] + (R) + 1;           \
 276        } while (0)
 277#else /* looping version */
 278#define R1024(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, pA, pB, pC, pD, pE, pF, \
 279              ROT, rn)                                                        \
 280        ROUND1024(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, pA, pB, pC, pD, pE, \
 281                  pF, ROT, rn)                                                \
 282
 283#define I1024(R)                                                        \
 284        do {                                                            \
 285                /* inject the key schedule value */                     \
 286                X00 += ks[r + (R) + 0];                                 \
 287                X01 += ks[r + (R) + 1];                                 \
 288                X02 += ks[r + (R) + 2];                                 \
 289                X03 += ks[r + (R) + 3];                                 \
 290                X04 += ks[r + (R) + 4];                                 \
 291                X05 += ks[r + (R) + 5];                                 \
 292                X06 += ks[r + (R) + 6];                                 \
 293                X07 += ks[r + (R) + 7];                                 \
 294                X08 += ks[r + (R) + 8];                                 \
 295                X09 += ks[r + (R) + 9];                                 \
 296                X10 += ks[r + (R) + 10];                                \
 297                X11 += ks[r + (R) + 11];                                \
 298                X12 += ks[r + (R) + 12];                                \
 299                X13 += ks[r + (R) + 13] + ts[r + (R) + 0];              \
 300                X14 += ks[r + (R) + 14] + ts[r + (R) + 1];              \
 301                X15 += ks[r + (R) + 15] + r + (R);                      \
 302                /* rotate key schedule */                               \
 303                ks[r + (R) + 16] = ks[r + (R) - 1];                     \
 304                ts[r + (R) + 2] = ts[r + (R) - 1];                      \
 305        } while (0)
 306
 307#endif
 308#define R1024_8_ROUNDS(R)                                                 \
 309        do {                                                              \
 310                R1024(00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, \
 311                      13, 14, 15, R1024_0, 8 * (R) + 1);                  \
 312                R1024(00, 09, 02, 13, 06, 11, 04, 15, 10, 07, 12, 03, 14, \
 313                      05, 08, 01, R1024_1, 8 * (R) + 2);                  \
 314                R1024(00, 07, 02, 05, 04, 03, 06, 01, 12, 15, 14, 13, 08, \
 315                      11, 10, 09, R1024_2, 8 * (R) + 3);                  \
 316                R1024(00, 15, 02, 11, 06, 13, 04, 09, 14, 01, 08, 05, 10, \
 317                      03, 12, 07, R1024_3, 8 * (R) + 4);                  \
 318                I1024(2 * (R));                                           \
 319                R1024(00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, \
 320                      13, 14, 15, R1024_4, 8 * (R) + 5);                  \
 321                R1024(00, 09, 02, 13, 06, 11, 04, 15, 10, 07, 12, 03, 14, \
 322                      05, 08, 01, R1024_5, 8 * (R) + 6);                  \
 323                R1024(00, 07, 02, 05, 04, 03, 06, 01, 12, 15, 14, 13, 08, \
 324                      11, 10, 09, R1024_6, 8 * (R) + 7);                  \
 325                R1024(00, 15, 02, 11, 06, 13, 04, 09, 14, 01, 08, 05, 10, \
 326                      03, 12, 07, R1024_7, 8 * (R) + 8);                  \
 327                I1024(2 * (R) + 1);                                       \
 328        } while (0)
 329
 330#define R1024_UNROLL_R(NN)                              \
 331                ((SKEIN_UNROLL_1024 == 0 &&             \
 332                SKEIN_1024_ROUNDS_TOTAL / 8 > (NN)) ||  \
 333                (SKEIN_UNROLL_1024 > (NN)))
 334
 335#if  (SKEIN_UNROLL_1024 > 14)
 336#error  "need more unrolling in Skein_1024_Process_Block"
 337#endif
 338#endif
 339
 340void skein_256_process_block(struct skein_256_ctx *ctx, const u8 *blk_ptr,
 341                             size_t blk_cnt, size_t byte_cnt_add);
 342void skein_512_process_block(struct skein_512_ctx *ctx, const u8 *blk_ptr,
 343                             size_t blk_cnt, size_t byte_cnt_add);
 344void skein_1024_process_block(struct skein_1024_ctx *ctx, const u8 *blk_ptr,
 345                              size_t blk_cnt, size_t byte_cnt_add);
 346
 347#endif
 348