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