@@ -105,36 +105,9 @@ aes_status_t aes_ecb_enc(byte_t *out, size_t o_sz, const byte_t *restrict in, si
105105
106106 for (i = 0 ; i < blocks ; i ++ ) {
107107
108- // Load State
109108 state = _mm_loadu_si128 ( & ((__m128i * )in )[i ]);
110109
111- // Xor State with first round Key (This XOR is equal to first AddRounKey Transformation)
112- state = AddRoundKey (state , ctx -> key .sched [0 ]);
113-
114- state = _mm_aesenc_si128 (state , ctx -> key .sched [1 ]);
115- state = _mm_aesenc_si128 (state , ctx -> key .sched [2 ]);
116- state = _mm_aesenc_si128 (state , ctx -> key .sched [3 ]);
117- state = _mm_aesenc_si128 (state , ctx -> key .sched [4 ]);
118- state = _mm_aesenc_si128 (state , ctx -> key .sched [5 ]);
119- state = _mm_aesenc_si128 (state , ctx -> key .sched [6 ]);
120- state = _mm_aesenc_si128 (state , ctx -> key .sched [7 ]);
121- state = _mm_aesenc_si128 (state , ctx -> key .sched [8 ]);
122- state = _mm_aesenc_si128 (state , ctx -> key .sched [9 ]);
123-
124- if (NR >= AES_192_NR ) {
125- state = _mm_aesenc_si128 (state , ctx -> key .sched [10 ]);
126- state = _mm_aesenc_si128 (state , ctx -> key .sched [11 ]);
127-
128- if (NR == AES_256_NR ) {
129- state = _mm_aesenc_si128 (state , ctx -> key .sched [12 ]);
130- state = _mm_aesenc_si128 (state , ctx -> key .sched [13 ]);
131- }
132- }
133-
134- // a[127:0] := ShiftRows(a[127:0])
135- // a[127:0] := SubBytes(a[127:0])
136- // dst[127:0] := a[127:0] (AddRoundKey) XOR RoundKey[127:0]
137- state = _mm_aesenclast_si128 (state , ctx -> key .sched [NR ]);
110+ state = aes_block_enc (state , & ctx -> key , NR );
138111
139112 _mm_storeu_si128 (& ((__m128i * )out )[i ], state );
140113 }
@@ -162,28 +135,8 @@ aes_status_t aes_ecb_dec(byte_t *out, size_t o_sz, const byte_t *restrict in, si
162135 for (i = 0 ; i < blocks ; i ++ ) {
163136 state = _mm_loadu_si128 ( & ((__m128i * )in )[i ]);
164137
165- state = AddRoundKey (state , ctx -> key .sched [NR ]);
166- state = _mm_aesdec_si128 (state , _mm_aesimc_si128 (ctx -> key .sched [NR - 1 ]));
167- state = _mm_aesdec_si128 (state , _mm_aesimc_si128 (ctx -> key .sched [NR - 2 ]));
168- state = _mm_aesdec_si128 (state , _mm_aesimc_si128 (ctx -> key .sched [NR - 3 ]));
169- state = _mm_aesdec_si128 (state , _mm_aesimc_si128 (ctx -> key .sched [NR - 4 ]));
170- state = _mm_aesdec_si128 (state , _mm_aesimc_si128 (ctx -> key .sched [NR - 5 ]));
171- state = _mm_aesdec_si128 (state , _mm_aesimc_si128 (ctx -> key .sched [NR - 6 ]));
172- state = _mm_aesdec_si128 (state , _mm_aesimc_si128 (ctx -> key .sched [NR - 7 ]));
173- state = _mm_aesdec_si128 (state , _mm_aesimc_si128 (ctx -> key .sched [NR - 8 ]));
174- state = _mm_aesdec_si128 (state , _mm_aesimc_si128 (ctx -> key .sched [NR - 9 ]));
175-
176- if (NR >= AES_192_NR ) {
177- state = _mm_aesdec_si128 (state , _mm_aesimc_si128 (ctx -> key .sched [NR - 10 ]));
178- state = _mm_aesdec_si128 (state , _mm_aesimc_si128 (ctx -> key .sched [NR - 11 ]));
179-
180- if (NR == AES_256_NR ) {
181- state = _mm_aesdec_si128 (state , _mm_aesimc_si128 (ctx -> key .sched [NR - 12 ]));
182- state = _mm_aesdec_si128 (state , _mm_aesimc_si128 (ctx -> key .sched [NR - 13 ]));
183- }
184- }
185-
186- state = _mm_aesdeclast_si128 (state , ctx -> key .sched [0 ]);
138+ state = aes_block_dec (state , & ctx -> key , NR );
139+
187140 _mm_storeu_si128 (& ((__m128i * )out )[i ], state );
188141 }
189142
0 commit comments