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