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 17d3123599STom Lendacky #include <linux/list.h> 18d3123599STom Lendacky #include <linux/wait.h> 19d3123599STom Lendacky #include <linux/pci.h> 20d3123599STom Lendacky #include <linux/ccp.h> 21d3123599STom Lendacky #include <linux/crypto.h> 22d3123599STom Lendacky #include <crypto/algapi.h> 23d3123599STom Lendacky #include <crypto/aes.h> 24d3123599STom Lendacky #include <crypto/ctr.h> 25d3123599STom Lendacky #include <crypto/hash.h> 26d3123599STom Lendacky #include <crypto/sha.h> 27d3123599STom Lendacky 28d3123599STom Lendacky 29d3123599STom Lendacky #define CCP_CRA_PRIORITY 300 30d3123599STom Lendacky 31d3123599STom Lendacky struct ccp_crypto_ablkcipher_alg { 32d3123599STom Lendacky struct list_head entry; 33d3123599STom Lendacky 34d3123599STom Lendacky u32 mode; 35d3123599STom Lendacky 36d3123599STom Lendacky struct crypto_alg alg; 37d3123599STom Lendacky }; 38d3123599STom Lendacky 39d3123599STom Lendacky struct ccp_crypto_ahash_alg { 40d3123599STom Lendacky struct list_head entry; 41d3123599STom Lendacky 426f0be9b2STom Lendacky const __be32 *init; 43d3123599STom Lendacky u32 type; 44d3123599STom Lendacky u32 mode; 45d3123599STom Lendacky 46d3123599STom Lendacky /* Child algorithm used for HMAC, CMAC, etc */ 47d3123599STom Lendacky char child_alg[CRYPTO_MAX_ALG_NAME]; 48d3123599STom Lendacky 49d3123599STom Lendacky struct ahash_alg alg; 50d3123599STom Lendacky }; 51d3123599STom Lendacky 52d3123599STom Lendacky static inline struct ccp_crypto_ablkcipher_alg * 53d3123599STom Lendacky ccp_crypto_ablkcipher_alg(struct crypto_tfm *tfm) 54d3123599STom Lendacky { 55d3123599STom Lendacky struct crypto_alg *alg = tfm->__crt_alg; 56d3123599STom Lendacky 57d3123599STom Lendacky return container_of(alg, struct ccp_crypto_ablkcipher_alg, alg); 58d3123599STom Lendacky } 59d3123599STom Lendacky 60d3123599STom Lendacky static inline struct ccp_crypto_ahash_alg * 61d3123599STom Lendacky ccp_crypto_ahash_alg(struct crypto_tfm *tfm) 62d3123599STom Lendacky { 63d3123599STom Lendacky struct crypto_alg *alg = tfm->__crt_alg; 64d3123599STom Lendacky struct ahash_alg *ahash_alg; 65d3123599STom Lendacky 66d3123599STom Lendacky ahash_alg = container_of(alg, struct ahash_alg, halg.base); 67d3123599STom Lendacky 68d3123599STom Lendacky return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg); 69d3123599STom Lendacky } 70d3123599STom Lendacky 71d3123599STom Lendacky 72d3123599STom Lendacky /***** AES related defines *****/ 73d3123599STom Lendacky struct ccp_aes_ctx { 74d3123599STom Lendacky /* Fallback cipher for XTS with unsupported unit sizes */ 75d3123599STom Lendacky struct crypto_ablkcipher *tfm_ablkcipher; 76d3123599STom Lendacky 77d3123599STom Lendacky /* Cipher used to generate CMAC K1/K2 keys */ 78d3123599STom Lendacky struct crypto_cipher *tfm_cipher; 79d3123599STom Lendacky 80d3123599STom Lendacky enum ccp_engine engine; 81d3123599STom Lendacky enum ccp_aes_type type; 82d3123599STom Lendacky enum ccp_aes_mode mode; 83d3123599STom Lendacky 84d3123599STom Lendacky struct scatterlist key_sg; 85d3123599STom Lendacky unsigned int key_len; 86d3123599STom Lendacky u8 key[AES_MAX_KEY_SIZE]; 87d3123599STom Lendacky 88d3123599STom Lendacky u8 nonce[CTR_RFC3686_NONCE_SIZE]; 89d3123599STom Lendacky 90d3123599STom Lendacky /* CMAC key structures */ 91d3123599STom Lendacky struct scatterlist k1_sg; 92d3123599STom Lendacky struct scatterlist k2_sg; 93d3123599STom Lendacky unsigned int kn_len; 94d3123599STom Lendacky u8 k1[AES_BLOCK_SIZE]; 95d3123599STom Lendacky u8 k2[AES_BLOCK_SIZE]; 96d3123599STom Lendacky }; 97d3123599STom Lendacky 98d3123599STom Lendacky struct ccp_aes_req_ctx { 99d3123599STom Lendacky struct scatterlist iv_sg; 100d3123599STom Lendacky u8 iv[AES_BLOCK_SIZE]; 101d3123599STom Lendacky 102d3123599STom Lendacky /* Fields used for RFC3686 requests */ 103d3123599STom Lendacky u8 *rfc3686_info; 104d3123599STom Lendacky u8 rfc3686_iv[AES_BLOCK_SIZE]; 105d3123599STom Lendacky 106d3123599STom Lendacky struct ccp_cmd cmd; 107d3123599STom Lendacky }; 108d3123599STom Lendacky 109d3123599STom Lendacky struct ccp_aes_cmac_req_ctx { 110d3123599STom Lendacky unsigned int null_msg; 111d3123599STom Lendacky unsigned int final; 112d3123599STom Lendacky 11381a59f00STom Lendacky struct scatterlist *src; 11481a59f00STom Lendacky unsigned int nbytes; 11581a59f00STom Lendacky 11681a59f00STom Lendacky u64 hash_cnt; 117d3123599STom Lendacky unsigned int hash_rem; 118d3123599STom Lendacky 119d3123599STom Lendacky struct sg_table data_sg; 120d3123599STom Lendacky 121d3123599STom Lendacky struct scatterlist iv_sg; 122d3123599STom Lendacky u8 iv[AES_BLOCK_SIZE]; 123d3123599STom Lendacky 124d3123599STom Lendacky struct scatterlist buf_sg; 125d3123599STom Lendacky unsigned int buf_count; 126d3123599STom Lendacky u8 buf[AES_BLOCK_SIZE]; 127d3123599STom Lendacky 128d3123599STom Lendacky struct scatterlist pad_sg; 129d3123599STom Lendacky unsigned int pad_count; 130d3123599STom Lendacky u8 pad[AES_BLOCK_SIZE]; 131d3123599STom Lendacky 132d3123599STom Lendacky struct ccp_cmd cmd; 133d3123599STom Lendacky }; 134d3123599STom Lendacky 135d3123599STom Lendacky /***** SHA related defines *****/ 136d3123599STom Lendacky #define MAX_SHA_CONTEXT_SIZE SHA256_DIGEST_SIZE 137d3123599STom Lendacky #define MAX_SHA_BLOCK_SIZE SHA256_BLOCK_SIZE 138d3123599STom Lendacky 139d3123599STom Lendacky struct ccp_sha_ctx { 140*c11baa02STom Lendacky struct scatterlist opad_sg; 141*c11baa02STom Lendacky unsigned int opad_count; 142*c11baa02STom Lendacky 143d3123599STom Lendacky unsigned int key_len; 144d3123599STom Lendacky u8 key[MAX_SHA_BLOCK_SIZE]; 145d3123599STom Lendacky u8 ipad[MAX_SHA_BLOCK_SIZE]; 146d3123599STom Lendacky u8 opad[MAX_SHA_BLOCK_SIZE]; 147*c11baa02STom Lendacky struct crypto_shash *hmac_tfm; 148d3123599STom Lendacky }; 149d3123599STom Lendacky 150d3123599STom Lendacky struct ccp_sha_req_ctx { 151d3123599STom Lendacky enum ccp_sha_type type; 152d3123599STom Lendacky 153d3123599STom Lendacky u64 msg_bits; 154d3123599STom Lendacky 155d3123599STom Lendacky unsigned int first; 156d3123599STom Lendacky unsigned int final; 157d3123599STom Lendacky 15881a59f00STom Lendacky struct scatterlist *src; 15981a59f00STom Lendacky unsigned int nbytes; 16081a59f00STom Lendacky 16181a59f00STom Lendacky u64 hash_cnt; 162d3123599STom Lendacky unsigned int hash_rem; 163d3123599STom Lendacky 164d3123599STom Lendacky struct sg_table data_sg; 165d3123599STom Lendacky 166d3123599STom Lendacky struct scatterlist ctx_sg; 167d3123599STom Lendacky u8 ctx[MAX_SHA_CONTEXT_SIZE]; 168d3123599STom Lendacky 169d3123599STom Lendacky struct scatterlist buf_sg; 170d3123599STom Lendacky unsigned int buf_count; 171d3123599STom Lendacky u8 buf[MAX_SHA_BLOCK_SIZE]; 172d3123599STom Lendacky 173d3123599STom Lendacky /* CCP driver command */ 174d3123599STom Lendacky struct ccp_cmd cmd; 175d3123599STom Lendacky }; 176d3123599STom Lendacky 177d3123599STom Lendacky /***** Common Context Structure *****/ 178d3123599STom Lendacky struct ccp_ctx { 179d3123599STom Lendacky int (*complete)(struct crypto_async_request *req, int ret); 180d3123599STom Lendacky 181d3123599STom Lendacky union { 182d3123599STom Lendacky struct ccp_aes_ctx aes; 183d3123599STom Lendacky struct ccp_sha_ctx sha; 184d3123599STom Lendacky } u; 185d3123599STom Lendacky }; 186d3123599STom Lendacky 187d3123599STom Lendacky int ccp_crypto_enqueue_request(struct crypto_async_request *req, 188d3123599STom Lendacky struct ccp_cmd *cmd); 189d3123599STom Lendacky struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table, 190d3123599STom Lendacky struct scatterlist *sg_add); 191d3123599STom Lendacky 192d3123599STom Lendacky int ccp_register_aes_algs(struct list_head *head); 193d3123599STom Lendacky int ccp_register_aes_cmac_algs(struct list_head *head); 194d3123599STom Lendacky int ccp_register_aes_xts_algs(struct list_head *head); 195d3123599STom Lendacky int ccp_register_sha_algs(struct list_head *head); 196d3123599STom Lendacky 197d3123599STom Lendacky #endif 198