1313
1414#include <crypto/algapi.h>
1515#include <crypto/internal/hash.h>
16+ #include <crypto/poly1305.h>
1617#include <linux/crypto.h>
1718#include <linux/kernel.h>
1819#include <linux/module.h>
1920
20- #define POLY1305_BLOCK_SIZE 16
21- #define POLY1305_KEY_SIZE 32
22- #define POLY1305_DIGEST_SIZE 16
23-
24- struct poly1305_desc_ctx {
25- /* key */
26- u32 r [5 ];
27- /* finalize key */
28- u32 s [4 ];
29- /* accumulator */
30- u32 h [5 ];
31- /* partial buffer */
32- u8 buf [POLY1305_BLOCK_SIZE ];
33- /* bytes used in partial buffer */
34- unsigned int buflen ;
35- /* r key has been set */
36- bool rset ;
37- /* s key has been set */
38- bool sset ;
39- };
40-
4121static inline u64 mlt (u64 a , u64 b )
4222{
4323 return a * b ;
@@ -58,7 +38,7 @@ static inline u32 le32_to_cpuvp(const void *p)
5838 return le32_to_cpup (p );
5939}
6040
61- static int poly1305_init (struct shash_desc * desc )
41+ int crypto_poly1305_init (struct shash_desc * desc )
6242{
6343 struct poly1305_desc_ctx * dctx = shash_desc_ctx (desc );
6444
@@ -69,8 +49,9 @@ static int poly1305_init(struct shash_desc *desc)
6949
7050 return 0 ;
7151}
52+ EXPORT_SYMBOL_GPL (crypto_poly1305_init );
7253
73- static int poly1305_setkey (struct crypto_shash * tfm ,
54+ int crypto_poly1305_setkey (struct crypto_shash * tfm ,
7455 const u8 * key , unsigned int keylen )
7556{
7657 /* Poly1305 requires a unique key for each tag, which implies that
@@ -79,6 +60,7 @@ static int poly1305_setkey(struct crypto_shash *tfm,
7960 * the update() call. */
8061 return - ENOTSUPP ;
8162}
63+ EXPORT_SYMBOL_GPL (crypto_poly1305_setkey );
8264
8365static void poly1305_setrkey (struct poly1305_desc_ctx * dctx , const u8 * key )
8466{
@@ -98,16 +80,10 @@ static void poly1305_setskey(struct poly1305_desc_ctx *dctx, const u8 *key)
9880 dctx -> s [3 ] = le32_to_cpuvp (key + 12 );
9981}
10082
101- static unsigned int poly1305_blocks (struct poly1305_desc_ctx * dctx ,
102- const u8 * src , unsigned int srclen ,
103- u32 hibit )
83+ unsigned int crypto_poly1305_setdesckey (struct poly1305_desc_ctx * dctx ,
84+ const u8 * src , unsigned int srclen )
10485{
105- u32 r0 , r1 , r2 , r3 , r4 ;
106- u32 s1 , s2 , s3 , s4 ;
107- u32 h0 , h1 , h2 , h3 , h4 ;
108- u64 d0 , d1 , d2 , d3 , d4 ;
109-
110- if (unlikely (!dctx -> sset )) {
86+ if (!dctx -> sset ) {
11187 if (!dctx -> rset && srclen >= POLY1305_BLOCK_SIZE ) {
11288 poly1305_setrkey (dctx , src );
11389 src += POLY1305_BLOCK_SIZE ;
@@ -121,6 +97,25 @@ static unsigned int poly1305_blocks(struct poly1305_desc_ctx *dctx,
12197 dctx -> sset = true;
12298 }
12399 }
100+ return srclen ;
101+ }
102+ EXPORT_SYMBOL_GPL (crypto_poly1305_setdesckey );
103+
104+ static unsigned int poly1305_blocks (struct poly1305_desc_ctx * dctx ,
105+ const u8 * src , unsigned int srclen ,
106+ u32 hibit )
107+ {
108+ u32 r0 , r1 , r2 , r3 , r4 ;
109+ u32 s1 , s2 , s3 , s4 ;
110+ u32 h0 , h1 , h2 , h3 , h4 ;
111+ u64 d0 , d1 , d2 , d3 , d4 ;
112+ unsigned int datalen ;
113+
114+ if (unlikely (!dctx -> sset )) {
115+ datalen = crypto_poly1305_setdesckey (dctx , src , srclen );
116+ src += srclen - datalen ;
117+ srclen = datalen ;
118+ }
124119
125120 r0 = dctx -> r [0 ];
126121 r1 = dctx -> r [1 ];
@@ -181,7 +176,7 @@ static unsigned int poly1305_blocks(struct poly1305_desc_ctx *dctx,
181176 return srclen ;
182177}
183178
184- static int poly1305_update (struct shash_desc * desc ,
179+ int crypto_poly1305_update (struct shash_desc * desc ,
185180 const u8 * src , unsigned int srclen )
186181{
187182 struct poly1305_desc_ctx * dctx = shash_desc_ctx (desc );
@@ -214,8 +209,9 @@ static int poly1305_update(struct shash_desc *desc,
214209
215210 return 0 ;
216211}
212+ EXPORT_SYMBOL_GPL (crypto_poly1305_update );
217213
218- static int poly1305_final (struct shash_desc * desc , u8 * dst )
214+ int crypto_poly1305_final (struct shash_desc * desc , u8 * dst )
219215{
220216 struct poly1305_desc_ctx * dctx = shash_desc_ctx (desc );
221217 __le32 * mac = (__le32 * )dst ;
@@ -282,13 +278,14 @@ static int poly1305_final(struct shash_desc *desc, u8 *dst)
282278
283279 return 0 ;
284280}
281+ EXPORT_SYMBOL_GPL (crypto_poly1305_final );
285282
286283static struct shash_alg poly1305_alg = {
287284 .digestsize = POLY1305_DIGEST_SIZE ,
288- .init = poly1305_init ,
289- .update = poly1305_update ,
290- .final = poly1305_final ,
291- .setkey = poly1305_setkey ,
285+ .init = crypto_poly1305_init ,
286+ .update = crypto_poly1305_update ,
287+ .final = crypto_poly1305_final ,
288+ .setkey = crypto_poly1305_setkey ,
292289 .descsize = sizeof (struct poly1305_desc_ctx ),
293290 .base = {
294291 .cra_name = "poly1305" ,
0 commit comments