Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
| 2 | |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
| 12 | * * Neither the name of Code Aurora Forum, Inc. nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <string.h> |
| 30 | #include <endian.h> |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 31 | #include <debug.h> |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 32 | #include <reg.h> |
| 33 | #include <platform/iomap.h> |
| 34 | #include "crypto_eng.h" |
| 35 | #include "crypto_hash.h" |
| 36 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 37 | /* |
| 38 | * Function to reset the crypto engine. |
| 39 | */ |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 40 | |
| 41 | void crypto_eng_reset(void) |
| 42 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 43 | wr_ce(SW_RST, CRYPTO3_CONFIG); |
Channagoud Kadabi | 9268f4c | 2012-04-27 11:31:04 +0530 | [diff] [blame^] | 44 | dmb(); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 47 | /* |
| 48 | * Function to initialize the crypto engine for a new session. It enables the |
| 49 | * auto shutdown feature of CRYPTO3 and mask various interrupts since we use |
| 50 | * polling. We are not using DMOV now. |
| 51 | */ |
| 52 | |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 53 | void crypto_eng_init(void) |
| 54 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 55 | unsigned int val; |
| 56 | val = (AUTO_SHUTDOWN_EN | MASK_ERR_INTR | MASK_AUTH_DONE_INTR | |
| 57 | MASK_DIN_INTR | MASK_DOUT_INTR | HIGH_SPD_IN_EN_N | |
| 58 | HIGH_SPD_OUT_EN_N | HIGH_SPD_HASH_EN_N); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 59 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 60 | wr_ce(val, CRYPTO3_CONFIG); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 63 | /* |
| 64 | * Function to set various SHAx registers in CRYPTO3 based on algorithm type. |
| 65 | */ |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 66 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 67 | void |
| 68 | crypto_set_sha_ctx(void *ctx_ptr, unsigned int bytes_to_write, |
| 69 | crypto_auth_alg_type auth_alg, bool first, bool last) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 70 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 71 | crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr; |
| 72 | crypto_SHA256_ctx *sha256_ctx = (crypto_SHA256_ctx *) ctx_ptr; |
| 73 | unsigned int i = 0; |
| 74 | unsigned int iv_len = 0; |
| 75 | unsigned int *auth_iv; |
| 76 | unsigned int seg_cfg_val; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 77 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 78 | seg_cfg_val = SEG_CFG_AUTH_ALG_SHA; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 79 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 80 | if (auth_alg == CRYPTO_AUTH_ALG_SHA1) { |
| 81 | seg_cfg_val |= SEG_CFG_AUTH_SIZE_SHA1; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 82 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 83 | if ((first) || ((sha1_ctx->saved_buff_indx != 0) && |
| 84 | (sha1_ctx->auth_bytecnt[0] != 0 |
| 85 | || sha1_ctx->auth_bytecnt[1] != 0))) { |
| 86 | seg_cfg_val |= SEG_CFG_FIRST; |
| 87 | } |
| 88 | if (last) { |
| 89 | seg_cfg_val |= SEG_CFG_LAST; |
| 90 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 91 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 92 | iv_len = SHA1_INIT_VECTOR_SIZE; |
| 93 | auth_iv = sha1_ctx->auth_iv; |
| 94 | } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) { |
| 95 | seg_cfg_val |= SEG_CFG_AUTH_SIZE_SHA256; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 96 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 97 | if ((first) || ((sha256_ctx->saved_buff_indx != 0) && |
| 98 | (sha256_ctx->auth_bytecnt[0] != 0 |
| 99 | || sha256_ctx->auth_bytecnt[1] != 0))) { |
| 100 | seg_cfg_val |= SEG_CFG_FIRST; |
| 101 | } |
| 102 | if (last) { |
| 103 | seg_cfg_val |= SEG_CFG_LAST; |
| 104 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 105 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 106 | iv_len = SHA256_INIT_VECTOR_SIZE; |
| 107 | auth_iv = sha256_ctx->auth_iv; |
| 108 | } else { |
| 109 | dprintf(CRITICAL, |
| 110 | "crypto_set_sha_ctx invalid auth algorithm\n"); |
| 111 | return; |
| 112 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 113 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 114 | for (i = 0; i < iv_len; i++) { |
| 115 | wr_ce(*(auth_iv + i), CRYPTO3_AUTH_IVn(i)); |
| 116 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 117 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 118 | wr_ce(seg_cfg_val, CRYPTO3_SEG_CFG); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 119 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 120 | /* Typecast with crypto_SHA1_ctx because offset of auth_bytecnt in both |
| 121 | crypto_SHA1_ctx and crypto_SHA256_ctx are same */ |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 122 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 123 | wr_ce(((crypto_SHA1_ctx *) ctx_ptr)->auth_bytecnt[0], |
| 124 | CRYPTO3_AUTH_BYTECNTn(0)); |
| 125 | wr_ce(((crypto_SHA1_ctx *) ctx_ptr)->auth_bytecnt[1], |
| 126 | CRYPTO3_AUTH_BYTECNTn(1)); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 127 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 128 | wr_ce((bytes_to_write << AUTH_SEG_CFG_AUTH_SIZE), CRYPTO3_AUTH_SEG_CFG); |
| 129 | wr_ce(bytes_to_write, CRYPTO3_SEG_SIZE); |
| 130 | wr_ce(GOPROC_GO, CRYPTO3_GOPROC); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 131 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 132 | return; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 135 | /* |
| 136 | * Function to send data to CRYPTO3. This is non-DMOV implementation and uses |
| 137 | * polling to send the requested amount of data. |
| 138 | */ |
| 139 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 140 | void |
| 141 | crypto_send_data(void *ctx_ptr, unsigned char *data_ptr, |
| 142 | unsigned int buff_size, unsigned int bytes_to_write, |
| 143 | unsigned int *ret_status) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 144 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 145 | crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr; |
| 146 | unsigned int bytes_left = 0; |
| 147 | unsigned int i = 0; |
| 148 | unsigned int ce_status = 0; |
| 149 | unsigned int ce_err_bmsk = 0; |
| 150 | unsigned int is_not_aligned = FALSE; |
| 151 | unsigned char data[4]; |
| 152 | unsigned char *buff_ptr = data_ptr; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 153 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 154 | /* Check if the buff_ptr is aligned */ |
| 155 | if (!(IS_ALIGNED(buff_ptr))) { |
| 156 | is_not_aligned = TRUE; |
| 157 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 158 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 159 | /* Fill the saved_buff with data from buff_ptr. First we have to write |
| 160 | all the data from the saved_buff and then we will write data from |
| 161 | buff_ptr. We will update bytes_left and buff_ptr in the while loop |
| 162 | once are done writing all the data from saved_buff. */ |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 163 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 164 | if (sha1_ctx->saved_buff_indx != 0) { |
| 165 | memcpy(sha1_ctx->saved_buff + sha1_ctx->saved_buff_indx, |
| 166 | buff_ptr, |
| 167 | (((buff_size + sha1_ctx->saved_buff_indx) <= |
| 168 | CRYPTO_SHA_BLOCK_SIZE) |
| 169 | ? buff_size : (CRYPTO_SHA_BLOCK_SIZE - |
| 170 | sha1_ctx->saved_buff_indx))); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 171 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 172 | if (bytes_to_write >= CRYPTO_SHA_BLOCK_SIZE) { |
| 173 | bytes_left = CRYPTO_SHA_BLOCK_SIZE; |
| 174 | } else { |
| 175 | bytes_left = bytes_to_write; |
| 176 | } |
| 177 | } else { |
| 178 | bytes_left = bytes_to_write; |
| 179 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 180 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 181 | /* Error bitmask to check crypto engine status */ |
| 182 | ce_err_bmsk = (SW_ERR | DIN_RDY | DIN_SIZE_AVAIL); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 183 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 184 | while (bytes_left >= 4) { |
| 185 | ce_status = rd_ce(CRYPTO3_STATUS); |
| 186 | ce_status &= ce_err_bmsk; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 187 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 188 | if (ce_status & SW_ERR) { |
| 189 | /* If there is SW_ERR, reset the engine */ |
| 190 | crypto_eng_reset(); |
| 191 | *ret_status = CRYPTO_ERR_FAIL; |
| 192 | dprintf(CRITICAL, "crypto_send_data sw error\n"); |
| 193 | return; |
| 194 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 195 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 196 | /* We can write data now - 4 bytes at a time in network byte order */ |
| 197 | if ((ce_status & DIN_RDY) |
| 198 | && ((ce_status & DIN_SIZE_AVAIL) >= 4)) { |
| 199 | if (sha1_ctx->saved_buff_indx != 0) { |
| 200 | /* Write from saved_buff */ |
| 201 | wr_ce(htonl |
| 202 | (* |
| 203 | ((unsigned int *)(sha1_ctx->saved_buff) + |
| 204 | i)), CRYPTO3_DATA_IN); |
| 205 | } else { |
| 206 | if (!is_not_aligned) { |
| 207 | /* Write from buff_ptr aligned */ |
| 208 | wr_ce(htonl |
| 209 | (*((unsigned int *)buff_ptr + i)), |
| 210 | CRYPTO3_DATA_IN); |
| 211 | } else { |
| 212 | /* If buff_ptr is not aligned write byte by byte */ |
| 213 | data[0] = *(buff_ptr + i); |
| 214 | data[1] = *(buff_ptr + i + 1); |
| 215 | data[2] = *(buff_ptr + i + 2); |
| 216 | data[3] = *(buff_ptr + i + 3); |
| 217 | /* i will incremented by 1 in outside block */ |
| 218 | i += 3; |
| 219 | wr_ce(htonl(*(unsigned int *)data), |
| 220 | CRYPTO3_DATA_IN); |
| 221 | memset(data, 0, 4); |
| 222 | } |
| 223 | } |
| 224 | i++; |
| 225 | bytes_left -= 4; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 226 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 227 | /* Check if we have written from saved_buff. Adjust buff_ptr and |
| 228 | bytes_left accordingly */ |
| 229 | if ((sha1_ctx->saved_buff_indx != 0) |
| 230 | && (bytes_left == 0) |
| 231 | && (bytes_to_write > CRYPTO_SHA_BLOCK_SIZE)) { |
| 232 | bytes_left = |
| 233 | (bytes_to_write - CRYPTO_SHA_BLOCK_SIZE); |
| 234 | buff_ptr = |
| 235 | (unsigned char *)((unsigned char *)data_ptr |
| 236 | + CRYPTO_SHA_BLOCK_SIZE - |
| 237 | sha1_ctx-> |
| 238 | saved_buff_indx); |
| 239 | i = 0; |
| 240 | sha1_ctx->saved_buff_indx = 0; |
| 241 | if (!(IS_ALIGNED(buff_ptr))) { |
| 242 | is_not_aligned = TRUE; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 247 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 248 | /* We might have bytes_left < 4. Write them now if available */ |
| 249 | if (bytes_left) { |
| 250 | memset(data, 0, sizeof(unsigned int)); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 251 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 252 | if (sha1_ctx->saved_buff_indx) |
| 253 | buff_ptr = (sha1_ctx->saved_buff + bytes_to_write - 1); |
| 254 | else |
| 255 | buff_ptr = |
| 256 | (((unsigned char *)data_ptr) + buff_size - 1); |
Subbaraman Narayanamurthy | 22d53c6 | 2011-04-12 18:15:50 -0700 | [diff] [blame] | 257 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 258 | for (i = 0; i < bytes_left; i++) { |
| 259 | data[3 - i] = *(buff_ptr - bytes_left + i + 1); |
| 260 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 261 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 262 | ce_status = rd_ce(CRYPTO3_STATUS); |
| 263 | ce_status &= ce_err_bmsk; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 264 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 265 | if (ce_status & SW_ERR) { |
| 266 | crypto_eng_reset(); |
| 267 | *ret_status = CRYPTO_ERR_FAIL; |
| 268 | dprintf(CRITICAL, "crypto_send_data sw error 2\n"); |
| 269 | return; |
| 270 | } |
| 271 | if ((ce_status & DIN_RDY) |
| 272 | && ((ce_status & DIN_SIZE_AVAIL) >= 4)) { |
| 273 | wr_ce(*(unsigned int *)data, CRYPTO3_DATA_IN); |
| 274 | } |
| 275 | } |
| 276 | *ret_status = CRYPTO_ERR_NONE; |
| 277 | return; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 280 | /* |
| 281 | * Function to get digest from CRYPTO3. We poll for AUTH_DONE from CRYPTO3. |
| 282 | */ |
| 283 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 284 | void |
| 285 | crypto_get_digest(unsigned char *digest_ptr, unsigned int *ret_status, |
| 286 | crypto_auth_alg_type auth_alg, bool last) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 287 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 288 | unsigned int ce_status = 0; |
| 289 | unsigned int ce_err_bmsk = (AUTH_DONE | SW_ERR); |
| 290 | unsigned int i = 0; |
| 291 | unsigned int digest_len = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 292 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 293 | do { |
| 294 | ce_status = rd_ce(CRYPTO3_STATUS); |
| 295 | ce_status &= ce_err_bmsk; |
| 296 | } |
| 297 | while (ce_status == 0); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 298 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 299 | if (ce_status & SW_ERR) { |
| 300 | crypto_eng_reset(); |
| 301 | *ret_status = CRYPTO_ERR_FAIL; |
| 302 | dprintf(CRITICAL, "crypto_get_digest sw error\n"); |
| 303 | return; |
| 304 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 305 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 306 | /* Digest length depends on auth_alg */ |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 307 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 308 | if (auth_alg == CRYPTO_AUTH_ALG_SHA1) { |
| 309 | digest_len = SHA1_INIT_VECTOR_SIZE; |
| 310 | } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) { |
| 311 | digest_len = SHA256_INIT_VECTOR_SIZE; |
| 312 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 313 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 314 | /* Retrieve digest from CRYPTO3 */ |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 315 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 316 | for (i = 0; i < digest_len; i++) { |
| 317 | unsigned int auth_iv = rd_ce(CRYPTO3_AUTH_IVn(i)); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 318 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 319 | if (last) { |
| 320 | *((unsigned int *)digest_ptr + i) = htonl(auth_iv); |
| 321 | } else { |
| 322 | *((unsigned int *)digest_ptr + i) = auth_iv; |
| 323 | } |
| 324 | } |
| 325 | *ret_status = CRYPTO_ERR_NONE; |
| 326 | return; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 329 | /* Function to restore auth_bytecnt registers for ctx_ptr */ |
| 330 | |
| 331 | void crypto_get_ctx(void *ctx_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 332 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 333 | ((crypto_SHA1_ctx *) ctx_ptr)->auth_bytecnt[0] = |
| 334 | rd_ce(CRYPTO3_AUTH_BYTECNTn(0)); |
| 335 | ((crypto_SHA1_ctx *) ctx_ptr)->auth_bytecnt[1] = |
| 336 | rd_ce(CRYPTO3_AUTH_BYTECNTn(1)); |
| 337 | return; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 338 | } |