Path: blob/master/arch/powerpc/crypto/aes-gcm-p10-glue.c
26424 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Glue code for accelerated AES-GCM stitched implementation for ppc64le.3*4* Copyright 2022- IBM Inc. All rights reserved5*/67#include <linux/unaligned.h>8#include <asm/simd.h>9#include <asm/switch_to.h>10#include <crypto/gcm.h>11#include <crypto/aes.h>12#include <crypto/algapi.h>13#include <crypto/b128ops.h>14#include <crypto/gf128mul.h>15#include <crypto/internal/simd.h>16#include <crypto/internal/aead.h>17#include <crypto/internal/hash.h>18#include <crypto/internal/skcipher.h>19#include <crypto/scatterwalk.h>20#include <linux/cpufeature.h>21#include <linux/crypto.h>22#include <linux/module.h>23#include <linux/types.h>2425#define PPC_ALIGN 1626#define GCM_IV_SIZE 1227#define RFC4106_NONCE_SIZE 42829MODULE_DESCRIPTION("PPC64le AES-GCM with Stitched implementation");30MODULE_AUTHOR("Danny Tsen <[email protected]");31MODULE_LICENSE("GPL v2");32MODULE_ALIAS_CRYPTO("aes");3334asmlinkage int aes_p10_set_encrypt_key(const u8 *userKey, const int bits,35void *key);36asmlinkage void aes_p10_encrypt(const u8 *in, u8 *out, const void *key);37asmlinkage void aes_p10_gcm_encrypt(const u8 *in, u8 *out, size_t len,38void *rkey, u8 *iv, void *Xi);39asmlinkage void aes_p10_gcm_decrypt(const u8 *in, u8 *out, size_t len,40void *rkey, u8 *iv, void *Xi);41asmlinkage void gcm_init_htable(unsigned char htable[], unsigned char Xi[]);42asmlinkage void gcm_ghash_p10(unsigned char *Xi, unsigned char *Htable,43unsigned char *aad, unsigned int alen);44asmlinkage void gcm_update(u8 *iv, void *Xi);4546struct aes_key {47u8 key[AES_MAX_KEYLENGTH];48u64 rounds;49};5051struct gcm_ctx {52u8 iv[16];53u8 ivtag[16];54u8 aad_hash[16];55u64 aadLen;56u64 Plen; /* offset 56 - used in aes_p10_gcm_{en/de}crypt */57u8 pblock[16];58};59struct Hash_ctx {60u8 H[16]; /* subkey */61u8 Htable[256]; /* Xi, Hash table(offset 32) */62};6364struct p10_aes_gcm_ctx {65struct aes_key enc_key;66u8 nonce[RFC4106_NONCE_SIZE];67};6869static void vsx_begin(void)70{71preempt_disable();72pagefault_disable();73enable_kernel_vsx();74}7576static void vsx_end(void)77{78disable_kernel_vsx();79pagefault_enable();80preempt_enable();81}8283static void set_subkey(unsigned char *hash)84{85*(u64 *)&hash[0] = be64_to_cpup((__be64 *)&hash[0]);86*(u64 *)&hash[8] = be64_to_cpup((__be64 *)&hash[8]);87}8889/*90* Compute aad if any.91* - Hash aad and copy to Xi.92*/93static void set_aad(struct gcm_ctx *gctx, struct Hash_ctx *hash,94unsigned char *aad, int alen)95{96int i;97u8 nXi[16] = {0, };9899gctx->aadLen = alen;100i = alen & ~0xf;101if (i) {102gcm_ghash_p10(nXi, hash->Htable+32, aad, i);103aad += i;104alen -= i;105}106if (alen) {107for (i = 0; i < alen; i++)108nXi[i] ^= aad[i];109110memset(gctx->aad_hash, 0, 16);111gcm_ghash_p10(gctx->aad_hash, hash->Htable+32, nXi, 16);112} else {113memcpy(gctx->aad_hash, nXi, 16);114}115116memcpy(hash->Htable, gctx->aad_hash, 16);117}118119static void gcmp10_init(struct gcm_ctx *gctx, u8 *iv, unsigned char *rdkey,120struct Hash_ctx *hash, u8 *assoc, unsigned int assoclen)121{122__be32 counter = cpu_to_be32(1);123124aes_p10_encrypt(hash->H, hash->H, rdkey);125set_subkey(hash->H);126gcm_init_htable(hash->Htable+32, hash->H);127128*((__be32 *)(iv+12)) = counter;129130gctx->Plen = 0;131132/*133* Encrypt counter vector as iv tag and increment counter.134*/135aes_p10_encrypt(iv, gctx->ivtag, rdkey);136137counter = cpu_to_be32(2);138*((__be32 *)(iv+12)) = counter;139memcpy(gctx->iv, iv, 16);140141gctx->aadLen = assoclen;142memset(gctx->aad_hash, 0, 16);143if (assoclen)144set_aad(gctx, hash, assoc, assoclen);145}146147static void finish_tag(struct gcm_ctx *gctx, struct Hash_ctx *hash, int len)148{149int i;150unsigned char len_ac[16 + PPC_ALIGN];151unsigned char *aclen = PTR_ALIGN((void *)len_ac, PPC_ALIGN);152__be64 clen = cpu_to_be64(len << 3);153__be64 alen = cpu_to_be64(gctx->aadLen << 3);154155if (len == 0 && gctx->aadLen == 0) {156memcpy(hash->Htable, gctx->ivtag, 16);157return;158}159160/*161* Len is in bits.162*/163*((__be64 *)(aclen)) = alen;164*((__be64 *)(aclen+8)) = clen;165166/*167* hash (AAD len and len)168*/169gcm_ghash_p10(hash->Htable, hash->Htable+32, aclen, 16);170171for (i = 0; i < 16; i++)172hash->Htable[i] ^= gctx->ivtag[i];173}174175static int set_authsize(struct crypto_aead *tfm, unsigned int authsize)176{177switch (authsize) {178case 4:179case 8:180case 12:181case 13:182case 14:183case 15:184case 16:185break;186default:187return -EINVAL;188}189190return 0;191}192193static int p10_aes_gcm_setkey(struct crypto_aead *aead, const u8 *key,194unsigned int keylen)195{196struct crypto_tfm *tfm = crypto_aead_tfm(aead);197struct p10_aes_gcm_ctx *ctx = crypto_tfm_ctx(tfm);198int ret;199200vsx_begin();201ret = aes_p10_set_encrypt_key(key, keylen * 8, &ctx->enc_key);202vsx_end();203204return ret ? -EINVAL : 0;205}206207static int p10_aes_gcm_crypt(struct aead_request *req, u8 *riv,208int assoclen, int enc)209{210struct crypto_tfm *tfm = req->base.tfm;211struct p10_aes_gcm_ctx *ctx = crypto_tfm_ctx(tfm);212u8 databuf[sizeof(struct gcm_ctx) + PPC_ALIGN];213struct gcm_ctx *gctx = PTR_ALIGN((void *)databuf, PPC_ALIGN);214u8 hashbuf[sizeof(struct Hash_ctx) + PPC_ALIGN];215struct Hash_ctx *hash = PTR_ALIGN((void *)hashbuf, PPC_ALIGN);216struct skcipher_walk walk;217u8 *assocmem = NULL;218u8 *assoc;219unsigned int cryptlen = req->cryptlen;220unsigned char ivbuf[AES_BLOCK_SIZE+PPC_ALIGN];221unsigned char *iv = PTR_ALIGN((void *)ivbuf, PPC_ALIGN);222int ret;223unsigned long auth_tag_len = crypto_aead_authsize(__crypto_aead_cast(tfm));224u8 otag[16];225int total_processed = 0;226int nbytes;227228memset(databuf, 0, sizeof(databuf));229memset(hashbuf, 0, sizeof(hashbuf));230memset(ivbuf, 0, sizeof(ivbuf));231memcpy(iv, riv, GCM_IV_SIZE);232233/* Linearize assoc, if not already linear */234if (req->src->length >= assoclen && req->src->length) {235assoc = sg_virt(req->src); /* ppc64 is !HIGHMEM */236} else {237gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?238GFP_KERNEL : GFP_ATOMIC;239240/* assoc can be any length, so must be on heap */241assocmem = kmalloc(assoclen, flags);242if (unlikely(!assocmem))243return -ENOMEM;244assoc = assocmem;245246scatterwalk_map_and_copy(assoc, req->src, 0, assoclen, 0);247}248249vsx_begin();250gcmp10_init(gctx, iv, (unsigned char *) &ctx->enc_key, hash, assoc, assoclen);251vsx_end();252253kfree(assocmem);254255if (enc)256ret = skcipher_walk_aead_encrypt(&walk, req, false);257else258ret = skcipher_walk_aead_decrypt(&walk, req, false);259if (ret)260return ret;261262while ((nbytes = walk.nbytes) > 0 && ret == 0) {263const u8 *src = walk.src.virt.addr;264u8 *dst = walk.dst.virt.addr;265u8 buf[AES_BLOCK_SIZE];266267if (unlikely(nbytes > 0 && nbytes < AES_BLOCK_SIZE))268src = dst = memcpy(buf, src, nbytes);269270vsx_begin();271if (enc)272aes_p10_gcm_encrypt(src, dst, nbytes,273&ctx->enc_key, gctx->iv, hash->Htable);274else275aes_p10_gcm_decrypt(src, dst, nbytes,276&ctx->enc_key, gctx->iv, hash->Htable);277278if (unlikely(nbytes > 0 && nbytes < AES_BLOCK_SIZE))279memcpy(walk.dst.virt.addr, buf, nbytes);280281vsx_end();282283total_processed += walk.nbytes;284ret = skcipher_walk_done(&walk, 0);285}286287if (ret)288return ret;289290/* Finalize hash */291vsx_begin();292gcm_update(gctx->iv, hash->Htable);293finish_tag(gctx, hash, total_processed);294vsx_end();295296/* copy Xi to end of dst */297if (enc)298scatterwalk_map_and_copy(hash->Htable, req->dst, req->assoclen + cryptlen,299auth_tag_len, 1);300else {301scatterwalk_map_and_copy(otag, req->src,302req->assoclen + cryptlen - auth_tag_len,303auth_tag_len, 0);304305if (crypto_memneq(otag, hash->Htable, auth_tag_len)) {306memzero_explicit(hash->Htable, 16);307return -EBADMSG;308}309}310311return 0;312}313314static int rfc4106_setkey(struct crypto_aead *tfm, const u8 *inkey,315unsigned int keylen)316{317struct p10_aes_gcm_ctx *ctx = crypto_aead_ctx(tfm);318int err;319320keylen -= RFC4106_NONCE_SIZE;321err = p10_aes_gcm_setkey(tfm, inkey, keylen);322if (err)323return err;324325memcpy(ctx->nonce, inkey + keylen, RFC4106_NONCE_SIZE);326return 0;327}328329static int rfc4106_setauthsize(struct crypto_aead *tfm, unsigned int authsize)330{331return crypto_rfc4106_check_authsize(authsize);332}333334static int rfc4106_encrypt(struct aead_request *req)335{336struct crypto_aead *aead = crypto_aead_reqtfm(req);337struct p10_aes_gcm_ctx *ctx = crypto_aead_ctx(aead);338u8 iv[AES_BLOCK_SIZE];339340memcpy(iv, ctx->nonce, RFC4106_NONCE_SIZE);341memcpy(iv + RFC4106_NONCE_SIZE, req->iv, GCM_RFC4106_IV_SIZE);342343return crypto_ipsec_check_assoclen(req->assoclen) ?:344p10_aes_gcm_crypt(req, iv, req->assoclen - GCM_RFC4106_IV_SIZE, 1);345}346347static int rfc4106_decrypt(struct aead_request *req)348{349struct crypto_aead *aead = crypto_aead_reqtfm(req);350struct p10_aes_gcm_ctx *ctx = crypto_aead_ctx(aead);351u8 iv[AES_BLOCK_SIZE];352353memcpy(iv, ctx->nonce, RFC4106_NONCE_SIZE);354memcpy(iv + RFC4106_NONCE_SIZE, req->iv, GCM_RFC4106_IV_SIZE);355356return crypto_ipsec_check_assoclen(req->assoclen) ?:357p10_aes_gcm_crypt(req, iv, req->assoclen - GCM_RFC4106_IV_SIZE, 0);358}359360static int p10_aes_gcm_encrypt(struct aead_request *req)361{362return p10_aes_gcm_crypt(req, req->iv, req->assoclen, 1);363}364365static int p10_aes_gcm_decrypt(struct aead_request *req)366{367return p10_aes_gcm_crypt(req, req->iv, req->assoclen, 0);368}369370static struct aead_alg gcm_aes_algs[] = {{371.ivsize = GCM_IV_SIZE,372.maxauthsize = 16,373374.setauthsize = set_authsize,375.setkey = p10_aes_gcm_setkey,376.encrypt = p10_aes_gcm_encrypt,377.decrypt = p10_aes_gcm_decrypt,378379.base.cra_name = "__gcm(aes)",380.base.cra_driver_name = "__aes_gcm_p10",381.base.cra_priority = 2100,382.base.cra_blocksize = 1,383.base.cra_ctxsize = sizeof(struct p10_aes_gcm_ctx)+3844 * sizeof(u64[2]),385.base.cra_module = THIS_MODULE,386.base.cra_flags = CRYPTO_ALG_INTERNAL,387}, {388.ivsize = GCM_RFC4106_IV_SIZE,389.maxauthsize = 16,390.setkey = rfc4106_setkey,391.setauthsize = rfc4106_setauthsize,392.encrypt = rfc4106_encrypt,393.decrypt = rfc4106_decrypt,394395.base.cra_name = "__rfc4106(gcm(aes))",396.base.cra_driver_name = "__rfc4106_aes_gcm_p10",397.base.cra_priority = 2100,398.base.cra_blocksize = 1,399.base.cra_ctxsize = sizeof(struct p10_aes_gcm_ctx) +4004 * sizeof(u64[2]),401.base.cra_module = THIS_MODULE,402.base.cra_flags = CRYPTO_ALG_INTERNAL,403}};404405static struct simd_aead_alg *p10_simd_aeads[ARRAY_SIZE(gcm_aes_algs)];406407static int __init p10_init(void)408{409int ret;410411if (!cpu_has_feature(CPU_FTR_ARCH_31))412return 0;413414ret = simd_register_aeads_compat(gcm_aes_algs,415ARRAY_SIZE(gcm_aes_algs),416p10_simd_aeads);417if (ret) {418simd_unregister_aeads(gcm_aes_algs, ARRAY_SIZE(gcm_aes_algs),419p10_simd_aeads);420return ret;421}422return 0;423}424425static void __exit p10_exit(void)426{427simd_unregister_aeads(gcm_aes_algs, ARRAY_SIZE(gcm_aes_algs),428p10_simd_aeads);429}430431module_init(p10_init);432module_exit(p10_exit);433434435