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 <debug.h> |
| 31 | #include <sys/types.h> |
| 32 | #include "crypto_hash.h" |
| 33 | #include "crypto_eng.h" |
| 34 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 35 | static crypto_SHA256_ctx g_sha256_ctx; |
| 36 | static crypto_SHA1_ctx g_sha1_ctx; |
| 37 | static unsigned char crypto_init_done = FALSE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 38 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 39 | /* |
| 40 | * Top level function which calculates SHAx digest with given data and size. |
| 41 | * Digest varies based on the authentication algorithm. |
| 42 | * It works on contiguous data and does single pass calculation. |
| 43 | */ |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 44 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 45 | void hash_find(unsigned char *addr, unsigned int size, unsigned char *digest, |
| 46 | unsigned char auth_alg) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 47 | { |
| 48 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
| 49 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 50 | if(auth_alg == 1) |
| 51 | { |
| 52 | ret_val = crypto_sha1(addr,size,digest); |
| 53 | } |
| 54 | else if(auth_alg == 2) |
| 55 | { |
| 56 | ret_val = crypto_sha256(addr,size,digest); |
| 57 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 58 | |
| 59 | if(ret_val != CRYPTO_SHA_ERR_NONE) |
| 60 | { |
| 61 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n",ret_val); |
| 62 | } |
| 63 | } |
| 64 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 65 | /* |
| 66 | * Function to reset and init crypto engine. It resets the engine for the |
| 67 | * first time. Used for multiple SHA operations. |
| 68 | */ |
| 69 | |
| 70 | static void crypto_init(void) |
| 71 | { |
| 72 | if(crypto_init_done != TRUE) |
| 73 | { |
| 74 | crypto_eng_reset(); |
| 75 | crypto_init_done = TRUE; |
| 76 | } |
| 77 | crypto_eng_init(); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Function to initialize SHA256 context |
| 82 | */ |
| 83 | |
| 84 | static crypto_result_type crypto_sha256_init(crypto_SHA256_ctx *ctx_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 85 | { |
| 86 | unsigned int i; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 87 | /* Standard initialization vector for SHA256 */ |
| 88 | unsigned int sha256_init_vector[] = { 0x6A09E667, 0xBB67AE85, |
| 89 | 0x3C6EF372, 0xA54FF53A, |
| 90 | 0x510E527F, 0x9B05688C, |
| 91 | 0x1F83D9AB, 0x5BE0CD19 }; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 92 | |
| 93 | if(ctx_ptr == NULL) |
| 94 | { |
| 95 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 96 | } |
| 97 | |
| 98 | ctx_ptr->auth_bytecnt[0] = 0; |
| 99 | ctx_ptr->auth_bytecnt[1] = 0; |
| 100 | |
| 101 | memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE); |
| 102 | |
| 103 | for(i=0;i<SHA256_INIT_VECTOR_SIZE;i++) |
| 104 | { |
| 105 | ctx_ptr->auth_iv[i] = sha256_init_vector[i]; |
| 106 | } |
| 107 | |
| 108 | ctx_ptr->saved_buff_indx = 0; |
| 109 | |
| 110 | return CRYPTO_SHA_ERR_NONE; |
| 111 | } |
| 112 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 113 | /* |
| 114 | * Function to initialize SHA1 context |
| 115 | */ |
| 116 | |
| 117 | static crypto_result_type crypto_sha1_init(crypto_SHA1_ctx *ctx_ptr) |
| 118 | { |
| 119 | unsigned int i; |
| 120 | /* Standard initialization vector for SHA1 */ |
| 121 | unsigned int sha1_init_vector[] = { 0x67452301, 0xEFCDAB89, |
| 122 | 0x98BADCFE, 0x10325476, |
| 123 | 0xC3D2E1F0 }; |
| 124 | |
| 125 | if(ctx_ptr == NULL) |
| 126 | { |
| 127 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 128 | } |
| 129 | |
| 130 | ctx_ptr->auth_bytecnt[0] = 0; |
| 131 | ctx_ptr->auth_bytecnt[1] = 0; |
| 132 | |
| 133 | memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE); |
| 134 | |
| 135 | for(i=0;i<SHA1_INIT_VECTOR_SIZE;i++) |
| 136 | { |
| 137 | ctx_ptr->auth_iv[i] = sha1_init_vector[i]; |
| 138 | } |
| 139 | |
| 140 | ctx_ptr->saved_buff_indx = 0; |
| 141 | |
| 142 | return CRYPTO_SHA_ERR_NONE; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Function to calculate SHA256 digest of given data buffer. |
| 147 | * It works on contiguous data and gives digest in single pass. |
| 148 | */ |
| 149 | |
| 150 | static crypto_result_type crypto_sha256(unsigned char *buff_ptr, |
| 151 | unsigned int buff_size, |
| 152 | unsigned char *digest_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 153 | { |
| 154 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
| 155 | |
| 156 | if((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) |
| 157 | { |
| 158 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 159 | } |
| 160 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 161 | /* Initialize crypto engine hardware for a new SHA256 operation */ |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 162 | crypto_init(); |
| 163 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 164 | /* Now do SHA256 hashing */ |
| 165 | ret_val = do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA256); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 166 | |
| 167 | if(ret_val != CRYPTO_SHA_ERR_NONE) |
| 168 | { |
| 169 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n",ret_val); |
| 170 | } |
| 171 | |
| 172 | return ret_val; |
| 173 | } |
| 174 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 175 | /* |
| 176 | * Function to calculate SHA1 digest of given data buffer. |
| 177 | * It works on contiguous data and gives digest in single pass. |
| 178 | */ |
| 179 | |
| 180 | static crypto_result_type crypto_sha1(unsigned char *buff_ptr, |
| 181 | unsigned int buff_size, |
| 182 | unsigned char *digest_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 183 | { |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 184 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
| 185 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 186 | if((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) |
| 187 | { |
| 188 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 189 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 190 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 191 | /* Initialize crypto engine hardware for a new SHA1 operation */ |
| 192 | crypto_init(); |
| 193 | |
| 194 | /* Now do SHA1 hashing */ |
| 195 | ret_val = do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA1); |
| 196 | |
| 197 | if(ret_val != CRYPTO_SHA_ERR_NONE) |
| 198 | { |
| 199 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n",ret_val); |
| 200 | } |
| 201 | |
| 202 | return ret_val; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Common function to calculate SHA1 and SHA256 digest based on auth algorithm. |
| 207 | */ |
| 208 | |
| 209 | static crypto_result_type do_sha(unsigned char *buff_ptr, |
| 210 | unsigned int buff_size, |
| 211 | unsigned char *digest_ptr, |
| 212 | crypto_auth_alg_type auth_alg) |
| 213 | { |
| 214 | void *ctx_ptr=NULL; |
| 215 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
| 216 | |
| 217 | /* Initialize SHA context based on algorithm */ |
| 218 | if(auth_alg == CRYPTO_AUTH_ALG_SHA1) |
| 219 | { |
| 220 | crypto_sha1_init(&g_sha1_ctx); |
| 221 | ctx_ptr = (void*)&g_sha1_ctx; |
| 222 | } |
| 223 | else if(auth_alg == CRYPTO_AUTH_ALG_SHA256) |
| 224 | { |
| 225 | crypto_sha256_init(&g_sha256_ctx); |
| 226 | ctx_ptr = (void*)&g_sha256_ctx; |
| 227 | } |
| 228 | |
| 229 | ret_val = do_sha_update(ctx_ptr, buff_ptr, buff_size, auth_alg, TRUE, TRUE); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 230 | |
| 231 | if(ret_val != CRYPTO_SHA_ERR_NONE) |
| 232 | { |
| 233 | dprintf(CRITICAL, "do_sha_update returns error %d\n",ret_val); |
| 234 | return ret_val; |
| 235 | } |
| 236 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 237 | /* Copy the digest value from context pointer to digest pointer */ |
| 238 | if(auth_alg == CRYPTO_AUTH_ALG_SHA1) |
| 239 | { |
| 240 | memcpy(digest_ptr, (unsigned char*)(((crypto_SHA1_ctx*)ctx_ptr)->auth_iv), 20); |
| 241 | } |
| 242 | else if(auth_alg == CRYPTO_AUTH_ALG_SHA256) |
| 243 | { |
| 244 | memcpy(digest_ptr, (unsigned char*)(((crypto_SHA256_ctx*)ctx_ptr)->auth_iv), 32); |
| 245 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 246 | |
| 247 | return CRYPTO_SHA_ERR_NONE; |
| 248 | } |
| 249 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 250 | /* |
| 251 | * Common function to calculate SHA1 and SHA256 digest based on auth algorithm. |
| 252 | * Calls crypto engine APIs to setup SHAx registers, send the data and gets |
| 253 | * the digest. |
| 254 | */ |
| 255 | |
| 256 | static crypto_result_type do_sha_update(void *ctx_ptr, |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 257 | unsigned char *buff_ptr, |
| 258 | unsigned int buff_size, |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 259 | crypto_auth_alg_type auth_alg, |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 260 | bool first, bool last) |
| 261 | { |
| 262 | unsigned int ret_val = CRYPTO_ERR_NONE; |
| 263 | unsigned int bytes_to_write = 0; |
| 264 | unsigned int bytes_remaining = 0; |
| 265 | unsigned int tmp_bytes = 0; |
| 266 | unsigned int bytes_written = 0; |
| 267 | unsigned int tmp_buff_size = 0; |
| 268 | unsigned char *tmp_buff_ptr = NULL; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 269 | unsigned char tmp_saved_buff_indx = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 270 | bool tmp_first; |
| 271 | bool tmp_last; |
| 272 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 273 | /* Type casting to SHA1 context as offset is similar for SHA256 context */ |
| 274 | crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx*)ctx_ptr; |
| 275 | |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 276 | bytes_to_write = calc_num_bytes_to_send(ctx_ptr, buff_size, last); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 277 | bytes_remaining = buff_size + sha1_ctx->saved_buff_indx - bytes_to_write; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 278 | |
| 279 | tmp_first = first; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 280 | tmp_saved_buff_indx = sha1_ctx->saved_buff_indx; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 281 | |
| 282 | do |
| 283 | { |
| 284 | if((bytes_to_write - bytes_written) > CRYPTO_MAX_AUTH_BLOCK_SIZE) |
| 285 | { |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 286 | /* Write CRYPTO_MAX_AUTH_BLOCK_SIZE bytes at a time to the CE */ |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 287 | tmp_bytes = CRYPTO_MAX_AUTH_BLOCK_SIZE; |
| 288 | tmp_last = FALSE; |
| 289 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 290 | if(sha1_ctx->saved_buff_indx != 0) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 291 | { |
| 292 | tmp_buff_ptr = buff_ptr; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 293 | tmp_buff_size = tmp_bytes - sha1_ctx->saved_buff_indx; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 294 | } |
| 295 | else |
| 296 | { |
| 297 | tmp_buff_ptr = buff_ptr + bytes_written - tmp_saved_buff_indx; |
| 298 | tmp_buff_size = tmp_bytes; |
| 299 | } |
| 300 | } |
| 301 | else |
| 302 | { |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 303 | /* Since bytes_to_write are less than CRYPTO_MAX_AUTH_BLOCK_SIZE |
| 304 | write all remaining bytes now */ |
| 305 | if(sha1_ctx->saved_buff_indx != 0) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 306 | { |
| 307 | tmp_buff_ptr = buff_ptr; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 308 | tmp_buff_size = bytes_to_write - bytes_written - sha1_ctx->saved_buff_indx; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 309 | } |
| 310 | else |
| 311 | { |
| 312 | tmp_buff_ptr = buff_ptr + bytes_written - tmp_saved_buff_indx; |
| 313 | tmp_buff_size = bytes_to_write - bytes_written - tmp_saved_buff_indx; |
| 314 | } |
| 315 | |
| 316 | tmp_bytes = (bytes_to_write - bytes_written); |
| 317 | tmp_last = last; |
| 318 | } |
| 319 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 320 | /* Set SHAx context in the crypto engine */ |
| 321 | crypto_set_sha_ctx(ctx_ptr, tmp_bytes, auth_alg, tmp_first, tmp_last); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 322 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 323 | /* Send data to the crypto engine */ |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 324 | crypto_send_data(ctx_ptr, tmp_buff_ptr, tmp_buff_size, tmp_bytes, &ret_val); |
| 325 | |
| 326 | if(ret_val != CRYPTO_ERR_NONE) |
| 327 | { |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 328 | dprintf(CRITICAL, "do_sha_update returns error from crypto_send_data\n"); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 329 | return CRYPTO_SHA_ERR_FAIL; |
| 330 | } |
| 331 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 332 | /* Get the SHAx digest from the crypto engine */ |
| 333 | crypto_get_digest((unsigned char *)(sha1_ctx->auth_iv), &ret_val, auth_alg, tmp_last); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 334 | |
| 335 | if(ret_val != CRYPTO_ERR_NONE) |
| 336 | { |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 337 | dprintf(CRITICAL, "do_sha_update returns error from crypto_get_digest\n"); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 338 | return CRYPTO_SHA_ERR_FAIL; |
| 339 | } |
| 340 | |
| 341 | if(!tmp_last) |
| 342 | { |
| 343 | crypto_get_ctx(ctx_ptr); |
| 344 | } |
| 345 | |
| 346 | bytes_written += tmp_bytes; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 347 | sha1_ctx->saved_buff_indx = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 348 | |
| 349 | if(bytes_written != bytes_to_write) |
| 350 | { |
| 351 | tmp_first = FALSE; |
| 352 | } |
| 353 | |
| 354 | }while ((bytes_to_write - bytes_written) != 0); |
| 355 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 356 | /* If there are bytes remaining, copy it to saved_buff */ |
| 357 | |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 358 | if(bytes_remaining) |
| 359 | { |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 360 | memcpy(sha1_ctx->saved_buff, (buff_ptr + buff_size - bytes_remaining), |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 361 | bytes_remaining); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 362 | sha1_ctx->saved_buff_indx = bytes_remaining; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 363 | } |
| 364 | else |
| 365 | { |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 366 | sha1_ctx->saved_buff_indx = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | return CRYPTO_SHA_ERR_NONE; |
| 370 | } |
| 371 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 372 | /* |
| 373 | * Function to calculate the number of bytes to be sent to crypto engine. |
| 374 | */ |
| 375 | |
| 376 | static unsigned int calc_num_bytes_to_send(void *ctx_ptr, |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 377 | unsigned int buff_size, bool last) |
| 378 | { |
| 379 | unsigned int bytes_to_write=0; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 380 | crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx*)ctx_ptr; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 381 | |
| 382 | if(last) |
| 383 | { |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 384 | bytes_to_write = buff_size + sha1_ctx->saved_buff_indx; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 385 | } |
| 386 | else |
| 387 | { |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 388 | bytes_to_write = ((buff_size + sha1_ctx->saved_buff_indx)/ |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 389 | CRYPTO_SHA_BLOCK_SIZE) * CRYPTO_SHA_BLOCK_SIZE; |
| 390 | } |
| 391 | return bytes_to_write; |
| 392 | } |