1 // SPDX-License-Identifier: GPL-2.0
3 * OpenSSL/Cryptogams accelerated Poly1305 transform for MIPS
8 #include <asm/unaligned.h>
9 #include <crypto/algapi.h>
10 #include <crypto/internal/hash.h>
11 #include <crypto/internal/poly1305.h>
12 #include <linux/cpufeature.h>
13 #include <linux/crypto.h>
14 #include <linux/module.h>
16 asmlinkage void poly1305_init_mips(void *state, const u8 *key);
17 asmlinkage void poly1305_blocks_mips(void *state, const u8 *src, u32 len, u32 hibit);
18 asmlinkage void poly1305_emit_mips(void *state, __le32 *digest, const u32 *nonce);
20 void poly1305_init_arch(struct poly1305_desc_ctx *dctx, const u8 *key)
22 poly1305_init_mips(&dctx->h, key);
23 dctx->s[0] = get_unaligned_le32(key + 16);
24 dctx->s[1] = get_unaligned_le32(key + 20);
25 dctx->s[2] = get_unaligned_le32(key + 24);
26 dctx->s[3] = get_unaligned_le32(key + 28);
29 EXPORT_SYMBOL(poly1305_init_arch);
31 static int mips_poly1305_init(struct shash_desc *desc)
33 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
42 static void mips_poly1305_blocks(struct poly1305_desc_ctx *dctx, const u8 *src,
45 if (unlikely(!dctx->sset)) {
47 poly1305_init_mips(&dctx->h, src);
48 src += POLY1305_BLOCK_SIZE;
49 len -= POLY1305_BLOCK_SIZE;
52 if (len >= POLY1305_BLOCK_SIZE) {
53 dctx->s[0] = get_unaligned_le32(src + 0);
54 dctx->s[1] = get_unaligned_le32(src + 4);
55 dctx->s[2] = get_unaligned_le32(src + 8);
56 dctx->s[3] = get_unaligned_le32(src + 12);
57 src += POLY1305_BLOCK_SIZE;
58 len -= POLY1305_BLOCK_SIZE;
61 if (len < POLY1305_BLOCK_SIZE)
65 len &= ~(POLY1305_BLOCK_SIZE - 1);
67 poly1305_blocks_mips(&dctx->h, src, len, hibit);
70 static int mips_poly1305_update(struct shash_desc *desc, const u8 *src,
73 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
75 if (unlikely(dctx->buflen)) {
76 u32 bytes = min(len, POLY1305_BLOCK_SIZE - dctx->buflen);
78 memcpy(dctx->buf + dctx->buflen, src, bytes);
81 dctx->buflen += bytes;
83 if (dctx->buflen == POLY1305_BLOCK_SIZE) {
84 mips_poly1305_blocks(dctx, dctx->buf, POLY1305_BLOCK_SIZE, 1);
89 if (likely(len >= POLY1305_BLOCK_SIZE)) {
90 mips_poly1305_blocks(dctx, src, len, 1);
91 src += round_down(len, POLY1305_BLOCK_SIZE);
92 len %= POLY1305_BLOCK_SIZE;
97 memcpy(dctx->buf, src, len);
102 void poly1305_update_arch(struct poly1305_desc_ctx *dctx, const u8 *src,
105 if (unlikely(dctx->buflen)) {
106 u32 bytes = min(nbytes, POLY1305_BLOCK_SIZE - dctx->buflen);
108 memcpy(dctx->buf + dctx->buflen, src, bytes);
111 dctx->buflen += bytes;
113 if (dctx->buflen == POLY1305_BLOCK_SIZE) {
114 poly1305_blocks_mips(&dctx->h, dctx->buf,
115 POLY1305_BLOCK_SIZE, 1);
120 if (likely(nbytes >= POLY1305_BLOCK_SIZE)) {
121 unsigned int len = round_down(nbytes, POLY1305_BLOCK_SIZE);
123 poly1305_blocks_mips(&dctx->h, src, len, 1);
125 nbytes %= POLY1305_BLOCK_SIZE;
128 if (unlikely(nbytes)) {
129 dctx->buflen = nbytes;
130 memcpy(dctx->buf, src, nbytes);
133 EXPORT_SYMBOL(poly1305_update_arch);
135 void poly1305_final_arch(struct poly1305_desc_ctx *dctx, u8 *dst)
140 if (unlikely(dctx->buflen)) {
141 dctx->buf[dctx->buflen++] = 1;
142 memset(dctx->buf + dctx->buflen, 0,
143 POLY1305_BLOCK_SIZE - dctx->buflen);
144 poly1305_blocks_mips(&dctx->h, dctx->buf, POLY1305_BLOCK_SIZE, 0);
147 poly1305_emit_mips(&dctx->h, digest, dctx->s);
149 /* mac = (h + s) % (2^128) */
150 f = (f >> 32) + le32_to_cpu(digest[0]);
151 put_unaligned_le32(f, dst);
152 f = (f >> 32) + le32_to_cpu(digest[1]);
153 put_unaligned_le32(f, dst + 4);
154 f = (f >> 32) + le32_to_cpu(digest[2]);
155 put_unaligned_le32(f, dst + 8);
156 f = (f >> 32) + le32_to_cpu(digest[3]);
157 put_unaligned_le32(f, dst + 12);
159 *dctx = (struct poly1305_desc_ctx){};
161 EXPORT_SYMBOL(poly1305_final_arch);
163 static int mips_poly1305_final(struct shash_desc *desc, u8 *dst)
165 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
167 if (unlikely(!dctx->sset))
170 poly1305_final_arch(dctx, dst);
174 static struct shash_alg mips_poly1305_alg = {
175 .init = mips_poly1305_init,
176 .update = mips_poly1305_update,
177 .final = mips_poly1305_final,
178 .digestsize = POLY1305_DIGEST_SIZE,
179 .descsize = sizeof(struct poly1305_desc_ctx),
181 .base.cra_name = "poly1305",
182 .base.cra_driver_name = "poly1305-mips",
183 .base.cra_priority = 200,
184 .base.cra_blocksize = POLY1305_BLOCK_SIZE,
185 .base.cra_module = THIS_MODULE,
188 static int __init mips_poly1305_mod_init(void)
190 return IS_REACHABLE(CONFIG_CRYPTO_HASH) ?
191 crypto_register_shash(&mips_poly1305_alg) : 0;
194 static void __exit mips_poly1305_mod_exit(void)
196 if (IS_REACHABLE(CONFIG_CRYPTO_HASH))
197 crypto_unregister_shash(&mips_poly1305_alg);
200 module_init(mips_poly1305_mod_init);
201 module_exit(mips_poly1305_mod_exit);
203 MODULE_LICENSE("GPL v2");
204 MODULE_ALIAS_CRYPTO("poly1305");
205 MODULE_ALIAS_CRYPTO("poly1305-mips");