lib: add BLAKE2 hash support
[platform/kernel/u-boot.git] / include / u-boot / blake2.h
1 /* SPDX-License-Identifier: CC0-1.0 */
2 /*
3    BLAKE2 reference source code package - reference C implementations
4
5    Copyright 2012, Samuel Neves <sneves@dei.uc.pt>.  You may use this under the
6    terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
7    your option.  The terms of these licenses can be found at:
8
9    - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
10    - OpenSSL license   : https://www.openssl.org/source/license.html
11    - Apache 2.0        : http://www.apache.org/licenses/LICENSE-2.0
12
13    More information about the BLAKE2 hash function can be found at
14    https://blake2.net.
15 */
16
17 /*
18  * Cross-ported from BLAKE2 (https://github.com/BLAKE2/BLAKE2).
19  * Modifications includes:
20  *
21  * - Remove unsupported compilers like MSC/CPP
22  * - Remove blake2s/blake2sp/blake2bp
23  *   This blake2 implementation is mostly for btrfs, which only utilizes
24  *   blake2b.
25  */
26 #ifndef BLAKE2_H
27 #define BLAKE2_H
28
29 #include <stddef.h>
30 #include <stdint.h>
31
32 #if defined(_MSC_VER)
33 #define BLAKE2_PACKED(x) __pragma(pack(push, 1)) x __pragma(pack(pop))
34 #else
35 #define BLAKE2_PACKED(x) x __attribute__((packed))
36 #endif
37
38   enum blake2b_constant
39   {
40     BLAKE2B_BLOCKBYTES = 128,
41     BLAKE2B_OUTBYTES   = 64,
42     BLAKE2B_KEYBYTES   = 64,
43     BLAKE2B_SALTBYTES  = 16,
44     BLAKE2B_PERSONALBYTES = 16
45   };
46
47   typedef struct blake2b_state__
48   {
49     uint64_t h[8];
50     uint64_t t[2];
51     uint64_t f[2];
52     uint8_t  buf[BLAKE2B_BLOCKBYTES];
53     size_t   buflen;
54     size_t   outlen;
55     uint8_t  last_node;
56   } blake2b_state;
57
58   BLAKE2_PACKED(struct blake2b_param__
59   {
60     uint8_t  digest_length; /* 1 */
61     uint8_t  key_length;    /* 2 */
62     uint8_t  fanout;        /* 3 */
63     uint8_t  depth;         /* 4 */
64     uint32_t leaf_length;   /* 8 */
65     uint32_t node_offset;   /* 12 */
66     uint32_t xof_length;    /* 16 */
67     uint8_t  node_depth;    /* 17 */
68     uint8_t  inner_length;  /* 18 */
69     uint8_t  reserved[14];  /* 32 */
70     uint8_t  salt[BLAKE2B_SALTBYTES]; /* 48 */
71     uint8_t  personal[BLAKE2B_PERSONALBYTES];  /* 64 */
72   });
73
74   typedef struct blake2b_param__ blake2b_param;
75
76   /* Padded structs result in a compile-time error */
77   enum {
78     BLAKE2_DUMMY_2 = 1/(int)(sizeof(blake2b_param) == BLAKE2B_OUTBYTES)
79   };
80
81   /* Streaming API */
82   int blake2b_init( blake2b_state *S, size_t outlen );
83   int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen );
84   int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
85   int blake2b_update( blake2b_state *S, const void *in, size_t inlen );
86   int blake2b_final( blake2b_state *S, void *out, size_t outlen );
87
88   /* Simple API */
89   int blake2b( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
90
91   /* This is simply an alias for blake2b */
92   int blake2( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen );
93
94 #endif