Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
0002 /*
0003  * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
0004  */
0005 
0006 #ifndef _CRYPTO_BLAKE2S_H
0007 #define _CRYPTO_BLAKE2S_H
0008 
0009 #include <linux/bug.h>
0010 #include <linux/kconfig.h>
0011 #include <linux/types.h>
0012 #include <linux/string.h>
0013 
0014 enum blake2s_lengths {
0015     BLAKE2S_BLOCK_SIZE = 64,
0016     BLAKE2S_HASH_SIZE = 32,
0017     BLAKE2S_KEY_SIZE = 32,
0018 
0019     BLAKE2S_128_HASH_SIZE = 16,
0020     BLAKE2S_160_HASH_SIZE = 20,
0021     BLAKE2S_224_HASH_SIZE = 28,
0022     BLAKE2S_256_HASH_SIZE = 32,
0023 };
0024 
0025 struct blake2s_state {
0026     /* 'h', 't', and 'f' are used in assembly code, so keep them as-is. */
0027     u32 h[8];
0028     u32 t[2];
0029     u32 f[2];
0030     u8 buf[BLAKE2S_BLOCK_SIZE];
0031     unsigned int buflen;
0032     unsigned int outlen;
0033 };
0034 
0035 enum blake2s_iv {
0036     BLAKE2S_IV0 = 0x6A09E667UL,
0037     BLAKE2S_IV1 = 0xBB67AE85UL,
0038     BLAKE2S_IV2 = 0x3C6EF372UL,
0039     BLAKE2S_IV3 = 0xA54FF53AUL,
0040     BLAKE2S_IV4 = 0x510E527FUL,
0041     BLAKE2S_IV5 = 0x9B05688CUL,
0042     BLAKE2S_IV6 = 0x1F83D9ABUL,
0043     BLAKE2S_IV7 = 0x5BE0CD19UL,
0044 };
0045 
0046 static inline void __blake2s_init(struct blake2s_state *state, size_t outlen,
0047                   const void *key, size_t keylen)
0048 {
0049     state->h[0] = BLAKE2S_IV0 ^ (0x01010000 | keylen << 8 | outlen);
0050     state->h[1] = BLAKE2S_IV1;
0051     state->h[2] = BLAKE2S_IV2;
0052     state->h[3] = BLAKE2S_IV3;
0053     state->h[4] = BLAKE2S_IV4;
0054     state->h[5] = BLAKE2S_IV5;
0055     state->h[6] = BLAKE2S_IV6;
0056     state->h[7] = BLAKE2S_IV7;
0057     state->t[0] = 0;
0058     state->t[1] = 0;
0059     state->f[0] = 0;
0060     state->f[1] = 0;
0061     state->buflen = 0;
0062     state->outlen = outlen;
0063     if (keylen) {
0064         memcpy(state->buf, key, keylen);
0065         memset(&state->buf[keylen], 0, BLAKE2S_BLOCK_SIZE - keylen);
0066         state->buflen = BLAKE2S_BLOCK_SIZE;
0067     }
0068 }
0069 
0070 static inline void blake2s_init(struct blake2s_state *state,
0071                 const size_t outlen)
0072 {
0073     __blake2s_init(state, outlen, NULL, 0);
0074 }
0075 
0076 static inline void blake2s_init_key(struct blake2s_state *state,
0077                     const size_t outlen, const void *key,
0078                     const size_t keylen)
0079 {
0080     WARN_ON(IS_ENABLED(DEBUG) && (!outlen || outlen > BLAKE2S_HASH_SIZE ||
0081         !key || !keylen || keylen > BLAKE2S_KEY_SIZE));
0082 
0083     __blake2s_init(state, outlen, key, keylen);
0084 }
0085 
0086 void blake2s_update(struct blake2s_state *state, const u8 *in, size_t inlen);
0087 void blake2s_final(struct blake2s_state *state, u8 *out);
0088 
0089 static inline void blake2s(u8 *out, const u8 *in, const u8 *key,
0090                const size_t outlen, const size_t inlen,
0091                const size_t keylen)
0092 {
0093     struct blake2s_state state;
0094 
0095     WARN_ON(IS_ENABLED(DEBUG) && ((!in && inlen > 0) || !out || !outlen ||
0096         outlen > BLAKE2S_HASH_SIZE || keylen > BLAKE2S_KEY_SIZE ||
0097         (!key && keylen)));
0098 
0099     __blake2s_init(&state, outlen, key, keylen);
0100     blake2s_update(&state, in, inlen);
0101     blake2s_final(&state, out);
0102 }
0103 
0104 #endif /* _CRYPTO_BLAKE2S_H */