aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/crypto/ccp/ccp-crypto.h
blob: aed3d2192d013ca397236c901286991f2f455375 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * AMD Cryptographic Coprocessor (CCP) crypto API support
 *
 * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
 *
 * Author: Tom Lendacky <thomas.lendacky@amd.com>
 */

#ifndef __CCP_CRYPTO_H__
#define __CCP_CRYPTO_H__

#include <linux/list.h>
#include <linux/wait.h>
#include <linux/ccp.h>
#include <crypto/algapi.h>
#include <crypto/aes.h>
#include <crypto/internal/aead.h>
#include <crypto/aead.h>
#include <crypto/ctr.h>
#include <crypto/hash.h>
#include <crypto/sha.h>
#include <crypto/akcipher.h>
#include <crypto/skcipher.h>
#include <crypto/internal/rsa.h>

/* We want the module name in front of our messages */
#undef pr_fmt
#define	pr_fmt(fmt)	KBUILD_MODNAME ": " fmt

#define	CCP_LOG_LEVEL	KERN_INFO

#define CCP_CRA_PRIORITY	300

struct ccp_crypto_skcipher_alg {
	struct list_head entry;

	u32 mode;

	struct skcipher_alg alg;
};

struct ccp_crypto_aead {
	struct list_head entry;

	u32 mode;

	struct aead_alg alg;
};

struct ccp_crypto_ahash_alg {
	struct list_head entry;

	const __be32 *init;
	u32 type;
	u32 mode;

	/* Child algorithm used for HMAC, CMAC, etc */
	char child_alg[CRYPTO_MAX_ALG_NAME];

	struct ahash_alg alg;
};

struct ccp_crypto_akcipher_alg {
	struct list_head entry;

	struct akcipher_alg alg;
};

static inline struct ccp_crypto_skcipher_alg *
	ccp_crypto_skcipher_alg(struct crypto_skcipher *tfm)
{
	struct skcipher_alg *alg = crypto_skcipher_alg(tfm);

	return container_of(alg, struct ccp_crypto_skcipher_alg, alg);
}

static inline struct ccp_crypto_ahash_alg *
	ccp_crypto_ahash_alg(struct crypto_tfm *tfm)
{
	struct crypto_alg *alg = tfm->__crt_alg;
	struct ahash_alg *ahash_alg;

	ahash_alg = container_of(alg, struct ahash_alg, halg.base);

	return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg);
}

/***** AES related defines *****/
struct ccp_aes_ctx {
	/* Fallback cipher for XTS with unsupported unit sizes */
	struct crypto_skcipher *tfm_skcipher;

	enum ccp_engine engine;
	enum ccp_aes_type type;
	enum ccp_aes_mode mode;

	struct scatterlist key_sg;
	unsigned int key_len;
	u8 key[AES_MAX_KEY_SIZE * 2];

	u8 nonce[CTR_RFC3686_NONCE_SIZE];

	/* CMAC key structures */
	struct scatterlist k1_sg;
	struct scatterlist k2_sg;
	unsigned int kn_len;
	u8 k1[AES_BLOCK_SIZE];
	u8 k2[AES_BLOCK_SIZE];
};

struct ccp_aes_req_ctx {
	struct scatterlist iv_sg;
	u8 iv[AES_BLOCK_SIZE];

	struct scatterlist tag_sg;
	u8 tag[AES_BLOCK_SIZE];

	/* Fields used for RFC3686 requests */
	u8 *rfc3686_info;
	u8 rfc3686_iv[AES_BLOCK_SIZE];

	struct ccp_cmd cmd;

	struct skcipher_request fallback_req;	// keep at the end
};

struct ccp_aes_cmac_req_ctx {
	unsigned int null_msg;
	unsigned int final;

	struct scatterlist *src;
	unsigned int nbytes;

	u64 hash_cnt;
	unsigned int hash_rem;

	struct sg_table data_sg;

	struct scatterlist iv_sg;
	u8 iv[AES_BLOCK_SIZE];

	struct scatterlist buf_sg;
	unsigned int buf_count;
	u8 buf[AES_BLOCK_SIZE];

