ccp-crypto.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <linux/crypto.h>
  19. #include <crypto/algapi.h>
  20. #include <crypto/aes.h>
  21. #include <crypto/ctr.h>
  22. #include <crypto/hash.h>
  23. #include <crypto/sha.h>
  24. #define CCP_CRA_PRIORITY 300
  25. struct ccp_crypto_ablkcipher_alg {
  26. struct list_head entry;
  27. u32 mode;
  28. struct crypto_alg alg;
  29. };
  30. struct ccp_crypto_ahash_alg {
  31. struct list_head entry;
  32. const __be32 *init;
  33. u32 type;
  34. u32 mode;
  35. /* Child algorithm used for HMAC, CMAC, etc */
  36. char child_alg[CRYPTO_MAX_ALG_NAME];
  37. struct ahash_alg alg;
  38. };
  39. static inline struct ccp_crypto_ablkcipher_alg *
  40. ccp_crypto_ablkcipher_alg(struct crypto_tfm *tfm)
  41. {
  42. struct crypto_alg *alg = tfm->__crt_alg;
  43. return container_of(alg, struct ccp_crypto_ablkcipher_alg, alg);
  44. }
  45. static inline struct ccp_crypto_ahash_alg *
  46. ccp_crypto_ahash_alg(struct crypto_tfm *tfm)
  47. {
  48. struct crypto_alg *alg = tfm->__crt_alg;
  49. struct ahash_alg *ahash_alg;
  50. ahash_alg = container_of(alg, struct ahash_alg, halg.base);
  51. return container_of(ahash_alg, struct ccp_crypto_ahash_alg, alg);
  52. }
  53. /***** AES related defines *****/
  54. struct ccp_aes_ctx {
  55. /* Fallback cipher for XTS with unsupported unit sizes */
  56. struct crypto_ablkcipher *tfm_ablkcipher;
  57. /* Cipher used to generate CMAC K1/K2 keys */
  58. struct crypto_cipher *tfm_cipher;
  59. enum ccp_engine engine;
  60. enum ccp_aes_type type;
  61. enum ccp_aes_mode mode;
  62. struct scatterlist key_sg;
  63. unsigned int key_len;
  64. u8 key[AES_MAX_KEY_SIZE];
  65. u8 nonce[CTR_RFC3686_NONCE_SIZE];
  66. /* CMAC key structures */
  67. struct scatterlist k1_sg;
  68. struct scatterlist k2_sg;
  69. unsigned int kn_len;
  70. u8 k1[AES_BLOCK_SIZE];
  71. u8 k2[AES_BLOCK_SIZE];
  72. };
  73. struct ccp_aes_req_ctx {
  74. struct scatterlist iv_sg;
  75. u8 iv[AES_BLOCK_SIZE];
  76. /* Fields used for RFC3686 requests */
  77. u8 *rfc3686_info;
  78. u8 rfc3686_iv[AES_BLOCK_SIZE];
  79. struct ccp_cmd cmd;
  80. };
  81. struct ccp_aes_cmac_req_ctx {
  82. unsigned int null_msg;
  83. unsigned int final;
  84. struct scatterlist *src;
  85. unsigned int nbytes;
  86. u64 hash_cnt;
  87. unsigned int hash_rem;
  88. struct sg_table data_sg;
  89. struct scatterlist iv_sg;
  90. u8 iv[AES_BLOCK_SIZE];
  91. struct scatterlist buf_sg;
  92. unsigned int buf_count;
  93. u8 buf[AES_BLOCK_SIZE];
  94. struct scatterlist pad_sg;
  95. unsigned int pad_count;
  96. u8 pad[AES_BLOCK_SIZE];
  97. struct ccp_cmd cmd;
  98. };
  99. struct ccp_aes_cmac_exp_ctx {
  100. unsigned int null_msg;
  101. u8 iv[AES_BLOCK_SIZE];
  102. unsigned int buf_count;
  103. u8 buf[AES_BLOCK_SIZE];
  104. };
  105. /***** SHA related defines *****/
  106. #define MAX_SHA_CONTEXT_SIZE SHA256_DIGEST_SIZE
  107. #define MAX_SHA_BLOCK_SIZE SHA256_BLOCK_SIZE
  108. struct ccp_sha_ctx {
  109. struct scatterlist opad_sg;
  110. unsigned int opad_count;
  111. unsigned int key_len;
  112. u8 key[MAX_SHA_BLOCK_SIZE];
  113. u8 ipad[MAX_SHA_BLOCK_SIZE];
  114. u8 opad[MAX_SHA_BLOCK_SIZE];
  115. struct crypto_shash *hmac_tfm;
  116. };
  117. struct ccp_sha_req_ctx {
  118. enum ccp_sha_type type;
  119. u64 msg_bits;
  120. unsigned int first;
  121. unsigned int final;
  122. struct scatterlist *src;
  123. unsigned int nbytes;
  124. u64 hash_cnt;
  125. unsigned int hash_rem;
  126. struct sg_table data_sg;
  127. struct scatterlist ctx_sg;
  128. u8 ctx[MAX_SHA_CONTEXT_SIZE];
  129. struct scatterlist buf_sg;
  130. unsigned int buf_count;
  131. u8 buf[MAX_SHA_BLOCK_SIZE];
  132. /* CCP driver command */
  133. struct ccp_cmd cmd;
  134. };
  135. struct ccp_sha_exp_ctx {
  136. enum ccp_sha_type type;
  137. u64 msg_bits;
  138. unsigned int first;
  139. u8 ctx[MAX_SHA_CONTEXT_SIZE];
  140. unsigned int buf_count;
  141. u8 buf[MAX_SHA_BLOCK_SIZE];
  142. };
  143. /***** Common Context Structure *****/
  144. struct ccp_ctx {
  145. int (*complete)(struct crypto_async_request *req, int ret);
  146. union {
  147. struct ccp_aes_ctx aes;
  148. struct ccp_sha_ctx sha;
  149. } u;
  150. };
  151. int ccp_crypto_enqueue_request(struct crypto_async_request *req,
  152. struct ccp_cmd *cmd);
  153. struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table,
  154. struct scatterlist *sg_add);
  155. int ccp_register_aes_algs(struct list_head *head);
  156. int ccp_register_aes_cmac_algs(struct list_head *head);
  157. int ccp_register_aes_xts_algs(struct list_head *head);
  158. int ccp_register_sha_algs(struct list_head *head);
  159. #endif