ccp-crypto.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * AMD Cryptographic Coprocessor (CCP) crypto API support
  4. *
  5. * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Tom Lendacky <[email protected]>
  8. */
  9. #ifndef __CCP_CRYPTO_H__
  10. #define __CCP_CRYPTO_H__
  11. #include <linux/list.h>
  12. #include <linux/wait.h>
  13. #include <linux/ccp.h>
  14. #include <crypto/algapi.h>
  15. #include <crypto/aes.h>
  16. #include <crypto/internal/aead.h>
  17. #include <crypto/aead.h>
  18. #include <crypto/ctr.h>
  19. #include <crypto/hash.h>
  20. #include <crypto/sha1.h>
  21. #include <crypto/sha2.h>
  22. #include <crypto/akcipher.h>
  23. #include <crypto/skcipher.h>
  24. #include <crypto/internal/rsa.h>
  25. /* We want the module name in front of our messages */
  26. #undef pr_fmt
  27. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  28. #define CCP_LOG_LEVEL KERN_INFO
  29. #define CCP_CRA_PRIORITY 300
  30. struct ccp_crypto_skcipher_alg {
  31. struct list_head entry;
  32. u32 mode;
  33. struct skcipher_alg alg;
  34. };
  35. struct ccp_crypto_aead {
  36. struct list_head entry;
  37. u32 mode;
  38. struct aead_alg alg;
  39. };
  40. struct ccp_crypto_ahash_alg {
  41. struct list_head entry;
  42. const __be32 *init;
  43. u32 type;
  44. u32 mode;
  45. /* Child algorithm used for HMAC, CMAC, etc */
  46. char child_alg[CRYPTO_MAX_ALG_NAME];
  47. struct ahash_alg alg;
  48. };
  49. struct ccp_crypto_akcipher_alg {
  50. struct list_head entry;
  51. struct akcipher_alg alg;
  52. };
  53. static inline struct ccp_crypto_skcipher_alg *
  54. ccp_crypto_skcipher_alg(struct crypto_skcipher *tfm)
  55. {
  56. struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
  57. return container_of(alg, struct ccp_crypto_skcipher_alg, alg);
  58. }
  59. static inline struct ccp_crypto_ahash_alg *
  60. ccp_crypto_ahash_alg(struct crypto_tfm *tfm)
  61. {
  62. struct crypto_alg *alg = tfm->__crt_alg;
  63. struct ahash_alg *ahash_alg;
  64. ahash_alg = container_of(alg, struct ahash_alg, halg.base);
  65. return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg);
  66. }
  67. /***** AES related defines *****/
  68. struct ccp_aes_ctx {
  69. /* Fallback cipher for XTS with unsupported unit sizes */
  70. struct crypto_skcipher *tfm_skcipher;
  71. enum ccp_engine engine;
  72. enum ccp_aes_type type;
  73. enum ccp_aes_mode mode;
  74. struct scatterlist key_sg;
  75. unsigned int key_len;
  76. u8 key[AES_MAX_KEY_SIZE * 2];
  77. u8 nonce[CTR_RFC3686_NONCE_SIZE];
  78. /* CMAC key structures */
  79. struct scatterlist k1_sg;
  80. struct scatterlist k2_sg;
  81. unsigned int kn_len;
  82. u8 k1[AES_BLOCK_SIZE];
  83. u8 k2[AES_BLOCK_SIZE];
  84. };
  85. struct ccp_aes_req_ctx {
  86. struct scatterlist iv_sg;
  87. u8 iv[AES_BLOCK_SIZE];
  88. struct scatterlist tag_sg;
  89. u8 tag[AES_BLOCK_SIZE];
  90. /* Fields used for RFC3686 requests */
  91. u8 *rfc3686_info;
  92. u8 rfc3686_iv[AES_BLOCK_SIZE];
  93. struct ccp_cmd cmd;
  94. struct skcipher_request fallback_req; // keep at the end
  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