xref: /openbmc/linux/drivers/crypto/ccp/ccp-crypto.h (revision ccebcf3f224a44ec8e9c5bfca9d8e5d29298a5a8)
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 <crypto/algapi.h>
21d3123599STom Lendacky #include <crypto/aes.h>
22d3123599STom Lendacky #include <crypto/ctr.h>
23d3123599STom Lendacky #include <crypto/hash.h>
24d3123599STom Lendacky #include <crypto/sha.h>
25d3123599STom Lendacky 
26d3123599STom Lendacky #define CCP_CRA_PRIORITY	300
27d3123599STom Lendacky 
28d3123599STom Lendacky struct ccp_crypto_ablkcipher_alg {
29d3123599STom Lendacky 	struct list_head entry;
30d3123599STom Lendacky 
31d3123599STom Lendacky 	u32 mode;
32d3123599STom Lendacky 
33d3123599STom Lendacky 	struct crypto_alg alg;
34d3123599STom Lendacky };
35d3123599STom Lendacky 
36d3123599STom Lendacky struct ccp_crypto_ahash_alg {
37d3123599STom Lendacky 	struct list_head entry;
38d3123599STom Lendacky 
396f0be9b2STom Lendacky 	const __be32 *init;
40d3123599STom Lendacky 	u32 type;
41d3123599STom Lendacky 	u32 mode;
42d3123599STom Lendacky 
43d3123599STom Lendacky 	/* Child algorithm used for HMAC, CMAC, etc */
44d3123599STom Lendacky 	char child_alg[CRYPTO_MAX_ALG_NAME];
45d3123599STom Lendacky 
46d3123599STom Lendacky 	struct ahash_alg alg;
47d3123599STom Lendacky };
48d3123599STom Lendacky 
49d3123599STom Lendacky static inline struct ccp_crypto_ablkcipher_alg *
50d3123599STom Lendacky 	ccp_crypto_ablkcipher_alg(struct crypto_tfm *tfm)
51d3123599STom Lendacky {
52d3123599STom Lendacky 	struct crypto_alg *alg = tfm->__crt_alg;
53d3123599STom Lendacky 
54d3123599STom Lendacky 	return container_of(alg, struct ccp_crypto_ablkcipher_alg, alg);
55d3123599STom Lendacky }
56d3123599STom Lendacky 
57d3123599STom Lendacky static inline struct ccp_crypto_ahash_alg *
58d3123599STom Lendacky 	ccp_crypto_ahash_alg(struct crypto_tfm *tfm)
59d3123599STom Lendacky {
60d3123599STom Lendacky 	struct crypto_alg *alg = tfm->__crt_alg;
61d3123599STom Lendacky 	struct ahash_alg *ahash_alg;
62d3123599STom Lendacky 
63d3123599STom Lendacky 	ahash_alg = container_of(alg, struct ahash_alg, halg.base);
64d3123599STom Lendacky 
65d3123599STom Lendacky 	return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg);
66d3123599STom Lendacky }
67d3123599STom Lendacky 
68d3123599STom Lendacky /***** AES related defines *****/
69d3123599STom Lendacky struct ccp_aes_ctx {
70d3123599STom Lendacky 	/* Fallback cipher for XTS with unsupported unit sizes */
71241118deSHerbert Xu 	struct crypto_skcipher *tfm_skcipher;
72d3123599STom Lendacky 
73d3123599STom Lendacky 	/* Cipher used to generate CMAC K1/K2 keys */
74d3123599STom Lendacky 	struct crypto_cipher *tfm_cipher;
75d3123599STom Lendacky 
76d3123599STom Lendacky 	enum ccp_engine engine;
77d3123599STom Lendacky 	enum ccp_aes_type type;
78d3123599STom Lendacky 	enum ccp_aes_mode mode;
79d3123599STom Lendacky 
80d3123599STom Lendacky 	struct scatterlist key_sg;
81d3123599STom Lendacky 	unsigned int key_len;
82d3123599STom Lendacky 	u8 key[AES_MAX_KEY_SIZE];
83d3123599STom Lendacky 
84d3123599STom Lendacky 	u8 nonce[CTR_RFC3686_NONCE_SIZE];
85d3123599STom Lendacky 
86d3123599STom Lendacky 	/* CMAC key structures */
87d3123599STom Lendacky 	struct scatterlist k1_sg;
88d3123599STom Lendacky 	struct scatterlist k2_sg;
89d3123599STom Lendacky 	unsigned int kn_len;
90d3123599STom Lendacky 	u8 k1[AES_BLOCK_SIZE];
91d3123599STom Lendacky 	u8 k2[AES_BLOCK_SIZE];
92d3123599STom Lendacky };
93d3123599STom Lendacky 
94d3123599STom Lendacky struct ccp_aes_req_ctx {
95d3123599STom Lendacky 	struct scatterlist iv_sg;
96d3123599STom Lendacky 	u8 iv[AES_BLOCK_SIZE];
97d3123599STom Lendacky 
98d3123599STom Lendacky 	/* Fields used for RFC3686 requests */
99d3123599STom Lendacky 	u8 *rfc3686_info;
100d3123599STom Lendacky 	u8 rfc3686_iv[AES_BLOCK_SIZE];
101d3123599STom Lendacky 
102d3123599STom Lendacky 	struct ccp_cmd cmd;
103d3123599STom Lendacky };
104d3123599STom Lendacky 
105d3123599STom Lendacky struct ccp_aes_cmac_req_ctx {
106d3123599STom Lendacky 	unsigned int null_msg;
107d3123599STom Lendacky 	unsigned int final;
108d3123599STom Lendacky 
10981a59f00STom Lendacky 	struct scatterlist *src;
11081a59f00STom Lendacky 	unsigned int nbytes;
11181a59f00STom Lendacky 
11281a59f00STom Lendacky 	u64 hash_cnt;
113d3123599STom Lendacky 	unsigned int hash_rem;
114d3123599STom Lendacky 
115d3123599STom Lendacky 	struct sg_table data_sg;
116d3123599STom Lendacky 
117d3123599STom Lendacky 	struct scatterlist iv_sg;
118d3123599STom Lendacky 	u8 iv[AES_BLOCK_SIZE];
119d3123599STom Lendacky 
120d3123599STom Lendacky 	struct scatterlist buf_sg;
121d3123599STom Lendacky 	unsigned int buf_count;
122d3123599STom Lendacky 	u8 buf[AES_BLOCK_SIZE];
123d3123599STom Lendacky 
124d3123599STom Lendacky 	struct scatterlist pad_sg;
125d3123599STom Lendacky 	unsigned int pad_count;
126d3123599STom Lendacky 	u8 pad[AES_BLOCK_SIZE];
127d3123599STom Lendacky 
128d3123599STom Lendacky 	struct ccp_cmd cmd;
129d3123599STom Lendacky };
130d3123599STom Lendacky 
131d1662165STom Lendacky struct ccp_aes_cmac_exp_ctx {
132d1662165STom Lendacky 	unsigned int null_msg;
133d1662165STom Lendacky 
134d1662165STom Lendacky 	u8 iv[AES_BLOCK_SIZE];
135d1662165STom Lendacky 
136d1662165STom Lendacky 	unsigned int buf_count;
137d1662165STom Lendacky 	u8 buf[AES_BLOCK_SIZE];
138d1662165STom Lendacky };
139d1662165STom Lendacky 
140*ccebcf3fSGary R Hook /* SHA-related defines
141*ccebcf3fSGary R Hook  * These values must be large enough to accommodate any variant
142*ccebcf3fSGary R Hook  */
143*ccebcf3fSGary R Hook #define MAX_SHA_CONTEXT_SIZE	SHA512_DIGEST_SIZE
144*ccebcf3fSGary R Hook #define MAX_SHA_BLOCK_SIZE	SHA512_BLOCK_SIZE
145d3123599STom Lendacky 
146d3123599STom Lendacky struct ccp_sha_ctx {
147c11baa02STom Lendacky 	struct scatterlist opad_sg;
148c11baa02STom Lendacky 	unsigned int opad_count;
149c11baa02STom Lendacky 
150d3123599STom Lendacky 	unsigned int key_len;
151d3123599STom Lendacky 	u8 key[MAX_SHA_BLOCK_SIZE];
152d3123599STom Lendacky 	u8 ipad[MAX_SHA_BLOCK_SIZE];
153d3123599STom Lendacky 	u8 opad[MAX_SHA_BLOCK_SIZE];
154c11baa02STom Lendacky 	struct crypto_shash *hmac_tfm;
155d3123599STom Lendacky };
156d3123599STom Lendacky 
157d3123599STom Lendacky struct ccp_sha_req_ctx {
158d3123599STom Lendacky 	enum ccp_sha_type type;
159d3123599STom Lendacky 
160d3123599STom Lendacky 	u64 msg_bits;
161d3123599STom Lendacky 
162d3123599STom Lendacky 	unsigned int first;
163d3123599STom Lendacky 	unsigned int final;
164d3123599STom Lendacky 
16581a59f00STom Lendacky 	struct scatterlist *src;
16681a59f00STom Lendacky 	unsigned int nbytes;
16781a59f00STom Lendacky 
16881a59f00STom Lendacky 	u64 hash_cnt;
169d3123599STom Lendacky 	unsigned int hash_rem;
170d3123599STom Lendacky 
171d3123599STom Lendacky 	struct sg_table data_sg;
172d3123599STom Lendacky 
173d3123599STom Lendacky 	struct scatterlist ctx_sg;
174d3123599STom Lendacky 	u8 ctx[MAX_SHA_CONTEXT_SIZE];
175d3123599STom Lendacky 
176d3123599STom Lendacky 	struct scatterlist buf_sg;
177d3123599STom Lendacky 	unsigned int buf_count;
178d3123599STom Lendacky 	u8 buf[MAX_SHA_BLOCK_SIZE];
179d3123599STom Lendacky 
180d3123599STom Lendacky 	/* CCP driver command */
181d3123599STom Lendacky 	struct ccp_cmd cmd;
182d3123599STom Lendacky };
183d3123599STom Lendacky 
184d1662165STom Lendacky struct ccp_sha_exp_ctx {
185d1662165STom Lendacky 	enum ccp_sha_type type;
186d1662165STom Lendacky 
187d1662165STom Lendacky 	u64 msg_bits;
188d1662165STom Lendacky 
189d1662165STom Lendacky 	unsigned int first;
190d1662165STom Lendacky 
191d1662165STom Lendacky 	u8 ctx[MAX_SHA_CONTEXT_SIZE];
192d1662165STom Lendacky 
193d1662165STom Lendacky 	unsigned int buf_count;
194d1662165STom Lendacky 	u8 buf[MAX_SHA_BLOCK_SIZE];
195d1662165STom Lendacky };
196d1662165STom Lendacky 
197d3123599STom Lendacky /***** Common Context Structure *****/
198d3123599STom Lendacky struct ccp_ctx {
199d3123599STom Lendacky 	int (*complete)(struct crypto_async_request *req, int ret);
200d3123599STom Lendacky 
201d3123599STom Lendacky 	union {
202d3123599STom Lendacky 		struct ccp_aes_ctx aes;
203d3123599STom Lendacky 		struct ccp_sha_ctx sha;
204d3123599STom Lendacky 	} u;
205d3123599STom Lendacky };
206d3123599STom Lendacky 
207d3123599STom Lendacky int ccp_crypto_enqueue_request(struct crypto_async_request *req,
208d3123599STom Lendacky 			       struct ccp_cmd *cmd);
209d3123599STom Lendacky struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table,
210d3123599STom Lendacky 					    struct scatterlist *sg_add);
211d3123599STom Lendacky 
212d3123599STom Lendacky int ccp_register_aes_algs(struct list_head *head);
213d3123599STom Lendacky int ccp_register_aes_cmac_algs(struct list_head *head);
214d3123599STom Lendacky int ccp_register_aes_xts_algs(struct list_head *head);
215d3123599STom Lendacky int ccp_register_sha_algs(struct list_head *head);
216d3123599STom Lendacky 
217d3123599STom Lendacky #endif
218