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