ccp-crypto.h 5.6 KB

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