0001
0002
0003
0004
0005
0006
0007 #ifndef _CRYPTO_INTERNAL_BLAKE2B_H
0008 #define _CRYPTO_INTERNAL_BLAKE2B_H
0009
0010 #include <crypto/blake2b.h>
0011 #include <crypto/internal/hash.h>
0012 #include <linux/string.h>
0013
0014 void blake2b_compress_generic(struct blake2b_state *state,
0015 const u8 *block, size_t nblocks, u32 inc);
0016
0017 static inline void blake2b_set_lastblock(struct blake2b_state *state)
0018 {
0019 state->f[0] = -1;
0020 }
0021
0022 typedef void (*blake2b_compress_t)(struct blake2b_state *state,
0023 const u8 *block, size_t nblocks, u32 inc);
0024
0025 static inline void __blake2b_update(struct blake2b_state *state,
0026 const u8 *in, size_t inlen,
0027 blake2b_compress_t compress)
0028 {
0029 const size_t fill = BLAKE2B_BLOCK_SIZE - state->buflen;
0030
0031 if (unlikely(!inlen))
0032 return;
0033 if (inlen > fill) {
0034 memcpy(state->buf + state->buflen, in, fill);
0035 (*compress)(state, state->buf, 1, BLAKE2B_BLOCK_SIZE);
0036 state->buflen = 0;
0037 in += fill;
0038 inlen -= fill;
0039 }
0040 if (inlen > BLAKE2B_BLOCK_SIZE) {
0041 const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2B_BLOCK_SIZE);
0042
0043 (*compress)(state, in, nblocks - 1, BLAKE2B_BLOCK_SIZE);
0044 in += BLAKE2B_BLOCK_SIZE * (nblocks - 1);
0045 inlen -= BLAKE2B_BLOCK_SIZE * (nblocks - 1);
0046 }
0047 memcpy(state->buf + state->buflen, in, inlen);
0048 state->buflen += inlen;
0049 }
0050
0051 static inline void __blake2b_final(struct blake2b_state *state, u8 *out,
0052 blake2b_compress_t compress)
0053 {
0054 int i;
0055
0056 blake2b_set_lastblock(state);
0057 memset(state->buf + state->buflen, 0,
0058 BLAKE2B_BLOCK_SIZE - state->buflen);
0059 (*compress)(state, state->buf, 1, state->buflen);
0060 for (i = 0; i < ARRAY_SIZE(state->h); i++)
0061 __cpu_to_le64s(&state->h[i]);
0062 memcpy(out, state->h, state->outlen);
0063 }
0064
0065
0066
0067 struct blake2b_tfm_ctx {
0068 u8 key[BLAKE2B_KEY_SIZE];
0069 unsigned int keylen;
0070 };
0071
0072 static inline int crypto_blake2b_setkey(struct crypto_shash *tfm,
0073 const u8 *key, unsigned int keylen)
0074 {
0075 struct blake2b_tfm_ctx *tctx = crypto_shash_ctx(tfm);
0076
0077 if (keylen == 0 || keylen > BLAKE2B_KEY_SIZE)
0078 return -EINVAL;
0079
0080 memcpy(tctx->key, key, keylen);
0081 tctx->keylen = keylen;
0082
0083 return 0;
0084 }
0085
0086 static inline int crypto_blake2b_init(struct shash_desc *desc)
0087 {
0088 const struct blake2b_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
0089 struct blake2b_state *state = shash_desc_ctx(desc);
0090 unsigned int outlen = crypto_shash_digestsize(desc->tfm);
0091
0092 __blake2b_init(state, outlen, tctx->key, tctx->keylen);
0093 return 0;
0094 }
0095
0096 static inline int crypto_blake2b_update(struct shash_desc *desc,
0097 const u8 *in, unsigned int inlen,
0098 blake2b_compress_t compress)
0099 {
0100 struct blake2b_state *state = shash_desc_ctx(desc);
0101
0102 __blake2b_update(state, in, inlen, compress);
0103 return 0;
0104 }
0105
0106 static inline int crypto_blake2b_final(struct shash_desc *desc, u8 *out,
0107 blake2b_compress_t compress)
0108 {
0109 struct blake2b_state *state = shash_desc_ctx(desc);
0110
0111 __blake2b_final(state, out, compress);
0112 return 0;
0113 }
0114
0115 #endif