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