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