blob: 499362285040d482bd9827bf41a10963a199ff53 [file] [log] [blame]
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -08001/* 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"
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;
36static unsigned char crypto_init_done = FALSE;
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;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080051
Ajay Dudanib01e5062011-12-03 23:23:42 -080052 if (auth_alg == 1) {
Channagoud Kadabi2b204562012-02-02 12:26:46 +053053#ifdef NO_CRYPTO_ENG
54 /* Hardware CE is not present , use software hashing */
55 digest = SHA1(addr, size, digest);
56#else
Ajay Dudanib01e5062011-12-03 23:23:42 -080057 ret_val = crypto_sha1(addr, size, digest);
Channagoud Kadabi2b204562012-02-02 12:26:46 +053058#endif
Ajay Dudanib01e5062011-12-03 23:23:42 -080059 } else if (auth_alg == 2) {
Channagoud Kadabi2b204562012-02-02 12:26:46 +053060#ifdef NO_CRYPTO_ENG
61 /* Hardware CE is not present , use software hashing */
62 digest = SHA256(addr, size, digest);
63#else
Ajay Dudanib01e5062011-12-03 23:23:42 -080064 ret_val = crypto_sha256(addr, size, digest);
Channagoud Kadabi2b204562012-02-02 12:26:46 +053065#endif
Ajay Dudanib01e5062011-12-03 23:23:42 -080066 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080067
Ajay Dudanib01e5062011-12-03 23:23:42 -080068 if (ret_val != CRYPTO_SHA_ERR_NONE) {
69 dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val);
70 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080071}
72
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080073/*
74 * Function to reset and init crypto engine. It resets the engine for the
75 * first time. Used for multiple SHA operations.
76 */
77
78static void crypto_init(void)
79{
Ajay Dudanib01e5062011-12-03 23:23:42 -080080 if (crypto_init_done != TRUE) {
81 ce_clock_init();
82 crypto_eng_reset();
83 crypto_init_done = TRUE;
84 }
85 crypto_eng_init();
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080086}
87
88/*
89 * Function to initialize SHA256 context
90 */
91
Ajay Dudanib01e5062011-12-03 23:23:42 -080092static crypto_result_type crypto_sha256_init(crypto_SHA256_ctx * ctx_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080093{
Ajay Dudanib01e5062011-12-03 23:23:42 -080094 unsigned int i;
95 /* Standard initialization vector for SHA256 */
96 unsigned int sha256_init_vector[] = { 0x6A09E667, 0xBB67AE85,
97 0x3C6EF372, 0xA54FF53A,
98 0x510E527F, 0x9B05688C,
99 0x1F83D9AB, 0x5BE0CD19
100 };
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800101
Ajay Dudanib01e5062011-12-03 23:23:42 -0800102 if (ctx_ptr == NULL) {
103 return CRYPTO_SHA_ERR_INVALID_PARAM;
104 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800105
Ajay Dudanib01e5062011-12-03 23:23:42 -0800106 ctx_ptr->auth_bytecnt[0] = 0;
107 ctx_ptr->auth_bytecnt[1] = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800108
Ajay Dudanib01e5062011-12-03 23:23:42 -0800109 memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800110
Ajay Dudanib01e5062011-12-03 23:23:42 -0800111 for (i = 0; i < SHA256_INIT_VECTOR_SIZE; i++) {
112 ctx_ptr->auth_iv[i] = sha256_init_vector[i];
113 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800114
Ajay Dudanib01e5062011-12-03 23:23:42 -0800115 ctx_ptr->saved_buff_indx = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800116
Ajay Dudanib01e5062011-12-03 23:23:42 -0800117 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800118}
119
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800120/*
121 * Function to initialize SHA1 context
122 */
123
Ajay Dudanib01e5062011-12-03 23:23:42 -0800124static crypto_result_type crypto_sha1_init(crypto_SHA1_ctx * ctx_ptr)
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800125{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800126 unsigned int i;
127 /* Standard initialization vector for SHA1 */
128 unsigned int sha1_init_vector[] = { 0x67452301, 0xEFCDAB89,
129 0x98BADCFE, 0x10325476,
130 0xC3D2E1F0
131 };
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800132
Ajay Dudanib01e5062011-12-03 23:23:42 -0800133 if (ctx_ptr == NULL) {
134 return CRYPTO_SHA_ERR_INVALID_PARAM;
135 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800136
Ajay Dudanib01e5062011-12-03 23:23:42 -0800137 ctx_ptr->auth_bytecnt[0] = 0;
138 ctx_ptr->auth_bytecnt[1] = 0;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800139
Ajay Dudanib01e5062011-12-03 23:23:42 -0800140 memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800141
Ajay Dudanib01e5062011-12-03 23:23:42 -0800142 for (i = 0; i < SHA1_INIT_VECTOR_SIZE; i++) {
143 ctx_ptr->auth_iv[i] = sha1_init_vector[i];
144 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800145
Ajay Dudanib01e5062011-12-03 23:23:42 -0800146 ctx_ptr->saved_buff_indx = 0;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800147
Ajay Dudanib01e5062011-12-03 23:23:42 -0800148 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800149}
150
151/*
152 * Function to calculate SHA256 digest of given data buffer.
153 * It works on contiguous data and gives digest in single pass.
154 */
155
Ajay Dudanib01e5062011-12-03 23:23:42 -0800156static crypto_result_type
157crypto_sha256(unsigned char *buff_ptr,
158 unsigned int buff_size, unsigned char *digest_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800159{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800160 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800161
Ajay Dudanib01e5062011-12-03 23:23:42 -0800162 if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) {
163 return CRYPTO_SHA_ERR_INVALID_PARAM;
164 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800165
Ajay Dudanib01e5062011-12-03 23:23:42 -0800166 /* Initialize crypto engine hardware for a new SHA256 operation */
167 crypto_init();
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800168
Ajay Dudanib01e5062011-12-03 23:23:42 -0800169 /* Now do SHA256 hashing */
170 ret_val =
171 do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA256);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800172
Ajay Dudanib01e5062011-12-03 23:23:42 -0800173 if (ret_val != CRYPTO_SHA_ERR_NONE) {
174 dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val);
175 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800176
Ajay Dudanib01e5062011-12-03 23:23:42 -0800177 return ret_val;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800178}
179
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800180/*
181 * Function to calculate SHA1 digest of given data buffer.
182 * It works on contiguous data and gives digest in single pass.
183 */
184
Ajay Dudanib01e5062011-12-03 23:23:42 -0800185static crypto_result_type
186crypto_sha1(unsigned char *buff_ptr,
187 unsigned int buff_size, unsigned char *digest_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800188{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800189 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800190
Ajay Dudanib01e5062011-12-03 23:23:42 -0800191 if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) {
192 return CRYPTO_SHA_ERR_INVALID_PARAM;
193 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800194
Ajay Dudanib01e5062011-12-03 23:23:42 -0800195 /* Initialize crypto engine hardware for a new SHA1 operation */
196 crypto_init();
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800197
Ajay Dudanib01e5062011-12-03 23:23:42 -0800198 /* Now do SHA1 hashing */
199 ret_val = do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA1);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800200
Ajay Dudanib01e5062011-12-03 23:23:42 -0800201 if (ret_val != CRYPTO_SHA_ERR_NONE) {
202 dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val);
203 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800204
Ajay Dudanib01e5062011-12-03 23:23:42 -0800205 return ret_val;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800206}
207
208/*
209 * Common function to calculate SHA1 and SHA256 digest based on auth algorithm.
210 */
211
Ajay Dudanib01e5062011-12-03 23:23:42 -0800212static crypto_result_type
213do_sha(unsigned char *buff_ptr,
214 unsigned int buff_size,
215 unsigned char *digest_ptr, crypto_auth_alg_type auth_alg)
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800216{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800217 void *ctx_ptr = NULL;
218 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800219
Ajay Dudanib01e5062011-12-03 23:23:42 -0800220 /* Initialize SHA context based on algorithm */
221 if (auth_alg == CRYPTO_AUTH_ALG_SHA1) {
222 crypto_sha1_init(&g_sha1_ctx);
223 ctx_ptr = (void *)&g_sha1_ctx;
224 } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) {
225 crypto_sha256_init(&g_sha256_ctx);
226 ctx_ptr = (void *)&g_sha256_ctx;
227 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800228
Ajay Dudanib01e5062011-12-03 23:23:42 -0800229 ret_val =
230 do_sha_update(ctx_ptr, buff_ptr, buff_size, auth_alg, TRUE, TRUE);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800231
Ajay Dudanib01e5062011-12-03 23:23:42 -0800232 if (ret_val != CRYPTO_SHA_ERR_NONE) {
233 dprintf(CRITICAL, "do_sha_update returns error %d\n", ret_val);
234 return ret_val;
235 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800236
Ajay Dudanib01e5062011-12-03 23:23:42 -0800237 /* Copy the digest value from context pointer to digest pointer */
238 if (auth_alg == CRYPTO_AUTH_ALG_SHA1) {
239 memcpy(digest_ptr,
240 (unsigned char *)(((crypto_SHA1_ctx *) ctx_ptr)->
241 auth_iv), 20);
242 } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) {
243 memcpy(digest_ptr,
244 (unsigned char *)(((crypto_SHA256_ctx *) ctx_ptr)->
245 auth_iv), 32);
246 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800247
Ajay Dudanib01e5062011-12-03 23:23:42 -0800248 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800249}
250
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800251/*
252 * Common function to calculate SHA1 and SHA256 digest based on auth algorithm.
253 * Calls crypto engine APIs to setup SHAx registers, send the data and gets
254 * the digest.
255 */
256
Ajay Dudanib01e5062011-12-03 23:23:42 -0800257static crypto_result_type
258do_sha_update(void *ctx_ptr,
259 unsigned char *buff_ptr,
260 unsigned int buff_size,
261 crypto_auth_alg_type auth_alg, bool first, bool last)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800262{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800263 unsigned int ret_val = CRYPTO_ERR_NONE;
264 unsigned int bytes_to_write = 0;
265 unsigned int bytes_remaining = 0;
266 unsigned int tmp_bytes = 0;
267 unsigned int bytes_written = 0;
268 unsigned int tmp_buff_size = 0;
269 unsigned char *tmp_buff_ptr = NULL;
270 unsigned char tmp_saved_buff_indx = 0;
271 bool tmp_first;
272 bool tmp_last;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800273
Ajay Dudanib01e5062011-12-03 23:23:42 -0800274 /* Type casting to SHA1 context as offset is similar for SHA256 context */
275 crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800276
Ajay Dudanib01e5062011-12-03 23:23:42 -0800277 bytes_to_write = calc_num_bytes_to_send(ctx_ptr, buff_size, last);
278 bytes_remaining =
279 buff_size + sha1_ctx->saved_buff_indx - bytes_to_write;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800280
Ajay Dudanib01e5062011-12-03 23:23:42 -0800281 tmp_first = first;
282 tmp_saved_buff_indx = sha1_ctx->saved_buff_indx;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800283
Ajay Dudanib01e5062011-12-03 23:23:42 -0800284 do {
285 if ((bytes_to_write - bytes_written) >
286 CRYPTO_MAX_AUTH_BLOCK_SIZE) {
287 /* Write CRYPTO_MAX_AUTH_BLOCK_SIZE bytes at a time to the CE */
288 tmp_bytes = CRYPTO_MAX_AUTH_BLOCK_SIZE;
289 tmp_last = FALSE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800290
Ajay Dudanib01e5062011-12-03 23:23:42 -0800291 if (sha1_ctx->saved_buff_indx != 0) {
292 tmp_buff_ptr = buff_ptr;
293 tmp_buff_size =
294 tmp_bytes - sha1_ctx->saved_buff_indx;
295 } else {
296 tmp_buff_ptr =
297 buff_ptr + bytes_written -
298 tmp_saved_buff_indx;
299 tmp_buff_size = tmp_bytes;
300 }
301 } else {
302 /* Since bytes_to_write are less than CRYPTO_MAX_AUTH_BLOCK_SIZE
303 write all remaining bytes now */
304 if (sha1_ctx->saved_buff_indx != 0) {
305 tmp_buff_ptr = buff_ptr;
306 tmp_buff_size =
307 bytes_to_write - bytes_written -
308 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 =
314 bytes_to_write - bytes_written -
315 tmp_saved_buff_indx;
316 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800317
Ajay Dudanib01e5062011-12-03 23:23:42 -0800318 tmp_bytes = (bytes_to_write - bytes_written);
319 tmp_last = last;
320 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800321
Ajay Dudanib01e5062011-12-03 23:23:42 -0800322 /* Set SHAx context in the crypto engine */
323 crypto_set_sha_ctx(ctx_ptr, tmp_bytes, auth_alg, tmp_first,
324 tmp_last);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800325
Ajay Dudanib01e5062011-12-03 23:23:42 -0800326 /* Send data to the crypto engine */
327 crypto_send_data(ctx_ptr, tmp_buff_ptr, tmp_buff_size,
328 tmp_bytes, &ret_val);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800329
Ajay Dudanib01e5062011-12-03 23:23:42 -0800330 if (ret_val != CRYPTO_ERR_NONE) {
331 dprintf(CRITICAL,
332 "do_sha_update returns error from crypto_send_data\n");
333 return CRYPTO_SHA_ERR_FAIL;
334 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800335
Ajay Dudanib01e5062011-12-03 23:23:42 -0800336 /* Get the SHAx digest from the crypto engine */
337 crypto_get_digest((unsigned char *)(sha1_ctx->auth_iv),
338 &ret_val, auth_alg, tmp_last);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800339
Ajay Dudanib01e5062011-12-03 23:23:42 -0800340 if (ret_val != CRYPTO_ERR_NONE) {
341 dprintf(CRITICAL,
342 "do_sha_update returns error from crypto_get_digest\n");
343 return CRYPTO_SHA_ERR_FAIL;
344 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800345
Ajay Dudanib01e5062011-12-03 23:23:42 -0800346 if (!tmp_last) {
347 crypto_get_ctx(ctx_ptr);
348 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800349
Ajay Dudanib01e5062011-12-03 23:23:42 -0800350 bytes_written += tmp_bytes;
351 sha1_ctx->saved_buff_indx = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800352
Ajay Dudanib01e5062011-12-03 23:23:42 -0800353 if (bytes_written != bytes_to_write) {
354 tmp_first = FALSE;
355 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800356
Ajay Dudanib01e5062011-12-03 23:23:42 -0800357 }
358 while ((bytes_to_write - bytes_written) != 0);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800359
Ajay Dudanib01e5062011-12-03 23:23:42 -0800360 /* If there are bytes remaining, copy it to saved_buff */
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800361
Ajay Dudanib01e5062011-12-03 23:23:42 -0800362 if (bytes_remaining) {
363 memcpy(sha1_ctx->saved_buff,
364 (buff_ptr + buff_size - bytes_remaining),
365 bytes_remaining);
366 sha1_ctx->saved_buff_indx = bytes_remaining;
367 } else {
368 sha1_ctx->saved_buff_indx = 0;
369 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800370
Ajay Dudanib01e5062011-12-03 23:23:42 -0800371 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800372}
373
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800374/*
375 * Function to calculate the number of bytes to be sent to crypto engine.
376 */
377
Ajay Dudanib01e5062011-12-03 23:23:42 -0800378static unsigned int
379calc_num_bytes_to_send(void *ctx_ptr, unsigned int buff_size, bool last)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800380{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800381 unsigned int bytes_to_write = 0;
382 crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800383
Ajay Dudanib01e5062011-12-03 23:23:42 -0800384 if (last) {
385 bytes_to_write = buff_size + sha1_ctx->saved_buff_indx;
386 } else {
387 bytes_to_write = ((buff_size + sha1_ctx->saved_buff_indx) /
388 CRYPTO_SHA_BLOCK_SIZE) *
389 CRYPTO_SHA_BLOCK_SIZE;
390 }
391 return bytes_to_write;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800392}