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> 22*36cf515bSGary R Hook #include <crypto/internal/aead.h> 23*36cf515bSGary R Hook #include <crypto/aead.h> 24d3123599STom Lendacky #include <crypto/ctr.h> 25d3123599STom Lendacky #include <crypto/hash.h> 26d3123599STom Lendacky #include <crypto/sha.h> 27d3123599STom Lendacky 28990672d4SGary R Hook #define CCP_LOG_LEVEL KERN_INFO 29990672d4SGary R Hook 30d3123599STom Lendacky #define CCP_CRA_PRIORITY 300 31d3123599STom Lendacky 32d3123599STom Lendacky struct ccp_crypto_ablkcipher_alg { 33d3123599STom Lendacky struct list_head entry; 34d3123599STom Lendacky 35d3123599STom Lendacky u32 mode; 36d3123599STom Lendacky 37d3123599STom Lendacky struct crypto_alg alg; 38d3123599STom Lendacky }; 39d3123599STom Lendacky 40*36cf515bSGary R Hook struct ccp_crypto_aead { 41*36cf515bSGary R Hook struct list_head entry; 42*36cf515bSGary R Hook 43*36cf515bSGary R Hook u32 mode; 44*36cf515bSGary R Hook 45*36cf515bSGary R Hook struct aead_alg alg; 46*36cf515bSGary R Hook }; 47*36cf515bSGary R Hook 48d3123599STom Lendacky struct ccp_crypto_ahash_alg { 49d3123599STom Lendacky struct list_head entry; 50d3123599STom Lendacky 516f0be9b2STom Lendacky const __be32 *init; 52d3123599STom Lendacky u32 type; 53d3123599STom Lendacky u32 mode; 54d3123599STom Lendacky 55d3123599STom Lendacky /* Child algorithm used for HMAC, CMAC, etc */ 56d3123599STom Lendacky char child_alg[CRYPTO_MAX_ALG_NAME]; 57d3123599STom Lendacky 58d3123599STom Lendacky struct ahash_alg alg; 59d3123599STom Lendacky }; 60d3123599STom Lendacky 61d3123599STom Lendacky static inline struct ccp_crypto_ablkcipher_alg * 62d3123599STom Lendacky ccp_crypto_ablkcipher_alg(struct crypto_tfm *tfm) 63d3123599STom Lendacky { 64d3123599STom Lendacky struct crypto_alg *alg = tfm->__crt_alg; 65d3123599STom Lendacky 66d3123599STom Lendacky return container_of(alg, struct ccp_crypto_ablkcipher_alg, alg); 67d3123599STom Lendacky } 68d3123599STom Lendacky 69d3123599STom Lendacky static inline struct ccp_crypto_ahash_alg * 70d3123599STom Lendacky ccp_crypto_ahash_alg(struct crypto_tfm *tfm) 71d3123599STom Lendacky { 72d3123599STom Lendacky struct crypto_alg *alg = tfm->__crt_alg; 73d3123599STom Lendacky struct ahash_alg *ahash_alg; 74d3123599STom Lendacky 75d3123599STom Lendacky ahash_alg = container_of(alg, struct ahash_alg, halg.base); 76d3123599STom Lendacky 77d3123599STom Lendacky return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg); 78d3123599STom Lendacky } 79d3123599STom Lendacky 80d3123599STom Lendacky /***** AES related defines *****/ 81d3123599STom Lendacky struct ccp_aes_ctx { 82d3123599STom Lendacky /* Fallback cipher for XTS with unsupported unit sizes */ 83241118deSHerbert Xu struct crypto_skcipher *tfm_skcipher; 84d3123599STom Lendacky 85d3123599STom Lendacky /* Cipher used to generate CMAC K1/K2 keys */ 86d3123599STom Lendacky struct crypto_cipher *tfm_cipher; 87d3123599STom Lendacky 88d3123599STom Lendacky enum ccp_engine engine; 89d3123599STom Lendacky enum ccp_aes_type type; 90d3123599STom Lendacky enum ccp_aes_mode mode; 91d3123599STom Lendacky 92d3123599STom Lendacky struct scatterlist key_sg; 93d3123599STom Lendacky unsigned int key_len; 94d3123599STom Lendacky u8 key[AES_MAX_KEY_SIZE]; 95d3123599STom Lendacky 96d3123599STom Lendacky u8 nonce[CTR_RFC3686_NONCE_SIZE]; 97d3123599STom Lendacky 98d3123599STom Lendacky /* CMAC key structures */ 99d3123599STom Lendacky struct scatterlist k1_sg; 100d3123599STom Lendacky struct scatterlist k2_sg; 101d3123599STom Lendacky unsigned int kn_len; 102d3123599STom Lendacky u8 k1[AES_BLOCK_SIZE]; 103d3123599STom Lendacky u8 k2[AES_BLOCK_SIZE]; 104d3123599STom Lendacky }; 105d3123599STom Lendacky 106d3123599STom Lendacky struct ccp_aes_req_ctx { 107d3123599STom Lendacky struct scatterlist iv_sg; 108d3123599STom Lendacky u8 iv[AES_BLOCK_SIZE]; 109d3123599STom Lendacky 110*36cf515bSGary R Hook struct scatterlist tag_sg; 111*36cf515bSGary R Hook u8 tag[AES_BLOCK_SIZE]; 112*36cf515bSGary R Hook 113d3123599STom Lendacky /* Fields used for RFC3686 requests */ 114d3123599STom Lendacky u8 *rfc3686_info; 115d3123599STom Lendacky u8 rfc3686_iv[AES_BLOCK_SIZE]; 116d3123599STom Lendacky 117d3123599STom Lendacky struct ccp_cmd cmd; 118d3123599STom Lendacky }; 119d3123599STom Lendacky 120d3123599STom Lendacky struct ccp_aes_cmac_req_ctx { 121d3123599STom Lendacky unsigned int null_msg; 122d3123599STom Lendacky unsigned int final; 123d3123599STom Lendacky 12481a59f00STom Lendacky struct scatterlist *src; 12581a59f00STom Lendacky unsigned int nbytes; 12681a59f00STom Lendacky 12781a59f00STom Lendacky u64 hash_cnt; 128d3123599STom Lendacky unsigned int hash_rem; 129d3123599STom Lendacky 130d3123599STom Lendacky struct sg_table data_sg; 131d3123599STom Lendacky 132d3123599STom Lendacky struct scatterlist iv_sg; 133d3123599STom Lendacky u8 iv[AES_BLOCK_SIZE]; 134d3123599STom Lendacky 135d3123599STom Lendacky struct scatterlist buf_sg; 136d3123599STom Lendacky unsigned int buf_count; 137d3123599STom Lendacky u8 buf[AES_BLOCK_SIZE]; 138d3123599STom Lendacky 139d3123599STom Lendacky struct scatterlist pad_sg; 140d3123599STom Lendacky unsigned int pad_count; 141d3123599STom Lendacky u8 pad[AES_BLOCK_SIZE]; 142d3123599STom Lendacky 143d3123599STom Lendacky struct ccp_cmd cmd; 144d3123599STom Lendacky }; 145d3123599STom Lendacky 146d1662165STom Lendacky struct ccp_aes_cmac_exp_ctx { 147d1662165STom Lendacky unsigned int null_msg; 148d1662165STom Lendacky 149d1662165STom Lendacky u8 iv[AES_BLOCK_SIZE]; 150d1662165STom Lendacky 151d1662165STom Lendacky unsigned int buf_count; 152d1662165STom Lendacky u8 buf[AES_BLOCK_SIZE]; 153d1662165STom Lendacky }; 154d1662165STom Lendacky 155990672d4SGary R Hook /***** 3DES related defines *****/ 156990672d4SGary R Hook struct ccp_des3_ctx { 157990672d4SGary R Hook enum ccp_engine engine; 158990672d4SGary R Hook enum ccp_des3_type type; 159990672d4SGary R Hook enum ccp_des3_mode mode; 160990672d4SGary R Hook 161990672d4SGary R Hook struct scatterlist key_sg; 162990672d4SGary R Hook unsigned int key_len; 163990672d4SGary R Hook u8 key[AES_MAX_KEY_SIZE]; 164990672d4SGary R Hook }; 165990672d4SGary R Hook 166990672d4SGary R Hook struct ccp_des3_req_ctx { 167990672d4SGary R Hook struct scatterlist iv_sg; 168990672d4SGary R Hook u8 iv[AES_BLOCK_SIZE]; 169990672d4SGary R Hook 170990672d4SGary R Hook struct ccp_cmd cmd; 171990672d4SGary R Hook }; 172990672d4SGary R Hook 173ccebcf3fSGary R Hook /* SHA-related defines 174ccebcf3fSGary R Hook * These values must be large enough to accommodate any variant 175ccebcf3fSGary R Hook */ 176ccebcf3fSGary R Hook #define MAX_SHA_CONTEXT_SIZE SHA512_DIGEST_SIZE 177ccebcf3fSGary R Hook #define MAX_SHA_BLOCK_SIZE SHA512_BLOCK_SIZE 178d3123599STom Lendacky 179d3123599STom Lendacky struct ccp_sha_ctx { 180c11baa02STom Lendacky struct scatterlist opad_sg; 181c11baa02STom Lendacky unsigned int opad_count; 182c11baa02STom Lendacky 183d3123599STom Lendacky unsigned int key_len; 184d3123599STom Lendacky u8 key[MAX_SHA_BLOCK_SIZE]; 185d3123599STom Lendacky u8 ipad[MAX_SHA_BLOCK_SIZE]; 186d3123599STom Lendacky u8 opad[MAX_SHA_BLOCK_SIZE]; 187c11baa02STom Lendacky struct crypto_shash *hmac_tfm; 188d3123599STom Lendacky }; 189d3123599STom Lendacky 190d3123599STom Lendacky struct ccp_sha_req_ctx { 191d3123599STom Lendacky enum ccp_sha_type type; 192d3123599STom Lendacky 193d3123599STom Lendacky u64 msg_bits; 194d3123599STom Lendacky 195d3123599STom Lendacky unsigned int first; 196d3123599STom Lendacky unsigned int final; 197d3123599STom Lendacky 19881a59f00STom Lendacky struct scatterlist *src; 19981a59f00STom Lendacky unsigned int nbytes; 20081a59f00STom Lendacky 20181a59f00STom Lendacky u64 hash_cnt; 202d3123599STom Lendacky unsigned int hash_rem; 203d3123599STom Lendacky 204d3123599STom Lendacky struct sg_table data_sg; 205d3123599STom Lendacky 206d3123599STom Lendacky struct scatterlist ctx_sg; 207d3123599STom Lendacky u8 ctx[MAX_SHA_CONTEXT_SIZE]; 208d3123599STom Lendacky 209d3123599STom Lendacky struct scatterlist buf_sg; 210d3123599STom Lendacky unsigned int buf_count; 211d3123599STom Lendacky u8 buf[MAX_SHA_BLOCK_SIZE]; 212d3123599STom Lendacky 213d3123599STom Lendacky /* CCP driver command */ 214d3123599STom Lendacky struct ccp_cmd cmd; 215d3123599STom Lendacky }; 216d3123599STom Lendacky 217d1662165STom Lendacky struct ccp_sha_exp_ctx { 218d1662165STom Lendacky enum ccp_sha_type type; 219d1662165STom Lendacky 220d1662165STom Lendacky u64 msg_bits; 221d1662165STom Lendacky 222d1662165STom Lendacky unsigned int first; 223d1662165STom Lendacky 224d1662165STom Lendacky u8 ctx[MAX_SHA_CONTEXT_SIZE]; 225d1662165STom Lendacky 226d1662165STom Lendacky unsigned int buf_count; 227d1662165STom Lendacky u8 buf[MAX_SHA_BLOCK_SIZE]; 228d1662165STom Lendacky }; 229d1662165STom Lendacky 230d3123599STom Lendacky /***** Common Context Structure *****/ 231d3123599STom Lendacky struct ccp_ctx { 232d3123599STom Lendacky int (*complete)(struct crypto_async_request *req, int ret); 233d3123599STom Lendacky 234d3123599STom Lendacky union { 235d3123599STom Lendacky struct ccp_aes_ctx aes; 236d3123599STom Lendacky struct ccp_sha_ctx sha; 237990672d4SGary R Hook struct ccp_des3_ctx des3; 238d3123599STom Lendacky } u; 239d3123599STom Lendacky }; 240d3123599STom Lendacky 241d3123599STom Lendacky int ccp_crypto_enqueue_request(struct crypto_async_request *req, 242d3123599STom Lendacky struct ccp_cmd *cmd); 243d3123599STom Lendacky struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table, 244d3123599STom Lendacky struct scatterlist *sg_add); 245d3123599STom Lendacky 246d3123599STom Lendacky int ccp_register_aes_algs(struct list_head *head); 247d3123599STom Lendacky int ccp_register_aes_cmac_algs(struct list_head *head); 248d3123599STom Lendacky int ccp_register_aes_xts_algs(struct list_head *head); 249*36cf515bSGary R Hook int ccp_register_aes_aeads(struct list_head *head); 250d3123599STom Lendacky int ccp_register_sha_algs(struct list_head *head); 251990672d4SGary R Hook int ccp_register_des3_algs(struct list_head *head); 252d3123599STom Lendacky 253d3123599STom Lendacky #endif 254