sun4i-ss.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * sun4i-ss.h - hardware cryptographic accelerator for Allwinner A20 SoC
  4. *
  5. * Copyright (C) 2013-2015 Corentin LABBE <[email protected]>
  6. *
  7. * Support AES cipher with 128,192,256 bits keysize.
  8. * Support MD5 and SHA1 hash algorithms.
  9. * Support DES and 3DES
  10. *
  11. * You could find the datasheet in Documentation/arm/sunxi.rst
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/crypto.h>
  15. #include <linux/io.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/reset.h>
  20. #include <crypto/scatterwalk.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/pm_runtime.h>
  25. #include <crypto/md5.h>
  26. #include <crypto/skcipher.h>
  27. #include <crypto/sha1.h>
  28. #include <crypto/hash.h>
  29. #include <crypto/internal/hash.h>
  30. #include <crypto/internal/skcipher.h>
  31. #include <crypto/aes.h>
  32. #include <crypto/internal/des.h>
  33. #include <crypto/internal/rng.h>
  34. #include <crypto/rng.h>
  35. #define SS_CTL 0x00
  36. #define SS_KEY0 0x04
  37. #define SS_KEY1 0x08
  38. #define SS_KEY2 0x0C
  39. #define SS_KEY3 0x10
  40. #define SS_KEY4 0x14
  41. #define SS_KEY5 0x18
  42. #define SS_KEY6 0x1C
  43. #define SS_KEY7 0x20
  44. #define SS_IV0 0x24
  45. #define SS_IV1 0x28
  46. #define SS_IV2 0x2C
  47. #define SS_IV3 0x30
  48. #define SS_FCSR 0x44
  49. #define SS_MD0 0x4C
  50. #define SS_MD1 0x50
  51. #define SS_MD2 0x54
  52. #define SS_MD3 0x58
  53. #define SS_MD4 0x5C
  54. #define SS_RXFIFO 0x200
  55. #define SS_TXFIFO 0x204
  56. /* SS_CTL configuration values */
  57. /* PRNG generator mode - bit 15 */
  58. #define SS_PRNG_ONESHOT (0 << 15)
  59. #define SS_PRNG_CONTINUE (1 << 15)
  60. /* IV mode for hash */
  61. #define SS_IV_ARBITRARY (1 << 14)
  62. /* SS operation mode - bits 12-13 */
  63. #define SS_ECB (0 << 12)
  64. #define SS_CBC (1 << 12)
  65. #define SS_CTS (3 << 12)
  66. /* Counter width for CNT mode - bits 10-11 */
  67. #define SS_CNT_16BITS (0 << 10)
  68. #define SS_CNT_32BITS (1 << 10)
  69. #define SS_CNT_64BITS (2 << 10)
  70. /* Key size for AES - bits 8-9 */
  71. #define SS_AES_128BITS (0 << 8)
  72. #define SS_AES_192BITS (1 << 8)
  73. #define SS_AES_256BITS (2 << 8)
  74. /* Operation direction - bit 7 */
  75. #define SS_ENCRYPTION (0 << 7)
  76. #define SS_DECRYPTION (1 << 7)
  77. /* SS Method - bits 4-6 */
  78. #define SS_OP_AES (0 << 4)
  79. #define SS_OP_DES (1 << 4)
  80. #define SS_OP_3DES (2 << 4)
  81. #define SS_OP_SHA1 (3 << 4)
  82. #define SS_OP_MD5 (4 << 4)
  83. #define SS_OP_PRNG (5 << 4)
  84. /* Data end bit - bit 2 */
  85. #define SS_DATA_END (1 << 2)
  86. /* PRNG start bit - bit 1 */
  87. #define SS_PRNG_START (1 << 1)
  88. /* SS Enable bit - bit 0 */
  89. #define SS_DISABLED (0 << 0)
  90. #define SS_ENABLED (1 << 0)
  91. /* SS_FCSR configuration values */
  92. /* RX FIFO status - bit 30 */
  93. #define SS_RXFIFO_FREE (1 << 30)
  94. /* RX FIFO empty spaces - bits 24-29 */
  95. #define SS_RXFIFO_SPACES(val) (((val) >> 24) & 0x3f)
  96. /* TX FIFO status - bit 22 */
  97. #define SS_TXFIFO_AVAILABLE (1 << 22)
  98. /* TX FIFO available spaces - bits 16-21 */
  99. #define SS_TXFIFO_SPACES(val) (((val) >> 16) & 0x3f)
  100. #define SS_RX_MAX 32
  101. #define SS_RX_DEFAULT SS_RX_MAX
  102. #define SS_TX_MAX 33
  103. #define SS_RXFIFO_EMP_INT_PENDING (1 << 10)
  104. #define SS_TXFIFO_AVA_INT_PENDING (1 << 8)
  105. #define SS_RXFIFO_EMP_INT_ENABLE (1 << 2)
  106. #define SS_TXFIFO_AVA_INT_ENABLE (1 << 0)
  107. #define SS_SEED_LEN 192
  108. #define SS_DATA_LEN 160
  109. /*
  110. * struct ss_variant - Describe SS hardware variant
  111. * @sha1_in_be: The SHA1 digest is given by SS in BE, and so need to be inverted.
  112. */
  113. struct ss_variant {
  114. bool sha1_in_be;
  115. };
  116. struct sun4i_ss_ctx {
  117. const struct ss_variant *variant;
  118. void __iomem *base;
  119. int irq;
  120. struct clk *busclk;
  121. struct clk *ssclk;
  122. struct reset_control *reset;
  123. struct device *dev;
  124. struct resource *res;
  125. char buf[4 * SS_RX_MAX];/* buffer for linearize SG src */
  126. char bufo[4 * SS_TX_MAX]; /* buffer for linearize SG dst */
  127. spinlock_t slock; /* control the use of the device */
  128. #ifdef CONFIG_CRYPTO_DEV_SUN4I_SS_PRNG
  129. u32 seed[SS_SEED_LEN / BITS_PER_LONG];
  130. #endif
  131. struct dentry *dbgfs_dir;
  132. struct dentry *dbgfs_stats;
  133. };
  134. struct sun4i_ss_alg_template {
  135. u32 type;
  136. u32 mode;
  137. union {
  138. struct skcipher_alg crypto;
  139. struct ahash_alg hash;
  140. struct rng_alg rng;
  141. } alg;
  142. struct sun4i_ss_ctx *ss;
  143. unsigned long stat_req;
  144. unsigned long stat_fb;
  145. unsigned long stat_bytes;
  146. unsigned long stat_opti;
  147. };
  148. struct sun4i_tfm_ctx {
  149. u32 key[AES_MAX_KEY_SIZE / 4];/* divided by sizeof(u32) */
  150. u32 keylen;
  151. u32 keymode;
  152. struct sun4i_ss_ctx *ss;
  153. struct crypto_skcipher *fallback_tfm;
  154. };
  155. struct sun4i_cipher_req_ctx {
  156. u32 mode;
  157. u8 backup_iv[AES_BLOCK_SIZE];
  158. struct skcipher_request fallback_req; // keep at the end
  159. };
  160. struct sun4i_req_ctx {
  161. u32 mode;
  162. u64 byte_count; /* number of bytes "uploaded" to the device */
  163. u32 hash[5]; /* for storing SS_IVx register */
  164. char buf[64];
  165. unsigned int len;
  166. int flags;
  167. };
  168. int sun4i_hash_crainit(struct crypto_tfm *tfm);
  169. void sun4i_hash_craexit(struct crypto_tfm *tfm);
  170. int sun4i_hash_init(struct ahash_request *areq);
  171. int sun4i_hash_update(struct ahash_request *areq);
  172. int sun4i_hash_final(struct ahash_request *areq);
  173. int sun4i_hash_finup(struct ahash_request *areq);
  174. int sun4i_hash_digest(struct ahash_request *areq);
  175. int sun4i_hash_export_md5(struct ahash_request *areq, void *out);
  176. int sun4i_hash_import_md5(struct ahash_request *areq, const void *in);
  177. int sun4i_hash_export_sha1(struct ahash_request *areq, void *out);
  178. int sun4i_hash_import_sha1(struct ahash_request *areq, const void *in);
  179. int sun4i_ss_cbc_aes_encrypt(struct skcipher_request *areq);
  180. int sun4i_ss_cbc_aes_decrypt(struct skcipher_request *areq);
  181. int sun4i_ss_ecb_aes_encrypt(struct skcipher_request *areq);
  182. int sun4i_ss_ecb_aes_decrypt(struct skcipher_request *areq);
  183. int sun4i_ss_cbc_des_encrypt(struct skcipher_request *areq);
  184. int sun4i_ss_cbc_des_decrypt(struct skcipher_request *areq);
  185. int sun4i_ss_ecb_des_encrypt(struct skcipher_request *areq);
  186. int sun4i_ss_ecb_des_decrypt(struct skcipher_request *areq);
  187. int sun4i_ss_cbc_des3_encrypt(struct skcipher_request *areq);
  188. int sun4i_ss_cbc_des3_decrypt(struct skcipher_request *areq);
  189. int sun4i_ss_ecb_des3_encrypt(struct skcipher_request *areq);
  190. int sun4i_ss_ecb_des3_decrypt(struct skcipher_request *areq);
  191. int sun4i_ss_cipher_init(struct crypto_tfm *tfm);
  192. void sun4i_ss_cipher_exit(struct crypto_tfm *tfm);
  193. int sun4i_ss_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
  194. unsigned int keylen);
  195. int sun4i_ss_des_setkey(struct crypto_skcipher *tfm, const u8 *key,
  196. unsigned int keylen);
  197. int sun4i_ss_des3_setkey(struct crypto_skcipher *tfm, const u8 *key,
  198. unsigned int keylen);
  199. int sun4i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
  200. unsigned int slen, u8 *dst, unsigned int dlen);
  201. int sun4i_ss_prng_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen);