blob: 9a2cdc98d8f0d9c9a1bec64af8051401bc95f19f [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>
vijay kumar4f4405f2014-08-08 11:49:53 +053030#include <sha.h>
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080031#include <debug.h>
32#include <sys/types.h>
33#include "crypto_hash.h"
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080034
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080035static crypto_SHA256_ctx g_sha256_ctx;
36static crypto_SHA1_ctx g_sha1_ctx;
Channagoud Kadabid7df33d2013-12-04 12:35:46 -080037static bool crypto_init_done;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080038
Shashank Mittal1fcde7a2011-07-25 13:41:50 -070039extern void ce_clock_init(void);
40
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080041/*
42 * Top level function which calculates SHAx digest with given data and size.
43 * Digest varies based on the authentication algorithm.
44 * It works on contiguous data and does single pass calculation.
45 */
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080046
Ajay Dudanib01e5062011-12-03 23:23:42 -080047void
48hash_find(unsigned char *addr, unsigned int size, unsigned char *digest,
49 unsigned char auth_alg)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080050{
Ajay Dudanib01e5062011-12-03 23:23:42 -080051 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Deepa Dinamani5e5c21a2012-02-16 18:59:57 -080052 crypto_engine_type platform_ce_type = board_ce_type();
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080053
Sundarajan Srinivasan6a2bad22013-11-11 18:48:27 -080054 if (auth_alg == CRYPTO_AUTH_ALG_SHA1) {
Deepa Dinamani5e5c21a2012-02-16 18:59:57 -080055 if(platform_ce_type == CRYPTO_ENGINE_TYPE_SW)
56 /* Hardware CE is not present , use software hashing */
57 digest = SHA1(addr, size, digest);
58 else if (platform_ce_type == CRYPTO_ENGINE_TYPE_HW)
59 ret_val = crypto_sha1(addr, size, digest);
60 else
61 ret_val = CRYPTO_SHA_ERR_FAIL;
Sundarajan Srinivasan6a2bad22013-11-11 18:48:27 -080062 } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) {
Deepa Dinamani5e5c21a2012-02-16 18:59:57 -080063 if(platform_ce_type == CRYPTO_ENGINE_TYPE_SW)
64 /* Hardware CE is not present , use software hashing */
65 digest = SHA256(addr, size, digest);
66 else if (platform_ce_type == CRYPTO_ENGINE_TYPE_HW)
67 ret_val = crypto_sha256(addr, size, digest);
68 else
69 ret_val = CRYPTO_SHA_ERR_FAIL;
Ajay Dudanib01e5062011-12-03 23:23:42 -080070 }
Sundarajan Srinivasan6a2bad22013-11-11 18:48:27 -080071 else
72 ret_val = CRYPTO_SHA_ERR_FAIL;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080073
Ajay Dudanib01e5062011-12-03 23:23:42 -080074 if (ret_val != CRYPTO_SHA_ERR_NONE) {
75 dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val);
76 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080077}
78
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080079/*
80 * Function to reset and init crypto engine. It resets the engine for the
81 * first time. Used for multiple SHA operations.
82 */
83
84static void crypto_init(void)
85{
Ajay Dudanib01e5062011-12-03 23:23:42 -080086 if (crypto_init_done != TRUE) {
87 ce_clock_init();
88 crypto_eng_reset();
89 crypto_init_done = TRUE;
90 }
91 crypto_eng_init();
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080092}
93
94/*
Channagoud Kadabid7df33d2013-12-04 12:35:46 -080095 * Function to return if crypto is initialized
96 */
97
98bool crypto_initialized()
99{
100 return crypto_init_done;
101}
102
103/*
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800104 * Function to initialize SHA256 context
105 */
106
Ajay Dudanib01e5062011-12-03 23:23:42 -0800107static crypto_result_type crypto_sha256_init(crypto_SHA256_ctx * ctx_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800108{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800109 unsigned int i;
110 /* Standard initialization vector for SHA256 */
111 unsigned int sha256_init_vector[] = { 0x6A09E667, 0xBB67AE85,
112 0x3C6EF372, 0xA54FF53A,
113 0x510E527F, 0x9B05688C,
114 0x1F83D9AB, 0x5BE0CD19
115 };
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800116
Ajay Dudanib01e5062011-12-03 23:23:42 -0800117 if (ctx_ptr == NULL) {
118 return CRYPTO_SHA_ERR_INVALID_PARAM;
119 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800120
Ajay Dudanib01e5062011-12-03 23:23:42 -0800121 ctx_ptr->auth_bytecnt[0] = 0;
122 ctx_ptr->auth_bytecnt[1] = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800123
Ajay Dudanib01e5062011-12-03 23:23:42 -0800124 memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800125
Ajay Dudanib01e5062011-12-03 23:23:42 -0800126 for (i = 0; i < SHA256_INIT_VECTOR_SIZE; i++) {
127 ctx_ptr->auth_iv[i] = sha256_init_vector[i];
128 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800129
Ajay Dudanib01e5062011-12-03 23:23:42 -0800130 ctx_ptr->saved_buff_indx = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800131
Ajay Dudanib01e5062011-12-03 23:23:42 -0800132 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800133}
134
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800135/*
136 * Function to initialize SHA1 context
137 */
138
Ajay Dudanib01e5062011-12-03 23:23:42 -0800139static crypto_result_type crypto_sha1_init(crypto_SHA1_ctx * ctx_ptr)
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800140{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800141 unsigned int i;
142 /* Standard initialization vector for SHA1 */
143 unsigned int sha1_init_vector[] = { 0x67452301, 0xEFCDAB89,
144 0x98BADCFE, 0x10325476,
145 0xC3D2E1F0
146 };
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800147
Ajay Dudanib01e5062011-12-03 23:23:42 -0800148 if (ctx_ptr == NULL) {
149 return CRYPTO_SHA_ERR_INVALID_PARAM;
150 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800151
Ajay Dudanib01e5062011-12-03 23:23:42 -0800152 ctx_ptr->auth_bytecnt[0] = 0;
153 ctx_ptr->auth_bytecnt[1] = 0;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800154
Ajay Dudanib01e5062011-12-03 23:23:42 -0800155 memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800156
Ajay Dudanib01e5062011-12-03 23:23:42 -0800157 for (i = 0; i < SHA1_INIT_VECTOR_SIZE; i++) {
158 ctx_ptr->auth_iv[i] = sha1_init_vector[i];
159 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800160
Ajay Dudanib01e5062011-12-03 23:23:42 -0800161 ctx_ptr->saved_buff_indx = 0;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800162
Ajay Dudanib01e5062011-12-03 23:23:42 -0800163 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800164}
165
166/*
167 * Function to calculate SHA256 digest of given data buffer.
168 * It works on contiguous data and gives digest in single pass.
169 */
170
Ajay Dudanib01e5062011-12-03 23:23:42 -0800171static crypto_result_type
172crypto_sha256(unsigned char *buff_ptr,
173 unsigned int buff_size, unsigned char *digest_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800174{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800175 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800176
Ajay Dudanib01e5062011-12-03 23:23:42 -0800177 if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) {
178 return CRYPTO_SHA_ERR_INVALID_PARAM;
179 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800180
Ajay Dudanib01e5062011-12-03 23:23:42 -0800181 /* Initialize crypto engine hardware for a new SHA256 operation */
182 crypto_init();
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800183
Ajay Dudanib01e5062011-12-03 23:23:42 -0800184 /* Now do SHA256 hashing */
185 ret_val =
186 do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA256);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800187
Ajay Dudanib01e5062011-12-03 23:23:42 -0800188 if (ret_val != CRYPTO_SHA_ERR_NONE) {
189 dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val);
190 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800191
Ajay Dudanib01e5062011-12-03 23:23:42 -0800192 return ret_val;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800193}
194
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800195/*
196 * Function to calculate SHA1 digest of given data buffer.
197 * It works on contiguous data and gives digest in single pass.
198 */
199
Ajay Dudanib01e5062011-12-03 23:23:42 -0800200static crypto_result_type
201crypto_sha1(unsigned char *buff_ptr,
202 unsigned int buff_size, unsigned char *digest_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800203{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800204 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800205
Ajay Dudanib01e5062011-12-03 23:23:42 -0800206 if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) {
207 return CRYPTO_SHA_ERR_INVALID_PARAM;
208 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800209
Ajay Dudanib01e5062011-12-03 23:23:42 -0800210 /* Initialize crypto engine hardware for a new SHA1 operation */
211 crypto_init();
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800212
Ajay Dudanib01e5062011-12-03 23:23:42 -0800213 /* Now do SHA1 hashing */
214 ret_val = do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA1);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800215
Ajay Dudanib01e5062011-12-03 23:23:42 -0800216 if (ret_val != CRYPTO_SHA_ERR_NONE) {
217 dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val);
218 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800219
Ajay Dudanib01e5062011-12-03 23:23:42 -0800220 return ret_val;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800221}
222
223/*
224 * Common function to calculate SHA1 and SHA256 digest based on auth algorithm.
225 */
226
Ajay Dudanib01e5062011-12-03 23:23:42 -0800227static crypto_result_type
228do_sha(unsigned char *buff_ptr,
229 unsigned int buff_size,
230 unsigned char *digest_ptr, crypto_auth_alg_type auth_alg)
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800231{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800232 void *ctx_ptr = NULL;
233 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800234
Ajay Dudanib01e5062011-12-03 23:23:42 -0800235 /* Initialize SHA context based on algorithm */
236 if (auth_alg == CRYPTO_AUTH_ALG_SHA1) {
237 crypto_sha1_init(&g_sha1_ctx);
238 ctx_ptr = (void *)&g_sha1_ctx;
239 } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) {
240 crypto_sha256_init(&g_sha256_ctx);
241 ctx_ptr = (void *)&g_sha256_ctx;
242 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800243
Ajay Dudanib01e5062011-12-03 23:23:42 -0800244 ret_val =
245 do_sha_update(ctx_ptr, buff_ptr, buff_size, auth_alg, TRUE, TRUE);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800246
Ajay Dudanib01e5062011-12-03 23:23:42 -0800247 if (ret_val != CRYPTO_SHA_ERR_NONE) {
248 dprintf(CRITICAL, "do_sha_update returns error %d\n", ret_val);
249 return ret_val;
250 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800251
Ajay Dudanib01e5062011-12-03 23:23:42 -0800252 /* Copy the digest value from context pointer to digest pointer */
253 if (auth_alg == CRYPTO_AUTH_ALG_SHA1) {
254 memcpy(digest_ptr,
255 (unsigned char *)(((crypto_SHA1_ctx *) ctx_ptr)->
256 auth_iv), 20);
257 } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) {
258 memcpy(digest_ptr,
259 (unsigned char *)(((crypto_SHA256_ctx *) ctx_ptr)->
260 auth_iv), 32);
261 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800262
Ajay Dudanib01e5062011-12-03 23:23:42 -0800263 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800264}
265
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800266/*
267 * Common function to calculate SHA1 and SHA256 digest based on auth algorithm.
268 * Calls crypto engine APIs to setup SHAx registers, send the data and gets
269 * the digest.
270 */
271
Ajay Dudanib01e5062011-12-03 23:23:42 -0800272static crypto_result_type
273do_sha_update(void *ctx_ptr,
274 unsigned char *buff_ptr,
275 unsigned int buff_size,
276 crypto_auth_alg_type auth_alg, bool first, bool last)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800277{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800278 unsigned int ret_val = CRYPTO_ERR_NONE;
279 unsigned int bytes_to_write = 0;
280 unsigned int bytes_remaining = 0;
281 unsigned int tmp_bytes = 0;
282 unsigned int bytes_written = 0;
283 unsigned int tmp_buff_size = 0;
284 unsigned char *tmp_buff_ptr = NULL;
285 unsigned char tmp_saved_buff_indx = 0;
286 bool tmp_first;
287 bool tmp_last;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800288
Ajay Dudanib01e5062011-12-03 23:23:42 -0800289 /* Type casting to SHA1 context as offset is similar for SHA256 context */
290 crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800291
Ajay Dudanib01e5062011-12-03 23:23:42 -0800292 bytes_to_write = calc_num_bytes_to_send(ctx_ptr, buff_size, last);
293 bytes_remaining =
294 buff_size + sha1_ctx->saved_buff_indx - bytes_to_write;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800295
Ajay Dudanib01e5062011-12-03 23:23:42 -0800296 tmp_first = first;
297 tmp_saved_buff_indx = sha1_ctx->saved_buff_indx;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800298
Ajay Dudanib01e5062011-12-03 23:23:42 -0800299 do {
300 if ((bytes_to_write - bytes_written) >
Amol Jadi7d3ad062013-01-04 15:30:19 -0800301 crypto_get_max_auth_blk_size()) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800302 /* Write CRYPTO_MAX_AUTH_BLOCK_SIZE bytes at a time to the CE */
Amol Jadi7d3ad062013-01-04 15:30:19 -0800303 tmp_bytes = crypto_get_max_auth_blk_size();
Ajay Dudanib01e5062011-12-03 23:23:42 -0800304 tmp_last = FALSE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800305
Ajay Dudanib01e5062011-12-03 23:23:42 -0800306 if (sha1_ctx->saved_buff_indx != 0) {
307 tmp_buff_ptr = buff_ptr;
308 tmp_buff_size =
309 tmp_bytes - sha1_ctx->saved_buff_indx;
310 } else {
311 tmp_buff_ptr =
312 buff_ptr + bytes_written -
313 tmp_saved_buff_indx;
314 tmp_buff_size = tmp_bytes;
315 }
316 } else {
317 /* Since bytes_to_write are less than CRYPTO_MAX_AUTH_BLOCK_SIZE
318 write all remaining bytes now */
319 if (sha1_ctx->saved_buff_indx != 0) {
320 tmp_buff_ptr = buff_ptr;
321 tmp_buff_size =
322 bytes_to_write - bytes_written -
323 sha1_ctx->saved_buff_indx;
324 } else {
325 tmp_buff_ptr =
326 buff_ptr + bytes_written -
327 tmp_saved_buff_indx;
328 tmp_buff_size =
329 bytes_to_write - bytes_written -
330 tmp_saved_buff_indx;
331 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800332
Ajay Dudanib01e5062011-12-03 23:23:42 -0800333 tmp_bytes = (bytes_to_write - bytes_written);
334 tmp_last = last;
335 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800336
Ajay Dudanib01e5062011-12-03 23:23:42 -0800337 /* Set SHAx context in the crypto engine */
338 crypto_set_sha_ctx(ctx_ptr, tmp_bytes, auth_alg, tmp_first,
339 tmp_last);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800340
Ajay Dudanib01e5062011-12-03 23:23:42 -0800341 /* Send data to the crypto engine */
342 crypto_send_data(ctx_ptr, tmp_buff_ptr, tmp_buff_size,
343 tmp_bytes, &ret_val);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800344
Ajay Dudanib01e5062011-12-03 23:23:42 -0800345 if (ret_val != CRYPTO_ERR_NONE) {
346 dprintf(CRITICAL,
347 "do_sha_update returns error from crypto_send_data\n");
348 return CRYPTO_SHA_ERR_FAIL;
349 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800350
Ajay Dudanib01e5062011-12-03 23:23:42 -0800351 /* Get the SHAx digest from the crypto engine */
352 crypto_get_digest((unsigned char *)(sha1_ctx->auth_iv),
353 &ret_val, auth_alg, tmp_last);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800354
Ajay Dudanib01e5062011-12-03 23:23:42 -0800355 if (ret_val != CRYPTO_ERR_NONE) {
356 dprintf(CRITICAL,
357 "do_sha_update returns error from crypto_get_digest\n");
358 return CRYPTO_SHA_ERR_FAIL;
359 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800360
Ajay Dudanib01e5062011-12-03 23:23:42 -0800361 if (!tmp_last) {
362 crypto_get_ctx(ctx_ptr);
363 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800364
Ajay Dudanib01e5062011-12-03 23:23:42 -0800365 bytes_written += tmp_bytes;
366 sha1_ctx->saved_buff_indx = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800367
Ajay Dudanib01e5062011-12-03 23:23:42 -0800368 if (bytes_written != bytes_to_write) {
369 tmp_first = FALSE;
370 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800371
Ajay Dudanib01e5062011-12-03 23:23:42 -0800372 }
373 while ((bytes_to_write - bytes_written) != 0);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800374
Ajay Dudanib01e5062011-12-03 23:23:42 -0800375 /* If there are bytes remaining, copy it to saved_buff */
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800376
Ajay Dudanib01e5062011-12-03 23:23:42 -0800377 if (bytes_remaining) {
378 memcpy(sha1_ctx->saved_buff,
379 (buff_ptr + buff_size - bytes_remaining),
380 bytes_remaining);
381 sha1_ctx->saved_buff_indx = bytes_remaining;
382 } else {
383 sha1_ctx->saved_buff_indx = 0;
384 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800385
Ajay Dudanib01e5062011-12-03 23:23:42 -0800386 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800387}
388
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800389/*
390 * Function to calculate the number of bytes to be sent to crypto engine.
391 */
392
Ajay Dudanib01e5062011-12-03 23:23:42 -0800393static unsigned int
394calc_num_bytes_to_send(void *ctx_ptr, unsigned int buff_size, bool last)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800395{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800396 unsigned int bytes_to_write = 0;
397 crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800398
Ajay Dudanib01e5062011-12-03 23:23:42 -0800399 if (last) {
400 bytes_to_write = buff_size + sha1_ctx->saved_buff_indx;
401 } else {
402 bytes_to_write = ((buff_size + sha1_ctx->saved_buff_indx) /
403 CRYPTO_SHA_BLOCK_SIZE) *
404 CRYPTO_SHA_BLOCK_SIZE;
405 }
406 return bytes_to_write;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800407}