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