Merge tag 'wireless-drivers-2019-12-17' of git://git.kernel.org/pub/scm/linux/kernel...
[platform/kernel/linux-starfive.git] / crypto / blake2s_generic.c
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
6 #include <crypto/internal/blake2s.h>
7 #include <crypto/internal/simd.h>
8 #include <crypto/internal/hash.h>
9
10 #include <linux/types.h>
11 #include <linux/jump_label.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14
15 static int crypto_blake2s_setkey(struct crypto_shash *tfm, const u8 *key,
16                                  unsigned int keylen)
17 {
18         struct blake2s_tfm_ctx *tctx = crypto_shash_ctx(tfm);
19
20         if (keylen == 0 || keylen > BLAKE2S_KEY_SIZE) {
21                 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
22                 return -EINVAL;
23         }
24
25         memcpy(tctx->key, key, keylen);
26         tctx->keylen = keylen;
27
28         return 0;
29 }
30
31 static int crypto_blake2s_init(struct shash_desc *desc)
32 {
33         struct blake2s_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
34         struct blake2s_state *state = shash_desc_ctx(desc);
35         const int outlen = crypto_shash_digestsize(desc->tfm);
36
37         if (tctx->keylen)
38                 blake2s_init_key(state, outlen, tctx->key, tctx->keylen);
39         else
40                 blake2s_init(state, outlen);
41
42         return 0;
43 }
44
45 static int crypto_blake2s_update(struct shash_desc *desc, const u8 *in,
46                                  unsigned int inlen)
47 {
48         struct blake2s_state *state = shash_desc_ctx(desc);
49         const size_t fill = BLAKE2S_BLOCK_SIZE - state->buflen;
50
51         if (unlikely(!inlen))
52                 return 0;
53         if (inlen > fill) {
54                 memcpy(state->buf + state->buflen, in, fill);
55                 blake2s_compress_generic(state, state->buf, 1, BLAKE2S_BLOCK_SIZE);
56                 state->buflen = 0;
57                 in += fill;
58                 inlen -= fill;
59         }
60         if (inlen > BLAKE2S_BLOCK_SIZE) {
61                 const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2S_BLOCK_SIZE);
62                 /* Hash one less (full) block than strictly possible */
63                 blake2s_compress_generic(state, in, nblocks - 1, BLAKE2S_BLOCK_SIZE);
64                 in += BLAKE2S_BLOCK_SIZE * (nblocks - 1);
65                 inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1);
66         }
67         memcpy(state->buf + state->buflen, in, inlen);
68         state->buflen += inlen;
69
70         return 0;
71 }
72
73 static int crypto_blake2s_final(struct shash_desc *desc, u8 *out)
74 {
75         struct blake2s_state *state = shash_desc_ctx(desc);
76
77         blake2s_set_lastblock(state);
78         memset(state->buf + state->buflen, 0,
79                BLAKE2S_BLOCK_SIZE - state->buflen); /* Padding */
80         blake2s_compress_generic(state, state->buf, 1, state->buflen);
81         cpu_to_le32_array(state->h, ARRAY_SIZE(state->h));
82         memcpy(out, state->h, state->outlen);
83         memzero_explicit(state, sizeof(*state));
84
85         return 0;
86 }
87
88 static struct shash_alg blake2s_algs[] = {{
89         .base.cra_name          = "blake2s-128",
90         .base.cra_driver_name   = "blake2s-128-generic",
91         .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
92         .base.cra_ctxsize       = sizeof(struct blake2s_tfm_ctx),
93         .base.cra_priority      = 200,
94         .base.cra_blocksize     = BLAKE2S_BLOCK_SIZE,
95         .base.cra_module        = THIS_MODULE,
96
97         .digestsize             = BLAKE2S_128_HASH_SIZE,
98         .setkey                 = crypto_blake2s_setkey,
99         .init                   = crypto_blake2s_init,
100         .update                 = crypto_blake2s_update,
101         .final                  = crypto_blake2s_final,
102         .descsize               = sizeof(struct blake2s_state),
103 }, {
104         .base.cra_name          = "blake2s-160",
105         .base.cra_driver_name   = "blake2s-160-generic",
106         .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
107         .base.cra_ctxsize       = sizeof(struct blake2s_tfm_ctx),
108         .base.cra_priority      = 200,
109         .base.cra_blocksize     = BLAKE2S_BLOCK_SIZE,
110         .base.cra_module        = THIS_MODULE,
111
112         .digestsize             = BLAKE2S_160_HASH_SIZE,
113         .setkey                 = crypto_blake2s_setkey,
114         .init                   = crypto_blake2s_init,
115         .update                 = crypto_blake2s_update,
116         .final                  = crypto_blake2s_final,
117         .descsize               = sizeof(struct blake2s_state),
118 }, {
119         .base.cra_name          = "blake2s-224",
120         .base.cra_driver_name   = "blake2s-224-generic",
121         .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
122         .base.cra_ctxsize       = sizeof(struct blake2s_tfm_ctx),
123         .base.cra_priority      = 200,
124         .base.cra_blocksize     = BLAKE2S_BLOCK_SIZE,
125         .base.cra_module        = THIS_MODULE,
126
127         .digestsize             = BLAKE2S_224_HASH_SIZE,
128         .setkey                 = crypto_blake2s_setkey,
129         .init                   = crypto_blake2s_init,
130         .update                 = crypto_blake2s_update,
131         .final                  = crypto_blake2s_final,
132         .descsize               = sizeof(struct blake2s_state),
133 }, {
134         .base.cra_name          = "blake2s-256",
135         .base.cra_driver_name   = "blake2s-256-generic",
136         .base.cra_flags         = CRYPTO_ALG_OPTIONAL_KEY,
137         .base.cra_ctxsize       = sizeof(struct blake2s_tfm_ctx),
138         .base.cra_priority      = 200,
139         .base.cra_blocksize     = BLAKE2S_BLOCK_SIZE,
140         .base.cra_module        = THIS_MODULE,
141
142         .digestsize             = BLAKE2S_256_HASH_SIZE,
143         .setkey                 = crypto_blake2s_setkey,
144         .init                   = crypto_blake2s_init,
145         .update                 = crypto_blake2s_update,
146         .final                  = crypto_blake2s_final,
147         .descsize               = sizeof(struct blake2s_state),
148 }};
149
150 static int __init blake2s_mod_init(void)
151 {
152         return crypto_register_shashes(blake2s_algs, ARRAY_SIZE(blake2s_algs));
153 }
154
155 static void __exit blake2s_mod_exit(void)
156 {
157         crypto_unregister_shashes(blake2s_algs, ARRAY_SIZE(blake2s_algs));
158 }
159
160 subsys_initcall(blake2s_mod_init);
161 module_exit(blake2s_mod_exit);
162
163 MODULE_ALIAS_CRYPTO("blake2s-128");
164 MODULE_ALIAS_CRYPTO("blake2s-128-generic");
165 MODULE_ALIAS_CRYPTO("blake2s-160");
166 MODULE_ALIAS_CRYPTO("blake2s-160-generic");
167 MODULE_ALIAS_CRYPTO("blake2s-224");
168 MODULE_ALIAS_CRYPTO("blake2s-224-generic");
169 MODULE_ALIAS_CRYPTO("blake2s-256");
170 MODULE_ALIAS_CRYPTO("blake2s-256-generic");
171 MODULE_LICENSE("GPL v2");