1414#include <linux/module.h>
1515#include <asm/simd.h>
1616
17- struct poly1305_simd_desc_ctx {
18- struct poly1305_desc_ctx base ;
19- /* derived key u set? */
20- bool uset ;
21- #ifdef CONFIG_AS_AVX2
22- /* derived keys r^3, r^4 set? */
23- bool wset ;
24- #endif
25- /* derived Poly1305 key r^2 */
26- u32 u [5 ];
27- /* ... silently appended r^3 and r^4 when using AVX2 */
28- };
29-
3017asmlinkage void poly1305_block_sse2 (u32 * h , const u8 * src ,
3118 const u32 * r , unsigned int blocks );
3219asmlinkage void poly1305_2block_sse2 (u32 * h , const u8 * src , const u32 * r ,
3320 unsigned int blocks , const u32 * u );
34- #ifdef CONFIG_AS_AVX2
3521asmlinkage void poly1305_4block_avx2 (u32 * h , const u8 * src , const u32 * r ,
3622 unsigned int blocks , const u32 * u );
37- static bool poly1305_use_avx2 ;
38- #endif
3923
40- static int poly1305_simd_init (struct shash_desc * desc )
41- {
42- struct poly1305_simd_desc_ctx * sctx = shash_desc_ctx (desc );
43-
44- sctx -> uset = false;
45- #ifdef CONFIG_AS_AVX2
46- sctx -> wset = false;
47- #endif
48-
49- return crypto_poly1305_init (desc );
50- }
24+ static bool poly1305_use_avx2 __ro_after_init ;
5125
5226static void poly1305_simd_mult (u32 * a , const u32 * b )
5327{
@@ -63,53 +37,49 @@ static void poly1305_simd_mult(u32 *a, const u32 *b)
6337static unsigned int poly1305_simd_blocks (struct poly1305_desc_ctx * dctx ,
6438 const u8 * src , unsigned int srclen )
6539{
66- struct poly1305_simd_desc_ctx * sctx ;
6740 unsigned int blocks , datalen ;
6841
69- BUILD_BUG_ON (offsetof(struct poly1305_simd_desc_ctx , base ));
70- sctx = container_of (dctx , struct poly1305_simd_desc_ctx , base );
71-
7242 if (unlikely (!dctx -> sset )) {
7343 datalen = crypto_poly1305_setdesckey (dctx , src , srclen );
7444 src += srclen - datalen ;
7545 srclen = datalen ;
7646 }
7747
78- #ifdef CONFIG_AS_AVX2
79- if ( poly1305_use_avx2 && srclen >= POLY1305_BLOCK_SIZE * 4 ) {
80- if ( unlikely (! sctx -> wset ) ) {
81- if (! sctx -> uset ) {
82- memcpy ( sctx -> u , dctx -> r . r , sizeof ( sctx -> u ));
83- poly1305_simd_mult ( sctx -> u , dctx -> r . r ) ;
84- sctx -> uset = true ;
48+ if ( IS_ENABLED ( CONFIG_AS_AVX2 ) &&
49+ poly1305_use_avx2 &&
50+ srclen >= POLY1305_BLOCK_SIZE * 4 ) {
51+ if (unlikely ( dctx -> rset < 4 ) ) {
52+ if ( dctx -> rset < 2 ) {
53+ dctx -> r [ 1 ] = dctx -> r [ 0 ] ;
54+ poly1305_simd_mult ( dctx -> r [ 1 ]. r , dctx -> r [ 0 ]. r ) ;
8555 }
86- memcpy ( sctx -> u + 5 , sctx -> u , sizeof ( sctx -> u )) ;
87- poly1305_simd_mult (sctx -> u + 5 , dctx -> r .r );
88- memcpy ( sctx -> u + 10 , sctx -> u + 5 , sizeof ( sctx -> u )) ;
89- poly1305_simd_mult (sctx -> u + 10 , dctx -> r .r );
90- sctx -> wset = true ;
56+ dctx -> r [ 2 ] = dctx -> r [ 1 ] ;
57+ poly1305_simd_mult (dctx -> r [ 2 ]. r , dctx -> r [ 0 ] .r );
58+ dctx -> r [ 3 ] = dctx -> r [ 2 ] ;
59+ poly1305_simd_mult (dctx -> r [ 3 ]. r , dctx -> r [ 0 ] .r );
60+ dctx -> rset = 4 ;
9161 }
9262 blocks = srclen / (POLY1305_BLOCK_SIZE * 4 );
93- poly1305_4block_avx2 (dctx -> h .h , src , dctx -> r .r , blocks ,
94- sctx -> u );
63+ poly1305_4block_avx2 (dctx -> h .h , src , dctx -> r [ 0 ] .r , blocks ,
64+ dctx -> r [ 1 ]. r );
9565 src += POLY1305_BLOCK_SIZE * 4 * blocks ;
9666 srclen -= POLY1305_BLOCK_SIZE * 4 * blocks ;
9767 }
98- #endif
68+
9969 if (likely (srclen >= POLY1305_BLOCK_SIZE * 2 )) {
100- if (unlikely (! sctx -> uset )) {
101- memcpy ( sctx -> u , dctx -> r . r , sizeof ( sctx -> u )) ;
102- poly1305_simd_mult (sctx -> u , dctx -> r .r );
103- sctx -> uset = true ;
70+ if (unlikely (dctx -> rset < 2 )) {
71+ dctx -> r [ 1 ] = dctx -> r [ 0 ] ;
72+ poly1305_simd_mult (dctx -> r [ 1 ]. r , dctx -> r [ 0 ] .r );
73+ dctx -> rset = 2 ;
10474 }
10575 blocks = srclen / (POLY1305_BLOCK_SIZE * 2 );
106- poly1305_2block_sse2 (dctx -> h .h , src , dctx -> r . r , blocks ,
107- sctx -> u );
76+ poly1305_2block_sse2 (dctx -> h .h , src , dctx -> r [ 0 ]. r ,
77+ blocks , dctx -> r [ 1 ]. r );
10878 src += POLY1305_BLOCK_SIZE * 2 * blocks ;
10979 srclen -= POLY1305_BLOCK_SIZE * 2 * blocks ;
11080 }
11181 if (srclen >= POLY1305_BLOCK_SIZE ) {
112- poly1305_block_sse2 (dctx -> h .h , src , dctx -> r .r , 1 );
82+ poly1305_block_sse2 (dctx -> h .h , src , dctx -> r [ 0 ] .r , 1 );
11383 srclen -= POLY1305_BLOCK_SIZE ;
11484 }
11585 return srclen ;
@@ -159,10 +129,10 @@ static int poly1305_simd_update(struct shash_desc *desc,
159129
160130static struct shash_alg alg = {
161131 .digestsize = POLY1305_DIGEST_SIZE ,
162- .init = poly1305_simd_init ,
132+ .init = crypto_poly1305_init ,
163133 .update = poly1305_simd_update ,
164134 .final = crypto_poly1305_final ,
165- .descsize = sizeof (struct poly1305_simd_desc_ctx ),
135+ .descsize = sizeof (struct poly1305_desc_ctx ),
166136 .base = {
167137 .cra_name = "poly1305" ,
168138 .cra_driver_name = "poly1305-simd" ,
@@ -177,14 +147,14 @@ static int __init poly1305_simd_mod_init(void)
177147 if (!boot_cpu_has (X86_FEATURE_XMM2 ))
178148 return - ENODEV ;
179149
180- #ifdef CONFIG_AS_AVX2
181- poly1305_use_avx2 = boot_cpu_has (X86_FEATURE_AVX ) &&
150+ poly1305_use_avx2 = IS_ENABLED ( CONFIG_AS_AVX2 ) &&
151+ boot_cpu_has (X86_FEATURE_AVX ) &&
182152 boot_cpu_has (X86_FEATURE_AVX2 ) &&
183153 cpu_has_xfeatures (XFEATURE_MASK_SSE | XFEATURE_MASK_YMM , NULL );
184- alg .descsize = sizeof (struct poly1305_simd_desc_ctx );
154+ alg .descsize = sizeof (struct poly1305_desc_ctx ) + 5 * sizeof ( u32 );
185155 if (poly1305_use_avx2 )
186156 alg .descsize += 10 * sizeof (u32 );
187- #endif
157+
188158 return crypto_register_shash (& alg );
189159}
190160
0 commit comments