xref: /linux/include/linux/ccp.h (revision d91517839e5d95adc0cf4b28caa7af62a71de526)
1 /*
2  * AMD Cryptographic Coprocessor (CCP) driver
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 __CPP_H__
14 #define __CPP_H__
15 
16 #include <linux/scatterlist.h>
17 #include <linux/workqueue.h>
18 #include <linux/list.h>
19 #include <crypto/aes.h>
20 #include <crypto/sha.h>
21 
22 
23 struct ccp_device;
24 struct ccp_cmd;
25 
26 #if defined(CONFIG_CRYPTO_DEV_CCP_DD) || \
27 	defined(CONFIG_CRYPTO_DEV_CCP_DD_MODULE)
28 
29 /**
30  * ccp_enqueue_cmd - queue an operation for processing by the CCP
31  *
32  * @cmd: ccp_cmd struct to be processed
33  *
34  * Refer to the ccp_cmd struct below for required fields.
35  *
36  * Queue a cmd to be processed by the CCP. If queueing the cmd
37  * would exceed the defined length of the cmd queue the cmd will
38  * only be queued if the CCP_CMD_MAY_BACKLOG flag is set and will
39  * result in a return code of -EBUSY.
40  *
41  * The callback routine specified in the ccp_cmd struct will be
42  * called to notify the caller of completion (if the cmd was not
43  * backlogged) or advancement out of the backlog. If the cmd has
44  * advanced out of the backlog the "err" value of the callback
45  * will be -EINPROGRESS. Any other "err" value during callback is
46  * the result of the operation.
47  *
48  * The cmd has been successfully queued if:
49  *   the return code is -EINPROGRESS or
50  *   the return code is -EBUSY and CCP_CMD_MAY_BACKLOG flag is set
51  */
52 int ccp_enqueue_cmd(struct ccp_cmd *cmd);
53 
54 #else /* CONFIG_CRYPTO_DEV_CCP_DD is not enabled */
55 
56 static inline int ccp_enqueue_cmd(struct ccp_cmd *cmd)
57 {
58 	return -ENODEV;
59 }
60 
61 #endif /* CONFIG_CRYPTO_DEV_CCP_DD */
62 
63 
64 /***** AES engine *****/
65 /**
66  * ccp_aes_type - AES key size
67  *
68  * @CCP_AES_TYPE_128: 128-bit key
69  * @CCP_AES_TYPE_192: 192-bit key
70  * @CCP_AES_TYPE_256: 256-bit key
71  */
72 enum ccp_aes_type {
73 	CCP_AES_TYPE_128 = 0,
74 	CCP_AES_TYPE_192,
75 	CCP_AES_TYPE_256,
76 	CCP_AES_TYPE__LAST,
77 };
78 
79 /**
80  * ccp_aes_mode - AES operation mode
81  *
82  * @CCP_AES_MODE_ECB: ECB mode
83  * @CCP_AES_MODE_CBC: CBC mode
84  * @CCP_AES_MODE_OFB: OFB mode
85  * @CCP_AES_MODE_CFB: CFB mode
86  * @CCP_AES_MODE_CTR: CTR mode
87  * @CCP_AES_MODE_CMAC: CMAC mode
88  */
89 enum ccp_aes_mode {
90 	CCP_AES_MODE_ECB = 0,
91 	CCP_AES_MODE_CBC,
92 	CCP_AES_MODE_OFB,
93 	CCP_AES_MODE_CFB,
94 	CCP_AES_MODE_CTR,
95 	CCP_AES_MODE_CMAC,
96 	CCP_AES_MODE__LAST,
97 };
98 
99 /**
100  * ccp_aes_mode - AES operation mode
101  *
102  * @CCP_AES_ACTION_DECRYPT: AES decrypt operation
103  * @CCP_AES_ACTION_ENCRYPT: AES encrypt operation
104  */
105 enum ccp_aes_action {
106 	CCP_AES_ACTION_DECRYPT = 0,
107 	CCP_AES_ACTION_ENCRYPT,
108 	CCP_AES_ACTION__LAST,
109 };
110 
111 /**
112  * struct ccp_aes_engine - CCP AES operation
113  * @type: AES operation key size
114  * @mode: AES operation mode
115  * @action: AES operation (decrypt/encrypt)
116  * @key: key to be used for this AES operation
117  * @key_len: length in bytes of key
118  * @iv: IV to be used for this AES operation
119  * @iv_len: length in bytes of iv
120  * @src: data to be used for this operation
121  * @dst: data produced by this operation
122  * @src_len: length in bytes of data used for this operation
123  * @cmac_final: indicates final operation when running in CMAC mode
124  * @cmac_key: K1/K2 key used in final CMAC operation
125  * @cmac_key_len: length in bytes of cmac_key
126  *
127  * Variables required to be set when calling ccp_enqueue_cmd():
128  *   - type, mode, action, key, key_len, src, dst, src_len
129  *   - iv, iv_len for any mode other than ECB
130  *   - cmac_final for CMAC mode
131  *   - cmac_key, cmac_key_len for CMAC mode if cmac_final is non-zero
132  *
133  * The iv variable is used as both input and output. On completion of the
134  * AES operation the new IV overwrites the old IV.
135  */
136 struct ccp_aes_engine {
137 	enum ccp_aes_type type;
138 	enum ccp_aes_mode mode;
139 	enum ccp_aes_action action;
140 
141 	struct scatterlist *key;
142 	u32 key_len;		/* In bytes */
143 
144 	struct scatterlist *iv;
145 	u32 iv_len;		/* In bytes */
146 
147 	struct scatterlist *src, *dst;
148 	u64 src_len;		/* In bytes */
149 
150 	u32 cmac_final;		/* Indicates final cmac cmd */
151 	struct scatterlist *cmac_key;	/* K1/K2 cmac key required for
152 					 * final cmac cmd */
153 	u32 cmac_key_len;	/* In bytes */
154 };
155 
156 /***** XTS-AES engine *****/
157 /**
158  * ccp_xts_aes_unit_size - XTS unit size
159  *
160  * @CCP_XTS_AES_UNIT_SIZE_16: Unit size of 16 bytes
161  * @CCP_XTS_AES_UNIT_SIZE_512: Unit size of 512 bytes
162  * @CCP_XTS_AES_UNIT_SIZE_1024: Unit size of 1024 bytes
163  * @CCP_XTS_AES_UNIT_SIZE_2048: Unit size of 2048 bytes
164  * @CCP_XTS_AES_UNIT_SIZE_4096: Unit size of 4096 bytes
165  */
166 enum ccp_xts_aes_unit_size {
167 	CCP_XTS_AES_UNIT_SIZE_16 = 0,
168 	CCP_XTS_AES_UNIT_SIZE_512,
169 	CCP_XTS_AES_UNIT_SIZE_1024,
170 	CCP_XTS_AES_UNIT_SIZE_2048,
171 	CCP_XTS_AES_UNIT_SIZE_4096,
172 	CCP_XTS_AES_UNIT_SIZE__LAST,
173 };
174 
175 /**
176  * struct ccp_xts_aes_engine - CCP XTS AES operation
177  * @action: AES operation (decrypt/encrypt)
178  * @unit_size: unit size of the XTS operation
179  * @key: key to be used for this XTS AES operation
180  * @key_len: length in bytes of key
181  * @iv: IV to be used for this XTS AES operation
182  * @iv_len: length in bytes of iv
183  * @src: data to be used for this operation
184  * @dst: data produced by this operation
185  * @src_len: length in bytes of data used for this operation
186  * @final: indicates final XTS operation
187  *
188  * Variables required to be set when calling ccp_enqueue_cmd():
189  *   - action, unit_size, key, key_len, iv, iv_len, src, dst, src_len, final
190  *
191  * The iv variable is used as both input and output. On completion of the
192  * AES operation the new IV overwrites the old IV.
193  */
194 struct ccp_xts_aes_engine {
195 	enum ccp_aes_action action;
196 	enum ccp_xts_aes_unit_size unit_size;
197 
198 	struct scatterlist *key;
199 	u32 key_len;		/* In bytes */
200 
201 	struct scatterlist *iv;
202 	u32 iv_len;		/* In bytes */
203 
204 	struct scatterlist *src, *dst;
205 	u64 src_len;		/* In bytes */
206 
207 	u32 final;
208 };
209 
210 /***** SHA engine *****/
211 #define CCP_SHA_BLOCKSIZE               SHA256_BLOCK_SIZE
212 #define CCP_SHA_CTXSIZE                 SHA256_DIGEST_SIZE
213 
214 /**
215  * ccp_sha_type - type of SHA operation
216  *
217  * @CCP_SHA_TYPE_1: SHA-1 operation
218  * @CCP_SHA_TYPE_224: SHA-224 operation
219  * @CCP_SHA_TYPE_256: SHA-256 operation
220  */
221 enum ccp_sha_type {
222 	CCP_SHA_TYPE_1 = 1,
223 	CCP_SHA_TYPE_224,
224 	CCP_SHA_TYPE_256,
225 	CCP_SHA_TYPE__LAST,
226 };
227 
228 /**
229  * struct ccp_sha_engine - CCP SHA operation
230  * @type: Type of SHA operation
231  * @ctx: current hash value
232  * @ctx_len: length in bytes of hash value
233  * @src: data to be used for this operation
234  * @src_len: length in bytes of data used for this operation
235  * @final: indicates final SHA operation
236  * @msg_bits: total length of the message in bits used in final SHA operation
237  *
238  * Variables required to be set when calling ccp_enqueue_cmd():
239  *   - type, ctx, ctx_len, src, src_len, final
240  *   - msg_bits if final is non-zero
241  *
242  * The ctx variable is used as both input and output. On completion of the
243  * SHA operation the new hash value overwrites the old hash value.
244  */
245 struct ccp_sha_engine {
246 	enum ccp_sha_type type;
247 
248 	struct scatterlist *ctx;
249 	u32 ctx_len;		/* In bytes */
250 
251 	struct scatterlist *src;
252 	u64 src_len;		/* In bytes */
253 
254 	u32 final;		/* Indicates final sha cmd */
255 	u64 msg_bits;		/* Message length in bits required for
256 				 * final sha cmd */
257 };
258 
259 /***** RSA engine *****/
260 /**
261  * struct ccp_rsa_engine - CCP RSA operation
262  * @key_size: length in bits of RSA key
263  * @exp: RSA exponent
264  * @exp_len: length in bytes of exponent
265  * @mod: RSA modulus
266  * @mod_len: length in bytes of modulus
267  * @src: data to be used for this operation
268  * @dst: data produced by this operation
269  * @src_len: length in bytes of data used for this operation
270  *
271  * Variables required to be set when calling ccp_enqueue_cmd():
272  *   - key_size, exp, exp_len, mod, mod_len, src, dst, src_len
273  */
274 struct ccp_rsa_engine {
275 	u32 key_size;		/* In bits */
276 
277 	struct scatterlist *exp;
278 	u32 exp_len;		/* In bytes */
279 
280 	struct scatterlist *mod;
281 	u32 mod_len;		/* In bytes */
282 
283 	struct scatterlist *src, *dst;
284 	u32 src_len;		/* In bytes */
285 };
286 
287 /***** Passthru engine *****/
288 /**
289  * ccp_passthru_bitwise - type of bitwise passthru operation
290  *
291  * @CCP_PASSTHRU_BITWISE_NOOP: no bitwise operation performed
292  * @CCP_PASSTHRU_BITWISE_AND: perform bitwise AND of src with mask
293  * @CCP_PASSTHRU_BITWISE_OR: perform bitwise OR of src with mask
294  * @CCP_PASSTHRU_BITWISE_XOR: perform bitwise XOR of src with mask
295  * @CCP_PASSTHRU_BITWISE_MASK: overwrite with mask
296  */
297 enum ccp_passthru_bitwise {
298 	CCP_PASSTHRU_BITWISE_NOOP = 0,
299 	CCP_PASSTHRU_BITWISE_AND,
300 	CCP_PASSTHRU_BITWISE_OR,
301 	CCP_PASSTHRU_BITWISE_XOR,
302 	CCP_PASSTHRU_BITWISE_MASK,
303 	CCP_PASSTHRU_BITWISE__LAST,
304 };
305 
306 /**
307  * ccp_passthru_byteswap - type of byteswap passthru operation
308  *
309  * @CCP_PASSTHRU_BYTESWAP_NOOP: no byte swapping performed
310  * @CCP_PASSTHRU_BYTESWAP_32BIT: swap bytes within 32-bit words
311  * @CCP_PASSTHRU_BYTESWAP_256BIT: swap bytes within 256-bit words
312  */
313 enum ccp_passthru_byteswap {
314 	CCP_PASSTHRU_BYTESWAP_NOOP = 0,
315 	CCP_PASSTHRU_BYTESWAP_32BIT,
316 	CCP_PASSTHRU_BYTESWAP_256BIT,
317 	CCP_PASSTHRU_BYTESWAP__LAST,
318 };
319 
320 /**
321  * struct ccp_passthru_engine - CCP pass-through operation
322  * @bit_mod: bitwise operation to perform
323  * @byte_swap: byteswap operation to perform
324  * @mask: mask to be applied to data
325  * @mask_len: length in bytes of mask
326  * @src: data to be used for this operation
327  * @dst: data produced by this operation
328  * @src_len: length in bytes of data used for this operation
329  * @final: indicate final pass-through operation
330  *
331  * Variables required to be set when calling ccp_enqueue_cmd():
332  *   - bit_mod, byte_swap, src, dst, src_len
333  *   - mask, mask_len if bit_mod is not CCP_PASSTHRU_BITWISE_NOOP
334  */
335 struct ccp_passthru_engine {
336 	enum ccp_passthru_bitwise bit_mod;
337 	enum ccp_passthru_byteswap byte_swap;
338 
339 	struct scatterlist *mask;
340 	u32 mask_len;		/* In bytes */
341 
342 	struct scatterlist *src, *dst;
343 	u64 src_len;		/* In bytes */
344 
345 	u32 final;
346 };
347 
348 /***** ECC engine *****/
349 #define CCP_ECC_MODULUS_BYTES	48	/* 384-bits */
350 #define CCP_ECC_MAX_OPERANDS	6
351 #define CCP_ECC_MAX_OUTPUTS	3
352 
353 /**
354  * ccp_ecc_function - type of ECC function
355  *
356  * @CCP_ECC_FUNCTION_MMUL_384BIT: 384-bit modular multiplication
357  * @CCP_ECC_FUNCTION_MADD_384BIT: 384-bit modular addition
358  * @CCP_ECC_FUNCTION_MINV_384BIT: 384-bit multiplicative inverse
359  * @CCP_ECC_FUNCTION_PADD_384BIT: 384-bit point addition
360  * @CCP_ECC_FUNCTION_PMUL_384BIT: 384-bit point multiplication
361  * @CCP_ECC_FUNCTION_PDBL_384BIT: 384-bit point doubling
362  */
363 enum ccp_ecc_function {
364 	CCP_ECC_FUNCTION_MMUL_384BIT = 0,
365 	CCP_ECC_FUNCTION_MADD_384BIT,
366 	CCP_ECC_FUNCTION_MINV_384BIT,
367 	CCP_ECC_FUNCTION_PADD_384BIT,
368 	CCP_ECC_FUNCTION_PMUL_384BIT,
369 	CCP_ECC_FUNCTION_PDBL_384BIT,
370 };
371 
372 /**
373  * struct ccp_ecc_modular_math - CCP ECC modular math parameters
374  * @operand_1: first operand for the modular math operation
375  * @operand_1_len: length of the first operand
376  * @operand_2: second operand for the modular math operation
377  *	       (not used for CCP_ECC_FUNCTION_MINV_384BIT)
378  * @operand_2_len: length of the second operand
379  *	       (not used for CCP_ECC_FUNCTION_MINV_384BIT)
380  * @result: result of the modular math operation
381  * @result_len: length of the supplied result buffer
382  */
383 struct ccp_ecc_modular_math {
384 	struct scatterlist *operand_1;
385 	unsigned int operand_1_len;	/* In bytes */
386 
387 	struct scatterlist *operand_2;
388 	unsigned int operand_2_len;	/* In bytes */
389 
390 	struct scatterlist *result;
391 	unsigned int result_len;	/* In bytes */
392 };
393 
394 /**
395  * struct ccp_ecc_point - CCP ECC point definition
396  * @x: the x coordinate of the ECC point
397  * @x_len: the length of the x coordinate
398  * @y: the y coordinate of the ECC point
399  * @y_len: the length of the y coordinate
400  */
401 struct ccp_ecc_point {
402 	struct scatterlist *x;
403 	unsigned int x_len;	/* In bytes */
404 
405 	struct scatterlist *y;
406 	unsigned int y_len;	/* In bytes */
407 };
408 
409 /**
410  * struct ccp_ecc_point_math - CCP ECC point math parameters
411  * @point_1: the first point of the ECC point math operation
412  * @point_2: the second point of the ECC point math operation
413  *	     (only used for CCP_ECC_FUNCTION_PADD_384BIT)
414  * @domain_a: the a parameter of the ECC curve
415  * @domain_a_len: the length of the a parameter
416  * @scalar: the scalar parameter for the point match operation
417  *	    (only used for CCP_ECC_FUNCTION_PMUL_384BIT)
418  * @scalar_len: the length of the scalar parameter
419  *		(only used for CCP_ECC_FUNCTION_PMUL_384BIT)
420  * @result: the point resulting from the point math operation
421  */
422 struct ccp_ecc_point_math {
423 	struct ccp_ecc_point point_1;
424 	struct ccp_ecc_point point_2;
425 
426 	struct scatterlist *domain_a;
427 	unsigned int domain_a_len;	/* In bytes */
428 
429 	struct scatterlist *scalar;
430 	unsigned int scalar_len;	/* In bytes */
431 
432 	struct ccp_ecc_point result;
433 };
434 
435 /**
436  * struct ccp_ecc_engine - CCP ECC operation
437  * @function: ECC function to perform
438  * @mod: ECC modulus
439  * @mod_len: length in bytes of modulus
440  * @mm: module math parameters
441  * @pm: point math parameters
442  * @ecc_result: result of the ECC operation
443  *
444  * Variables required to be set when calling ccp_enqueue_cmd():
445  *   - function, mod, mod_len
446  *   - operand, operand_len, operand_count, output, output_len, output_count
447  *   - ecc_result
448  */
449 struct ccp_ecc_engine {
450 	enum ccp_ecc_function function;
451 
452 	struct scatterlist *mod;
453 	u32 mod_len;		/* In bytes */
454 
455 	union {
456 		struct ccp_ecc_modular_math mm;
457 		struct ccp_ecc_point_math pm;
458 	} u;
459 
460 	u16 ecc_result;
461 };
462 
463 
464 /**
465  * ccp_engine - CCP operation identifiers
466  *
467  * @CCP_ENGINE_AES: AES operation
468  * @CCP_ENGINE_XTS_AES: 128-bit XTS AES operation
469  * @CCP_ENGINE_RSVD1: unused
470  * @CCP_ENGINE_SHA: SHA operation
471  * @CCP_ENGINE_RSA: RSA operation
472  * @CCP_ENGINE_PASSTHRU: pass-through operation
473  * @CCP_ENGINE_ZLIB_DECOMPRESS: unused
474  * @CCP_ENGINE_ECC: ECC operation
475  */
476 enum ccp_engine {
477 	CCP_ENGINE_AES = 0,
478 	CCP_ENGINE_XTS_AES_128,
479 	CCP_ENGINE_RSVD1,
480 	CCP_ENGINE_SHA,
481 	CCP_ENGINE_RSA,
482 	CCP_ENGINE_PASSTHRU,
483 	CCP_ENGINE_ZLIB_DECOMPRESS,
484 	CCP_ENGINE_ECC,
485 	CCP_ENGINE__LAST,
486 };
487 
488 /* Flag values for flags member of ccp_cmd */
489 #define CCP_CMD_MAY_BACKLOG	0x00000001
490 
491 /**
492  * struct ccp_cmd - CPP operation request
493  * @entry: list element (ccp driver use only)
494  * @work: work element used for callbacks (ccp driver use only)
495  * @ccp: CCP device to be run on (ccp driver use only)
496  * @ret: operation return code (ccp driver use only)
497  * @flags: cmd processing flags
498  * @engine: CCP operation to perform
499  * @engine_error: CCP engine return code
500  * @u: engine specific structures, refer to specific engine struct below
501  * @callback: operation completion callback function
502  * @data: parameter value to be supplied to the callback function
503  *
504  * Variables required to be set when calling ccp_enqueue_cmd():
505  *   - engine, callback
506  *   - See the operation structures below for what is required for each
507  *     operation.
508  */
509 struct ccp_cmd {
510 	/* The list_head, work_struct, ccp and ret variables are for use
511 	 * by the CCP driver only.
512 	 */
513 	struct list_head entry;
514 	struct work_struct work;
515 	struct ccp_device *ccp;
516 	int ret;
517 
518 	u32 flags;
519 
520 	enum ccp_engine engine;
521 	u32 engine_error;
522 
523 	union {
524 		struct ccp_aes_engine aes;
525 		struct ccp_xts_aes_engine xts;
526 		struct ccp_sha_engine sha;
527 		struct ccp_rsa_engine rsa;
528 		struct ccp_passthru_engine passthru;
529 		struct ccp_ecc_engine ecc;
530 	} u;
531 
532 	/* Completion callback support */
533 	void (*callback)(void *data, int err);
534 	void *data;
535 };
536 
537 #endif
538