// SPDX-License-Identifier: GPL-2.0
/*
* Cryptographic API.
*
* s390 implementation of the AES Cipher Algorithm with protected keys.
*
* s390 Version:
* Copyright IBM Corp. 2017, 2023
* Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
* Harald Freudenberger <freude@de.ibm.com>
*/
#define KMSG_COMPONENT "paes_s390"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
#include <crypto/aes.h>
#include <crypto/algapi.h>
#include <linux/bug.h>
#include <linux/err.h>
#include <linux/module.h>
#include <linux/cpufeature.h>
#include <linux/init.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/delay.h>
#include <crypto/internal/skcipher.h>
#include <crypto/xts.h>
#include <asm/cpacf.h>
#include <asm/pkey.h>
/*
* Key blobs smaller/bigger than these defines are rejected
* by the common code even before the individual setkey function
* is called. As paes can handle different kinds of key blobs
* and padding is also possible, the limits need to be generous.
*/
#define PAES_MIN_KEYSIZE 16
#define PAES_MAX_KEYSIZE MAXEP11AESKEYBLOBSIZE
#define PAES_256_PROTKEY_SIZE (32 + 32) /* key + verification pattern */
#define PXTS_256_PROTKEY_SIZE (32 + 32 + 32) /* k1 + k2 + verification pattern */
static u8 *ctrblk;
static DEFINE_MUTEX(ctrblk_lock);
static cpacf_mask_t km_functions, kmc_functions, kmctr_functions;
struct paes_protkey {
u32 type;
u32 len;
u8 protkey[PXTS_256_PROTKEY_SIZE];
};
struct key_blob {
/*
* Small keys will be stored in the keybuf. Larger keys are
* stored in extra allocated memory. In both cases does
* key point to the memory where the key is stored.
* The code distinguishes by checking keylen against
* sizeof(keybuf). See the two following helper functions.
*/
u8 *key;
u8 keybuf[128];
unsigned int keylen;
};
/*
* make_clrkey_token() - wrap the raw key ck with pkey clearkey token
* information.
* @returns the size of the clearkey token
*/
static inline u32 make_clrkey_token(const u8 *ck, size_t cklen, u8 *dest)
{
struct clrkey_token {
u8 type;
u8 res0[3];
u8 version;
u8 res1[3];
u32 keytype;
u32 len;
u8 key[];
} __packed *token = (struct clrkey_token *)dest;
token->type = 0x00;
token->version = 0x02;
token->keytype = (cklen - 8) >> 3;
token->len = cklen;
memcpy(token->key, ck, cklen);
return sizeof(*token) + cklen;
}
static inline int _key_to_kb(struct key_blob *kb,
const u8 *key,
unsigned int keylen)
{
switch (keylen) {
case 16:
case 24:
case 32:
/* clear key value, prepare pkey clear key token in keybuf */
memset(kb->keybuf, 0, sizeof(kb->keybuf));
kb->keylen = make_clrkey_token(key, keylen, kb->keybuf);
kb->key = kb->keybuf;
break;
default:
/* other key material, let pkey handle this */
if (keylen <= sizeof(kb->keybuf))
kb->key = kb->keybuf;
else {
kb->key = kmalloc(keylen, GFP_KERNEL);
if (!kb->key)
return -ENOMEM;
}
memcpy(kb->key, key, keylen);
kb->keylen = keylen;
break;
}
return 0;
}
static inline int _xts_key_to_kb(struct key_blob *kb,
const u8 *key,
unsigned int keylen)
{
size_t cklen = keylen / 2;
memset(kb->keybuf, 0, sizeof(kb->keybuf));
switch (keylen) {
case 32:
case 64:
/* clear key value, prepare pkey clear key tokens in keybuf */
kb->key = kb->keybuf;
kb->keylen = make_clrkey_token(key, cklen, kb->key);
kb->keylen += make_clrkey_token(key + cklen, cklen,
kb->key + kb->keylen);
break;
default:
/* other key material, let pkey handle this */
if (keylen <= sizeof(kb->keybuf)) {
kb->key = kb->keybuf;
} else {
kb->key = kmalloc(keylen, GFP_KERNEL);
if (!kb->key)
return -ENOMEM;
}
memcpy(kb->key, key, keylen);
kb->keylen = keylen;
break;
}
return 0;
}
static inline void _free_kb_keybuf(struct key_blob *kb)
{
if (kb->key && kb->key != kb->keybuf
&& kb->keylen > sizeof(kb->keybuf)) {
kfree_sensitive(kb->key);
kb->key = NULL;
}
memzero_explicit(kb->keybuf, sizeof(kb->keybuf));
}
struct s390_paes_ctx {
struct key_blob kb;
struct paes_protkey pk;
spinlock_t pk_lock;
unsigned long fc;
};
struct s390_pxts_ctx {
struct key_blob kb;
struct paes_protkey pk[2];
spinlock_t pk_lock;
unsigned long fc;
};
static inline int __paes_keyblob2pkey(const u8 *key, unsigned int keylen,
struct paes_protkey *pk)
{
int i, rc = -EIO;
/* try three times in case of busy card */
for (i = 0; rc && i < 3; i++) {
if (rc == -EBUSY && in_task()) {
if (msleep_interruptible(1000))
return -EINTR;
}
rc = pkey_key2protkey(key, keylen, pk->protkey, &pk->len,
&pk->type);
}
return rc;
}
static inline int __paes_convert_key(struct s390_paes_ctx *ctx)
{
struct paes_protkey pk;
int rc;
pk.len = sizeof(pk.protkey);
rc = __paes_keyblob2pkey(ctx->kb.key, ctx->kb.keylen, &pk);
if (rc)
return rc;
spin_lock_bh(