blob: d431f42648613f37cf10aac6e9aaab90aa6beefa [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) {
53 ret_val = crypto_sha1(addr, size, digest);
54 } else if (auth_alg == 2) {
55 ret_val = crypto_sha256(addr, size, digest);
56 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080057
Ajay Dudanib01e5062011-12-03 23:23:42 -080058 if (ret_val != CRYPTO_SHA_ERR_NONE) {
59 dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val);
60 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080061}
62
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080063/*
64 * Function to reset and init crypto engine. It resets the engine for the
65 * first time. Used for multiple SHA operations.
66 */
67
68static void crypto_init(void)
69{
Ajay Dudanib01e5062011-12-03 23:23:42 -080070 if (crypto_init_done != TRUE) {
71 ce_clock_init();
72 crypto_eng_reset();
73 crypto_init_done = TRUE;
74 }
75 crypto_eng_init();
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080076}
77
78/*
79 * Function to initialize SHA256 context
80 */
81
Ajay Dudanib01e5062011-12-03 23:23:42 -080082static crypto_result_type crypto_sha256_init(crypto_SHA256_ctx * ctx_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080083{
Ajay Dudanib01e5062011-12-03 23:23:42 -080084 unsigned int i;
85 /* Standard initialization vector for SHA256 */
86 unsigned int sha256_init_vector[] = { 0x6A09E667, 0xBB67AE85,
87 0x3C6EF372, 0xA54FF53A,
88 0x510E527F, 0x9B05688C,
89 0x1F83D9AB, 0x5BE0CD19
90 };
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080091
Ajay Dudanib01e5062011-12-03 23:23:42 -080092 if (ctx_ptr == NULL) {
93 return CRYPTO_SHA_ERR_INVALID_PARAM;
94 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080095
Ajay Dudanib01e5062011-12-03 23:23:42 -080096 ctx_ptr->auth_bytecnt[0] = 0;
97 ctx_ptr->auth_bytecnt[1] = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080098
Ajay Dudanib01e5062011-12-03 23:23:42 -080099 memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800100
Ajay Dudanib01e5062011-12-03 23:23:42 -0800101 for (i = 0; i < SHA256_INIT_VECTOR_SIZE; i++) {
102 ctx_ptr->auth_iv[i] = sha256_init_vector[i];
103 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800104
Ajay Dudanib01e5062011-12-03 23:23:42 -0800105 ctx_ptr->saved_buff_indx = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800106
Ajay Dudanib01e5062011-12-03 23:23:42 -0800107 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800108}
109
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800110/*
111 * Function to initialize SHA1 context
112 */
113
Ajay Dudanib01e5062011-12-03 23:23:42 -0800114static crypto_result_type crypto_sha1_init(crypto_SHA1_ctx * ctx_ptr)
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800115{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800116 unsigned int i;
117 /* Standard initialization vector for SHA1 */
118 unsigned int sha1_init_vector[] = { 0x67452301, 0xEFCDAB89,
119 0x98BADCFE, 0x10325476,
120 0xC3D2E1F0
121 };
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800122
Ajay Dudanib01e5062011-12-03 23:23:42 -0800123 if (ctx_ptr == NULL) {
124 return CRYPTO_SHA_ERR_INVALID_PARAM;
125 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800126
Ajay Dudanib01e5062011-12-03 23:23:42 -0800127 ctx_ptr->auth_bytecnt[0] = 0;
128 ctx_ptr->auth_bytecnt[1] = 0;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800129
Ajay Dudanib01e5062011-12-03 23:23:42 -0800130 memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800131
Ajay Dudanib01e5062011-12-03 23:23:42 -0800132 for (i = 0; i < SHA1_INIT_VECTOR_SIZE; i++) {
133 ctx_ptr->auth_iv[i] = sha1_init_vector[i];
134 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800135
Ajay Dudanib01e5062011-12-03 23:23:42 -0800136 ctx_ptr->saved_buff_indx = 0;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800137
Ajay Dudanib01e5062011-12-03 23:23:42 -0800138 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800139}
140
141/*
142 * Function to calculate SHA256 digest of given data buffer.
143 * It works on contiguous data and gives digest in single pass.
144 */
145
Ajay Dudanib01e5062011-12-03 23:23:42 -0800146static crypto_result_type
147crypto_sha256(unsigned char *buff_ptr,
148 unsigned int buff_size, unsigned char *digest_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800149{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800150 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800151
Ajay Dudanib01e5062011-12-03 23:23:42 -0800152 if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) {
153 return CRYPTO_SHA_ERR_INVALID_PARAM;
154 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800155
Ajay Dudanib01e5062011-12-03 23:23:42 -0800156 /* Initialize crypto engine hardware for a new SHA256 operation */
157 crypto_init();
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800158
Ajay Dudanib01e5062011-12-03 23:23:42 -0800159 /* Now do SHA256 hashing */
160 ret_val =
161 do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA256);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800162
Ajay Dudanib01e5062011-12-03 23:23:42 -0800163 if (ret_val != CRYPTO_SHA_ERR_NONE) {
164 dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val);
165 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800166
Ajay Dudanib01e5062011-12-03 23:23:42 -0800167 return ret_val;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800168}
169
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800170/*
171 * Function to calculate SHA1 digest of given data buffer.
172 * It works on contiguous data and gives digest in single pass.
173 */
174
Ajay Dudanib01e5062011-12-03 23:23:42 -0800175static crypto_result_type
176crypto_sha1(unsigned char *buff_ptr,
177 unsigned int buff_size, unsigned char *digest_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800178{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800179 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800180
Ajay Dudanib01e5062011-12-03 23:23:42 -0800181 if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) {
182 return CRYPTO_SHA_ERR_INVALID_PARAM;
183 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800184
Ajay Dudanib01e5062011-12-03 23:23:42 -0800185 /* Initialize crypto engine hardware for a new SHA1 operation */
186 crypto_init();
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800187
Ajay Dudanib01e5062011-12-03 23:23:42 -0800188 /* Now do SHA1 hashing */
189 ret_val = do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA1);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800190
Ajay Dudanib01e5062011-12-03 23:23:42 -0800191 if (ret_val != CRYPTO_SHA_ERR_NONE) {
192 dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val);
193 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800194
Ajay Dudanib01e5062011-12-03 23:23:42 -0800195 return ret_val;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800196}
197
198/*
199 * Common function to calculate SHA1 and SHA256 digest based on auth algorithm.
200 */
201
Ajay Dudanib01e5062011-12-03 23:23:42 -0800202static crypto_result_type
203do_sha(unsigned char *buff_ptr,
204 unsigned int buff_size,
205 unsigned char *digest_ptr, crypto_auth_alg_type auth_alg)
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800206{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800207 void *ctx_ptr = NULL;
208 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800209
Ajay Dudanib01e5062011-12-03 23:23:42 -0800210 /* Initialize SHA context based on algorithm */
211 if (auth_alg == CRYPTO_AUTH_ALG_SHA1) {
212 crypto_sha1_init(&g_sha1_ctx);
213 ctx_ptr = (void *)&g_sha1_ctx;
214 } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) {
215 crypto_sha256_init(&g_sha256_ctx);
216 ctx_ptr = (void *)&g_sha256_ctx;
217 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800218
Ajay Dudanib01e5062011-12-03 23:23:42 -0800219 ret_val =
220 do_sha_update(ctx_ptr, buff_ptr, buff_size, auth_alg, TRUE, TRUE);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800221
Ajay Dudanib01e5062011-12-03 23:23:42 -0800222 if (ret_val != CRYPTO_SHA_ERR_NONE) {
223 dprintf(CRITICAL, "do_sha_update returns error %d\n", ret_val);
224 return ret_val;
225 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800226
Ajay Dudanib01e5062011-12-03 23:23:42 -0800227 /* Copy the digest value from context pointer to digest pointer */
228 if (auth_alg == CRYPTO_AUTH_ALG_SHA1) {
229 memcpy(digest_ptr,
230 (unsigned char *)(((crypto_SHA1_ctx *) ctx_ptr)->
231 auth_iv), 20);
232 } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) {
233 memcpy(digest_ptr,
234 (unsigned char *)(((crypto_SHA256_ctx *) ctx_ptr)->
235 auth_iv), 32);
236 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800237
Ajay Dudanib01e5062011-12-03 23:23:42 -0800238 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800239}
240
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800241/*
242 * Common function to calculate SHA1 and SHA256 digest based on auth algorithm.
243 * Calls crypto engine APIs to setup SHAx registers, send the data and gets
244 * the digest.
245 */
246
Ajay Dudanib01e5062011-12-03 23:23:42 -0800247static crypto_result_type
248do_sha_update(void *ctx_ptr,
249 unsigned char *buff_ptr,
250 unsigned int buff_size,
251 crypto_auth_alg_type auth_alg, bool first, bool last)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800252{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800253 unsigned int ret_val = CRYPTO_ERR_NONE;
254 unsigned int bytes_to_write = 0;
255 unsigned int bytes_remaining = 0;
256 unsigned int tmp_bytes = 0;
257 unsigned int bytes_written = 0;
258 unsigned int tmp_buff_size = 0;
259 unsigned char *tmp_buff_ptr = NULL;
260 unsigned char tmp_saved_buff_indx = 0;
261 bool tmp_first;
262 bool tmp_last;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800263
Ajay Dudanib01e5062011-12-03 23:23:42 -0800264 /* Type casting to SHA1 context as offset is similar for SHA256 context */
265 crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800266
Ajay Dudanib01e5062011-12-03 23:23:42 -0800267 bytes_to_write = calc_num_bytes_to_send(ctx_ptr, buff_size, last);
268 bytes_remaining =
269 buff_size + sha1_ctx->saved_buff_indx - bytes_to_write;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800270
Ajay Dudanib01e5062011-12-03 23:23:42 -0800271 tmp_first = first;
272 tmp_saved_buff_indx = sha1_ctx->saved_buff_indx;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800273
Ajay Dudanib01e5062011-12-03 23:23:42 -0800274 do {
275 if ((bytes_to_write - bytes_written) >
276 CRYPTO_MAX_AUTH_BLOCK_SIZE) {
277 /* Write CRYPTO_MAX_AUTH_BLOCK_SIZE bytes at a time to the CE */
278 tmp_bytes = CRYPTO_MAX_AUTH_BLOCK_SIZE;
279 tmp_last = FALSE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800280
Ajay Dudanib01e5062011-12-03 23:23:42 -0800281 if (sha1_ctx->saved_buff_indx != 0) {
282 tmp_buff_ptr = buff_ptr;
283 tmp_buff_size =
284 tmp_bytes - sha1_ctx->saved_buff_indx;
285 } else {
286 tmp_buff_ptr =
287 buff_ptr + bytes_written -
288 tmp_saved_buff_indx;
289 tmp_buff_size = tmp_bytes;
290 }
291 } else {
292 /* Since bytes_to_write are less than CRYPTO_MAX_AUTH_BLOCK_SIZE
293 write all remaining bytes now */
294 if (sha1_ctx->saved_buff_indx != 0) {
295 tmp_buff_ptr = buff_ptr;
296 tmp_buff_size =
297 bytes_to_write - bytes_written -
298 sha1_ctx->saved_buff_indx;
299 } else {
300 tmp_buff_ptr =
301 buff_ptr + bytes_written -
302 tmp_saved_buff_indx;
303 tmp_buff_size =
304 bytes_to_write - bytes_written -
305 tmp_saved_buff_indx;
306 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800307
Ajay Dudanib01e5062011-12-03 23:23:42 -0800308 tmp_bytes = (bytes_to_write - bytes_written);
309 tmp_last = last;
310 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800311
Ajay Dudanib01e5062011-12-03 23:23:42 -0800312 /* Set SHAx context in the crypto engine */
313 crypto_set_sha_ctx(ctx_ptr, tmp_bytes, auth_alg, tmp_first,
314 tmp_last);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800315
Ajay Dudanib01e5062011-12-03 23:23:42 -0800316 /* Send data to the crypto engine */
317 crypto_send_data(ctx_ptr, tmp_buff_ptr, tmp_buff_size,
318 tmp_bytes, &ret_val);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800319
Ajay Dudanib01e5062011-12-03 23:23:42 -0800320 if (ret_val != CRYPTO_ERR_NONE) {
321 dprintf(CRITICAL,
322 "do_sha_update returns error from crypto_send_data\n");
323 return CRYPTO_SHA_ERR_FAIL;
324 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800325
Ajay Dudanib01e5062011-12-03 23:23:42 -0800326 /* Get the SHAx digest from the crypto engine */
327 crypto_get_digest((unsigned char *)(sha1_ctx->auth_iv),
328 &ret_val, auth_alg, tmp_last);
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_get_digest\n");
333 return CRYPTO_SHA_ERR_FAIL;
334 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800335
Ajay Dudanib01e5062011-12-03 23:23:42 -0800336 if (!tmp_last) {
337 crypto_get_ctx(ctx_ptr);
338 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800339
Ajay Dudanib01e5062011-12-03 23:23:42 -0800340 bytes_written += tmp_bytes;
341 sha1_ctx->saved_buff_indx = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800342
Ajay Dudanib01e5062011-12-03 23:23:42 -0800343 if (bytes_written != bytes_to_write) {
344 tmp_first = FALSE;
345 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800346
Ajay Dudanib01e5062011-12-03 23:23:42 -0800347 }
348 while ((bytes_to_write - bytes_written) != 0);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800349
Ajay Dudanib01e5062011-12-03 23:23:42 -0800350 /* If there are bytes remaining, copy it to saved_buff */
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800351
Ajay Dudanib01e5062011-12-03 23:23:42 -0800352 if (bytes_remaining) {
353 memcpy(sha1_ctx->saved_buff,
354 (buff_ptr + buff_size - bytes_remaining),
355 bytes_remaining);
356 sha1_ctx->saved_buff_indx = bytes_remaining;
357 } else {
358 sha1_ctx->saved_buff_indx = 0;
359 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800360
Ajay Dudanib01e5062011-12-03 23:23:42 -0800361 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800362}
363
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800364/*
365 * Function to calculate the number of bytes to be sent to crypto engine.
366 */
367
Ajay Dudanib01e5062011-12-03 23:23:42 -0800368static unsigned int
369calc_num_bytes_to_send(void *ctx_ptr, unsigned int buff_size, bool last)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800370{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800371 unsigned int bytes_to_write = 0;
372 crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800373
Ajay Dudanib01e5062011-12-03 23:23:42 -0800374 if (last) {
375 bytes_to_write = buff_size + sha1_ctx->saved_buff_indx;
376 } else {
377 bytes_to_write = ((buff_size + sha1_ctx->saved_buff_indx) /
378 CRYPTO_SHA_BLOCK_SIZE) *
379 CRYPTO_SHA_BLOCK_SIZE;
380 }
381 return bytes_to_write;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800382}