blake2s_transform.c (2419B)
1 /* ISC license. */ 2 3 #include <stdint.h> 4 5 #include <skalibs/uint32.h> 6 #include <skalibs/blake2s.h> 7 #include "blake2s-internal.h" 8 9 static uint32_t const blake2s_iv[8] = 10 { 11 0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL, 12 0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL 13 }; 14 15 static uint8_t const blake2s_sigma[10][16] = 16 { 17 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, 18 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }, 19 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 }, 20 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 }, 21 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 }, 22 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 }, 23 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 }, 24 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 }, 25 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 }, 26 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 } 27 } ; 28 29 static uint32_t ror32 (uint32_t word, unsigned int shift) 30 { 31 return (word >> (shift & 31)) | (word << ((-shift) & 31)) ; 32 } 33 34 #define G(r, i, a, b, c, d) do \ 35 { \ 36 a += b + m[blake2s_sigma[r][i<<1]] ; \ 37 d = ror32(d ^ a, 16) ; \ 38 c += d ; \ 39 b = ror32(b ^ c, 12) ; \ 40 a += b + m[blake2s_sigma[r][(i<<1)+1]] ; \ 41 d = ror32(d ^ a, 8) ; \ 42 c += d ; \ 43 b = ror32(b ^ c, 7) ; \ 44 } while (0) 45 46 void blake2s_transform (blake2s_ctx *ctx, char const *block, size_t n, uint32_t inc) 47 { 48 uint32_t m[16] ; 49 uint32_t v[16] ; 50 51 while (n--) 52 { 53 unsigned int i = 0 ; 54 ctx->t[0] += inc ; 55 ctx->t[1] += (ctx->t[0] < inc) ; 56 for (; i < 16 ; i++) uint32_unpack(block + (i << 2), m + i) ; 57 for (i = 0 ; i < 8 ; i++) v[i] = ctx->h[i] ; 58 v[8] = blake2s_iv[0] ; 59 v[9] = blake2s_iv[1] ; 60 v[10] = blake2s_iv[2] ; 61 v[11] = blake2s_iv[3] ; 62 v[12] = blake2s_iv[4] ^ ctx->t[0] ; 63 v[13] = blake2s_iv[5] ^ ctx->t[1] ; 64 v[14] = blake2s_iv[6] ^ ctx->f[0] ; 65 v[15] = blake2s_iv[7] ^ ctx->f[1] ; 66 67 for (i = 0 ; i < 10 ; i++) 68 { 69 G(i, 0, v[0], v[ 4], v[ 8], v[12]) ; 70 G(i, 1, v[1], v[ 5], v[ 9], v[13]) ; 71 G(i, 2, v[2], v[ 6], v[10], v[14]) ; 72 G(i, 3, v[3], v[ 7], v[11], v[15]) ; 73 G(i, 4, v[0], v[ 5], v[10], v[15]) ; 74 G(i, 5, v[1], v[ 6], v[11], v[12]) ; 75 G(i, 6, v[2], v[ 7], v[ 8], v[13]) ; 76 G(i, 7, v[3], v[ 4], v[ 9], v[14]) ; 77 } 78 79 for (i = 0 ; i < 8 ; i++) ctx->h[i] ^= v[i] ^ v[i+8] ; 80 block += 64 ; 81 } 82 }