ace_sha.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Advanced Crypto Engine - SHA Firmware
  3. * Copyright (c) 2012 Samsung Electronics
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #include <common.h>
  21. #include <sha256.h>
  22. #include <sha1.h>
  23. #include <asm/errno.h>
  24. #include "ace_sha.h"
  25. /* SHA1 value for the message of zero length */
  26. static const unsigned char sha1_digest_emptymsg[SHA1_SUM_LEN] = {
  27. 0xDA, 0x39, 0xA3, 0xEE, 0x5E, 0x6B, 0x4B, 0x0D,
  28. 0x32, 0x55, 0xBF, 0xFF, 0x95, 0x60, 0x18, 0x90,
  29. 0xAF, 0xD8, 0x07, 0x09};
  30. /* SHA256 value for the message of zero length */
  31. static const unsigned char sha256_digest_emptymsg[SHA256_SUM_LEN] = {
  32. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  33. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  34. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  35. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55};
  36. int ace_sha_hash_digest(const unsigned char *pbuf, unsigned int buf_len,
  37. unsigned char *pout, unsigned int hash_type)
  38. {
  39. unsigned int i, reg, len;
  40. unsigned int *pdigest;
  41. struct exynos_ace_sfr *ace_sha_reg =
  42. (struct exynos_ace_sfr *)samsung_get_base_ace_sfr();
  43. if (buf_len == 0) {
  44. /* ACE H/W cannot compute hash value for empty string */
  45. if (hash_type == ACE_SHA_TYPE_SHA1)
  46. memcpy(pout, sha1_digest_emptymsg, SHA1_SUM_LEN);
  47. else
  48. memcpy(pout, sha256_digest_emptymsg, SHA256_SUM_LEN);
  49. return 0;
  50. }
  51. /* Flush HRDMA */
  52. writel(ACE_FC_HRDMACFLUSH_ON, &ace_sha_reg->fc_hrdmac);
  53. writel(ACE_FC_HRDMACFLUSH_OFF, &ace_sha_reg->fc_hrdmac);
  54. /* Set byte swap of data in */
  55. writel(ACE_HASH_SWAPDI_ON | ACE_HASH_SWAPDO_ON | ACE_HASH_SWAPIV_ON,
  56. &ace_sha_reg->hash_byteswap);
  57. /* Select Hash input mux as external source */
  58. reg = readl(&ace_sha_reg->fc_fifoctrl);
  59. reg = (reg & ~ACE_FC_SELHASH_MASK) | ACE_FC_SELHASH_EXOUT;
  60. writel(reg, &ace_sha_reg->fc_fifoctrl);
  61. /* Set Hash as SHA1 or SHA256 and start Hash engine */
  62. reg = (hash_type == ACE_SHA_TYPE_SHA1) ?
  63. ACE_HASH_ENGSEL_SHA1HASH : ACE_HASH_ENGSEL_SHA256HASH;
  64. reg |= ACE_HASH_STARTBIT_ON;
  65. writel(reg, &ace_sha_reg->hash_control);
  66. /* Enable FIFO mode */
  67. writel(ACE_HASH_FIFO_ON, &ace_sha_reg->hash_fifo_mode);
  68. /* Set message length */
  69. writel(buf_len, &ace_sha_reg->hash_msgsize_low);
  70. writel(0, &ace_sha_reg->hash_msgsize_high);
  71. /* Set HRDMA */
  72. writel((unsigned int)pbuf, &ace_sha_reg->fc_hrdmas);
  73. writel(buf_len, &ace_sha_reg->fc_hrdmal);
  74. while ((readl(&ace_sha_reg->hash_status) & ACE_HASH_MSGDONE_MASK) ==
  75. ACE_HASH_MSGDONE_OFF) {
  76. /*
  77. * PRNG error bit goes HIGH if a PRNG request occurs without
  78. * a complete seed setup. We are using this bit to check h/w
  79. * fault because proper setup is not expected in that case.
  80. */
  81. if ((readl(&ace_sha_reg->hash_status)
  82. & ACE_HASH_PRNGERROR_MASK) == ACE_HASH_PRNGERROR_ON)
  83. return -EBUSY;
  84. }
  85. /* Clear MSG_DONE bit */
  86. writel(ACE_HASH_MSGDONE_ON, &ace_sha_reg->hash_status);
  87. /* Read hash result */
  88. pdigest = (unsigned int *)pout;
  89. len = (hash_type == ACE_SHA_TYPE_SHA1) ? SHA1_SUM_LEN : SHA256_SUM_LEN;
  90. for (i = 0; i < len / 4; i++)
  91. pdigest[i] = readl(&ace_sha_reg->hash_result[i]);
  92. /* Clear HRDMA pending bit */
  93. writel(ACE_FC_HRDMA, &ace_sha_reg->fc_intpend);
  94. return 0;
  95. }
  96. void hw_sha256(const unsigned char *pbuf, unsigned int buf_len,
  97. unsigned char *pout, unsigned int chunk_size)
  98. {
  99. if (ace_sha_hash_digest(pbuf, buf_len, pout, ACE_SHA_TYPE_SHA256))
  100. debug("ACE was not setup properly or it is faulty\n");
  101. }
  102. void hw_sha1(const unsigned char *pbuf, unsigned int buf_len,
  103. unsigned char *pout, unsigned int chunk_size)
  104. {
  105. if (ace_sha_hash_digest(pbuf, buf_len, pout, ACE_SHA_TYPE_SHA1))
  106. debug("ACE was not setup properly or it is faulty\n");
  107. }