1 /* 2 * IDI,NTNU 3 * 4 * CDDL HEADER START 5 * 6 * The contents of this file are subject to the terms of the 7 * Common Development and Distribution License (the "License"). 8 * You may not use this file except in compliance with the License. 9 * 10 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 11 * or http://opensource.org/licenses/CDDL-1.0. 12 * See the License for the specific language governing permissions 13 * and limitations under the License. 14 * 15 * When distributing Covered Code, include this CDDL HEADER in each 16 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 17 * If applicable, add the following below this CDDL HEADER, with the 18 * fields enclosed by brackets "[]" replaced with your own identifying 19 * information: Portions Copyright [yyyy] [name of copyright owner] 20 * 21 * CDDL HEADER END 22 * 23 * Copyright (C) 2009, 2010, Jorn Amundsen <jorn.amundsen@ntnu.no> 24 * Tweaked Edon-R implementation for SUPERCOP, based on NIST API. 25 * 26 * $Id: edonr.c 517 2013-02-17 20:34:39Z joern $ 27 */ 28 /* 29 * Portions copyright (c) 2013, Saso Kiselkov, All rights reserved 30 */ 31 32 /* 33 * Unlike sha2 or skein, we won't expose edonr via the Kernel Cryptographic 34 * Framework (KCF), because Edon-R is *NOT* suitable for general-purpose 35 * cryptographic use. Users of Edon-R must interface directly to this module. 36 */ 37 38 #include <sys/string.h> 39 #include <sys/edonr.h> 40 #include <sys/debug.h> 41 42 /* big endian support, provides no-op's if run on little endian hosts */ 43 #include "edonr_byteorder.h" 44 45 #define hashState224(x) ((x)->pipe->p256) 46 #define hashState256(x) ((x)->pipe->p256) 47 #define hashState384(x) ((x)->pipe->p512) 48 #define hashState512(x) ((x)->pipe->p512) 49 50 /* rotate shortcuts */ 51 #define rotl32(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) 52 #define rotr32(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) 53 54 #define rotl64(x, n) (((x) << (n)) | ((x) >> (64 - (n)))) 55 #define rotr64(x, n) (((x) >> (n)) | ((x) << (64 - (n)))) 56 57 #if !defined(__C99_RESTRICT) 58 #define restrict /* restrict */ 59 #endif 60 61 #define EDONR_VALID_HASHBITLEN(x) \ 62 ((x) == 512 || (x) == 384 || (x) == 256 || (x) == 224) 63 64 /* EdonR224 initial double chaining pipe */ 65 static const uint32_t i224p2[16] = { 66 0x00010203ul, 0x04050607ul, 0x08090a0bul, 0x0c0d0e0ful, 67 0x10111213ul, 0x14151617ul, 0x18191a1bul, 0x1c1d1e1ful, 68 0x20212223ul, 0x24252627ul, 0x28292a2bul, 0x2c2d2e2ful, 69 0x30313233ul, 0x34353637ul, 0x38393a3bul, 0x3c3d3e3ful, 70 }; 71 72 /* EdonR256 initial double chaining pipe */ 73 static const uint32_t i256p2[16] = { 74 0x40414243ul, 0x44454647ul, 0x48494a4bul, 0x4c4d4e4ful, 75 0x50515253ul, 0x54555657ul, 0x58595a5bul, 0x5c5d5e5ful, 76 0x60616263ul, 0x64656667ul, 0x68696a6bul, 0x6c6d6e6ful, 77 0x70717273ul, 0x74757677ul, 0x78797a7bul, 0x7c7d7e7ful, 78 }; 79 80 /* EdonR384 initial double chaining pipe */ 81 static const uint64_t i384p2[16] = { 82 0x0001020304050607ull, 0x08090a0b0c0d0e0full, 83 0x1011121314151617ull, 0x18191a1b1c1d1e1full, 84 0x2021222324252627ull, 0x28292a2b2c2d2e2full, 85 0x3031323334353637ull, 0x38393a3b3c3d3e3full, 86 0x4041424344454647ull, 0x48494a4b4c4d4e4full, 87 0x5051525354555657ull, 0x58595a5b5c5d5e5full, 88 0x6061626364656667ull, 0x68696a6b6c6d6e6full, 89 0x7071727374757677ull, 0x78797a7b7c7d7e7full 90 }; 91 92 /* EdonR512 initial double chaining pipe */ 93 static const uint64_t i512p2[16] = { 94 0x8081828384858687ull, 0x88898a8b8c8d8e8full, 95 0x9091929394959697ull, 0x98999a9b9c9d9e9full, 96 0xa0a1a2a3a4a5a6a7ull, 0xa8a9aaabacadaeafull, 97 0xb0b1b2b3b4b5b6b7ull, 0xb8b9babbbcbdbebfull, 98 0xc0c1c2c3c4c5c6c7ull, 0xc8c9cacbcccdcecfull, 99 0xd0d1d2d3d4d5d6d7ull, 0xd8d9dadbdcdddedfull, 100 0xe0e1e2e3e4e5e6e7ull, 0xe8e9eaebecedeeefull, 101 0xf0f1f2f3f4f5f6f7ull, 0xf8f9fafbfcfdfeffull 102 }; 103 104 /* 105 * First Latin Square 106 * 0 7 1 3 2 4 6 5 107 * 4 1 7 6 3 0 5 2 108 * 7 0 4 2 5 3 1 6 109 * 1 4 0 5 6 2 7 3 110 * 2 3 6 7 1 5 0 4 111 * 5 2 3 1 7 6 4 0 112 * 3 6 5 0 4 7 2 1 113 * 6 5 2 4 0 1 3 7 114 */ 115 #define LS1_256(c, x0, x1, x2, x3, x4, x5, x6, x7) \ 116 { \ 117 uint32_t x04, x17, x23, x56, x07, x26; \ 118 x04 = x0+x4, x17 = x1+x7, x07 = x04+x17; \ 119 s0 = c + x07 + x2; \ 120 s1 = rotl32(x07 + x3, 4); \ 121 s2 = rotl32(x07 + x6, 8); \ 122 x23 = x2 + x3; \ 123 s5 = rotl32(x04 + x23 + x5, 22); \ 124 x56 = x5 + x6; \ 125 s6 = rotl32(x17 + x56 + x0, 24); \ 126 x26 = x23+x56; \ 127 s3 = rotl32(x26 + x7, 13); \ 128 s4 = rotl32(x26 + x1, 17); \ 129 s7 = rotl32(x26 + x4, 29); \ 130 } 131 132 #define LS1_512(c, x0, x1, x2, x3, x4, x5, x6, x7) \ 133 { \ 134 uint64_t x04, x17, x23, x56, x07, x26; \ 135 x04 = x0+x4, x17 = x1+x7, x07 = x04+x17; \ 136 s0 = c + x07 + x2; \ 137 s1 = rotl64(x07 + x3, 5); \ 138 s2 = rotl64(x07 + x6, 15); \ 139 x23 = x2 + x3; \ 140 s5 = rotl64(x04 + x23 + x5, 40); \ 141 x56 = x5 + x6; \ 142 s6 = rotl64(x17 + x56 + x0, 50); \ 143 x26 = x23+x56; \ 144 s3 = rotl64(x26 + x7, 22); \ 145 s4 = rotl64(x26 + x1, 31); \ 146 s7 = rotl64(x26 + x4, 59); \ 147 } 148 149 /* 150 * Second Orthogonal Latin Square 151 * 0 4 2 3 1 6 5 7 152 * 7 6 3 2 5 4 1 0 153 * 5 3 1 6 0 2 7 4 154 * 1 0 5 4 3 7 2 6 155 * 2 1 0 7 4 5 6 3 156 * 3 5 7 0 6 1 4 2 157 * 4 7 6 1 2 0 3 5 158 * 6 2 4 5 7 3 0 1 159 */ 160 #define LS2_256(c, y0, y1, y2, y3, y4, y5, y6, y7) \ 161 { \ 162 uint32_t y01, y25, y34, y67, y04, y05, y27, y37; \ 163 y01 = y0+y1, y25 = y2+y5, y05 = y01+y25; \ 164 t0 = ~c + y05 + y7; \ 165 t2 = rotl32(y05 + y3, 9); \ 166 y34 = y3+y4, y04 = y01+y34; \ 167 t1 = rotl32(y04 + y6, 5); \ 168 t4 = rotl32(y04 + y5, 15); \ 169 y67 = y6+y7, y37 = y34+y67; \ 170 t3 = rotl32(y37 + y2, 11); \ 171 t7 = rotl32(y37 + y0, 27); \ 172 y27 = y25+y67; \ 173 t5 = rotl32(y27 + y4, 20); \ 174 t6 = rotl32(y27 + y1, 25); \ 175 } 176 177 #define LS2_512(c, y0, y1, y2, y3, y4, y5, y6, y7) \ 178 { \ 179 uint64_t y01, y25, y34, y67, y04, y05, y27, y37; \ 180 y01 = y0+y1, y25 = y2+y5, y05 = y01+y25; \ 181 t0 = ~c + y05 + y7; \ 182 t2 = rotl64(y05 + y3, 19); \ 183 y34 = y3+y4, y04 = y01+y34; \ 184 t1 = rotl64(y04 + y6, 10); \ 185 t4 = rotl64(y04 + y5, 36); \ 186 y67 = y6+y7, y37 = y34+y67; \ 187 t3 = rotl64(y37 + y2, 29); \ 188 t7 = rotl64(y37 + y0, 55); \ 189 y27 = y25+y67; \ 190 t5 = rotl64(y27 + y4, 44); \ 191 t6 = rotl64(y27 + y1, 48); \ 192 } 193 194 #define quasi_exform256(r0, r1, r2, r3, r4, r5, r6, r7) \ 195 { \ 196 uint32_t s04, s17, s23, s56, t01, t25, t34, t67; \ 197 s04 = s0 ^ s4, t01 = t0 ^ t1; \ 198 r0 = (s04 ^ s1) + (t01 ^ t5); \ 199 t67 = t6 ^ t7; \ 200 r1 = (s04 ^ s7) + (t2 ^ t67); \ 201 s23 = s2 ^ s3; \ 202 r7 = (s23 ^ s5) + (t4 ^ t67); \ 203 t34 = t3 ^ t4; \ 204 r3 = (s23 ^ s4) + (t0 ^ t34); \ 205 s56 = s5 ^ s6; \ 206 r5 = (s3 ^ s56) + (t34 ^ t6); \ 207 t25 = t2 ^ t5; \ 208 r6 = (s2 ^ s56) + (t25 ^ t7); \ 209 s17 = s1 ^ s7; \ 210 r4 = (s0 ^ s17) + (t1 ^ t25); \ 211 r2 = (s17 ^ s6) + (t01 ^ t3); \ 212 } 213 214 #define quasi_exform512(r0, r1, r2, r3, r4, r5, r6, r7) \ 215 { \ 216 uint64_t s04, s17, s23, s56, t01, t25, t34, t67; \ 217 s04 = s0 ^ s4, t01 = t0 ^ t1; \ 218 r0 = (s04 ^ s1) + (t01 ^ t5); \ 219 t67 = t6 ^ t7; \ 220 r1 = (s04 ^ s7) + (t2 ^ t67); \ 221 s23 = s2 ^ s3; \ 222 r7 = (s23 ^ s5) + (t4 ^ t67); \ 223 t34 = t3 ^ t4; \ 224 r3 = (s23 ^ s4) + (t0 ^ t34); \ 225 s56 = s5 ^ s6; \ 226 r5 = (s3 ^ s56) + (t34 ^ t6); \ 227 t25 = t2 ^ t5; \ 228 r6 = (s2 ^ s56) + (t25 ^ t7); \ 229 s17 = s1 ^ s7; \ 230 r4 = (s0 ^ s17) + (t1 ^ t25); \ 231 r2 = (s17 ^ s6) + (t01 ^ t3); \ 232 } 233 234 static size_t 235 Q256(size_t bitlen, const uint32_t *data, uint32_t *restrict p) 236 { 237 size_t bl; 238 239 for (bl = bitlen; bl >= EdonR256_BLOCK_BITSIZE; 240 bl -= EdonR256_BLOCK_BITSIZE, data += 16) { 241 uint32_t s0, s1, s2, s3, s4, s5, s6, s7, t0, t1, t2, t3, t4, 242 t5, t6, t7; 243 uint32_t p0, p1, p2, p3, p4, p5, p6, p7, q0, q1, q2, q3, q4, 244 q5, q6, q7; 245 const uint32_t defix = 0xaaaaaaaa; 246 #if defined(MACHINE_IS_BIG_ENDIAN) 247 uint32_t swp0, swp1, swp2, swp3, swp4, swp5, swp6, swp7, swp8, 248 swp9, swp10, swp11, swp12, swp13, swp14, swp15; 249 #define d(j) swp ## j 250 #define s32(j) ld_swap32((uint32_t *)data + j, swp ## j) 251 #else 252 #define d(j) data[j] 253 #endif 254 255 /* First row of quasigroup e-transformations */ 256 #if defined(MACHINE_IS_BIG_ENDIAN) 257 s32(8); 258 s32(9); 259 s32(10); 260 s32(11); 261 s32(12); 262 s32(13); 263 s32(14); 264 s32(15); 265 #endif 266 LS1_256(defix, d(15), d(14), d(13), d(12), d(11), d(10), d(9), 267 d(8)); 268 #if defined(MACHINE_IS_BIG_ENDIAN) 269 s32(0); 270 s32(1); 271 s32(2); 272 s32(3); 273 s32(4); 274 s32(5); 275 s32(6); 276 s32(7); 277 #undef s32 278 #endif 279 LS2_256(defix, d(0), d(1), d(2), d(3), d(4), d(5), d(6), d(7)); 280 quasi_exform256(p0, p1, p2, p3, p4, p5, p6, p7); 281 282 LS1_256(defix, p0, p1, p2, p3, p4, p5, p6, p7); 283 LS2_256(defix, d(8), d(9), d(10), d(11), d(12), d(13), d(14), 284 d(15)); 285 quasi_exform256(q0, q1, q2, q3, q4, q5, q6, q7); 286 287 /* Second row of quasigroup e-transformations */ 288 LS1_256(defix, p[8], p[9], p[10], p[11], p[12], p[13], p[14], 289 p[15]); 290 LS2_256(defix, p0, p1, p2, p3, p4, p5, p6, p7); 291 quasi_exform256(p0, p1, p2, p3, p4, p5, p6, p7); 292 293 LS1_256(defix, p0, p1, p2, p3, p4, p5, p6, p7); 294 LS2_256(defix, q0, q1, q2, q3, q4, q5, q6, q7); 295 quasi_exform256(q0, q1, q2, q3, q4, q5, q6, q7); 296 297 /* Third row of quasigroup e-transformations */ 298 LS1_256(defix, p0, p1, p2, p3, p4, p5, p6, p7); 299 LS2_256(defix, p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 300 quasi_exform256(p0, p1, p2, p3, p4, p5, p6, p7); 301 302 LS1_256(defix, q0, q1, q2, q3, q4, q5, q6, q7); 303 LS2_256(defix, p0, p1, p2, p3, p4, p5, p6, p7); 304 quasi_exform256(q0, q1, q2, q3, q4, q5, q6, q7); 305 306 /* Fourth row of quasigroup e-transformations */ 307 LS1_256(defix, d(7), d(6), d(5), d(4), d(3), d(2), d(1), d(0)); 308 LS2_256(defix, p0, p1, p2, p3, p4, p5, p6, p7); 309 quasi_exform256(p0, p1, p2, p3, p4, p5, p6, p7); 310 311 LS1_256(defix, p0, p1, p2, p3, p4, p5, p6, p7); 312 LS2_256(defix, q0, q1, q2, q3, q4, q5, q6, q7); 313 quasi_exform256(q0, q1, q2, q3, q4, q5, q6, q7); 314 315 /* Edon-R tweak on the original SHA-3 Edon-R submission. */ 316 p[0] ^= d(8) ^ p0; 317 p[1] ^= d(9) ^ p1; 318 p[2] ^= d(10) ^ p2; 319 p[3] ^= d(11) ^ p3; 320 p[4] ^= d(12) ^ p4; 321 p[5] ^= d(13) ^ p5; 322 p[6] ^= d(14) ^ p6; 323 p[7] ^= d(15) ^ p7; 324 p[8] ^= d(0) ^ q0; 325 p[9] ^= d(1) ^ q1; 326 p[10] ^= d(2) ^ q2; 327 p[11] ^= d(3) ^ q3; 328 p[12] ^= d(4) ^ q4; 329 p[13] ^= d(5) ^ q5; 330 p[14] ^= d(6) ^ q6; 331 p[15] ^= d(7) ^ q7; 332 } 333 334 #undef d 335 return (bitlen - bl); 336 } 337 338 /* 339 * Why is this #pragma here? 340 * 341 * Checksum functions like this one can go over the stack frame size check 342 * Linux imposes on 32-bit platforms (-Wframe-larger-than=1024). We can 343 * safely ignore the compiler error since we know that in OpenZFS, that 344 * the function will be called from a worker thread that won't be using 345 * much stack. The only function that goes over the 1k limit is Q512(), 346 * which only goes over it by a hair (1248 bytes on ARM32). 347 */ 348 #include <sys/isa_defs.h> /* for _ILP32 */ 349 #if defined(_ILP32) /* We're 32-bit, assume small stack frames */ 350 #if defined(__GNUC__) && !defined(__clang__) 351 #pragma GCC diagnostic ignored "-Wframe-larger-than=" 352 #endif 353 #endif 354 355 #if defined(__IBMC__) && defined(_AIX) && defined(__64BIT__) 356 static inline size_t 357 #else 358 static size_t 359 #endif 360 Q512(size_t bitlen, const uint64_t *data, uint64_t *restrict p) 361 { 362 size_t bl; 363 364 for (bl = bitlen; bl >= EdonR512_BLOCK_BITSIZE; 365 bl -= EdonR512_BLOCK_BITSIZE, data += 16) { 366 uint64_t s0, s1, s2, s3, s4, s5, s6, s7, t0, t1, t2, t3, t4, 367 t5, t6, t7; 368 uint64_t p0, p1, p2, p3, p4, p5, p6, p7, q0, q1, q2, q3, q4, 369 q5, q6, q7; 370 const uint64_t defix = 0xaaaaaaaaaaaaaaaaull; 371 #if defined(MACHINE_IS_BIG_ENDIAN) 372 uint64_t swp0, swp1, swp2, swp3, swp4, swp5, swp6, swp7, swp8, 373 swp9, swp10, swp11, swp12, swp13, swp14, swp15; 374 #define d(j) swp##j 375 #define s64(j) ld_swap64((uint64_t *)data+j, swp##j) 376 #else 377 #define d(j) data[j] 378 #endif 379 380 /* First row of quasigroup e-transformations */ 381 #if defined(MACHINE_IS_BIG_ENDIAN) 382 s64(8); 383 s64(9); 384 s64(10); 385 s64(11); 386 s64(12); 387 s64(13); 388 s64(14); 389 s64(15); 390 #endif 391 LS1_512(defix, d(15), d(14), d(13), d(12), d(11), d(10), d(9), 392 d(8)); 393 #if defined(MACHINE_IS_BIG_ENDIAN) 394 s64(0); 395 s64(1); 396 s64(2); 397 s64(3); 398 s64(4); 399 s64(5); 400 s64(6); 401 s64(7); 402 #undef s64 403 #endif 404 LS2_512(defix, d(0), d(1), d(2), d(3), d(4), d(5), d(6), d(7)); 405 quasi_exform512(p0, p1, p2, p3, p4, p5, p6, p7); 406 407 LS1_512(defix, p0, p1, p2, p3, p4, p5, p6, p7); 408 LS2_512(defix, d(8), d(9), d(10), d(11), d(12), d(13), d(14), 409 d(15)); 410 quasi_exform512(q0, q1, q2, q3, q4, q5, q6, q7); 411 412 /* Second row of quasigroup e-transformations */ 413 LS1_512(defix, p[8], p[9], p[10], p[11], p[12], p[13], p[14], 414 p[15]); 415 LS2_512(defix, p0, p1, p2, p3, p4, p5, p6, p7); 416 quasi_exform512(p0, p1, p2, p3, p4, p5, p6, p7); 417 418 LS1_512(defix, p0, p1, p2, p3, p4, p5, p6, p7); 419 LS2_512(defix, q0, q1, q2, q3, q4, q5, q6, q7); 420 quasi_exform512(q0, q1, q2, q3, q4, q5, q6, q7); 421 422 /* Third row of quasigroup e-transformations */ 423 LS1_512(defix, p0, p1, p2, p3, p4, p5, p6, p7); 424 LS2_512(defix, p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]); 425 quasi_exform512(p0, p1, p2, p3, p4, p5, p6, p7); 426 427 LS1_512(defix, q0, q1, q2, q3, q4, q5, q6, q7); 428 LS2_512(defix, p0, p1, p2, p3, p4, p5, p6, p7); 429 quasi_exform512(q0, q1, q2, q3, q4, q5, q6, q7); 430 431 /* Fourth row of quasigroup e-transformations */ 432 LS1_512(defix, d(7), d(6), d(5), d(4), d(3), d(2), d(1), d(0)); 433 LS2_512(defix, p0, p1, p2, p3, p4, p5, p6, p7); 434 quasi_exform512(p0, p1, p2, p3, p4, p5, p6, p7); 435 436 LS1_512(defix, p0, p1, p2, p3, p4, p5, p6, p7); 437 LS2_512(defix, q0, q1, q2, q3, q4, q5, q6, q7); 438 quasi_exform512(q0, q1, q2, q3, q4, q5, q6, q7); 439 440 /* Edon-R tweak on the original SHA-3 Edon-R submission. */ 441 p[0] ^= d(8) ^ p0; 442 p[1] ^= d(9) ^ p1; 443 p[2] ^= d(10) ^ p2; 444 p[3] ^= d(11) ^ p3; 445 p[4] ^= d(12) ^ p4; 446 p[5] ^= d(13) ^ p5; 447 p[6] ^= d(14) ^ p6; 448 p[7] ^= d(15) ^ p7; 449 p[8] ^= d(0) ^ q0; 450 p[9] ^= d(1) ^ q1; 451 p[10] ^= d(2) ^ q2; 452 p[11] ^= d(3) ^ q3; 453 p[12] ^= d(4) ^ q4; 454 p[13] ^= d(5) ^ q5; 455 p[14] ^= d(6) ^ q6; 456 p[15] ^= d(7) ^ q7; 457 } 458 459 #undef d 460 return (bitlen - bl); 461 } 462 463 void 464 EdonRInit(EdonRState *state, size_t hashbitlen) 465 { 466 ASSERT(EDONR_VALID_HASHBITLEN(hashbitlen)); 467 switch (hashbitlen) { 468 case 224: 469 state->hashbitlen = 224; 470 state->bits_processed = 0; 471 state->unprocessed_bits = 0; 472 memcpy(hashState224(state)->DoublePipe, i224p2, 473 sizeof (i224p2)); 474 break; 475 476 case 256: 477 state->hashbitlen = 256; 478 state->bits_processed = 0; 479 state->unprocessed_bits = 0; 480 memcpy(hashState256(state)->DoublePipe, i256p2, 481 sizeof (i256p2)); 482 break; 483 484 case 384: 485 state->hashbitlen = 384; 486 state->bits_processed = 0; 487 state->unprocessed_bits = 0; 488 memcpy(hashState384(state)->DoublePipe, i384p2, 489 sizeof (i384p2)); 490 break; 491 492 case 512: 493 state->hashbitlen = 512; 494 state->bits_processed = 0; 495 state->unprocessed_bits = 0; 496 memcpy(hashState512(state)->DoublePipe, i512p2, 497 sizeof (i512p2)); 498 break; 499 } 500 } 501 502 503 void 504 EdonRUpdate(EdonRState *state, const uint8_t *data, size_t databitlen) 505 { 506 uint32_t *data32; 507 uint64_t *data64; 508 509 size_t bits_processed; 510 511 ASSERT(EDONR_VALID_HASHBITLEN(state->hashbitlen)); 512 switch (state->hashbitlen) { 513 case 224: 514 case 256: 515 if (state->unprocessed_bits > 0) { 516 /* LastBytes = databitlen / 8 */ 517 int LastBytes = (int)databitlen >> 3; 518 519 ASSERT(state->unprocessed_bits + databitlen <= 520 EdonR256_BLOCK_SIZE * 8); 521 522 memcpy(hashState256(state)->LastPart 523 + (state->unprocessed_bits >> 3), 524 data, LastBytes); 525 state->unprocessed_bits += (int)databitlen; 526 databitlen = state->unprocessed_bits; 527 /* LINTED E_BAD_PTR_CAST_ALIGN */ 528 data32 = (uint32_t *)hashState256(state)->LastPart; 529 } else 530 /* LINTED E_BAD_PTR_CAST_ALIGN */ 531 data32 = (uint32_t *)data; 532 533 bits_processed = Q256(databitlen, data32, 534 hashState256(state)->DoublePipe); 535 state->bits_processed += bits_processed; 536 databitlen -= bits_processed; 537 state->unprocessed_bits = (int)databitlen; 538 if (databitlen > 0) { 539 /* LastBytes = Ceil(databitlen / 8) */ 540 int LastBytes = 541 ((~(((-(int)databitlen) >> 3) & 0x01ff)) + 542 1) & 0x01ff; 543 544 data32 += bits_processed >> 5; /* byte size update */ 545 memmove(hashState256(state)->LastPart, 546 data32, LastBytes); 547 } 548 break; 549 550 case 384: 551 case 512: 552 if (state->unprocessed_bits > 0) { 553 /* LastBytes = databitlen / 8 */ 554 int LastBytes = (int)databitlen >> 3; 555 556 ASSERT(state->unprocessed_bits + databitlen <= 557 EdonR512_BLOCK_SIZE * 8); 558 559 memcpy(hashState512(state)->LastPart 560 + (state->unprocessed_bits >> 3), 561 data, LastBytes); 562 state->unprocessed_bits += (int)databitlen; 563 databitlen = state->unprocessed_bits; 564 /* LINTED E_BAD_PTR_CAST_ALIGN */ 565 data64 = (uint64_t *)hashState512(state)->LastPart; 566 } else 567 /* LINTED E_BAD_PTR_CAST_ALIGN */ 568 data64 = (uint64_t *)data; 569 570 bits_processed = Q512(databitlen, data64, 571 hashState512(state)->DoublePipe); 572 state->bits_processed += bits_processed; 573 databitlen -= bits_processed; 574 state->unprocessed_bits = (int)databitlen; 575 if (databitlen > 0) { 576 /* LastBytes = Ceil(databitlen / 8) */ 577 int LastBytes = 578 ((~(((-(int)databitlen) >> 3) & 0x03ff)) + 579 1) & 0x03ff; 580 581 data64 += bits_processed >> 6; /* byte size update */ 582 memmove(hashState512(state)->LastPart, 583 data64, LastBytes); 584 } 585 break; 586 } 587 } 588 589 void 590 EdonRFinal(EdonRState *state, uint8_t *hashval) 591 { 592 uint32_t *data32; 593 uint64_t *data64, num_bits; 594 595 size_t databitlen; 596 int LastByte, PadOnePosition; 597 598 num_bits = state->bits_processed + state->unprocessed_bits; 599 ASSERT(EDONR_VALID_HASHBITLEN(state->hashbitlen)); 600 switch (state->hashbitlen) { 601 case 224: 602 case 256: 603 LastByte = (int)state->unprocessed_bits >> 3; 604 PadOnePosition = 7 - (state->unprocessed_bits & 0x07); 605 hashState256(state)->LastPart[LastByte] = 606 (hashState256(state)->LastPart[LastByte] 607 & (0xff << (PadOnePosition + 1))) ^ 608 (0x01 << PadOnePosition); 609 /* LINTED E_BAD_PTR_CAST_ALIGN */ 610 data64 = (uint64_t *)hashState256(state)->LastPart; 611 612 if (state->unprocessed_bits < 448) { 613 (void) memset((hashState256(state)->LastPart) + 614 LastByte + 1, 0x00, 615 EdonR256_BLOCK_SIZE - LastByte - 9); 616 databitlen = EdonR256_BLOCK_SIZE * 8; 617 #if defined(MACHINE_IS_BIG_ENDIAN) 618 st_swap64(num_bits, data64 + 7); 619 #else 620 data64[7] = num_bits; 621 #endif 622 } else { 623 (void) memset((hashState256(state)->LastPart) + 624 LastByte + 1, 0x00, 625 EdonR256_BLOCK_SIZE * 2 - LastByte - 9); 626 databitlen = EdonR256_BLOCK_SIZE * 16; 627 #if defined(MACHINE_IS_BIG_ENDIAN) 628 st_swap64(num_bits, data64 + 15); 629 #else 630 data64[15] = num_bits; 631 #endif 632 } 633 634 /* LINTED E_BAD_PTR_CAST_ALIGN */ 635 data32 = (uint32_t *)hashState256(state)->LastPart; 636 state->bits_processed += Q256(databitlen, data32, 637 hashState256(state)->DoublePipe); 638 break; 639 640 case 384: 641 case 512: 642 LastByte = (int)state->unprocessed_bits >> 3; 643 PadOnePosition = 7 - (state->unprocessed_bits & 0x07); 644 hashState512(state)->LastPart[LastByte] = 645 (hashState512(state)->LastPart[LastByte] 646 & (0xff << (PadOnePosition + 1))) ^ 647 (0x01 << PadOnePosition); 648 /* LINTED E_BAD_PTR_CAST_ALIGN */ 649 data64 = (uint64_t *)hashState512(state)->LastPart; 650 651 if (state->unprocessed_bits < 960) { 652 (void) memset((hashState512(state)->LastPart) + 653 LastByte + 1, 0x00, 654 EdonR512_BLOCK_SIZE - LastByte - 9); 655 databitlen = EdonR512_BLOCK_SIZE * 8; 656 #if defined(MACHINE_IS_BIG_ENDIAN) 657 st_swap64(num_bits, data64 + 15); 658 #else 659 data64[15] = num_bits; 660 #endif 661 } else { 662 (void) memset((hashState512(state)->LastPart) + 663 LastByte + 1, 0x00, 664 EdonR512_BLOCK_SIZE * 2 - LastByte - 9); 665 databitlen = EdonR512_BLOCK_SIZE * 16; 666 #if defined(MACHINE_IS_BIG_ENDIAN) 667 st_swap64(num_bits, data64 + 31); 668 #else 669 data64[31] = num_bits; 670 #endif 671 } 672 673 state->bits_processed += Q512(databitlen, data64, 674 hashState512(state)->DoublePipe); 675 break; 676 } 677 678 switch (state->hashbitlen) { 679 case 224: { 680 #if defined(MACHINE_IS_BIG_ENDIAN) 681 uint32_t *d32 = (uint32_t *)hashval; 682 uint32_t *s32 = hashState224(state)->DoublePipe + 9; 683 int j; 684 685 for (j = 0; j < EdonR224_DIGEST_SIZE >> 2; j++) 686 st_swap32(s32[j], d32 + j); 687 #else 688 memcpy(hashval, hashState256(state)->DoublePipe + 9, 689 EdonR224_DIGEST_SIZE); 690 #endif 691 break; 692 } 693 case 256: { 694 #if defined(MACHINE_IS_BIG_ENDIAN) 695 uint32_t *d32 = (uint32_t *)hashval; 696 uint32_t *s32 = hashState224(state)->DoublePipe + 8; 697 int j; 698 699 for (j = 0; j < EdonR256_DIGEST_SIZE >> 2; j++) 700 st_swap32(s32[j], d32 + j); 701 #else 702 memcpy(hashval, hashState256(state)->DoublePipe + 8, 703 EdonR256_DIGEST_SIZE); 704 #endif 705 break; 706 } 707 case 384: { 708 #if defined(MACHINE_IS_BIG_ENDIAN) 709 uint64_t *d64 = (uint64_t *)hashval; 710 uint64_t *s64 = hashState384(state)->DoublePipe + 10; 711 int j; 712 713 for (j = 0; j < EdonR384_DIGEST_SIZE >> 3; j++) 714 st_swap64(s64[j], d64 + j); 715 #else 716 memcpy(hashval, hashState384(state)->DoublePipe + 10, 717 EdonR384_DIGEST_SIZE); 718 #endif 719 break; 720 } 721 case 512: { 722 #if defined(MACHINE_IS_BIG_ENDIAN) 723 uint64_t *d64 = (uint64_t *)hashval; 724 uint64_t *s64 = hashState512(state)->DoublePipe + 8; 725 int j; 726 727 for (j = 0; j < EdonR512_DIGEST_SIZE >> 3; j++) 728 st_swap64(s64[j], d64 + j); 729 #else 730 memcpy(hashval, hashState512(state)->DoublePipe + 8, 731 EdonR512_DIGEST_SIZE); 732 #endif 733 break; 734 } 735 } 736 } 737 738 739 void 740 EdonRHash(size_t hashbitlen, const uint8_t *data, size_t databitlen, 741 uint8_t *hashval) 742 { 743 EdonRState state; 744 745 EdonRInit(&state, hashbitlen); 746 EdonRUpdate(&state, data, databitlen); 747 EdonRFinal(&state, hashval); 748 } 749 750 #ifdef _KERNEL 751 EXPORT_SYMBOL(EdonRInit); 752 EXPORT_SYMBOL(EdonRUpdate); 753 EXPORT_SYMBOL(EdonRHash); 754 EXPORT_SYMBOL(EdonRFinal); 755 #endif 756