1 // SPDX-License-Identifier: GPL-2.0
3 * Poly1305 authenticator algorithm, RFC7539.
5 * Copyright 2023- IBM Corp. All rights reserved.
8 #include <crypto/algapi.h>
9 #include <linux/crypto.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/jump_label.h>
13 #include <crypto/internal/hash.h>
14 #include <crypto/internal/poly1305.h>
15 #include <crypto/internal/simd.h>
16 #include <linux/cpufeature.h>
17 #include <asm/unaligned.h>
19 #include <asm/switch_to.h>
21 asmlinkage void poly1305_p10le_4blocks(void *h, const u8 *m, u32 mlen);
22 asmlinkage void poly1305_64s(void *h, const u8 *m, u32 mlen, int highbit);
23 asmlinkage void poly1305_emit_64(void *h, void *s, u8 *dst);
25 static void vsx_begin(void)
31 static void vsx_end(void)
37 static int crypto_poly1305_p10_init(struct shash_desc *desc)
39 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
41 poly1305_core_init(&dctx->h);
49 static unsigned int crypto_poly1305_setdctxkey(struct poly1305_desc_ctx *dctx,
50 const u8 *inp, unsigned int len)
54 if (unlikely(!dctx->sset)) {
55 if (!dctx->rset && len >= POLY1305_BLOCK_SIZE) {
56 struct poly1305_core_key *key = &dctx->core_r;
58 key->key.r64[0] = get_unaligned_le64(&inp[0]);
59 key->key.r64[1] = get_unaligned_le64(&inp[8]);
60 inp += POLY1305_BLOCK_SIZE;
61 len -= POLY1305_BLOCK_SIZE;
62 acc += POLY1305_BLOCK_SIZE;
65 if (len >= POLY1305_BLOCK_SIZE) {
66 dctx->s[0] = get_unaligned_le32(&inp[0]);
67 dctx->s[1] = get_unaligned_le32(&inp[4]);
68 dctx->s[2] = get_unaligned_le32(&inp[8]);
69 dctx->s[3] = get_unaligned_le32(&inp[12]);
70 acc += POLY1305_BLOCK_SIZE;
77 static int crypto_poly1305_p10_update(struct shash_desc *desc,
78 const u8 *src, unsigned int srclen)
80 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
81 unsigned int bytes, used;
83 if (unlikely(dctx->buflen)) {
84 bytes = min(srclen, POLY1305_BLOCK_SIZE - dctx->buflen);
85 memcpy(dctx->buf + dctx->buflen, src, bytes);
88 dctx->buflen += bytes;
90 if (dctx->buflen == POLY1305_BLOCK_SIZE) {
91 if (likely(!crypto_poly1305_setdctxkey(dctx, dctx->buf,
92 POLY1305_BLOCK_SIZE))) {
94 poly1305_64s(&dctx->h, dctx->buf,
95 POLY1305_BLOCK_SIZE, 1);
102 if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
103 bytes = round_down(srclen, POLY1305_BLOCK_SIZE);
104 used = crypto_poly1305_setdctxkey(dctx, src, bytes);
109 if (crypto_simd_usable() && (srclen >= POLY1305_BLOCK_SIZE*4)) {
111 poly1305_p10le_4blocks(&dctx->h, src, srclen);
113 src += srclen - (srclen % (POLY1305_BLOCK_SIZE * 4));
114 srclen %= POLY1305_BLOCK_SIZE * 4;
116 while (srclen >= POLY1305_BLOCK_SIZE) {
118 poly1305_64s(&dctx->h, src, POLY1305_BLOCK_SIZE, 1);
120 srclen -= POLY1305_BLOCK_SIZE;
121 src += POLY1305_BLOCK_SIZE;
125 if (unlikely(srclen)) {
126 dctx->buflen = srclen;
127 memcpy(dctx->buf, src, srclen);
133 static int crypto_poly1305_p10_final(struct shash_desc *desc, u8 *dst)
135 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
137 if (unlikely(!dctx->sset))
140 if ((dctx->buflen)) {
141 dctx->buf[dctx->buflen++] = 1;
142 memset(dctx->buf + dctx->buflen, 0,
143 POLY1305_BLOCK_SIZE - dctx->buflen);
145 poly1305_64s(&dctx->h, dctx->buf, POLY1305_BLOCK_SIZE, 0);
150 poly1305_emit_64(&dctx->h, &dctx->s, dst);
154 static struct shash_alg poly1305_alg = {
155 .digestsize = POLY1305_DIGEST_SIZE,
156 .init = crypto_poly1305_p10_init,
157 .update = crypto_poly1305_p10_update,
158 .final = crypto_poly1305_p10_final,
159 .descsize = sizeof(struct poly1305_desc_ctx),
161 .cra_name = "poly1305",
162 .cra_driver_name = "poly1305-p10",
164 .cra_blocksize = POLY1305_BLOCK_SIZE,
165 .cra_module = THIS_MODULE,
169 static int __init poly1305_p10_init(void)
171 return crypto_register_shash(&poly1305_alg);
174 static void __exit poly1305_p10_exit(void)
176 crypto_unregister_shash(&poly1305_alg);
179 module_cpu_feature_match(PPC_MODULE_FEATURE_P10, poly1305_p10_init);
180 module_exit(poly1305_p10_exit);
182 MODULE_LICENSE("GPL");
184 MODULE_DESCRIPTION("Optimized Poly1305 for P10");
185 MODULE_ALIAS_CRYPTO("poly1305");
186 MODULE_ALIAS_CRYPTO("poly1305-p10");