blob: e3aa6e7ff3d83af1382b09e1ca502cbc7eda289f [file] [log] [blame]
Ard Biesheuvel7f9b0882019-11-08 13:22:30 +01001// 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
15static 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
Eric Biggers674f3682019-12-30 21:19:36 -060020 if (keylen == 0 || keylen > BLAKE2S_KEY_SIZE)
Ard Biesheuvel7f9b0882019-11-08 13:22:30 +010021 return -EINVAL;
Ard Biesheuvel7f9b0882019-11-08 13:22:30 +010022
23 memcpy(tctx->key, key, keylen);
24 tctx->keylen = keylen;
25
26 return 0;
27}
28
29static int crypto_blake2s_init(struct shash_desc *desc)
30{
31 struct blake2s_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
32 struct blake2s_state *state = shash_desc_ctx(desc);
33 const int outlen = crypto_shash_digestsize(desc->tfm);
34
35 if (tctx->keylen)
36 blake2s_init_key(state, outlen, tctx->key, tctx->keylen);
37 else
38 blake2s_init(state, outlen);
39
40 return 0;
41}
42
43static int crypto_blake2s_update(struct shash_desc *desc, const u8 *in,
44 unsigned int inlen)
45{
46 struct blake2s_state *state = shash_desc_ctx(desc);
47 const size_t fill = BLAKE2S_BLOCK_SIZE - state->buflen;
48
49 if (unlikely(!inlen))
50 return 0;
51 if (inlen > fill) {
52 memcpy(state->buf + state->buflen, in, fill);
53 blake2s_compress_generic(state, state->buf, 1, BLAKE2S_BLOCK_SIZE);
54 state->buflen = 0;
55 in += fill;
56 inlen -= fill;
57 }
58 if (inlen > BLAKE2S_BLOCK_SIZE) {
59 const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2S_BLOCK_SIZE);
60 /* Hash one less (full) block than strictly possible */
61 blake2s_compress_generic(state, in, nblocks - 1, BLAKE2S_BLOCK_SIZE);
62 in += BLAKE2S_BLOCK_SIZE * (nblocks - 1);
63 inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1);
64 }
65 memcpy(state->buf + state->buflen, in, inlen);
66 state->buflen += inlen;
67
68 return 0;
69}
70
71static int crypto_blake2s_final(struct shash_desc *desc, u8 *out)
72{
73 struct blake2s_state *state = shash_desc_ctx(desc);
74
75 blake2s_set_lastblock(state);
76 memset(state->buf + state->buflen, 0,
77 BLAKE2S_BLOCK_SIZE - state->buflen); /* Padding */
78 blake2s_compress_generic(state, state->buf, 1, state->buflen);
79 cpu_to_le32_array(state->h, ARRAY_SIZE(state->h));
80 memcpy(out, state->h, state->outlen);
81 memzero_explicit(state, sizeof(*state));
82
83 return 0;
84}
85
Eric Biggers0d396052020-12-23 00:09:50 -080086#define BLAKE2S_ALG(name, driver_name, digest_size) \
87 { \
88 .base.cra_name = name, \
89 .base.cra_driver_name = driver_name, \
90 .base.cra_priority = 100, \
91 .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY, \
92 .base.cra_blocksize = BLAKE2S_BLOCK_SIZE, \
93 .base.cra_ctxsize = sizeof(struct blake2s_tfm_ctx), \
94 .base.cra_module = THIS_MODULE, \
95 .digestsize = digest_size, \
96 .setkey = crypto_blake2s_setkey, \
97 .init = crypto_blake2s_init, \
98 .update = crypto_blake2s_update, \
99 .final = crypto_blake2s_final, \
100 .descsize = sizeof(struct blake2s_state), \
101 }
Ard Biesheuvel7f9b0882019-11-08 13:22:30 +0100102
Eric Biggers0d396052020-12-23 00:09:50 -0800103static struct shash_alg blake2s_algs[] = {
104 BLAKE2S_ALG("blake2s-128", "blake2s-128-generic",
105 BLAKE2S_128_HASH_SIZE),
106 BLAKE2S_ALG("blake2s-160", "blake2s-160-generic",
107 BLAKE2S_160_HASH_SIZE),
108 BLAKE2S_ALG("blake2s-224", "blake2s-224-generic",
109 BLAKE2S_224_HASH_SIZE),
110 BLAKE2S_ALG("blake2s-256", "blake2s-256-generic",
111 BLAKE2S_256_HASH_SIZE),
112};
Ard Biesheuvel7f9b0882019-11-08 13:22:30 +0100113
114static int __init blake2s_mod_init(void)
115{
116 return crypto_register_shashes(blake2s_algs, ARRAY_SIZE(blake2s_algs));
117}
118
119static void __exit blake2s_mod_exit(void)
120{
121 crypto_unregister_shashes(blake2s_algs, ARRAY_SIZE(blake2s_algs));
122}
123
124subsys_initcall(blake2s_mod_init);
125module_exit(blake2s_mod_exit);
126
127MODULE_ALIAS_CRYPTO("blake2s-128");
128MODULE_ALIAS_CRYPTO("blake2s-128-generic");
129MODULE_ALIAS_CRYPTO("blake2s-160");
130MODULE_ALIAS_CRYPTO("blake2s-160-generic");
131MODULE_ALIAS_CRYPTO("blake2s-224");
132MODULE_ALIAS_CRYPTO("blake2s-224-generic");
133MODULE_ALIAS_CRYPTO("blake2s-256");
134MODULE_ALIAS_CRYPTO("blake2s-256-generic");
135MODULE_LICENSE("GPL v2");