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/blake2s.h> 12 #include <linux/bug.h> 13 #include <linux/export.h> 14 #include <linux/kernel.h> 15 #include <linux/module.h> 16 #include <linux/string.h> 17 #include <linux/unroll.h> 18 #include <linux/types.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_ctx *ctx, u32 inc) 34 { 35 ctx->t[0] += inc; 36 ctx->t[1] += (ctx->t[0] < inc); 37 } 38 39 static void __maybe_unused 40 blake2s_compress_generic(struct blake2s_ctx *ctx, 41 const u8 *data, size_t nblocks, u32 inc) 42 { 43 u32 m[16]; 44 u32 v[16]; 45 int i; 46 47 WARN_ON(IS_ENABLED(DEBUG) && 48 (nblocks > 1 && inc != BLAKE2S_BLOCK_SIZE)); 49 50 while (nblocks > 0) { 51 blake2s_increment_counter(ctx, inc); 52 memcpy(m, data, BLAKE2S_BLOCK_SIZE); 53 le32_to_cpu_array(m, ARRAY_SIZE(m)); 54 memcpy(v, ctx->h, 32); 55 v[ 8] = BLAKE2S_IV0; 56 v[ 9] = BLAKE2S_IV1; 57 v[10] = BLAKE2S_IV2; 58 v[11] = BLAKE2S_IV3; 59 v[12] = BLAKE2S_IV4 ^ ctx->t[0]; 60 v[13] = BLAKE2S_IV5 ^ ctx->t[1]; 61 v[14] = BLAKE2S_IV6 ^ ctx->f[0]; 62 v[15] = BLAKE2S_IV7 ^ ctx->f[1]; 63 64 #define G(r, i, a, b, c, d) do { \ 65 a += b + m[blake2s_sigma[r][2 * i + 0]]; \ 66 d = ror32(d ^ a, 16); \ 67 c += d; \ 68 b = ror32(b ^ c, 12); \ 69 a += b + m[blake2s_sigma[r][2 * i + 1]]; \ 70 d = ror32(d ^ a, 8); \ 71 c += d; \ 72 b = ror32(b ^ c, 7); \ 73 } while (0) 74 75 /* 76 * Unroll the rounds loop to enable constant-folding of the 77 * blake2s_sigma values. 78 */ 79 unrolled_full 80 for (int r = 0; r < 10; r++) { 81 G(r, 0, v[0], v[4], v[8], v[12]); 82 G(r, 1, v[1], v[5], v[9], v[13]); 83 G(r, 2, v[2], v[6], v[10], v[14]); 84 G(r, 3, v[3], v[7], v[11], v[15]); 85 G(r, 4, v[0], v[5], v[10], v[15]); 86 G(r, 5, v[1], v[6], v[11], v[12]); 87 G(r, 6, v[2], v[7], v[8], v[13]); 88 G(r, 7, v[3], v[4], v[9], v[14]); 89 } 90 #undef G 91 92 for (i = 0; i < 8; ++i) 93 ctx->h[i] ^= v[i] ^ v[i + 8]; 94 95 data += BLAKE2S_BLOCK_SIZE; 96 --nblocks; 97 } 98 } 99 100 #ifdef CONFIG_CRYPTO_LIB_BLAKE2S_ARCH 101 #include "blake2s.h" /* $(SRCARCH)/blake2s.h */ 102 #else 103 #define blake2s_compress blake2s_compress_generic 104 #endif 105 106 static inline void blake2s_set_lastblock(struct blake2s_ctx *ctx) 107 { 108 ctx->f[0] = -1; 109 } 110 111 void blake2s_update(struct blake2s_ctx *ctx, const u8 *in, size_t inlen) 112 { 113 const size_t fill = BLAKE2S_BLOCK_SIZE - ctx->buflen; 114 115 if (unlikely(!inlen)) 116 return; 117 if (inlen > fill) { 118 memcpy(ctx->buf + ctx->buflen, in, fill); 119 blake2s_compress(ctx, ctx->buf, 1, BLAKE2S_BLOCK_SIZE); 120 ctx->buflen = 0; 121 in += fill; 122 inlen -= fill; 123 } 124 if (inlen > BLAKE2S_BLOCK_SIZE) { 125 const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2S_BLOCK_SIZE); 126 127 blake2s_compress(ctx, in, nblocks - 1, BLAKE2S_BLOCK_SIZE); 128 in += BLAKE2S_BLOCK_SIZE * (nblocks - 1); 129 inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1); 130 } 131 memcpy(ctx->buf + ctx->buflen, in, inlen); 132 ctx->buflen += inlen; 133 } 134 EXPORT_SYMBOL(blake2s_update); 135 136 void blake2s_final(struct blake2s_ctx *ctx, u8 *out) 137 { 138 WARN_ON(IS_ENABLED(DEBUG) && !out); 139 blake2s_set_lastblock(ctx); 140 memset(ctx->buf + ctx->buflen, 0, 141 BLAKE2S_BLOCK_SIZE - ctx->buflen); /* Padding */ 142 blake2s_compress(ctx, ctx->buf, 1, ctx->buflen); 143 cpu_to_le32_array(ctx->h, ARRAY_SIZE(ctx->h)); 144 memcpy(out, ctx->h, ctx->outlen); 145 memzero_explicit(ctx, sizeof(*ctx)); 146 } 147 EXPORT_SYMBOL(blake2s_final); 148 149 #ifdef blake2s_mod_init_arch 150 static int __init blake2s_mod_init(void) 151 { 152 blake2s_mod_init_arch(); 153 return 0; 154 } 155 subsys_initcall(blake2s_mod_init); 156 #endif 157 158 MODULE_DESCRIPTION("BLAKE2s hash function"); 159 MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>"); 160