/* SPDX-License-Identifier: GPL-2.0 OR MIT */1/*2* Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved.3*/45#ifndef _CRYPTO_BLAKE2S_H6#define _CRYPTO_BLAKE2S_H78#include <linux/bug.h>9#include <linux/kconfig.h>10#include <linux/types.h>11#include <linux/string.h>1213enum blake2s_lengths {14BLAKE2S_BLOCK_SIZE = 64,15BLAKE2S_HASH_SIZE = 32,16BLAKE2S_KEY_SIZE = 32,1718BLAKE2S_128_HASH_SIZE = 16,19BLAKE2S_160_HASH_SIZE = 20,20BLAKE2S_224_HASH_SIZE = 28,21BLAKE2S_256_HASH_SIZE = 32,22};2324/**25* struct blake2s_ctx - Context for hashing a message with BLAKE2s26* @h: compression function state27* @t: block counter28* @f: finalization indicator29* @buf: partial block buffer; 'buflen' bytes are valid30* @buflen: number of bytes buffered in @buf31* @outlen: length of output hash value in bytes, at most BLAKE2S_HASH_SIZE32*/33struct blake2s_ctx {34/* 'h', 't', and 'f' are used in assembly code, so keep them as-is. */35u32 h[8];36u32 t[2];37u32 f[2];38u8 buf[BLAKE2S_BLOCK_SIZE];39unsigned int buflen;40unsigned int outlen;41};4243enum blake2s_iv {44BLAKE2S_IV0 = 0x6A09E667UL,45BLAKE2S_IV1 = 0xBB67AE85UL,46BLAKE2S_IV2 = 0x3C6EF372UL,47BLAKE2S_IV3 = 0xA54FF53AUL,48BLAKE2S_IV4 = 0x510E527FUL,49BLAKE2S_IV5 = 0x9B05688CUL,50BLAKE2S_IV6 = 0x1F83D9ABUL,51BLAKE2S_IV7 = 0x5BE0CD19UL,52};5354static inline void __blake2s_init(struct blake2s_ctx *ctx, size_t outlen,55const void *key, size_t keylen)56{57ctx->h[0] = BLAKE2S_IV0 ^ (0x01010000 | keylen << 8 | outlen);58ctx->h[1] = BLAKE2S_IV1;59ctx->h[2] = BLAKE2S_IV2;60ctx->h[3] = BLAKE2S_IV3;61ctx->h[4] = BLAKE2S_IV4;62ctx->h[5] = BLAKE2S_IV5;63ctx->h[6] = BLAKE2S_IV6;64ctx->h[7] = BLAKE2S_IV7;65ctx->t[0] = 0;66ctx->t[1] = 0;67ctx->f[0] = 0;68ctx->f[1] = 0;69ctx->buflen = 0;70ctx->outlen = outlen;71if (keylen) {72memcpy(ctx->buf, key, keylen);73memset(&ctx->buf[keylen], 0, BLAKE2S_BLOCK_SIZE - keylen);74ctx->buflen = BLAKE2S_BLOCK_SIZE;75}76}7778/**79* blake2s_init() - Initialize a BLAKE2s context for a new message (unkeyed)80* @ctx: the context to initialize81* @outlen: length of output hash value in bytes, at most BLAKE2S_HASH_SIZE82*83* Context: Any context.84*/85static inline void blake2s_init(struct blake2s_ctx *ctx, size_t outlen)86{87__blake2s_init(ctx, outlen, NULL, 0);88}8990/**91* blake2s_init_key() - Initialize a BLAKE2s context for a new message (keyed)92* @ctx: the context to initialize93* @outlen: length of output hash value in bytes, at most BLAKE2S_HASH_SIZE94* @key: the key95* @keylen: the key length in bytes, at most BLAKE2S_KEY_SIZE96*97* Context: Any context.98*/99static inline void blake2s_init_key(struct blake2s_ctx *ctx, size_t outlen,100const void *key, size_t keylen)101{102WARN_ON(IS_ENABLED(DEBUG) && (!outlen || outlen > BLAKE2S_HASH_SIZE ||103!key || !keylen || keylen > BLAKE2S_KEY_SIZE));104105__blake2s_init(ctx, outlen, key, keylen);106}107108/**109* blake2s_update() - Update a BLAKE2s context with message data110* @ctx: the context to update; must have been initialized111* @in: the message data112* @inlen: the data length in bytes113*114* This can be called any number of times.115*116* Context: Any context.117*/118void blake2s_update(struct blake2s_ctx *ctx, const u8 *in, size_t inlen);119120/**121* blake2s_final() - Finish computing a BLAKE2s hash122* @ctx: the context to finalize; must have been initialized123* @out: (output) the resulting BLAKE2s hash. Its length will be equal to the124* @outlen that was passed to blake2s_init() or blake2s_init_key().125*126* After finishing, this zeroizes @ctx. So the caller does not need to do it.127*128* Context: Any context.129*/130void blake2s_final(struct blake2s_ctx *ctx, u8 *out);131132/**133* blake2s() - Compute BLAKE2s hash in one shot134* @key: the key, or NULL for an unkeyed hash135* @keylen: the key length in bytes (at most BLAKE2S_KEY_SIZE), or 0 for an136* unkeyed hash137* @in: the message data138* @inlen: the data length in bytes139* @out: (output) the resulting BLAKE2s hash, with length @outlen140* @outlen: length of output hash value in bytes, at most BLAKE2S_HASH_SIZE141*142* Context: Any context.143*/144static inline void blake2s(const u8 *key, size_t keylen,145const u8 *in, size_t inlen,146u8 *out, size_t outlen)147{148struct blake2s_ctx ctx;149150WARN_ON(IS_ENABLED(DEBUG) && ((!in && inlen > 0) || !out || !outlen ||151outlen > BLAKE2S_HASH_SIZE || keylen > BLAKE2S_KEY_SIZE ||152(!key && keylen)));153154__blake2s_init(&ctx, outlen, key, keylen);155blake2s_update(&ctx, in, inlen);156blake2s_final(&ctx, out);157}158159#endif /* _CRYPTO_BLAKE2S_H */160161162