xref: /openbmc/linux/drivers/crypto/ccp/ccp-crypto.h (revision 990672d48515ce09c76fcf1ceccee48b0dd1942b)
1d3123599STom Lendacky /*
2d3123599STom Lendacky  * AMD Cryptographic Coprocessor (CCP) crypto API support
3d3123599STom Lendacky  *
4d3123599STom Lendacky  * Copyright (C) 2013 Advanced Micro Devices, Inc.
5d3123599STom Lendacky  *
6d3123599STom Lendacky  * Author: Tom Lendacky <thomas.lendacky@amd.com>
7d3123599STom Lendacky  *
8d3123599STom Lendacky  * This program is free software; you can redistribute it and/or modify
9d3123599STom Lendacky  * it under the terms of the GNU General Public License version 2 as
10d3123599STom Lendacky  * published by the Free Software Foundation.
11d3123599STom Lendacky  */
12d3123599STom Lendacky 
13d3123599STom Lendacky #ifndef __CCP_CRYPTO_H__
14d3123599STom Lendacky #define __CCP_CRYPTO_H__
15d3123599STom Lendacky 
16d3123599STom Lendacky #include <linux/list.h>
17d3123599STom Lendacky #include <linux/wait.h>
18d3123599STom Lendacky #include <linux/pci.h>
19d3123599STom Lendacky #include <linux/ccp.h>
20d3123599STom Lendacky #include <crypto/algapi.h>
21d3123599STom Lendacky #include <crypto/aes.h>
22d3123599STom Lendacky #include <crypto/ctr.h>
23d3123599STom Lendacky #include <crypto/hash.h>
24d3123599STom Lendacky #include <crypto/sha.h>
25d3123599STom Lendacky 
26*990672d4SGary R Hook #define	CCP_LOG_LEVEL	KERN_INFO
27*990672d4SGary R Hook 
28d3123599STom Lendacky #define CCP_CRA_PRIORITY	300
29d3123599STom Lendacky 
30d3123599STom Lendacky struct ccp_crypto_ablkcipher_alg {
31d3123599STom Lendacky 	struct list_head entry;
32d3123599STom Lendacky 
33d3123599STom Lendacky 	u32 mode;
34d3123599STom Lendacky 
35d3123599STom Lendacky 	struct crypto_alg alg;
36d3123599STom Lendacky };
37d3123599STom Lendacky 
38d3123599STom Lendacky struct ccp_crypto_ahash_alg {
39d3123599STom Lendacky 	struct list_head entry;
40d3123599STom Lendacky 
416f0be9b2STom Lendacky 	const __be32 *init;
42d3123599STom Lendacky 	u32 type;
43d3123599STom Lendacky 	u32 mode;
44d3123599STom Lendacky 
45d3123599STom Lendacky 	/* Child algorithm used for HMAC, CMAC, etc */
46d3123599STom Lendacky 	char child_alg[CRYPTO_MAX_ALG_NAME];
47d3123599STom Lendacky 
48d3123599STom Lendacky 	struct ahash_alg alg;
49d3123599STom Lendacky };
50d3123599STom Lendacky 
51d3123599STom Lendacky static inline struct ccp_crypto_ablkcipher_alg *
52d3123599STom Lendacky 	ccp_crypto_ablkcipher_alg(struct crypto_tfm *tfm)
53d3123599STom Lendacky {
54d3123599STom Lendacky 	struct crypto_alg *alg = tfm->__crt_alg;
55d3123599STom Lendacky 
56d3123599STom Lendacky 	return container_of(alg, struct ccp_crypto_ablkcipher_alg, alg);
57d3123599STom Lendacky }
58d3123599STom Lendacky 
59d3123599STom Lendacky static inline struct ccp_crypto_ahash_alg *
60d3123599STom Lendacky 	ccp_crypto_ahash_alg(struct crypto_tfm *tfm)
61d3123599STom Lendacky {
62d3123599STom Lendacky 	struct crypto_alg *alg = tfm->__crt_alg;
63d3123599STom Lendacky 	struct ahash_alg *ahash_alg;
64d3123599STom Lendacky 
65d3123599STom Lendacky 	ahash_alg = container_of(alg, struct ahash_alg, halg.base);
66d3123599STom Lendacky 
67d3123599STom Lendacky 	return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg);
68d3123599STom Lendacky }
69d3123599STom Lendacky 
70d3123599STom Lendacky /***** AES related defines *****/
71d3123599STom Lendacky struct ccp_aes_ctx {
72d3123599STom Lendacky 	/* Fallback cipher for XTS with unsupported unit sizes */
73241118deSHerbert Xu 	struct crypto_skcipher *tfm_skcipher;
74d3123599STom Lendacky 
75d3123599STom Lendacky 	/* Cipher used to generate CMAC K1/K2 keys */
76d3123599STom Lendacky 	struct crypto_cipher *tfm_cipher;
77d3123599STom Lendacky 
78d3123599STom Lendacky 	enum ccp_engine engine;
79d3123599STom Lendacky 	enum ccp_aes_type type;
80d3123599STom Lendacky 	enum ccp_aes_mode mode;
81d3123599STom Lendacky 
82d3123599STom Lendacky 	struct scatterlist key_sg;
83d3123599STom Lendacky 	unsigned int key_len;
84d3123599STom Lendacky 	u8 key[AES_MAX_KEY_SIZE];
85d3123599STom Lendacky 
86d3123599STom Lendacky 	u8 nonce[CTR_RFC3686_NONCE_SIZE];
87d3123599STom Lendacky 
88d3123599STom Lendacky 	/* CMAC key structures */
89d3123599STom Lendacky 	struct scatterlist k1_sg;
90d3123599STom Lendacky 	struct scatterlist k2_sg;
91d3123599STom Lendacky 	unsigned int kn_len;
92d3123599STom Lendacky 	u8 k1[AES_BLOCK_SIZE];
93d3123599STom Lendacky 	u8 k2[AES_BLOCK_SIZE];
94d3123599STom Lendacky };
95d3123599STom Lendacky 
96d3123599STom Lendacky struct ccp_aes_req_ctx {
97d3123599STom Lendacky 	struct scatterlist iv_sg;
98d3123599STom Lendacky 	u8 iv[AES_BLOCK_SIZE];
99d3123599STom Lendacky 
100d3123599STom Lendacky 	/* Fields used for RFC3686 requests */
101d3123599STom Lendacky 	u8 *rfc3686_info;
102d3123599STom Lendacky 	u8 rfc3686_iv[AES_BLOCK_SIZE];
103d3123599STom Lendacky 
104d3123599STom Lendacky 	struct ccp_cmd cmd;
105d3123599STom Lendacky };
106d3123599STom Lendacky 
107d3123599STom Lendacky struct ccp_aes_cmac_req_ctx {
108d3123599STom Lendacky 	unsigned int null_msg;
109d3123599STom Lendacky 	unsigned int final;
110d3123599STom Lendacky 
11181a59f00STom Lendacky 	struct scatterlist *src;
11281a59f00STom Lendacky 	unsigned int nbytes;
11381a59f00STom Lendacky 
11481a59f00STom Lendacky 	u64 hash_cnt;
115d3123599STom Lendacky 	unsigned int hash_rem;
116d3123599STom Lendacky 
117d3123599STom Lendacky 	struct sg_table data_sg;
118d3123599STom Lendacky 
119d3123599STom Lendacky 	struct scatterlist iv_sg;
120d3123599STom Lendacky 	u8 iv[AES_BLOCK_SIZE];
121d3123599STom Lendacky 
122d3123599STom Lendacky 	struct scatterlist buf_sg;
123d3123599STom Lendacky 	unsigned int buf_count;
124d3123599STom Lendacky 	u8 buf[AES_BLOCK_SIZE];
125d3123599STom Lendacky 
126d3123599STom Lendacky 	struct scatterlist pad_sg;
127d3123599STom Lendacky 	unsigned int pad_count;
128d3123599STom Lendacky 	u8 pad[AES_BLOCK_SIZE];
129d3123599STom Lendacky 
130d3123599STom Lendacky 	struct ccp_cmd cmd;
131d3123599STom Lendacky };
132d3123599STom Lendacky 
133d1662165STom Lendacky struct ccp_aes_cmac_exp_ctx {
134d1662165STom Lendacky 	unsigned int null_msg;
135d1662165STom Lendacky 
136d1662165STom Lendacky 	u8 iv[AES_BLOCK_SIZE];
137d1662165STom Lendacky 
138d1662165STom Lendacky 	unsigned int buf_count;
139d1662165STom Lendacky 	u8 buf[AES_BLOCK_SIZE];
140d1662165STom Lendacky };
141d1662165STom Lendacky 
142*990672d4SGary R Hook /***** 3DES related defines *****/
143*990672d4SGary R Hook struct ccp_des3_ctx {
144*990672d4SGary R Hook 	enum ccp_engine engine;
145*990672d4SGary R Hook 	enum ccp_des3_type type;
146*990672d4SGary R Hook 	enum ccp_des3_mode mode;
147*990672d4SGary R Hook 
148*990672d4SGary R Hook 	struct scatterlist key_sg;
149*990672d4SGary R Hook 	unsigned int key_len;
150*990672d4SGary R Hook 	u8 key[AES_MAX_KEY_SIZE];
151*990672d4SGary R Hook };
152*990672d4SGary R Hook 
153*990672d4SGary R Hook struct ccp_des3_req_ctx {
154*990672d4SGary R Hook 	struct scatterlist iv_sg;
155*990672d4SGary R Hook 	u8 iv[AES_BLOCK_SIZE];
156*990672d4SGary R Hook 
157*990672d4SGary R Hook 	struct ccp_cmd cmd;
158*990672d4SGary R Hook };
159*990672d4SGary R Hook 
160ccebcf3fSGary R Hook /* SHA-related defines
161ccebcf3fSGary R Hook  * These values must be large enough to accommodate any variant
162ccebcf3fSGary R Hook  */
163ccebcf3fSGary R Hook #define MAX_SHA_CONTEXT_SIZE	SHA512_DIGEST_SIZE
164ccebcf3fSGary R Hook #define MAX_SHA_BLOCK_SIZE	SHA512_BLOCK_SIZE
165d3123599STom Lendacky 
166d3123599STom Lendacky struct ccp_sha_ctx {
167c11baa02STom Lendacky 	struct scatterlist opad_sg;
168c11baa02STom Lendacky 	unsigned int opad_count;
169c11baa02STom Lendacky 
170d3123599STom Lendacky 	unsigned int key_len;
171d3123599STom Lendacky 	u8 key[MAX_SHA_BLOCK_SIZE];
172d3123599STom Lendacky 	u8 ipad[MAX_SHA_BLOCK_SIZE];
173d3123599STom Lendacky 	u8 opad[MAX_SHA_BLOCK_SIZE];
174c11baa02STom Lendacky 	struct crypto_shash *hmac_tfm;
175d3123599STom Lendacky };
176d3123599STom Lendacky 
177d3123599STom Lendacky struct ccp_sha_req_ctx {
178d3123599STom Lendacky 	enum ccp_sha_type type;
179d3123599STom Lendacky 
180d3123599STom Lendacky 	u64 msg_bits;
181d3123599STom Lendacky 
182d3123599STom Lendacky 	unsigned int first;
183d3123599STom Lendacky 	unsigned int final;
184d3123599STom Lendacky 
18581a59f00STom Lendacky 	struct scatterlist *src;
18681a59f00STom Lendacky 	unsigned int nbytes;
18781a59f00STom Lendacky 
18881a59f00STom Lendacky 	u64 hash_cnt;
189d3123599STom Lendacky 	unsigned int hash_rem;
190d3123599STom Lendacky 
191d3123599STom Lendacky 	struct sg_table data_sg;
192d3123599STom Lendacky 
193d3123599STom Lendacky 	struct scatterlist ctx_sg;
194d3123599STom Lendacky 	u8 ctx[MAX_SHA_CONTEXT_SIZE];
195d3123599STom Lendacky 
196d3123599STom Lendacky 	struct scatterlist buf_sg;
197d3123599STom Lendacky 	unsigned int buf_count;
198d3123599STom Lendacky 	u8 buf[MAX_SHA_BLOCK_SIZE];
199d3123599STom Lendacky 
200d3123599STom Lendacky 	/* CCP driver command */
201d3123599STom Lendacky 	struct ccp_cmd cmd;
202d3123599STom Lendacky };
203d3123599STom Lendacky 
204d1662165STom Lendacky struct ccp_sha_exp_ctx {
205d1662165STom Lendacky 	enum ccp_sha_type type;
206d1662165STom Lendacky 
207d1662165STom Lendacky 	u64 msg_bits;
208d1662165STom Lendacky 
209d1662165STom Lendacky 	unsigned int first;
210d1662165STom Lendacky 
211d1662165STom Lendacky 	u8 ctx[MAX_SHA_CONTEXT_SIZE];
212d1662165STom Lendacky 
213d1662165STom Lendacky 	unsigned int buf_count;
214d1662165STom Lendacky 	u8 buf[MAX_SHA_BLOCK_SIZE];
215d1662165STom Lendacky };
216d1662165STom Lendacky 
217d3123599STom Lendacky /***** Common Context Structure *****/
218d3123599STom Lendacky struct ccp_ctx {
219d3123599STom Lendacky 	int (*complete)(struct crypto_async_request *req, int ret);
220d3123599STom Lendacky 
221d3123599STom Lendacky 	union {
222d3123599STom Lendacky 		struct ccp_aes_ctx aes;
223d3123599STom Lendacky 		struct ccp_sha_ctx sha;
224*990672d4SGary R Hook 		struct ccp_des3_ctx des3;
225d3123599STom Lendacky 	} u;
226d3123599STom Lendacky };
227d3123599STom Lendacky 
228d3123599STom Lendacky int ccp_crypto_enqueue_request(struct crypto_async_request *req,
229d3123599STom Lendacky 			       struct ccp_cmd *cmd);
230d3123599STom Lendacky struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table,
231d3123599STom Lendacky 					    struct scatterlist *sg_add);
232d3123599STom Lendacky 
233d3123599STom Lendacky int ccp_register_aes_algs(struct list_head *head);
234d3123599STom Lendacky int ccp_register_aes_cmac_algs(struct list_head *head);
235d3123599STom Lendacky int ccp_register_aes_xts_algs(struct list_head *head);
236d3123599STom Lendacky int ccp_register_sha_algs(struct list_head *head);
237*990672d4SGary R Hook int ccp_register_des3_algs(struct list_head *head);
238d3123599STom Lendacky 
239d3123599STom Lendacky #endif
240