0001
0002
0003
0004
0005
0006
0007
0008 #include <asm/unaligned.h>
0009 #include <crypto/algapi.h>
0010 #include <crypto/internal/hash.h>
0011 #include <crypto/internal/poly1305.h>
0012 #include <linux/cpufeature.h>
0013 #include <linux/crypto.h>
0014 #include <linux/module.h>
0015
0016 asmlinkage void poly1305_init_mips(void *state, const u8 *key);
0017 asmlinkage void poly1305_blocks_mips(void *state, const u8 *src, u32 len, u32 hibit);
0018 asmlinkage void poly1305_emit_mips(void *state, u8 *digest, const u32 *nonce);
0019
0020 void poly1305_init_arch(struct poly1305_desc_ctx *dctx, const u8 key[POLY1305_KEY_SIZE])
0021 {
0022 poly1305_init_mips(&dctx->h, key);
0023 dctx->s[0] = get_unaligned_le32(key + 16);
0024 dctx->s[1] = get_unaligned_le32(key + 20);
0025 dctx->s[2] = get_unaligned_le32(key + 24);
0026 dctx->s[3] = get_unaligned_le32(key + 28);
0027 dctx->buflen = 0;
0028 }
0029 EXPORT_SYMBOL(poly1305_init_arch);
0030
0031 static int mips_poly1305_init(struct shash_desc *desc)
0032 {
0033 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
0034
0035 dctx->buflen = 0;
0036 dctx->rset = 0;
0037 dctx->sset = false;
0038
0039 return 0;
0040 }
0041
0042 static void mips_poly1305_blocks(struct poly1305_desc_ctx *dctx, const u8 *src,
0043 u32 len, u32 hibit)
0044 {
0045 if (unlikely(!dctx->sset)) {
0046 if (!dctx->rset) {
0047 poly1305_init_mips(&dctx->h, src);
0048 src += POLY1305_BLOCK_SIZE;
0049 len -= POLY1305_BLOCK_SIZE;
0050 dctx->rset = 1;
0051 }
0052 if (len >= POLY1305_BLOCK_SIZE) {
0053 dctx->s[0] = get_unaligned_le32(src + 0);
0054 dctx->s[1] = get_unaligned_le32(src + 4);
0055 dctx->s[2] = get_unaligned_le32(src + 8);
0056 dctx->s[3] = get_unaligned_le32(src + 12);
0057 src += POLY1305_BLOCK_SIZE;
0058 len -= POLY1305_BLOCK_SIZE;
0059 dctx->sset = true;
0060 }
0061 if (len < POLY1305_BLOCK_SIZE)
0062 return;
0063 }
0064
0065 len &= ~(POLY1305_BLOCK_SIZE - 1);
0066
0067 poly1305_blocks_mips(&dctx->h, src, len, hibit);
0068 }
0069
0070 static int mips_poly1305_update(struct shash_desc *desc, const u8 *src,
0071 unsigned int len)
0072 {
0073 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
0074
0075 if (unlikely(dctx->buflen)) {
0076 u32 bytes = min(len, POLY1305_BLOCK_SIZE - dctx->buflen);
0077
0078 memcpy(dctx->buf + dctx->buflen, src, bytes);
0079 src += bytes;
0080 len -= bytes;
0081 dctx->buflen += bytes;
0082
0083 if (dctx->buflen == POLY1305_BLOCK_SIZE) {
0084 mips_poly1305_blocks(dctx, dctx->buf, POLY1305_BLOCK_SIZE, 1);
0085 dctx->buflen = 0;
0086 }
0087 }
0088
0089 if (likely(len >= POLY1305_BLOCK_SIZE)) {
0090 mips_poly1305_blocks(dctx, src, len, 1);
0091 src += round_down(len, POLY1305_BLOCK_SIZE);
0092 len %= POLY1305_BLOCK_SIZE;
0093 }
0094
0095 if (unlikely(len)) {
0096 dctx->buflen = len;
0097 memcpy(dctx->buf, src, len);
0098 }
0099 return 0;
0100 }
0101
0102 void poly1305_update_arch(struct poly1305_desc_ctx *dctx, const u8 *src,
0103 unsigned int nbytes)
0104 {
0105 if (unlikely(dctx->buflen)) {
0106 u32 bytes = min(nbytes, POLY1305_BLOCK_SIZE - dctx->buflen);
0107
0108 memcpy(dctx->buf + dctx->buflen, src, bytes);
0109 src += bytes;
0110 nbytes -= bytes;
0111 dctx->buflen += bytes;
0112
0113 if (dctx->buflen == POLY1305_BLOCK_SIZE) {
0114 poly1305_blocks_mips(&dctx->h, dctx->buf,
0115 POLY1305_BLOCK_SIZE, 1);
0116 dctx->buflen = 0;
0117 }
0118 }
0119
0120 if (likely(nbytes >= POLY1305_BLOCK_SIZE)) {
0121 unsigned int len = round_down(nbytes, POLY1305_BLOCK_SIZE);
0122
0123 poly1305_blocks_mips(&dctx->h, src, len, 1);
0124 src += len;
0125 nbytes %= POLY1305_BLOCK_SIZE;
0126 }
0127
0128 if (unlikely(nbytes)) {
0129 dctx->buflen = nbytes;
0130 memcpy(dctx->buf, src, nbytes);
0131 }
0132 }
0133 EXPORT_SYMBOL(poly1305_update_arch);
0134
0135 void poly1305_final_arch(struct poly1305_desc_ctx *dctx, u8 *dst)
0136 {
0137 if (unlikely(dctx->buflen)) {
0138 dctx->buf[dctx->buflen++] = 1;
0139 memset(dctx->buf + dctx->buflen, 0,
0140 POLY1305_BLOCK_SIZE - dctx->buflen);
0141 poly1305_blocks_mips(&dctx->h, dctx->buf, POLY1305_BLOCK_SIZE, 0);
0142 }
0143
0144 poly1305_emit_mips(&dctx->h, dst, dctx->s);
0145 *dctx = (struct poly1305_desc_ctx){};
0146 }
0147 EXPORT_SYMBOL(poly1305_final_arch);
0148
0149 static int mips_poly1305_final(struct shash_desc *desc, u8 *dst)
0150 {
0151 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
0152
0153 if (unlikely(!dctx->sset))
0154 return -ENOKEY;
0155
0156 poly1305_final_arch(dctx, dst);
0157 return 0;
0158 }
0159
0160 static struct shash_alg mips_poly1305_alg = {
0161 .init = mips_poly1305_init,
0162 .update = mips_poly1305_update,
0163 .final = mips_poly1305_final,
0164 .digestsize = POLY1305_DIGEST_SIZE,
0165 .descsize = sizeof(struct poly1305_desc_ctx),
0166
0167 .base.cra_name = "poly1305",
0168 .base.cra_driver_name = "poly1305-mips",
0169 .base.cra_priority = 200,
0170 .base.cra_blocksize = POLY1305_BLOCK_SIZE,
0171 .base.cra_module = THIS_MODULE,
0172 };
0173
0174 static int __init mips_poly1305_mod_init(void)
0175 {
0176 return IS_REACHABLE(CONFIG_CRYPTO_HASH) ?
0177 crypto_register_shash(&mips_poly1305_alg) : 0;
0178 }
0179
0180 static void __exit mips_poly1305_mod_exit(void)
0181 {
0182 if (IS_REACHABLE(CONFIG_CRYPTO_HASH))
0183 crypto_unregister_shash(&mips_poly1305_alg);
0184 }
0185
0186 module_init(mips_poly1305_mod_init);
0187 module_exit(mips_poly1305_mod_exit);
0188
0189 MODULE_LICENSE("GPL v2");
0190 MODULE_ALIAS_CRYPTO("poly1305");
0191 MODULE_ALIAS_CRYPTO("poly1305-mips");