| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#define SKEIN_PORT_CODE |
| 12 |
|
| 13 |
#include <stddef.h> |
| 14 |
#include <string.h> |
| 15 |
#include "skein.h" |
| 16 |
|
| 17 |
#define DISABLE_UNUSED 0 |
| 18 |
|
| 19 |
#ifndef SKEIN_256_NIST_MAX_HASHBITS |
| 20 |
#define SKEIN_256_NIST_MAX_HASHBITS (0) |
| 21 |
#endif |
| 22 |
|
| 23 |
#ifndef SKEIN_512_NIST_MAX_HASHBITS |
| 24 |
#define SKEIN_512_NIST_MAX_HASHBITS (512) |
| 25 |
#endif |
| 26 |
|
| 27 |
#define SKEIN_MODIFIER_WORDS ( 2) |
| 28 |
|
| 29 |
#define SKEIN_256_STATE_WORDS ( 4) |
| 30 |
#define SKEIN_512_STATE_WORDS ( 8) |
| 31 |
#define SKEIN1024_STATE_WORDS (16) |
| 32 |
#define SKEIN_MAX_STATE_WORDS (16) |
| 33 |
|
| 34 |
#define SKEIN_256_STATE_BYTES ( 8*SKEIN_256_STATE_WORDS) |
| 35 |
#define SKEIN_512_STATE_BYTES ( 8*SKEIN_512_STATE_WORDS) |
| 36 |
#define SKEIN1024_STATE_BYTES ( 8*SKEIN1024_STATE_WORDS) |
| 37 |
|
| 38 |
#define SKEIN_256_STATE_BITS (64*SKEIN_256_STATE_WORDS) |
| 39 |
#define SKEIN_512_STATE_BITS (64*SKEIN_512_STATE_WORDS) |
| 40 |
#define SKEIN1024_STATE_BITS (64*SKEIN1024_STATE_WORDS) |
| 41 |
|
| 42 |
#define SKEIN_256_BLOCK_BYTES ( 8*SKEIN_256_STATE_WORDS) |
| 43 |
#define SKEIN_512_BLOCK_BYTES ( 8*SKEIN_512_STATE_WORDS) |
| 44 |
#define SKEIN1024_BLOCK_BYTES ( 8*SKEIN1024_STATE_WORDS) |
| 45 |
|
| 46 |
#define SKEIN_RND_SPECIAL (1000u) |
| 47 |
#define SKEIN_RND_KEY_INITIAL (SKEIN_RND_SPECIAL+0u) |
| 48 |
#define SKEIN_RND_KEY_INJECT (SKEIN_RND_SPECIAL+1u) |
| 49 |
#define SKEIN_RND_FEED_FWD (SKEIN_RND_SPECIAL+2u) |
| 50 |
|
| 51 |
typedef struct |
| 52 |
{ |
| 53 |
size_t hashBitLen; |
| 54 |
size_t bCnt; |
| 55 |
u64b_t T[SKEIN_MODIFIER_WORDS]; |
| 56 |
} Skein_Ctxt_Hdr_t; |
| 57 |
|
| 58 |
typedef struct |
| 59 |
{ |
| 60 |
Skein_Ctxt_Hdr_t h; |
| 61 |
u64b_t X[SKEIN_256_STATE_WORDS]; |
| 62 |
u08b_t b[SKEIN_256_BLOCK_BYTES]; |
| 63 |
} Skein_256_Ctxt_t; |
| 64 |
|
| 65 |
typedef struct |
| 66 |
{ |
| 67 |
Skein_Ctxt_Hdr_t h; |
| 68 |
u64b_t X[SKEIN_512_STATE_WORDS]; |
| 69 |
u08b_t b[SKEIN_512_BLOCK_BYTES]; |
| 70 |
} Skein_512_Ctxt_t; |
| 71 |
|
| 72 |
typedef struct |
| 73 |
{ |
| 74 |
Skein_Ctxt_Hdr_t h; |
| 75 |
u64b_t X[SKEIN1024_STATE_WORDS]; |
| 76 |
u08b_t b[SKEIN1024_BLOCK_BYTES]; |
| 77 |
} Skein1024_Ctxt_t; |
| 78 |
|
| 79 |
|
| 80 |
#if SKEIN_256_NIST_MAX_HASHBITS |
| 81 |
static int Skein_256_Init (Skein_256_Ctxt_t *ctx, size_t hashBitLen); |
| 82 |
#endif |
| 83 |
static int Skein_512_Init (Skein_512_Ctxt_t *ctx, size_t hashBitLen); |
| 84 |
static int Skein1024_Init (Skein1024_Ctxt_t *ctx, size_t hashBitLen); |
| 85 |
|
| 86 |
static int Skein_256_Update(Skein_256_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt); |
| 87 |
static int Skein_512_Update(Skein_512_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt); |
| 88 |
static int Skein1024_Update(Skein1024_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt); |
| 89 |
|
| 90 |
static int Skein_256_Final (Skein_256_Ctxt_t *ctx, u08b_t * hashVal); |
| 91 |
static int Skein_512_Final (Skein_512_Ctxt_t *ctx, u08b_t * hashVal); |
| 92 |
static int Skein1024_Final (Skein1024_Ctxt_t *ctx, u08b_t * hashVal); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
#if 0 |
| 109 |
static int Skein_256_InitExt(Skein_256_Ctxt_t *ctx, size_t hashBitLen, u64b_t treeInfo, const u08b_t *key, size_t keyBytes); |
| 110 |
static int Skein_512_InitExt(Skein_512_Ctxt_t *ctx, size_t hashBitLen, u64b_t treeInfo, const u08b_t *key, size_t keyBytes); |
| 111 |
static int Skein1024_InitExt(Skein1024_Ctxt_t *ctx, size_t hashBitLen, u64b_t treeInfo, const u08b_t *key, size_t keyBytes); |
| 112 |
#endif |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
#if 0 |
| 120 |
static int Skein_256_Final_Pad(Skein_256_Ctxt_t *ctx, u08b_t * hashVal); |
| 121 |
static int Skein_512_Final_Pad(Skein_512_Ctxt_t *ctx, u08b_t * hashVal); |
| 122 |
static int Skein1024_Final_Pad(Skein1024_Ctxt_t *ctx, u08b_t * hashVal); |
| 123 |
#endif |
| 124 |
|
| 125 |
#ifndef SKEIN_TREE_HASH |
| 126 |
#define SKEIN_TREE_HASH (1) |
| 127 |
#endif |
| 128 |
#if 0 |
| 129 |
#if SKEIN_TREE_HASH |
| 130 |
static int Skein_256_Output (Skein_256_Ctxt_t *ctx, u08b_t * hashVal); |
| 131 |
static int Skein_512_Output (Skein_512_Ctxt_t *ctx, u08b_t * hashVal); |
| 132 |
static int Skein1024_Output (Skein1024_Ctxt_t *ctx, u08b_t * hashVal); |
| 133 |
#endif |
| 134 |
#endif |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
#define SKEIN_T1_BIT(BIT) ((BIT) - 64) |
| 146 |
|
| 147 |
#define SKEIN_T1_POS_TREE_LVL SKEIN_T1_BIT(112) |
| 148 |
#define SKEIN_T1_POS_BIT_PAD SKEIN_T1_BIT(119) |
| 149 |
#define SKEIN_T1_POS_BLK_TYPE SKEIN_T1_BIT(120) |
| 150 |
#define SKEIN_T1_POS_FIRST SKEIN_T1_BIT(126) |
| 151 |
#define SKEIN_T1_POS_FINAL SKEIN_T1_BIT(127) |
| 152 |
|
| 153 |
|
| 154 |
#define SKEIN_T1_FLAG_FIRST (((u64b_t) 1 ) << SKEIN_T1_POS_FIRST) |
| 155 |
#define SKEIN_T1_FLAG_FINAL (((u64b_t) 1 ) << SKEIN_T1_POS_FINAL) |
| 156 |
#define SKEIN_T1_FLAG_BIT_PAD (((u64b_t) 1 ) << SKEIN_T1_POS_BIT_PAD) |
| 157 |
|
| 158 |
|
| 159 |
#define SKEIN_T1_TREE_LVL_MASK (((u64b_t)0x7F) << SKEIN_T1_POS_TREE_LVL) |
| 160 |
#define SKEIN_T1_TREE_LEVEL(n) (((u64b_t) (n)) << SKEIN_T1_POS_TREE_LVL) |
| 161 |
|
| 162 |
|
| 163 |
#define SKEIN_BLK_TYPE_KEY ( 0) |
| 164 |
#define SKEIN_BLK_TYPE_CFG ( 4) |
| 165 |
#define SKEIN_BLK_TYPE_PERS ( 8) |
| 166 |
#define SKEIN_BLK_TYPE_PK (12) |
| 167 |
#define SKEIN_BLK_TYPE_KDF (16) |
| 168 |
#define SKEIN_BLK_TYPE_NONCE (20) |
| 169 |
#define SKEIN_BLK_TYPE_MSG (48) |
| 170 |
#define SKEIN_BLK_TYPE_OUT (63) |
| 171 |
#define SKEIN_BLK_TYPE_MASK (63) |
| 172 |
|
| 173 |
#define SKEIN_T1_BLK_TYPE(T) (((u64b_t) (SKEIN_BLK_TYPE_##T)) << SKEIN_T1_POS_BLK_TYPE) |
| 174 |
#define SKEIN_T1_BLK_TYPE_KEY SKEIN_T1_BLK_TYPE(KEY) |
| 175 |
#define SKEIN_T1_BLK_TYPE_CFG SKEIN_T1_BLK_TYPE(CFG) |
| 176 |
#define SKEIN_T1_BLK_TYPE_PERS SKEIN_T1_BLK_TYPE(PERS) |
| 177 |
#define SKEIN_T1_BLK_TYPE_PK SKEIN_T1_BLK_TYPE(PK) |
| 178 |
#define SKEIN_T1_BLK_TYPE_KDF SKEIN_T1_BLK_TYPE(KDF) |
| 179 |
#define SKEIN_T1_BLK_TYPE_NONCE SKEIN_T1_BLK_TYPE(NONCE) |
| 180 |
#define SKEIN_T1_BLK_TYPE_MSG SKEIN_T1_BLK_TYPE(MSG) |
| 181 |
#define SKEIN_T1_BLK_TYPE_OUT SKEIN_T1_BLK_TYPE(OUT) |
| 182 |
#define SKEIN_T1_BLK_TYPE_MASK SKEIN_T1_BLK_TYPE(MASK) |
| 183 |
|
| 184 |
#define SKEIN_T1_BLK_TYPE_CFG_FINAL (SKEIN_T1_BLK_TYPE_CFG | SKEIN_T1_FLAG_FINAL) |
| 185 |
#define SKEIN_T1_BLK_TYPE_OUT_FINAL (SKEIN_T1_BLK_TYPE_OUT | SKEIN_T1_FLAG_FINAL) |
| 186 |
|
| 187 |
#define SKEIN_VERSION (1) |
| 188 |
|
| 189 |
#ifndef SKEIN_ID_STRING_LE |
| 190 |
#define SKEIN_ID_STRING_LE (0x33414853) |
| 191 |
#endif |
| 192 |
|
| 193 |
#define SKEIN_MK_64(hi32,lo32) ((lo32) + (((u64b_t) (hi32)) << 32)) |
| 194 |
#define SKEIN_SCHEMA_VER SKEIN_MK_64(SKEIN_VERSION,SKEIN_ID_STRING_LE) |
| 195 |
#define SKEIN_KS_PARITY SKEIN_MK_64(0x1BD11BDA,0xA9FC1A22) |
| 196 |
|
| 197 |
#define SKEIN_CFG_STR_LEN (4*8) |
| 198 |
|
| 199 |
|
| 200 |
#define SKEIN_CFG_TREE_LEAF_SIZE_POS ( 0) |
| 201 |
#define SKEIN_CFG_TREE_NODE_SIZE_POS ( 8) |
| 202 |
#define SKEIN_CFG_TREE_MAX_LEVEL_POS (16) |
| 203 |
|
| 204 |
#define SKEIN_CFG_TREE_LEAF_SIZE_MSK (((u64b_t) 0xFF) << SKEIN_CFG_TREE_LEAF_SIZE_POS) |
| 205 |
#define SKEIN_CFG_TREE_NODE_SIZE_MSK (((u64b_t) 0xFF) << SKEIN_CFG_TREE_NODE_SIZE_POS) |
| 206 |
#define SKEIN_CFG_TREE_MAX_LEVEL_MSK (((u64b_t) 0xFF) << SKEIN_CFG_TREE_MAX_LEVEL_POS) |
| 207 |
|
| 208 |
#define SKEIN_CFG_TREE_INFO(leaf,node,maxLvl) \ |
| 209 |
( (((u64b_t)(leaf )) << SKEIN_CFG_TREE_LEAF_SIZE_POS) | \ |
| 210 |
(((u64b_t)(node )) << SKEIN_CFG_TREE_NODE_SIZE_POS) | \ |
| 211 |
(((u64b_t)(maxLvl)) << SKEIN_CFG_TREE_MAX_LEVEL_POS) ) |
| 212 |
|
| 213 |
#define SKEIN_CFG_TREE_INFO_SEQUENTIAL SKEIN_CFG_TREE_INFO(0,0,0) |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
#define Skein_Get_Tweak(ctxPtr,TWK_NUM) ((ctxPtr)->h.T[TWK_NUM]) |
| 220 |
#define Skein_Set_Tweak(ctxPtr,TWK_NUM,tVal) {(ctxPtr)->h.T[TWK_NUM] = (tVal);} |
| 221 |
|
| 222 |
#define Skein_Get_T0(ctxPtr) Skein_Get_Tweak(ctxPtr,0) |
| 223 |
#define Skein_Get_T1(ctxPtr) Skein_Get_Tweak(ctxPtr,1) |
| 224 |
#define Skein_Set_T0(ctxPtr,T0) Skein_Set_Tweak(ctxPtr,0,T0) |
| 225 |
#define Skein_Set_T1(ctxPtr,T1) Skein_Set_Tweak(ctxPtr,1,T1) |
| 226 |
|
| 227 |
|
| 228 |
#define Skein_Set_T0_T1(ctxPtr,T0,T1) \ |
| 229 |
{ \ |
| 230 |
Skein_Set_T0(ctxPtr,(T0)); \ |
| 231 |
Skein_Set_T1(ctxPtr,(T1)); \ |
| 232 |
} |
| 233 |
|
| 234 |
#define Skein_Set_Type(ctxPtr,BLK_TYPE) \ |
| 235 |
Skein_Set_T1(ctxPtr,SKEIN_T1_BLK_TYPE_##BLK_TYPE) |
| 236 |
|
| 237 |
|
| 238 |
#define Skein_Start_New_Type(ctxPtr,BLK_TYPE) \ |
| 239 |
{ Skein_Set_T0_T1(ctxPtr,0,SKEIN_T1_FLAG_FIRST | SKEIN_T1_BLK_TYPE_##BLK_TYPE); (ctxPtr)->h.bCnt=0; } |
| 240 |
|
| 241 |
#define Skein_Clear_First_Flag(hdr) { (hdr).T[1] &= ~SKEIN_T1_FLAG_FIRST; } |
| 242 |
#define Skein_Set_Bit_Pad_Flag(hdr) { (hdr).T[1] |= SKEIN_T1_FLAG_BIT_PAD; } |
| 243 |
|
| 244 |
#define Skein_Set_Tree_Level(hdr,height) { (hdr).T[1] |= SKEIN_T1_TREE_LEVEL(height);} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
#define Skein_Show_Block(bits,ctx,X,blkPtr,wPtr,ksEvenPtr,ksOddPtr) |
| 250 |
#define Skein_Show_Round(bits,ctx,r,X) |
| 251 |
#define Skein_Show_R_Ptr(bits,ctx,r,X_ptr) |
| 252 |
#define Skein_Show_Final(bits,ctx,cnt,outPtr) |
| 253 |
#define Skein_Show_Key(bits,ctx,key,keyBytes) |
| 254 |
|
| 255 |
|
| 256 |
#ifndef SKEIN_ERR_CHECK |
| 257 |
#define Skein_Assert(x,retCode) |
| 258 |
#define Skein_assert(x) |
| 259 |
#elif defined(SKEIN_ASSERT) |
| 260 |
#include <assert.h> |
| 261 |
#define Skein_Assert(x,retCode) assert(x) |
| 262 |
#define Skein_assert(x) assert(x) |
| 263 |
#else |
| 264 |
#include <assert.h> |
| 265 |
#define Skein_Assert(x,retCode) { if (!(x)) return retCode; } |
| 266 |
#define Skein_assert(x) assert(x) |
| 267 |
#endif |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
enum |
| 273 |
{ |
| 274 |
|
| 275 |
R_256_0_0=14, R_256_0_1=16, |
| 276 |
R_256_1_0=52, R_256_1_1=57, |
| 277 |
R_256_2_0=23, R_256_2_1=40, |
| 278 |
R_256_3_0= 5, R_256_3_1=37, |
| 279 |
R_256_4_0=25, R_256_4_1=33, |
| 280 |
R_256_5_0=46, R_256_5_1=12, |
| 281 |
R_256_6_0=58, R_256_6_1=22, |
| 282 |
R_256_7_0=32, R_256_7_1=32, |
| 283 |
|
| 284 |
|
| 285 |
R_512_0_0=46, R_512_0_1=36, R_512_0_2=19, R_512_0_3=37, |
| 286 |
R_512_1_0=33, R_512_1_1=27, R_512_1_2=14, R_512_1_3=42, |
| 287 |
R_512_2_0=17, R_512_2_1=49, R_512_2_2=36, R_512_2_3=39, |
| 288 |
R_512_3_0=44, R_512_3_1= 9, R_512_3_2=54, R_512_3_3=56, |
| 289 |
R_512_4_0=39, R_512_4_1=30, R_512_4_2=34, R_512_4_3=24, |
| 290 |
R_512_5_0=13, R_512_5_1=50, R_512_5_2=10, R_512_5_3=17, |
| 291 |
R_512_6_0=25, R_512_6_1=29, R_512_6_2=39, R_512_6_3=43, |
| 292 |
R_512_7_0= 8, R_512_7_1=35, R_512_7_2=56, R_512_7_3=22, |
| 293 |
|
| 294 |
|
| 295 |
R1024_0_0=24, R1024_0_1=13, R1024_0_2= 8, R1024_0_3=47, R1024_0_4= 8, R1024_0_5=17, R1024_0_6=22, R1024_0_7=37, |
| 296 |
R1024_1_0=38, R1024_1_1=19, R1024_1_2=10, R1024_1_3=55, R1024_1_4=49, R1024_1_5=18, R1024_1_6=23, R1024_1_7=52, |
| 297 |
R1024_2_0=33, R1024_2_1= 4, R1024_2_2=51, R1024_2_3=13, R1024_2_4=34, R1024_2_5=41, R1024_2_6=59, R1024_2_7=17, |
| 298 |
R1024_3_0= 5, R1024_3_1=20, R1024_3_2=48, R1024_3_3=41, R1024_3_4=47, R1024_3_5=28, R1024_3_6=16, R1024_3_7=25, |
| 299 |
R1024_4_0=41, R1024_4_1= 9, R1024_4_2=37, R1024_4_3=31, R1024_4_4=12, R1024_4_5=47, R1024_4_6=44, R1024_4_7=30, |
| 300 |
R1024_5_0=16, R1024_5_1=34, R1024_5_2=56, R1024_5_3=51, R1024_5_4= 4, R1024_5_5=53, R1024_5_6=42, R1024_5_7=41, |
| 301 |
R1024_6_0=31, R1024_6_1=44, R1024_6_2=47, R1024_6_3=46, R1024_6_4=19, R1024_6_5=42, R1024_6_6=44, R1024_6_7=25, |
| 302 |
R1024_7_0= 9, R1024_7_1=48, R1024_7_2=35, R1024_7_3=52, R1024_7_4=23, R1024_7_5=31, R1024_7_6=37, R1024_7_7=20 |
| 303 |
}; |
| 304 |
|
| 305 |
#ifndef SKEIN_ROUNDS |
| 306 |
#define SKEIN_256_ROUNDS_TOTAL (72) |
| 307 |
#define SKEIN_512_ROUNDS_TOTAL (72) |
| 308 |
#define SKEIN1024_ROUNDS_TOTAL (80) |
| 309 |
#else |
| 310 |
#define SKEIN_256_ROUNDS_TOTAL (8*((((SKEIN_ROUNDS/100) + 5) % 10) + 5)) |
| 311 |
#define SKEIN_512_ROUNDS_TOTAL (8*((((SKEIN_ROUNDS/ 10) + 5) % 10) + 5)) |
| 312 |
#define SKEIN1024_ROUNDS_TOTAL (8*((((SKEIN_ROUNDS ) + 5) % 10) + 5)) |
| 313 |
#endif |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
#define MK_64 SKEIN_MK_64 |
| 331 |
|
| 332 |
|
| 333 |
const u64b_t SKEIN_256_IV_128[] = |
| 334 |
{ |
| 335 |
MK_64(0xE1111906,0x964D7260), |
| 336 |
MK_64(0x883DAAA7,0x7C8D811C), |
| 337 |
MK_64(0x10080DF4,0x91960F7A), |
| 338 |
MK_64(0xCCF7DDE5,0xB45BC1C2) |
| 339 |
}; |
| 340 |
|
| 341 |
|
| 342 |
const u64b_t SKEIN_256_IV_160[] = |
| 343 |
{ |
| 344 |
MK_64(0x14202314,0x72825E98), |
| 345 |
MK_64(0x2AC4E9A2,0x5A77E590), |
| 346 |
MK_64(0xD47A5856,0x8838D63E), |
| 347 |
MK_64(0x2DD2E496,0x8586AB7D) |
| 348 |
}; |
| 349 |
|
| 350 |
|
| 351 |
const u64b_t SKEIN_256_IV_224[] = |
| 352 |
{ |
| 353 |
MK_64(0xC6098A8C,0x9AE5EA0B), |
| 354 |
MK_64(0x876D5686,0x08C5191C), |
| 355 |
MK_64(0x99CB88D7,0xD7F53884), |
| 356 |
MK_64(0x384BDDB1,0xAEDDB5DE) |
| 357 |
}; |
| 358 |
|
| 359 |
|
| 360 |
const u64b_t SKEIN_256_IV_256[] = |
| 361 |
{ |
| 362 |
MK_64(0xFC9DA860,0xD048B449), |
| 363 |
MK_64(0x2FCA6647,0x9FA7D833), |
| 364 |
MK_64(0xB33BC389,0x6656840F), |
| 365 |
MK_64(0x6A54E920,0xFDE8DA69) |
| 366 |
}; |
| 367 |
|
| 368 |
|
| 369 |
const u64b_t SKEIN_512_IV_128[] = |
| 370 |
{ |
| 371 |
MK_64(0xA8BC7BF3,0x6FBF9F52), |
| 372 |
MK_64(0x1E9872CE,0xBD1AF0AA), |
| 373 |
MK_64(0x309B1790,0xB32190D3), |
| 374 |
MK_64(0xBCFBB854,0x3F94805C), |
| 375 |
MK_64(0x0DA61BCD,0x6E31B11B), |
| 376 |
MK_64(0x1A18EBEA,0xD46A32E3), |
| 377 |
MK_64(0xA2CC5B18,0xCE84AA82), |
| 378 |
MK_64(0x6982AB28,0x9D46982D) |
| 379 |
}; |
| 380 |
|
| 381 |
|
| 382 |
const u64b_t SKEIN_512_IV_160[] = |
| 383 |
{ |
| 384 |
MK_64(0x28B81A2A,0xE013BD91), |
| 385 |
MK_64(0xC2F11668,0xB5BDF78F), |
| 386 |
MK_64(0x1760D8F3,0xF6A56F12), |
| 387 |
MK_64(0x4FB74758,0x8239904F), |
| 388 |
MK_64(0x21EDE07F,0x7EAF5056), |
| 389 |
MK_64(0xD908922E,0x63ED70B8), |
| 390 |
MK_64(0xB8EC76FF,0xECCB52FA), |
| 391 |
MK_64(0x01A47BB8,0xA3F27A6E) |
| 392 |
}; |
| 393 |
|
| 394 |
|
| 395 |
const u64b_t SKEIN_512_IV_224[] = |
| 396 |
{ |
| 397 |
MK_64(0xCCD06162,0x48677224), |
| 398 |
MK_64(0xCBA65CF3,0xA92339EF), |
| 399 |
MK_64(0x8CCD69D6,0x52FF4B64), |
| 400 |
MK_64(0x398AED7B,0x3AB890B4), |
| 401 |
MK_64(0x0F59D1B1,0x457D2BD0), |
| 402 |
MK_64(0x6776FE65,0x75D4EB3D), |
| 403 |
MK_64(0x99FBC70E,0x997413E9), |
| 404 |
MK_64(0x9E2CFCCF,0xE1C41EF7) |
| 405 |
}; |
| 406 |
|
| 407 |
|
| 408 |
const u64b_t SKEIN_512_IV_256[] = |
| 409 |
{ |
| 410 |
MK_64(0xCCD044A1,0x2FDB3E13), |
| 411 |
MK_64(0xE8359030,0x1A79A9EB), |
| 412 |
MK_64(0x55AEA061,0x4F816E6F), |
| 413 |
MK_64(0x2A2767A4,0xAE9B94DB), |
| 414 |
MK_64(0xEC06025E,0x74DD7683), |
| 415 |
MK_64(0xE7A436CD,0xC4746251), |
| 416 |
MK_64(0xC36FBAF9,0x393AD185), |
| 417 |
MK_64(0x3EEDBA18,0x33EDFC13) |
| 418 |
}; |
| 419 |
|
| 420 |
|
| 421 |
const u64b_t SKEIN_512_IV_384[] = |
| 422 |
{ |
| 423 |
MK_64(0xA3F6C6BF,0x3A75EF5F), |
| 424 |
MK_64(0xB0FEF9CC,0xFD84FAA4), |
| 425 |
MK_64(0x9D77DD66,0x3D770CFE), |
| 426 |
MK_64(0xD798CBF3,0xB468FDDA), |
| 427 |
MK_64(0x1BC4A666,0x8A0E4465), |
| 428 |
MK_64(0x7ED7D434,0xE5807407), |
| 429 |
MK_64(0x548FC1AC,0xD4EC44D6), |
| 430 |
MK_64(0x266E1754,0x6AA18FF8) |
| 431 |
}; |
| 432 |
|
| 433 |
|
| 434 |
const u64b_t SKEIN_512_IV_512[] = |
| 435 |
{ |
| 436 |
MK_64(0x4903ADFF,0x749C51CE), |
| 437 |
MK_64(0x0D95DE39,0x9746DF03), |
| 438 |
MK_64(0x8FD19341,0x27C79BCE), |
| 439 |
MK_64(0x9A255629,0xFF352CB1), |
| 440 |
MK_64(0x5DB62599,0xDF6CA7B0), |
| 441 |
MK_64(0xEABE394C,0xA9D5C3F4), |
| 442 |
MK_64(0x991112C7,0x1A75B523), |
| 443 |
MK_64(0xAE18A40B,0x660FCC33) |
| 444 |
}; |
| 445 |
|
| 446 |
|
| 447 |
const u64b_t SKEIN1024_IV_384[] = |
| 448 |
{ |
| 449 |
MK_64(0x5102B6B8,0xC1894A35), |
| 450 |
MK_64(0xFEEBC9E3,0xFE8AF11A), |
| 451 |
MK_64(0x0C807F06,0xE32BED71), |
| 452 |
MK_64(0x60C13A52,0xB41A91F6), |
| 453 |
MK_64(0x9716D35D,0xD4917C38), |
| 454 |
MK_64(0xE780DF12,0x6FD31D3A), |
| 455 |
MK_64(0x797846B6,0xC898303A), |
| 456 |
MK_64(0xB172C2A8,0xB3572A3B), |
| 457 |
MK_64(0xC9BC8203,0xA6104A6C), |
| 458 |
MK_64(0x65909338,0xD75624F4), |
| 459 |
MK_64(0x94BCC568,0x4B3F81A0), |
| 460 |
MK_64(0x3EBBF51E,0x10ECFD46), |
| 461 |
MK_64(0x2DF50F0B,0xEEB08542), |
| 462 |
MK_64(0x3B5A6530,0x0DBC6516), |
| 463 |
MK_64(0x484B9CD2,0x167BBCE1), |
| 464 |
MK_64(0x2D136947,0xD4CBAFEA) |
| 465 |
}; |
| 466 |
|
| 467 |
|
| 468 |
const u64b_t SKEIN1024_IV_512[] = |
| 469 |
{ |
| 470 |
MK_64(0xCAEC0E5D,0x7C1B1B18), |
| 471 |
MK_64(0xA01B0E04,0x5F03E802), |
| 472 |
MK_64(0x33840451,0xED912885), |
| 473 |
MK_64(0x374AFB04,0xEAEC2E1C), |
| 474 |
MK_64(0xDF25A0E2,0x813581F7), |
| 475 |
MK_64(0xE4004093,0x8B12F9D2), |
| 476 |
MK_64(0xA662D539,0xC2ED39B6), |
| 477 |
MK_64(0xFA8B85CF,0x45D8C75A), |
| 478 |
MK_64(0x8316ED8E,0x29EDE796), |
| 479 |
MK_64(0x053289C0,0x2E9F91B8), |
| 480 |
MK_64(0xC3F8EF1D,0x6D518B73), |
| 481 |
MK_64(0xBDCEC3C4,0xD5EF332E), |
| 482 |
MK_64(0x549A7E52,0x22974487), |
| 483 |
MK_64(0x67070872,0x5B749816), |
| 484 |
MK_64(0xB9CD28FB,0xF0581BD1), |
| 485 |
MK_64(0x0E2940B8,0x15804974) |
| 486 |
}; |
| 487 |
|
| 488 |
|
| 489 |
const u64b_t SKEIN1024_IV_1024[] = |
| 490 |
{ |
| 491 |
MK_64(0xD593DA07,0x41E72355), |
| 492 |
MK_64(0x15B5E511,0xAC73E00C), |
| 493 |
MK_64(0x5180E5AE,0xBAF2C4F0), |
| 494 |
MK_64(0x03BD41D3,0xFCBCAFAF), |
| 495 |
MK_64(0x1CAEC6FD,0x1983A898), |
| 496 |
MK_64(0x6E510B8B,0xCDD0589F), |
| 497 |
MK_64(0x77E2BDFD,0xC6394ADA), |
| 498 |
MK_64(0xC11E1DB5,0x24DCB0A3), |
| 499 |
MK_64(0xD6D14AF9,0xC6329AB5), |
| 500 |
MK_64(0x6A9B0BFC,0x6EB67E0D), |
| 501 |
MK_64(0x9243C60D,0xCCFF1332), |
| 502 |
MK_64(0x1A1F1DDE,0x743F02D4), |
| 503 |
MK_64(0x0996753C,0x10ED0BB8), |
| 504 |
MK_64(0x6572DD22,0xF2B4969A), |
| 505 |
MK_64(0x61FD3062,0xD00A579A), |
| 506 |
MK_64(0x1DE0536E,0x8682E539) |
| 507 |
}; |
| 508 |
|
| 509 |
|
| 510 |
#ifndef SKEIN_USE_ASM |
| 511 |
#define SKEIN_USE_ASM (0) |
| 512 |
#endif |
| 513 |
|
| 514 |
#ifndef SKEIN_LOOP |
| 515 |
#define SKEIN_LOOP 001 |
| 516 |
#endif |
| 517 |
|
| 518 |
#define BLK_BITS (WCNT*64) |
| 519 |
#define KW_TWK_BASE (0) |
| 520 |
#define KW_KEY_BASE (3) |
| 521 |
#define ks (kw + KW_KEY_BASE) |
| 522 |
#define ts (kw + KW_TWK_BASE) |
| 523 |
|
| 524 |
#ifdef SKEIN_DEBUG |
| 525 |
#define DebugSaveTweak(ctx) { ctx->h.T[0] = ts[0]; ctx->h.T[1] = ts[1]; } |
| 526 |
#else |
| 527 |
#define DebugSaveTweak(ctx) |
| 528 |
#endif |
| 529 |
|
| 530 |
|
| 531 |
#if !(SKEIN_USE_ASM & 256) |
| 532 |
static void Skein_256_Process_Block(Skein_256_Ctxt_t *ctx,const u08b_t *blkPtr,size_t blkCnt,size_t byteCntAdd) |
| 533 |
{ |
| 534 |
enum |
| 535 |
{ |
| 536 |
WCNT = SKEIN_256_STATE_WORDS |
| 537 |
}; |
| 538 |
#undef RCNT |
| 539 |
#define RCNT (SKEIN_256_ROUNDS_TOTAL/8) |
| 540 |
|
| 541 |
#ifdef SKEIN_LOOP |
| 542 |
#define SKEIN_UNROLL_256 (((SKEIN_LOOP)/100)%10) |
| 543 |
#else |
| 544 |
#define SKEIN_UNROLL_256 (0) |
| 545 |
#endif |
| 546 |
|
| 547 |
#if SKEIN_UNROLL_256 |
| 548 |
#if (RCNT % SKEIN_UNROLL_256) |
| 549 |
#error "Invalid SKEIN_UNROLL_256" |
| 550 |
#endif |
| 551 |
size_t r; |
| 552 |
u64b_t kw[WCNT+4+RCNT*2]; |
| 553 |
#else |
| 554 |
u64b_t kw[WCNT+4]; |
| 555 |
#endif |
| 556 |
u64b_t X0,X1,X2,X3; |
| 557 |
u64b_t w [WCNT]; |
| 558 |
#ifdef SKEIN_DEBUG |
| 559 |
const u64b_t *Xptr[4]; |
| 560 |
Xptr[0] = &X0; Xptr[1] = &X1; Xptr[2] = &X2; Xptr[3] = &X3; |
| 561 |
#endif |
| 562 |
Skein_assert(blkCnt != 0); |
| 563 |
ts[0] = ctx->h.T[0]; |
| 564 |
ts[1] = ctx->h.T[1]; |
| 565 |
do { |
| 566 |
|
| 567 |
ts[0] += byteCntAdd; |
| 568 |
|
| 569 |
|
| 570 |
ks[0] = ctx->X[0]; |
| 571 |
ks[1] = ctx->X[1]; |
| 572 |
ks[2] = ctx->X[2]; |
| 573 |
ks[3] = ctx->X[3]; |
| 574 |
ks[4] = ks[0] ^ ks[1] ^ ks[2] ^ ks[3] ^ SKEIN_KS_PARITY; |
| 575 |
|
| 576 |
ts[2] = ts[0] ^ ts[1]; |
| 577 |
|
| 578 |
Skein_Get64_LSB_First(w,blkPtr,WCNT); |
| 579 |
DebugSaveTweak(ctx); |
| 580 |
Skein_Show_Block(BLK_BITS,&ctx->h,ctx->X,blkPtr,w,ks,ts); |
| 581 |
|
| 582 |
X0 = w[0] + ks[0]; |
| 583 |
X1 = w[1] + ks[1] + ts[0]; |
| 584 |
X2 = w[2] + ks[2] + ts[1]; |
| 585 |
X3 = w[3] + ks[3]; |
| 586 |
|
| 587 |
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INITIAL,Xptr); |
| 588 |
|
| 589 |
blkPtr += SKEIN_256_BLOCK_BYTES; |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
#define Round256(p0,p1,p2,p3,ROT,rNum) \ |
| 594 |
X##p0 += X##p1; X##p1 = RotL_64(X##p1,ROT##_0); X##p1 ^= X##p0; \ |
| 595 |
X##p2 += X##p3; X##p3 = RotL_64(X##p3,ROT##_1); X##p3 ^= X##p2; \ |
| 596 |
|
| 597 |
#if SKEIN_UNROLL_256 == 0 |
| 598 |
#define R256(p0,p1,p2,p3,ROT,rNum) \ |
| 599 |
Round256(p0,p1,p2,p3,ROT,rNum) \ |
| 600 |
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,rNum,Xptr); |
| 601 |
|
| 602 |
#define I256(R) \ |
| 603 |
X0 += ks[((R)+1) % 5]; \ |
| 604 |
X1 += ks[((R)+2) % 5] + ts[((R)+1) % 3]; \ |
| 605 |
X2 += ks[((R)+3) % 5] + ts[((R)+2) % 3]; \ |
| 606 |
X3 += ks[((R)+4) % 5] + (R)+1; \ |
| 607 |
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr); |
| 608 |
#else |
| 609 |
#define R256(p0,p1,p2,p3,ROT,rNum) \ |
| 610 |
Round256(p0,p1,p2,p3,ROT,rNum) \ |
| 611 |
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,4*(r-1)+rNum,Xptr); |
| 612 |
|
| 613 |
#define I256(R) \ |
| 614 |
X0 += ks[r+(R)+0]; \ |
| 615 |
X1 += ks[r+(R)+1] + ts[r+(R)+0]; \ |
| 616 |
X2 += ks[r+(R)+2] + ts[r+(R)+1]; \ |
| 617 |
X3 += ks[r+(R)+3] + r+(R) ; \ |
| 618 |
ks[r + (R)+4 ] = ks[r+(R)-1]; \ |
| 619 |
ts[r + (R)+2 ] = ts[r+(R)-1]; \ |
| 620 |
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr); |
| 621 |
|
| 622 |
for (r=1;r < 2*RCNT;r+=2*SKEIN_UNROLL_256) |
| 623 |
#endif |
| 624 |
{ |
| 625 |
#define R256_8_rounds(R) \ |
| 626 |
R256(0,1,2,3,R_256_0,8*(R) + 1); \ |
| 627 |
R256(0,3,2,1,R_256_1,8*(R) + 2); \ |
| 628 |
R256(0,1,2,3,R_256_2,8*(R) + 3); \ |
| 629 |
R256(0,3,2,1,R_256_3,8*(R) + 4); \ |
| 630 |
I256(2*(R)); \ |
| 631 |
R256(0,1,2,3,R_256_4,8*(R) + 5); \ |
| 632 |
R256(0,3,2,1,R_256_5,8*(R) + 6); \ |
| 633 |
R256(0,1,2,3,R_256_6,8*(R) + 7); \ |
| 634 |
R256(0,3,2,1,R_256_7,8*(R) + 8); \ |
| 635 |
I256(2*(R)+1); |
| 636 |
|
| 637 |
R256_8_rounds( 0); |
| 638 |
|
| 639 |
#define R256_Unroll_R(NN) ((SKEIN_UNROLL_256 == 0 && SKEIN_256_ROUNDS_TOTAL/8 > (NN)) || (SKEIN_UNROLL_256 > (NN))) |
| 640 |
|
| 641 |
#if R256_Unroll_R( 1) |
| 642 |
R256_8_rounds( 1); |
| 643 |
#endif |
| 644 |
#if R256_Unroll_R( 2) |
| 645 |
R256_8_rounds( 2); |
| 646 |
#endif |
| 647 |
#if R256_Unroll_R( 3) |
| 648 |
R256_8_rounds( 3); |
| 649 |
#endif |
| 650 |
#if R256_Unroll_R( 4) |
| 651 |
R256_8_rounds( 4); |
| 652 |
#endif |
| 653 |
#if R256_Unroll_R( 5) |
| 654 |
R256_8_rounds( 5); |
| 655 |
#endif |
| 656 |
#if R256_Unroll_R( 6) |
| 657 |
R256_8_rounds( 6); |
| 658 |
#endif |
| 659 |
#if R256_Unroll_R( 7) |
| 660 |
R256_8_rounds( 7); |
| 661 |
#endif |
| 662 |
#if R256_Unroll_R( 8) |
| 663 |
R256_8_rounds( 8); |
| 664 |
#endif |
| 665 |
#if R256_Unroll_R( 9) |
| 666 |
R256_8_rounds( 9); |
| 667 |
#endif |
| 668 |
#if R256_Unroll_R(10) |
| 669 |
R256_8_rounds(10); |
| 670 |
#endif |
| 671 |
#if R256_Unroll_R(11) |
| 672 |
R256_8_rounds(11); |
| 673 |
#endif |
| 674 |
#if R256_Unroll_R(12) |
| 675 |
R256_8_rounds(12); |
| 676 |
#endif |
| 677 |
#if R256_Unroll_R(13) |
| 678 |
R256_8_rounds(13); |
| 679 |
#endif |
| 680 |
#if R256_Unroll_R(14) |
| 681 |
R256_8_rounds(14); |
| 682 |
#endif |
| 683 |
#if (SKEIN_UNROLL_256 > 14) |
| 684 |
#error "need more unrolling in Skein_256_Process_Block" |
| 685 |
#endif |
| 686 |
} |
| 687 |
|
| 688 |
ctx->X[0] = X0 ^ w[0]; |
| 689 |
ctx->X[1] = X1 ^ w[1]; |
| 690 |
ctx->X[2] = X2 ^ w[2]; |
| 691 |
ctx->X[3] = X3 ^ w[3]; |
| 692 |
|
| 693 |
Skein_Show_Round(BLK_BITS,&ctx->h,SKEIN_RND_FEED_FWD,ctx->X); |
| 694 |
|
| 695 |
ts[1] &= ~SKEIN_T1_FLAG_FIRST; |
| 696 |
} |
| 697 |
while (--blkCnt); |
| 698 |
ctx->h.T[0] = ts[0]; |
| 699 |
ctx->h.T[1] = ts[1]; |
| 700 |
} |
| 701 |
|
| 702 |
#if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF) |
| 703 |
static size_t Skein_256_Process_Block_CodeSize(void) |
| 704 |
{ |
| 705 |
return ((u08b_t *) Skein_256_Process_Block_CodeSize) - |
| 706 |
((u08b_t *) Skein_256_Process_Block); |
| 707 |
} |
| 708 |
static uint_t Skein_256_Unroll_Cnt(void) |
| 709 |
{ |
| 710 |
return SKEIN_UNROLL_256; |
| 711 |
} |
| 712 |
#endif |
| 713 |
#endif |
| 714 |
|
| 715 |
|
| 716 |
#if !(SKEIN_USE_ASM & 512) |
| 717 |
static void Skein_512_Process_Block(Skein_512_Ctxt_t *ctx,const u08b_t *blkPtr,size_t blkCnt,size_t byteCntAdd) |
| 718 |
{ |
| 719 |
enum |
| 720 |
{ |
| 721 |
WCNT = SKEIN_512_STATE_WORDS |
| 722 |
}; |
| 723 |
#undef RCNT |
| 724 |
#define RCNT (SKEIN_512_ROUNDS_TOTAL/8) |
| 725 |
|
| 726 |
#ifdef SKEIN_LOOP |
| 727 |
#define SKEIN_UNROLL_512 (((SKEIN_LOOP)/10)%10) |
| 728 |
#else |
| 729 |
#define SKEIN_UNROLL_512 (0) |
| 730 |
#endif |
| 731 |
|
| 732 |
#if SKEIN_UNROLL_512 |
| 733 |
#if (RCNT % SKEIN_UNROLL_512) |
| 734 |
#error "Invalid SKEIN_UNROLL_512" |
| 735 |
#endif |
| 736 |
size_t r; |
| 737 |
u64b_t kw[WCNT+4+RCNT*2]; |
| 738 |
#else |
| 739 |
u64b_t kw[WCNT+4]; |
| 740 |
#endif |
| 741 |
u64b_t X0,X1,X2,X3,X4,X5,X6,X7; |
| 742 |
u64b_t w [WCNT]; |
| 743 |
#ifdef SKEIN_DEBUG |
| 744 |
const u64b_t *Xptr[8]; |
| 745 |
Xptr[0] = &X0; Xptr[1] = &X1; Xptr[2] = &X2; Xptr[3] = &X3; |
| 746 |
Xptr[4] = &X4; Xptr[5] = &X5; Xptr[6] = &X6; Xptr[7] = &X7; |
| 747 |
#endif |
| 748 |
|
| 749 |
Skein_assert(blkCnt != 0); |
| 750 |
ts[0] = ctx->h.T[0]; |
| 751 |
ts[1] = ctx->h.T[1]; |
| 752 |
do { |
| 753 |
|
| 754 |
ts[0] += byteCntAdd; |
| 755 |
|
| 756 |
|
| 757 |
ks[0] = ctx->X[0]; |
| 758 |
ks[1] = ctx->X[1]; |
| 759 |
ks[2] = ctx->X[2]; |
| 760 |
ks[3] = ctx->X[3]; |
| 761 |
ks[4] = ctx->X[4]; |
| 762 |
ks[5] = ctx->X[5]; |
| 763 |
ks[6] = ctx->X[6]; |
| 764 |
ks[7] = ctx->X[7]; |
| 765 |
ks[8] = ks[0] ^ ks[1] ^ ks[2] ^ ks[3] ^ |
| 766 |
ks[4] ^ ks[5] ^ ks[6] ^ ks[7] ^ SKEIN_KS_PARITY; |
| 767 |
|
| 768 |
ts[2] = ts[0] ^ ts[1]; |
| 769 |
|
| 770 |
Skein_Get64_LSB_First(w,blkPtr,WCNT); |
| 771 |
DebugSaveTweak(ctx); |
| 772 |
Skein_Show_Block(BLK_BITS,&ctx->h,ctx->X,blkPtr,w,ks,ts); |
| 773 |
|
| 774 |
X0 = w[0] + ks[0]; |
| 775 |
X1 = w[1] + ks[1]; |
| 776 |
X2 = w[2] + ks[2]; |
| 777 |
X3 = w[3] + ks[3]; |
| 778 |
X4 = w[4] + ks[4]; |
| 779 |
X5 = w[5] + ks[5] + ts[0]; |
| 780 |
X6 = w[6] + ks[6] + ts[1]; |
| 781 |
X7 = w[7] + ks[7]; |
| 782 |
|
| 783 |
blkPtr += SKEIN_512_BLOCK_BYTES; |
| 784 |
|
| 785 |
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INITIAL,Xptr); |
| 786 |
|
| 787 |
#define Round512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \ |
| 788 |
X##p0 += X##p1; X##p1 = RotL_64(X##p1,ROT##_0); X##p1 ^= X##p0; \ |
| 789 |
X##p2 += X##p3; X##p3 = RotL_64(X##p3,ROT##_1); X##p3 ^= X##p2; \ |
| 790 |
X##p4 += X##p5; X##p5 = RotL_64(X##p5,ROT##_2); X##p5 ^= X##p4; \ |
| 791 |
X##p6 += X##p7; X##p7 = RotL_64(X##p7,ROT##_3); X##p7 ^= X##p6; \ |
| 792 |
|
| 793 |
#if SKEIN_UNROLL_512 == 0 |
| 794 |
#define R512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \ |
| 795 |
Round512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \ |
| 796 |
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,rNum,Xptr); |
| 797 |
|
| 798 |
#define I512(R) \ |
| 799 |
X0 += ks[((R)+1) % 9]; \ |
| 800 |
X1 += ks[((R)+2) % 9]; \ |
| 801 |
X2 += ks[((R)+3) % 9]; \ |
| 802 |
X3 += ks[((R)+4) % 9]; \ |
| 803 |
X4 += ks[((R)+5) % 9]; \ |
| 804 |
X5 += ks[((R)+6) % 9] + ts[((R)+1) % 3]; \ |
| 805 |
X6 += ks[((R)+7) % 9] + ts[((R)+2) % 3]; \ |
| 806 |
X7 += ks[((R)+8) % 9] + (R)+1; \ |
| 807 |
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr); |
| 808 |
#else |
| 809 |
#define R512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \ |
| 810 |
Round512(p0,p1,p2,p3,p4,p5,p6,p7,ROT,rNum) \ |
| 811 |
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,4*(r-1)+rNum,Xptr); |
| 812 |
|
| 813 |
#define I512(R) \ |
| 814 |
X0 += ks[r+(R)+0]; \ |
| 815 |
X1 += ks[r+(R)+1]; \ |
| 816 |
X2 += ks[r+(R)+2]; \ |
| 817 |
X3 += ks[r+(R)+3]; \ |
| 818 |
X4 += ks[r+(R)+4]; \ |
| 819 |
X5 += ks[r+(R)+5] + ts[r+(R)+0]; \ |
| 820 |
X6 += ks[r+(R)+6] + ts[r+(R)+1]; \ |
| 821 |
X7 += ks[r+(R)+7] + r+(R) ; \ |
| 822 |
ks[r + (R)+8] = ks[r+(R)-1]; \ |
| 823 |
ts[r + (R)+2] = ts[r+(R)-1]; \ |
| 824 |
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr); |
| 825 |
|
| 826 |
for (r=1;r < 2*RCNT;r+=2*SKEIN_UNROLL_512) |
| 827 |
#endif |
| 828 |
{ |
| 829 |
#define R512_8_rounds(R) \ |
| 830 |
R512(0,1,2,3,4,5,6,7,R_512_0,8*(R)+ 1); \ |
| 831 |
R512(2,1,4,7,6,5,0,3,R_512_1,8*(R)+ 2); \ |
| 832 |
R512(4,1,6,3,0,5,2,7,R_512_2,8*(R)+ 3); \ |
| 833 |
R512(6,1,0,7,2,5,4,3,R_512_3,8*(R)+ 4); \ |
| 834 |
I512(2*(R)); \ |
| 835 |
R512(0,1,2,3,4,5,6,7,R_512_4,8*(R)+ 5); \ |
| 836 |
R512(2,1,4,7,6,5,0,3,R_512_5,8*(R)+ 6); \ |
| 837 |
R512(4,1,6,3,0,5,2,7,R_512_6,8*(R)+ 7); \ |
| 838 |
R512(6,1,0,7,2,5,4,3,R_512_7,8*(R)+ 8); \ |
| 839 |
I512(2*(R)+1); |
| 840 |
|
| 841 |
R512_8_rounds( 0); |
| 842 |
|
| 843 |
#define R512_Unroll_R(NN) ((SKEIN_UNROLL_512 == 0 && SKEIN_512_ROUNDS_TOTAL/8 > (NN)) || (SKEIN_UNROLL_512 > (NN))) |
| 844 |
|
| 845 |
#if R512_Unroll_R( 1) |
| 846 |
R512_8_rounds( 1); |
| 847 |
#endif |
| 848 |
#if R512_Unroll_R( 2) |
| 849 |
R512_8_rounds( 2); |
| 850 |
#endif |
| 851 |
#if R512_Unroll_R( 3) |
| 852 |
R512_8_rounds( 3); |
| 853 |
#endif |
| 854 |
#if R512_Unroll_R( 4) |
| 855 |
R512_8_rounds( 4); |
| 856 |
#endif |
| 857 |
#if R512_Unroll_R( 5) |
| 858 |
R512_8_rounds( 5); |
| 859 |
#endif |
| 860 |
#if R512_Unroll_R( 6) |
| 861 |
R512_8_rounds( 6); |
| 862 |
#endif |
| 863 |
#if R512_Unroll_R( 7) |
| 864 |
R512_8_rounds( 7); |
| 865 |
#endif |
| 866 |
#if R512_Unroll_R( 8) |
| 867 |
R512_8_rounds( 8); |
| 868 |
#endif |
| 869 |
#if R512_Unroll_R( 9) |
| 870 |
R512_8_rounds( 9); |
| 871 |
#endif |
| 872 |
#if R512_Unroll_R(10) |
| 873 |
R512_8_rounds(10); |
| 874 |
#endif |
| 875 |
#if R512_Unroll_R(11) |
| 876 |
R512_8_rounds(11); |
| 877 |
#endif |
| 878 |
#if R512_Unroll_R(12) |
| 879 |
R512_8_rounds(12); |
| 880 |
#endif |
| 881 |
#if R512_Unroll_R(13) |
| 882 |
R512_8_rounds(13); |
| 883 |
#endif |
| 884 |
#if R512_Unroll_R(14) |
| 885 |
R512_8_rounds(14); |
| 886 |
#endif |
| 887 |
#if (SKEIN_UNROLL_512 > 14) |
| 888 |
#error "need more unrolling in Skein_512_Process_Block" |
| 889 |
#endif |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
ctx->X[0] = X0 ^ w[0]; |
| 894 |
ctx->X[1] = X1 ^ w[1]; |
| 895 |
ctx->X[2] = X2 ^ w[2]; |
| 896 |
ctx->X[3] = X3 ^ w[3]; |
| 897 |
ctx->X[4] = X4 ^ w[4]; |
| 898 |
ctx->X[5] = X5 ^ w[5]; |
| 899 |
ctx->X[6] = X6 ^ w[6]; |
| 900 |
ctx->X[7] = X7 ^ w[7]; |
| 901 |
Skein_Show_Round(BLK_BITS,&ctx->h,SKEIN_RND_FEED_FWD,ctx->X); |
| 902 |
|
| 903 |
ts[1] &= ~SKEIN_T1_FLAG_FIRST; |
| 904 |
} |
| 905 |
while (--blkCnt); |
| 906 |
ctx->h.T[0] = ts[0]; |
| 907 |
ctx->h.T[1] = ts[1]; |
| 908 |
} |
| 909 |
|
| 910 |
#if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF) |
| 911 |
static size_t Skein_512_Process_Block_CodeSize(void) |
| 912 |
{ |
| 913 |
return ((u08b_t *) Skein_512_Process_Block_CodeSize) - |
| 914 |
((u08b_t *) Skein_512_Process_Block); |
| 915 |
} |
| 916 |
static uint_t Skein_512_Unroll_Cnt(void) |
| 917 |
{ |
| 918 |
return SKEIN_UNROLL_512; |
| 919 |
} |
| 920 |
#endif |
| 921 |
#endif |
| 922 |
|
| 923 |
|
| 924 |
#if !(SKEIN_USE_ASM & 1024) |
| 925 |
static void Skein1024_Process_Block(Skein1024_Ctxt_t *ctx,const u08b_t *blkPtr,size_t blkCnt,size_t byteCntAdd) |
| 926 |
{ |
| 927 |
enum |
| 928 |
{ |
| 929 |
WCNT = SKEIN1024_STATE_WORDS |
| 930 |
}; |
| 931 |
#undef RCNT |
| 932 |
#define RCNT (SKEIN1024_ROUNDS_TOTAL/8) |
| 933 |
|
| 934 |
#ifdef SKEIN_LOOP |
| 935 |
#define SKEIN_UNROLL_1024 ((SKEIN_LOOP)%10) |
| 936 |
#else |
| 937 |
#define SKEIN_UNROLL_1024 (0) |
| 938 |
#endif |
| 939 |
|
| 940 |
#if (SKEIN_UNROLL_1024 != 0) |
| 941 |
#if (RCNT % SKEIN_UNROLL_1024) |
| 942 |
#error "Invalid SKEIN_UNROLL_1024" |
| 943 |
#endif |
| 944 |
size_t r; |
| 945 |
u64b_t kw[WCNT+4+RCNT*2]; |
| 946 |
#else |
| 947 |
u64b_t kw[WCNT+4]; |
| 948 |
#endif |
| 949 |
|
| 950 |
u64b_t X00,X01,X02,X03,X04,X05,X06,X07, |
| 951 |
X08,X09,X10,X11,X12,X13,X14,X15; |
| 952 |
u64b_t w [WCNT]; |
| 953 |
#ifdef SKEIN_DEBUG |
| 954 |
const u64b_t *Xptr[16]; |
| 955 |
Xptr[ 0] = &X00; Xptr[ 1] = &X01; Xptr[ 2] = &X02; Xptr[ 3] = &X03; |
| 956 |
Xptr[ 4] = &X04; Xptr[ 5] = &X05; Xptr[ 6] = &X06; Xptr[ 7] = &X07; |
| 957 |
Xptr[ 8] = &X08; Xptr[ 9] = &X09; Xptr[10] = &X10; Xptr[11] = &X11; |
| 958 |
Xptr[12] = &X12; Xptr[13] = &X13; Xptr[14] = &X14; Xptr[15] = &X15; |
| 959 |
#endif |
| 960 |
|
| 961 |
Skein_assert(blkCnt != 0); |
| 962 |
ts[0] = ctx->h.T[0]; |
| 963 |
ts[1] = ctx->h.T[1]; |
| 964 |
do { |
| 965 |
|
| 966 |
ts[0] += byteCntAdd; |
| 967 |
|
| 968 |
|
| 969 |
ks[ 0] = ctx->X[ 0]; |
| 970 |
ks[ 1] = ctx->X[ 1]; |
| 971 |
ks[ 2] = ctx->X[ 2]; |
| 972 |
ks[ 3] = ctx->X[ 3]; |
| 973 |
ks[ 4] = ctx->X[ 4]; |
| 974 |
ks[ 5] = ctx->X[ 5]; |
| 975 |
ks[ 6] = ctx->X[ 6]; |
| 976 |
ks[ 7] = ctx->X[ 7]; |
| 977 |
ks[ 8] = ctx->X[ 8]; |
| 978 |
ks[ 9] = ctx->X[ 9]; |
| 979 |
ks[10] = ctx->X[10]; |
| 980 |
ks[11] = ctx->X[11]; |
| 981 |
ks[12] = ctx->X[12]; |
| 982 |
ks[13] = ctx->X[13]; |
| 983 |
ks[14] = ctx->X[14]; |
| 984 |
ks[15] = ctx->X[15]; |
| 985 |
ks[16] = ks[ 0] ^ ks[ 1] ^ ks[ 2] ^ ks[ 3] ^ |
| 986 |
ks[ 4] ^ ks[ 5] ^ ks[ 6] ^ ks[ 7] ^ |
| 987 |
ks[ 8] ^ ks[ 9] ^ ks[10] ^ ks[11] ^ |
| 988 |
ks[12] ^ ks[13] ^ ks[14] ^ ks[15] ^ SKEIN_KS_PARITY; |
| 989 |
|
| 990 |
ts[2] = ts[0] ^ ts[1]; |
| 991 |
|
| 992 |
Skein_Get64_LSB_First(w,blkPtr,WCNT); |
| 993 |
DebugSaveTweak(ctx); |
| 994 |
Skein_Show_Block(BLK_BITS,&ctx->h,ctx->X,blkPtr,w,ks,ts); |
| 995 |
|
| 996 |
X00 = w[ 0] + ks[ 0]; |
| 997 |
X01 = w[ 1] + ks[ 1]; |
| 998 |
X02 = w[ 2] + ks[ 2]; |
| 999 |
X03 = w[ 3] + ks[ 3]; |
| 1000 |
X04 = w[ 4] + ks[ 4]; |
| 1001 |
X05 = w[ 5] + ks[ 5]; |
| 1002 |
X06 = w[ 6] + ks[ 6]; |
| 1003 |
X07 = w[ 7] + ks[ 7]; |
| 1004 |
X08 = w[ 8] + ks[ 8]; |
| 1005 |
X09 = w[ 9] + ks[ 9]; |
| 1006 |
X10 = w[10] + ks[10]; |
| 1007 |
X11 = w[11] + ks[11]; |
| 1008 |
X12 = w[12] + ks[12]; |
| 1009 |
X13 = w[13] + ks[13] + ts[0]; |
| 1010 |
X14 = w[14] + ks[14] + ts[1]; |
| 1011 |
X15 = w[15] + ks[15]; |
| 1012 |
|
| 1013 |
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INITIAL,Xptr); |
| 1014 |
|
| 1015 |
#define Round1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rNum) \ |
| 1016 |
X##p0 += X##p1; X##p1 = RotL_64(X##p1,ROT##_0); X##p1 ^= X##p0; \ |
| 1017 |
X##p2 += X##p3; X##p3 = RotL_64(X##p3,ROT##_1); X##p3 ^= X##p2; \ |
| 1018 |
X##p4 += X##p5; X##p5 = RotL_64(X##p5,ROT##_2); X##p5 ^= X##p4; \ |
| 1019 |
X##p6 += X##p7; X##p7 = RotL_64(X##p7,ROT##_3); X##p7 ^= X##p6; \ |
| 1020 |
X##p8 += X##p9; X##p9 = RotL_64(X##p9,ROT##_4); X##p9 ^= X##p8; \ |
| 1021 |
X##pA += X##pB; X##pB = RotL_64(X##pB,ROT##_5); X##pB ^= X##pA; \ |
| 1022 |
X##pC += X##pD; X##pD = RotL_64(X##pD,ROT##_6); X##pD ^= X##pC; \ |
| 1023 |
X##pE += X##pF; X##pF = RotL_64(X##pF,ROT##_7); X##pF ^= X##pE; \ |
| 1024 |
|
| 1025 |
#if SKEIN_UNROLL_1024 == 0 |
| 1026 |
#define R1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rn) \ |
| 1027 |
Round1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rn) \ |
| 1028 |
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,rn,Xptr); |
| 1029 |
|
| 1030 |
#define I1024(R) \ |
| 1031 |
X00 += ks[((R)+ 1) % 17]; \ |
| 1032 |
X01 += ks[((R)+ 2) % 17]; \ |
| 1033 |
X02 += ks[((R)+ 3) % 17]; \ |
| 1034 |
X03 += ks[((R)+ 4) % 17]; \ |
| 1035 |
X04 += ks[((R)+ 5) % 17]; \ |
| 1036 |
X05 += ks[((R)+ 6) % 17]; \ |
| 1037 |
X06 += ks[((R)+ 7) % 17]; \ |
| 1038 |
X07 += ks[((R)+ 8) % 17]; \ |
| 1039 |
X08 += ks[((R)+ 9) % 17]; \ |
| 1040 |
X09 += ks[((R)+10) % 17]; \ |
| 1041 |
X10 += ks[((R)+11) % 17]; \ |
| 1042 |
X11 += ks[((R)+12) % 17]; \ |
| 1043 |
X12 += ks[((R)+13) % 17]; \ |
| 1044 |
X13 += ks[((R)+14) % 17] + ts[((R)+1) % 3]; \ |
| 1045 |
X14 += ks[((R)+15) % 17] + ts[((R)+2) % 3]; \ |
| 1046 |
X15 += ks[((R)+16) % 17] + (R)+1; \ |
| 1047 |
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr); |
| 1048 |
#else |
| 1049 |
#define R1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rn) \ |
| 1050 |
Round1024(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,pA,pB,pC,pD,pE,pF,ROT,rn) \ |
| 1051 |
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,4*(r-1)+rn,Xptr); |
| 1052 |
|
| 1053 |
#define I1024(R) \ |
| 1054 |
X00 += ks[r+(R)+ 0]; \ |
| 1055 |
X01 += ks[r+(R)+ 1]; \ |
| 1056 |
X02 += ks[r+(R)+ 2]; \ |
| 1057 |
X03 += ks[r+(R)+ 3]; \ |
| 1058 |
X04 += ks[r+(R)+ 4]; \ |
| 1059 |
X05 += ks[r+(R)+ 5]; \ |
| 1060 |
X06 += ks[r+(R)+ 6]; \ |
| 1061 |
X07 += ks[r+(R)+ 7]; \ |
| 1062 |
X08 += ks[r+(R)+ 8]; \ |
| 1063 |
X09 += ks[r+(R)+ 9]; \ |
| 1064 |
X10 += ks[r+(R)+10]; \ |
| 1065 |
X11 += ks[r+(R)+11]; \ |
| 1066 |
X12 += ks[r+(R)+12]; \ |
| 1067 |
X13 += ks[r+(R)+13] + ts[r+(R)+0]; \ |
| 1068 |
X14 += ks[r+(R)+14] + ts[r+(R)+1]; \ |
| 1069 |
X15 += ks[r+(R)+15] + r+(R) ; \ |
| 1070 |
ks[r + (R)+16] = ks[r+(R)-1]; \ |
| 1071 |
ts[r + (R)+ 2] = ts[r+(R)-1]; \ |
| 1072 |
Skein_Show_R_Ptr(BLK_BITS,&ctx->h,SKEIN_RND_KEY_INJECT,Xptr); |
| 1073 |
|
| 1074 |
for (r=1;r <= 2*RCNT;r+=2*SKEIN_UNROLL_1024) |
| 1075 |
#endif |
| 1076 |
{ |
| 1077 |
#define R1024_8_rounds(R) \ |
| 1078 |
R1024(00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,R1024_0,8*(R) + 1); \ |
| 1079 |
R1024(00,09,02,13,06,11,04,15,10,07,12,03,14,05,08,01,R1024_1,8*(R) + 2); \ |
| 1080 |
R1024(00,07,02,05,04,03,06,01,12,15,14,13,08,11,10,09,R1024_2,8*(R) + 3); \ |
| 1081 |
R1024(00,15,02,11,06,13,04,09,14,01,08,05,10,03,12,07,R1024_3,8*(R) + 4); \ |
| 1082 |
I1024(2*(R)); \ |
| 1083 |
R1024(00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,R1024_4,8*(R) + 5); \ |
| 1084 |
R1024(00,09,02,13,06,11,04,15,10,07,12,03,14,05,08,01,R1024_5,8*(R) + 6); \ |
| 1085 |
R1024(00,07,02,05,04,03,06,01,12,15,14,13,08,11,10,09,R1024_6,8*(R) + 7); \ |
| 1086 |
R1024(00,15,02,11,06,13,04,09,14,01,08,05,10,03,12,07,R1024_7,8*(R) + 8); \ |
| 1087 |
I1024(2*(R)+1); |
| 1088 |
|
| 1089 |
R1024_8_rounds( 0); |
| 1090 |
|
| 1091 |
#define R1024_Unroll_R(NN) ((SKEIN_UNROLL_1024 == 0 && SKEIN1024_ROUNDS_TOTAL/8 > (NN)) || (SKEIN_UNROLL_1024 > (NN))) |
| 1092 |
|
| 1093 |
#if R1024_Unroll_R( 1) |
| 1094 |
R1024_8_rounds( 1); |
| 1095 |
#endif |
| 1096 |
#if R1024_Unroll_R( 2) |
| 1097 |
R1024_8_rounds( 2); |
| 1098 |
#endif |
| 1099 |
#if R1024_Unroll_R( 3) |
| 1100 |
R1024_8_rounds( 3); |
| 1101 |
#endif |
| 1102 |
#if R1024_Unroll_R( 4) |
| 1103 |
R1024_8_rounds( 4); |
| 1104 |
#endif |
| 1105 |
#if R1024_Unroll_R( 5) |
| 1106 |
R1024_8_rounds( 5); |
| 1107 |
#endif |
| 1108 |
#if R1024_Unroll_R( 6) |
| 1109 |
R1024_8_rounds( 6); |
| 1110 |
#endif |
| 1111 |
#if R1024_Unroll_R( 7) |
| 1112 |
R1024_8_rounds( 7); |
| 1113 |
#endif |
| 1114 |
#if R1024_Unroll_R( 8) |
| 1115 |
R1024_8_rounds( 8); |
| 1116 |
#endif |
| 1117 |
#if R1024_Unroll_R( 9) |
| 1118 |
R1024_8_rounds( 9); |
| 1119 |
#endif |
| 1120 |
#if R1024_Unroll_R(10) |
| 1121 |
R1024_8_rounds(10); |
| 1122 |
#endif |
| 1123 |
#if R1024_Unroll_R(11) |
| 1124 |
R1024_8_rounds(11); |
| 1125 |
#endif |
| 1126 |
#if R1024_Unroll_R(12) |
| 1127 |
R1024_8_rounds(12); |
| 1128 |
#endif |
| 1129 |
#if R1024_Unroll_R(13) |
| 1130 |
R1024_8_rounds(13); |
| 1131 |
#endif |
| 1132 |
#if R1024_Unroll_R(14) |
| 1133 |
R1024_8_rounds(14); |
| 1134 |
#endif |
| 1135 |
#if (SKEIN_UNROLL_1024 > 14) |
| 1136 |
#error "need more unrolling in Skein_1024_Process_Block" |
| 1137 |
#endif |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
ctx->X[ 0] = X00 ^ w[ 0]; |
| 1142 |
ctx->X[ 1] = X01 ^ w[ 1]; |
| 1143 |
ctx->X[ 2] = X02 ^ w[ 2]; |
| 1144 |
ctx->X[ 3] = X03 ^ w[ 3]; |
| 1145 |
ctx->X[ 4] = X04 ^ w[ 4]; |
| 1146 |
ctx->X[ 5] = X05 ^ w[ 5]; |
| 1147 |
ctx->X[ 6] = X06 ^ w[ 6]; |
| 1148 |
ctx->X[ 7] = X07 ^ w[ 7]; |
| 1149 |
ctx->X[ 8] = X08 ^ w[ 8]; |
| 1150 |
ctx->X[ 9] = X09 ^ w[ 9]; |
| 1151 |
ctx->X[10] = X10 ^ w[10]; |
| 1152 |
ctx->X[11] = X11 ^ w[11]; |
| 1153 |
ctx->X[12] = X12 ^ w[12]; |
| 1154 |
ctx->X[13] = X13 ^ w[13]; |
| 1155 |
ctx->X[14] = X14 ^ w[14]; |
| 1156 |
ctx->X[15] = X15 ^ w[15]; |
| 1157 |
|
| 1158 |
Skein_Show_Round(BLK_BITS,&ctx->h,SKEIN_RND_FEED_FWD,ctx->X); |
| 1159 |
|
| 1160 |
ts[1] &= ~SKEIN_T1_FLAG_FIRST; |
| 1161 |
blkPtr += SKEIN1024_BLOCK_BYTES; |
| 1162 |
} |
| 1163 |
while (--blkCnt); |
| 1164 |
ctx->h.T[0] = ts[0]; |
| 1165 |
ctx->h.T[1] = ts[1]; |
| 1166 |
} |
| 1167 |
|
| 1168 |
#if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF) |
| 1169 |
static size_t Skein1024_Process_Block_CodeSize(void) |
| 1170 |
{ |
| 1171 |
return ((u08b_t *) Skein1024_Process_Block_CodeSize) - |
| 1172 |
((u08b_t *) Skein1024_Process_Block); |
| 1173 |
} |
| 1174 |
static uint_t Skein1024_Unroll_Cnt(void) |
| 1175 |
{ |
| 1176 |
return SKEIN_UNROLL_1024; |
| 1177 |
} |
| 1178 |
#endif |
| 1179 |
#endif |
| 1180 |
|
| 1181 |
|
| 1182 |
#if 0 |
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
static int Skein_256_Init(Skein_256_Ctxt_t *ctx, size_t hashBitLen) |
| 1190 |
{ |
| 1191 |
union |
| 1192 |
{ |
| 1193 |
u08b_t b[SKEIN_256_STATE_BYTES]; |
| 1194 |
u64b_t w[SKEIN_256_STATE_WORDS]; |
| 1195 |
} cfg; |
| 1196 |
|
| 1197 |
Skein_Assert(hashBitLen > 0,SKEIN_BAD_HASHLEN); |
| 1198 |
ctx->h.hashBitLen = hashBitLen; |
| 1199 |
|
| 1200 |
switch (hashBitLen) |
| 1201 |
{ |
| 1202 |
#ifndef SKEIN_NO_PRECOMP |
| 1203 |
case 256: memcpy(ctx->X,SKEIN_256_IV_256,sizeof(ctx->X)); break; |
| 1204 |
case 224: memcpy(ctx->X,SKEIN_256_IV_224,sizeof(ctx->X)); break; |
| 1205 |
case 160: memcpy(ctx->X,SKEIN_256_IV_160,sizeof(ctx->X)); break; |
| 1206 |
case 128: memcpy(ctx->X,SKEIN_256_IV_128,sizeof(ctx->X)); break; |
| 1207 |
#endif |
| 1208 |
default: |
| 1209 |
|
| 1210 |
|
| 1211 |
Skein_Start_New_Type(ctx,CFG_FINAL); |
| 1212 |
|
| 1213 |
cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER); |
| 1214 |
cfg.w[1] = Skein_Swap64(hashBitLen); |
| 1215 |
cfg.w[2] = Skein_Swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL); |
| 1216 |
memset(&cfg.w[3],0,sizeof(cfg) - 3*sizeof(cfg.w[0])); |
| 1217 |
|
| 1218 |
|
| 1219 |
memset(ctx->X,0,sizeof(ctx->X)); |
| 1220 |
Skein_256_Process_Block(ctx,cfg.b,1,SKEIN_CFG_STR_LEN); |
| 1221 |
break; |
| 1222 |
} |
| 1223 |
|
| 1224 |
|
| 1225 |
Skein_Start_New_Type(ctx,MSG); |
| 1226 |
|
| 1227 |
return SKEIN_SUCCESS; |
| 1228 |
} |
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
static int Skein_256_InitExt(Skein_256_Ctxt_t *ctx,size_t hashBitLen,u64b_t treeInfo, const u08b_t *key, size_t keyBytes) |
| 1234 |
{ |
| 1235 |
union |
| 1236 |
{ |
| 1237 |
u08b_t b[SKEIN_256_STATE_BYTES]; |
| 1238 |
u64b_t w[SKEIN_256_STATE_WORDS]; |
| 1239 |
} cfg; |
| 1240 |
|
| 1241 |
Skein_Assert(hashBitLen > 0,SKEIN_BAD_HASHLEN); |
| 1242 |
Skein_Assert(keyBytes == 0 || key != NULL,SKEIN_FAIL); |
| 1243 |
|
| 1244 |
|
| 1245 |
if (keyBytes == 0) |
| 1246 |
{ |
| 1247 |
memset(ctx->X,0,sizeof(ctx->X)); |
| 1248 |
} |
| 1249 |
else |
| 1250 |
{ |
| 1251 |
Skein_assert(sizeof(cfg.b) >= sizeof(ctx->X)); |
| 1252 |
|
| 1253 |
ctx->h.hashBitLen=8*sizeof(ctx->X); |
| 1254 |
Skein_Start_New_Type(ctx,KEY); |
| 1255 |
memset(ctx->X,0,sizeof(ctx->X)); |
| 1256 |
Skein_256_Update(ctx,key,keyBytes); |
| 1257 |
Skein_256_Final_Pad(ctx,cfg.b); |
| 1258 |
memcpy(ctx->X,cfg.b,sizeof(cfg.b)); |
| 1259 |
#if SKEIN_NEED_SWAP |
| 1260 |
{ |
| 1261 |
uint_t i; |
| 1262 |
for (i=0;i<SKEIN_256_STATE_WORDS;i++) |
| 1263 |
ctx->X[i] = Skein_Swap64(ctx->X[i]); |
| 1264 |
} |
| 1265 |
#endif |
| 1266 |
} |
| 1267 |
|
| 1268 |
ctx->h.hashBitLen = hashBitLen; |
| 1269 |
Skein_Start_New_Type(ctx,CFG_FINAL); |
| 1270 |
|
| 1271 |
memset(&cfg.w,0,sizeof(cfg.w)); |
| 1272 |
cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER); |
| 1273 |
cfg.w[1] = Skein_Swap64(hashBitLen); |
| 1274 |
cfg.w[2] = Skein_Swap64(treeInfo); |
| 1275 |
|
| 1276 |
Skein_Show_Key(256,&ctx->h,key,keyBytes); |
| 1277 |
|
| 1278 |
|
| 1279 |
Skein_256_Process_Block(ctx,cfg.b,1,SKEIN_CFG_STR_LEN); |
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
ctx->h.bCnt = 0; |
| 1284 |
Skein_Start_New_Type(ctx,MSG); |
| 1285 |
|
| 1286 |
return SKEIN_SUCCESS; |
| 1287 |
} |
| 1288 |
#endif |
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
static int Skein_256_Update(Skein_256_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt) |
| 1293 |
{ |
| 1294 |
size_t n; |
| 1295 |
|
| 1296 |
Skein_Assert(ctx->h.bCnt <= SKEIN_256_BLOCK_BYTES,SKEIN_FAIL); |
| 1297 |
|
| 1298 |
|
| 1299 |
if (msgByteCnt + ctx->h.bCnt > SKEIN_256_BLOCK_BYTES) |
| 1300 |
{ |
| 1301 |
if (ctx->h.bCnt) |
| 1302 |
{ |
| 1303 |
n = SKEIN_256_BLOCK_BYTES - ctx->h.bCnt; |
| 1304 |
if (n) |
| 1305 |
{ |
| 1306 |
Skein_assert(n < msgByteCnt); |
| 1307 |
memcpy(&ctx->b[ctx->h.bCnt],msg,n); |
| 1308 |
msgByteCnt -= n; |
| 1309 |
msg += n; |
| 1310 |
ctx->h.bCnt += n; |
| 1311 |
} |
| 1312 |
Skein_assert(ctx->h.bCnt == SKEIN_256_BLOCK_BYTES); |
| 1313 |
Skein_256_Process_Block(ctx,ctx->b,1,SKEIN_256_BLOCK_BYTES); |
| 1314 |
ctx->h.bCnt = 0; |
| 1315 |
} |
| 1316 |
|
| 1317 |
if (msgByteCnt > SKEIN_256_BLOCK_BYTES) |
| 1318 |
{ |
| 1319 |
n = (msgByteCnt-1) / SKEIN_256_BLOCK_BYTES; |
| 1320 |
Skein_256_Process_Block(ctx,msg,n,SKEIN_256_BLOCK_BYTES); |
| 1321 |
msgByteCnt -= n * SKEIN_256_BLOCK_BYTES; |
| 1322 |
msg += n * SKEIN_256_BLOCK_BYTES; |
| 1323 |
} |
| 1324 |
Skein_assert(ctx->h.bCnt == 0); |
| 1325 |
} |
| 1326 |
|
| 1327 |
|
| 1328 |
if (msgByteCnt) |
| 1329 |
{ |
| 1330 |
Skein_assert(msgByteCnt + ctx->h.bCnt <= SKEIN_256_BLOCK_BYTES); |
| 1331 |
memcpy(&ctx->b[ctx->h.bCnt],msg,msgByteCnt); |
| 1332 |
ctx->h.bCnt += msgByteCnt; |
| 1333 |
} |
| 1334 |
|
| 1335 |
return SKEIN_SUCCESS; |
| 1336 |
} |
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
static int Skein_256_Final(Skein_256_Ctxt_t *ctx, u08b_t *hashVal) |
| 1341 |
{ |
| 1342 |
size_t i,n,byteCnt; |
| 1343 |
u64b_t X[SKEIN_256_STATE_WORDS]; |
| 1344 |
Skein_Assert(ctx->h.bCnt <= SKEIN_256_BLOCK_BYTES,SKEIN_FAIL); |
| 1345 |
|
| 1346 |
ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; |
| 1347 |
if (ctx->h.bCnt < SKEIN_256_BLOCK_BYTES) |
| 1348 |
memset(&ctx->b[ctx->h.bCnt],0,SKEIN_256_BLOCK_BYTES - ctx->h.bCnt); |
| 1349 |
|
| 1350 |
Skein_256_Process_Block(ctx,ctx->b,1,ctx->h.bCnt); |
| 1351 |
|
| 1352 |
|
| 1353 |
byteCnt = (ctx->h.hashBitLen + 7) >> 3; |
| 1354 |
|
| 1355 |
|
| 1356 |
memset(ctx->b,0,sizeof(ctx->b)); |
| 1357 |
memcpy(X,ctx->X,sizeof(X)); |
| 1358 |
for (i=0;i*SKEIN_256_BLOCK_BYTES < byteCnt;i++) |
| 1359 |
{ |
| 1360 |
((u64b_t *)ctx->b)[0]= Skein_Swap64((u64b_t) i); |
| 1361 |
Skein_Start_New_Type(ctx,OUT_FINAL); |
| 1362 |
Skein_256_Process_Block(ctx,ctx->b,1,sizeof(u64b_t)); |
| 1363 |
n = byteCnt - i*SKEIN_256_BLOCK_BYTES; |
| 1364 |
if (n >= SKEIN_256_BLOCK_BYTES) |
| 1365 |
n = SKEIN_256_BLOCK_BYTES; |
| 1366 |
Skein_Put64_LSB_First(hashVal+i*SKEIN_256_BLOCK_BYTES,ctx->X,n); |
| 1367 |
Skein_Show_Final(256,&ctx->h,n,hashVal+i*SKEIN_256_BLOCK_BYTES); |
| 1368 |
memcpy(ctx->X,X,sizeof(X)); |
| 1369 |
} |
| 1370 |
return SKEIN_SUCCESS; |
| 1371 |
} |
| 1372 |
|
| 1373 |
#if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF) |
| 1374 |
static size_t Skein_256_API_CodeSize(void) |
| 1375 |
{ |
| 1376 |
return ((u08b_t *) Skein_256_API_CodeSize) - |
| 1377 |
((u08b_t *) Skein_256_Init); |
| 1378 |
} |
| 1379 |
#endif |
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
static int Skein_512_Init(Skein_512_Ctxt_t *ctx, size_t hashBitLen) |
| 1388 |
{ |
| 1389 |
union |
| 1390 |
{ |
| 1391 |
u08b_t b[SKEIN_512_STATE_BYTES]; |
| 1392 |
u64b_t w[SKEIN_512_STATE_WORDS]; |
| 1393 |
} cfg; |
| 1394 |
|
| 1395 |
Skein_Assert(hashBitLen > 0,SKEIN_BAD_HASHLEN); |
| 1396 |
ctx->h.hashBitLen = hashBitLen; |
| 1397 |
|
| 1398 |
switch (hashBitLen) |
| 1399 |
{ |
| 1400 |
#ifndef SKEIN_NO_PRECOMP |
| 1401 |
case 512: memcpy(ctx->X,SKEIN_512_IV_512,sizeof(ctx->X)); break; |
| 1402 |
case 384: memcpy(ctx->X,SKEIN_512_IV_384,sizeof(ctx->X)); break; |
| 1403 |
case 256: memcpy(ctx->X,SKEIN_512_IV_256,sizeof(ctx->X)); break; |
| 1404 |
case 224: memcpy(ctx->X,SKEIN_512_IV_224,sizeof(ctx->X)); break; |
| 1405 |
#endif |
| 1406 |
default: |
| 1407 |
|
| 1408 |
|
| 1409 |
Skein_Start_New_Type(ctx,CFG_FINAL); |
| 1410 |
|
| 1411 |
cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER); |
| 1412 |
cfg.w[1] = Skein_Swap64(hashBitLen); |
| 1413 |
cfg.w[2] = Skein_Swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL); |
| 1414 |
memset(&cfg.w[3],0,sizeof(cfg) - 3*sizeof(cfg.w[0])); |
| 1415 |
|
| 1416 |
|
| 1417 |
memset(ctx->X,0,sizeof(ctx->X)); |
| 1418 |
Skein_512_Process_Block(ctx,cfg.b,1,SKEIN_CFG_STR_LEN); |
| 1419 |
break; |
| 1420 |
} |
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
Skein_Start_New_Type(ctx,MSG); |
| 1425 |
|
| 1426 |
return SKEIN_SUCCESS; |
| 1427 |
} |
| 1428 |
|
| 1429 |
#if 0 |
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
static int Skein_512_InitExt(Skein_512_Ctxt_t *ctx,size_t hashBitLen,u64b_t treeInfo, const u08b_t *key, size_t keyBytes) |
| 1434 |
{ |
| 1435 |
union |
| 1436 |
{ |
| 1437 |
u08b_t b[SKEIN_512_STATE_BYTES]; |
| 1438 |
u64b_t w[SKEIN_512_STATE_WORDS]; |
| 1439 |
} cfg; |
| 1440 |
|
| 1441 |
Skein_Assert(hashBitLen > 0,SKEIN_BAD_HASHLEN); |
| 1442 |
Skein_Assert(keyBytes == 0 || key != NULL,SKEIN_FAIL); |
| 1443 |
|
| 1444 |
|
| 1445 |
if (keyBytes == 0) |
| 1446 |
{ |
| 1447 |
memset(ctx->X,0,sizeof(ctx->X)); |
| 1448 |
} |
| 1449 |
else |
| 1450 |
{ |
| 1451 |
Skein_assert(sizeof(cfg.b) >= sizeof(ctx->X)); |
| 1452 |
|
| 1453 |
ctx->h.hashBitLen=8*sizeof(ctx->X); |
| 1454 |
Skein_Start_New_Type(ctx,KEY); |
| 1455 |
memset(ctx->X,0,sizeof(ctx->X)); |
| 1456 |
Skein_512_Update(ctx,key,keyBytes); |
| 1457 |
Skein_512_Final_Pad(ctx,cfg.b); |
| 1458 |
memcpy(ctx->X,cfg.b,sizeof(cfg.b)); |
| 1459 |
#if SKEIN_NEED_SWAP |
| 1460 |
{ |
| 1461 |
uint_t i; |
| 1462 |
for (i=0;i<SKEIN_512_STATE_WORDS;i++) |
| 1463 |
ctx->X[i] = Skein_Swap64(ctx->X[i]); |
| 1464 |
} |
| 1465 |
#endif |
| 1466 |
} |
| 1467 |
|
| 1468 |
ctx->h.hashBitLen = hashBitLen; |
| 1469 |
Skein_Start_New_Type(ctx,CFG_FINAL); |
| 1470 |
|
| 1471 |
memset(&cfg.w,0,sizeof(cfg.w)); |
| 1472 |
cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER); |
| 1473 |
cfg.w[1] = Skein_Swap64(hashBitLen); |
| 1474 |
cfg.w[2] = Skein_Swap64(treeInfo); |
| 1475 |
|
| 1476 |
Skein_Show_Key(512,&ctx->h,key,keyBytes); |
| 1477 |
|
| 1478 |
|
| 1479 |
Skein_512_Process_Block(ctx,cfg.b,1,SKEIN_CFG_STR_LEN); |
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
ctx->h.bCnt = 0; |
| 1484 |
Skein_Start_New_Type(ctx,MSG); |
| 1485 |
|
| 1486 |
return SKEIN_SUCCESS; |
| 1487 |
} |
| 1488 |
#endif |
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
static int Skein_512_Update(Skein_512_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt) |
| 1493 |
{ |
| 1494 |
size_t n; |
| 1495 |
|
| 1496 |
Skein_Assert(ctx->h.bCnt <= SKEIN_512_BLOCK_BYTES,SKEIN_FAIL); |
| 1497 |
|
| 1498 |
|
| 1499 |
if (msgByteCnt + ctx->h.bCnt > SKEIN_512_BLOCK_BYTES) |
| 1500 |
{ |
| 1501 |
if (ctx->h.bCnt) |
| 1502 |
{ |
| 1503 |
n = SKEIN_512_BLOCK_BYTES - ctx->h.bCnt; |
| 1504 |
if (n) |
| 1505 |
{ |
| 1506 |
Skein_assert(n < msgByteCnt); |
| 1507 |
memcpy(&ctx->b[ctx->h.bCnt],msg,n); |
| 1508 |
msgByteCnt -= n; |
| 1509 |
msg += n; |
| 1510 |
ctx->h.bCnt += n; |
| 1511 |
} |
| 1512 |
Skein_assert(ctx->h.bCnt == SKEIN_512_BLOCK_BYTES); |
| 1513 |
Skein_512_Process_Block(ctx,ctx->b,1,SKEIN_512_BLOCK_BYTES); |
| 1514 |
ctx->h.bCnt = 0; |
| 1515 |
} |
| 1516 |
|
| 1517 |
if (msgByteCnt > SKEIN_512_BLOCK_BYTES) |
| 1518 |
{ |
| 1519 |
n = (msgByteCnt-1) / SKEIN_512_BLOCK_BYTES; |
| 1520 |
Skein_512_Process_Block(ctx,msg,n,SKEIN_512_BLOCK_BYTES); |
| 1521 |
msgByteCnt -= n * SKEIN_512_BLOCK_BYTES; |
| 1522 |
msg += n * SKEIN_512_BLOCK_BYTES; |
| 1523 |
} |
| 1524 |
Skein_assert(ctx->h.bCnt == 0); |
| 1525 |
} |
| 1526 |
|
| 1527 |
|
| 1528 |
if (msgByteCnt) |
| 1529 |
{ |
| 1530 |
Skein_assert(msgByteCnt + ctx->h.bCnt <= SKEIN_512_BLOCK_BYTES); |
| 1531 |
memcpy(&ctx->b[ctx->h.bCnt],msg,msgByteCnt); |
| 1532 |
ctx->h.bCnt += msgByteCnt; |
| 1533 |
} |
| 1534 |
|
| 1535 |
return SKEIN_SUCCESS; |
| 1536 |
} |
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
static int Skein_512_Final(Skein_512_Ctxt_t *ctx, u08b_t *hashVal) |
| 1541 |
{ |
| 1542 |
size_t i,n,byteCnt; |
| 1543 |
u64b_t X[SKEIN_512_STATE_WORDS]; |
| 1544 |
Skein_Assert(ctx->h.bCnt <= SKEIN_512_BLOCK_BYTES,SKEIN_FAIL); |
| 1545 |
|
| 1546 |
ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; |
| 1547 |
if (ctx->h.bCnt < SKEIN_512_BLOCK_BYTES) |
| 1548 |
memset(&ctx->b[ctx->h.bCnt],0,SKEIN_512_BLOCK_BYTES - ctx->h.bCnt); |
| 1549 |
|
| 1550 |
Skein_512_Process_Block(ctx,ctx->b,1,ctx->h.bCnt); |
| 1551 |
|
| 1552 |
|
| 1553 |
byteCnt = (ctx->h.hashBitLen + 7) >> 3; |
| 1554 |
|
| 1555 |
|
| 1556 |
memset(ctx->b,0,sizeof(ctx->b)); |
| 1557 |
memcpy(X,ctx->X,sizeof(X)); |
| 1558 |
for (i=0;i*SKEIN_512_BLOCK_BYTES < byteCnt;i++) |
| 1559 |
{ |
| 1560 |
((u64b_t *)ctx->b)[0]= Skein_Swap64((u64b_t) i); |
| 1561 |
Skein_Start_New_Type(ctx,OUT_FINAL); |
| 1562 |
Skein_512_Process_Block(ctx,ctx->b,1,sizeof(u64b_t)); |
| 1563 |
n = byteCnt - i*SKEIN_512_BLOCK_BYTES; |
| 1564 |
if (n >= SKEIN_512_BLOCK_BYTES) |
| 1565 |
n = SKEIN_512_BLOCK_BYTES; |
| 1566 |
Skein_Put64_LSB_First(hashVal+i*SKEIN_512_BLOCK_BYTES,ctx->X,n); |
| 1567 |
Skein_Show_Final(512,&ctx->h,n,hashVal+i*SKEIN_512_BLOCK_BYTES); |
| 1568 |
memcpy(ctx->X,X,sizeof(X)); |
| 1569 |
} |
| 1570 |
return SKEIN_SUCCESS; |
| 1571 |
} |
| 1572 |
|
| 1573 |
#if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF) |
| 1574 |
static size_t Skein_512_API_CodeSize(void) |
| 1575 |
{ |
| 1576 |
return ((u08b_t *) Skein_512_API_CodeSize) - |
| 1577 |
((u08b_t *) Skein_512_Init); |
| 1578 |
} |
| 1579 |
#endif |
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
static int Skein1024_Init(Skein1024_Ctxt_t *ctx, size_t hashBitLen) |
| 1587 |
{ |
| 1588 |
union |
| 1589 |
{ |
| 1590 |
u08b_t b[SKEIN1024_STATE_BYTES]; |
| 1591 |
u64b_t w[SKEIN1024_STATE_WORDS]; |
| 1592 |
} cfg; |
| 1593 |
|
| 1594 |
Skein_Assert(hashBitLen > 0,SKEIN_BAD_HASHLEN); |
| 1595 |
ctx->h.hashBitLen = hashBitLen; |
| 1596 |
|
| 1597 |
switch (hashBitLen) |
| 1598 |
{ |
| 1599 |
#ifndef SKEIN_NO_PRECOMP |
| 1600 |
case 512: memcpy(ctx->X,SKEIN1024_IV_512 ,sizeof(ctx->X)); break; |
| 1601 |
case 384: memcpy(ctx->X,SKEIN1024_IV_384 ,sizeof(ctx->X)); break; |
| 1602 |
case 1024: memcpy(ctx->X,SKEIN1024_IV_1024,sizeof(ctx->X)); break; |
| 1603 |
#endif |
| 1604 |
default: |
| 1605 |
|
| 1606 |
|
| 1607 |
Skein_Start_New_Type(ctx,CFG_FINAL); |
| 1608 |
|
| 1609 |
cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER); |
| 1610 |
cfg.w[1] = Skein_Swap64(hashBitLen); |
| 1611 |
cfg.w[2] = Skein_Swap64(SKEIN_CFG_TREE_INFO_SEQUENTIAL); |
| 1612 |
memset(&cfg.w[3],0,sizeof(cfg) - 3*sizeof(cfg.w[0])); |
| 1613 |
|
| 1614 |
|
| 1615 |
memset(ctx->X,0,sizeof(ctx->X)); |
| 1616 |
Skein1024_Process_Block(ctx,cfg.b,1,SKEIN_CFG_STR_LEN); |
| 1617 |
break; |
| 1618 |
} |
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
Skein_Start_New_Type(ctx,MSG); |
| 1623 |
|
| 1624 |
return SKEIN_SUCCESS; |
| 1625 |
} |
| 1626 |
|
| 1627 |
#if 0 |
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
static int Skein1024_InitExt(Skein1024_Ctxt_t *ctx,size_t hashBitLen,u64b_t treeInfo, const u08b_t *key, size_t keyBytes) |
| 1632 |
{ |
| 1633 |
union |
| 1634 |
{ |
| 1635 |
u08b_t b[SKEIN1024_STATE_BYTES]; |
| 1636 |
u64b_t w[SKEIN1024_STATE_WORDS]; |
| 1637 |
} cfg; |
| 1638 |
|
| 1639 |
Skein_Assert(hashBitLen > 0,SKEIN_BAD_HASHLEN); |
| 1640 |
Skein_Assert(keyBytes == 0 || key != NULL,SKEIN_FAIL); |
| 1641 |
|
| 1642 |
|
| 1643 |
if (keyBytes == 0) |
| 1644 |
{ |
| 1645 |
memset(ctx->X,0,sizeof(ctx->X)); |
| 1646 |
} |
| 1647 |
else |
| 1648 |
{ |
| 1649 |
Skein_assert(sizeof(cfg.b) >= sizeof(ctx->X)); |
| 1650 |
|
| 1651 |
ctx->h.hashBitLen=8*sizeof(ctx->X); |
| 1652 |
Skein_Start_New_Type(ctx,KEY); |
| 1653 |
memset(ctx->X,0,sizeof(ctx->X)); |
| 1654 |
Skein1024_Update(ctx,key,keyBytes); |
| 1655 |
Skein1024_Final_Pad(ctx,cfg.b); |
| 1656 |
memcpy(ctx->X,cfg.b,sizeof(cfg.b)); |
| 1657 |
#if SKEIN_NEED_SWAP |
| 1658 |
{ |
| 1659 |
uint_t i; |
| 1660 |
for (i=0;i<SKEIN1024_STATE_WORDS;i++) |
| 1661 |
ctx->X[i] = Skein_Swap64(ctx->X[i]); |
| 1662 |
} |
| 1663 |
#endif |
| 1664 |
} |
| 1665 |
|
| 1666 |
ctx->h.hashBitLen = hashBitLen; |
| 1667 |
Skein_Start_New_Type(ctx,CFG_FINAL); |
| 1668 |
|
| 1669 |
memset(&cfg.w,0,sizeof(cfg.w)); |
| 1670 |
cfg.w[0] = Skein_Swap64(SKEIN_SCHEMA_VER); |
| 1671 |
cfg.w[1] = Skein_Swap64(hashBitLen); |
| 1672 |
cfg.w[2] = Skein_Swap64(treeInfo); |
| 1673 |
|
| 1674 |
Skein_Show_Key(1024,&ctx->h,key,keyBytes); |
| 1675 |
|
| 1676 |
|
| 1677 |
Skein1024_Process_Block(ctx,cfg.b,1,SKEIN_CFG_STR_LEN); |
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
ctx->h.bCnt = 0; |
| 1682 |
Skein_Start_New_Type(ctx,MSG); |
| 1683 |
|
| 1684 |
return SKEIN_SUCCESS; |
| 1685 |
} |
| 1686 |
#endif |
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
static int Skein1024_Update(Skein1024_Ctxt_t *ctx, const u08b_t *msg, size_t msgByteCnt) |
| 1691 |
{ |
| 1692 |
size_t n; |
| 1693 |
|
| 1694 |
Skein_Assert(ctx->h.bCnt <= SKEIN1024_BLOCK_BYTES,SKEIN_FAIL); |
| 1695 |
|
| 1696 |
|
| 1697 |
if (msgByteCnt + ctx->h.bCnt > SKEIN1024_BLOCK_BYTES) |
| 1698 |
{ |
| 1699 |
if (ctx->h.bCnt) |
| 1700 |
{ |
| 1701 |
n = SKEIN1024_BLOCK_BYTES - ctx->h.bCnt; |
| 1702 |
if (n) |
| 1703 |
{ |
| 1704 |
Skein_assert(n < msgByteCnt); |
| 1705 |
memcpy(&ctx->b[ctx->h.bCnt],msg,n); |
| 1706 |
msgByteCnt -= n; |
| 1707 |
msg += n; |
| 1708 |
ctx->h.bCnt += n; |
| 1709 |
} |
| 1710 |
Skein_assert(ctx->h.bCnt == SKEIN1024_BLOCK_BYTES); |
| 1711 |
Skein1024_Process_Block(ctx,ctx->b,1,SKEIN1024_BLOCK_BYTES); |
| 1712 |
ctx->h.bCnt = 0; |
| 1713 |
} |
| 1714 |
|
| 1715 |
if (msgByteCnt > SKEIN1024_BLOCK_BYTES) |
| 1716 |
{ |
| 1717 |
n = (msgByteCnt-1) / SKEIN1024_BLOCK_BYTES; |
| 1718 |
Skein1024_Process_Block(ctx,msg,n,SKEIN1024_BLOCK_BYTES); |
| 1719 |
msgByteCnt -= n * SKEIN1024_BLOCK_BYTES; |
| 1720 |
msg += n * SKEIN1024_BLOCK_BYTES; |
| 1721 |
} |
| 1722 |
Skein_assert(ctx->h.bCnt == 0); |
| 1723 |
} |
| 1724 |
|
| 1725 |
|
| 1726 |
if (msgByteCnt) |
| 1727 |
{ |
| 1728 |
Skein_assert(msgByteCnt + ctx->h.bCnt <= SKEIN1024_BLOCK_BYTES); |
| 1729 |
memcpy(&ctx->b[ctx->h.bCnt],msg,msgByteCnt); |
| 1730 |
ctx->h.bCnt += msgByteCnt; |
| 1731 |
} |
| 1732 |
|
| 1733 |
return SKEIN_SUCCESS; |
| 1734 |
} |
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
static int Skein1024_Final(Skein1024_Ctxt_t *ctx, u08b_t *hashVal) |
| 1739 |
{ |
| 1740 |
size_t i,n,byteCnt; |
| 1741 |
u64b_t X[SKEIN1024_STATE_WORDS]; |
| 1742 |
Skein_Assert(ctx->h.bCnt <= SKEIN1024_BLOCK_BYTES,SKEIN_FAIL); |
| 1743 |
|
| 1744 |
ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; |
| 1745 |
if (ctx->h.bCnt < SKEIN1024_BLOCK_BYTES) |
| 1746 |
memset(&ctx->b[ctx->h.bCnt],0,SKEIN1024_BLOCK_BYTES - ctx->h.bCnt); |
| 1747 |
|
| 1748 |
Skein1024_Process_Block(ctx,ctx->b,1,ctx->h.bCnt); |
| 1749 |
|
| 1750 |
|
| 1751 |
byteCnt = (ctx->h.hashBitLen + 7) >> 3; |
| 1752 |
|
| 1753 |
|
| 1754 |
memset(ctx->b,0,sizeof(ctx->b)); |
| 1755 |
memcpy(X,ctx->X,sizeof(X)); |
| 1756 |
for (i=0;i*SKEIN1024_BLOCK_BYTES < byteCnt;i++) |
| 1757 |
{ |
| 1758 |
((u64b_t *)ctx->b)[0]= Skein_Swap64((u64b_t) i); |
| 1759 |
Skein_Start_New_Type(ctx,OUT_FINAL); |
| 1760 |
Skein1024_Process_Block(ctx,ctx->b,1,sizeof(u64b_t)); |
| 1761 |
n = byteCnt - i*SKEIN1024_BLOCK_BYTES; |
| 1762 |
if (n >= SKEIN1024_BLOCK_BYTES) |
| 1763 |
n = SKEIN1024_BLOCK_BYTES; |
| 1764 |
Skein_Put64_LSB_First(hashVal+i*SKEIN1024_BLOCK_BYTES,ctx->X,n); |
| 1765 |
Skein_Show_Final(1024,&ctx->h,n,hashVal+i*SKEIN1024_BLOCK_BYTES); |
| 1766 |
memcpy(ctx->X,X,sizeof(X)); |
| 1767 |
} |
| 1768 |
return SKEIN_SUCCESS; |
| 1769 |
} |
| 1770 |
|
| 1771 |
#if defined(SKEIN_CODE_SIZE) || defined(SKEIN_PERF) |
| 1772 |
static size_t Skein1024_API_CodeSize(void) |
| 1773 |
{ |
| 1774 |
return ((u08b_t *) Skein1024_API_CodeSize) - |
| 1775 |
((u08b_t *) Skein1024_Init); |
| 1776 |
} |
| 1777 |
#endif |
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
#if 0 |
| 1783 |
|
| 1784 |
|
| 1785 |
static int Skein_256_Final_Pad(Skein_256_Ctxt_t *ctx, u08b_t *hashVal) |
| 1786 |
{ |
| 1787 |
Skein_Assert(ctx->h.bCnt <= SKEIN_256_BLOCK_BYTES,SKEIN_FAIL); |
| 1788 |
|
| 1789 |
ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; |
| 1790 |
if (ctx->h.bCnt < SKEIN_256_BLOCK_BYTES) |
| 1791 |
memset(&ctx->b[ctx->h.bCnt],0,SKEIN_256_BLOCK_BYTES - ctx->h.bCnt); |
| 1792 |
Skein_256_Process_Block(ctx,ctx->b,1,ctx->h.bCnt); |
| 1793 |
|
| 1794 |
Skein_Put64_LSB_First(hashVal,ctx->X,SKEIN_256_BLOCK_BYTES); |
| 1795 |
|
| 1796 |
return SKEIN_SUCCESS; |
| 1797 |
} |
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
static int Skein_512_Final_Pad(Skein_512_Ctxt_t *ctx, u08b_t *hashVal) |
| 1802 |
{ |
| 1803 |
Skein_Assert(ctx->h.bCnt <= SKEIN_512_BLOCK_BYTES,SKEIN_FAIL); |
| 1804 |
|
| 1805 |
ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; |
| 1806 |
if (ctx->h.bCnt < SKEIN_512_BLOCK_BYTES) |
| 1807 |
memset(&ctx->b[ctx->h.bCnt],0,SKEIN_512_BLOCK_BYTES - ctx->h.bCnt); |
| 1808 |
Skein_512_Process_Block(ctx,ctx->b,1,ctx->h.bCnt); |
| 1809 |
|
| 1810 |
Skein_Put64_LSB_First(hashVal,ctx->X,SKEIN_512_BLOCK_BYTES); |
| 1811 |
|
| 1812 |
return SKEIN_SUCCESS; |
| 1813 |
} |
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
static int Skein1024_Final_Pad(Skein1024_Ctxt_t *ctx, u08b_t *hashVal) |
| 1818 |
{ |
| 1819 |
Skein_Assert(ctx->h.bCnt <= SKEIN1024_BLOCK_BYTES,SKEIN_FAIL); |
| 1820 |
|
| 1821 |
ctx->h.T[1] |= SKEIN_T1_FLAG_FINAL; |
| 1822 |
if (ctx->h.bCnt < SKEIN1024_BLOCK_BYTES) |
| 1823 |
memset(&ctx->b[ctx->h.bCnt],0,SKEIN1024_BLOCK_BYTES - ctx->h.bCnt); |
| 1824 |
Skein1024_Process_Block(ctx,ctx->b,1,ctx->h.bCnt); |
| 1825 |
|
| 1826 |
Skein_Put64_LSB_First(hashVal,ctx->X,SKEIN1024_BLOCK_BYTES); |
| 1827 |
|
| 1828 |
return SKEIN_SUCCESS; |
| 1829 |
} |
| 1830 |
|
| 1831 |
|
| 1832 |
#if SKEIN_TREE_HASH |
| 1833 |
|
| 1834 |
|
| 1835 |
static int Skein_256_Output(Skein_256_Ctxt_t *ctx, u08b_t *hashVal) |
| 1836 |
{ |
| 1837 |
size_t i,n,byteCnt; |
| 1838 |
u64b_t X[SKEIN_256_STATE_WORDS]; |
| 1839 |
Skein_Assert(ctx->h.bCnt <= SKEIN_256_BLOCK_BYTES,SKEIN_FAIL); |
| 1840 |
|
| 1841 |
|
| 1842 |
byteCnt = (ctx->h.hashBitLen + 7) >> 3; |
| 1843 |
|
| 1844 |
|
| 1845 |
memset(ctx->b,0,sizeof(ctx->b)); |
| 1846 |
memcpy(X,ctx->X,sizeof(X)); |
| 1847 |
for (i=0;i*SKEIN_256_BLOCK_BYTES < byteCnt;i++) |
| 1848 |
{ |
| 1849 |
((u64b_t *)ctx->b)[0]= Skein_Swap64((u64b_t) i); |
| 1850 |
Skein_Start_New_Type(ctx,OUT_FINAL); |
| 1851 |
Skein_256_Process_Block(ctx,ctx->b,1,sizeof(u64b_t)); |
| 1852 |
n = byteCnt - i*SKEIN_256_BLOCK_BYTES; |
| 1853 |
if (n >= SKEIN_256_BLOCK_BYTES) |
| 1854 |
n = SKEIN_256_BLOCK_BYTES; |
| 1855 |
Skein_Put64_LSB_First(hashVal+i*SKEIN_256_BLOCK_BYTES,ctx->X,n); |
| 1856 |
Skein_Show_Final(256,&ctx->h,n,hashVal+i*SKEIN_256_BLOCK_BYTES); |
| 1857 |
memcpy(ctx->X,X,sizeof(X)); |
| 1858 |
} |
| 1859 |
return SKEIN_SUCCESS; |
| 1860 |
} |
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
static int Skein_512_Output(Skein_512_Ctxt_t *ctx, u08b_t *hashVal) |
| 1865 |
{ |
| 1866 |
size_t i,n,byteCnt; |
| 1867 |
u64b_t X[SKEIN_512_STATE_WORDS]; |
| 1868 |
Skein_Assert(ctx->h.bCnt <= SKEIN_512_BLOCK_BYTES,SKEIN_FAIL); |
| 1869 |
|
| 1870 |
|
| 1871 |
byteCnt = (ctx->h.hashBitLen + 7) >> 3; |
| 1872 |
|
| 1873 |
|
| 1874 |
memset(ctx->b,0,sizeof(ctx->b)); |
| 1875 |
memcpy(X,ctx->X,sizeof(X)); |
| 1876 |
for (i=0;i*SKEIN_512_BLOCK_BYTES < byteCnt;i++) |
| 1877 |
{ |
| 1878 |
((u64b_t *)ctx->b)[0]= Skein_Swap64((u64b_t) i); |
| 1879 |
Skein_Start_New_Type(ctx,OUT_FINAL); |
| 1880 |
Skein_512_Process_Block(ctx,ctx->b,1,sizeof(u64b_t)); |
| 1881 |
n = byteCnt - i*SKEIN_512_BLOCK_BYTES; |
| 1882 |
if (n >= SKEIN_512_BLOCK_BYTES) |
| 1883 |
n = SKEIN_512_BLOCK_BYTES; |
| 1884 |
Skein_Put64_LSB_First(hashVal+i*SKEIN_512_BLOCK_BYTES,ctx->X,n); |
| 1885 |
Skein_Show_Final(256,&ctx->h,n,hashVal+i*SKEIN_512_BLOCK_BYTES); |
| 1886 |
memcpy(ctx->X,X,sizeof(X)); |
| 1887 |
} |
| 1888 |
return SKEIN_SUCCESS; |
| 1889 |
} |
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
static int Skein1024_Output(Skein1024_Ctxt_t *ctx, u08b_t *hashVal) |
| 1894 |
{ |
| 1895 |
size_t i,n,byteCnt; |
| 1896 |
u64b_t X[SKEIN1024_STATE_WORDS]; |
| 1897 |
Skein_Assert(ctx->h.bCnt <= SKEIN1024_BLOCK_BYTES,SKEIN_FAIL); |
| 1898 |
|
| 1899 |
|
| 1900 |
byteCnt = (ctx->h.hashBitLen + 7) >> 3; |
| 1901 |
|
| 1902 |
|
| 1903 |
memset(ctx->b,0,sizeof(ctx->b)); |
| 1904 |
memcpy(X,ctx->X,sizeof(X)); |
| 1905 |
for (i=0;i*SKEIN1024_BLOCK_BYTES < byteCnt;i++) |
| 1906 |
{ |
| 1907 |
((u64b_t *)ctx->b)[0]= Skein_Swap64((u64b_t) i); |
| 1908 |
Skein_Start_New_Type(ctx,OUT_FINAL); |
| 1909 |
Skein1024_Process_Block(ctx,ctx->b,1,sizeof(u64b_t)); |
| 1910 |
n = byteCnt - i*SKEIN1024_BLOCK_BYTES; |
| 1911 |
if (n >= SKEIN1024_BLOCK_BYTES) |
| 1912 |
n = SKEIN1024_BLOCK_BYTES; |
| 1913 |
Skein_Put64_LSB_First(hashVal+i*SKEIN1024_BLOCK_BYTES,ctx->X,n); |
| 1914 |
Skein_Show_Final(256,&ctx->h,n,hashVal+i*SKEIN1024_BLOCK_BYTES); |
| 1915 |
memcpy(ctx->X,X,sizeof(X)); |
| 1916 |
} |
| 1917 |
return SKEIN_SUCCESS; |
| 1918 |
} |
| 1919 |
#endif |
| 1920 |
#endif |
| 1921 |
|
| 1922 |
typedef struct |
| 1923 |
{ |
| 1924 |
uint_t statebits; |
| 1925 |
union |
| 1926 |
{ |
| 1927 |
Skein_Ctxt_Hdr_t h; |
| 1928 |
Skein_256_Ctxt_t ctx_256; |
| 1929 |
Skein_512_Ctxt_t ctx_512; |
| 1930 |
Skein1024_Ctxt_t ctx1024; |
| 1931 |
} u; |
| 1932 |
} |
| 1933 |
hashState; |
| 1934 |
|
| 1935 |
|
| 1936 |
static HashReturn Init (hashState *state, int hashbitlen); |
| 1937 |
static HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen); |
| 1938 |
static HashReturn Final (hashState *state, BitSequence *hashval); |
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
static HashReturn Init(hashState *state, int hashbitlen) |
| 1943 |
{ |
| 1944 |
#if SKEIN_256_NIST_MAX_HASHBITS |
| 1945 |
if (hashbitlen <= SKEIN_256_NIST_MAX_HASHBITS) |
| 1946 |
{ |
| 1947 |
Skein_Assert(hashbitlen > 0,BAD_HASHLEN); |
| 1948 |
state->statebits = 64*SKEIN_256_STATE_WORDS; |
| 1949 |
return Skein_256_Init(&state->u.ctx_256,(size_t) hashbitlen); |
| 1950 |
} |
| 1951 |
#endif |
| 1952 |
if (hashbitlen <= SKEIN_512_NIST_MAX_HASHBITS) |
| 1953 |
{ |
| 1954 |
state->statebits = 64*SKEIN_512_STATE_WORDS; |
| 1955 |
return Skein_512_Init(&state->u.ctx_512,(size_t) hashbitlen); |
| 1956 |
} |
| 1957 |
else |
| 1958 |
{ |
| 1959 |
state->statebits = 64*SKEIN1024_STATE_WORDS; |
| 1960 |
return Skein1024_Init(&state->u.ctx1024,(size_t) hashbitlen); |
| 1961 |
} |
| 1962 |
} |
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
static HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen) |
| 1967 |
{ |
| 1968 |
|
| 1969 |
Skein_Assert((state->u.h.T[1] & SKEIN_T1_FLAG_BIT_PAD) == 0 || databitlen == 0, SKEIN_FAIL); |
| 1970 |
|
| 1971 |
Skein_Assert(state->statebits % 256 == 0 && (state->statebits-256) < 1024,SKEIN_FAIL); |
| 1972 |
if ((databitlen & 7) == 0) |
| 1973 |
{ |
| 1974 |
switch ((state->statebits >> 8) & 3) |
| 1975 |
{ |
| 1976 |
case 2: return Skein_512_Update(&state->u.ctx_512,data,databitlen >> 3); |
| 1977 |
case 1: return Skein_256_Update(&state->u.ctx_256,data,databitlen >> 3); |
| 1978 |
case 0: return Skein1024_Update(&state->u.ctx1024,data,databitlen >> 3); |
| 1979 |
default: return SKEIN_FAIL; |
| 1980 |
} |
| 1981 |
} |
| 1982 |
else |
| 1983 |
{ |
| 1984 |
size_t bCnt = (databitlen >> 3) + 1; |
| 1985 |
u08b_t b,mask; |
| 1986 |
|
| 1987 |
mask = (u08b_t) (1u << (7 - (databitlen & 7))); |
| 1988 |
b = (u08b_t) ((data[bCnt-1] & (0-mask)) | mask); |
| 1989 |
|
| 1990 |
switch ((state->statebits >> 8) & 3) |
| 1991 |
{ |
| 1992 |
case 2: Skein_512_Update(&state->u.ctx_512,data,bCnt-1); |
| 1993 |
Skein_512_Update(&state->u.ctx_512,&b , 1 ); |
| 1994 |
break; |
| 1995 |
case 1: Skein_256_Update(&state->u.ctx_256,data,bCnt-1); |
| 1996 |
Skein_256_Update(&state->u.ctx_256,&b , 1 ); |
| 1997 |
break; |
| 1998 |
case 0: Skein1024_Update(&state->u.ctx1024,data,bCnt-1); |
| 1999 |
Skein1024_Update(&state->u.ctx1024,&b , 1 ); |
| 2000 |
break; |
| 2001 |
default: return SKEIN_FAIL; |
| 2002 |
} |
| 2003 |
Skein_Set_Bit_Pad_Flag(state->u.h); |
| 2004 |
|
| 2005 |
return SKEIN_SUCCESS; |
| 2006 |
} |
| 2007 |
} |
| 2008 |
|
| 2009 |
|
| 2010 |
|
| 2011 |
static HashReturn Final(hashState *state, BitSequence *hashval) |
| 2012 |
{ |
| 2013 |
Skein_Assert(state->statebits % 256 == 0 && (state->statebits-256) < 1024,FAIL); |
| 2014 |
switch ((state->statebits >> 8) & 3) |
| 2015 |
{ |
| 2016 |
case 2: return Skein_512_Final(&state->u.ctx_512,hashval); |
| 2017 |
case 1: return Skein_256_Final(&state->u.ctx_256,hashval); |
| 2018 |
case 0: return Skein1024_Final(&state->u.ctx1024,hashval); |
| 2019 |
default: return SKEIN_FAIL; |
| 2020 |
} |
| 2021 |
} |
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
HashReturn skein_hash(int hashbitlen, const BitSequence *data, |
| 2026 |
DataLength databitlen,BitSequence *hashval) |
| 2027 |
{ |
| 2028 |
hashState state; |
| 2029 |
HashReturn r = Init(&state,hashbitlen); |
| 2030 |
if (r == SKEIN_SUCCESS) |
| 2031 |
{ |
| 2032 |
r = Update(&state,data,databitlen); |
| 2033 |
Final(&state,hashval); |
| 2034 |
} |
| 2035 |
return r; |
| 2036 |
} |