Skip to content

Commit b4e39c8

Browse files
authored
Merge pull request #2 from Unam3dd/dev
Dev
2 parents 3077f25 + cee3672 commit b4e39c8

File tree

14 files changed

+2075
-26
lines changed

14 files changed

+2075
-26
lines changed

DOC.md

Lines changed: 2044 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- [Références](#-références)
2929
- [Contribution](#-contribution)
3030
- [Licence](#-licence)
31+
- [Documentation](./DOC.md)
3132

3233
---
3334

src/gcm/aes_gcm.c

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: stales <stales@student.42angouleme.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/10/20 12:46:51 by stales #+# #+# */
9-
/* Updated: 2025/02/08 16:20:25 by stales ### ########.fr */
9+
/* Updated: 2025/11/28 19:14:37 by stales ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -20,12 +20,6 @@
2020
#include <immintrin.h>
2121
#include <xmmintrin.h>
2222

23-
/**
24-
* @WARNING: the implementation of GCM is not finish please take care of this
25-
* and don't use it.
26-
*
27-
*/
28-
2923
/////////////////////////////////////
3024
//
3125
//
@@ -86,6 +80,7 @@ static __m128i compute_ghash(const __m128i hash_subkey, const byte_t *restrict a
8680
if (aad && aad_len > 0) {
8781

8882
aad_blocks = aad_len >> 4;
83+
8984
for (i = 0; i < aad_blocks; i++) {
9085
temp = _mm_loadu_si128(&((__m128i*)aad)[i]);
9186
ghash = _mm_xor_si128(ghash, temp);
@@ -146,11 +141,7 @@ static __m128i compute_ghash(const __m128i hash_subkey, const byte_t *restrict a
146141
//
147142
////////////////////////////////////
148143

149-
// Forward declaration de la fonction interne
150-
static aes_status_t aes_gcm_crypt(aes_gcm_counter_t *out, const iv_t nonce, const byte_t *restrict aad, size_t aad_len, const byte_t *restrict in, size_t i_sz, const aes_ctx_t *ctx, int is_decrypt);
151-
152-
153-
static aes_status_t aes_gcm_crypt(aes_gcm_counter_t *out, const iv_t nonce, const byte_t *restrict aad, size_t aad_len, const byte_t *restrict in, size_t i_sz, const aes_ctx_t *ctx, int is_decrypt)
144+
static aes_status_t aes_gcm_crypt(aes_gcm_counter_t *out, const iv_t nonce, const byte_t *restrict aad, size_t aad_len, const byte_t *restrict in, size_t i_sz, const aes_ctx_t *ctx, bool_t is_decrypt)
154145
{
155146
if (!ctx || !out || !in || !out->out || (out->size < i_sz))
156147
return (AES_ERR);
@@ -160,13 +151,16 @@ static aes_status_t aes_gcm_crypt(aes_gcm_counter_t *out, const iv_t nonce, cons
160151
__m128i j0_encrypted = _mm_setzero_si128();
161152
__m128i hash_subkey = _mm_setzero_si128();
162153
__m128i ghash = _mm_setzero_si128();
154+
iv_t nonce_local = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
155+
uint32_t *cnt = NULL;
156+
size_t NR = 0, blocks = 0, i = 0;
163157

164158
// Copie locale du nonce pour pouvoir incrémenter le compteur
165-
byte_t nonce_copy[16];
166-
memcpy(nonce_copy, nonce, 16);
167-
uint32_t *cnt = (uint32_t *)(nonce_copy + 0xC);
159+
memcpy(nonce_local, nonce, 16);
168160

169-
size_t NR = (ctx->key_size == AES_KEY_128
161+
cnt = (uint32_t *)(nonce_local + 0xC);
162+
163+
NR = (ctx->key_size == AES_KEY_128
170164
? AES_128_NR
171165
: ctx->key_size == AES_KEY_192
172166
? AES_192_NR
@@ -175,39 +169,38 @@ static aes_status_t aes_gcm_crypt(aes_gcm_counter_t *out, const iv_t nonce, cons
175169
hash_subkey = create_hash_subkey(NR, ctx);
176170

177171
// Calculer E(K, J0) pour le tag
178-
feedback = _mm_loadu_si128((__m128i*)nonce_copy);
172+
feedback = _mm_loadu_si128((__m128i*)nonce_local);
179173
j0_encrypted = aes_block_enc(feedback, &ctx->key, NR);
180-
174+
181175
*cnt += 0x01000000;
182176

183177
// Chiffrer/Déchiffrer les données avec J1, J2, J3, ...
184-
size_t blocks = (i_sz & 0xF ? -~(i_sz >> 0x4) : (i_sz >> 0x4));
178+
blocks = (i_sz & 0xF ? -~(i_sz >> 0x4) : (i_sz >> 0x4));
185179

186-
for (size_t i = 0; i < blocks; i++) {
180+
for (i = 0; i < blocks; i++) {
187181

188182
// Prefetching
189183
_mm_prefetch((__m128i*)(in + 0x20), _MM_HINT_T0);
190184

191185
state = _mm_loadu_si128( &((__m128i*)in)[i]);
192186

193187
// Load current counter (J1, J2, J3, ...)
194-
feedback = _mm_loadu_si128((__m128i*)nonce_copy);
188+
feedback = _mm_loadu_si128((__m128i*)nonce_local);
195189

196190
feedback = aes_block_enc(feedback, &ctx->key, NR);
197191

198192
state = _mm_xor_si128(feedback, state);
199193

200194
_mm_storeu_si128(&((__m128i*)out->out)[i], state);
201195

202-
// Incrémenter pour le prochain bloc
196+
// Incrémenter pour le prochain bloc (big endian)
203197
*cnt += 0x01000000;
204198
}
205199

206200
// Calculer GHASH sur le ciphertext (toujours)
207201
// Pour encryption: ciphertext = out->out (résultat du CTR)
208202
// Pour decryption: ciphertext = in (entrée)
209-
const byte_t *ciphertext = is_decrypt ? in : out->out;
210-
ghash = compute_ghash(hash_subkey, aad, aad_len, ciphertext, i_sz);
203+
ghash = compute_ghash(hash_subkey, aad, aad_len, is_decrypt ? in : out->out, i_sz);
211204

212205
// Tag final = GHASH XOR E(K, J0)
213206
out->tag = _mm_xor_si128(ghash, j0_encrypted);
@@ -217,11 +210,11 @@ static aes_status_t aes_gcm_crypt(aes_gcm_counter_t *out, const iv_t nonce, cons
217210

218211
aes_status_t aes_gcm_enc(aes_gcm_counter_t *out, const iv_t nonce, const byte_t *restrict aad, size_t aad_len, const byte_t *restrict in, size_t i_sz, const aes_ctx_t *ctx)
219212
{
220-
return aes_gcm_crypt(out, nonce, aad, aad_len, in, i_sz, ctx, 0);
213+
return (aes_gcm_crypt(out, nonce, aad, aad_len, in, i_sz, ctx, FALSE));
221214
}
222215

223216
aes_status_t aes_gcm_dec(aes_gcm_counter_t *out, const iv_t nonce, const byte_t *restrict aad, size_t aad_len, const byte_t *restrict in, size_t i_sz, const aes_ctx_t *ctx)
224217
{
225-
return aes_gcm_crypt(out, nonce, aad, aad_len, in, i_sz, ctx, 1);
218+
return (aes_gcm_crypt(out, nonce, aad, aad_len, in, i_sz, ctx, TRUE));
226219
}
227220

tests/README_TESTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,4 @@ Les tests `aes_128_gcm_test1` et `aes_128_gcm_test2` utilisent des vecteurs offi
151151
- FIPS 197 - Advanced Encryption Standard (AES)
152152
- Intel AES-NI White Papers
153153

154+

tests/aes_cbc/aes_256_cbc_two.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,4 @@ int main(void)
100100
return 0;
101101
}
102102

103+

tests/aes_cfb/aes_256_cfb_two.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,4 @@ int main(void)
9595
return 0;
9696
}
9797

98+

tests/aes_ecb/aes_128_ecb_two.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,4 @@ int main(void)
9292
return 0;
9393
}
9494

95+

tests/aes_gcm/aes_128_gcm_test3_aad.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,4 @@ int main(void)
112112
return 0;
113113
}
114114

115+

tests/aes_gcm/aes_128_gcm_test4_multiblock.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,4 @@ int main(void)
122122
return 0;
123123
}
124124

125+

tests/aes_gcm/aes_128_gcm_test5_partial_block.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,4 @@ int main(void)
117117
return 0;
118118
}
119119

120+

0 commit comments

Comments
 (0)