1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /* 3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. 4 * 5 * This is an implementation of the BLAKE2s hash and PRF functions. 6 * 7 * Information: https://blake2.net/ 8 * 9 */ 10 11 #include <crypto/internal/blake2s.h> 12 #include <linux/types.h> 13 #include <linux/string.h> 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/init.h> 17 #include <linux/bug.h> 18 #include <asm/unaligned.h> 19 20 static const u8 blake2s_sigma[10][16] = { 21 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, 22 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }, 23 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 }, 24 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 }, 25 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 }, 26 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 }, 27 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 }, 28 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 }, 29 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 }, 30 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 }, 31 }; 32 33 static inline void blake2s_increment_counter(struct blake2s_state *state, 34 const u32 inc) 35 { 36 state->t[0] += inc; 37 state->t[1] += (state->t[0] < inc); 38 } 39 40 void blake2s_compress(struct blake2s_state *state, const u8 *block, 41 size_t nblocks, const u32 inc) 42 __weak __alias(blake2s_compress_generic); 43 44 void blake2s_compress_generic(struct blake2s_state *state, const u8 *block, 45 size_t nblocks, const u32 inc) 46 { 47 u32 m[16]; 48 u32 v[16]; 49 int i; 50 51 WARN_ON(IS_ENABLED(DEBUG) && 52 (nblocks > 1 && inc != BLAKE2S_BLOCK_SIZE)); 53 54 while (nblocks > 0) { 55 blake2s_increment_counter(state, inc); 56 memcpy(m, block, BLAKE2S_BLOCK_SIZE); 57 le32_to_cpu_array(m, ARRAY_SIZE(m)); 58 memcpy(v, state->h, 32); 59 v[ 8] = BLAKE2S_IV0; 60 v[ 9] = BLAKE2S_IV1; 61 v[10] = BLAKE2S_IV2; 62 v[11] = BLAKE2S_IV3; 63 v[12] = BLAKE2S_IV4 ^ state->t[0]; 64 v[13] = BLAKE2S_IV5 ^ state->t[1]; 65 v[14] = BLAKE2S_IV6 ^ state->f[0]; 66 v[15] = BLAKE2S_IV7 ^ state->f[1]; 67 68 #define G(r, i, a, b, c, d) do { \ 69 a += b + m[blake2s_sigma[r][2 * i + 0]]; \ 70 d = ror32(d ^ a, 16); \ 71 c += d; \ 72 b = ror32(b ^ c, 12); \ 73 a += b + m[blake2s_sigma[r][2 * i + 1]]; \ 74 d = ror32(d ^ a, 8); \ 75 c += d; \ 76 b = ror32(b ^ c, 7); \ 77 } while (0) 78 79 #define ROUND(r) do { \ 80 G(r, 0, v[0], v[ 4], v[ 8], v[12]); \ 81 G(r, 1, v[1], v[ 5], v[ 9], v[13]); \ 82 G(r, 2, v[2], v[ 6], v[10], v[14]); \ 83 G(r, 3, v[3], v[ 7], v[11], v[15]); \ 84 G(r, 4, v[0], v[ 5], v[10], v[15]); \ 85 G(r, 5, v[1], v[ 6], v[11], v[12]); \ 86 G(r, 6, v[2], v[ 7], v[ 8], v[13]); \ 87 G(r, 7, v[3], v[ 4], v[ 9], v[14]); \ 88 } while (0) 89 ROUND(0); 90 ROUND(1); 91 ROUND(2); 92 ROUND(3); 93 ROUND(4); 94 ROUND(5); 95 ROUND(6); 96 ROUND(7); 97 ROUND(8); 98 ROUND(9); 99 100 #undef G 101 #undef ROUND 102 103 for (i = 0; i < 8; ++i) 104 state->h[i] ^= v[i] ^ v[i + 8]; 105 106 block += BLAKE2S_BLOCK_SIZE; 107 --nblocks; 108 } 109 } 110 111 EXPORT_SYMBOL(blake2s_compress_generic); 112 113 MODULE_LICENSE("GPL v2"); 114 MODULE_DESCRIPTION("BLAKE2s hash function"); 115 MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>"); 116