18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * AMD Cryptographic Coprocessor (CCP) crypto API support
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Tom Lendacky <thomas.lendacky@amd.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef __CCP_CRYPTO_H__
118c2ecf20Sopenharmony_ci#define __CCP_CRYPTO_H__
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/list.h>
148c2ecf20Sopenharmony_ci#include <linux/wait.h>
158c2ecf20Sopenharmony_ci#include <linux/ccp.h>
168c2ecf20Sopenharmony_ci#include <crypto/algapi.h>
178c2ecf20Sopenharmony_ci#include <crypto/aes.h>
188c2ecf20Sopenharmony_ci#include <crypto/internal/aead.h>
198c2ecf20Sopenharmony_ci#include <crypto/aead.h>
208c2ecf20Sopenharmony_ci#include <crypto/ctr.h>
218c2ecf20Sopenharmony_ci#include <crypto/hash.h>
228c2ecf20Sopenharmony_ci#include <crypto/sha.h>
238c2ecf20Sopenharmony_ci#include <crypto/akcipher.h>
248c2ecf20Sopenharmony_ci#include <crypto/skcipher.h>
258c2ecf20Sopenharmony_ci#include <crypto/internal/rsa.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* We want the module name in front of our messages */
288c2ecf20Sopenharmony_ci#undef pr_fmt
298c2ecf20Sopenharmony_ci#define	pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define	CCP_LOG_LEVEL	KERN_INFO
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define CCP_CRA_PRIORITY	300
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistruct ccp_crypto_skcipher_alg {
368c2ecf20Sopenharmony_ci	struct list_head entry;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	u32 mode;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	struct skcipher_alg alg;
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistruct ccp_crypto_aead {
448c2ecf20Sopenharmony_ci	struct list_head entry;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	u32 mode;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	struct aead_alg alg;
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistruct ccp_crypto_ahash_alg {
528c2ecf20Sopenharmony_ci	struct list_head entry;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	const __be32 *init;
558c2ecf20Sopenharmony_ci	u32 type;
568c2ecf20Sopenharmony_ci	u32 mode;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	/* Child algorithm used for HMAC, CMAC, etc */
598c2ecf20Sopenharmony_ci	char child_alg[CRYPTO_MAX_ALG_NAME];
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	struct ahash_alg alg;
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistruct ccp_crypto_akcipher_alg {
658c2ecf20Sopenharmony_ci	struct list_head entry;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	struct akcipher_alg alg;
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic inline struct ccp_crypto_skcipher_alg *
718c2ecf20Sopenharmony_ci	ccp_crypto_skcipher_alg(struct crypto_skcipher *tfm)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	return container_of(alg, struct ccp_crypto_skcipher_alg, alg);
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic inline struct ccp_crypto_ahash_alg *
798c2ecf20Sopenharmony_ci	ccp_crypto_ahash_alg(struct crypto_tfm *tfm)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct crypto_alg *alg = tfm->__crt_alg;
828c2ecf20Sopenharmony_ci	struct ahash_alg *ahash_alg;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	ahash_alg = container_of(alg, struct ahash_alg, halg.base);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg);
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/***** AES related defines *****/
908c2ecf20Sopenharmony_cistruct ccp_aes_ctx {
918c2ecf20Sopenharmony_ci	/* Fallback cipher for XTS with unsupported unit sizes */
928c2ecf20Sopenharmony_ci	struct crypto_skcipher *tfm_skcipher;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	enum ccp_engine engine;
958c2ecf20Sopenharmony_ci	enum ccp_aes_type type;
968c2ecf20Sopenharmony_ci	enum ccp_aes_mode mode;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	struct scatterlist key_sg;
998c2ecf20Sopenharmony_ci	unsigned int key_len;
1008c2ecf20Sopenharmony_ci	u8 key[AES_MAX_KEY_SIZE * 2];
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	u8 nonce[CTR_RFC3686_NONCE_SIZE];
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	/* CMAC key structures */
1058c2ecf20Sopenharmony_ci	struct scatterlist k1_sg;
1068c2ecf20Sopenharmony_ci	struct scatterlist k2_sg;
1078c2ecf20Sopenharmony_ci	unsigned int kn_len;
1088c2ecf20Sopenharmony_ci	u8 k1[AES_BLOCK_SIZE];
1098c2ecf20Sopenharmony_ci	u8 k2[AES_BLOCK_SIZE];
1108c2ecf20Sopenharmony_ci};
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistruct ccp_aes_req_ctx {
1138c2ecf20Sopenharmony_ci	struct scatterlist iv_sg;
1148c2ecf20Sopenharmony_ci	u8 iv[AES_BLOCK_SIZE];
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	struct scatterlist tag_sg;
1178c2ecf20Sopenharmony_ci	u8 tag[AES_BLOCK_SIZE];
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	/* Fields used for RFC3686 requests */
1208c2ecf20Sopenharmony_ci	u8 *rfc3686_info;
1218c2ecf20Sopenharmony_ci	u8 rfc3686_iv[AES_BLOCK_SIZE];
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	struct ccp_cmd cmd;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	struct skcipher_request fallback_req;	// keep at the end
1268c2ecf20Sopenharmony_ci};
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistruct ccp_aes_cmac_req_ctx {
1298c2ecf20Sopenharmony_ci	unsigned int null_msg;
1308c2ecf20Sopenharmony_ci	unsigned int final;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	struct scatterlist *src;
1338c2ecf20Sopenharmony_ci	unsigned int nbytes;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	u64 hash_cnt;
1368c2ecf20Sopenharmony_ci	unsigned int hash_rem;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	struct sg_table data_sg;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	struct scatterlist iv_sg;
1418c2ecf20Sopenharmony_ci	u8 iv[AES_BLOCK_SIZE];
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	struct scatterlist buf_sg;
1448c2ecf20Sopenharmony_ci	unsigned int buf_count;
1458c2ecf20Sopenharmony_ci	u8 buf[AES_BLOCK_SIZE];
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	struct scatterlist pad_sg;
1488c2ecf20Sopenharmony_ci	unsigned int pad_count;
1498c2ecf20Sopenharmony_ci	u8 pad[AES_BLOCK_SIZE];
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	struct ccp_cmd cmd;
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistruct ccp_aes_cmac_exp_ctx {
1558c2ecf20Sopenharmony_ci	unsigned int null_msg;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	u8 iv[AES_BLOCK_SIZE];
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	unsigned int buf_count;
1608c2ecf20Sopenharmony_ci	u8 buf[AES_BLOCK_SIZE];
1618c2ecf20Sopenharmony_ci};
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/***** 3DES related defines *****/
1648c2ecf20Sopenharmony_cistruct ccp_des3_ctx {
1658c2ecf20Sopenharmony_ci	enum ccp_engine engine;
1668c2ecf20Sopenharmony_ci	enum ccp_des3_type type;
1678c2ecf20Sopenharmony_ci	enum ccp_des3_mode mode;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	struct scatterlist key_sg;
1708c2ecf20Sopenharmony_ci	unsigned int key_len;
1718c2ecf20Sopenharmony_ci	u8 key[AES_MAX_KEY_SIZE];
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistruct ccp_des3_req_ctx {
1758c2ecf20Sopenharmony_ci	struct scatterlist iv_sg;
1768c2ecf20Sopenharmony_ci	u8 iv[AES_BLOCK_SIZE];
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	struct ccp_cmd cmd;
1798c2ecf20Sopenharmony_ci};
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci/* SHA-related defines
1828c2ecf20Sopenharmony_ci * These values must be large enough to accommodate any variant
1838c2ecf20Sopenharmony_ci */
1848c2ecf20Sopenharmony_ci#define MAX_SHA_CONTEXT_SIZE	SHA512_DIGEST_SIZE
1858c2ecf20Sopenharmony_ci#define MAX_SHA_BLOCK_SIZE	SHA512_BLOCK_SIZE
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistruct ccp_sha_ctx {
1888c2ecf20Sopenharmony_ci	struct scatterlist opad_sg;
1898c2ecf20Sopenharmony_ci	unsigned int opad_count;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	unsigned int key_len;
1928c2ecf20Sopenharmony_ci	u8 key[MAX_SHA_BLOCK_SIZE];
1938c2ecf20Sopenharmony_ci	u8 ipad[MAX_SHA_BLOCK_SIZE];
1948c2ecf20Sopenharmony_ci	u8 opad[MAX_SHA_BLOCK_SIZE];
1958c2ecf20Sopenharmony_ci	struct crypto_shash *hmac_tfm;
1968c2ecf20Sopenharmony_ci};
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistruct ccp_sha_req_ctx {
1998c2ecf20Sopenharmony_ci	enum ccp_sha_type type;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	u64 msg_bits;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	unsigned int first;
2048c2ecf20Sopenharmony_ci	unsigned int final;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	struct scatterlist *src;
2078c2ecf20Sopenharmony_ci	unsigned int nbytes;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	u64 hash_cnt;
2108c2ecf20Sopenharmony_ci	unsigned int hash_rem;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	struct sg_table data_sg;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	struct scatterlist ctx_sg;
2158c2ecf20Sopenharmony_ci	u8 ctx[MAX_SHA_CONTEXT_SIZE];
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	struct scatterlist buf_sg;
2188c2ecf20Sopenharmony_ci	unsigned int buf_count;
2198c2ecf20Sopenharmony_ci	u8 buf[MAX_SHA_BLOCK_SIZE];
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	/* CCP driver command */
2228c2ecf20Sopenharmony_ci	struct ccp_cmd cmd;
2238c2ecf20Sopenharmony_ci};
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistruct ccp_sha_exp_ctx {
2268c2ecf20Sopenharmony_ci	enum ccp_sha_type type;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	u64 msg_bits;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	unsigned int first;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	u8 ctx[MAX_SHA_CONTEXT_SIZE];
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	unsigned int buf_count;
2358c2ecf20Sopenharmony_ci	u8 buf[MAX_SHA_BLOCK_SIZE];
2368c2ecf20Sopenharmony_ci};
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci/***** RSA related defines *****/
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cistruct ccp_rsa_ctx {
2418c2ecf20Sopenharmony_ci	unsigned int key_len; /* in bits */
2428c2ecf20Sopenharmony_ci	struct scatterlist e_sg;
2438c2ecf20Sopenharmony_ci	u8 *e_buf;
2448c2ecf20Sopenharmony_ci	unsigned int e_len;
2458c2ecf20Sopenharmony_ci	struct scatterlist n_sg;
2468c2ecf20Sopenharmony_ci	u8 *n_buf;
2478c2ecf20Sopenharmony_ci	unsigned int n_len;
2488c2ecf20Sopenharmony_ci	struct scatterlist d_sg;
2498c2ecf20Sopenharmony_ci	u8 *d_buf;
2508c2ecf20Sopenharmony_ci	unsigned int d_len;
2518c2ecf20Sopenharmony_ci};
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistruct ccp_rsa_req_ctx {
2548c2ecf20Sopenharmony_ci	struct ccp_cmd cmd;
2558c2ecf20Sopenharmony_ci};
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci#define	CCP_RSA_MAXMOD	(4 * 1024 / 8)
2588c2ecf20Sopenharmony_ci#define	CCP5_RSA_MAXMOD	(16 * 1024 / 8)
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci/***** Common Context Structure *****/
2618c2ecf20Sopenharmony_cistruct ccp_ctx {
2628c2ecf20Sopenharmony_ci	int (*complete)(struct crypto_async_request *req, int ret);
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	union {
2658c2ecf20Sopenharmony_ci		struct ccp_aes_ctx aes;
2668c2ecf20Sopenharmony_ci		struct ccp_rsa_ctx rsa;
2678c2ecf20Sopenharmony_ci		struct ccp_sha_ctx sha;
2688c2ecf20Sopenharmony_ci		struct ccp_des3_ctx des3;
2698c2ecf20Sopenharmony_ci	} u;
2708c2ecf20Sopenharmony_ci};
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ciint ccp_crypto_enqueue_request(struct crypto_async_request *req,
2738c2ecf20Sopenharmony_ci			       struct ccp_cmd *cmd);
2748c2ecf20Sopenharmony_cistruct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table,
2758c2ecf20Sopenharmony_ci					    struct scatterlist *sg_add);
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ciint ccp_register_aes_algs(struct list_head *head);
2788c2ecf20Sopenharmony_ciint ccp_register_aes_cmac_algs(struct list_head *head);
2798c2ecf20Sopenharmony_ciint ccp_register_aes_xts_algs(struct list_head *head);
2808c2ecf20Sopenharmony_ciint ccp_register_aes_aeads(struct list_head *head);
2818c2ecf20Sopenharmony_ciint ccp_register_sha_algs(struct list_head *head);
2828c2ecf20Sopenharmony_ciint ccp_register_des3_algs(struct list_head *head);
2838c2ecf20Sopenharmony_ciint ccp_register_rsa_algs(struct list_head *head);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci#endif
286