^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Cryptographic API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * s390 implementation of the AES Cipher Algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * s390 Version:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright IBM Corp. 2005, 2017
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Author(s): Jan Glauber (jang@de.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Sebastian Siewior (sebastian@breakpoint.cc> SW-Fallback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Patrick Steuer <patrick.steuer@de.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Harald Freudenberger <freude@de.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Derived from "crypto/aes_generic.c"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define KMSG_COMPONENT "aes_s390"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <crypto/aes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <crypto/ghash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <crypto/internal/aead.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <crypto/internal/cipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/cpufeature.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/fips.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <crypto/xts.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <asm/cpacf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static u8 *ctrblk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static DEFINE_MUTEX(ctrblk_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static cpacf_mask_t km_functions, kmc_functions, kmctr_functions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) kma_functions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct s390_aes_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) u8 key[AES_MAX_KEY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned long fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct crypto_skcipher *skcipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct crypto_cipher *cip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) } fallback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct s390_xts_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) u8 key[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) u8 pcc_key[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) unsigned long fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct crypto_skcipher *fallback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct gcm_sg_walk {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct scatter_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) unsigned int walk_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) u8 *walk_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned int walk_bytes_remain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) u8 buf[AES_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) unsigned int buf_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) u8 *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) unsigned int nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static int setkey_fallback_cip(struct crypto_tfm *tfm, const u8 *in_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) sctx->fallback.cip->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) sctx->fallback.cip->base.crt_flags |= (tfm->crt_flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return crypto_cipher_setkey(sctx->fallback.cip, in_key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) unsigned long fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* Pick the correct function code based on the key length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) fc = (key_len == 16) ? CPACF_KM_AES_128 :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) (key_len == 24) ? CPACF_KM_AES_192 :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) (key_len == 32) ? CPACF_KM_AES_256 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* Check if the function code is available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) sctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (!sctx->fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return setkey_fallback_cip(tfm, in_key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) sctx->key_len = key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) memcpy(sctx->key, in_key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static void crypto_aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (unlikely(!sctx->fc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) crypto_cipher_encrypt_one(sctx->fallback.cip, out, in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) cpacf_km(sctx->fc, &sctx->key, out, in, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static void crypto_aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (unlikely(!sctx->fc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) crypto_cipher_decrypt_one(sctx->fallback.cip, out, in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) cpacf_km(sctx->fc | CPACF_DECRYPT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) &sctx->key, out, in, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static int fallback_init_cip(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) const char *name = tfm->__crt_alg->cra_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) sctx->fallback.cip = crypto_alloc_cipher(name, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) CRYPTO_ALG_NEED_FALLBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (IS_ERR(sctx->fallback.cip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) pr_err("Allocating AES fallback algorithm %s failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return PTR_ERR(sctx->fallback.cip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static void fallback_exit_cip(struct crypto_tfm *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct s390_aes_ctx *sctx = crypto_tfm_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) crypto_free_cipher(sctx->fallback.cip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) sctx->fallback.cip = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static struct crypto_alg aes_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) .cra_name = "aes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) .cra_driver_name = "aes-s390",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) .cra_priority = 300,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) .cra_flags = CRYPTO_ALG_TYPE_CIPHER |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) CRYPTO_ALG_NEED_FALLBACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) .cra_blocksize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .cra_ctxsize = sizeof(struct s390_aes_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) .cra_init = fallback_init_cip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) .cra_exit = fallback_exit_cip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) .cra_u = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) .cipher = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) .cia_min_keysize = AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) .cia_max_keysize = AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) .cia_setkey = aes_set_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) .cia_encrypt = crypto_aes_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) .cia_decrypt = crypto_aes_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static int setkey_fallback_skcipher(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) crypto_skcipher_clear_flags(sctx->fallback.skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) crypto_skcipher_set_flags(sctx->fallback.skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) crypto_skcipher_get_flags(tfm) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return crypto_skcipher_setkey(sctx->fallback.skcipher, key, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static int fallback_skcipher_crypt(struct s390_aes_ctx *sctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct skcipher_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) unsigned long modifier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct skcipher_request *subreq = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) *subreq = *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) skcipher_request_set_tfm(subreq, sctx->fallback.skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return (modifier & CPACF_DECRYPT) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) crypto_skcipher_decrypt(subreq) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) crypto_skcipher_encrypt(subreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static int ecb_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) unsigned long fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /* Pick the correct function code based on the key length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) fc = (key_len == 16) ? CPACF_KM_AES_128 :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) (key_len == 24) ? CPACF_KM_AES_192 :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) (key_len == 32) ? CPACF_KM_AES_256 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /* Check if the function code is available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) sctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (!sctx->fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return setkey_fallback_skcipher(tfm, in_key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) sctx->key_len = key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) memcpy(sctx->key, in_key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static int ecb_aes_crypt(struct skcipher_request *req, unsigned long modifier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) unsigned int nbytes, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (unlikely(!sctx->fc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return fallback_skcipher_crypt(sctx, req, modifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) ret = skcipher_walk_virt(&walk, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) while ((nbytes = walk.nbytes) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* only use complete blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) n = nbytes & ~(AES_BLOCK_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) cpacf_km(sctx->fc | modifier, sctx->key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) walk.dst.virt.addr, walk.src.virt.addr, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) ret = skcipher_walk_done(&walk, nbytes - n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) static int ecb_aes_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return ecb_aes_crypt(req, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static int ecb_aes_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return ecb_aes_crypt(req, CPACF_DECRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static int fallback_init_skcipher(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) const char *name = crypto_tfm_alg_name(&tfm->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) sctx->fallback.skcipher = crypto_alloc_skcipher(name, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (IS_ERR(sctx->fallback.skcipher)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) pr_err("Allocating AES fallback algorithm %s failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return PTR_ERR(sctx->fallback.skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) crypto_skcipher_reqsize(sctx->fallback.skcipher));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static void fallback_exit_skcipher(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) crypto_free_skcipher(sctx->fallback.skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) static struct skcipher_alg ecb_aes_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) .base.cra_name = "ecb(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) .base.cra_driver_name = "ecb-aes-s390",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) .base.cra_priority = 401, /* combo: aes + ecb + 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) .base.cra_blocksize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) .base.cra_ctxsize = sizeof(struct s390_aes_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) .init = fallback_init_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) .exit = fallback_exit_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) .min_keysize = AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) .max_keysize = AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) .setkey = ecb_aes_set_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) .encrypt = ecb_aes_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) .decrypt = ecb_aes_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static int cbc_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) unsigned long fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /* Pick the correct function code based on the key length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) fc = (key_len == 16) ? CPACF_KMC_AES_128 :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) (key_len == 24) ? CPACF_KMC_AES_192 :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) (key_len == 32) ? CPACF_KMC_AES_256 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) /* Check if the function code is available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) sctx->fc = (fc && cpacf_test_func(&kmc_functions, fc)) ? fc : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (!sctx->fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return setkey_fallback_skcipher(tfm, in_key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) sctx->key_len = key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) memcpy(sctx->key, in_key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) static int cbc_aes_crypt(struct skcipher_request *req, unsigned long modifier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) unsigned int nbytes, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) u8 iv[AES_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) u8 key[AES_MAX_KEY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) } param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (unlikely(!sctx->fc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return fallback_skcipher_crypt(sctx, req, modifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) ret = skcipher_walk_virt(&walk, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) memcpy(param.iv, walk.iv, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) memcpy(param.key, sctx->key, sctx->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) while ((nbytes = walk.nbytes) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) /* only use complete blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) n = nbytes & ~(AES_BLOCK_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) cpacf_kmc(sctx->fc | modifier, ¶m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) walk.dst.virt.addr, walk.src.virt.addr, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) memcpy(walk.iv, param.iv, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) ret = skcipher_walk_done(&walk, nbytes - n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) memzero_explicit(¶m, sizeof(param));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static int cbc_aes_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return cbc_aes_crypt(req, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static int cbc_aes_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return cbc_aes_crypt(req, CPACF_DECRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static struct skcipher_alg cbc_aes_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) .base.cra_name = "cbc(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) .base.cra_driver_name = "cbc-aes-s390",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) .base.cra_priority = 402, /* ecb-aes-s390 + 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) .base.cra_blocksize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) .base.cra_ctxsize = sizeof(struct s390_aes_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) .init = fallback_init_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) .exit = fallback_exit_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) .min_keysize = AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) .max_keysize = AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) .ivsize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) .setkey = cbc_aes_set_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) .encrypt = cbc_aes_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) .decrypt = cbc_aes_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) static int xts_fallback_setkey(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) crypto_skcipher_clear_flags(xts_ctx->fallback, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) crypto_skcipher_set_flags(xts_ctx->fallback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) crypto_skcipher_get_flags(tfm) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) return crypto_skcipher_setkey(xts_ctx->fallback, key, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) static int xts_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) unsigned long fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) err = xts_fallback_setkey(tfm, in_key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) /* In fips mode only 128 bit or 256 bit keys are valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (fips_enabled && key_len != 32 && key_len != 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) /* Pick the correct function code based on the key length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) fc = (key_len == 32) ? CPACF_KM_XTS_128 :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) (key_len == 64) ? CPACF_KM_XTS_256 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) /* Check if the function code is available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) xts_ctx->fc = (fc && cpacf_test_func(&km_functions, fc)) ? fc : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (!xts_ctx->fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) /* Split the XTS key into the two subkeys */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) key_len = key_len / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) xts_ctx->key_len = key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) memcpy(xts_ctx->key, in_key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) memcpy(xts_ctx->pcc_key, in_key + key_len, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static int xts_aes_crypt(struct skcipher_request *req, unsigned long modifier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) unsigned int offset, nbytes, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) u8 key[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) u8 tweak[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) u8 block[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) u8 bit[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) u8 xts[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) } pcc_param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) u8 key[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) u8 init[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) } xts_param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (req->cryptlen < AES_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (unlikely(!xts_ctx->fc || (req->cryptlen % AES_BLOCK_SIZE) != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) struct skcipher_request *subreq = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) *subreq = *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) skcipher_request_set_tfm(subreq, xts_ctx->fallback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return (modifier & CPACF_DECRYPT) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) crypto_skcipher_decrypt(subreq) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) crypto_skcipher_encrypt(subreq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) ret = skcipher_walk_virt(&walk, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) offset = xts_ctx->key_len & 0x10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) memset(pcc_param.block, 0, sizeof(pcc_param.block));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) memset(pcc_param.bit, 0, sizeof(pcc_param.bit));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) memset(pcc_param.xts, 0, sizeof(pcc_param.xts));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) memcpy(pcc_param.tweak, walk.iv, sizeof(pcc_param.tweak));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) memcpy(pcc_param.key + offset, xts_ctx->pcc_key, xts_ctx->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) cpacf_pcc(xts_ctx->fc, pcc_param.key + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) memcpy(xts_param.key + offset, xts_ctx->key, xts_ctx->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) memcpy(xts_param.init, pcc_param.xts, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) while ((nbytes = walk.nbytes) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) /* only use complete blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) n = nbytes & ~(AES_BLOCK_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) cpacf_km(xts_ctx->fc | modifier, xts_param.key + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) walk.dst.virt.addr, walk.src.virt.addr, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) ret = skcipher_walk_done(&walk, nbytes - n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) memzero_explicit(&pcc_param, sizeof(pcc_param));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) memzero_explicit(&xts_param, sizeof(xts_param));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) static int xts_aes_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) return xts_aes_crypt(req, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) static int xts_aes_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) return xts_aes_crypt(req, CPACF_DECRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) static int xts_fallback_init(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) const char *name = crypto_tfm_alg_name(&tfm->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) xts_ctx->fallback = crypto_alloc_skcipher(name, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) CRYPTO_ALG_NEED_FALLBACK | CRYPTO_ALG_ASYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (IS_ERR(xts_ctx->fallback)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) pr_err("Allocating XTS fallback algorithm %s failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) return PTR_ERR(xts_ctx->fallback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) crypto_skcipher_reqsize(xts_ctx->fallback));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) static void xts_fallback_exit(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) struct s390_xts_ctx *xts_ctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) crypto_free_skcipher(xts_ctx->fallback);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) static struct skcipher_alg xts_aes_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) .base.cra_name = "xts(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) .base.cra_driver_name = "xts-aes-s390",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) .base.cra_priority = 402, /* ecb-aes-s390 + 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) .base.cra_blocksize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) .base.cra_ctxsize = sizeof(struct s390_xts_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) .init = xts_fallback_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) .exit = xts_fallback_exit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) .min_keysize = 2 * AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) .max_keysize = 2 * AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) .ivsize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) .setkey = xts_aes_set_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) .encrypt = xts_aes_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) .decrypt = xts_aes_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) static int ctr_aes_set_key(struct crypto_skcipher *tfm, const u8 *in_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) unsigned int key_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) unsigned long fc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) /* Pick the correct function code based on the key length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) fc = (key_len == 16) ? CPACF_KMCTR_AES_128 :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) (key_len == 24) ? CPACF_KMCTR_AES_192 :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) (key_len == 32) ? CPACF_KMCTR_AES_256 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) /* Check if the function code is available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) sctx->fc = (fc && cpacf_test_func(&kmctr_functions, fc)) ? fc : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (!sctx->fc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) return setkey_fallback_skcipher(tfm, in_key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) sctx->key_len = key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) memcpy(sctx->key, in_key, key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) static unsigned int __ctrblk_init(u8 *ctrptr, u8 *iv, unsigned int nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) unsigned int i, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) /* only use complete blocks, max. PAGE_SIZE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) memcpy(ctrptr, iv, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) n = (nbytes > PAGE_SIZE) ? PAGE_SIZE : nbytes & ~(AES_BLOCK_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) for (i = (n / AES_BLOCK_SIZE) - 1; i > 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) memcpy(ctrptr + AES_BLOCK_SIZE, ctrptr, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) crypto_inc(ctrptr + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) ctrptr += AES_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) static int ctr_aes_crypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) struct s390_aes_ctx *sctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) u8 buf[AES_BLOCK_SIZE], *ctrptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) struct skcipher_walk walk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) unsigned int n, nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) int ret, locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (unlikely(!sctx->fc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) return fallback_skcipher_crypt(sctx, req, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) locked = mutex_trylock(&ctrblk_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) ret = skcipher_walk_virt(&walk, req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) n = AES_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (nbytes >= 2*AES_BLOCK_SIZE && locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) n = __ctrblk_init(ctrblk, walk.iv, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) ctrptr = (n > AES_BLOCK_SIZE) ? ctrblk : walk.iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) cpacf_kmctr(sctx->fc, sctx->key, walk.dst.virt.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) walk.src.virt.addr, n, ctrptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) if (ctrptr == ctrblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) memcpy(walk.iv, ctrptr + n - AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) crypto_inc(walk.iv, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) ret = skcipher_walk_done(&walk, nbytes - n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) if (locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) mutex_unlock(&ctrblk_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) * final block may be < AES_BLOCK_SIZE, copy only nbytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) if (nbytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) cpacf_kmctr(sctx->fc, sctx->key, buf, walk.src.virt.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) AES_BLOCK_SIZE, walk.iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) memcpy(walk.dst.virt.addr, buf, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) crypto_inc(walk.iv, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) ret = skcipher_walk_done(&walk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) static struct skcipher_alg ctr_aes_alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) .base.cra_name = "ctr(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) .base.cra_driver_name = "ctr-aes-s390",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) .base.cra_priority = 402, /* ecb-aes-s390 + 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) .base.cra_blocksize = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) .base.cra_ctxsize = sizeof(struct s390_aes_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) .base.cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) .init = fallback_init_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) .exit = fallback_exit_skcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) .min_keysize = AES_MIN_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) .max_keysize = AES_MAX_KEY_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) .ivsize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) .setkey = ctr_aes_set_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) .encrypt = ctr_aes_crypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) .decrypt = ctr_aes_crypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) .chunksize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) static int gcm_aes_setkey(struct crypto_aead *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) struct s390_aes_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) switch (keylen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) case AES_KEYSIZE_128:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) ctx->fc = CPACF_KMA_GCM_AES_128;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) case AES_KEYSIZE_192:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) ctx->fc = CPACF_KMA_GCM_AES_192;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) case AES_KEYSIZE_256:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) ctx->fc = CPACF_KMA_GCM_AES_256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) memcpy(ctx->key, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) ctx->key_len = keylen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) static int gcm_aes_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) switch (authsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) case 12:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) case 13:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) case 14:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) case 15:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) case 16:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) static void gcm_walk_start(struct gcm_sg_walk *gw, struct scatterlist *sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) memset(gw, 0, sizeof(*gw));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) gw->walk_bytes_remain = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) scatterwalk_start(&gw->walk, sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) static inline unsigned int _gcm_sg_clamp_and_map(struct gcm_sg_walk *gw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) struct scatterlist *nextsg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) gw->walk_bytes = scatterwalk_clamp(&gw->walk, gw->walk_bytes_remain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) while (!gw->walk_bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) nextsg = sg_next(gw->walk.sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) if (!nextsg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) scatterwalk_start(&gw->walk, nextsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) gw->walk_bytes = scatterwalk_clamp(&gw->walk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) gw->walk_bytes_remain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) gw->walk_ptr = scatterwalk_map(&gw->walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) return gw->walk_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) static inline void _gcm_sg_unmap_and_advance(struct gcm_sg_walk *gw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) unsigned int nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) gw->walk_bytes_remain -= nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) scatterwalk_unmap(&gw->walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) scatterwalk_advance(&gw->walk, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) scatterwalk_done(&gw->walk, 0, gw->walk_bytes_remain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) gw->walk_ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) static int gcm_in_walk_go(struct gcm_sg_walk *gw, unsigned int minbytesneeded)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) if (gw->buf_bytes && gw->buf_bytes >= minbytesneeded) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) gw->ptr = gw->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) gw->nbytes = gw->buf_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) if (gw->walk_bytes_remain == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) gw->ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) gw->nbytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) if (!_gcm_sg_clamp_and_map(gw)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) gw->ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) gw->nbytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (!gw->buf_bytes && gw->walk_bytes >= minbytesneeded) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) gw->ptr = gw->walk_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) gw->nbytes = gw->walk_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) n = min(gw->walk_bytes, AES_BLOCK_SIZE - gw->buf_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) memcpy(gw->buf + gw->buf_bytes, gw->walk_ptr, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) gw->buf_bytes += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) _gcm_sg_unmap_and_advance(gw, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) if (gw->buf_bytes >= minbytesneeded) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) gw->ptr = gw->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) gw->nbytes = gw->buf_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) if (!_gcm_sg_clamp_and_map(gw)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) gw->ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) gw->nbytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) return gw->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) static int gcm_out_walk_go(struct gcm_sg_walk *gw, unsigned int minbytesneeded)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) if (gw->walk_bytes_remain == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) gw->ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) gw->nbytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) if (!_gcm_sg_clamp_and_map(gw)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) gw->ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) gw->nbytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) if (gw->walk_bytes >= minbytesneeded) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) gw->ptr = gw->walk_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) gw->nbytes = gw->walk_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) scatterwalk_unmap(&gw->walk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) gw->walk_ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) gw->ptr = gw->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) gw->nbytes = sizeof(gw->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) return gw->nbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) static int gcm_in_walk_done(struct gcm_sg_walk *gw, unsigned int bytesdone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) if (gw->ptr == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) if (gw->ptr == gw->buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) int n = gw->buf_bytes - bytesdone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) if (n > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) memmove(gw->buf, gw->buf + bytesdone, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) gw->buf_bytes = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) gw->buf_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) _gcm_sg_unmap_and_advance(gw, bytesdone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) return bytesdone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) static int gcm_out_walk_done(struct gcm_sg_walk *gw, unsigned int bytesdone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) int i, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) if (gw->ptr == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) if (gw->ptr == gw->buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) for (i = 0; i < bytesdone; i += n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) if (!_gcm_sg_clamp_and_map(gw))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) n = min(gw->walk_bytes, bytesdone - i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) memcpy(gw->walk_ptr, gw->buf + i, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) _gcm_sg_unmap_and_advance(gw, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) _gcm_sg_unmap_and_advance(gw, bytesdone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) return bytesdone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) static int gcm_aes_crypt(struct aead_request *req, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) struct crypto_aead *tfm = crypto_aead_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) struct s390_aes_ctx *ctx = crypto_aead_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) unsigned int ivsize = crypto_aead_ivsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) unsigned int taglen = crypto_aead_authsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) unsigned int aadlen = req->assoclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) unsigned int pclen = req->cryptlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) unsigned int n, len, in_bytes, out_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) min_bytes, bytes, aad_bytes, pc_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) struct gcm_sg_walk gw_in, gw_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) u8 tag[GHASH_DIGEST_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) u32 _[3]; /* reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) u32 cv; /* Counter Value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) u8 t[GHASH_DIGEST_SIZE];/* Tag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) u8 h[AES_BLOCK_SIZE]; /* Hash-subkey */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) u64 taadl; /* Total AAD Length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) u64 tpcl; /* Total Plain-/Cipher-text Length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) u8 j0[GHASH_BLOCK_SIZE];/* initial counter value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) u8 k[AES_MAX_KEY_SIZE]; /* Key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) } param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) * encrypt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) * req->src: aad||plaintext
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) * req->dst: aad||ciphertext||tag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) * decrypt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) * req->src: aad||ciphertext||tag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) * req->dst: aad||plaintext, return 0 or -EBADMSG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) * aad, plaintext and ciphertext may be empty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) if (flags & CPACF_DECRYPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) pclen -= taglen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) len = aadlen + pclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) memset(¶m, 0, sizeof(param));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) param.cv = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) param.taadl = aadlen * 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) param.tpcl = pclen * 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) memcpy(param.j0, req->iv, ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) *(u32 *)(param.j0 + ivsize) = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) memcpy(param.k, ctx->key, ctx->key_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) gcm_walk_start(&gw_in, req->src, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) gcm_walk_start(&gw_out, req->dst, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) min_bytes = min_t(unsigned int,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) aadlen > 0 ? aadlen : pclen, AES_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) in_bytes = gcm_in_walk_go(&gw_in, min_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) out_bytes = gcm_out_walk_go(&gw_out, min_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) bytes = min(in_bytes, out_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) if (aadlen + pclen <= bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) aad_bytes = aadlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) pc_bytes = pclen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) flags |= CPACF_KMA_LAAD | CPACF_KMA_LPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) if (aadlen <= bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) aad_bytes = aadlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) pc_bytes = (bytes - aadlen) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) ~(AES_BLOCK_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) flags |= CPACF_KMA_LAAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) aad_bytes = bytes & ~(AES_BLOCK_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) pc_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) if (aad_bytes > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) memcpy(gw_out.ptr, gw_in.ptr, aad_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) cpacf_kma(ctx->fc | flags, ¶m,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) gw_out.ptr + aad_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) gw_in.ptr + aad_bytes, pc_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) gw_in.ptr, aad_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) n = aad_bytes + pc_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) if (gcm_in_walk_done(&gw_in, n) != n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) if (gcm_out_walk_done(&gw_out, n) != n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) aadlen -= aad_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) pclen -= pc_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) } while (aadlen + pclen > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) if (flags & CPACF_DECRYPT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) scatterwalk_map_and_copy(tag, req->src, len, taglen, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) if (crypto_memneq(tag, param.t, taglen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) ret = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) scatterwalk_map_and_copy(param.t, req->dst, len, taglen, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) memzero_explicit(¶m, sizeof(param));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) static int gcm_aes_encrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) return gcm_aes_crypt(req, CPACF_ENCRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) static int gcm_aes_decrypt(struct aead_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) return gcm_aes_crypt(req, CPACF_DECRYPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) static struct aead_alg gcm_aes_aead = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) .setkey = gcm_aes_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) .setauthsize = gcm_aes_setauthsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) .encrypt = gcm_aes_encrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) .decrypt = gcm_aes_decrypt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) .ivsize = GHASH_BLOCK_SIZE - sizeof(u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) .maxauthsize = GHASH_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) .chunksize = AES_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) .cra_blocksize = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) .cra_ctxsize = sizeof(struct s390_aes_ctx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) .cra_priority = 900,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) .cra_name = "gcm(aes)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) .cra_driver_name = "gcm-aes-s390",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) static struct crypto_alg *aes_s390_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) static struct skcipher_alg *aes_s390_skcipher_algs[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) static int aes_s390_skciphers_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) static struct aead_alg *aes_s390_aead_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) static int aes_s390_register_skcipher(struct skcipher_alg *alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) ret = crypto_register_skcipher(alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) aes_s390_skcipher_algs[aes_s390_skciphers_num++] = alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) static void aes_s390_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) if (aes_s390_alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) crypto_unregister_alg(aes_s390_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) while (aes_s390_skciphers_num--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) crypto_unregister_skcipher(aes_s390_skcipher_algs[aes_s390_skciphers_num]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) if (ctrblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) free_page((unsigned long) ctrblk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) if (aes_s390_aead_alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) crypto_unregister_aead(aes_s390_aead_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) static int __init aes_s390_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) /* Query available functions for KM, KMC, KMCTR and KMA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) cpacf_query(CPACF_KM, &km_functions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) cpacf_query(CPACF_KMC, &kmc_functions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) cpacf_query(CPACF_KMCTR, &kmctr_functions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) cpacf_query(CPACF_KMA, &kma_functions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) if (cpacf_test_func(&km_functions, CPACF_KM_AES_128) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) cpacf_test_func(&km_functions, CPACF_KM_AES_192) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) cpacf_test_func(&km_functions, CPACF_KM_AES_256)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) ret = crypto_register_alg(&aes_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) aes_s390_alg = &aes_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) ret = aes_s390_register_skcipher(&ecb_aes_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) if (cpacf_test_func(&kmc_functions, CPACF_KMC_AES_128) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) cpacf_test_func(&kmc_functions, CPACF_KMC_AES_192) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) cpacf_test_func(&kmc_functions, CPACF_KMC_AES_256)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) ret = aes_s390_register_skcipher(&cbc_aes_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) if (cpacf_test_func(&km_functions, CPACF_KM_XTS_128) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) cpacf_test_func(&km_functions, CPACF_KM_XTS_256)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) ret = aes_s390_register_skcipher(&xts_aes_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) if (cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_128) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_192) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) cpacf_test_func(&kmctr_functions, CPACF_KMCTR_AES_256)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) ctrblk = (u8 *) __get_free_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) if (!ctrblk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) ret = aes_s390_register_skcipher(&ctr_aes_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) if (cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_128) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_192) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) cpacf_test_func(&kma_functions, CPACF_KMA_GCM_AES_256)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) ret = crypto_register_aead(&gcm_aes_aead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) aes_s390_aead_alg = &gcm_aes_aead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) aes_s390_fini();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) module_cpu_feature_match(MSA, aes_s390_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) module_exit(aes_s390_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) MODULE_ALIAS_CRYPTO("aes-all");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) MODULE_IMPORT_NS(CRYPTO_INTERNAL);