xref: /openbmc/linux/drivers/crypto/ccp/ccp-crypto.h (revision e8f6f3b4)
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 __be32 *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 	struct scatterlist *src;
114 	unsigned int nbytes;
115 
116 	u64 hash_cnt;
117 	unsigned int hash_rem;
118 
119 	struct sg_table data_sg;
120 
121 	struct scatterlist iv_sg;
122 	u8 iv[AES_BLOCK_SIZE];
123 
124 	struct scatterlist buf_sg;
125 	unsigned int buf_count;
126 	u8 buf[AES_BLOCK_SIZE];
127 
128 	struct scatterlist pad_sg;
129 	unsigned int pad_count;
130 	u8 pad[AES_BLOCK_SIZE];
131 
132 	struct ccp_cmd cmd;
133 };
134 
135 /***** SHA related defines *****/
136 #define MAX_SHA_CONTEXT_SIZE	SHA256_DIGEST_SIZE
137 #define MAX_SHA_BLOCK_SIZE	SHA256_BLOCK_SIZE
138 
139 struct ccp_sha_ctx {
140 	struct scatterlist opad_sg;
141 	unsigned int opad_count;
142 
143 	unsigned int key_len;
144 	u8 key[MAX_SHA_BLOCK_SIZE];
145 	u8 ipad[MAX_SHA_BLOCK_SIZE];
146 	u8 opad[MAX_SHA_BLOCK_SIZE];
147 	struct crypto_shash *hmac_tfm;
148 };
149 
150 struct ccp_sha_req_ctx {
151 	enum ccp_sha_type type;
152 
153 	u64 msg_bits;
154 
155 	unsigned int first;
156 	unsigned int final;
157 
158 	struct scatterlist *src;
159 	unsigned int nbytes;
160 
161 	u64 hash_cnt;
162 	unsigned int hash_rem;
163 
164 	struct sg_table data_sg;
165 
166 	struct scatterlist ctx_sg;
167 	u8 ctx[MAX_SHA_CONTEXT_SIZE];
168 
169 	struct scatterlist buf_sg;
170 	unsigned int buf_count;
171 	u8 buf[MAX_SHA_BLOCK_SIZE];
172 
173 	/* CCP driver command */
174 	struct ccp_cmd cmd;
175 };
176 
177 /***** Common Context Structure *****/
178 struct ccp_ctx {
179 	int (*complete)(struct crypto_async_request *req, int ret);
180 
181 	union {
182 		struct ccp_aes_ctx aes;
183 		struct ccp_sha_ctx sha;
184 	} u;
185 };
186 
187 int ccp_crypto_enqueue_request(struct crypto_async_request *req,
188 			       struct ccp_cmd *cmd);
189 struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table,
190 					    struct scatterlist *sg_add);
191 
192 int ccp_register_aes_algs(struct list_head *head);
193 int ccp_register_aes_cmac_algs(struct list_head *head);
194 int ccp_register_aes_xts_algs(struct list_head *head);
195 int ccp_register_sha_algs(struct list_head *head);
196 
197 #endif
198