.gitlab-ci.yml: updated to new images by gnutls
[gd/nettle] / eax.h
1 /* eax.h
2
3    EAX mode, see http://www.cs.ucdavis.edu/~rogaway/papers/eax.pdf
4
5    Copyright (C) 2013 Niels Möller
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
12      * the GNU Lesser General Public License as published by the Free
13        Software Foundation; either version 3 of the License, or (at your
14        option) any later version.
15
16    or
17
18      * the GNU General Public License as published by the Free
19        Software Foundation; either version 2 of the License, or (at your
20        option) any later version.
21
22    or both in parallel, as here.
23
24    GNU Nettle is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27    General Public License for more details.
28
29    You should have received copies of the GNU General Public License and
30    the GNU Lesser General Public License along with this program.  If
31    not, see http://www.gnu.org/licenses/.
32 */
33
34 #ifndef NETTLE_EAX_H_INCLUDED
35 #define NETTLE_EAX_H_INCLUDED
36
37 #include "aes.h"
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 /* Name mangling */
44 #define eax_set_key nettle_eax_set_key
45 #define eax_set_nonce nettle_eax_set_nonce
46 #define eax_update nettle_eax_update
47 #define eax_encrypt nettle_eax_encrypt
48 #define eax_decrypt nettle_eax_decrypt
49 #define eax_digest nettle_eax_digest
50
51 #define eax_aes128_set_key nettle_eax_aes128_set_key
52 #define eax_aes128_set_nonce nettle_eax_aes128_set_nonce
53 #define eax_aes128_update nettle_eax_aes128_update
54 #define eax_aes128_encrypt nettle_eax_aes128_encrypt
55 #define eax_aes128_decrypt nettle_eax_aes128_decrypt
56 #define eax_aes128_digest nettle_eax_aes128_digest
57
58 /* Restricted to block ciphers with 128 bit block size. FIXME: Reflect
59    this in naming? */
60
61 #define EAX_BLOCK_SIZE 16
62 #define EAX_DIGEST_SIZE 16
63 /* FIXME: Reasonable default? */
64 #define EAX_IV_SIZE 16
65
66 /* Values independent of message and nonce */
67 struct eax_key
68 {
69   union nettle_block16 pad_block;
70   union nettle_block16 pad_partial;
71 };
72
73 struct eax_ctx
74 {
75   union nettle_block16 omac_nonce;
76   union nettle_block16 omac_data;
77   union nettle_block16 omac_message;
78   union nettle_block16 ctr;
79 };
80
81 void
82 eax_set_key (struct eax_key *key, const void *cipher, nettle_cipher_func *f);
83
84 void
85 eax_set_nonce (struct eax_ctx *eax, const struct eax_key *key,
86                const void *cipher, nettle_cipher_func *f,
87                size_t nonce_length, const uint8_t *nonce);
88
89 void
90 eax_update (struct eax_ctx *eax, const struct eax_key *key,
91             const void *cipher, nettle_cipher_func *f,
92             size_t data_length, const uint8_t *data);
93
94 void
95 eax_encrypt (struct eax_ctx *eax, const struct eax_key *key,
96              const void *cipher, nettle_cipher_func *f,
97              size_t length, uint8_t *dst, const uint8_t *src);
98
99 void
100 eax_decrypt (struct eax_ctx *eax, const struct eax_key *key,
101              const void *cipher, nettle_cipher_func *f,
102              size_t length, uint8_t *dst, const uint8_t *src);
103
104 void
105 eax_digest (struct eax_ctx *eax, const struct eax_key *key,
106             const void *cipher, nettle_cipher_func *f,
107             size_t length, uint8_t *digest);
108
109 /* Put the cipher last, to get cipher-independent offsets for the EAX
110  * state. */
111 #define EAX_CTX(type) \
112   { struct eax_key key; struct eax_ctx eax; type cipher; }
113
114 #define EAX_SET_KEY(ctx, set_key, encrypt, data)                        \
115   do {                                                                  \
116     (set_key)(&(ctx)->cipher, (data));                                  \
117     if (0) (encrypt) (&(ctx)->cipher, ~(size_t) 0,                      \
118                       (uint8_t *) 0, (const uint8_t *) 0);              \
119     eax_set_key (&(ctx)->key, &(ctx)->cipher, (nettle_cipher_func *) encrypt); \
120   } while (0)
121
122 #define EAX_SET_NONCE(ctx, encrypt, length, nonce)                      \
123   (0 ? (encrypt) (&(ctx)->cipher, ~(size_t) 0,                          \
124                   (uint8_t *) 0, (const uint8_t *) 0)                   \
125    : eax_set_nonce (&(ctx)->eax, &(ctx)->key,                   \
126                     &(ctx)->cipher, (nettle_cipher_func *) (encrypt),   \
127                     (length), (nonce)))
128
129 #define EAX_UPDATE(ctx, encrypt, length, data)                          \
130   (0 ? (encrypt) (&(ctx)->cipher, ~(size_t) 0,                          \
131                   (uint8_t *) 0, (const uint8_t *) 0)                   \
132    : eax_update (&(ctx)->eax, &(ctx)->key,                              \
133                  &(ctx)->cipher, (nettle_cipher_func *) (encrypt),      \
134                  (length), (data)))
135
136 #define EAX_ENCRYPT(ctx, encrypt, length, dst, src)                     \
137   (0 ? (encrypt) (&(ctx)->cipher, ~(size_t) 0,                          \
138                   (uint8_t *) 0, (const uint8_t *) 0)                   \
139    : eax_encrypt (&(ctx)->eax, &(ctx)->key,                             \
140                  &(ctx)->cipher, (nettle_cipher_func *) (encrypt),      \
141                   (length), (dst), (src)))
142
143 #define EAX_DECRYPT(ctx, encrypt, length, dst, src)                     \
144   (0 ? (encrypt) (&(ctx)->cipher, ~(size_t) 0,                          \
145                   (uint8_t *) 0, (const uint8_t *) 0)                   \
146    : eax_decrypt (&(ctx)->eax, &(ctx)->key,                             \
147                  &(ctx)->cipher, (nettle_cipher_func *) (encrypt),      \
148                   (length), (dst), (src)))
149
150 #define EAX_DIGEST(ctx, encrypt, length, digest)                        \
151   (0 ? (encrypt) (&(ctx)->cipher, ~(size_t) 0,                          \
152                   (uint8_t *) 0, (const uint8_t *) 0)                   \
153    : eax_digest (&(ctx)->eax, &(ctx)->key,                              \
154                  &(ctx)->cipher, (nettle_cipher_func *) (encrypt),      \
155                  (length), (digest)))
156
157 struct eax_aes128_ctx EAX_CTX(struct aes128_ctx);
158
159 void
160 eax_aes128_set_key(struct eax_aes128_ctx *ctx, const uint8_t *key);
161
162 void
163 eax_aes128_set_nonce(struct eax_aes128_ctx *ctx,
164                      size_t length, const uint8_t *iv);
165
166 void
167 eax_aes128_update(struct eax_aes128_ctx *ctx,
168                   size_t length, const uint8_t *data);
169
170 void
171 eax_aes128_encrypt(struct eax_aes128_ctx *ctx,
172                    size_t length, uint8_t *dst, const uint8_t *src);
173
174 void
175 eax_aes128_decrypt(struct eax_aes128_ctx *ctx,
176                    size_t length, uint8_t *dst, const uint8_t *src);
177
178 void
179 eax_aes128_digest(struct eax_aes128_ctx *ctx, size_t length, uint8_t *digest);
180
181 #ifdef __cplusplus
182 }
183 #endif
184
185 #endif /* NETTLE_EAX_H_INCLUDED */