1/* SPDX-License-Identifier: GPL-2.0 */ 2/* 3 * AMD Cryptographic Coprocessor (CCP) crypto API support 4 * 5 * Copyright (C) 2013,2017 Advanced Micro Devices, Inc. 6 * 7 * Author: Tom Lendacky <thomas.lendacky@amd.com> 8 */ 9 10#ifndef __CCP_CRYPTO_H__ 11#define __CCP_CRYPTO_H__ 12 13#include <linux/list.h> 14#include <linux/wait.h> 15#include <linux/ccp.h> 16#include <crypto/algapi.h> 17#include <crypto/aes.h> 18#include <crypto/internal/aead.h> 19#include <crypto/aead.h> 20#include <crypto/ctr.h> 21#include <crypto/hash.h> 22#include <crypto/sha.h> 23#include <crypto/akcipher.h> 24#include <crypto/skcipher.h> 25#include <crypto/internal/rsa.h> 26 27/* We want the module name in front of our messages */ 28#undef pr_fmt 29#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 30 31#define CCP_LOG_LEVEL KERN_INFO 32 33#define CCP_CRA_PRIORITY 300 34 35struct ccp_crypto_skcipher_alg { 36 struct list_head entry; 37 38 u32 mode; 39 40 struct skcipher_alg alg; 41}; 42 43struct ccp_crypto_aead { 44 struct list_head entry; 45 46 u32 mode; 47 48 struct aead_alg alg; 49}; 50 51struct ccp_crypto_ahash_alg { 52 struct list_head entry; 53 54 const __be32 *init; 55 u32 type; 56 u32 mode; 57 58 /* Child algorithm used for HMAC, CMAC, etc */ 59 char child_alg[CRYPTO_MAX_ALG_NAME]; 60 61 struct ahash_alg alg; 62}; 63 64struct ccp_crypto_akcipher_alg { 65 struct list_head entry; 66 67 struct akcipher_alg alg; 68}; 69 70static inline struct ccp_crypto_skcipher_alg * 71 ccp_crypto_skcipher_alg(struct crypto_skcipher *tfm) 72{ 73 struct skcipher_alg *alg = crypto_skcipher_alg(tfm); 74 75 return container_of(alg, struct ccp_crypto_skcipher_alg, alg); 76} 77 78static inline struct ccp_crypto_ahash_alg * 79 ccp_crypto_ahash_alg(struct crypto_tfm *tfm) 80{ 81 struct crypto_alg *alg = tfm->__crt_alg; 82 struct ahash_alg *ahash_alg; 83 84 ahash_alg = container_of(alg, struct ahash_alg, halg.base); 85 86 return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg); 87} 88 89/***** AES related defines *****/ 90struct ccp_aes_ctx { 91 /* Fallback cipher for XTS with unsupported unit sizes */ 92 struct crypto_skcipher *tfm_skcipher; 93 94 /* Cipher used to generate CMAC K1/K2 keys */ 95 struct crypto_cipher *tfm_cipher; 96 97 enum ccp_engine engine; 98 enum ccp_aes_type type; 99 enum ccp_aes_mode mode; 100 101 struct scatterlist key_sg; 102 unsigned int key_len; 103 u8 key[AES_MAX_KEY_SIZE * 2]; 104 105 u8 nonce[CTR_RFC3686_NONCE_SIZE]; 106 107 /* CMAC key structures */ 108 struct scatterlist k1_sg; 109 struct scatterlist k2_sg; 110 unsigned int kn_len; 111 u8 k1[AES_BLOCK_SIZE]; 112 u8 k2[AES_BLOCK_SIZE]; 113}; 114 115struct ccp_aes_req_ctx { 116 struct scatterlist iv_sg; 117 u8 iv[AES_BLOCK_SIZE]; 118 119 struct scatterlist tag_sg; 120 u8 tag[AES_BLOCK_SIZE]; 121 122 /* Fields used for RFC3686 requests */ 123 u8 *rfc3686_info; 124 u8 rfc3686_iv[AES_BLOCK_SIZE]; 125 126 struct ccp_cmd cmd; 127 128 struct skcipher_request fallback_req; // keep at the end 129}; 130 131struct ccp_aes_cmac_req_ctx { 132 unsigned int null_msg; 133 unsigned int final; 134 135 struct scatterlist *src; 136 unsigned int nbytes; 137 138 u64 hash_cnt; 139 unsigned int hash_rem; 140 141 struct sg_table data_sg; 142 143 struct scatterlist iv_sg; 144 u8 iv[AES_BLOCK_SIZE]; 145 146 struct scatterlist buf_sg; 147 unsigned int buf_count; 148 u8 buf[AES_BLOCK_SIZE]; 149 150 struct scatterlist pad_sg; 151 unsigned int pad_count; 152 u8 pad[AES_BLOCK_SIZE]; 153 154 struct ccp_cmd cmd; 155}; 156 157struct ccp_aes_cmac_exp_ctx { 158 unsigned int null_msg; 159 160 u8 iv[AES_BLOCK_SIZE]; 161 162 unsigned int buf_count; 163 u8 buf[AES_BLOCK_SIZE]; 164}; 165 166/***** 3DES related defines *****/ 167struct ccp_des3_ctx { 168 enum ccp_engine engine; 169 enum ccp_des3_type type; 170 enum ccp_des3_mode mode; 171 172 struct scatterlist key_sg; 173 unsigned int key_len; 174 u8 key[AES_MAX_KEY_SIZE]; 175}; 176 177struct ccp_des3_req_ctx { 178 struct scatterlist iv_sg; 179 u8 iv[AES_BLOCK_SIZE]; 180 181 struct ccp_cmd cmd; 182}; 183 184/* SHA-related defines 185 * These values must be large enough to accommodate any variant 186 */ 187#define MAX_SHA_CONTEXT_SIZE SHA512_DIGEST_SIZE 188#define MAX_SHA_BLOCK_SIZE SHA512_BLOCK_SIZE 189 190struct ccp_sha_ctx { 191 struct scatterlist opad_sg; 192 unsigned int opad_count; 193 194 unsigned int key_len; 195 u8 key[MAX_SHA_BLOCK_SIZE]; 196 u8 ipad[MAX_SHA_BLOCK_SIZE]; 197 u8 opad[MAX_SHA_BLOCK_SIZE]; 198 struct crypto_shash *hmac_tfm; 199}; 200 201struct ccp_sha_req_ctx { 202 enum ccp_sha_type type; 203 204 u64 msg_bits; 205 206 unsigned int first; 207 unsigned int final; 208 209 struct scatterlist *src; 210 unsigned int nbytes; 211 212 u64 hash_cnt; 213 unsigned int hash_rem; 214 215 struct sg_table data_sg; 216 217 struct scatterlist ctx_sg; 218 u8 ctx[MAX_SHA_CONTEXT_SIZE]; 219 220 struct scatterlist buf_sg; 221 unsigned int buf_count; 222 u8 buf[MAX_SHA_BLOCK_SIZE]; 223 224 /* CCP driver command */ 225 struct ccp_cmd cmd; 226}; 227 228struct ccp_sha_exp_ctx { 229 enum ccp_sha_type type; 230 231 u64 msg_bits; 232 233 unsigned int first; 234 235 u8 ctx[MAX_SHA_CONTEXT_SIZE]; 236 237 unsigned int buf_count; 238 u8 buf[MAX_SHA_BLOCK_SIZE]; 239}; 240 241/***** RSA related defines *****/ 242 243struct ccp_rsa_ctx { 244 unsigned int key_len; /* in bits */ 245 struct scatterlist e_sg; 246 u8 *e_buf; 247 unsigned int e_len; 248 struct scatterlist n_sg; 249 u8 *n_buf; 250 unsigned int n_len; 251 struct scatterlist d_sg; 252 u8 *d_buf; 253 unsigned int d_len; 254}; 255 256struct ccp_rsa_req_ctx { 257 struct ccp_cmd cmd; 258}; 259 260#define CCP_RSA_MAXMOD (4 * 1024 / 8) 261#define CCP5_RSA_MAXMOD (16 * 1024 / 8) 262 263/***** Common Context Structure *****/ 264struct ccp_ctx { 265 int (*complete)(struct crypto_async_request *req, int ret); 266 267 union { 268 struct ccp_aes_ctx aes; 269 struct ccp_rsa_ctx rsa; 270 struct ccp_sha_ctx sha; 271 struct ccp_des3_ctx des3; 272 } u; 273}; 274 275int ccp_crypto_enqueue_request(struct crypto_async_request *req, 276 struct ccp_cmd *cmd); 277struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table, 278 struct scatterlist *sg_add); 279 280int ccp_register_aes_algs(struct list_head *head); 281int ccp_register_aes_cmac_algs(struct list_head *head); 282int ccp_register_aes_xts_algs(struct list_head *head); 283int ccp_register_aes_aeads(struct list_head *head); 284int ccp_register_sha_algs(struct list_head *head); 285int ccp_register_des3_algs(struct list_head *head); 286int ccp_register_rsa_algs(struct list_head *head); 287 288#endif 289