2 * AMD Cryptographic Coprocessor (CCP) crypto API support
4 * Copyright (C) 2013 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
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.
13 #ifndef __CCP_CRYPTO_H__
14 #define __CCP_CRYPTO_H__
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>
29 #define CCP_CRA_PRIORITY 300
31 struct ccp_crypto_ablkcipher_alg
{
32 struct list_head entry
;
36 struct crypto_alg alg
;
39 struct ccp_crypto_ahash_alg
{
40 struct list_head entry
;
46 /* Child algorithm used for HMAC, CMAC, etc */
47 char child_alg
[CRYPTO_MAX_ALG_NAME
];
52 static inline struct ccp_crypto_ablkcipher_alg
*
53 ccp_crypto_ablkcipher_alg(struct crypto_tfm
*tfm
)
55 struct crypto_alg
*alg
= tfm
->__crt_alg
;
57 return container_of(alg
, struct ccp_crypto_ablkcipher_alg
, alg
);
60 static inline struct ccp_crypto_ahash_alg
*
61 ccp_crypto_ahash_alg(struct crypto_tfm
*tfm
)
63 struct crypto_alg
*alg
= tfm
->__crt_alg
;
64 struct ahash_alg
*ahash_alg
;
66 ahash_alg
= container_of(alg
, struct ahash_alg
, halg
.base
);
68 return container_of(ahash_alg
, struct ccp_crypto_ahash_alg
, alg
);
72 /***** AES related defines *****/
74 /* Fallback cipher for XTS with unsupported unit sizes */
75 struct crypto_ablkcipher
*tfm_ablkcipher
;
77 /* Cipher used to generate CMAC K1/K2 keys */
78 struct crypto_cipher
*tfm_cipher
;
80 enum ccp_engine engine
;
81 enum ccp_aes_type type
;
82 enum ccp_aes_mode mode
;
84 struct scatterlist key_sg
;
86 u8 key
[AES_MAX_KEY_SIZE
];
88 u8 nonce
[CTR_RFC3686_NONCE_SIZE
];
90 /* CMAC key structures */
91 struct scatterlist k1_sg
;
92 struct scatterlist k2_sg
;
94 u8 k1
[AES_BLOCK_SIZE
];
95 u8 k2
[AES_BLOCK_SIZE
];
98 struct ccp_aes_req_ctx
{
99 struct scatterlist iv_sg
;
100 u8 iv
[AES_BLOCK_SIZE
];
102 /* Fields used for RFC3686 requests */
104 u8 rfc3686_iv
[AES_BLOCK_SIZE
];
109 struct ccp_aes_cmac_req_ctx
{
110 unsigned int null_msg
;
113 struct scatterlist
*src
;
117 unsigned int hash_rem
;
119 struct sg_table data_sg
;
121 struct scatterlist iv_sg
;
122 u8 iv
[AES_BLOCK_SIZE
];
124 struct scatterlist buf_sg
;
125 unsigned int buf_count
;
126 u8 buf
[AES_BLOCK_SIZE
];
128 struct scatterlist pad_sg
;
129 unsigned int pad_count
;
130 u8 pad
[AES_BLOCK_SIZE
];
135 /***** SHA related defines *****/
136 #define MAX_SHA_CONTEXT_SIZE SHA256_DIGEST_SIZE
137 #define MAX_SHA_BLOCK_SIZE SHA256_BLOCK_SIZE
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_ahash
*hmac_tfm
;
147 struct ccp_sha_req_ctx
{
148 enum ccp_sha_type type
;
155 struct scatterlist
*src
;
159 unsigned int hash_rem
;
161 struct sg_table data_sg
;
163 struct scatterlist ctx_sg
;
164 u8 ctx
[MAX_SHA_CONTEXT_SIZE
];
166 struct scatterlist buf_sg
;
167 unsigned int buf_count
;
168 u8 buf
[MAX_SHA_BLOCK_SIZE
];
170 /* HMAC support field */
171 struct scatterlist pad_sg
;
173 /* CCP driver command */
177 /***** Common Context Structure *****/
179 int (*complete
)(struct crypto_async_request
*req
, int ret
);
182 struct ccp_aes_ctx aes
;
183 struct ccp_sha_ctx sha
;
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
);
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
);