ace_sha.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Advanced Crypto Engine - SHA Firmware
  3. * Copyright (c) 2012 Samsung Electronics
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include "ace_sha.h"
  9. #ifdef CONFIG_SHA_HW_ACCEL
  10. #include <u-boot/sha256.h>
  11. #include <u-boot/sha1.h>
  12. #include <linux/errno.h>
  13. /* SHA1 value for the message of zero length */
  14. static const unsigned char sha1_digest_emptymsg[SHA1_SUM_LEN] = {
  15. 0xDA, 0x39, 0xA3, 0xEE, 0x5E, 0x6B, 0x4B, 0x0D,
  16. 0x32, 0x55, 0xBF, 0xFF, 0x95, 0x60, 0x18, 0x90,
  17. 0xAF, 0xD8, 0x07, 0x09};
  18. /* SHA256 value for the message of zero length */
  19. static const unsigned char sha256_digest_emptymsg[SHA256_SUM_LEN] = {
  20. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  21. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  22. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  23. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55};
  24. int ace_sha_hash_digest(const unsigned char *pbuf, unsigned int buf_len,
  25. unsigned char *pout, unsigned int hash_type)
  26. {
  27. unsigned int i, reg, len;
  28. unsigned int *pdigest;
  29. struct exynos_ace_sfr *ace_sha_reg =
  30. (struct exynos_ace_sfr *)samsung_get_base_ace_sfr();
  31. if (buf_len == 0) {
  32. /* ACE H/W cannot compute hash value for empty string */
  33. if (hash_type == ACE_SHA_TYPE_SHA1)
  34. memcpy(pout, sha1_digest_emptymsg, SHA1_SUM_LEN);
  35. else
  36. memcpy(pout, sha256_digest_emptymsg, SHA256_SUM_LEN);
  37. return 0;
  38. }
  39. /* Flush HRDMA */
  40. writel(ACE_FC_HRDMACFLUSH_ON, &ace_sha_reg->fc_hrdmac);
  41. writel(ACE_FC_HRDMACFLUSH_OFF, &ace_sha_reg->fc_hrdmac);
  42. /* Set byte swap of data in */
  43. writel(ACE_HASH_SWAPDI_ON | ACE_HASH_SWAPDO_ON | ACE_HASH_SWAPIV_ON,
  44. &ace_sha_reg->hash_byteswap);
  45. /* Select Hash input mux as external source */
  46. reg = readl(&ace_sha_reg->fc_fifoctrl);
  47. reg = (reg & ~ACE_FC_SELHASH_MASK) | ACE_FC_SELHASH_EXOUT;
  48. writel(reg, &ace_sha_reg->fc_fifoctrl);
  49. /* Set Hash as SHA1 or SHA256 and start Hash engine */
  50. reg = (hash_type == ACE_SHA_TYPE_SHA1) ?
  51. ACE_HASH_ENGSEL_SHA1HASH : ACE_HASH_ENGSEL_SHA256HASH;
  52. reg |= ACE_HASH_STARTBIT_ON;
  53. writel(reg, &ace_sha_reg->hash_control);
  54. /* Enable FIFO mode */
  55. writel(ACE_HASH_FIFO_ON, &ace_sha_reg->hash_fifo_mode);
  56. /* Set message length */
  57. writel(buf_len, &ace_sha_reg->hash_msgsize_low);
  58. writel(0, &ace_sha_reg->hash_msgsize_high);
  59. /* Set HRDMA */
  60. writel((unsigned int)pbuf, &ace_sha_reg->fc_hrdmas);
  61. writel(buf_len, &ace_sha_reg->fc_hrdmal);
  62. while ((readl(&ace_sha_reg->hash_status) & ACE_HASH_MSGDONE_MASK) ==
  63. ACE_HASH_MSGDONE_OFF) {
  64. /*
  65. * PRNG error bit goes HIGH if a PRNG request occurs without
  66. * a complete seed setup. We are using this bit to check h/w
  67. * fault because proper setup is not expected in that case.
  68. */
  69. if ((readl(&ace_sha_reg->hash_status)
  70. & ACE_HASH_PRNGERROR_MASK) == ACE_HASH_PRNGERROR_ON)
  71. return -EBUSY;
  72. }
  73. /* Clear MSG_DONE bit */
  74. writel(ACE_HASH_MSGDONE_ON, &ace_sha_reg->hash_status);
  75. /* Read hash result */
  76. pdigest = (unsigned int *)pout;
  77. len = (hash_type == ACE_SHA_TYPE_SHA1) ? SHA1_SUM_LEN : SHA256_SUM_LEN;
  78. for (i = 0; i < len / 4; i++)
  79. pdigest[i] = readl(&ace_sha_reg->hash_result[i]);
  80. /* Clear HRDMA pending bit */
  81. writel(ACE_FC_HRDMA, &ace_sha_reg->fc_intpend);
  82. return 0;
  83. }
  84. void hw_sha256(const unsigned char *pbuf, unsigned int buf_len,
  85. unsigned char *pout, unsigned int chunk_size)
  86. {
  87. if (ace_sha_hash_digest(pbuf, buf_len, pout, ACE_SHA_TYPE_SHA256))
  88. debug("ACE was not setup properly or it is faulty\n");
  89. }
  90. void hw_sha1(const unsigned char *pbuf, unsigned int buf_len,
  91. unsigned char *pout, unsigned int chunk_size)
  92. {
  93. if (ace_sha_hash_digest(pbuf, buf_len, pout, ACE_SHA_TYPE_SHA1))
  94. debug("ACE was not setup properly or it is faulty\n");
  95. }
  96. #endif /* CONFIG_SHA_HW_ACCEL */
  97. #ifdef CONFIG_LIB_HW_RAND
  98. static unsigned int seed_done;
  99. void srand(unsigned int seed)
  100. {
  101. struct exynos_ace_sfr *reg =
  102. (struct exynos_ace_sfr *)samsung_get_base_ace_sfr();
  103. int i, status;
  104. /* Seed data */
  105. for (i = 0; i < ACE_HASH_PRNG_REG_NUM; i++)
  106. writel(seed << i, &reg->hash_seed[i]);
  107. /* Wait for seed setup done */
  108. while (1) {
  109. status = readl(&reg->hash_status);
  110. if ((status & ACE_HASH_SEEDSETTING_MASK) ||
  111. (status & ACE_HASH_PRNGERROR_MASK))
  112. break;
  113. }
  114. seed_done = 1;
  115. }
  116. unsigned int rand(void)
  117. {
  118. struct exynos_ace_sfr *reg =
  119. (struct exynos_ace_sfr *)samsung_get_base_ace_sfr();
  120. int i, status;
  121. unsigned int seed = (unsigned int)&status;
  122. unsigned int ret = 0;
  123. if (!seed_done)
  124. srand(seed);
  125. /* Start PRNG */
  126. writel(ACE_HASH_ENGSEL_PRNG | ACE_HASH_STARTBIT_ON, &reg->hash_control);
  127. /* Wait for PRNG done */
  128. while (1) {
  129. status = readl(&reg->hash_status);
  130. if (status & ACE_HASH_PRNGDONE_MASK)
  131. break;
  132. if (status & ACE_HASH_PRNGERROR_MASK) {
  133. seed_done = 0;
  134. return 0;
  135. }
  136. }
  137. /* Clear Done IRQ */
  138. writel(ACE_HASH_PRNGDONE_MASK, &reg->hash_status);
  139. /* Read a PRNG result */
  140. for (i = 0; i < ACE_HASH_PRNG_REG_NUM; i++)
  141. ret += readl(&reg->hash_prng[i]);
  142. seed_done = 0;
  143. return ret;
  144. }
  145. unsigned int rand_r(unsigned int *seedp)
  146. {
  147. srand(*seedp);
  148. return rand();
  149. }
  150. #endif /* CONFIG_LIB_HW_RAND */