	struct scatterlist pad_sg;
	unsigned int pad_count;
	u8 pad[AES_BLOCK_SIZE];

	struct ccp_cmd cmd;
};

struct ccp_aes_cmac_exp_ctx {
	unsigned int null_msg;

	u8 iv[AES_BLOCK_SIZE];

	unsigned int buf_count;
	u8 buf[AES_BLOCK_SIZE];
};

/***** 3DES related defines *****/
struct ccp_des3_ctx {
	enum ccp_engine engine;
	enum ccp_des3_type type;
	enum ccp_des3_mode mode;

	struct scatterlist key_sg;
	unsigned int key_len;
	u8 key[AES_MAX_KEY_SIZE];
};

struct ccp_des3_req_ctx {
	struct scatterlist iv_sg;
	u8 iv[AES_BLOCK_SIZE];

	struct ccp_cmd cmd;
};

/* SHA-related defines
 * These values must be large enough to accommodate any variant
 */
#define MAX_SHA_CONTEXT_SIZE	SHA512_DIGEST_SIZE
#define MAX_SHA_BLOCK_SIZE	SHA512_BLOCK_SIZE

struct ccp_sha_ctx {
	struct scatterlist opad_sg;
	unsigned int opad_count;

	unsigned int key_len;
	u8 key[MAX_SHA_BLOCK_SIZE];
	u8 ipad[MAX_SHA_BLOCK_SIZE];
	u8 opad[MAX_SHA_BLOCK_SIZE];
	struct crypto_shash *hmac_tfm;
};

struct ccp_sha_req_ctx {
	enum ccp_sha_type type;

	u64 msg_bits;

	unsigned int first;
	unsigned int final;

	struct scatterlist *src;
	unsigned int nbytes;

	u64 hash_cnt;
	unsigned int hash_rem;

	struct sg_table data_sg;

	struct scatterlist ctx_sg;
	u8 ctx[MAX_SHA_CONTEXT_SIZE];

	struct scatterlist buf_sg;
	unsigned int buf_count;
	u8 buf[MAX_SHA_BLOCK_SIZE];

	/* CCP driver command */
	struct ccp_cmd cmd;
};

struct ccp_sha_exp_ctx {
	enum ccp_sha_type type;

	u64 msg_bits;

	unsigned int first;

	u8 ctx[MAX_SHA_CONTEXT_SIZE];

	unsigned int buf_count;
	u8 buf[MAX_SHA_BLOCK_SIZE];
};

/***** RSA related defines *****/

struct ccp_rsa_ctx {
	unsigned int key_len; /* in bits */
	struct scatterlist e_sg;
	u8 *e_buf;
	unsigned int e_len;
	struct scatterlist n_sg;
	u8 *n_buf;
	unsigned int n_len;
	struct scatterlist d_sg;
	u8 *d_buf;
	unsigned int d_len;
};

struct ccp_rsa_req_ctx {
	struct ccp_cmd cmd;
};

#define	CCP_RSA_MAXMOD	(4 * 1024 / 8)
#define	CCP5_RSA_MAXMOD	(16 * 1024 / 8)

/***** Common Context Structure *****/
struct ccp_ctx {
	int (*complete)(struct crypto_async_request *req, int ret);

	union {
		struct ccp_aes_ctx aes;
		struct ccp_rsa_ctx rsa;
		struct ccp_sha_ctx sha;
		struct ccp_des3_ctx des3;
	} u;
};

int ccp_crypto_enqueue_request(struct crypto_async_request *req,
			       struct ccp_cmd *cmd);
struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table,
					    struct scatterlist *sg_add);

int ccp_register_aes_algs(struct list_head *head);
int ccp_register_aes_cmac_algs(struct list_head *head);
int ccp_register_aes_xts_algs(struct list_head *head);
int ccp_register_aes_aeads(struct list_head *head);
int ccp_register_sha_algs(struct list_head *head);
int ccp_register_des3_algs(struct list_head *head);
int ccp_register_rsa_algs(struct list_head *head);

#endif