blob: 0d3cce87825623df56d8cabfe282accb1aae90da [file] [log] [blame]
Amol Jadi7d3ad062013-01-04 15:30:19 -08001/* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -08002
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.
Amol Jadi7d3ad062013-01-04 15:30:19 -080012 * * Neither the name of The Linux Foundation nor the names of its
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080013 * 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"
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080033
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080034static crypto_SHA256_ctx g_sha256_ctx;
35static crypto_SHA1_ctx g_sha1_ctx;
Channagoud Kadabi317df2e2013-12-10 12:22:04 -080036static bool crypto_init_done;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080037
Shashank Mittal1fcde7a2011-07-25 13:41:50 -070038extern void ce_clock_init(void);
39
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080040/*
41 * Top level function which calculates SHAx digest with given data and size.
42 * Digest varies based on the authentication algorithm.
43 * It works on contiguous data and does single pass calculation.
44 */
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080045
Ajay Dudanib01e5062011-12-03 23:23:42 -080046void
47hash_find(unsigned char *addr, unsigned int size, unsigned char *digest,
48 unsigned char auth_alg)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080049{
Ajay Dudanib01e5062011-12-03 23:23:42 -080050 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Deepa Dinamani5e5c21a2012-02-16 18:59:57 -080051 crypto_engine_type platform_ce_type = board_ce_type();
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080052
Sundarajan Srinivasan5bba3de2013-11-11 18:48:27 -080053 if (auth_alg == CRYPTO_AUTH_ALG_SHA1) {
Deepa Dinamani5e5c21a2012-02-16 18:59:57 -080054 if(platform_ce_type == CRYPTO_ENGINE_TYPE_SW)
55 /* Hardware CE is not present , use software hashing */
56 digest = SHA1(addr, size, digest);
57 else if (platform_ce_type == CRYPTO_ENGINE_TYPE_HW)
58 ret_val = crypto_sha1(addr, size, digest);
59 else
60 ret_val = CRYPTO_SHA_ERR_FAIL;
Sundarajan Srinivasan5bba3de2013-11-11 18:48:27 -080061 } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) {
Deepa Dinamani5e5c21a2012-02-16 18:59:57 -080062 if(platform_ce_type == CRYPTO_ENGINE_TYPE_SW)
63 /* Hardware CE is not present , use software hashing */
64 digest = SHA256(addr, size, digest);
65 else if (platform_ce_type == CRYPTO_ENGINE_TYPE_HW)
66 ret_val = crypto_sha256(addr, size, digest);
67 else
68 ret_val = CRYPTO_SHA_ERR_FAIL;
Ajay Dudanib01e5062011-12-03 23:23:42 -080069 }
Sundarajan Srinivasan5bba3de2013-11-11 18:48:27 -080070 else
71 ret_val = CRYPTO_SHA_ERR_FAIL;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080072
Ajay Dudanib01e5062011-12-03 23:23:42 -080073 if (ret_val != CRYPTO_SHA_ERR_NONE) {
74 dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val);
75 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080076}
77
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080078/*
79 * Function to reset and init crypto engine. It resets the engine for the
80 * first time. Used for multiple SHA operations.
81 */
82
83static void crypto_init(void)
84{
Ajay Dudanib01e5062011-12-03 23:23:42 -080085 if (crypto_init_done != TRUE) {
86 ce_clock_init();
87 crypto_eng_reset();
88 crypto_init_done = TRUE;
89 }
90 crypto_eng_init();
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080091}
92
93/*
Channagoud Kadabi1d3e9b42013-12-04 12:35:46 -080094 * Function to return if crypto is initialized
95 */
96
97bool crypto_initialized()
98{
99 return crypto_init_done;
100}
101
102/*
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800103 * Function to initialize SHA256 context
104 */
105
Ajay Dudanib01e5062011-12-03 23:23:42 -0800106static crypto_result_type crypto_sha256_init(crypto_SHA256_ctx * ctx_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800107{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800108 unsigned int i;
109 /* Standard initialization vector for SHA256 */
110 unsigned int sha256_init_vector[] = { 0x6A09E667, 0xBB67AE85,
111 0x3C6EF372, 0xA54FF53A,
112 0x510E527F, 0x9B05688C,
113 0x1F83D9AB, 0x5BE0CD19
114 };
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800115
Ajay Dudanib01e5062011-12-03 23:23:42 -0800116 if (ctx_ptr == NULL) {
117 return CRYPTO_SHA_ERR_INVALID_PARAM;
118 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800119
Ajay Dudanib01e5062011-12-03 23:23:42 -0800120 ctx_ptr->auth_bytecnt[0] = 0;
121 ctx_ptr->auth_bytecnt[1] = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800122
Ajay Dudanib01e5062011-12-03 23:23:42 -0800123 memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800124
Ajay Dudanib01e5062011-12-03 23:23:42 -0800125 for (i = 0; i < SHA256_INIT_VECTOR_SIZE; i++) {
126 ctx_ptr->auth_iv[i] = sha256_init_vector[i];
127 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800128
Ajay Dudanib01e5062011-12-03 23:23:42 -0800129 ctx_ptr->saved_buff_indx = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800130
Ajay Dudanib01e5062011-12-03 23:23:42 -0800131 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800132}
133
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800134/*
135 * Function to initialize SHA1 context
136 */
137
Ajay Dudanib01e5062011-12-03 23:23:42 -0800138static crypto_result_type crypto_sha1_init(crypto_SHA1_ctx * ctx_ptr)
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800139{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800140 unsigned int i;
141 /* Standard initialization vector for SHA1 */
142 unsigned int sha1_init_vector[] = { 0x67452301, 0xEFCDAB89,
143 0x98BADCFE, 0x10325476,
144 0xC3D2E1F0
145 };
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800146
Ajay Dudanib01e5062011-12-03 23:23:42 -0800147 if (ctx_ptr == NULL) {
148 return CRYPTO_SHA_ERR_INVALID_PARAM;
149 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800150
Ajay Dudanib01e5062011-12-03 23:23:42 -0800151 ctx_ptr->auth_bytecnt[0] = 0;
152 ctx_ptr->auth_bytecnt[1] = 0;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800153
Ajay Dudanib01e5062011-12-03 23:23:42 -0800154 memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800155
Ajay Dudanib01e5062011-12-03 23:23:42 -0800156 for (i = 0; i < SHA1_INIT_VECTOR_SIZE; i++) {
157 ctx_ptr->auth_iv[i] = sha1_init_vector[i];
158 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800159
Ajay Dudanib01e5062011-12-03 23:23:42 -0800160 ctx_ptr->saved_buff_indx = 0;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800161
Ajay Dudanib01e5062011-12-03 23:23:42 -0800162 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800163}
164
165/*
166 * Function to calculate SHA256 digest of given data buffer.
167 * It works on contiguous data and gives digest in single pass.
168 */
169
Ajay Dudanib01e5062011-12-03 23:23:42 -0800170static crypto_result_type
171crypto_sha256(unsigned char *buff_ptr,
172 unsigned int buff_size, unsigned char *digest_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800173{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800174 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800175
Ajay Dudanib01e5062011-12-03 23:23:42 -0800176 if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) {
177 return CRYPTO_SHA_ERR_INVALID_PARAM;
178 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800179
Ajay Dudanib01e5062011-12-03 23:23:42 -0800180 /* Initialize crypto engine hardware for a new SHA256 operation */
181 crypto_init();
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800182
Ajay Dudanib01e5062011-12-03 23:23:42 -0800183 /* Now do SHA256 hashing */
184 ret_val =
185 do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA256);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800186
Ajay Dudanib01e5062011-12-03 23:23:42 -0800187 if (ret_val != CRYPTO_SHA_ERR_NONE) {
188 dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val);
189 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800190
Ajay Dudanib01e5062011-12-03 23:23:42 -0800191 return ret_val;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800192}
193
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800194/*
195 * Function to calculate SHA1 digest of given data buffer.
196 * It works on contiguous data and gives digest in single pass.
197 */
198
Ajay Dudanib01e5062011-12-03 23:23:42 -0800199static crypto_result_type
200crypto_sha1(unsigned char *buff_ptr,
201 unsigned int buff_size, unsigned char *digest_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800202{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800203 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800204
Ajay Dudanib01e5062011-12-03 23:23:42 -0800205 if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) {
206 return CRYPTO_SHA_ERR_INVALID_PARAM;
207 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800208
Ajay Dudanib01e5062011-12-03 23:23:42 -0800209 /* Initialize crypto engine hardware for a new SHA1 operation */
210 crypto_init();
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800211
Ajay Dudanib01e5062011-12-03 23:23:42 -0800212 /* Now do SHA1 hashing */
213 ret_val = do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA1);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800214
Ajay Dudanib01e5062011-12-03 23:23:42 -0800215 if (ret_val != CRYPTO_SHA_ERR_NONE) {
216 dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val);
217 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800218
Ajay Dudanib01e5062011-12-03 23:23:42 -0800219 return ret_val;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800220}
221
222/*
223 * Common function to calculate SHA1 and SHA256 digest based on auth algorithm.
224 */
225
Ajay Dudanib01e5062011-12-03 23:23:42 -0800226static crypto_result_type
227do_sha(unsigned char *buff_ptr,
228 unsigned int buff_size,
229 unsigned char *digest_ptr, crypto_auth_alg_type auth_alg)
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800230{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800231 void *ctx_ptr = NULL;
232 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800233
Ajay Dudanib01e5062011-12-03 23:23:42 -0800234 /* Initialize SHA context based on algorithm */
235 if (auth_alg == CRYPTO_AUTH_ALG_SHA1) {
236 crypto_sha1_init(&g_sha1_ctx);
237 ctx_ptr = (void *)&g_sha1_ctx;
238 } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) {
239 crypto_sha256_init(&g_sha256_ctx);
240 ctx_ptr = (void *)&g_sha256_ctx;
241 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800242
Ajay Dudanib01e5062011-12-03 23:23:42 -0800243 ret_val =
244 do_sha_update(ctx_ptr, buff_ptr, buff_size, auth_alg, TRUE, TRUE);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800245
Ajay Dudanib01e5062011-12-03 23:23:42 -0800246 if (ret_val != CRYPTO_SHA_ERR_NONE) {
247 dprintf(CRITICAL, "do_sha_update returns error %d\n", ret_val);
248 return ret_val;
249 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800250
Ajay Dudanib01e5062011-12-03 23:23:42 -0800251 /* Copy the digest value from context pointer to digest pointer */
252 if (auth_alg == CRYPTO_AUTH_ALG_SHA1) {
253 memcpy(digest_ptr,
254 (unsigned char *)(((crypto_SHA1_ctx *) ctx_ptr)->
255 auth_iv), 20);
256 } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) {
257 memcpy(digest_ptr,
258 (unsigned char *)(((crypto_SHA256_ctx *) ctx_ptr)->
259 auth_iv), 32);
260 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800261
Ajay Dudanib01e5062011-12-03 23:23:42 -0800262 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800263}
264
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800265/*
266 * Common function to calculate SHA1 and SHA256 digest based on auth algorithm.
267 * Calls crypto engine APIs to setup SHAx registers, send the data and gets
268 * the digest.
269 */
270
Ajay Dudanib01e5062011-12-03 23:23:42 -0800271static crypto_result_type
272do_sha_update(void *ctx_ptr,
273 unsigned char *buff_ptr,
274 unsigned int buff_size,
275 crypto_auth_alg_type auth_alg, bool first, bool last)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800276{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800277 unsigned int ret_val = CRYPTO_ERR_NONE;
278 unsigned int bytes_to_write = 0;
279 unsigned int bytes_remaining = 0;
280 unsigned int tmp_bytes = 0;
281 unsigned int bytes_written = 0;
282 unsigned int tmp_buff_size = 0;
283 unsigned char *tmp_buff_ptr = NULL;
284 unsigned char tmp_saved_buff_indx = 0;
285 bool tmp_first;
286 bool tmp_last;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800287
Ajay Dudanib01e5062011-12-03 23:23:42 -0800288 /* Type casting to SHA1 context as offset is similar for SHA256 context */
289 crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800290
Ajay Dudanib01e5062011-12-03 23:23:42 -0800291 bytes_to_write = calc_num_bytes_to_send(ctx_ptr, buff_size, last);
292 bytes_remaining =
293 buff_size + sha1_ctx->saved_buff_indx - bytes_to_write;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800294
Ajay Dudanib01e5062011-12-03 23:23:42 -0800295 tmp_first = first;
296 tmp_saved_buff_indx = sha1_ctx->saved_buff_indx;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800297
Ajay Dudanib01e5062011-12-03 23:23:42 -0800298 do {
299 if ((bytes_to_write - bytes_written) >
Amol Jadi7d3ad062013-01-04 15:30:19 -0800300 crypto_get_max_auth_blk_size()) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800301 /* Write CRYPTO_MAX_AUTH_BLOCK_SIZE bytes at a time to the CE */
Amol Jadi7d3ad062013-01-04 15:30:19 -0800302 tmp_bytes = crypto_get_max_auth_blk_size();
Ajay Dudanib01e5062011-12-03 23:23:42 -0800303 tmp_last = FALSE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800304
Ajay Dudanib01e5062011-12-03 23:23:42 -0800305 if (sha1_ctx->saved_buff_indx != 0) {
306 tmp_buff_ptr = buff_ptr;
307 tmp_buff_size =
308 tmp_bytes - sha1_ctx->saved_buff_indx;
309 } else {
310 tmp_buff_ptr =
311 buff_ptr + bytes_written -
312 tmp_saved_buff_indx;
313 tmp_buff_size = tmp_bytes;
314 }
315 } else {
316 /* Since bytes_to_write are less than CRYPTO_MAX_AUTH_BLOCK_SIZE
317 write all remaining bytes now */
318 if (sha1_ctx->saved_buff_indx != 0) {
319 tmp_buff_ptr = buff_ptr;
320 tmp_buff_size =
321 bytes_to_write - bytes_written -
322 sha1_ctx->saved_buff_indx;
323 } else {
324 tmp_buff_ptr =
325 buff_ptr + bytes_written -
326 tmp_saved_buff_indx;
327 tmp_buff_size =
328 bytes_to_write - bytes_written -
329 tmp_saved_buff_indx;
330 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800331
Ajay Dudanib01e5062011-12-03 23:23:42 -0800332 tmp_bytes = (bytes_to_write - bytes_written);
333 tmp_last = last;
334 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800335
Ajay Dudanib01e5062011-12-03 23:23:42 -0800336 /* Set SHAx context in the crypto engine */
337 crypto_set_sha_ctx(ctx_ptr, tmp_bytes, auth_alg, tmp_first,
338 tmp_last);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800339
Ajay Dudanib01e5062011-12-03 23:23:42 -0800340 /* Send data to the crypto engine */
341 crypto_send_data(ctx_ptr, tmp_buff_ptr, tmp_buff_size,
342 tmp_bytes, &ret_val);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800343
Ajay Dudanib01e5062011-12-03 23:23:42 -0800344 if (ret_val != CRYPTO_ERR_NONE) {
345 dprintf(CRITICAL,
346 "do_sha_update returns error from crypto_send_data\n");
347 return CRYPTO_SHA_ERR_FAIL;
348 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800349
Ajay Dudanib01e5062011-12-03 23:23:42 -0800350 /* Get the SHAx digest from the crypto engine */
351 crypto_get_digest((unsigned char *)(sha1_ctx->auth_iv),
352 &ret_val, auth_alg, tmp_last);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800353
Ajay Dudanib01e5062011-12-03 23:23:42 -0800354 if (ret_val != CRYPTO_ERR_NONE) {
355 dprintf(CRITICAL,
356 "do_sha_update returns error from crypto_get_digest\n");
357 return CRYPTO_SHA_ERR_FAIL;
358 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800359
Ajay Dudanib01e5062011-12-03 23:23:42 -0800360 if (!tmp_last) {
361 crypto_get_ctx(ctx_ptr);
362 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800363
Ajay Dudanib01e5062011-12-03 23:23:42 -0800364 bytes_written += tmp_bytes;
365 sha1_ctx->saved_buff_indx = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800366
Ajay Dudanib01e5062011-12-03 23:23:42 -0800367 if (bytes_written != bytes_to_write) {
368 tmp_first = FALSE;
369 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800370
Ajay Dudanib01e5062011-12-03 23:23:42 -0800371 }
372 while ((bytes_to_write - bytes_written) != 0);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800373
Ajay Dudanib01e5062011-12-03 23:23:42 -0800374 /* If there are bytes remaining, copy it to saved_buff */
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800375
Ajay Dudanib01e5062011-12-03 23:23:42 -0800376 if (bytes_remaining) {
377 memcpy(sha1_ctx->saved_buff,
378 (buff_ptr + buff_size - bytes_remaining),
379 bytes_remaining);
380 sha1_ctx->saved_buff_indx = bytes_remaining;
381 } else {
382 sha1_ctx->saved_buff_indx = 0;
383 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800384
Ajay Dudanib01e5062011-12-03 23:23:42 -0800385 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800386}
387
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800388/*
389 * Function to calculate the number of bytes to be sent to crypto engine.
390 */
391
Ajay Dudanib01e5062011-12-03 23:23:42 -0800392static unsigned int
393calc_num_bytes_to_send(void *ctx_ptr, unsigned int buff_size, bool last)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800394{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800395 unsigned int bytes_to_write = 0;
396 crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800397
Ajay Dudanib01e5062011-12-03 23:23:42 -0800398 if (last) {
399 bytes_to_write = buff_size + sha1_ctx->saved_buff_indx;
400 } else {
401 bytes_to_write = ((buff_size + sha1_ctx->saved_buff_indx) /
402 CRYPTO_SHA_BLOCK_SIZE) *
403 CRYPTO_SHA_BLOCK_SIZE;
404 }
405 return bytes_to_write;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800406}