ccp-crypto.h 5.5 KB

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