]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/crypto/ccp/ccp-crypto.h
crypto: ccp - crypto API interface to the CCP device driver
[karo-tx-linux.git] / drivers / crypto / ccp / ccp-crypto.h
1 /*
2  * AMD Cryptographic Coprocessor (CCP) crypto API support
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 __CCP_CRYPTO_H__
14 #define __CCP_CRYPTO_H__
15
16
17 #include <linux/list.h>
18 #include <linux/wait.h>
19 #include <linux/pci.h>
20 #include <linux/ccp.h>
21 #include <linux/crypto.h>
22 #include <crypto/algapi.h>
23 #include <crypto/aes.h>
24 #include <crypto/ctr.h>
25 #include <crypto/hash.h>
26 #include <crypto/sha.h>
27
28
29 #define CCP_CRA_PRIORITY        300
30
31 struct ccp_crypto_ablkcipher_alg {
32         struct list_head entry;
33
34         u32 mode;
35
36         struct crypto_alg alg;
37 };
38
39 struct ccp_crypto_ahash_alg {
40         struct list_head entry;
41
42         const u32 *init;
43         u32 type;
44         u32 mode;
45
46         /* Child algorithm used for HMAC, CMAC, etc */
47         char child_alg[CRYPTO_MAX_ALG_NAME];
48
49         struct ahash_alg alg;
50 };
51
52 static inline struct ccp_crypto_ablkcipher_alg *
53         ccp_crypto_ablkcipher_alg(struct crypto_tfm *tfm)
54 {
55         struct crypto_alg *alg = tfm->__crt_alg;
56
57         return container_of(alg, struct ccp_crypto_ablkcipher_alg, alg);
58 }
59
60 static inline struct ccp_crypto_ahash_alg *
61         ccp_crypto_ahash_alg(struct crypto_tfm *tfm)
62 {
63         struct crypto_alg *alg = tfm->__crt_alg;
64         struct ahash_alg *ahash_alg;
65
66         ahash_alg = container_of(alg, struct ahash_alg, halg.base);
67
68         return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg);
69 }
70
71
72 /***** AES related defines *****/
73 struct ccp_aes_ctx {
74         /* Fallback cipher for XTS with unsupported unit sizes */
75         struct crypto_ablkcipher *tfm_ablkcipher;
76
77         /* Cipher used to generate CMAC K1/K2 keys */
78         struct crypto_cipher *tfm_cipher;
79
80         enum ccp_engine engine;
81         enum ccp_aes_type type;
82         enum ccp_aes_mode mode;
83
84         struct scatterlist key_sg;
85         unsigned int key_len;
86         u8 key[AES_MAX_KEY_SIZE];
87
88         u8 nonce[CTR_RFC3686_NONCE_SIZE];
89
90         /* CMAC key structures */
91         struct scatterlist k1_sg;
92         struct scatterlist k2_sg;
93         unsigned int kn_len;
94         u8 k1[AES_BLOCK_SIZE];
95         u8 k2[AES_BLOCK_SIZE];
96 };
97
98 struct ccp_aes_req_ctx {
99         struct scatterlist iv_sg;
100         u8 iv[AES_BLOCK_SIZE];
101
102         /* Fields used for RFC3686 requests */
103         u8 *rfc3686_info;
104         u8 rfc3686_iv[AES_BLOCK_SIZE];
105
106         struct ccp_cmd cmd;
107 };
108
109 struct ccp_aes_cmac_req_ctx {
110         unsigned int null_msg;
111         unsigned int final;
112
113         unsigned int hash_cnt;
114         unsigned int hash_rem;
115
116         struct sg_table data_sg;
117
118         struct scatterlist iv_sg;
119         u8 iv[AES_BLOCK_SIZE];
120
121         struct scatterlist buf_sg;
122         unsigned int buf_count;
123         u8 buf[AES_BLOCK_SIZE];
124
125         struct scatterlist pad_sg;
126         unsigned int pad_count;
127         u8 pad[AES_BLOCK_SIZE];
128
129         struct ccp_cmd cmd;
130 };
131
132 /***** SHA related defines *****/
133 #define MAX_SHA_CONTEXT_SIZE    SHA256_DIGEST_SIZE
134 #define MAX_SHA_BLOCK_SIZE      SHA256_BLOCK_SIZE
135
136 struct ccp_sha_ctx {
137         unsigned int key_len;
138         u8 key[MAX_SHA_BLOCK_SIZE];
139         u8 ipad[MAX_SHA_BLOCK_SIZE];
140         u8 opad[MAX_SHA_BLOCK_SIZE];
141         struct crypto_ahash *hmac_tfm;
142 };
143
144 struct ccp_sha_req_ctx {
145         enum ccp_sha_type type;
146
147         u64 msg_bits;
148
149         unsigned int first;
150         unsigned int final;
151
152         unsigned int hash_cnt;
153         unsigned int hash_rem;
154
155         struct sg_table data_sg;
156
157         struct scatterlist ctx_sg;
158         u8 ctx[MAX_SHA_CONTEXT_SIZE];
159
160         struct scatterlist buf_sg;
161         unsigned int buf_count;
162         u8 buf[MAX_SHA_BLOCK_SIZE];
163
164         /* HMAC support field */
165         struct scatterlist pad_sg;
166
167         /* CCP driver command */
168         struct ccp_cmd cmd;
169 };
170
171 /***** Common Context Structure *****/
172 struct ccp_ctx {
173         int (*complete)(struct crypto_async_request *req, int ret);
174
175         union {
176                 struct ccp_aes_ctx aes;
177                 struct ccp_sha_ctx sha;
178         } u;
179 };
180
181 int ccp_crypto_enqueue_request(struct crypto_async_request *req,
182                                struct ccp_cmd *cmd);
183 struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table,
184                                             struct scatterlist *sg_add);
185
186 int ccp_register_aes_algs(struct list_head *head);
187 int ccp_register_aes_cmac_algs(struct list_head *head);
188 int ccp_register_aes_xts_algs(struct list_head *head);
189 int ccp_register_sha_algs(struct list_head *head);
190
191 #endif