Loading...
1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * AMD Cryptographic Coprocessor (CCP) driver
4 *
5 * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
6 *
7 * Author: Tom Lendacky <thomas.lendacky@amd.com>
8 * Author: Gary R Hook <gary.hook@amd.com>
9 */
10
11#ifndef __CCP_H__
12#define __CCP_H__
13
14#include <linux/scatterlist.h>
15#include <linux/workqueue.h>
16#include <linux/list.h>
17#include <crypto/aes.h>
18#include <crypto/sha.h>
19
20struct ccp_device;
21struct ccp_cmd;
22
23#if defined(CONFIG_CRYPTO_DEV_SP_CCP)
24
25/**
26 * ccp_present - check if a CCP device is present
27 *
28 * Returns zero if a CCP device is present, -ENODEV otherwise.
29 */
30int ccp_present(void);
31
32#define CCP_VSIZE 16
33#define CCP_VMASK ((unsigned int)((1 << CCP_VSIZE) - 1))
34#define CCP_VERSION(v, r) ((unsigned int)((v << CCP_VSIZE) \
35 | (r & CCP_VMASK)))
36
37/**
38 * ccp_version - get the version of the CCP
39 *
40 * Returns a positive version number, or zero if no CCP
41 */
42unsigned int ccp_version(void);
43
44/**
45 * ccp_enqueue_cmd - queue an operation for processing by the CCP
46 *
47 * @cmd: ccp_cmd struct to be processed
48 *
49 * Refer to the ccp_cmd struct below for required fields.
50 *
51 * Queue a cmd to be processed by the CCP. If queueing the cmd
52 * would exceed the defined length of the cmd queue the cmd will
53 * only be queued if the CCP_CMD_MAY_BACKLOG flag is set and will
54 * result in a return code of -EBUSY.
55 *
56 * The callback routine specified in the ccp_cmd struct will be
57 * called to notify the caller of completion (if the cmd was not
58 * backlogged) or advancement out of the backlog. If the cmd has
59 * advanced out of the backlog the "err" value of the callback
60 * will be -EINPROGRESS. Any other "err" value during callback is
61 * the result of the operation.
62 *
63 * The cmd has been successfully queued if:
64 * the return code is -EINPROGRESS or
65 * the return code is -EBUSY and CCP_CMD_MAY_BACKLOG flag is set
66 */
67int ccp_enqueue_cmd(struct ccp_cmd *cmd);
68
69#else /* CONFIG_CRYPTO_DEV_CCP_SP_DEV is not enabled */
70
71static inline int ccp_present(void)
72{
73 return -ENODEV;
74}
75
76static inline unsigned int ccp_version(void)
77{
78 return 0;
79}
80
81static inline int ccp_enqueue_cmd(struct ccp_cmd *cmd)
82{
83 return -ENODEV;
84}
85
86#endif /* CONFIG_CRYPTO_DEV_SP_CCP */
87
88
89/***** AES engine *****/
90/**
91 * ccp_aes_type - AES key size
92 *
93 * @CCP_AES_TYPE_128: 128-bit key
94 * @CCP_AES_TYPE_192: 192-bit key
95 * @CCP_AES_TYPE_256: 256-bit key
96 */
97enum ccp_aes_type {
98 CCP_AES_TYPE_128 = 0,
99 CCP_AES_TYPE_192,
100 CCP_AES_TYPE_256,
101 CCP_AES_TYPE__LAST,
102};
103
104/**
105 * ccp_aes_mode - AES operation mode
106 *
107 * @CCP_AES_MODE_ECB: ECB mode
108 * @CCP_AES_MODE_CBC: CBC mode
109 * @CCP_AES_MODE_OFB: OFB mode
110 * @CCP_AES_MODE_CFB: CFB mode
111 * @CCP_AES_MODE_CTR: CTR mode
112 * @CCP_AES_MODE_CMAC: CMAC mode
113 */
114enum ccp_aes_mode {
115 CCP_AES_MODE_ECB = 0,
116 CCP_AES_MODE_CBC,
117 CCP_AES_MODE_OFB,
118 CCP_AES_MODE_CFB,
119 CCP_AES_MODE_CTR,
120 CCP_AES_MODE_CMAC,
121 CCP_AES_MODE_GHASH,
122 CCP_AES_MODE_GCTR,
123 CCP_AES_MODE_GCM,
124 CCP_AES_MODE_GMAC,
125 CCP_AES_MODE__LAST,
126};
127
128/**
129 * ccp_aes_mode - AES operation mode
130 *
131 * @CCP_AES_ACTION_DECRYPT: AES decrypt operation
132 * @CCP_AES_ACTION_ENCRYPT: AES encrypt operation
133 */
134enum ccp_aes_action {
135 CCP_AES_ACTION_DECRYPT = 0,
136 CCP_AES_ACTION_ENCRYPT,
137 CCP_AES_ACTION__LAST,
138};
139/* Overloaded field */
140#define CCP_AES_GHASHAAD CCP_AES_ACTION_DECRYPT
141#define CCP_AES_GHASHFINAL CCP_AES_ACTION_ENCRYPT
142
143/**
144 * struct ccp_aes_engine - CCP AES operation
145 * @type: AES operation key size
146 * @mode: AES operation mode
147 * @action: AES operation (decrypt/encrypt)
148 * @key: key to be used for this AES operation
149 * @key_len: length in bytes of key
150 * @iv: IV to be used for this AES operation
151 * @iv_len: length in bytes of iv
152 * @src: data to be used for this operation
153 * @dst: data produced by this operation
154 * @src_len: length in bytes of data used for this operation
155 * @cmac_final: indicates final operation when running in CMAC mode
156 * @cmac_key: K1/K2 key used in final CMAC operation
157 * @cmac_key_len: length in bytes of cmac_key
158 *
159 * Variables required to be set when calling ccp_enqueue_cmd():
160 * - type, mode, action, key, key_len, src, dst, src_len
161 * - iv, iv_len for any mode other than ECB
162 * - cmac_final for CMAC mode
163 * - cmac_key, cmac_key_len for CMAC mode if cmac_final is non-zero
164 *
165 * The iv variable is used as both input and output. On completion of the
166 * AES operation the new IV overwrites the old IV.
167 */
168struct ccp_aes_engine {
169 enum ccp_aes_type type;
170 enum ccp_aes_mode mode;
171 enum ccp_aes_action action;
172
173 u32 authsize;
174
175 struct scatterlist *key;
176 u32 key_len; /* In bytes */
177
178 struct scatterlist *iv;
179 u32 iv_len; /* In bytes */
180
181 struct scatterlist *src, *dst;
182 u64 src_len; /* In bytes */
183
184 u32 cmac_final; /* Indicates final cmac cmd */
185 struct scatterlist *cmac_key; /* K1/K2 cmac key required for
186 * final cmac cmd */
187 u32 cmac_key_len; /* In bytes */
188
189 u32 aad_len; /* In bytes */
190};
191
192/***** XTS-AES engine *****/
193/**
194 * ccp_xts_aes_unit_size - XTS unit size
195 *
196 * @CCP_XTS_AES_UNIT_SIZE_16: Unit size of 16 bytes
197 * @CCP_XTS_AES_UNIT_SIZE_512: Unit size of 512 bytes
198 * @CCP_XTS_AES_UNIT_SIZE_1024: Unit size of 1024 bytes
199 * @CCP_XTS_AES_UNIT_SIZE_2048: Unit size of 2048 bytes
200 * @CCP_XTS_AES_UNIT_SIZE_4096: Unit size of 4096 bytes
201 */
202enum ccp_xts_aes_unit_size {
203 CCP_XTS_AES_UNIT_SIZE_16 = 0,
204 CCP_XTS_AES_UNIT_SIZE_512,
205 CCP_XTS_AES_UNIT_SIZE_1024,
206 CCP_XTS_AES_UNIT_SIZE_2048,
207 CCP_XTS_AES_UNIT_SIZE_4096,
208 CCP_XTS_AES_UNIT_SIZE__LAST,
209};
210
211/**
212 * struct ccp_xts_aes_engine - CCP XTS AES operation
213 * @action: AES operation (decrypt/encrypt)
214 * @unit_size: unit size of the XTS operation
215 * @key: key to be used for this XTS AES operation
216 * @key_len: length in bytes of key
217 * @iv: IV to be used for this XTS AES operation
218 * @iv_len: length in bytes of iv
219 * @src: data to be used for this operation
220 * @dst: data produced by this operation
221 * @src_len: length in bytes of data used for this operation
222 * @final: indicates final XTS operation
223 *
224 * Variables required to be set when calling ccp_enqueue_cmd():
225 * - action, unit_size, key, key_len, iv, iv_len, src, dst, src_len, final
226 *
227 * The iv variable is used as both input and output. On completion of the
228 * AES operation the new IV overwrites the old IV.
229 */
230struct ccp_xts_aes_engine {
231 enum ccp_aes_type type;
232 enum ccp_aes_action action;
233 enum ccp_xts_aes_unit_size unit_size;
234
235 struct scatterlist *key;
236 u32 key_len; /* In bytes */
237
238 struct scatterlist *iv;
239 u32 iv_len; /* In bytes */
240
241 struct scatterlist *src, *dst;
242 u64 src_len; /* In bytes */
243
244 u32 final;
245};
246
247/***** SHA engine *****/
248/**
249 * ccp_sha_type - type of SHA operation
250 *
251 * @CCP_SHA_TYPE_1: SHA-1 operation
252 * @CCP_SHA_TYPE_224: SHA-224 operation
253 * @CCP_SHA_TYPE_256: SHA-256 operation
254 */
255enum ccp_sha_type {
256 CCP_SHA_TYPE_1 = 1,
257 CCP_SHA_TYPE_224,
258 CCP_SHA_TYPE_256,
259 CCP_SHA_TYPE_384,
260 CCP_SHA_TYPE_512,
261 CCP_SHA_TYPE__LAST,
262};
263
264/**
265 * struct ccp_sha_engine - CCP SHA operation
266 * @type: Type of SHA operation
267 * @ctx: current hash value
268 * @ctx_len: length in bytes of hash value
269 * @src: data to be used for this operation
270 * @src_len: length in bytes of data used for this operation
271 * @opad: data to be used for final HMAC operation
272 * @opad_len: length in bytes of data used for final HMAC operation
273 * @first: indicates first SHA operation
274 * @final: indicates final SHA operation
275 * @msg_bits: total length of the message in bits used in final SHA operation
276 *
277 * Variables required to be set when calling ccp_enqueue_cmd():
278 * - type, ctx, ctx_len, src, src_len, final
279 * - msg_bits if final is non-zero
280 *
281 * The ctx variable is used as both input and output. On completion of the
282 * SHA operation the new hash value overwrites the old hash value.
283 */
284struct ccp_sha_engine {
285 enum ccp_sha_type type;
286
287 struct scatterlist *ctx;
288 u32 ctx_len; /* In bytes */
289
290 struct scatterlist *src;
291 u64 src_len; /* In bytes */
292
293 struct scatterlist *opad;
294 u32 opad_len; /* In bytes */
295
296 u32 first; /* Indicates first sha cmd */
297 u32 final; /* Indicates final sha cmd */
298 u64 msg_bits; /* Message length in bits required for
299 * final sha cmd */
300};
301
302/***** 3DES engine *****/
303enum ccp_des3_mode {
304 CCP_DES3_MODE_ECB = 0,
305 CCP_DES3_MODE_CBC,
306 CCP_DES3_MODE_CFB,
307 CCP_DES3_MODE__LAST,
308};
309
310enum ccp_des3_type {
311 CCP_DES3_TYPE_168 = 1,
312 CCP_DES3_TYPE__LAST,
313 };
314
315enum ccp_des3_action {
316 CCP_DES3_ACTION_DECRYPT = 0,
317 CCP_DES3_ACTION_ENCRYPT,
318 CCP_DES3_ACTION__LAST,
319};
320
321/**
322 * struct ccp_des3_engine - CCP SHA operation
323 * @type: Type of 3DES operation
324 * @mode: cipher mode
325 * @action: 3DES operation (decrypt/encrypt)
326 * @key: key to be used for this 3DES operation
327 * @key_len: length of key (in bytes)
328 * @iv: IV to be used for this AES operation
329 * @iv_len: length in bytes of iv
330 * @src: input data to be used for this operation
331 * @src_len: length of input data used for this operation (in bytes)
332 * @dst: output data produced by this operation
333 *
334 * Variables required to be set when calling ccp_enqueue_cmd():
335 * - type, mode, action, key, key_len, src, dst, src_len
336 * - iv, iv_len for any mode other than ECB
337 *
338 * The iv variable is used as both input and output. On completion of the
339 * 3DES operation the new IV overwrites the old IV.
340 */
341struct ccp_des3_engine {
342 enum ccp_des3_type type;
343 enum ccp_des3_mode mode;
344 enum ccp_des3_action action;
345
346 struct scatterlist *key;
347 u32 key_len; /* In bytes */
348
349 struct scatterlist *iv;
350 u32 iv_len; /* In bytes */
351
352 struct scatterlist *src, *dst;
353 u64 src_len; /* In bytes */
354};
355
356/***** RSA engine *****/
357/**
358 * struct ccp_rsa_engine - CCP RSA operation
359 * @key_size: length in bits of RSA key
360 * @exp: RSA exponent
361 * @exp_len: length in bytes of exponent
362 * @mod: RSA modulus
363 * @mod_len: length in bytes of modulus
364 * @src: data to be used for this operation
365 * @dst: data produced by this operation
366 * @src_len: length in bytes of data used for this operation
367 *
368 * Variables required to be set when calling ccp_enqueue_cmd():
369 * - key_size, exp, exp_len, mod, mod_len, src, dst, src_len
370 */
371struct ccp_rsa_engine {
372 u32 key_size; /* In bits */
373
374 struct scatterlist *exp;
375 u32 exp_len; /* In bytes */
376
377 struct scatterlist *mod;
378 u32 mod_len; /* In bytes */
379
380 struct scatterlist *src, *dst;
381 u32 src_len; /* In bytes */
382};
383
384/***** Passthru engine *****/
385/**
386 * ccp_passthru_bitwise - type of bitwise passthru operation
387 *
388 * @CCP_PASSTHRU_BITWISE_NOOP: no bitwise operation performed
389 * @CCP_PASSTHRU_BITWISE_AND: perform bitwise AND of src with mask
390 * @CCP_PASSTHRU_BITWISE_OR: perform bitwise OR of src with mask
391 * @CCP_PASSTHRU_BITWISE_XOR: perform bitwise XOR of src with mask
392 * @CCP_PASSTHRU_BITWISE_MASK: overwrite with mask
393 */
394enum ccp_passthru_bitwise {
395 CCP_PASSTHRU_BITWISE_NOOP = 0,
396 CCP_PASSTHRU_BITWISE_AND,
397 CCP_PASSTHRU_BITWISE_OR,
398 CCP_PASSTHRU_BITWISE_XOR,
399 CCP_PASSTHRU_BITWISE_MASK,
400 CCP_PASSTHRU_BITWISE__LAST,
401};
402
403/**
404 * ccp_passthru_byteswap - type of byteswap passthru operation
405 *
406 * @CCP_PASSTHRU_BYTESWAP_NOOP: no byte swapping performed
407 * @CCP_PASSTHRU_BYTESWAP_32BIT: swap bytes within 32-bit words
408 * @CCP_PASSTHRU_BYTESWAP_256BIT: swap bytes within 256-bit words
409 */
410enum ccp_passthru_byteswap {
411 CCP_PASSTHRU_BYTESWAP_NOOP = 0,
412 CCP_PASSTHRU_BYTESWAP_32BIT,
413 CCP_PASSTHRU_BYTESWAP_256BIT,
414 CCP_PASSTHRU_BYTESWAP__LAST,
415};
416
417/**
418 * struct ccp_passthru_engine - CCP pass-through operation
419 * @bit_mod: bitwise operation to perform
420 * @byte_swap: byteswap operation to perform
421 * @mask: mask to be applied to data
422 * @mask_len: length in bytes of mask
423 * @src: data to be used for this operation
424 * @dst: data produced by this operation
425 * @src_len: length in bytes of data used for this operation
426 * @final: indicate final pass-through operation
427 *
428 * Variables required to be set when calling ccp_enqueue_cmd():
429 * - bit_mod, byte_swap, src, dst, src_len
430 * - mask, mask_len if bit_mod is not CCP_PASSTHRU_BITWISE_NOOP
431 */
432struct ccp_passthru_engine {
433 enum ccp_passthru_bitwise bit_mod;
434 enum ccp_passthru_byteswap byte_swap;
435
436 struct scatterlist *mask;
437 u32 mask_len; /* In bytes */
438
439 struct scatterlist *src, *dst;
440 u64 src_len; /* In bytes */
441
442 u32 final;
443};
444
445/**
446 * struct ccp_passthru_nomap_engine - CCP pass-through operation
447 * without performing DMA mapping
448 * @bit_mod: bitwise operation to perform
449 * @byte_swap: byteswap operation to perform
450 * @mask: mask to be applied to data
451 * @mask_len: length in bytes of mask
452 * @src: data to be used for this operation
453 * @dst: data produced by this operation
454 * @src_len: length in bytes of data used for this operation
455 * @final: indicate final pass-through operation
456 *
457 * Variables required to be set when calling ccp_enqueue_cmd():
458 * - bit_mod, byte_swap, src, dst, src_len
459 * - mask, mask_len if bit_mod is not CCP_PASSTHRU_BITWISE_NOOP
460 */
461struct ccp_passthru_nomap_engine {
462 enum ccp_passthru_bitwise bit_mod;
463 enum ccp_passthru_byteswap byte_swap;
464
465 dma_addr_t mask;
466 u32 mask_len; /* In bytes */
467
468 dma_addr_t src_dma, dst_dma;
469 u64 src_len; /* In bytes */
470
471 u32 final;
472};
473
474/***** ECC engine *****/
475#define CCP_ECC_MODULUS_BYTES 48 /* 384-bits */
476#define CCP_ECC_MAX_OPERANDS 6
477#define CCP_ECC_MAX_OUTPUTS 3
478
479/**
480 * ccp_ecc_function - type of ECC function
481 *
482 * @CCP_ECC_FUNCTION_MMUL_384BIT: 384-bit modular multiplication
483 * @CCP_ECC_FUNCTION_MADD_384BIT: 384-bit modular addition
484 * @CCP_ECC_FUNCTION_MINV_384BIT: 384-bit multiplicative inverse
485 * @CCP_ECC_FUNCTION_PADD_384BIT: 384-bit point addition
486 * @CCP_ECC_FUNCTION_PMUL_384BIT: 384-bit point multiplication
487 * @CCP_ECC_FUNCTION_PDBL_384BIT: 384-bit point doubling
488 */
489enum ccp_ecc_function {
490 CCP_ECC_FUNCTION_MMUL_384BIT = 0,
491 CCP_ECC_FUNCTION_MADD_384BIT,
492 CCP_ECC_FUNCTION_MINV_384BIT,
493 CCP_ECC_FUNCTION_PADD_384BIT,
494 CCP_ECC_FUNCTION_PMUL_384BIT,
495 CCP_ECC_FUNCTION_PDBL_384BIT,
496};
497
498/**
499 * struct ccp_ecc_modular_math - CCP ECC modular math parameters
500 * @operand_1: first operand for the modular math operation
501 * @operand_1_len: length of the first operand
502 * @operand_2: second operand for the modular math operation
503 * (not used for CCP_ECC_FUNCTION_MINV_384BIT)
504 * @operand_2_len: length of the second operand
505 * (not used for CCP_ECC_FUNCTION_MINV_384BIT)
506 * @result: result of the modular math operation
507 * @result_len: length of the supplied result buffer
508 */
509struct ccp_ecc_modular_math {
510 struct scatterlist *operand_1;
511 unsigned int operand_1_len; /* In bytes */
512
513 struct scatterlist *operand_2;
514 unsigned int operand_2_len; /* In bytes */
515
516 struct scatterlist *result;
517 unsigned int result_len; /* In bytes */
518};
519
520/**
521 * struct ccp_ecc_point - CCP ECC point definition
522 * @x: the x coordinate of the ECC point
523 * @x_len: the length of the x coordinate
524 * @y: the y coordinate of the ECC point
525 * @y_len: the length of the y coordinate
526 */
527struct ccp_ecc_point {
528 struct scatterlist *x;
529 unsigned int x_len; /* In bytes */
530
531 struct scatterlist *y;
532 unsigned int y_len; /* In bytes */
533};
534
535/**
536 * struct ccp_ecc_point_math - CCP ECC point math parameters
537 * @point_1: the first point of the ECC point math operation
538 * @point_2: the second point of the ECC point math operation
539 * (only used for CCP_ECC_FUNCTION_PADD_384BIT)
540 * @domain_a: the a parameter of the ECC curve
541 * @domain_a_len: the length of the a parameter
542 * @scalar: the scalar parameter for the point match operation
543 * (only used for CCP_ECC_FUNCTION_PMUL_384BIT)
544 * @scalar_len: the length of the scalar parameter
545 * (only used for CCP_ECC_FUNCTION_PMUL_384BIT)
546 * @result: the point resulting from the point math operation
547 */
548struct ccp_ecc_point_math {
549 struct ccp_ecc_point point_1;
550 struct ccp_ecc_point point_2;
551
552 struct scatterlist *domain_a;
553 unsigned int domain_a_len; /* In bytes */
554
555 struct scatterlist *scalar;
556 unsigned int scalar_len; /* In bytes */
557
558 struct ccp_ecc_point result;
559};
560
561/**
562 * struct ccp_ecc_engine - CCP ECC operation
563 * @function: ECC function to perform
564 * @mod: ECC modulus
565 * @mod_len: length in bytes of modulus
566 * @mm: module math parameters
567 * @pm: point math parameters
568 * @ecc_result: result of the ECC operation
569 *
570 * Variables required to be set when calling ccp_enqueue_cmd():
571 * - function, mod, mod_len
572 * - operand, operand_len, operand_count, output, output_len, output_count
573 * - ecc_result
574 */
575struct ccp_ecc_engine {
576 enum ccp_ecc_function function;
577
578 struct scatterlist *mod;
579 u32 mod_len; /* In bytes */
580
581 union {
582 struct ccp_ecc_modular_math mm;
583 struct ccp_ecc_point_math pm;
584 } u;
585
586 u16 ecc_result;
587};
588
589
590/**
591 * ccp_engine - CCP operation identifiers
592 *
593 * @CCP_ENGINE_AES: AES operation
594 * @CCP_ENGINE_XTS_AES: 128-bit XTS AES operation
595 * @CCP_ENGINE_RSVD1: unused
596 * @CCP_ENGINE_SHA: SHA operation
597 * @CCP_ENGINE_RSA: RSA operation
598 * @CCP_ENGINE_PASSTHRU: pass-through operation
599 * @CCP_ENGINE_ZLIB_DECOMPRESS: unused
600 * @CCP_ENGINE_ECC: ECC operation
601 */
602enum ccp_engine {
603 CCP_ENGINE_AES = 0,
604 CCP_ENGINE_XTS_AES_128,
605 CCP_ENGINE_DES3,
606 CCP_ENGINE_SHA,
607 CCP_ENGINE_RSA,
608 CCP_ENGINE_PASSTHRU,
609 CCP_ENGINE_ZLIB_DECOMPRESS,
610 CCP_ENGINE_ECC,
611 CCP_ENGINE__LAST,
612};
613
614/* Flag values for flags member of ccp_cmd */
615#define CCP_CMD_MAY_BACKLOG 0x00000001
616#define CCP_CMD_PASSTHRU_NO_DMA_MAP 0x00000002
617
618/**
619 * struct ccp_cmd - CCP operation request
620 * @entry: list element (ccp driver use only)
621 * @work: work element used for callbacks (ccp driver use only)
622 * @ccp: CCP device to be run on
623 * @ret: operation return code (ccp driver use only)
624 * @flags: cmd processing flags
625 * @engine: CCP operation to perform
626 * @engine_error: CCP engine return code
627 * @u: engine specific structures, refer to specific engine struct below
628 * @callback: operation completion callback function
629 * @data: parameter value to be supplied to the callback function
630 *
631 * Variables required to be set when calling ccp_enqueue_cmd():
632 * - engine, callback
633 * - See the operation structures below for what is required for each
634 * operation.
635 */
636struct ccp_cmd {
637 /* The list_head, work_struct, ccp and ret variables are for use
638 * by the CCP driver only.
639 */
640 struct list_head entry;
641 struct work_struct work;
642 struct ccp_device *ccp;
643 int ret;
644
645 u32 flags;
646
647 enum ccp_engine engine;
648 u32 engine_error;
649
650 union {
651 struct ccp_aes_engine aes;
652 struct ccp_xts_aes_engine xts;
653 struct ccp_des3_engine des3;
654 struct ccp_sha_engine sha;
655 struct ccp_rsa_engine rsa;
656 struct ccp_passthru_engine passthru;
657 struct ccp_passthru_nomap_engine passthru_nomap;
658 struct ccp_ecc_engine ecc;
659 } u;
660
661 /* Completion callback support */
662 void (*callback)(void *data, int err);
663 void *data;
664};
665
666#endif
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
23struct ccp_device;
24struct 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 */
52int ccp_enqueue_cmd(struct ccp_cmd *cmd);
53
54#else /* CONFIG_CRYPTO_DEV_CCP_DD is not enabled */
55
56static 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 */
72enum 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 */
89enum 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 */
105enum 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 */
136struct 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 */
166enum 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 */
194struct 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 */
221enum 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 * @opad: data to be used for final HMAC operation
236 * @opad_len: length in bytes of data used for final HMAC operation
237 * @first: indicates first SHA operation
238 * @final: indicates final SHA operation
239 * @msg_bits: total length of the message in bits used in final SHA operation
240 *
241 * Variables required to be set when calling ccp_enqueue_cmd():
242 * - type, ctx, ctx_len, src, src_len, final
243 * - msg_bits if final is non-zero
244 *
245 * The ctx variable is used as both input and output. On completion of the
246 * SHA operation the new hash value overwrites the old hash value.
247 */
248struct ccp_sha_engine {
249 enum ccp_sha_type type;
250
251 struct scatterlist *ctx;
252 u32 ctx_len; /* In bytes */
253
254 struct scatterlist *src;
255 u64 src_len; /* In bytes */
256
257 struct scatterlist *opad;
258 u32 opad_len; /* In bytes */
259
260 u32 first; /* Indicates first sha cmd */
261 u32 final; /* Indicates final sha cmd */
262 u64 msg_bits; /* Message length in bits required for
263 * final sha cmd */
264};
265
266/***** RSA engine *****/
267/**
268 * struct ccp_rsa_engine - CCP RSA operation
269 * @key_size: length in bits of RSA key
270 * @exp: RSA exponent
271 * @exp_len: length in bytes of exponent
272 * @mod: RSA modulus
273 * @mod_len: length in bytes of modulus
274 * @src: data to be used for this operation
275 * @dst: data produced by this operation
276 * @src_len: length in bytes of data used for this operation
277 *
278 * Variables required to be set when calling ccp_enqueue_cmd():
279 * - key_size, exp, exp_len, mod, mod_len, src, dst, src_len
280 */
281struct ccp_rsa_engine {
282 u32 key_size; /* In bits */
283
284 struct scatterlist *exp;
285 u32 exp_len; /* In bytes */
286
287 struct scatterlist *mod;
288 u32 mod_len; /* In bytes */
289
290 struct scatterlist *src, *dst;
291 u32 src_len; /* In bytes */
292};
293
294/***** Passthru engine *****/
295/**
296 * ccp_passthru_bitwise - type of bitwise passthru operation
297 *
298 * @CCP_PASSTHRU_BITWISE_NOOP: no bitwise operation performed
299 * @CCP_PASSTHRU_BITWISE_AND: perform bitwise AND of src with mask
300 * @CCP_PASSTHRU_BITWISE_OR: perform bitwise OR of src with mask
301 * @CCP_PASSTHRU_BITWISE_XOR: perform bitwise XOR of src with mask
302 * @CCP_PASSTHRU_BITWISE_MASK: overwrite with mask
303 */
304enum ccp_passthru_bitwise {
305 CCP_PASSTHRU_BITWISE_NOOP = 0,
306 CCP_PASSTHRU_BITWISE_AND,
307 CCP_PASSTHRU_BITWISE_OR,
308 CCP_PASSTHRU_BITWISE_XOR,
309 CCP_PASSTHRU_BITWISE_MASK,
310 CCP_PASSTHRU_BITWISE__LAST,
311};
312
313/**
314 * ccp_passthru_byteswap - type of byteswap passthru operation
315 *
316 * @CCP_PASSTHRU_BYTESWAP_NOOP: no byte swapping performed
317 * @CCP_PASSTHRU_BYTESWAP_32BIT: swap bytes within 32-bit words
318 * @CCP_PASSTHRU_BYTESWAP_256BIT: swap bytes within 256-bit words
319 */
320enum ccp_passthru_byteswap {
321 CCP_PASSTHRU_BYTESWAP_NOOP = 0,
322 CCP_PASSTHRU_BYTESWAP_32BIT,
323 CCP_PASSTHRU_BYTESWAP_256BIT,
324 CCP_PASSTHRU_BYTESWAP__LAST,
325};
326
327/**
328 * struct ccp_passthru_engine - CCP pass-through operation
329 * @bit_mod: bitwise operation to perform
330 * @byte_swap: byteswap operation to perform
331 * @mask: mask to be applied to data
332 * @mask_len: length in bytes of mask
333 * @src: data to be used for this operation
334 * @dst: data produced by this operation
335 * @src_len: length in bytes of data used for this operation
336 * @final: indicate final pass-through operation
337 *
338 * Variables required to be set when calling ccp_enqueue_cmd():
339 * - bit_mod, byte_swap, src, dst, src_len
340 * - mask, mask_len if bit_mod is not CCP_PASSTHRU_BITWISE_NOOP
341 */
342struct ccp_passthru_engine {
343 enum ccp_passthru_bitwise bit_mod;
344 enum ccp_passthru_byteswap byte_swap;
345
346 struct scatterlist *mask;
347 u32 mask_len; /* In bytes */
348
349 struct scatterlist *src, *dst;
350 u64 src_len; /* In bytes */
351
352 u32 final;
353};
354
355/***** ECC engine *****/
356#define CCP_ECC_MODULUS_BYTES 48 /* 384-bits */
357#define CCP_ECC_MAX_OPERANDS 6
358#define CCP_ECC_MAX_OUTPUTS 3
359
360/**
361 * ccp_ecc_function - type of ECC function
362 *
363 * @CCP_ECC_FUNCTION_MMUL_384BIT: 384-bit modular multiplication
364 * @CCP_ECC_FUNCTION_MADD_384BIT: 384-bit modular addition
365 * @CCP_ECC_FUNCTION_MINV_384BIT: 384-bit multiplicative inverse
366 * @CCP_ECC_FUNCTION_PADD_384BIT: 384-bit point addition
367 * @CCP_ECC_FUNCTION_PMUL_384BIT: 384-bit point multiplication
368 * @CCP_ECC_FUNCTION_PDBL_384BIT: 384-bit point doubling
369 */
370enum ccp_ecc_function {
371 CCP_ECC_FUNCTION_MMUL_384BIT = 0,
372 CCP_ECC_FUNCTION_MADD_384BIT,
373 CCP_ECC_FUNCTION_MINV_384BIT,
374 CCP_ECC_FUNCTION_PADD_384BIT,
375 CCP_ECC_FUNCTION_PMUL_384BIT,
376 CCP_ECC_FUNCTION_PDBL_384BIT,
377};
378
379/**
380 * struct ccp_ecc_modular_math - CCP ECC modular math parameters
381 * @operand_1: first operand for the modular math operation
382 * @operand_1_len: length of the first operand
383 * @operand_2: second operand for the modular math operation
384 * (not used for CCP_ECC_FUNCTION_MINV_384BIT)
385 * @operand_2_len: length of the second operand
386 * (not used for CCP_ECC_FUNCTION_MINV_384BIT)
387 * @result: result of the modular math operation
388 * @result_len: length of the supplied result buffer
389 */
390struct ccp_ecc_modular_math {
391 struct scatterlist *operand_1;
392 unsigned int operand_1_len; /* In bytes */
393
394 struct scatterlist *operand_2;
395 unsigned int operand_2_len; /* In bytes */
396
397 struct scatterlist *result;
398 unsigned int result_len; /* In bytes */
399};
400
401/**
402 * struct ccp_ecc_point - CCP ECC point definition
403 * @x: the x coordinate of the ECC point
404 * @x_len: the length of the x coordinate
405 * @y: the y coordinate of the ECC point
406 * @y_len: the length of the y coordinate
407 */
408struct ccp_ecc_point {
409 struct scatterlist *x;
410 unsigned int x_len; /* In bytes */
411
412 struct scatterlist *y;
413 unsigned int y_len; /* In bytes */
414};
415
416/**
417 * struct ccp_ecc_point_math - CCP ECC point math parameters
418 * @point_1: the first point of the ECC point math operation
419 * @point_2: the second point of the ECC point math operation
420 * (only used for CCP_ECC_FUNCTION_PADD_384BIT)
421 * @domain_a: the a parameter of the ECC curve
422 * @domain_a_len: the length of the a parameter
423 * @scalar: the scalar parameter for the point match operation
424 * (only used for CCP_ECC_FUNCTION_PMUL_384BIT)
425 * @scalar_len: the length of the scalar parameter
426 * (only used for CCP_ECC_FUNCTION_PMUL_384BIT)
427 * @result: the point resulting from the point math operation
428 */
429struct ccp_ecc_point_math {
430 struct ccp_ecc_point point_1;
431 struct ccp_ecc_point point_2;
432
433 struct scatterlist *domain_a;
434 unsigned int domain_a_len; /* In bytes */
435
436 struct scatterlist *scalar;
437 unsigned int scalar_len; /* In bytes */
438
439 struct ccp_ecc_point result;
440};
441
442/**
443 * struct ccp_ecc_engine - CCP ECC operation
444 * @function: ECC function to perform
445 * @mod: ECC modulus
446 * @mod_len: length in bytes of modulus
447 * @mm: module math parameters
448 * @pm: point math parameters
449 * @ecc_result: result of the ECC operation
450 *
451 * Variables required to be set when calling ccp_enqueue_cmd():
452 * - function, mod, mod_len
453 * - operand, operand_len, operand_count, output, output_len, output_count
454 * - ecc_result
455 */
456struct ccp_ecc_engine {
457 enum ccp_ecc_function function;
458
459 struct scatterlist *mod;
460 u32 mod_len; /* In bytes */
461
462 union {
463 struct ccp_ecc_modular_math mm;
464 struct ccp_ecc_point_math pm;
465 } u;
466
467 u16 ecc_result;
468};
469
470
471/**
472 * ccp_engine - CCP operation identifiers
473 *
474 * @CCP_ENGINE_AES: AES operation
475 * @CCP_ENGINE_XTS_AES: 128-bit XTS AES operation
476 * @CCP_ENGINE_RSVD1: unused
477 * @CCP_ENGINE_SHA: SHA operation
478 * @CCP_ENGINE_RSA: RSA operation
479 * @CCP_ENGINE_PASSTHRU: pass-through operation
480 * @CCP_ENGINE_ZLIB_DECOMPRESS: unused
481 * @CCP_ENGINE_ECC: ECC operation
482 */
483enum ccp_engine {
484 CCP_ENGINE_AES = 0,
485 CCP_ENGINE_XTS_AES_128,
486 CCP_ENGINE_RSVD1,
487 CCP_ENGINE_SHA,
488 CCP_ENGINE_RSA,
489 CCP_ENGINE_PASSTHRU,
490 CCP_ENGINE_ZLIB_DECOMPRESS,
491 CCP_ENGINE_ECC,
492 CCP_ENGINE__LAST,
493};
494
495/* Flag values for flags member of ccp_cmd */
496#define CCP_CMD_MAY_BACKLOG 0x00000001
497
498/**
499 * struct ccp_cmd - CPP operation request
500 * @entry: list element (ccp driver use only)
501 * @work: work element used for callbacks (ccp driver use only)
502 * @ccp: CCP device to be run on (ccp driver use only)
503 * @ret: operation return code (ccp driver use only)
504 * @flags: cmd processing flags
505 * @engine: CCP operation to perform
506 * @engine_error: CCP engine return code
507 * @u: engine specific structures, refer to specific engine struct below
508 * @callback: operation completion callback function
509 * @data: parameter value to be supplied to the callback function
510 *
511 * Variables required to be set when calling ccp_enqueue_cmd():
512 * - engine, callback
513 * - See the operation structures below for what is required for each
514 * operation.
515 */
516struct ccp_cmd {
517 /* The list_head, work_struct, ccp and ret variables are for use
518 * by the CCP driver only.
519 */
520 struct list_head entry;
521 struct work_struct work;
522 struct ccp_device *ccp;
523 int ret;
524
525 u32 flags;
526
527 enum ccp_engine engine;
528 u32 engine_error;
529
530 union {
531 struct ccp_aes_engine aes;
532 struct ccp_xts_aes_engine xts;
533 struct ccp_sha_engine sha;
534 struct ccp_rsa_engine rsa;
535 struct ccp_passthru_engine passthru;
536 struct ccp_ecc_engine ecc;
537 } u;
538
539 /* Completion callback support */
540 void (*callback)(void *data, int err);
541 void *data;
542};
543
544#endif