wireguard: initial implementation of wireguard protocol
[vpp.git] / src / plugins / wireguard / blake / blake2s.h
1 /*
2  * Copyright (c) 2020 Doc.ai and/or its affiliates.
3  * Copyright (c) 2012 Samuel Neves <sneves@dei.uc.pt>.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 /*
17    More information about the BLAKE2 hash function can be found at
18    https://blake2.net.
19 */
20
21 #ifndef __included_crypto_blake2s_h__
22 #define __included_crypto_blake2s_h__
23
24 #include <vlib/vlib.h>
25
26 #if defined(_MSC_VER)
27 #define BLAKE2_PACKED(x) __pragma(pack(push, 1)) x __pragma(pack(pop))
28 #else
29 #define BLAKE2_PACKED(x) x __attribute__((packed))
30 #endif
31
32 enum blake2s_constant
33 {
34   BLAKE2S_BLOCK_BYTES = 64,
35   BLAKE2S_OUT_BYTES = 32,
36   BLAKE2S_KEY_BYTES = 32,
37   BLAKE2S_HASH_SIZE = BLAKE2S_OUT_BYTES,
38   BLAKE2S_SALT_BYTES = 8,
39   BLAKE2S_PERSONAL_BYTES = 8
40 };
41
42 typedef struct blake2s_state
43 {
44   uint32_t h[8];
45   uint32_t t[2];
46   uint32_t f[2];
47   uint8_t buf[BLAKE2S_BLOCK_BYTES];
48   size_t buflen;
49   size_t outlen;
50   uint8_t last_node;
51 } blake2s_state_t;
52
53 BLAKE2_PACKED (struct blake2s_param
54                {
55                uint8_t digest_length;   /* 1 */
56                uint8_t key_length;      /* 2 */
57                uint8_t fanout;  /* 3 */
58                uint8_t depth;   /* 4 */
59                uint32_t leaf_length;    /* 8 */
60                uint32_t node_offset;    /* 12 */
61                uint16_t xof_length;     /* 14 */
62                uint8_t node_depth;      /* 15 */
63                uint8_t inner_length;    /* 16 */
64                /* uint8_t  reserved[0]; */
65                uint8_t salt[BLAKE2S_SALT_BYTES];        /* 24 */
66                uint8_t personal[BLAKE2S_PERSONAL_BYTES];        /* 32 */
67                });
68
69 typedef struct blake2s_param blake2s_param_t;
70
71 /* Streaming API */
72 int blake2s_init (blake2s_state_t * S, size_t outlen);
73 int blake2s_init_key (blake2s_state_t * S, size_t outlen, const void *key,
74                       size_t keylen);
75 int blake2s_init_param (blake2s_state_t * S, const blake2s_param_t * P);
76 int blake2s_update (blake2s_state_t * S, const void *in, size_t inlen);
77 int blake2s_final (blake2s_state_t * S, void *out, size_t outlen);
78
79 int blake2s (void *out, size_t outlen, const void *in, size_t inlen,
80              const void *key, size_t keylen);
81
82 #endif /* __included_crypto_blake2s_h__ */
83
84 /*
85  * fd.io coding-style-patch-verification: ON
86  *
87  * Local Variables:
88  * eval: (c-set-style "gnu")
89  * End:
90  */