xref: /openbmc/linux/drivers/crypto/ccp/ccp-crypto.h (revision be9fe620af63e76781d869f61d7e306bbe482415)
1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2d3123599STom Lendacky /*
3d3123599STom Lendacky  * AMD Cryptographic Coprocessor (CCP) crypto API support
4d3123599STom Lendacky  *
568cc652fSGary R Hook  * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
6d3123599STom Lendacky  *
7d3123599STom Lendacky  * Author: Tom Lendacky <thomas.lendacky@amd.com>
8d3123599STom Lendacky  */
9d3123599STom Lendacky 
10d3123599STom Lendacky #ifndef __CCP_CRYPTO_H__
11d3123599STom Lendacky #define __CCP_CRYPTO_H__
12d3123599STom Lendacky 
13d3123599STom Lendacky #include <linux/list.h>
14d3123599STom Lendacky #include <linux/wait.h>
15d3123599STom Lendacky #include <linux/ccp.h>
16d3123599STom Lendacky #include <crypto/algapi.h>
17d3123599STom Lendacky #include <crypto/aes.h>
1836cf515bSGary R Hook #include <crypto/internal/aead.h>
1936cf515bSGary R Hook #include <crypto/aead.h>
20d3123599STom Lendacky #include <crypto/ctr.h>
21d3123599STom Lendacky #include <crypto/hash.h>
22d3123599STom Lendacky #include <crypto/sha.h>
23ceeec0afSGary R Hook #include <crypto/akcipher.h>
24*be9fe620SArd Biesheuvel #include <crypto/skcipher.h>
25ceeec0afSGary R Hook #include <crypto/internal/rsa.h>
26d3123599STom Lendacky 
272a03e3a5SHook, Gary /* We want the module name in front of our messages */
282a03e3a5SHook, Gary #undef pr_fmt
292a03e3a5SHook, Gary #define	pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
302a03e3a5SHook, Gary 
31990672d4SGary R Hook #define	CCP_LOG_LEVEL	KERN_INFO
32990672d4SGary R Hook 
33d3123599STom Lendacky #define CCP_CRA_PRIORITY	300
34d3123599STom Lendacky 
35*be9fe620SArd Biesheuvel struct ccp_crypto_skcipher_alg {
36d3123599STom Lendacky 	struct list_head entry;
37d3123599STom Lendacky 
38d3123599STom Lendacky 	u32 mode;
39d3123599STom Lendacky 
40*be9fe620SArd Biesheuvel 	struct skcipher_alg alg;
41d3123599STom Lendacky };
42d3123599STom Lendacky 
4336cf515bSGary R Hook struct ccp_crypto_aead {
4436cf515bSGary R Hook 	struct list_head entry;
4536cf515bSGary R Hook 
4636cf515bSGary R Hook 	u32 mode;
4736cf515bSGary R Hook 
4836cf515bSGary R Hook 	struct aead_alg alg;
4936cf515bSGary R Hook };
5036cf515bSGary R Hook 
51d3123599STom Lendacky struct ccp_crypto_ahash_alg {
52d3123599STom Lendacky 	struct list_head entry;
53d3123599STom Lendacky 
546f0be9b2STom Lendacky 	const __be32 *init;
55d3123599STom Lendacky 	u32 type;
56d3123599STom Lendacky 	u32 mode;
57d3123599STom Lendacky 
58d3123599STom Lendacky 	/* Child algorithm used for HMAC, CMAC, etc */
59d3123599STom Lendacky 	char child_alg[CRYPTO_MAX_ALG_NAME];
60d3123599STom Lendacky 
61d3123599STom Lendacky 	struct ahash_alg alg;
62d3123599STom Lendacky };
63d3123599STom Lendacky 
64ceeec0afSGary R Hook struct ccp_crypto_akcipher_alg {
65ceeec0afSGary R Hook 	struct list_head entry;
66ceeec0afSGary R Hook 
67ceeec0afSGary R Hook 	struct akcipher_alg alg;
68ceeec0afSGary R Hook };
69ceeec0afSGary R Hook 
70*be9fe620SArd Biesheuvel static inline struct ccp_crypto_skcipher_alg *
71*be9fe620SArd Biesheuvel 	ccp_crypto_skcipher_alg(struct crypto_skcipher *tfm)
72d3123599STom Lendacky {
73*be9fe620SArd Biesheuvel 	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
74d3123599STom Lendacky 
75*be9fe620SArd Biesheuvel 	return container_of(alg, struct ccp_crypto_skcipher_alg, alg);
76d3123599STom Lendacky }
77d3123599STom Lendacky 
78d3123599STom Lendacky static inline struct ccp_crypto_ahash_alg *
79d3123599STom Lendacky 	ccp_crypto_ahash_alg(struct crypto_tfm *tfm)
80d3123599STom Lendacky {
81d3123599STom Lendacky 	struct crypto_alg *alg = tfm->__crt_alg;
82d3123599STom Lendacky 	struct ahash_alg *ahash_alg;
83d3123599STom Lendacky 
84d3123599STom Lendacky 	ahash_alg = container_of(alg, struct ahash_alg, halg.base);
85d3123599STom Lendacky 
86d3123599STom Lendacky 	return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg);
87d3123599STom Lendacky }
88d3123599STom Lendacky 
89d3123599STom Lendacky /***** AES related defines *****/
90d3123599STom Lendacky struct ccp_aes_ctx {
91d3123599STom Lendacky 	/* Fallback cipher for XTS with unsupported unit sizes */
927f28615dSKees Cook 	struct crypto_sync_skcipher *tfm_skcipher;
93d3123599STom Lendacky 
94d3123599STom Lendacky 	enum ccp_engine engine;
95d3123599STom Lendacky 	enum ccp_aes_type type;
96d3123599STom Lendacky 	enum ccp_aes_mode mode;
97d3123599STom Lendacky 
98d3123599STom Lendacky 	struct scatterlist key_sg;
99d3123599STom Lendacky 	unsigned int key_len;
1005060ffc9SGary R Hook 	u8 key[AES_MAX_KEY_SIZE * 2];
101d3123599STom Lendacky 
102d3123599STom Lendacky 	u8 nonce[CTR_RFC3686_NONCE_SIZE];
103d3123599STom Lendacky 
104d3123599STom Lendacky 	/* CMAC key structures */
105d3123599STom Lendacky 	struct scatterlist k1_sg;
106d3123599STom Lendacky 	struct scatterlist k2_sg;
107d3123599STom Lendacky 	unsigned int kn_len;
108d3123599STom Lendacky 	u8 k1[AES_BLOCK_SIZE];
109d3123599STom Lendacky 	u8 k2[AES_BLOCK_SIZE];
110d3123599STom Lendacky };
111d3123599STom Lendacky 
112d3123599STom Lendacky struct ccp_aes_req_ctx {
113d3123599STom Lendacky 	struct scatterlist iv_sg;
114d3123599STom Lendacky 	u8 iv[AES_BLOCK_SIZE];
115d3123599STom Lendacky 
11636cf515bSGary R Hook 	struct scatterlist tag_sg;
11736cf515bSGary R Hook 	u8 tag[AES_BLOCK_SIZE];
11836cf515bSGary R Hook 
119d3123599STom Lendacky 	/* Fields used for RFC3686 requests */
120d3123599STom Lendacky 	u8 *rfc3686_info;
121d3123599STom Lendacky 	u8 rfc3686_iv[AES_BLOCK_SIZE];
122d3123599STom Lendacky 
123d3123599STom Lendacky 	struct ccp_cmd cmd;
124d3123599STom Lendacky };
125d3123599STom Lendacky 
126d3123599STom Lendacky struct ccp_aes_cmac_req_ctx {
127d3123599STom Lendacky 	unsigned int null_msg;
128d3123599STom Lendacky 	unsigned int final;
129d3123599STom Lendacky 
13081a59f00STom Lendacky 	struct scatterlist *src;
13181a59f00STom Lendacky 	unsigned int nbytes;
13281a59f00STom Lendacky 
13381a59f00STom Lendacky 	u64 hash_cnt;
134d3123599STom Lendacky 	unsigned int hash_rem;
135d3123599STom Lendacky 
136d3123599STom Lendacky 	struct sg_table data_sg;
137d3123599STom Lendacky 
138d3123599STom Lendacky 	struct scatterlist iv_sg;
139d3123599STom Lendacky 	u8 iv[AES_BLOCK_SIZE];
140d3123599STom Lendacky 
141d3123599STom Lendacky 	struct scatterlist buf_sg;
142d3123599STom Lendacky 	unsigned int buf_count;
143d3123599STom Lendacky 	u8 buf[AES_BLOCK_SIZE];
144d3123599STom Lendacky 
145d3123599STom Lendacky 	struct scatterlist pad_sg;
146d3123599STom Lendacky 	unsigned int pad_count;
147d3123599STom Lendacky 	u8 pad[AES_BLOCK_SIZE];
148d3123599STom Lendacky 
149d3123599STom Lendacky 	struct ccp_cmd cmd;
150d3123599STom Lendacky };
151d3123599STom Lendacky 
152d1662165STom Lendacky struct ccp_aes_cmac_exp_ctx {
153d1662165STom Lendacky 	unsigned int null_msg;
154d1662165STom Lendacky 
155d1662165STom Lendacky 	u8 iv[AES_BLOCK_SIZE];
156d1662165STom Lendacky 
157d1662165STom Lendacky 	unsigned int buf_count;
158d1662165STom Lendacky 	u8 buf[AES_BLOCK_SIZE];
159d1662165STom Lendacky };
160d1662165STom Lendacky 
161990672d4SGary R Hook /***** 3DES related defines *****/
162990672d4SGary R Hook struct ccp_des3_ctx {
163990672d4SGary R Hook 	enum ccp_engine engine;
164990672d4SGary R Hook 	enum ccp_des3_type type;
165990672d4SGary R Hook 	enum ccp_des3_mode mode;
166990672d4SGary R Hook 
167990672d4SGary R Hook 	struct scatterlist key_sg;
168990672d4SGary R Hook 	unsigned int key_len;
169990672d4SGary R Hook 	u8 key[AES_MAX_KEY_SIZE];
170990672d4SGary R Hook };
171990672d4SGary R Hook 
172990672d4SGary R Hook struct ccp_des3_req_ctx {
173990672d4SGary R Hook 	struct scatterlist iv_sg;
174990672d4SGary R Hook 	u8 iv[AES_BLOCK_SIZE];
175990672d4SGary R Hook 
176990672d4SGary R Hook 	struct ccp_cmd cmd;
177990672d4SGary R Hook };
178990672d4SGary R Hook 
179ccebcf3fSGary R Hook /* SHA-related defines
180ccebcf3fSGary R Hook  * These values must be large enough to accommodate any variant
181ccebcf3fSGary R Hook  */
182ccebcf3fSGary R Hook #define MAX_SHA_CONTEXT_SIZE	SHA512_DIGEST_SIZE
183ccebcf3fSGary R Hook #define MAX_SHA_BLOCK_SIZE	SHA512_BLOCK_SIZE
184d3123599STom Lendacky 
185d3123599STom Lendacky struct ccp_sha_ctx {
186c11baa02STom Lendacky 	struct scatterlist opad_sg;
187c11baa02STom Lendacky 	unsigned int opad_count;
188c11baa02STom Lendacky 
189d3123599STom Lendacky 	unsigned int key_len;
190d3123599STom Lendacky 	u8 key[MAX_SHA_BLOCK_SIZE];
191d3123599STom Lendacky 	u8 ipad[MAX_SHA_BLOCK_SIZE];
192d3123599STom Lendacky 	u8 opad[MAX_SHA_BLOCK_SIZE];
193c11baa02STom Lendacky 	struct crypto_shash *hmac_tfm;
194d3123599STom Lendacky };
195d3123599STom Lendacky 
196d3123599STom Lendacky struct ccp_sha_req_ctx {
197d3123599STom Lendacky 	enum ccp_sha_type type;
198d3123599STom Lendacky 
199d3123599STom Lendacky 	u64 msg_bits;
200d3123599STom Lendacky 
201d3123599STom Lendacky 	unsigned int first;
202d3123599STom Lendacky 	unsigned int final;
203d3123599STom Lendacky 
20481a59f00STom Lendacky 	struct scatterlist *src;
20581a59f00STom Lendacky 	unsigned int nbytes;
20681a59f00STom Lendacky 
20781a59f00STom Lendacky 	u64 hash_cnt;
208d3123599STom Lendacky 	unsigned int hash_rem;
209d3123599STom Lendacky 
210d3123599STom Lendacky 	struct sg_table data_sg;
211d3123599STom Lendacky 
212d3123599STom Lendacky 	struct scatterlist ctx_sg;
213d3123599STom Lendacky 	u8 ctx[MAX_SHA_CONTEXT_SIZE];
214d3123599STom Lendacky 
215d3123599STom Lendacky 	struct scatterlist buf_sg;
216d3123599STom Lendacky 	unsigned int buf_count;
217d3123599STom Lendacky 	u8 buf[MAX_SHA_BLOCK_SIZE];
218d3123599STom Lendacky 
219d3123599STom Lendacky 	/* CCP driver command */
220d3123599STom Lendacky 	struct ccp_cmd cmd;
221d3123599STom Lendacky };
222d3123599STom Lendacky 
223d1662165STom Lendacky struct ccp_sha_exp_ctx {
224d1662165STom Lendacky 	enum ccp_sha_type type;
225d1662165STom Lendacky 
226d1662165STom Lendacky 	u64 msg_bits;
227d1662165STom Lendacky 
228d1662165STom Lendacky 	unsigned int first;
229d1662165STom Lendacky 
230d1662165STom Lendacky 	u8 ctx[MAX_SHA_CONTEXT_SIZE];
231d1662165STom Lendacky 
232d1662165STom Lendacky 	unsigned int buf_count;
233d1662165STom Lendacky 	u8 buf[MAX_SHA_BLOCK_SIZE];
234d1662165STom Lendacky };
235d1662165STom Lendacky 
236ceeec0afSGary R Hook /***** RSA related defines *****/
237ceeec0afSGary R Hook 
238ceeec0afSGary R Hook struct ccp_rsa_ctx {
239ceeec0afSGary R Hook 	unsigned int key_len; /* in bits */
240ceeec0afSGary R Hook 	struct scatterlist e_sg;
241ceeec0afSGary R Hook 	u8 *e_buf;
242ceeec0afSGary R Hook 	unsigned int e_len;
243ceeec0afSGary R Hook 	struct scatterlist n_sg;
244ceeec0afSGary R Hook 	u8 *n_buf;
245ceeec0afSGary R Hook 	unsigned int n_len;
246ceeec0afSGary R Hook 	struct scatterlist d_sg;
247ceeec0afSGary R Hook 	u8 *d_buf;
248ceeec0afSGary R Hook 	unsigned int d_len;
249ceeec0afSGary R Hook };
250ceeec0afSGary R Hook 
251ceeec0afSGary R Hook struct ccp_rsa_req_ctx {
252ceeec0afSGary R Hook 	struct ccp_cmd cmd;
253ceeec0afSGary R Hook };
254ceeec0afSGary R Hook 
255ceeec0afSGary R Hook #define	CCP_RSA_MAXMOD	(4 * 1024 / 8)
256e28c190dSGary R Hook #define	CCP5_RSA_MAXMOD	(16 * 1024 / 8)
257ceeec0afSGary R Hook 
258d3123599STom Lendacky /***** Common Context Structure *****/
259d3123599STom Lendacky struct ccp_ctx {
260d3123599STom Lendacky 	int (*complete)(struct crypto_async_request *req, int ret);
261d3123599STom Lendacky 
262d3123599STom Lendacky 	union {
263d3123599STom Lendacky 		struct ccp_aes_ctx aes;
264ceeec0afSGary R Hook 		struct ccp_rsa_ctx rsa;
265d3123599STom Lendacky 		struct ccp_sha_ctx sha;
266990672d4SGary R Hook 		struct ccp_des3_ctx des3;
267d3123599STom Lendacky 	} u;
268d3123599STom Lendacky };
269d3123599STom Lendacky 
270d3123599STom Lendacky int ccp_crypto_enqueue_request(struct crypto_async_request *req,
271d3123599STom Lendacky 			       struct ccp_cmd *cmd);
272d3123599STom Lendacky struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table,
273d3123599STom Lendacky 					    struct scatterlist *sg_add);
274d3123599STom Lendacky 
275d3123599STom Lendacky int ccp_register_aes_algs(struct list_head *head);
276d3123599STom Lendacky int ccp_register_aes_cmac_algs(struct list_head *head);
277d3123599STom Lendacky int ccp_register_aes_xts_algs(struct list_head *head);
27836cf515bSGary R Hook int ccp_register_aes_aeads(struct list_head *head);
279d3123599STom Lendacky int ccp_register_sha_algs(struct list_head *head);
280990672d4SGary R Hook int ccp_register_des3_algs(struct list_head *head);
281ceeec0afSGary R Hook int ccp_register_rsa_algs(struct list_head *head);
282d3123599STom Lendacky 
283d3123599STom Lendacky #endif
284