Line data Source code
1 : /* fd_aes_ref.c was imported from the OpenSSL project circa 2023-Aug.
2 : Original source files: crypto/evp/e_aes.c crypto/modes/gcm128.c */
3 :
4 : #include "fd_aes_gcm.h"
5 :
6 4220807 : #define fd_gcm_init fd_gcm_init_4bit
7 4001606 : #define fd_gcm_gmult fd_gcm_gmult_4bit
8 11508349 : #define fd_gcm_ghash fd_gcm_ghash_4bit
9 :
10 : static void
11 : fd_aes_gcm_setiv( fd_aes_gcm_ref_t * gcm,
12 4220807 : uchar const iv[ 12 ] ) {
13 :
14 4220807 : uint ctr;
15 4220807 : gcm->len.u[ 0 ] = 0; /* AAD length */
16 4220807 : gcm->len.u[ 1 ] = 0; /* Message length */
17 4220807 : gcm->ares = 0;
18 4220807 : gcm->mres = 0;
19 :
20 4220807 : memcpy( gcm->Yi.c, iv, 12 );
21 4220807 : gcm->Yi.c[12] = 0;
22 4220807 : gcm->Yi.c[13] = 0;
23 4220807 : gcm->Yi.c[14] = 0;
24 4220807 : gcm->Yi.c[15] = 1;
25 4220807 : ctr = 1;
26 :
27 4220807 : gcm->Xi.u[0] = 0;
28 4220807 : gcm->Xi.u[1] = 0;
29 :
30 4220807 : fd_aes_encrypt( gcm->Yi.c, gcm->EK0.c, &gcm->key );
31 4220807 : ctr++;
32 :
33 4220807 : gcm->Yi.d[3] = fd_uint_bswap( ctr );
34 4220807 : }
35 :
36 : void
37 : fd_aes_gcm_init_ref( fd_aes_gcm_ref_t * gcm,
38 : uchar const * key,
39 : ulong key_sz,
40 4220807 : uchar const iv[ 12 ] ) {
41 4220807 : memset( gcm, 0, sizeof(fd_aes_gcm_ref_t) );
42 :
43 4220807 : fd_aes_key_ref_t * ks = &gcm->key;
44 4220807 : fd_aes_set_encrypt_key( key, key_sz<<3UL, ks );
45 :
46 4220807 : fd_aes_encrypt( gcm->H.c, gcm->H.c, ks );
47 4220807 : gcm->H.u[ 0 ] = fd_ulong_bswap( gcm->H.u[ 0 ] );
48 4220807 : gcm->H.u[ 1 ] = fd_ulong_bswap( gcm->H.u[ 1 ] );
49 :
50 4220807 : fd_gcm_init( gcm->Htable, gcm->H.u );
51 4220807 : fd_aes_gcm_setiv( gcm, iv );
52 4220807 : }
53 :
54 : static int
55 : fd_gcm128_aad( fd_aes_gcm_ref_t * aes_gcm,
56 : uchar const * aad,
57 4220807 : ulong aad_sz ) {
58 :
59 4220807 : ulong alen = aes_gcm->len.u[ 0 ];
60 :
61 4220807 : if( FD_UNLIKELY( aes_gcm->len.u[ 1 ] ) )
62 0 : return -2;
63 :
64 4220807 : alen += aad_sz;
65 4220807 : if (alen > (1UL<<61) || (sizeof(aad_sz) == 8 && alen < aad_sz))
66 0 : return -1;
67 4220807 : aes_gcm->len.u[0] = alen;
68 :
69 4220807 : uint n = aes_gcm->ares;
70 4220807 : if (n) {
71 0 : while (n && aad_sz) {
72 0 : aes_gcm->Xi.c[n] ^= *(aad++);
73 0 : --aad_sz;
74 0 : n = (n + 1) % 16;
75 0 : }
76 0 : if (n == 0)
77 0 : fd_gcm_gmult( aes_gcm->Xi.u, aes_gcm->Htable );
78 0 : else {
79 0 : aes_gcm->ares = n;
80 0 : return 0;
81 0 : }
82 0 : }
83 4220807 : ulong i;
84 4220807 : if ((i = (aad_sz & (ulong)-16))) {
85 4063726 : fd_gcm_ghash( aes_gcm->Xi.u, aes_gcm->Htable, aad, i );
86 4063726 : aad += i;
87 4063726 : aad_sz -= i;
88 4063726 : }
89 4220807 : if (aad_sz) {
90 4216197 : n = (unsigned int)aad_sz;
91 41582305 : for (i = 0; i < aad_sz; ++i)
92 37366108 : aes_gcm->Xi.c[i] ^= aad[i];
93 4216197 : }
94 :
95 4220807 : aes_gcm->ares = n;
96 4220807 : return 0;
97 4220807 : }
98 :
99 : /* TODO separate reference code and GCM128 */
100 :
101 : static int
102 : fd_gcm128_encrypt( fd_aes_gcm_ref_t * ctx,
103 : uchar const * in,
104 : uchar * out,
105 2105845 : ulong len ) {
106 :
107 2105845 : uint n, ctr, mres;
108 2105845 : ulong i;
109 2105845 : ulong mlen = ctx->len.u[1];
110 2105845 : void *key = &ctx->key;
111 :
112 2105845 : mlen += len;
113 2105845 : if (mlen > ((1UL<<36) - 32) || (sizeof(len) == 8 && mlen < len))
114 0 : return -1;
115 2105845 : ctx->len.u[1] = mlen;
116 :
117 2105845 : mres = ctx->mres;
118 :
119 2105845 : if (ctx->ares) {
120 : /* First call to encrypt finalizes GHASH(AAD) */
121 2103540 : if (len == 0) {
122 2000007 : fd_gcm_gmult( ctx->Xi.u, ctx->Htable );
123 2000007 : ctx->ares = 0;
124 2000007 : return 0;
125 2000007 : }
126 103533 : memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
127 103533 : ctx->Xi.u[0] = 0;
128 103533 : ctx->Xi.u[1] = 0;
129 103533 : mres = sizeof(ctx->Xi);
130 103533 : ctx->ares = 0;
131 103533 : }
132 :
133 105838 : ctr = fd_uint_bswap( ctx->Yi.d[3] );
134 :
135 105838 : n = mres % 16;
136 70210409 : for (i = 0; i < len; ++i) {
137 70104571 : if (n == 0) {
138 4443805 : fd_aes_encrypt( ctx->Yi.c, ctx->EKi.c, key );
139 4443805 : ++ctr;
140 4443805 : ctx->Yi.d[3] = fd_uint_bswap( ctr );
141 4443805 : }
142 70104571 : ctx->Xn[mres++] = out[i] = in[i] ^ ctx->EKi.c[n];
143 70104571 : n = (n + 1) % 16;
144 70104571 : if (mres == sizeof(ctx->Xn)) {
145 1428492 : fd_gcm_ghash( ctx->Xi.u, ctx->Htable, ctx->Xn, sizeof(ctx->Xn) );
146 1428492 : mres = 0;
147 1428492 : }
148 70104571 : }
149 :
150 105838 : ctx->mres = mres;
151 105838 : return 0;
152 2105845 : }
153 :
154 : static int
155 : fd_gcm128_decrypt( fd_aes_gcm_ref_t * ctx,
156 : uchar const * in,
157 : uchar * out,
158 2114962 : ulong len ) {
159 :
160 2114962 : uint n, ctr, mres;
161 2114962 : ulong i;
162 2114962 : ulong mlen = ctx->len.u[1];
163 2114962 : void * key = &ctx->key;
164 :
165 2114962 : mlen += len;
166 2114962 : if (mlen > ((1UL<<36) - 32) || (sizeof(len) == 8 && mlen < len))
167 0 : return -1;
168 2114962 : ctx->len.u[1] = mlen;
169 :
170 2114962 : mres = ctx->mres;
171 :
172 2114962 : if (ctx->ares) {
173 : /* First call to decrypt finalizes GHASH(AAD) */
174 2112657 : if (len == 0) {
175 2001599 : fd_gcm_gmult( ctx->Xi.u, ctx->Htable );
176 2001599 : ctx->ares = 0;
177 2001599 : return 0;
178 2001599 : }
179 111058 : memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
180 111058 : ctx->Xi.u[0] = 0;
181 111058 : ctx->Xi.u[1] = 0;
182 111058 : mres = sizeof(ctx->Xi);
183 111058 : ctx->ares = 0;
184 111058 : }
185 :
186 113363 : ctr = fd_uint_bswap( ctx->Yi.d[3] );
187 :
188 113363 : n = mres % 16;
189 81736307 : for (i = 0; i < len; ++i) {
190 81622944 : uchar c;
191 81622944 : if (n == 0) {
192 5162371 : fd_aes_encrypt( ctx->Yi.c, ctx->EKi.c, key );
193 5162371 : ++ctr;
194 5162371 : ctx->Yi.d[3] = fd_uint_bswap( ctr );
195 5162371 : }
196 81622944 : out[i] = (ctx->Xn[mres++] = c = in[i]) ^ ctx->EKi.c[n];
197 81622944 : n = (n + 1) % 16;
198 81622944 : if (mres == sizeof(ctx->Xn)) {
199 1668843 : fd_gcm_ghash( ctx->Xi.u, ctx->Htable, ctx->Xn, sizeof(ctx->Xn) );
200 1668843 : mres = 0;
201 1668843 : }
202 81622944 : }
203 :
204 113363 : ctx->mres = mres;
205 113363 : return 0;
206 2114962 : }
207 :
208 : static void
209 4220807 : fd_gcm128_finish( fd_aes_gcm_ref_t * ctx ) {
210 :
211 4220807 : ulong alen = ctx->len.u[0] << 3; // 176
212 4220807 : ulong clen = ctx->len.u[1] << 3; // 9296
213 :
214 4220807 : struct {
215 4220807 : ulong hi;
216 4220807 : ulong lo;
217 4220807 : } bitlen;
218 4220807 : uint mres = ctx->mres;
219 :
220 4220807 : if( mres ) {
221 218737 : uint blocks = (mres + 15u) & 0xfffffff0u; // 16
222 :
223 218737 : memset(ctx->Xn + mres, 0, blocks - mres);
224 218737 : mres = blocks;
225 218737 : if (mres == sizeof(ctx->Xn)) {
226 126481 : fd_gcm_ghash( ctx->Xi.u, ctx->Htable, ctx->Xn, mres );
227 126481 : mres = 0;
228 126481 : }
229 4002070 : } else if( ctx->ares ) {
230 0 : fd_gcm_gmult( ctx->Xi.u, ctx->Htable );
231 0 : }
232 :
233 4220807 : alen = fd_ulong_bswap( alen );
234 4220807 : clen = fd_ulong_bswap( clen );
235 :
236 4220807 : bitlen.hi = alen;
237 4220807 : bitlen.lo = clen;
238 4220807 : memcpy( ctx->Xn + mres, &bitlen, sizeof(bitlen) );
239 4220807 : mres += (uint)sizeof(bitlen);
240 4220807 : fd_gcm_ghash( ctx->Xi.u, ctx->Htable, ctx->Xn, mres );
241 :
242 4220807 : ctx->Xi.u[0] ^= ctx->EK0.u[0];
243 4220807 : ctx->Xi.u[1] ^= ctx->EK0.u[1];
244 4220807 : }
245 :
246 : void
247 : fd_aes_gcm_encrypt_ref( fd_aes_gcm_ref_t * aes_gcm,
248 : uchar * c,
249 : uchar const * p,
250 : ulong sz,
251 : uchar const * aad,
252 : ulong aad_sz,
253 2105845 : uchar tag[ 16 ] ) {
254 :
255 2105845 : fd_gcm128_aad( aes_gcm, aad, aad_sz );
256 :
257 2105845 : ulong bulk = 0UL;
258 2105845 : int res = fd_gcm128_encrypt( aes_gcm, p+bulk, c+bulk, sz-bulk );
259 2105845 : FD_DCHECK_CRIT( res==0, "internal error" );
260 :
261 : /* CRYPTO_gcm128_tag */
262 2105845 : fd_gcm128_finish( aes_gcm );
263 2105845 : fd_memcpy( tag, aes_gcm->Xi.c, 16 );
264 2105845 : }
265 :
266 : int
267 : fd_aes_gcm_decrypt_ref( fd_aes_gcm_ref_t * aes_gcm,
268 : uchar const * c,
269 : uchar * p,
270 : ulong sz,
271 : uchar const * aad,
272 : ulong aad_sz,
273 2114962 : uchar const tag[ 16 ] ) {
274 :
275 2114962 : fd_gcm128_aad( aes_gcm, aad, aad_sz );
276 :
277 2114962 : ulong bulk = 0UL;
278 2114962 : int res = fd_gcm128_decrypt( aes_gcm, c+bulk, p+bulk, sz-bulk );
279 2114962 : FD_DCHECK_CRIT( res==0, "internal error" );
280 :
281 : /* CRYPTO_gcm128_finish */
282 2114962 : fd_gcm128_finish( aes_gcm );
283 2114962 : return 0==memcmp( aes_gcm->Xi.c, tag, 16 ); /* TODO USE CONSTANT TIME COMPARE */
284 2114962 : }
|