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