treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / crypto / ccp / ccp-crypto.h
blob90a009e6b5c138763bd0af3601319e2c7939e4cd
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * AMD Cryptographic Coprocessor (CCP) crypto API support
5 * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
7 * Author: Tom Lendacky <thomas.lendacky@amd.com>
8 */
10 #ifndef __CCP_CRYPTO_H__
11 #define __CCP_CRYPTO_H__
13 #include <linux/list.h>
14 #include <linux/wait.h>
15 #include <linux/ccp.h>
16 #include <crypto/algapi.h>
17 #include <crypto/aes.h>
18 #include <crypto/internal/aead.h>
19 #include <crypto/aead.h>
20 #include <crypto/ctr.h>
21 #include <crypto/hash.h>
22 #include <crypto/sha.h>
23 #include <crypto/akcipher.h>
24 #include <crypto/skcipher.h>
25 #include <crypto/internal/rsa.h>
27 /* We want the module name in front of our messages */
28 #undef pr_fmt
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #define CCP_LOG_LEVEL KERN_INFO
33 #define CCP_CRA_PRIORITY 300
35 struct ccp_crypto_skcipher_alg {
36 struct list_head entry;
38 u32 mode;
40 struct skcipher_alg alg;
43 struct ccp_crypto_aead {
44 struct list_head entry;
46 u32 mode;
48 struct aead_alg alg;
51 struct ccp_crypto_ahash_alg {
52 struct list_head entry;
54 const __be32 *init;
55 u32 type;
56 u32 mode;
58 /* Child algorithm used for HMAC, CMAC, etc */
59 char child_alg[CRYPTO_MAX_ALG_NAME];
61 struct ahash_alg alg;
64 struct ccp_crypto_akcipher_alg {
65 struct list_head entry;
67 struct akcipher_alg alg;
70 static inline struct ccp_crypto_skcipher_alg *
71 ccp_crypto_skcipher_alg(struct crypto_skcipher *tfm)
73 struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
75 return container_of(alg, struct ccp_crypto_skcipher_alg, alg);
78 static inline struct ccp_crypto_ahash_alg *
79 ccp_crypto_ahash_alg(struct crypto_tfm *tfm)
81 struct crypto_alg *alg = tfm->__crt_alg;
82 struct ahash_alg *ahash_alg;
84 ahash_alg = container_of(alg, struct ahash_alg, halg.base);
86 return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg);
89 /***** AES related defines *****/
90 struct ccp_aes_ctx {
91 /* Fallback cipher for XTS with unsupported unit sizes */
92 struct crypto_sync_skcipher *tfm_skcipher;
94 enum ccp_engine engine;
95 enum ccp_aes_type type;
96 enum ccp_aes_mode mode;
98 struct scatterlist key_sg;
99 unsigned int key_len;
100 u8 key[AES_MAX_KEY_SIZE * 2];
102 u8 nonce[CTR_RFC3686_NONCE_SIZE];
104 /* CMAC key structures */
105 struct scatterlist k1_sg;
106 struct scatterlist k2_sg;
107 unsigned int kn_len;
108 u8 k1[AES_BLOCK_SIZE];
109 u8 k2[AES_BLOCK_SIZE];
112 struct ccp_aes_req_ctx {
113 struct scatterlist iv_sg;
114 u8 iv[AES_BLOCK_SIZE];
116 struct scatterlist tag_sg;
117 u8 tag[AES_BLOCK_SIZE];
119 /* Fields used for RFC3686 requests */
120 u8 *rfc3686_info;
121 u8 rfc3686_iv[AES_BLOCK_SIZE];
123 struct ccp_cmd cmd;
126 struct ccp_aes_cmac_req_ctx {
127 unsigned int null_msg;
128 unsigned int final;
130 struct scatterlist *src;
131 unsigned int nbytes;
133 u64 hash_cnt;
134 unsigned int hash_rem;
136 struct sg_table data_sg;
138 struct scatterlist iv_sg;
139 u8 iv[AES_BLOCK_SIZE];
141 struct scatterlist buf_sg;
142 unsigned int buf_count;
143 u8 buf[AES_BLOCK_SIZE];
145 struct scatterlist pad_sg;
146 unsigned int pad_count;
147 u8 pad[AES_BLOCK_SIZE];
149 struct ccp_cmd cmd;
152 struct ccp_aes_cmac_exp_ctx {
153 unsigned int null_msg;
155 u8 iv[AES_BLOCK_SIZE];
157 unsigned int buf_count;
158 u8 buf[AES_BLOCK_SIZE];
161 /***** 3DES related defines *****/
162 struct ccp_des3_ctx {
163 enum ccp_engine engine;
164 enum ccp_des3_type type;
165 enum ccp_des3_mode mode;
167 struct scatterlist key_sg;
168 unsigned int key_len;
169 u8 key[AES_MAX_KEY_SIZE];
172 struct ccp_des3_req_ctx {
173 struct scatterlist iv_sg;
174 u8 iv[AES_BLOCK_SIZE];
176 struct ccp_cmd cmd;
179 /* SHA-related defines
180 * These values must be large enough to accommodate any variant
182 #define MAX_SHA_CONTEXT_SIZE SHA512_DIGEST_SIZE
183 #define MAX_SHA_BLOCK_SIZE SHA512_BLOCK_SIZE
185 struct ccp_sha_ctx {
186 struct scatterlist opad_sg;
187 unsigned int opad_count;
189 unsigned int key_len;
190 u8 key[MAX_SHA_BLOCK_SIZE];
191 u8 ipad[MAX_SHA_BLOCK_SIZE];
192 u8 opad[MAX_SHA_BLOCK_SIZE];
193 struct crypto_shash *hmac_tfm;
196 struct ccp_sha_req_ctx {
197 enum ccp_sha_type type;
199 u64 msg_bits;
201 unsigned int first;
202 unsigned int final;
204 struct scatterlist *src;
205 unsigned int nbytes;
207 u64 hash_cnt;
208 unsigned int hash_rem;
210 struct sg_table data_sg;
212 struct scatterlist ctx_sg;
213 u8 ctx[MAX_SHA_CONTEXT_SIZE];
215 struct scatterlist buf_sg;
216 unsigned int buf_count;
217 u8 buf[MAX_SHA_BLOCK_SIZE];
219 /* CCP driver command */
220 struct ccp_cmd cmd;
223 struct ccp_sha_exp_ctx {
224 enum ccp_sha_type type;
226 u64 msg_bits;
228 unsigned int first;
230 u8 ctx[MAX_SHA_CONTEXT_SIZE];
232 unsigned int buf_count;
233 u8 buf[MAX_SHA_BLOCK_SIZE];
236 /***** RSA related defines *****/
238 struct ccp_rsa_ctx {
239 unsigned int key_len; /* in bits */
240 struct scatterlist e_sg;
241 u8 *e_buf;
242 unsigned int e_len;
243 struct scatterlist n_sg;
244 u8 *n_buf;
245 unsigned int n_len;
246 struct scatterlist d_sg;
247 u8 *d_buf;
248 unsigned int d_len;
251 struct ccp_rsa_req_ctx {
252 struct ccp_cmd cmd;
255 #define CCP_RSA_MAXMOD (4 * 1024 / 8)
256 #define CCP5_RSA_MAXMOD (16 * 1024 / 8)
258 /***** Common Context Structure *****/
259 struct ccp_ctx {
260 int (*complete)(struct crypto_async_request *req, int ret);
262 union {
263 struct ccp_aes_ctx aes;
264 struct ccp_rsa_ctx rsa;
265 struct ccp_sha_ctx sha;
266 struct ccp_des3_ctx des3;
267 } u;
270 int ccp_crypto_enqueue_request(struct crypto_async_request *req,
271 struct ccp_cmd *cmd);
272 struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table,
273 struct scatterlist *sg_add);
275 int ccp_register_aes_algs(struct list_head *head);
276 int ccp_register_aes_cmac_algs(struct list_head *head);
277 int ccp_register_aes_xts_algs(struct list_head *head);
278 int ccp_register_aes_aeads(struct list_head *head);
279 int ccp_register_sha_algs(struct list_head *head);
280 int ccp_register_des3_algs(struct list_head *head);
281 int ccp_register_rsa_algs(struct list_head *head);
283 #endif