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 17:54:57 by stales ### ########.fr */
1010/* */
1111/* ************************************************************************** */
1212
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 (little 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
218211aes_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
223216aes_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
0 commit comments