xref: /openbmc/linux/drivers/crypto/ccp/ccp-crypto.h (revision d1662165ae612ec8b5f94a6b07e65ea58b6dce34)
1d3123599STom Lendacky /*
2d3123599STom Lendacky  * AMD Cryptographic Coprocessor (CCP) crypto API support
3d3123599STom Lendacky  *
4d3123599STom Lendacky  * Copyright (C) 2013 Advanced Micro Devices, Inc.
5d3123599STom Lendacky  *
6d3123599STom Lendacky  * Author: Tom Lendacky <thomas.lendacky@amd.com>
7d3123599STom Lendacky  *
8d3123599STom Lendacky  * This program is free software; you can redistribute it and/or modify
9d3123599STom Lendacky  * it under the terms of the GNU General Public License version 2 as
10d3123599STom Lendacky  * published by the Free Software Foundation.
11d3123599STom Lendacky  */
12d3123599STom Lendacky 
13d3123599STom Lendacky #ifndef __CCP_CRYPTO_H__
14d3123599STom Lendacky #define __CCP_CRYPTO_H__
15d3123599STom Lendacky 
16d3123599STom Lendacky #include <linux/list.h>
17d3123599STom Lendacky #include <linux/wait.h>
18d3123599STom Lendacky #include <linux/pci.h>
19d3123599STom Lendacky #include <linux/ccp.h>
20d3123599STom Lendacky #include <linux/crypto.h>
21d3123599STom Lendacky #include <crypto/algapi.h>
22d3123599STom Lendacky #include <crypto/aes.h>
23d3123599STom Lendacky #include <crypto/ctr.h>
24d3123599STom Lendacky #include <crypto/hash.h>
25d3123599STom Lendacky #include <crypto/sha.h>
26d3123599STom Lendacky 
27d3123599STom Lendacky #define CCP_CRA_PRIORITY	300
28d3123599STom Lendacky 
29d3123599STom Lendacky struct ccp_crypto_ablkcipher_alg {
30d3123599STom Lendacky 	struct list_head entry;
31d3123599STom Lendacky 
32d3123599STom Lendacky 	u32 mode;
33d3123599STom Lendacky 
34d3123599STom Lendacky 	struct crypto_alg alg;
35d3123599STom Lendacky };
36d3123599STom Lendacky 
37d3123599STom Lendacky struct ccp_crypto_ahash_alg {
38d3123599STom Lendacky 	struct list_head entry;
39d3123599STom Lendacky 
406f0be9b2STom Lendacky 	const __be32 *init;
41d3123599STom Lendacky 	u32 type;
42d3123599STom Lendacky 	u32 mode;
43d3123599STom Lendacky 
44d3123599STom Lendacky 	/* Child algorithm used for HMAC, CMAC, etc */
45d3123599STom Lendacky 	char child_alg[CRYPTO_MAX_ALG_NAME];
46d3123599STom Lendacky 
47d3123599STom Lendacky 	struct ahash_alg alg;
48d3123599STom Lendacky };
49d3123599STom Lendacky 
50d3123599STom Lendacky static inline struct ccp_crypto_ablkcipher_alg *
51d3123599STom Lendacky 	ccp_crypto_ablkcipher_alg(struct crypto_tfm *tfm)
52d3123599STom Lendacky {
53d3123599STom Lendacky 	struct crypto_alg *alg = tfm->__crt_alg;
54d3123599STom Lendacky 
55d3123599STom Lendacky 	return container_of(alg, struct ccp_crypto_ablkcipher_alg, alg);
56d3123599STom Lendacky }
57d3123599STom Lendacky 
58d3123599STom Lendacky static inline struct ccp_crypto_ahash_alg *
59d3123599STom Lendacky 	ccp_crypto_ahash_alg(struct crypto_tfm *tfm)
60d3123599STom Lendacky {
61d3123599STom Lendacky 	struct crypto_alg *alg = tfm->__crt_alg;
62d3123599STom Lendacky 	struct ahash_alg *ahash_alg;
63d3123599STom Lendacky 
64d3123599STom Lendacky 	ahash_alg = container_of(alg, struct ahash_alg, halg.base);
65d3123599STom Lendacky 
66d3123599STom Lendacky 	return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg);
67d3123599STom Lendacky }
68d3123599STom Lendacky 
69d3123599STom Lendacky /***** AES related defines *****/
70d3123599STom Lendacky struct ccp_aes_ctx {
71d3123599STom Lendacky 	/* Fallback cipher for XTS with unsupported unit sizes */
72d3123599STom Lendacky 	struct crypto_ablkcipher *tfm_ablkcipher;
73d3123599STom Lendacky 
74d3123599STom Lendacky 	/* Cipher used to generate CMAC K1/K2 keys */
75d3123599STom Lendacky 	struct crypto_cipher *tfm_cipher;
76d3123599STom Lendacky 
77d3123599STom Lendacky 	enum ccp_engine engine;
78d3123599STom Lendacky 	enum ccp_aes_type type;
79d3123599STom Lendacky 	enum ccp_aes_mode mode;
80d3123599STom Lendacky 
81d3123599STom Lendacky 	struct scatterlist key_sg;
82d3123599STom Lendacky 	unsigned int key_len;
83d3123599STom Lendacky 	u8 key[AES_MAX_KEY_SIZE];
84d3123599STom Lendacky 
85d3123599STom Lendacky 	u8 nonce[CTR_RFC3686_NONCE_SIZE];
86d3123599STom Lendacky 
87d3123599STom Lendacky 	/* CMAC key structures */
88d3123599STom Lendacky 	struct scatterlist k1_sg;
89d3123599STom Lendacky 	struct scatterlist k2_sg;
90d3123599STom Lendacky 	unsigned int kn_len;
91d3123599STom Lendacky 	u8 k1[AES_BLOCK_SIZE];
92d3123599STom Lendacky 	u8 k2[AES_BLOCK_SIZE];
93d3123599STom Lendacky };
94d3123599STom Lendacky 
95d3123599STom Lendacky struct ccp_aes_req_ctx {
96d3123599STom Lendacky 	struct scatterlist iv_sg;
97d3123599STom Lendacky 	u8 iv[AES_BLOCK_SIZE];
98d3123599STom Lendacky 
99d3123599STom Lendacky 	/* Fields used for RFC3686 requests */
100d3123599STom Lendacky 	u8 *rfc3686_info;
101d3123599STom Lendacky 	u8 rfc3686_iv[AES_BLOCK_SIZE];
102d3123599STom Lendacky 
103d3123599STom Lendacky 	struct ccp_cmd cmd;
104d3123599STom Lendacky };
105d3123599STom Lendacky 
106d3123599STom Lendacky struct ccp_aes_cmac_req_ctx {
107d3123599STom Lendacky 	unsigned int null_msg;
108d3123599STom Lendacky 	unsigned int final;
109d3123599STom Lendacky 
11081a59f00STom Lendacky 	struct scatterlist *src;
11181a59f00STom Lendacky 	unsigned int nbytes;
11281a59f00STom Lendacky 
11381a59f00STom Lendacky 	u64 hash_cnt;
114d3123599STom Lendacky 	unsigned int hash_rem;
115d3123599STom Lendacky 
116d3123599STom Lendacky 	struct sg_table data_sg;
117d3123599STom Lendacky 
118d3123599STom Lendacky 	struct scatterlist iv_sg;
119d3123599STom Lendacky 	u8 iv[AES_BLOCK_SIZE];
120d3123599STom Lendacky 
121d3123599STom Lendacky 	struct scatterlist buf_sg;
122d3123599STom Lendacky 	unsigned int buf_count;
123d3123599STom Lendacky 	u8 buf[AES_BLOCK_SIZE];
124d3123599STom Lendacky 
125d3123599STom Lendacky 	struct scatterlist pad_sg;
126d3123599STom Lendacky 	unsigned int pad_count;
127d3123599STom Lendacky 	u8 pad[AES_BLOCK_SIZE];
128d3123599STom Lendacky 
129d3123599STom Lendacky 	struct ccp_cmd cmd;
130d3123599STom Lendacky };
131d3123599STom Lendacky 
132*d1662165STom Lendacky struct ccp_aes_cmac_exp_ctx {
133*d1662165STom Lendacky 	unsigned int null_msg;
134*d1662165STom Lendacky 
135*d1662165STom Lendacky 	u8 iv[AES_BLOCK_SIZE];
136*d1662165STom Lendacky 
137*d1662165STom Lendacky 	unsigned int buf_count;
138*d1662165STom Lendacky 	u8 buf[AES_BLOCK_SIZE];
139*d1662165STom Lendacky };
140*d1662165STom Lendacky 
141d3123599STom Lendacky /***** SHA related defines *****/
142d3123599STom Lendacky #define MAX_SHA_CONTEXT_SIZE	SHA256_DIGEST_SIZE
143d3123599STom Lendacky #define MAX_SHA_BLOCK_SIZE	SHA256_BLOCK_SIZE
144d3123599STom Lendacky 
145d3123599STom Lendacky struct ccp_sha_ctx {
146c11baa02STom Lendacky 	struct scatterlist opad_sg;
147c11baa02STom Lendacky 	unsigned int opad_count;
148c11baa02STom Lendacky 
149d3123599STom Lendacky 	unsigned int key_len;
150d3123599STom Lendacky 	u8 key[MAX_SHA_BLOCK_SIZE];
151d3123599STom Lendacky 	u8 ipad[MAX_SHA_BLOCK_SIZE];
152d3123599STom Lendacky 	u8 opad[MAX_SHA_BLOCK_SIZE];
153c11baa02STom Lendacky 	struct crypto_shash *hmac_tfm;
154d3123599STom Lendacky };
155d3123599STom Lendacky 
156d3123599STom Lendacky struct ccp_sha_req_ctx {
157d3123599STom Lendacky 	enum ccp_sha_type type;
158d3123599STom Lendacky 
159d3123599STom Lendacky 	u64 msg_bits;
160d3123599STom Lendacky 
161d3123599STom Lendacky 	unsigned int first;
162d3123599STom Lendacky 	unsigned int final;
163d3123599STom Lendacky 
16481a59f00STom Lendacky 	struct scatterlist *src;
16581a59f00STom Lendacky 	unsigned int nbytes;
16681a59f00STom Lendacky 
16781a59f00STom Lendacky 	u64 hash_cnt;
168d3123599STom Lendacky 	unsigned int hash_rem;
169d3123599STom Lendacky 
170d3123599STom Lendacky 	struct sg_table data_sg;
171d3123599STom Lendacky 
172d3123599STom Lendacky 	struct scatterlist ctx_sg;
173d3123599STom Lendacky 	u8 ctx[MAX_SHA_CONTEXT_SIZE];
174d3123599STom Lendacky 
175d3123599STom Lendacky 	struct scatterlist buf_sg;
176d3123599STom Lendacky 	unsigned int buf_count;
177d3123599STom Lendacky 	u8 buf[MAX_SHA_BLOCK_SIZE];
178d3123599STom Lendacky 
179d3123599STom Lendacky 	/* CCP driver command */
180d3123599STom Lendacky 	struct ccp_cmd cmd;
181d3123599STom Lendacky };
182d3123599STom Lendacky 
183*d1662165STom Lendacky struct ccp_sha_exp_ctx {
184*d1662165STom Lendacky 	enum ccp_sha_type type;
185*d1662165STom Lendacky 
186*d1662165STom Lendacky 	u64 msg_bits;
187*d1662165STom Lendacky 
188*d1662165STom Lendacky 	unsigned int first;
189*d1662165STom Lendacky 
190*d1662165STom Lendacky 	u8 ctx[MAX_SHA_CONTEXT_SIZE];
191*d1662165STom Lendacky 
192*d1662165STom Lendacky 	unsigned int buf_count;
193*d1662165STom Lendacky 	u8 buf[MAX_SHA_BLOCK_SIZE];
194*d1662165STom Lendacky };
195*d1662165STom Lendacky 
196d3123599STom Lendacky /***** Common Context Structure *****/
197d3123599STom Lendacky struct ccp_ctx {
198d3123599STom Lendacky 	int (*complete)(struct crypto_async_request *req, int ret);
199d3123599STom Lendacky 
200d3123599STom Lendacky 	union {
201d3123599STom Lendacky 		struct ccp_aes_ctx aes;
202d3123599STom Lendacky 		struct ccp_sha_ctx sha;
203d3123599STom Lendacky 	} u;
204d3123599STom Lendacky };
205d3123599STom Lendacky 
206d3123599STom Lendacky int ccp_crypto_enqueue_request(struct crypto_async_request *req,
207d3123599STom Lendacky 			       struct ccp_cmd *cmd);
208d3123599STom Lendacky struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table,
209d3123599STom Lendacky 					    struct scatterlist *sg_add);
210d3123599STom Lendacky 
211d3123599STom Lendacky int ccp_register_aes_algs(struct list_head *head);
212d3123599STom Lendacky int ccp_register_aes_cmac_algs(struct list_head *head);
213d3123599STom Lendacky int ccp_register_aes_xts_algs(struct list_head *head);
214d3123599STom Lendacky int ccp_register_sha_algs(struct list_head *head);
215d3123599STom Lendacky 
216d3123599STom Lendacky #endif
217