2121#define POLY1305_KEY_SIZE 32
2222#define POLY1305_DIGEST_SIZE 16
2323
24- struct poly1305_ctx {
24+ struct poly1305_desc_ctx {
2525 /* key */
2626 u32 r [5 ];
2727 /* finalize key */
2828 u32 s [4 ];
29- };
30-
31- struct poly1305_desc_ctx {
3229 /* accumulator */
3330 u32 h [5 ];
3431 /* partial buffer */
3532 u8 buf [POLY1305_BLOCK_SIZE ];
3633 /* bytes used in partial buffer */
3734 unsigned int buflen ;
35+ /* r key has been set */
36+ bool rset ;
37+ /* s key has been set */
38+ bool sset ;
3839};
3940
4041static inline u64 mlt (u64 a , u64 b )
@@ -63,49 +64,69 @@ static int poly1305_init(struct shash_desc *desc)
6364
6465 memset (dctx -> h , 0 , sizeof (dctx -> h ));
6566 dctx -> buflen = 0 ;
67+ dctx -> rset = false;
68+ dctx -> sset = false;
6669
6770 return 0 ;
6871}
6972
7073static int poly1305_setkey (struct crypto_shash * tfm ,
7174 const u8 * key , unsigned int keylen )
7275{
73- struct poly1305_ctx * ctx = crypto_shash_ctx ( tfm );
74-
75- if ( keylen != POLY1305_KEY_SIZE ) {
76- crypto_shash_set_flags ( tfm , CRYPTO_TFM_RES_BAD_KEY_LEN );
77- return - EINVAL ;
78- }
76+ /* Poly1305 requires a unique key for each tag, which implies that
77+ * we can't set it on the tfm that gets accessed by multiple users
78+ * simultaneously. Instead we expect the key as the first 32 bytes in
79+ * the update() call. */
80+ return - ENOTSUPP ;
81+ }
7982
83+ static void poly1305_setrkey (struct poly1305_desc_ctx * dctx , const u8 * key )
84+ {
8085 /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
81- ctx -> r [0 ] = (le32_to_cpuvp (key + 0 ) >> 0 ) & 0x3ffffff ;
82- ctx -> r [1 ] = (le32_to_cpuvp (key + 3 ) >> 2 ) & 0x3ffff03 ;
83- ctx -> r [2 ] = (le32_to_cpuvp (key + 6 ) >> 4 ) & 0x3ffc0ff ;
84- ctx -> r [3 ] = (le32_to_cpuvp (key + 9 ) >> 6 ) & 0x3f03fff ;
85- ctx -> r [4 ] = (le32_to_cpuvp (key + 12 ) >> 8 ) & 0x00fffff ;
86-
87- ctx -> s [0 ] = le32_to_cpuvp (key + 16 );
88- ctx -> s [1 ] = le32_to_cpuvp (key + 20 );
89- ctx -> s [2 ] = le32_to_cpuvp (key + 24 );
90- ctx -> s [3 ] = le32_to_cpuvp (key + 28 );
86+ dctx -> r [0 ] = (le32_to_cpuvp (key + 0 ) >> 0 ) & 0x3ffffff ;
87+ dctx -> r [1 ] = (le32_to_cpuvp (key + 3 ) >> 2 ) & 0x3ffff03 ;
88+ dctx -> r [2 ] = (le32_to_cpuvp (key + 6 ) >> 4 ) & 0x3ffc0ff ;
89+ dctx -> r [3 ] = (le32_to_cpuvp (key + 9 ) >> 6 ) & 0x3f03fff ;
90+ dctx -> r [4 ] = (le32_to_cpuvp (key + 12 ) >> 8 ) & 0x00fffff ;
91+ }
9192
92- return 0 ;
93+ static void poly1305_setskey (struct poly1305_desc_ctx * dctx , const u8 * key )
94+ {
95+ dctx -> s [0 ] = le32_to_cpuvp (key + 0 );
96+ dctx -> s [1 ] = le32_to_cpuvp (key + 4 );
97+ dctx -> s [2 ] = le32_to_cpuvp (key + 8 );
98+ dctx -> s [3 ] = le32_to_cpuvp (key + 12 );
9399}
94100
95101static unsigned int poly1305_blocks (struct poly1305_desc_ctx * dctx ,
96- struct poly1305_ctx * ctx , const u8 * src ,
97- unsigned int srclen , u32 hibit )
102+ const u8 * src , unsigned int srclen ,
103+ u32 hibit )
98104{
99105 u32 r0 , r1 , r2 , r3 , r4 ;
100106 u32 s1 , s2 , s3 , s4 ;
101107 u32 h0 , h1 , h2 , h3 , h4 ;
102108 u64 d0 , d1 , d2 , d3 , d4 ;
103109
104- r0 = ctx -> r [0 ];
105- r1 = ctx -> r [1 ];
106- r2 = ctx -> r [2 ];
107- r3 = ctx -> r [3 ];
108- r4 = ctx -> r [4 ];
110+ if (unlikely (!dctx -> sset )) {
111+ if (!dctx -> rset && srclen >= POLY1305_BLOCK_SIZE ) {
112+ poly1305_setrkey (dctx , src );
113+ src += POLY1305_BLOCK_SIZE ;
114+ srclen -= POLY1305_BLOCK_SIZE ;
115+ dctx -> rset = true;
116+ }
117+ if (srclen >= POLY1305_BLOCK_SIZE ) {
118+ poly1305_setskey (dctx , src );
119+ src += POLY1305_BLOCK_SIZE ;
120+ srclen -= POLY1305_BLOCK_SIZE ;
121+ dctx -> sset = true;
122+ }
123+ }
124+
125+ r0 = dctx -> r [0 ];
126+ r1 = dctx -> r [1 ];
127+ r2 = dctx -> r [2 ];
128+ r3 = dctx -> r [3 ];
129+ r4 = dctx -> r [4 ];
109130
110131 s1 = r1 * 5 ;
111132 s2 = r2 * 5 ;
@@ -164,7 +185,6 @@ static int poly1305_update(struct shash_desc *desc,
164185 const u8 * src , unsigned int srclen )
165186{
166187 struct poly1305_desc_ctx * dctx = shash_desc_ctx (desc );
167- struct poly1305_ctx * ctx = crypto_shash_ctx (desc -> tfm );
168188 unsigned int bytes ;
169189
170190 if (unlikely (dctx -> buflen )) {
@@ -175,14 +195,14 @@ static int poly1305_update(struct shash_desc *desc,
175195 dctx -> buflen += bytes ;
176196
177197 if (dctx -> buflen == POLY1305_BLOCK_SIZE ) {
178- poly1305_blocks (dctx , ctx , dctx -> buf ,
198+ poly1305_blocks (dctx , dctx -> buf ,
179199 POLY1305_BLOCK_SIZE , 1 << 24 );
180200 dctx -> buflen = 0 ;
181201 }
182202 }
183203
184204 if (likely (srclen >= POLY1305_BLOCK_SIZE )) {
185- bytes = poly1305_blocks (dctx , ctx , src , srclen , 1 << 24 );
205+ bytes = poly1305_blocks (dctx , src , srclen , 1 << 24 );
186206 src += srclen - bytes ;
187207 srclen = bytes ;
188208 }
@@ -198,18 +218,20 @@ static int poly1305_update(struct shash_desc *desc,
198218static int poly1305_final (struct shash_desc * desc , u8 * dst )
199219{
200220 struct poly1305_desc_ctx * dctx = shash_desc_ctx (desc );
201- struct poly1305_ctx * ctx = crypto_shash_ctx (desc -> tfm );
202221 __le32 * mac = (__le32 * )dst ;
203222 u32 h0 , h1 , h2 , h3 , h4 ;
204223 u32 g0 , g1 , g2 , g3 , g4 ;
205224 u32 mask ;
206225 u64 f = 0 ;
207226
227+ if (unlikely (!dctx -> sset ))
228+ return - ENOKEY ;
229+
208230 if (unlikely (dctx -> buflen )) {
209231 dctx -> buf [dctx -> buflen ++ ] = 1 ;
210232 memset (dctx -> buf + dctx -> buflen , 0 ,
211233 POLY1305_BLOCK_SIZE - dctx -> buflen );
212- poly1305_blocks (dctx , ctx , dctx -> buf , POLY1305_BLOCK_SIZE , 0 );
234+ poly1305_blocks (dctx , dctx -> buf , POLY1305_BLOCK_SIZE , 0 );
213235 }
214236
215237 /* fully carry h */
@@ -253,10 +275,10 @@ static int poly1305_final(struct shash_desc *desc, u8 *dst)
253275 h3 = (h3 >> 18 ) | (h4 << 8 );
254276
255277 /* mac = (h + s) % (2^128) */
256- f = (f >> 32 ) + h0 + ctx -> s [0 ]; mac [0 ] = cpu_to_le32 (f );
257- f = (f >> 32 ) + h1 + ctx -> s [1 ]; mac [1 ] = cpu_to_le32 (f );
258- f = (f >> 32 ) + h2 + ctx -> s [2 ]; mac [2 ] = cpu_to_le32 (f );
259- f = (f >> 32 ) + h3 + ctx -> s [3 ]; mac [3 ] = cpu_to_le32 (f );
278+ f = (f >> 32 ) + h0 + dctx -> s [0 ]; mac [0 ] = cpu_to_le32 (f );
279+ f = (f >> 32 ) + h1 + dctx -> s [1 ]; mac [1 ] = cpu_to_le32 (f );
280+ f = (f >> 32 ) + h2 + dctx -> s [2 ]; mac [2 ] = cpu_to_le32 (f );
281+ f = (f >> 32 ) + h3 + dctx -> s [3 ]; mac [3 ] = cpu_to_le32 (f );
260282
261283 return 0 ;
262284}
@@ -275,7 +297,6 @@ static struct shash_alg poly1305_alg = {
275297 .cra_flags = CRYPTO_ALG_TYPE_SHASH ,
276298 .cra_alignmask = sizeof (u32 ) - 1 ,
277299 .cra_blocksize = POLY1305_BLOCK_SIZE ,
278- .cra_ctxsize = sizeof (struct poly1305_ctx ),
279300 .cra_module = THIS_MODULE ,
280301 },
281302};
0 commit comments