Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
0002 
0003 #ifndef _CRYPTO_BLAKE2B_H
0004 #define _CRYPTO_BLAKE2B_H
0005 
0006 #include <linux/bug.h>
0007 #include <linux/types.h>
0008 #include <linux/string.h>
0009 
0010 enum blake2b_lengths {
0011     BLAKE2B_BLOCK_SIZE = 128,
0012     BLAKE2B_HASH_SIZE = 64,
0013     BLAKE2B_KEY_SIZE = 64,
0014 
0015     BLAKE2B_160_HASH_SIZE = 20,
0016     BLAKE2B_256_HASH_SIZE = 32,
0017     BLAKE2B_384_HASH_SIZE = 48,
0018     BLAKE2B_512_HASH_SIZE = 64,
0019 };
0020 
0021 struct blake2b_state {
0022     /* 'h', 't', and 'f' are used in assembly code, so keep them as-is. */
0023     u64 h[8];
0024     u64 t[2];
0025     u64 f[2];
0026     u8 buf[BLAKE2B_BLOCK_SIZE];
0027     unsigned int buflen;
0028     unsigned int outlen;
0029 };
0030 
0031 enum blake2b_iv {
0032     BLAKE2B_IV0 = 0x6A09E667F3BCC908ULL,
0033     BLAKE2B_IV1 = 0xBB67AE8584CAA73BULL,
0034     BLAKE2B_IV2 = 0x3C6EF372FE94F82BULL,
0035     BLAKE2B_IV3 = 0xA54FF53A5F1D36F1ULL,
0036     BLAKE2B_IV4 = 0x510E527FADE682D1ULL,
0037     BLAKE2B_IV5 = 0x9B05688C2B3E6C1FULL,
0038     BLAKE2B_IV6 = 0x1F83D9ABFB41BD6BULL,
0039     BLAKE2B_IV7 = 0x5BE0CD19137E2179ULL,
0040 };
0041 
0042 static inline void __blake2b_init(struct blake2b_state *state, size_t outlen,
0043                   const void *key, size_t keylen)
0044 {
0045     state->h[0] = BLAKE2B_IV0 ^ (0x01010000 | keylen << 8 | outlen);
0046     state->h[1] = BLAKE2B_IV1;
0047     state->h[2] = BLAKE2B_IV2;
0048     state->h[3] = BLAKE2B_IV3;
0049     state->h[4] = BLAKE2B_IV4;
0050     state->h[5] = BLAKE2B_IV5;
0051     state->h[6] = BLAKE2B_IV6;
0052     state->h[7] = BLAKE2B_IV7;
0053     state->t[0] = 0;
0054     state->t[1] = 0;
0055     state->f[0] = 0;
0056     state->f[1] = 0;
0057     state->buflen = 0;
0058     state->outlen = outlen;
0059     if (keylen) {
0060         memcpy(state->buf, key, keylen);
0061         memset(&state->buf[keylen], 0, BLAKE2B_BLOCK_SIZE - keylen);
0062         state->buflen = BLAKE2B_BLOCK_SIZE;
0063     }
0064 }
0065 
0066 #endif /* _CRYPTO_BLAKE2B_H */