191d68933SDavid Sterba // SPDX-License-Identifier: (GPL-2.0-only OR Apache-2.0)
291d68933SDavid Sterba /*
3*0cdc438eSEric Biggers * Generic implementation of the BLAKE2b digest algorithm. Based on the BLAKE2b
4*0cdc438eSEric Biggers * reference implementation, but it has been heavily modified for use in the
5*0cdc438eSEric Biggers * kernel. The reference implementation was:
691d68933SDavid Sterba *
7*0cdc438eSEric Biggers * Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under
8*0cdc438eSEric Biggers * the terms of the CC0, the OpenSSL Licence, or the Apache Public License
9*0cdc438eSEric Biggers * 2.0, at your option. The terms of these licenses can be found at:
1091d68933SDavid Sterba *
1191d68933SDavid Sterba * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
1291d68933SDavid Sterba * - OpenSSL license : https://www.openssl.org/source/license.html
139332a9e7SAlexander A. Klimov * - Apache 2.0 : https://www.apache.org/licenses/LICENSE-2.0
1491d68933SDavid Sterba *
15*0cdc438eSEric Biggers * More information about BLAKE2 can be found at https://blake2.net.
1691d68933SDavid Sterba */
1791d68933SDavid Sterba
1891d68933SDavid Sterba #include <asm/unaligned.h>
1991d68933SDavid Sterba #include <linux/module.h>
2091d68933SDavid Sterba #include <linux/kernel.h>
2191d68933SDavid Sterba #include <linux/bitops.h>
2228dcca4cSEric Biggers #include <crypto/internal/blake2b.h>
2391d68933SDavid Sterba #include <crypto/internal/hash.h>
2491d68933SDavid Sterba
2591d68933SDavid Sterba static const u8 blake2b_sigma[12][16] = {
2691d68933SDavid Sterba { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
2791d68933SDavid Sterba { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
2891d68933SDavid Sterba { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
2991d68933SDavid Sterba { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
3091d68933SDavid Sterba { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
3191d68933SDavid Sterba { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
3291d68933SDavid Sterba { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
3391d68933SDavid Sterba { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
3491d68933SDavid Sterba { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
3591d68933SDavid Sterba { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
3691d68933SDavid Sterba { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
3791d68933SDavid Sterba { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
3891d68933SDavid Sterba };
3991d68933SDavid Sterba
blake2b_increment_counter(struct blake2b_state * S,const u64 inc)4091d68933SDavid Sterba static void blake2b_increment_counter(struct blake2b_state *S, const u64 inc)
4191d68933SDavid Sterba {
4291d68933SDavid Sterba S->t[0] += inc;
4391d68933SDavid Sterba S->t[1] += (S->t[0] < inc);
4491d68933SDavid Sterba }
4591d68933SDavid Sterba
4691d68933SDavid Sterba #define G(r,i,a,b,c,d) \
4791d68933SDavid Sterba do { \
4891d68933SDavid Sterba a = a + b + m[blake2b_sigma[r][2*i+0]]; \
4991d68933SDavid Sterba d = ror64(d ^ a, 32); \
5091d68933SDavid Sterba c = c + d; \
5191d68933SDavid Sterba b = ror64(b ^ c, 24); \
5291d68933SDavid Sterba a = a + b + m[blake2b_sigma[r][2*i+1]]; \
5391d68933SDavid Sterba d = ror64(d ^ a, 16); \
5491d68933SDavid Sterba c = c + d; \
5591d68933SDavid Sterba b = ror64(b ^ c, 63); \
5691d68933SDavid Sterba } while (0)
5791d68933SDavid Sterba
5891d68933SDavid Sterba #define ROUND(r) \
5991d68933SDavid Sterba do { \
6091d68933SDavid Sterba G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
6191d68933SDavid Sterba G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
6291d68933SDavid Sterba G(r,2,v[ 2],v[ 6],v[10],v[14]); \
6391d68933SDavid Sterba G(r,3,v[ 3],v[ 7],v[11],v[15]); \
6491d68933SDavid Sterba G(r,4,v[ 0],v[ 5],v[10],v[15]); \
6591d68933SDavid Sterba G(r,5,v[ 1],v[ 6],v[11],v[12]); \
6691d68933SDavid Sterba G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
6791d68933SDavid Sterba G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
6891d68933SDavid Sterba } while (0)
6991d68933SDavid Sterba
blake2b_compress_one_generic(struct blake2b_state * S,const u8 block[BLAKE2B_BLOCK_SIZE])7028dcca4cSEric Biggers static void blake2b_compress_one_generic(struct blake2b_state *S,
7128dcca4cSEric Biggers const u8 block[BLAKE2B_BLOCK_SIZE])
7291d68933SDavid Sterba {
7391d68933SDavid Sterba u64 m[16];
7491d68933SDavid Sterba u64 v[16];
7591d68933SDavid Sterba size_t i;
7691d68933SDavid Sterba
7791d68933SDavid Sterba for (i = 0; i < 16; ++i)
7891d68933SDavid Sterba m[i] = get_unaligned_le64(block + i * sizeof(m[i]));
7991d68933SDavid Sterba
8091d68933SDavid Sterba for (i = 0; i < 8; ++i)
8191d68933SDavid Sterba v[i] = S->h[i];
8291d68933SDavid Sterba
8328dcca4cSEric Biggers v[ 8] = BLAKE2B_IV0;
8428dcca4cSEric Biggers v[ 9] = BLAKE2B_IV1;
8528dcca4cSEric Biggers v[10] = BLAKE2B_IV2;
8628dcca4cSEric Biggers v[11] = BLAKE2B_IV3;
8728dcca4cSEric Biggers v[12] = BLAKE2B_IV4 ^ S->t[0];
8828dcca4cSEric Biggers v[13] = BLAKE2B_IV5 ^ S->t[1];
8928dcca4cSEric Biggers v[14] = BLAKE2B_IV6 ^ S->f[0];
9028dcca4cSEric Biggers v[15] = BLAKE2B_IV7 ^ S->f[1];
9191d68933SDavid Sterba
9291d68933SDavid Sterba ROUND(0);
9391d68933SDavid Sterba ROUND(1);
9491d68933SDavid Sterba ROUND(2);
9591d68933SDavid Sterba ROUND(3);
9691d68933SDavid Sterba ROUND(4);
9791d68933SDavid Sterba ROUND(5);
9891d68933SDavid Sterba ROUND(6);
9991d68933SDavid Sterba ROUND(7);
10091d68933SDavid Sterba ROUND(8);
10191d68933SDavid Sterba ROUND(9);
10291d68933SDavid Sterba ROUND(10);
10391d68933SDavid Sterba ROUND(11);
1040c0408e8SArnd Bergmann #ifdef CONFIG_CC_IS_CLANG
1050c0408e8SArnd Bergmann #pragma nounroll /* https://bugs.llvm.org/show_bug.cgi?id=45803 */
1060c0408e8SArnd Bergmann #endif
10791d68933SDavid Sterba for (i = 0; i < 8; ++i)
10891d68933SDavid Sterba S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
10991d68933SDavid Sterba }
11091d68933SDavid Sterba
11191d68933SDavid Sterba #undef G
11291d68933SDavid Sterba #undef ROUND
11391d68933SDavid Sterba
blake2b_compress_generic(struct blake2b_state * state,const u8 * block,size_t nblocks,u32 inc)11428dcca4cSEric Biggers void blake2b_compress_generic(struct blake2b_state *state,
11528dcca4cSEric Biggers const u8 *block, size_t nblocks, u32 inc)
11691d68933SDavid Sterba {
11728dcca4cSEric Biggers do {
11828dcca4cSEric Biggers blake2b_increment_counter(state, inc);
11928dcca4cSEric Biggers blake2b_compress_one_generic(state, block);
12028dcca4cSEric Biggers block += BLAKE2B_BLOCK_SIZE;
12128dcca4cSEric Biggers } while (--nblocks);
12291d68933SDavid Sterba }
12328dcca4cSEric Biggers EXPORT_SYMBOL(blake2b_compress_generic);
12491d68933SDavid Sterba
crypto_blake2b_update_generic(struct shash_desc * desc,const u8 * in,unsigned int inlen)12528dcca4cSEric Biggers static int crypto_blake2b_update_generic(struct shash_desc *desc,
12628dcca4cSEric Biggers const u8 *in, unsigned int inlen)
12791d68933SDavid Sterba {
12828dcca4cSEric Biggers return crypto_blake2b_update(desc, in, inlen, blake2b_compress_generic);
12991d68933SDavid Sterba }
13091d68933SDavid Sterba
crypto_blake2b_final_generic(struct shash_desc * desc,u8 * out)13128dcca4cSEric Biggers static int crypto_blake2b_final_generic(struct shash_desc *desc, u8 *out)
13291d68933SDavid Sterba {
13328dcca4cSEric Biggers return crypto_blake2b_final(desc, out, blake2b_compress_generic);
13491d68933SDavid Sterba }
13591d68933SDavid Sterba
13628dcca4cSEric Biggers #define BLAKE2B_ALG(name, driver_name, digest_size) \
13728dcca4cSEric Biggers { \
13828dcca4cSEric Biggers .base.cra_name = name, \
13928dcca4cSEric Biggers .base.cra_driver_name = driver_name, \
14028dcca4cSEric Biggers .base.cra_priority = 100, \
14128dcca4cSEric Biggers .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY, \
14228dcca4cSEric Biggers .base.cra_blocksize = BLAKE2B_BLOCK_SIZE, \
14328dcca4cSEric Biggers .base.cra_ctxsize = sizeof(struct blake2b_tfm_ctx), \
14428dcca4cSEric Biggers .base.cra_module = THIS_MODULE, \
14528dcca4cSEric Biggers .digestsize = digest_size, \
14628dcca4cSEric Biggers .setkey = crypto_blake2b_setkey, \
14728dcca4cSEric Biggers .init = crypto_blake2b_init, \
14828dcca4cSEric Biggers .update = crypto_blake2b_update_generic, \
14928dcca4cSEric Biggers .final = crypto_blake2b_final_generic, \
15028dcca4cSEric Biggers .descsize = sizeof(struct blake2b_state), \
15191d68933SDavid Sterba }
15291d68933SDavid Sterba
15391d68933SDavid Sterba static struct shash_alg blake2b_algs[] = {
15428dcca4cSEric Biggers BLAKE2B_ALG("blake2b-160", "blake2b-160-generic",
15528dcca4cSEric Biggers BLAKE2B_160_HASH_SIZE),
15628dcca4cSEric Biggers BLAKE2B_ALG("blake2b-256", "blake2b-256-generic",
15728dcca4cSEric Biggers BLAKE2B_256_HASH_SIZE),
15828dcca4cSEric Biggers BLAKE2B_ALG("blake2b-384", "blake2b-384-generic",
15928dcca4cSEric Biggers BLAKE2B_384_HASH_SIZE),
16028dcca4cSEric Biggers BLAKE2B_ALG("blake2b-512", "blake2b-512-generic",
16128dcca4cSEric Biggers BLAKE2B_512_HASH_SIZE),
16291d68933SDavid Sterba };
16391d68933SDavid Sterba
blake2b_mod_init(void)16491d68933SDavid Sterba static int __init blake2b_mod_init(void)
16591d68933SDavid Sterba {
16691d68933SDavid Sterba return crypto_register_shashes(blake2b_algs, ARRAY_SIZE(blake2b_algs));
16791d68933SDavid Sterba }
16891d68933SDavid Sterba
blake2b_mod_fini(void)16991d68933SDavid Sterba static void __exit blake2b_mod_fini(void)
17091d68933SDavid Sterba {
17191d68933SDavid Sterba crypto_unregister_shashes(blake2b_algs, ARRAY_SIZE(blake2b_algs));
17291d68933SDavid Sterba }
17391d68933SDavid Sterba
17491d68933SDavid Sterba subsys_initcall(blake2b_mod_init);
17591d68933SDavid Sterba module_exit(blake2b_mod_fini);
17691d68933SDavid Sterba
17791d68933SDavid Sterba MODULE_AUTHOR("David Sterba <kdave@kernel.org>");
17891d68933SDavid Sterba MODULE_DESCRIPTION("BLAKE2b generic implementation");
17991d68933SDavid Sterba MODULE_LICENSE("GPL");
18091d68933SDavid Sterba MODULE_ALIAS_CRYPTO("blake2b-160");
18191d68933SDavid Sterba MODULE_ALIAS_CRYPTO("blake2b-160-generic");
18291d68933SDavid Sterba MODULE_ALIAS_CRYPTO("blake2b-256");
18391d68933SDavid Sterba MODULE_ALIAS_CRYPTO("blake2b-256-generic");
18491d68933SDavid Sterba MODULE_ALIAS_CRYPTO("blake2b-384");
18591d68933SDavid Sterba MODULE_ALIAS_CRYPTO("blake2b-384-generic");
18691d68933SDavid Sterba MODULE_ALIAS_CRYPTO("blake2b-512");
18791d68933SDavid Sterba MODULE_ALIAS_CRYPTO("blake2b-512-generic");
188