crypto: blake2b - sync with blake2s implementation
[platform/kernel/linux-starfive.git] / crypto / blake2b_generic.c
1 // SPDX-License-Identifier: (GPL-2.0-only OR Apache-2.0)
2 /*
3  * BLAKE2b reference source code package - reference C implementations
4  *
5  * Copyright 2012, Samuel Neves <sneves@dei.uc.pt>.  You may use this under the
6  * terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
7  * your option.  The terms of these licenses can be found at:
8  *
9  * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
10  * - OpenSSL license   : https://www.openssl.org/source/license.html
11  * - Apache 2.0        : https://www.apache.org/licenses/LICENSE-2.0
12  *
13  * More information about the BLAKE2 hash function can be found at
14  * https://blake2.net.
15  *
16  * Note: the original sources have been modified for inclusion in linux kernel
17  * in terms of coding style, using generic helpers and simplifications of error
18  * handling.
19  */
20
21 #include <asm/unaligned.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/bitops.h>
25 #include <crypto/internal/blake2b.h>
26 #include <crypto/internal/hash.h>
27
28 static const u8 blake2b_sigma[12][16] = {
29         {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15 },
30         { 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3 },
31         { 11,  8, 12,  0,  5,  2, 15, 13, 10, 14,  3,  6,  7,  1,  9,  4 },
32         {  7,  9,  3,  1, 13, 12, 11, 14,  2,  6,  5, 10,  4,  0, 15,  8 },
33         {  9,  0,  5,  7,  2,  4, 10, 15, 14,  1, 11, 12,  6,  8,  3, 13 },
34         {  2, 12,  6, 10,  0, 11,  8,  3,  4, 13,  7,  5, 15, 14,  1,  9 },
35         { 12,  5,  1, 15, 14, 13,  4, 10,  0,  7,  6,  3,  9,  2,  8, 11 },
36         { 13, 11,  7, 14, 12,  1,  3,  9,  5,  0, 15,  4,  8,  6,  2, 10 },
37         {  6, 15, 14,  9, 11,  3,  0,  8, 12,  2, 13,  7,  1,  4, 10,  5 },
38         { 10,  2,  8,  4,  7,  6,  1,  5, 15, 11,  9, 14,  3, 12, 13,  0 },
39         {  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15 },
40         { 14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3 }
41 };
42
43 static void blake2b_increment_counter(struct blake2b_state *S, const u64 inc)
44 {
45         S->t[0] += inc;
46         S->t[1] += (S->t[0] < inc);
47 }
48
49 #define G(r,i,a,b,c,d)                                  \
50         do {                                            \
51                 a = a + b + m[blake2b_sigma[r][2*i+0]]; \
52                 d = ror64(d ^ a, 32);                   \
53                 c = c + d;                              \
54                 b = ror64(b ^ c, 24);                   \
55                 a = a + b + m[blake2b_sigma[r][2*i+1]]; \
56                 d = ror64(d ^ a, 16);                   \
57                 c = c + d;                              \
58                 b = ror64(b ^ c, 63);                   \
59         } while (0)
60
61 #define ROUND(r)                                \
62         do {                                    \
63                 G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
64                 G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
65                 G(r,2,v[ 2],v[ 6],v[10],v[14]); \
66                 G(r,3,v[ 3],v[ 7],v[11],v[15]); \
67                 G(r,4,v[ 0],v[ 5],v[10],v[15]); \
68                 G(r,5,v[ 1],v[ 6],v[11],v[12]); \
69                 G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
70                 G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
71         } while (0)
72
73 static void blake2b_compress_one_generic(struct blake2b_state *S,
74                                          const u8 block[BLAKE2B_BLOCK_SIZE])
75 {
76         u64 m[16];
77         u64 v[16];
78         size_t i;
79
80         for (i = 0; i < 16; ++i)
81                 m[i] = get_unaligned_le64(block + i * sizeof(m[i]));
82
83         for (i = 0; i < 8; ++i)
84                 v[i] = S->h[i];
85
86         v[ 8] = BLAKE2B_IV0;
87         v[ 9] = BLAKE2B_IV1;
88         v[10] = BLAKE2B_IV2;
89         v[11] = BLAKE2B_IV3;
90         v[12] = BLAKE2B_IV4 ^ S->t[0];
91         v[13] = BLAKE2B_IV5 ^ S->t[1];
92         v[14] = BLAKE2B_IV6 ^ S->f[0];
93         v[15] = BLAKE2B_IV7 ^ S->f[1];
94
95         ROUND(0);
96         ROUND(1);
97         ROUND(2);
98         ROUND(3);
99         ROUND(4);
100         ROUND(5);
101         ROUND(6);
102         ROUND(7);
103         ROUND(8);
104         ROUND(9);
105         ROUND(10);
106         ROUND(11);
107 #ifdef CONFIG_CC_IS_CLANG
108 #pragma nounroll /* https://bugs.llvm.org/show_bug.cgi?id=45803 */
109 #endif
110         for (i = 0; i < 8; ++i)
111                 S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
112 }
113
114 #undef G
115 #undef ROUND
116
117 void blake2b_compress_generic(struct blake2b_state *state,
118                               const u8 *block, size_t nblocks, u32 inc)
119 {
120         do {
121                 blake2b_increment_counter(state, inc);
122                 blake2b_compress_one_generic(state, block);
123                 block += BLAKE2B_BLOCK_SIZE;
124         } while (--nblocks);
125 }
126 EXPORT_SYMBOL(blake2b_compress_generic);
127
128 static int crypto_blake2b_update_generic(struct shash_desc *desc,
129                                          const u8 *in, unsigned int inlen)
130 {
131         return crypto_blake2b_update(desc, in, inlen, blake2b_compress_generic);
132 }
133
134 static int crypto_blake2b_final_generic(struct shash_desc *desc, u8 *out)
135 {
136         return crypto_blake2b_final(desc, out, blake2b_compress_generic);
137 }
138
139 #define BLAKE2B_ALG(name, driver_name, digest_size)                     \
140         {                                                               \
141                 .base.cra_name          = name,                         \
142                 .base.cra_driver_name   = driver_name,                  \
143                 .base.cra_priority      = 100,                          \
144                 .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,      \
145                 .base.cra_blocksize     = BLAKE2B_BLOCK_SIZE,           \
146                 .base.cra_ctxsize       = sizeof(struct blake2b_tfm_ctx), \
147                 .base.cra_module        = THIS_MODULE,                  \
148                 .digestsize             = digest_size,                  \
149                 .setkey                 = crypto_blake2b_setkey,        \
150                 .init                   = crypto_blake2b_init,          \
151                 .update                 = crypto_blake2b_update_generic, \
152                 .final                  = crypto_blake2b_final_generic, \
153                 .descsize               = sizeof(struct blake2b_state), \
154         }
155
156 static struct shash_alg blake2b_algs[] = {
157         BLAKE2B_ALG("blake2b-160", "blake2b-160-generic",
158                     BLAKE2B_160_HASH_SIZE),
159         BLAKE2B_ALG("blake2b-256", "blake2b-256-generic",
160                     BLAKE2B_256_HASH_SIZE),
161         BLAKE2B_ALG("blake2b-384", "blake2b-384-generic",
162                     BLAKE2B_384_HASH_SIZE),
163         BLAKE2B_ALG("blake2b-512", "blake2b-512-generic",
164                     BLAKE2B_512_HASH_SIZE),
165 };
166
167 static int __init blake2b_mod_init(void)
168 {
169         return crypto_register_shashes(blake2b_algs, ARRAY_SIZE(blake2b_algs));
170 }
171
172 static void __exit blake2b_mod_fini(void)
173 {
174         crypto_unregister_shashes(blake2b_algs, ARRAY_SIZE(blake2b_algs));
175 }
176
177 subsys_initcall(blake2b_mod_init);
178 module_exit(blake2b_mod_fini);
179
180 MODULE_AUTHOR("David Sterba <kdave@kernel.org>");
181 MODULE_DESCRIPTION("BLAKE2b generic implementation");
182 MODULE_LICENSE("GPL");
183 MODULE_ALIAS_CRYPTO("blake2b-160");
184 MODULE_ALIAS_CRYPTO("blake2b-160-generic");
185 MODULE_ALIAS_CRYPTO("blake2b-256");
186 MODULE_ALIAS_CRYPTO("blake2b-256-generic");
187 MODULE_ALIAS_CRYPTO("blake2b-384");
188 MODULE_ALIAS_CRYPTO("blake2b-384-generic");
189 MODULE_ALIAS_CRYPTO("blake2b-512");
190 MODULE_ALIAS_CRYPTO("blake2b-512-generic");