Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * AMD Cryptographic Coprocessor (CCP) crypto API support
0004  *
0005  * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
0006  *
0007  * Author: Tom Lendacky <thomas.lendacky@amd.com>
0008  */
0009 
0010 #ifndef __CCP_CRYPTO_H__
0011 #define __CCP_CRYPTO_H__
0012 
0013 #include <linux/list.h>
0014 #include <linux/wait.h>
0015 #include <linux/ccp.h>
0016 #include <crypto/algapi.h>
0017 #include <crypto/aes.h>
0018 #include <crypto/internal/aead.h>
0019 #include <crypto/aead.h>
0020 #include <crypto/ctr.h>
0021 #include <crypto/hash.h>
0022 #include <crypto/sha1.h>
0023 #include <crypto/sha2.h>
0024 #include <crypto/akcipher.h>
0025 #include <crypto/skcipher.h>
0026 #include <crypto/internal/rsa.h>
0027 
0028 /* We want the module name in front of our messages */
0029 #undef pr_fmt
0030 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0031 
0032 #define CCP_LOG_LEVEL   KERN_INFO
0033 
0034 #define CCP_CRA_PRIORITY    300
0035 
0036 struct ccp_crypto_skcipher_alg {
0037     struct list_head entry;
0038 
0039     u32 mode;
0040 
0041     struct skcipher_alg alg;
0042 };
0043 
0044 struct ccp_crypto_aead {
0045     struct list_head entry;
0046 
0047     u32 mode;
0048 
0049     struct aead_alg alg;
0050 };
0051 
0052 struct ccp_crypto_ahash_alg {
0053     struct list_head entry;
0054 
0055     const __be32 *init;
0056     u32 type;
0057     u32 mode;
0058 
0059     /* Child algorithm used for HMAC, CMAC, etc */
0060     char child_alg[CRYPTO_MAX_ALG_NAME];
0061 
0062     struct ahash_alg alg;
0063 };
0064 
0065 struct ccp_crypto_akcipher_alg {
0066     struct list_head entry;
0067 
0068     struct akcipher_alg alg;
0069 };
0070 
0071 static inline struct ccp_crypto_skcipher_alg *
0072     ccp_crypto_skcipher_alg(struct crypto_skcipher *tfm)
0073 {
0074     struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
0075 
0076     return container_of(alg, struct ccp_crypto_skcipher_alg, alg);
0077 }
0078 
0079 static inline struct ccp_crypto_ahash_alg *
0080     ccp_crypto_ahash_alg(struct crypto_tfm *tfm)
0081 {
0082     struct crypto_alg *alg = tfm->__crt_alg;
0083     struct ahash_alg *ahash_alg;
0084 
0085     ahash_alg = container_of(alg, struct ahash_alg, halg.base);
0086 
0087     return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg);
0088 }
0089 
0090 /***** AES related defines *****/
0091 struct ccp_aes_ctx {
0092     /* Fallback cipher for XTS with unsupported unit sizes */
0093     struct crypto_skcipher *tfm_skcipher;
0094 
0095     enum ccp_engine engine;
0096     enum ccp_aes_type type;
0097     enum ccp_aes_mode mode;
0098 
0099     struct scatterlist key_sg;
0100     unsigned int key_len;
0101     u8 key[AES_MAX_KEY_SIZE * 2];
0102 
0103     u8 nonce[CTR_RFC3686_NONCE_SIZE];
0104 
0105     /* CMAC key structures */
0106     struct scatterlist k1_sg;
0107     struct scatterlist k2_sg;
0108     unsigned int kn_len;
0109     u8 k1[AES_BLOCK_SIZE];
0110     u8 k2[AES_BLOCK_SIZE];
0111 };
0112 
0113 struct ccp_aes_req_ctx {
0114     struct scatterlist iv_sg;
0115     u8 iv[AES_BLOCK_SIZE];
0116 
0117     struct scatterlist tag_sg;
0118     u8 tag[AES_BLOCK_SIZE];
0119 
0120     /* Fields used for RFC3686 requests */
0121     u8 *rfc3686_info;
0122     u8 rfc3686_iv[AES_BLOCK_SIZE];
0123 
0124     struct ccp_cmd cmd;
0125 
0126     struct skcipher_request fallback_req;   // keep at the end
0127 };
0128 
0129 struct ccp_aes_cmac_req_ctx {
0130     unsigned int null_msg;
0131     unsigned int final;
0132 
0133     struct scatterlist *src;
0134     unsigned int nbytes;
0135 
0136     u64 hash_cnt;
0137     unsigned int hash_rem;
0138 
0139     struct sg_table data_sg;
0140 
0141     struct scatterlist iv_sg;
0142     u8 iv[AES_BLOCK_SIZE];
0143 
0144     struct scatterlist buf_sg;
0145     unsigned int buf_count;
0146     u8 buf[AES_BLOCK_SIZE];
0147 
0148     struct scatterlist pad_sg;
0149     unsigned int pad_count;
0150     u8 pad[AES_BLOCK_SIZE];
0151 
0152     struct ccp_cmd cmd;
0153 };
0154 
0155 struct ccp_aes_cmac_exp_ctx {
0156     unsigned int null_msg;
0157 
0158     u8 iv[AES_BLOCK_SIZE];
0159 
0160     unsigned int buf_count;
0161     u8 buf[AES_BLOCK_SIZE];
0162 };
0163 
0164 /***** 3DES related defines *****/
0165 struct ccp_des3_ctx {
0166     enum ccp_engine engine;
0167     enum ccp_des3_type type;
0168     enum ccp_des3_mode mode;
0169 
0170     struct scatterlist key_sg;
0171     unsigned int key_len;
0172     u8 key[AES_MAX_KEY_SIZE];
0173 };
0174 
0175 struct ccp_des3_req_ctx {
0176     struct scatterlist iv_sg;
0177     u8 iv[AES_BLOCK_SIZE];
0178 
0179     struct ccp_cmd cmd;
0180 };
0181 
0182 /* SHA-related defines
0183  * These values must be large enough to accommodate any variant
0184  */
0185 #define MAX_SHA_CONTEXT_SIZE    SHA512_DIGEST_SIZE
0186 #define MAX_SHA_BLOCK_SIZE  SHA512_BLOCK_SIZE
0187 
0188 struct ccp_sha_ctx {
0189     struct scatterlist opad_sg;
0190     unsigned int opad_count;
0191 
0192     unsigned int key_len;
0193     u8 key[MAX_SHA_BLOCK_SIZE];
0194     u8 ipad[MAX_SHA_BLOCK_SIZE];
0195     u8 opad[MAX_SHA_BLOCK_SIZE];
0196     struct crypto_shash *hmac_tfm;
0197 };
0198 
0199 struct ccp_sha_req_ctx {
0200     enum ccp_sha_type type;
0201 
0202     u64 msg_bits;
0203 
0204     unsigned int first;
0205     unsigned int final;
0206 
0207     struct scatterlist *src;
0208     unsigned int nbytes;
0209 
0210     u64 hash_cnt;
0211     unsigned int hash_rem;
0212 
0213     struct sg_table data_sg;
0214 
0215     struct scatterlist ctx_sg;
0216     u8 ctx[MAX_SHA_CONTEXT_SIZE];
0217 
0218     struct scatterlist buf_sg;
0219     unsigned int buf_count;
0220     u8 buf[MAX_SHA_BLOCK_SIZE];
0221 
0222     /* CCP driver command */
0223     struct ccp_cmd cmd;
0224 };
0225 
0226 struct ccp_sha_exp_ctx {
0227     enum ccp_sha_type type;
0228 
0229     u64 msg_bits;
0230 
0231     unsigned int first;
0232 
0233     u8 ctx[MAX_SHA_CONTEXT_SIZE];
0234 
0235     unsigned int buf_count;
0236     u8 buf[MAX_SHA_BLOCK_SIZE];
0237 };
0238 
0239 /***** RSA related defines *****/
0240 
0241 struct ccp_rsa_ctx {
0242     unsigned int key_len; /* in bits */
0243     struct scatterlist e_sg;
0244     u8 *e_buf;
0245     unsigned int e_len;
0246     struct scatterlist n_sg;
0247     u8 *n_buf;
0248     unsigned int n_len;
0249     struct scatterlist d_sg;
0250     u8 *d_buf;
0251     unsigned int d_len;
0252 };
0253 
0254 struct ccp_rsa_req_ctx {
0255     struct ccp_cmd cmd;
0256 };
0257 
0258 #define CCP_RSA_MAXMOD  (4 * 1024 / 8)
0259 #define CCP5_RSA_MAXMOD (16 * 1024 / 8)
0260 
0261 /***** Common Context Structure *****/
0262 struct ccp_ctx {
0263     int (*complete)(struct crypto_async_request *req, int ret);
0264 
0265     union {
0266         struct ccp_aes_ctx aes;
0267         struct ccp_rsa_ctx rsa;
0268         struct ccp_sha_ctx sha;
0269         struct ccp_des3_ctx des3;
0270     } u;
0271 };
0272 
0273 int ccp_crypto_enqueue_request(struct crypto_async_request *req,
0274                    struct ccp_cmd *cmd);
0275 struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table,
0276                         struct scatterlist *sg_add);
0277 
0278 int ccp_register_aes_algs(struct list_head *head);
0279 int ccp_register_aes_cmac_algs(struct list_head *head);
0280 int ccp_register_aes_xts_algs(struct list_head *head);
0281 int ccp_register_aes_aeads(struct list_head *head);
0282 int ccp_register_sha_algs(struct list_head *head);
0283 int ccp_register_des3_algs(struct list_head *head);
0284 int ccp_register_rsa_algs(struct list_head *head);
0285 
0286 #endif