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