[OLEACC]
[reactos.git] / reactos / include / reactos / libs / gnutls / nettle / gcm.h
1 /* gcm.h
2 *
3 * Galois counter mode, specified by NIST,
4 * http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
5 *
6 */
7
8 /* NOTE: Tentative interface, subject to change. No effort will be
9 made to avoid incompatible changes. */
10
11 /* nettle, low-level cryptographics library
12 *
13 * Copyright (C) 2011 Niels Möller
14 * Copyright (C) 2011 Katholieke Universiteit Leuven
15 *
16 * Contributed by Nikos Mavrogiannopoulos
17 *
18 * The nettle library is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU Lesser General Public License as published by
20 * the Free Software Foundation; either version 2.1 of the License, or (at your
21 * option) any later version.
22 *
23 * The nettle library is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
26 * License for more details.
27 *
28 * You should have received a copy of the GNU Lesser General Public License
29 * along with the nettle library; see the file COPYING.LIB. If not, write to
30 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
31 * MA 02111-1301, USA.
32 */
33
34 #ifndef NETTLE_GCM_H_INCLUDED
35 #define NETTLE_GCM_H_INCLUDED
36
37 #include "aes.h"
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 /* Name mangling */
44 #define gcm_set_key nettle_gcm_set_key
45 #define gcm_set_iv nettle_gcm_set_iv
46 #define gcm_update nettle_gcm_update
47 #define gcm_encrypt nettle_gcm_encrypt
48 #define gcm_decrypt nettle_gcm_decrypt
49 #define gcm_digest nettle_gcm_digest
50
51 #define gcm_aes_set_key nettle_gcm_aes_set_key
52 #define gcm_aes_set_iv nettle_gcm_aes_set_iv
53 #define gcm_aes_update nettle_gcm_aes_update
54 #define gcm_aes_encrypt nettle_gcm_aes_encrypt
55 #define gcm_aes_decrypt nettle_gcm_aes_decrypt
56 #define gcm_aes_digest nettle_gcm_aes_digest
57
58 #define GCM_BLOCK_SIZE 16
59 #define GCM_IV_SIZE (GCM_BLOCK_SIZE - 4)
60
61 #define GCM_TABLE_BITS 8
62
63 /* To make sure that we have proper alignment. */
64 union gcm_block {
65 uint8_t b[GCM_BLOCK_SIZE];
66 unsigned long w[GCM_BLOCK_SIZE / sizeof(unsigned long)];
67 };
68
69 /* Hashing subkey */
70 struct gcm_key {
71 union gcm_block h[1 << GCM_TABLE_BITS];
72 };
73
74 /* Per-message state, depending on the iv */
75 struct gcm_ctx {
76 /* Original counter block */
77 union gcm_block iv;
78 /* Updated for each block. */
79 union gcm_block ctr;
80 /* Hashing state */
81 union gcm_block x;
82 uint64_t auth_size;
83 uint64_t data_size;
84 };
85
86 /* FIXME: Should use const for the cipher context. Then needs const for
87 nettle_crypt_func, which also rules out using that abstraction for
88 arcfour. */
89 void
90 gcm_set_key(struct gcm_key *key,
91 void *cipher, nettle_crypt_func * f);
92
93 void
94 gcm_set_iv(struct gcm_ctx *ctx, const struct gcm_key *key,
95 unsigned length, const uint8_t * iv);
96
97 void
98 gcm_update(struct gcm_ctx *ctx, const struct gcm_key *key,
99 unsigned length, const uint8_t * data);
100
101 void
102 gcm_encrypt(struct gcm_ctx *ctx, const struct gcm_key *key,
103 void *cipher, nettle_crypt_func * f,
104 unsigned length, uint8_t * dst, const uint8_t * src);
105
106 void
107 gcm_decrypt(struct gcm_ctx *ctx, const struct gcm_key *key,
108 void *cipher, nettle_crypt_func * f,
109 unsigned length, uint8_t * dst, const uint8_t * src);
110
111 void
112 gcm_digest(struct gcm_ctx *ctx, const struct gcm_key *key,
113 void *cipher, nettle_crypt_func * f,
114 unsigned length, uint8_t * digest);
115
116 /* Convenience macrology (not sure how useful it is) */
117
118 /* All-in-one context, with cipher, hash subkey, and message state. */
119 #define GCM_CTX(type) \
120 { type cipher; struct gcm_key key; struct gcm_ctx gcm; }
121
122 /* NOTE: Avoid using NULL, as we don't include anything defining it. */
123 #define GCM_SET_KEY(ctx, set_key, encrypt, length, data) \
124 do { \
125 (set_key)(&(ctx)->cipher, (length), (data)); \
126 if (0) (encrypt)(&(ctx)->cipher, 0, (void *)0, (void *)0); \
127 gcm_set_key(&(ctx)->key, &(ctx)->cipher, \
128 (nettle_crypt_func *) (encrypt)); \
129 } while (0)
130
131 #define GCM_SET_IV(ctx, length, data) \
132 gcm_set_iv(&(ctx)->gcm, &(ctx)->key, (length), (data))
133
134 #define GCM_UPDATE(ctx, length, data) \
135 gcm_update(&(ctx)->gcm, &(ctx)->key, (length), (data))
136
137 #define GCM_ENCRYPT(ctx, encrypt, length, dst, src) \
138 (0 ? (encrypt)(&(ctx)->cipher, 0, (void *)0, (void *)0) \
139 : gcm_encrypt(&(ctx)->gcm, &(ctx)->key, &(ctx)->cipher, \
140 (nettle_crypt_func *) (encrypt), \
141 (length), (dst), (src)))
142
143 #define GCM_DECRYPT(ctx, encrypt, length, dst, src) \
144 (0 ? (encrypt)(&(ctx)->cipher, 0, (void *)0, (void *)0) \
145 : gcm_decrypt(&(ctx)->gcm, &(ctx)->key, &(ctx)->cipher, \
146 (nettle_crypt_func *) (encrypt), \
147 (length), (dst), (src)))
148
149 #define GCM_DIGEST(ctx, encrypt, length, digest) \
150 (0 ? (encrypt)(&(ctx)->cipher, 0, (void *)0, (void *)0) \
151 : gcm_digest(&(ctx)->gcm, &(ctx)->key, &(ctx)->cipher, \
152 (nettle_crypt_func *) (encrypt), \
153 (length), (digest)))
154
155 struct gcm_aes_ctx GCM_CTX(struct aes_ctx);
156
157 void
158 gcm_aes_set_key(struct gcm_aes_ctx *ctx,
159 unsigned length, const uint8_t * key);
160
161 void
162 gcm_aes_set_iv(struct gcm_aes_ctx *ctx,
163 unsigned length, const uint8_t * iv);
164
165 void
166 gcm_aes_update(struct gcm_aes_ctx *ctx,
167 unsigned length, const uint8_t * data);
168
169 void
170 gcm_aes_encrypt(struct gcm_aes_ctx *ctx,
171 unsigned length, uint8_t * dst,
172 const uint8_t * src);
173
174 void
175 gcm_aes_decrypt(struct gcm_aes_ctx *ctx,
176 unsigned length, uint8_t * dst,
177 const uint8_t * src);
178
179 void
180 gcm_aes_digest(struct gcm_aes_ctx *ctx, unsigned length,
181 uint8_t * digest);
182
183 #ifdef __cplusplus
184 }
185 #endif
186 #endif /* NETTLE_GCM_H_INCLUDED */