ccp-crypto.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) crypto API support
  3. *
  4. * Copyright (C) 2013 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. #define CCP_LOG_LEVEL KERN_INFO
  26. #define CCP_CRA_PRIORITY 300
  27. struct ccp_crypto_ablkcipher_alg {
  28. struct list_head entry;
  29. u32 mode;
  30. struct crypto_alg alg;
  31. };
  32. struct ccp_crypto_aead {
  33. struct list_head entry;
  34. u32 mode;
  35. struct aead_alg alg;
  36. };
  37. struct ccp_crypto_ahash_alg {
  38. struct list_head entry;
  39. const __be32 *init;
  40. u32 type;
  41. u32 mode;
  42. /* Child algorithm used for HMAC, CMAC, etc */
  43. char child_alg[CRYPTO_MAX_ALG_NAME];
  44. struct ahash_alg alg;
  45. };
  46. static inline struct ccp_crypto_ablkcipher_alg *
  47. ccp_crypto_ablkcipher_alg(struct crypto_tfm *tfm)
  48. {
  49. struct crypto_alg *alg = tfm->__crt_alg;
  50. return container_of(alg, struct ccp_crypto_ablkcipher_alg, alg);
  51. }
  52. static inline struct ccp_crypto_ahash_alg *
  53. ccp_crypto_ahash_alg(struct crypto_tfm *tfm)
  54. {
  55. struct crypto_alg *alg = tfm->__crt_alg;
  56. struct ahash_alg *ahash_alg;
  57. ahash_alg = container_of(alg, struct ahash_alg, halg.base);
  58. return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg);
  59. }
  60. /***** AES related defines *****/
  61. struct ccp_aes_ctx {
  62. /* Fallback cipher for XTS with unsupported unit sizes */
  63. struct crypto_skcipher *tfm_skcipher;
  64. /* Cipher used to generate CMAC K1/K2 keys */
  65. struct crypto_cipher *tfm_cipher;
  66. enum ccp_engine engine;
  67. enum ccp_aes_type type;
  68. enum ccp_aes_mode mode;
  69. struct scatterlist key_sg;
  70. unsigned int key_len;
  71. u8 key[AES_MAX_KEY_SIZE];
  72. u8 nonce[CTR_RFC3686_NONCE_SIZE];
  73. /* CMAC key structures */
  74. struct scatterlist k1_sg;
  75. struct scatterlist k2_sg;
  76. unsigned int kn_len;
  77. u8 k1[AES_BLOCK_SIZE];
  78. u8 k2[AES_BLOCK_SIZE];
  79. };
  80. struct ccp_aes_req_ctx {
  81. struct scatterlist iv_sg;
  82. u8 iv[AES_BLOCK_SIZE];
  83. struct scatterlist tag_sg;
  84. u8 tag[AES_BLOCK_SIZE];
  85. /* Fields used for RFC3686 requests */
  86. u8 *rfc3686_info;
  87. u8 rfc3686_iv[AES_BLOCK_SIZE];
  88. struct ccp_cmd cmd;
  89. };
  90. struct ccp_aes_cmac_req_ctx {
  91. unsigned int null_msg;
  92. unsigned int final;
  93. struct scatterlist *src;
  94. unsigned int nbytes;
  95. u64 hash_cnt;
  96. unsigned int hash_rem;
  97. struct sg_table data_sg;
  98. struct scatterlist iv_sg;
  99. u8 iv[AES_BLOCK_SIZE];
  100. struct scatterlist buf_sg;
  101. unsigned int buf_count;
  102. u8 buf[AES_BLOCK_SIZE];
  103. struct scatterlist pad_sg;
  104. unsigned int pad_count;
  105. u8 pad[AES_BLOCK_SIZE];
  106. struct ccp_cmd cmd;
  107. };
  108. struct ccp_aes_cmac_exp_ctx {
  109. unsigned int null_msg;
  110. u8 iv[AES_BLOCK_SIZE];
  111. unsigned int buf_count;
  112. u8 buf[AES_BLOCK_SIZE];
  113. };
  114. /***** 3DES related defines *****/
  115. struct ccp_des3_ctx {
  116. enum ccp_engine engine;
  117. enum ccp_des3_type type;
  118. enum ccp_des3_mode mode;
  119. struct scatterlist key_sg;
  120. unsigned int key_len;
  121. u8 key[AES_MAX_KEY_SIZE];
  122. };
  123. struct ccp_des3_req_ctx {
  124. struct scatterlist iv_sg;
  125. u8 iv[AES_BLOCK_SIZE];
  126. struct ccp_cmd cmd;
  127. };
  128. /* SHA-related defines
  129. * These values must be large enough to accommodate any variant
  130. */
  131. #define MAX_SHA_CONTEXT_SIZE SHA512_DIGEST_SIZE
  132. #define MAX_SHA_BLOCK_SIZE SHA512_BLOCK_SIZE
  133. struct ccp_sha_ctx {
  134. struct scatterlist opad_sg;
  135. unsigned int opad_count;
  136. unsigned int key_len;
  137. u8 key[MAX_SHA_BLOCK_SIZE];
  138. u8 ipad[MAX_SHA_BLOCK_SIZE];
  139. u8 opad[MAX_SHA_BLOCK_SIZE];
  140. struct crypto_shash *hmac_tfm;
  141. };
  142. struct ccp_sha_req_ctx {
  143. enum ccp_sha_type type;
  144. u64 msg_bits;
  145. unsigned int first;
  146. unsigned int final;
  147. struct scatterlist *src;
  148. unsigned int nbytes;
  149. u64 hash_cnt;
  150. unsigned int hash_rem;
  151. struct sg_table data_sg;
  152. struct scatterlist ctx_sg;
  153. u8 ctx[MAX_SHA_CONTEXT_SIZE];
  154. struct scatterlist buf_sg;
  155. unsigned int buf_count;
  156. u8 buf[MAX_SHA_BLOCK_SIZE];
  157. /* CCP driver command */
  158. struct ccp_cmd cmd;
  159. };
  160. struct ccp_sha_exp_ctx {
  161. enum ccp_sha_type type;
  162. u64 msg_bits;
  163. unsigned int first;
  164. u8 ctx[MAX_SHA_CONTEXT_SIZE];
  165. unsigned int buf_count;
  166. u8 buf[MAX_SHA_BLOCK_SIZE];
  167. };
  168. /***** Common Context Structure *****/
  169. struct ccp_ctx {
  170. int (*complete)(struct crypto_async_request *req, int ret);
  171. union {
  172. struct ccp_aes_ctx aes;
  173. struct ccp_sha_ctx sha;
  174. struct ccp_des3_ctx des3;
  175. } u;
  176. };
  177. int ccp_crypto_enqueue_request(struct crypto_async_request *req,
  178. struct ccp_cmd *cmd);
  179. struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table,
  180. struct scatterlist *sg_add);
  181. int ccp_register_aes_algs(struct list_head *head);
  182. int ccp_register_aes_cmac_algs(struct list_head *head);
  183. int ccp_register_aes_xts_algs(struct list_head *head);
  184. int ccp_register_aes_aeads(struct list_head *head);
  185. int ccp_register_sha_algs(struct list_head *head);
  186. int ccp_register_des3_algs(struct list_head *head);
  187. #endif