Skip to content

Commit c594c54

Browse files
Dave Watsonherbertx
authored andcommitted
crypto: aesni - Split AAD hash calculation to separate macro
AAD hash only needs to be calculated once for each scatter/gather operation. Move it to its own macro, and call it from GCM_INIT instead of INITIAL_BLOCKS. Signed-off-by: Dave Watson <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 9ee4a5d commit c594c54

File tree

1 file changed

+43
-28
lines changed

1 file changed

+43
-28
lines changed

arch/x86/crypto/aesni-intel_asm.S

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ ALL_F: .octa 0xffffffffffffffffffffffffffffffff
230230
mov %arg5, %r13 # %xmm13 holds HashKey<<1 (mod poly)
231231
and $-16, %r13
232232
mov %r13, %r12
233+
234+
CALC_AAD_HASH %xmm13 %xmm0 %xmm1 %xmm2 %xmm3 %xmm4 \
235+
%xmm5 %xmm6
236+
mov %r13, %r12
233237
.endm
234238

235239
# GCM_ENC_DEC Encodes/Decodes given data. Assumes that the passed gcm_context
@@ -497,51 +501,62 @@ _read_next_byte_lt8_\@:
497501
_done_read_partial_block_\@:
498502
.endm
499503

500-
/*
501-
* if a = number of total plaintext bytes
502-
* b = floor(a/16)
503-
* num_initial_blocks = b mod 4
504-
* encrypt the initial num_initial_blocks blocks and apply ghash on
505-
* the ciphertext
506-
* %r10, %r11, %r12, %rax, %xmm5, %xmm6, %xmm7, %xmm8, %xmm9 registers
507-
* are clobbered
508-
* arg1, %arg3, %arg4, %r14 are used as a pointer only, not modified
509-
*/
510-
511-
512-
.macro INITIAL_BLOCKS_ENC_DEC TMP1 TMP2 TMP3 TMP4 TMP5 XMM0 XMM1 \
513-
XMM2 XMM3 XMM4 XMMDst TMP6 TMP7 i i_seq operation
514-
MOVADQ SHUF_MASK(%rip), %xmm14
515-
mov arg8, %r10 # %r10 = AAD
516-
mov arg9, %r11 # %r11 = aadLen
517-
pxor %xmm\i, %xmm\i
518-
pxor \XMM2, \XMM2
504+
# CALC_AAD_HASH: Calculates the hash of the data which will not be encrypted.
505+
# clobbers r10-11, xmm14
506+
.macro CALC_AAD_HASH HASHKEY TMP1 TMP2 TMP3 TMP4 TMP5 \
507+
TMP6 TMP7
508+
MOVADQ SHUF_MASK(%rip), %xmm14
509+
mov arg8, %r10 # %r10 = AAD
510+
mov arg9, %r11 # %r11 = aadLen
511+
pxor \TMP7, \TMP7
512+
pxor \TMP6, \TMP6
519513

520514
cmp $16, %r11
521515
jl _get_AAD_rest\@
522516
_get_AAD_blocks\@:
523-
movdqu (%r10), %xmm\i
524-
PSHUFB_XMM %xmm14, %xmm\i # byte-reflect the AAD data
525-
pxor %xmm\i, \XMM2
526-
GHASH_MUL \XMM2, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
517+
movdqu (%r10), \TMP7
518+
PSHUFB_XMM %xmm14, \TMP7 # byte-reflect the AAD data
519+
pxor \TMP7, \TMP6
520+
GHASH_MUL \TMP6, \HASHKEY, \TMP1, \TMP2, \TMP3, \TMP4, \TMP5
527521
add $16, %r10
528522
sub $16, %r11
529523
cmp $16, %r11
530524
jge _get_AAD_blocks\@
531525

532-
movdqu \XMM2, %xmm\i
526+
movdqu \TMP6, \TMP7
533527

534528
/* read the last <16B of AAD */
535529
_get_AAD_rest\@:
536530
cmp $0, %r11
537531
je _get_AAD_done\@
538532

539-
READ_PARTIAL_BLOCK %r10, %r11, \TMP1, %xmm\i
540-
PSHUFB_XMM %xmm14, %xmm\i # byte-reflect the AAD data
541-
pxor \XMM2, %xmm\i
542-
GHASH_MUL %xmm\i, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
533+
READ_PARTIAL_BLOCK %r10, %r11, \TMP1, \TMP7
534+
PSHUFB_XMM %xmm14, \TMP7 # byte-reflect the AAD data
535+
pxor \TMP6, \TMP7
536+
GHASH_MUL \TMP7, \HASHKEY, \TMP1, \TMP2, \TMP3, \TMP4, \TMP5
537+
movdqu \TMP7, \TMP6
543538

544539
_get_AAD_done\@:
540+
movdqu \TMP6, AadHash(%arg2)
541+
.endm
542+
543+
/*
544+
* if a = number of total plaintext bytes
545+
* b = floor(a/16)
546+
* num_initial_blocks = b mod 4
547+
* encrypt the initial num_initial_blocks blocks and apply ghash on
548+
* the ciphertext
549+
* %r10, %r11, %r12, %rax, %xmm5, %xmm6, %xmm7, %xmm8, %xmm9 registers
550+
* are clobbered
551+
* arg1, %arg2, %arg3, %r14 are used as a pointer only, not modified
552+
*/
553+
554+
555+
.macro INITIAL_BLOCKS_ENC_DEC TMP1 TMP2 TMP3 TMP4 TMP5 XMM0 XMM1 \
556+
XMM2 XMM3 XMM4 XMMDst TMP6 TMP7 i i_seq operation
557+
558+
movdqu AadHash(%arg2), %xmm\i # XMM0 = Y0
559+
545560
xor %r11, %r11 # initialise the data pointer offset as zero
546561
# start AES for num_initial_blocks blocks
547562

0 commit comments

Comments
 (0)