sun4i-ss.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * sun4i-ss.h - hardware cryptographic accelerator for Allwinner A20 SoC
  3. *
  4. * Copyright (C) 2013-2015 Corentin LABBE <clabbe.montjoie@gmail.com>
  5. *
  6. * Support AES cipher with 128,192,256 bits keysize.
  7. * Support MD5 and SHA1 hash algorithms.
  8. * Support DES and 3DES
  9. *
  10. * You could find the datasheet in Documentation/arm/sunxi/README
  11. *
  12. * Licensed under the GPL-2.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/crypto.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/reset.h>
  21. #include <crypto/scatterwalk.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <crypto/md5.h>
  26. #include <crypto/sha.h>
  27. #include <crypto/hash.h>
  28. #include <crypto/internal/hash.h>
  29. #include <crypto/aes.h>
  30. #include <crypto/des.h>
  31. #include <crypto/internal/rng.h>
  32. #define SS_CTL 0x00
  33. #define SS_KEY0 0x04
  34. #define SS_KEY1 0x08
  35. #define SS_KEY2 0x0C
  36. #define SS_KEY3 0x10
  37. #define SS_KEY4 0x14
  38. #define SS_KEY5 0x18
  39. #define SS_KEY6 0x1C
  40. #define SS_KEY7 0x20
  41. #define SS_IV0 0x24
  42. #define SS_IV1 0x28
  43. #define SS_IV2 0x2C
  44. #define SS_IV3 0x30
  45. #define SS_FCSR 0x44
  46. #define SS_MD0 0x4C
  47. #define SS_MD1 0x50
  48. #define SS_MD2 0x54
  49. #define SS_MD3 0x58
  50. #define SS_MD4 0x5C
  51. #define SS_RXFIFO 0x200
  52. #define SS_TXFIFO 0x204
  53. /* SS_CTL configuration values */
  54. /* PRNG generator mode - bit 15 */
  55. #define SS_PRNG_ONESHOT (0 << 15)
  56. #define SS_PRNG_CONTINUE (1 << 15)
  57. /* IV mode for hash */
  58. #define SS_IV_ARBITRARY (1 << 14)
  59. /* SS operation mode - bits 12-13 */
  60. #define SS_ECB (0 << 12)
  61. #define SS_CBC (1 << 12)
  62. #define SS_CTS (3 << 12)
  63. /* Counter width for CNT mode - bits 10-11 */
  64. #define SS_CNT_16BITS (0 << 10)
  65. #define SS_CNT_32BITS (1 << 10)
  66. #define SS_CNT_64BITS (2 << 10)
  67. /* Key size for AES - bits 8-9 */
  68. #define SS_AES_128BITS (0 << 8)
  69. #define SS_AES_192BITS (1 << 8)
  70. #define SS_AES_256BITS (2 << 8)
  71. /* Operation direction - bit 7 */
  72. #define SS_ENCRYPTION (0 << 7)
  73. #define SS_DECRYPTION (1 << 7)
  74. /* SS Method - bits 4-6 */
  75. #define SS_OP_AES (0 << 4)
  76. #define SS_OP_DES (1 << 4)
  77. #define SS_OP_3DES (2 << 4)
  78. #define SS_OP_SHA1 (3 << 4)
  79. #define SS_OP_MD5 (4 << 4)
  80. #define SS_OP_PRNG (5 << 4)
  81. /* Data end bit - bit 2 */
  82. #define SS_DATA_END (1 << 2)
  83. /* PRNG start bit - bit 1 */
  84. #define SS_PRNG_START (1 << 1)
  85. /* SS Enable bit - bit 0 */
  86. #define SS_DISABLED (0 << 0)
  87. #define SS_ENABLED (1 << 0)
  88. /* SS_FCSR configuration values */
  89. /* RX FIFO status - bit 30 */
  90. #define SS_RXFIFO_FREE (1 << 30)
  91. /* RX FIFO empty spaces - bits 24-29 */
  92. #define SS_RXFIFO_SPACES(val) (((val) >> 24) & 0x3f)
  93. /* TX FIFO status - bit 22 */
  94. #define SS_TXFIFO_AVAILABLE (1 << 22)
  95. /* TX FIFO available spaces - bits 16-21 */
  96. #define SS_TXFIFO_SPACES(val) (((val) >> 16) & 0x3f)
  97. #define SS_RX_MAX 32
  98. #define SS_RX_DEFAULT SS_RX_MAX
  99. #define SS_TX_MAX 33
  100. #define SS_RXFIFO_EMP_INT_PENDING (1 << 10)
  101. #define SS_TXFIFO_AVA_INT_PENDING (1 << 8)
  102. #define SS_RXFIFO_EMP_INT_ENABLE (1 << 2)
  103. #define SS_TXFIFO_AVA_INT_ENABLE (1 << 0)
  104. struct sun4i_ss_ctx {
  105. void __iomem *base;
  106. int irq;
  107. struct clk *busclk;
  108. struct clk *ssclk;
  109. struct reset_control *reset;
  110. struct device *dev;
  111. struct resource *res;
  112. spinlock_t slock; /* control the use of the device */
  113. };
  114. struct sun4i_ss_alg_template {
  115. u32 type;
  116. u32 mode;
  117. union {
  118. struct crypto_alg crypto;
  119. struct ahash_alg hash;
  120. } alg;
  121. struct sun4i_ss_ctx *ss;
  122. };
  123. struct sun4i_tfm_ctx {
  124. u32 key[AES_MAX_KEY_SIZE / 4];/* divided by sizeof(u32) */
  125. u32 keylen;
  126. u32 keymode;
  127. struct sun4i_ss_ctx *ss;
  128. };
  129. struct sun4i_cipher_req_ctx {
  130. u32 mode;
  131. };
  132. struct sun4i_req_ctx {
  133. u32 mode;
  134. u64 byte_count; /* number of bytes "uploaded" to the device */
  135. u32 hash[5]; /* for storing SS_IVx register */
  136. char buf[64];
  137. unsigned int len;
  138. struct sun4i_ss_ctx *ss;
  139. };
  140. int sun4i_hash_crainit(struct crypto_tfm *tfm);
  141. int sun4i_hash_init(struct ahash_request *areq);
  142. int sun4i_hash_update(struct ahash_request *areq);
  143. int sun4i_hash_final(struct ahash_request *areq);
  144. int sun4i_hash_finup(struct ahash_request *areq);
  145. int sun4i_hash_digest(struct ahash_request *areq);
  146. int sun4i_hash_export_md5(struct ahash_request *areq, void *out);
  147. int sun4i_hash_import_md5(struct ahash_request *areq, const void *in);
  148. int sun4i_hash_export_sha1(struct ahash_request *areq, void *out);
  149. int sun4i_hash_import_sha1(struct ahash_request *areq, const void *in);
  150. int sun4i_ss_cbc_aes_encrypt(struct ablkcipher_request *areq);
  151. int sun4i_ss_cbc_aes_decrypt(struct ablkcipher_request *areq);
  152. int sun4i_ss_ecb_aes_encrypt(struct ablkcipher_request *areq);
  153. int sun4i_ss_ecb_aes_decrypt(struct ablkcipher_request *areq);
  154. int sun4i_ss_cbc_des_encrypt(struct ablkcipher_request *areq);
  155. int sun4i_ss_cbc_des_decrypt(struct ablkcipher_request *areq);
  156. int sun4i_ss_ecb_des_encrypt(struct ablkcipher_request *areq);
  157. int sun4i_ss_ecb_des_decrypt(struct ablkcipher_request *areq);
  158. int sun4i_ss_cbc_des3_encrypt(struct ablkcipher_request *areq);
  159. int sun4i_ss_cbc_des3_decrypt(struct ablkcipher_request *areq);
  160. int sun4i_ss_ecb_des3_encrypt(struct ablkcipher_request *areq);
  161. int sun4i_ss_ecb_des3_decrypt(struct ablkcipher_request *areq);
  162. int sun4i_ss_cipher_init(struct crypto_tfm *tfm);
  163. int sun4i_ss_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  164. unsigned int keylen);
  165. int sun4i_ss_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  166. unsigned int keylen);
  167. int sun4i_ss_des3_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  168. unsigned int keylen);