ccp-crypto.h 4.3 KB

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