/* SPDX-License-Identifier: GPL-2.0 OR MIT */12#ifndef _CRYPTO_BLAKE2B_H3#define _CRYPTO_BLAKE2B_H45#include <linux/bug.h>6#include <linux/types.h>7#include <linux/string.h>89enum blake2b_lengths {10BLAKE2B_BLOCK_SIZE = 128,11BLAKE2B_HASH_SIZE = 64,12BLAKE2B_KEY_SIZE = 64,1314BLAKE2B_160_HASH_SIZE = 20,15BLAKE2B_256_HASH_SIZE = 32,16BLAKE2B_384_HASH_SIZE = 48,17BLAKE2B_512_HASH_SIZE = 64,18};1920/**21* struct blake2b_ctx - Context for hashing a message with BLAKE2b22* @h: compression function state23* @t: block counter24* @f: finalization indicator25* @buf: partial block buffer; 'buflen' bytes are valid26* @buflen: number of bytes buffered in @buf27* @outlen: length of output hash value in bytes, at most BLAKE2B_HASH_SIZE28*/29struct blake2b_ctx {30/* 'h', 't', and 'f' are used in assembly code, so keep them as-is. */31u64 h[8];32u64 t[2];33u64 f[2];34u8 buf[BLAKE2B_BLOCK_SIZE];35unsigned int buflen;36unsigned int outlen;37};3839enum blake2b_iv {40BLAKE2B_IV0 = 0x6A09E667F3BCC908ULL,41BLAKE2B_IV1 = 0xBB67AE8584CAA73BULL,42BLAKE2B_IV2 = 0x3C6EF372FE94F82BULL,43BLAKE2B_IV3 = 0xA54FF53A5F1D36F1ULL,44BLAKE2B_IV4 = 0x510E527FADE682D1ULL,45BLAKE2B_IV5 = 0x9B05688C2B3E6C1FULL,46BLAKE2B_IV6 = 0x1F83D9ABFB41BD6BULL,47BLAKE2B_IV7 = 0x5BE0CD19137E2179ULL,48};4950static inline void __blake2b_init(struct blake2b_ctx *ctx, size_t outlen,51const void *key, size_t keylen)52{53ctx->h[0] = BLAKE2B_IV0 ^ (0x01010000 | keylen << 8 | outlen);54ctx->h[1] = BLAKE2B_IV1;55ctx->h[2] = BLAKE2B_IV2;56ctx->h[3] = BLAKE2B_IV3;57ctx->h[4] = BLAKE2B_IV4;58ctx->h[5] = BLAKE2B_IV5;59ctx->h[6] = BLAKE2B_IV6;60ctx->h[7] = BLAKE2B_IV7;61ctx->t[0] = 0;62ctx->t[1] = 0;63ctx->f[0] = 0;64ctx->f[1] = 0;65ctx->buflen = 0;66ctx->outlen = outlen;67if (keylen) {68memcpy(ctx->buf, key, keylen);69memset(&ctx->buf[keylen], 0, BLAKE2B_BLOCK_SIZE - keylen);70ctx->buflen = BLAKE2B_BLOCK_SIZE;71}72}7374/**75* blake2b_init() - Initialize a BLAKE2b context for a new message (unkeyed)76* @ctx: the context to initialize77* @outlen: length of output hash value in bytes, at most BLAKE2B_HASH_SIZE78*79* Context: Any context.80*/81static inline void blake2b_init(struct blake2b_ctx *ctx, size_t outlen)82{83__blake2b_init(ctx, outlen, NULL, 0);84}8586/**87* blake2b_init_key() - Initialize a BLAKE2b context for a new message (keyed)88* @ctx: the context to initialize89* @outlen: length of output hash value in bytes, at most BLAKE2B_HASH_SIZE90* @key: the key91* @keylen: the key length in bytes, at most BLAKE2B_KEY_SIZE92*93* Context: Any context.94*/95static inline void blake2b_init_key(struct blake2b_ctx *ctx, size_t outlen,96const void *key, size_t keylen)97{98WARN_ON(IS_ENABLED(DEBUG) && (!outlen || outlen > BLAKE2B_HASH_SIZE ||99!key || !keylen || keylen > BLAKE2B_KEY_SIZE));100101__blake2b_init(ctx, outlen, key, keylen);102}103104/**105* blake2b_update() - Update a BLAKE2b context with message data106* @ctx: the context to update; must have been initialized107* @in: the message data108* @inlen: the data length in bytes109*110* This can be called any number of times.111*112* Context: Any context.113*/114void blake2b_update(struct blake2b_ctx *ctx, const u8 *in, size_t inlen);115116/**117* blake2b_final() - Finish computing a BLAKE2b hash118* @ctx: the context to finalize; must have been initialized119* @out: (output) the resulting BLAKE2b hash. Its length will be equal to the120* @outlen that was passed to blake2b_init() or blake2b_init_key().121*122* After finishing, this zeroizes @ctx. So the caller does not need to do it.123*124* Context: Any context.125*/126void blake2b_final(struct blake2b_ctx *ctx, u8 *out);127128/**129* blake2b() - Compute BLAKE2b hash in one shot130* @key: the key, or NULL for an unkeyed hash131* @keylen: the key length in bytes (at most BLAKE2B_KEY_SIZE), or 0 for an132* unkeyed hash133* @in: the message data134* @inlen: the data length in bytes135* @out: (output) the resulting BLAKE2b hash, with length @outlen136* @outlen: length of output hash value in bytes, at most BLAKE2B_HASH_SIZE137*138* Context: Any context.139*/140static inline void blake2b(const u8 *key, size_t keylen,141const u8 *in, size_t inlen,142u8 *out, size_t outlen)143{144struct blake2b_ctx ctx;145146WARN_ON(IS_ENABLED(DEBUG) && ((!in && inlen > 0) || !out || !outlen ||147outlen > BLAKE2B_HASH_SIZE || keylen > BLAKE2B_KEY_SIZE ||148(!key && keylen)));149150__blake2b_init(&ctx, outlen, key, keylen);151blake2b_update(&ctx, in, inlen);152blake2b_final(&ctx, out);153}154155#endif /* _CRYPTO_BLAKE2B_H */156157158