2 * AMD Cryptographic Coprocessor (CCP) crypto API support
4 * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
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.
13 #ifndef __CCP_CRYPTO_H__
14 #define __CCP_CRYPTO_H__
16 #include <linux/list.h>
17 #include <linux/wait.h>
18 #include <linux/pci.h>
19 #include <linux/ccp.h>
20 #include <crypto/algapi.h>
21 #include <crypto/aes.h>
22 #include <crypto/internal/aead.h>
23 #include <crypto/aead.h>
24 #include <crypto/ctr.h>
25 #include <crypto/hash.h>
26 #include <crypto/sha.h>
27 #include <crypto/akcipher.h>
28 #include <crypto/internal/rsa.h>
30 #define CCP_LOG_LEVEL KERN_INFO
32 #define CCP_CRA_PRIORITY 300
34 struct ccp_crypto_ablkcipher_alg
{
35 struct list_head entry
;
39 struct crypto_alg alg
;
42 struct ccp_crypto_aead
{
43 struct list_head entry
;
50 struct ccp_crypto_ahash_alg
{
51 struct list_head entry
;
57 /* Child algorithm used for HMAC, CMAC, etc */
58 char child_alg
[CRYPTO_MAX_ALG_NAME
];
63 struct ccp_crypto_akcipher_alg
{
64 struct list_head entry
;
66 struct akcipher_alg alg
;
69 static inline struct ccp_crypto_ablkcipher_alg
*
70 ccp_crypto_ablkcipher_alg(struct crypto_tfm
*tfm
)
72 struct crypto_alg
*alg
= tfm
->__crt_alg
;
74 return container_of(alg
, struct ccp_crypto_ablkcipher_alg
, alg
);
77 static inline struct ccp_crypto_ahash_alg
*
78 ccp_crypto_ahash_alg(struct crypto_tfm
*tfm
)
80 struct crypto_alg
*alg
= tfm
->__crt_alg
;
81 struct ahash_alg
*ahash_alg
;
83 ahash_alg
= container_of(alg
, struct ahash_alg
, halg
.base
);
85 return container_of(ahash_alg
, struct ccp_crypto_ahash_alg
, alg
);
88 /***** AES related defines *****/
90 /* Fallback cipher for XTS with unsupported unit sizes */
91 struct crypto_skcipher
*tfm_skcipher
;
93 /* Cipher used to generate CMAC K1/K2 keys */
94 struct crypto_cipher
*tfm_cipher
;
96 enum ccp_engine engine
;
97 enum ccp_aes_type type
;
98 enum ccp_aes_mode mode
;
100 struct scatterlist key_sg
;
101 unsigned int key_len
;
102 u8 key
[AES_MAX_KEY_SIZE
* 2];
104 u8 nonce
[CTR_RFC3686_NONCE_SIZE
];
106 /* CMAC key structures */
107 struct scatterlist k1_sg
;
108 struct scatterlist k2_sg
;
110 u8 k1
[AES_BLOCK_SIZE
];
111 u8 k2
[AES_BLOCK_SIZE
];
114 struct ccp_aes_req_ctx
{
115 struct scatterlist iv_sg
;
116 u8 iv
[AES_BLOCK_SIZE
];
118 struct scatterlist tag_sg
;
119 u8 tag
[AES_BLOCK_SIZE
];
121 /* Fields used for RFC3686 requests */
123 u8 rfc3686_iv
[AES_BLOCK_SIZE
];
128 struct ccp_aes_cmac_req_ctx
{
129 unsigned int null_msg
;
132 struct scatterlist
*src
;
136 unsigned int hash_rem
;
138 struct sg_table data_sg
;
140 struct scatterlist iv_sg
;
141 u8 iv
[AES_BLOCK_SIZE
];
143 struct scatterlist buf_sg
;
144 unsigned int buf_count
;
145 u8 buf
[AES_BLOCK_SIZE
];
147 struct scatterlist pad_sg
;
148 unsigned int pad_count
;
149 u8 pad
[AES_BLOCK_SIZE
];
154 struct ccp_aes_cmac_exp_ctx
{
155 unsigned int null_msg
;
157 u8 iv
[AES_BLOCK_SIZE
];
159 unsigned int buf_count
;
160 u8 buf
[AES_BLOCK_SIZE
];
163 /***** 3DES related defines *****/
164 struct ccp_des3_ctx
{
165 enum ccp_engine engine
;
166 enum ccp_des3_type type
;
167 enum ccp_des3_mode mode
;
169 struct scatterlist key_sg
;
170 unsigned int key_len
;
171 u8 key
[AES_MAX_KEY_SIZE
];
174 struct ccp_des3_req_ctx
{
175 struct scatterlist iv_sg
;
176 u8 iv
[AES_BLOCK_SIZE
];
181 /* SHA-related defines
182 * These values must be large enough to accommodate any variant
184 #define MAX_SHA_CONTEXT_SIZE SHA512_DIGEST_SIZE
185 #define MAX_SHA_BLOCK_SIZE SHA512_BLOCK_SIZE
188 struct scatterlist opad_sg
;
189 unsigned int opad_count
;
191 unsigned int key_len
;
192 u8 key
[MAX_SHA_BLOCK_SIZE
];
193 u8 ipad
[MAX_SHA_BLOCK_SIZE
];
194 u8 opad
[MAX_SHA_BLOCK_SIZE
];
195 struct crypto_shash
*hmac_tfm
;
198 struct ccp_sha_req_ctx
{
199 enum ccp_sha_type type
;
206 struct scatterlist
*src
;
210 unsigned int hash_rem
;
212 struct sg_table data_sg
;
214 struct scatterlist ctx_sg
;
215 u8 ctx
[MAX_SHA_CONTEXT_SIZE
];
217 struct scatterlist buf_sg
;
218 unsigned int buf_count
;
219 u8 buf
[MAX_SHA_BLOCK_SIZE
];
221 /* CCP driver command */
225 struct ccp_sha_exp_ctx
{
226 enum ccp_sha_type type
;
232 u8 ctx
[MAX_SHA_CONTEXT_SIZE
];
234 unsigned int buf_count
;
235 u8 buf
[MAX_SHA_BLOCK_SIZE
];
238 /***** RSA related defines *****/
241 unsigned int key_len
; /* in bits */
242 struct scatterlist e_sg
;
245 struct scatterlist n_sg
;
248 struct scatterlist d_sg
;
253 struct ccp_rsa_req_ctx
{
257 #define CCP_RSA_MAXMOD (4 * 1024 / 8)
258 #define CCP5_RSA_MAXMOD (16 * 1024 / 8)
260 /***** Common Context Structure *****/
262 int (*complete
)(struct crypto_async_request
*req
, int ret
);
265 struct ccp_aes_ctx aes
;
266 struct ccp_rsa_ctx rsa
;
267 struct ccp_sha_ctx sha
;
268 struct ccp_des3_ctx des3
;
272 int ccp_crypto_enqueue_request(struct crypto_async_request
*req
,
273 struct ccp_cmd
*cmd
);
274 struct scatterlist
*ccp_crypto_sg_table_add(struct sg_table
*table
,
275 struct scatterlist
*sg_add
);
277 int ccp_register_aes_algs(struct list_head
*head
);
278 int ccp_register_aes_cmac_algs(struct list_head
*head
);
279 int ccp_register_aes_xts_algs(struct list_head
*head
);
280 int ccp_register_aes_aeads(struct list_head
*head
);
281 int ccp_register_sha_algs(struct list_head
*head
);
282 int ccp_register_des3_algs(struct list_head
*head
);
283 int ccp_register_rsa_algs(struct list_head
*head
);