Skip to content

Commit 087e791

Browse files
minipligregkh
authored andcommitted
crypto: aesni - fix "by8" variant for 128 bit keys
commit 0b1e95b upstream. The "by8" counter mode optimization is broken for 128 bit keys with input data longer than 128 bytes. It uses the wrong key material for en- and decryption. The key registers xkey0, xkey4, xkey8 and xkey12 need to be preserved in case we're handling more than 128 bytes of input data -- they won't get reloaded after the initial load. They must therefore be (a) loaded on the first iteration and (b) be preserved for the latter ones. The implementation for 128 bit keys does not comply with (a) nor (b). Fix this by bringing the implementation back to its original source and correctly load the key registers and preserve their values by *not* re-using the registers for other purposes. Kudos to James for reporting the issue and providing a test case showing the discrepancies. Reported-by: James Yonan <james@openvpn.net> Cc: Chandramouli Narayanan <mouli@linux.intel.com> Signed-off-by: Mathias Krause <minipli@googlemail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 553aeac commit 087e791

1 file changed

Lines changed: 35 additions & 11 deletions

File tree

‎arch/x86/crypto/aes_ctrby8_avx-x86_64.S‎

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ ddq_add_8:
208208

209209
.if (klen == KEY_128)
210210
.if (load_keys)
211-
vmovdqa 3*16(p_keys), xkeyA
211+
vmovdqa 3*16(p_keys), xkey4
212212
.endif
213213
.else
214214
vmovdqa 3*16(p_keys), xkeyA
@@ -224,7 +224,7 @@ ddq_add_8:
224224
add $(16*by), p_in
225225

226226
.if (klen == KEY_128)
227-
vmovdqa 4*16(p_keys), xkey4
227+
vmovdqa 4*16(p_keys), xkeyB
228228
.else
229229
.if (load_keys)
230230
vmovdqa 4*16(p_keys), xkey4
@@ -234,7 +234,12 @@ ddq_add_8:
234234
.set i, 0
235235
.rept by
236236
club XDATA, i
237-
vaesenc xkeyA, var_xdata, var_xdata /* key 3 */
237+
/* key 3 */
238+
.if (klen == KEY_128)
239+
vaesenc xkey4, var_xdata, var_xdata
240+
.else
241+
vaesenc xkeyA, var_xdata, var_xdata
242+
.endif
238243
.set i, (i +1)
239244
.endr
240245

@@ -243,13 +248,18 @@ ddq_add_8:
243248
.set i, 0
244249
.rept by
245250
club XDATA, i
246-
vaesenc xkey4, var_xdata, var_xdata /* key 4 */
251+
/* key 4 */
252+
.if (klen == KEY_128)
253+
vaesenc xkeyB, var_xdata, var_xdata
254+
.else
255+
vaesenc xkey4, var_xdata, var_xdata
256+
.endif
247257
.set i, (i +1)
248258
.endr
249259

250260
.if (klen == KEY_128)
251261
.if (load_keys)
252-
vmovdqa 6*16(p_keys), xkeyB
262+
vmovdqa 6*16(p_keys), xkey8
253263
.endif
254264
.else
255265
vmovdqa 6*16(p_keys), xkeyB
@@ -267,12 +277,17 @@ ddq_add_8:
267277
.set i, 0
268278
.rept by
269279
club XDATA, i
270-
vaesenc xkeyB, var_xdata, var_xdata /* key 6 */
280+
/* key 6 */
281+
.if (klen == KEY_128)
282+
vaesenc xkey8, var_xdata, var_xdata
283+
.else
284+
vaesenc xkeyB, var_xdata, var_xdata
285+
.endif
271286
.set i, (i +1)
272287
.endr
273288

274289
.if (klen == KEY_128)
275-
vmovdqa 8*16(p_keys), xkey8
290+
vmovdqa 8*16(p_keys), xkeyB
276291
.else
277292
.if (load_keys)
278293
vmovdqa 8*16(p_keys), xkey8
@@ -288,7 +303,7 @@ ddq_add_8:
288303

289304
.if (klen == KEY_128)
290305
.if (load_keys)
291-
vmovdqa 9*16(p_keys), xkeyA
306+
vmovdqa 9*16(p_keys), xkey12
292307
.endif
293308
.else
294309
vmovdqa 9*16(p_keys), xkeyA
@@ -297,7 +312,12 @@ ddq_add_8:
297312
.set i, 0
298313
.rept by
299314
club XDATA, i
300-
vaesenc xkey8, var_xdata, var_xdata /* key 8 */
315+
/* key 8 */
316+
.if (klen == KEY_128)
317+
vaesenc xkeyB, var_xdata, var_xdata
318+
.else
319+
vaesenc xkey8, var_xdata, var_xdata
320+
.endif
301321
.set i, (i +1)
302322
.endr
303323

@@ -306,7 +326,12 @@ ddq_add_8:
306326
.set i, 0
307327
.rept by
308328
club XDATA, i
309-
vaesenc xkeyA, var_xdata, var_xdata /* key 9 */
329+
/* key 9 */
330+
.if (klen == KEY_128)
331+
vaesenc xkey12, var_xdata, var_xdata
332+
.else
333+
vaesenc xkeyA, var_xdata, var_xdata
334+
.endif
310335
.set i, (i +1)
311336
.endr
312337

@@ -412,7 +437,6 @@ ddq_add_8:
412437
/* main body of aes ctr load */
413438

414439
.macro do_aes_ctrmain key_len
415-
416440
cmp $16, num_bytes
417441
jb .Ldo_return2\key_len
418442

0 commit comments

Comments
 (0)