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 26 /* SHA1 value for the message of zero length */ 27 static const unsigned char sha1_digest_emptymsg[SHA1_SUM_LEN] = { 28 0xDA, 0x39, 0xA3, 0xEE, 0x5E, 0x6B, 0x4B, 0x0D, 29 0x32, 0x55, 0xBF, 0xFF, 0x95, 0x60, 0x18, 0x90, 30 0xAF, 0xD8, 0x07, 0x09}; 31 32 /* SHA256 value for the message of zero length */ 33 static const unsigned char sha256_digest_emptymsg[SHA256_SUM_LEN] = { 34 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14, 35 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24, 36 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C, 37 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55}; 38 39 int ace_sha_hash_digest(const unsigned char *pbuf, unsigned int buf_len, 40 unsigned char *pout, unsigned int hash_type) 41 { 42 unsigned int i, reg, len; 43 unsigned int *pdigest; 44 struct exynos_ace_sfr *ace_sha_reg = 45 (struct exynos_ace_sfr *)samsung_get_base_ace_sfr(); 46 47 if (buf_len == 0) { 48 /* ACE H/W cannot compute hash value for empty string */ 49 if (hash_type == ACE_SHA_TYPE_SHA1) 50 memcpy(pout, sha1_digest_emptymsg, SHA1_SUM_LEN); 51 else 52 memcpy(pout, sha256_digest_emptymsg, SHA256_SUM_LEN); 53 return 0; 54 } 55 56 /* Flush HRDMA */ 57 writel(ACE_FC_HRDMACFLUSH_ON, &ace_sha_reg->fc_hrdmac); 58 writel(ACE_FC_HRDMACFLUSH_OFF, &ace_sha_reg->fc_hrdmac); 59 60 /* Set byte swap of data in */ 61 writel(ACE_HASH_SWAPDI_ON | ACE_HASH_SWAPDO_ON | ACE_HASH_SWAPIV_ON, 62 &ace_sha_reg->hash_byteswap); 63 64 /* Select Hash input mux as external source */ 65 reg = readl(&ace_sha_reg->fc_fifoctrl); 66 reg = (reg & ~ACE_FC_SELHASH_MASK) | ACE_FC_SELHASH_EXOUT; 67 writel(reg, &ace_sha_reg->fc_fifoctrl); 68 69 /* Set Hash as SHA1 or SHA256 and start Hash engine */ 70 reg = (hash_type == ACE_SHA_TYPE_SHA1) ? 71 ACE_HASH_ENGSEL_SHA1HASH : ACE_HASH_ENGSEL_SHA256HASH; 72 reg |= ACE_HASH_STARTBIT_ON; 73 writel(reg, &ace_sha_reg->hash_control); 74 75 /* Enable FIFO mode */ 76 writel(ACE_HASH_FIFO_ON, &ace_sha_reg->hash_fifo_mode); 77 78 /* Set message length */ 79 writel(buf_len, &ace_sha_reg->hash_msgsize_low); 80 writel(0, &ace_sha_reg->hash_msgsize_high); 81 82 /* Set HRDMA */ 83 writel((unsigned int)pbuf, &ace_sha_reg->fc_hrdmas); 84 writel(buf_len, &ace_sha_reg->fc_hrdmal); 85 86 while ((readl(&ace_sha_reg->hash_status) & ACE_HASH_MSGDONE_MASK) == 87 ACE_HASH_MSGDONE_OFF) { 88 /* 89 * PRNG error bit goes HIGH if a PRNG request occurs without 90 * a complete seed setup. We are using this bit to check h/w 91 * fault because proper setup is not expected in that case. 92 */ 93 if ((readl(&ace_sha_reg->hash_status) 94 & ACE_HASH_PRNGERROR_MASK) == ACE_HASH_PRNGERROR_ON) 95 return -EBUSY; 96 } 97 98 /* Clear MSG_DONE bit */ 99 writel(ACE_HASH_MSGDONE_ON, &ace_sha_reg->hash_status); 100 101 /* Read hash result */ 102 pdigest = (unsigned int *)pout; 103 len = (hash_type == ACE_SHA_TYPE_SHA1) ? SHA1_SUM_LEN : SHA256_SUM_LEN; 104 105 for (i = 0; i < len / 4; i++) 106 pdigest[i] = readl(&ace_sha_reg->hash_result[i]); 107 108 /* Clear HRDMA pending bit */ 109 writel(ACE_FC_HRDMA, &ace_sha_reg->fc_intpend); 110 111 return 0; 112 } 113 114 void hw_sha256(const unsigned char *pbuf, unsigned int buf_len, 115 unsigned char *pout, unsigned int chunk_size) 116 { 117 if (ace_sha_hash_digest(pbuf, buf_len, pout, ACE_SHA_TYPE_SHA256)) 118 debug("ACE was not setup properly or it is faulty\n"); 119 } 120 121 void hw_sha1(const unsigned char *pbuf, unsigned int buf_len, 122 unsigned char *pout, unsigned int chunk_size) 123 { 124 if (ace_sha_hash_digest(pbuf, buf_len, pout, ACE_SHA_TYPE_SHA1)) 125 debug("ACE was not setup properly or it is faulty\n"); 126 } 127