blob: 50d4a1796dea7c130cf296d9a8c5838d1f9154c6 [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
Ajay Dudanib01e5062011-12-03 23:23:42 -080053 if (auth_alg == 1) {
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;
Ajay Dudanib01e5062011-12-03 23:23:42 -080061 } else if (auth_alg == 2) {
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 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080070
Ajay Dudanib01e5062011-12-03 23:23:42 -080071 if (ret_val != CRYPTO_SHA_ERR_NONE) {
72 dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val);
73 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080074}
75
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080076/*
77 * Function to reset and init crypto engine. It resets the engine for the
78 * first time. Used for multiple SHA operations.
79 */
80
81static void crypto_init(void)
82{
Ajay Dudanib01e5062011-12-03 23:23:42 -080083 if (crypto_init_done != TRUE) {
84 ce_clock_init();
85 crypto_eng_reset();
86 crypto_init_done = TRUE;
87 }
88 crypto_eng_init();
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080089}
90
91/*
Channagoud Kadabi1d3e9b42013-12-04 12:35:46 -080092 * Function to return if crypto is initialized
93 */
94
95bool crypto_initialized()
96{
97 return crypto_init_done;
98}
99
100/*
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800101 * Function to initialize SHA256 context
102 */
103
Ajay Dudanib01e5062011-12-03 23:23:42 -0800104static crypto_result_type crypto_sha256_init(crypto_SHA256_ctx * ctx_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800105{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800106 unsigned int i;
107 /* Standard initialization vector for SHA256 */
108 unsigned int sha256_init_vector[] = { 0x6A09E667, 0xBB67AE85,
109 0x3C6EF372, 0xA54FF53A,
110 0x510E527F, 0x9B05688C,
111 0x1F83D9AB, 0x5BE0CD19
112 };
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800113
Ajay Dudanib01e5062011-12-03 23:23:42 -0800114 if (ctx_ptr == NULL) {
115 return CRYPTO_SHA_ERR_INVALID_PARAM;
116 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800117
Ajay Dudanib01e5062011-12-03 23:23:42 -0800118 ctx_ptr->auth_bytecnt[0] = 0;
119 ctx_ptr->auth_bytecnt[1] = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800120
Ajay Dudanib01e5062011-12-03 23:23:42 -0800121 memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800122
Ajay Dudanib01e5062011-12-03 23:23:42 -0800123 for (i = 0; i < SHA256_INIT_VECTOR_SIZE; i++) {
124 ctx_ptr->auth_iv[i] = sha256_init_vector[i];
125 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800126
Ajay Dudanib01e5062011-12-03 23:23:42 -0800127 ctx_ptr->saved_buff_indx = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800128
Ajay Dudanib01e5062011-12-03 23:23:42 -0800129 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800130}
131
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800132/*
133 * Function to initialize SHA1 context
134 */
135
Ajay Dudanib01e5062011-12-03 23:23:42 -0800136static crypto_result_type crypto_sha1_init(crypto_SHA1_ctx * ctx_ptr)
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800137{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800138 unsigned int i;
139 /* Standard initialization vector for SHA1 */
140 unsigned int sha1_init_vector[] = { 0x67452301, 0xEFCDAB89,
141 0x98BADCFE, 0x10325476,
142 0xC3D2E1F0
143 };
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800144
Ajay Dudanib01e5062011-12-03 23:23:42 -0800145 if (ctx_ptr == NULL) {
146 return CRYPTO_SHA_ERR_INVALID_PARAM;
147 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800148
Ajay Dudanib01e5062011-12-03 23:23:42 -0800149 ctx_ptr->auth_bytecnt[0] = 0;
150 ctx_ptr->auth_bytecnt[1] = 0;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800151
Ajay Dudanib01e5062011-12-03 23:23:42 -0800152 memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800153
Ajay Dudanib01e5062011-12-03 23:23:42 -0800154 for (i = 0; i < SHA1_INIT_VECTOR_SIZE; i++) {
155 ctx_ptr->auth_iv[i] = sha1_init_vector[i];
156 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800157
Ajay Dudanib01e5062011-12-03 23:23:42 -0800158 ctx_ptr->saved_buff_indx = 0;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800159
Ajay Dudanib01e5062011-12-03 23:23:42 -0800160 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800161}
162
163/*
164 * Function to calculate SHA256 digest of given data buffer.
165 * It works on contiguous data and gives digest in single pass.
166 */
167
Ajay Dudanib01e5062011-12-03 23:23:42 -0800168static crypto_result_type
169crypto_sha256(unsigned char *buff_ptr,
170 unsigned int buff_size, unsigned char *digest_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800171{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800172 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800173
Ajay Dudanib01e5062011-12-03 23:23:42 -0800174 if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) {
175 return CRYPTO_SHA_ERR_INVALID_PARAM;
176 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800177
Ajay Dudanib01e5062011-12-03 23:23:42 -0800178 /* Initialize crypto engine hardware for a new SHA256 operation */
179 crypto_init();
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800180
Ajay Dudanib01e5062011-12-03 23:23:42 -0800181 /* Now do SHA256 hashing */
182 ret_val =
183 do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA256);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800184
Ajay Dudanib01e5062011-12-03 23:23:42 -0800185 if (ret_val != CRYPTO_SHA_ERR_NONE) {
186 dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val);
187 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800188
Ajay Dudanib01e5062011-12-03 23:23:42 -0800189 return ret_val;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800190}
191
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800192/*
193 * Function to calculate SHA1 digest of given data buffer.
194 * It works on contiguous data and gives digest in single pass.
195 */
196
Ajay Dudanib01e5062011-12-03 23:23:42 -0800197static crypto_result_type
198crypto_sha1(unsigned char *buff_ptr,
199 unsigned int buff_size, unsigned char *digest_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800200{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800201 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800202
Ajay Dudanib01e5062011-12-03 23:23:42 -0800203 if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) {
204 return CRYPTO_SHA_ERR_INVALID_PARAM;
205 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800206
Ajay Dudanib01e5062011-12-03 23:23:42 -0800207 /* Initialize crypto engine hardware for a new SHA1 operation */
208 crypto_init();
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800209
Ajay Dudanib01e5062011-12-03 23:23:42 -0800210 /* Now do SHA1 hashing */
211 ret_val = do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA1);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800212
Ajay Dudanib01e5062011-12-03 23:23:42 -0800213 if (ret_val != CRYPTO_SHA_ERR_NONE) {
214 dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val);
215 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800216
Ajay Dudanib01e5062011-12-03 23:23:42 -0800217 return ret_val;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800218}
219
220/*
221 * Common function to calculate SHA1 and SHA256 digest based on auth algorithm.
222 */
223
Ajay Dudanib01e5062011-12-03 23:23:42 -0800224static crypto_result_type
225do_sha(unsigned char *buff_ptr,
226 unsigned int buff_size,
227 unsigned char *digest_ptr, crypto_auth_alg_type auth_alg)
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800228{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800229 void *ctx_ptr = NULL;
230 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800231
Ajay Dudanib01e5062011-12-03 23:23:42 -0800232 /* Initialize SHA context based on algorithm */
233 if (auth_alg == CRYPTO_AUTH_ALG_SHA1) {
234 crypto_sha1_init(&g_sha1_ctx);
235 ctx_ptr = (void *)&g_sha1_ctx;
236 } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) {
237 crypto_sha256_init(&g_sha256_ctx);
238 ctx_ptr = (void *)&g_sha256_ctx;
239 }
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800240
Ajay Dudanib01e5062011-12-03 23:23:42 -0800241 ret_val =
242 do_sha_update(ctx_ptr, buff_ptr, buff_size, auth_alg, TRUE, TRUE);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800243
Ajay Dudanib01e5062011-12-03 23:23:42 -0800244 if (ret_val != CRYPTO_SHA_ERR_NONE) {
245 dprintf(CRITICAL, "do_sha_update returns error %d\n", ret_val);
246 return ret_val;
247 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800248
Ajay Dudanib01e5062011-12-03 23:23:42 -0800249 /* Copy the digest value from context pointer to digest pointer */
250 if (auth_alg == CRYPTO_AUTH_ALG_SHA1) {
251 memcpy(digest_ptr,
252 (unsigned char *)(((crypto_SHA1_ctx *) ctx_ptr)->
253 auth_iv), 20);
254 } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) {
255 memcpy(digest_ptr,
256 (unsigned char *)(((crypto_SHA256_ctx *) ctx_ptr)->
257 auth_iv), 32);
258 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800259
Ajay Dudanib01e5062011-12-03 23:23:42 -0800260 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800261}
262
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800263/*
264 * Common function to calculate SHA1 and SHA256 digest based on auth algorithm.
265 * Calls crypto engine APIs to setup SHAx registers, send the data and gets
266 * the digest.
267 */
268
Ajay Dudanib01e5062011-12-03 23:23:42 -0800269static crypto_result_type
270do_sha_update(void *ctx_ptr,
271 unsigned char *buff_ptr,
272 unsigned int buff_size,
273 crypto_auth_alg_type auth_alg, bool first, bool last)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800274{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800275 unsigned int ret_val = CRYPTO_ERR_NONE;
276 unsigned int bytes_to_write = 0;
277 unsigned int bytes_remaining = 0;
278 unsigned int tmp_bytes = 0;
279 unsigned int bytes_written = 0;
280 unsigned int tmp_buff_size = 0;
281 unsigned char *tmp_buff_ptr = NULL;
282 unsigned char tmp_saved_buff_indx = 0;
283 bool tmp_first;
284 bool tmp_last;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800285
Ajay Dudanib01e5062011-12-03 23:23:42 -0800286 /* Type casting to SHA1 context as offset is similar for SHA256 context */
287 crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800288
Ajay Dudanib01e5062011-12-03 23:23:42 -0800289 bytes_to_write = calc_num_bytes_to_send(ctx_ptr, buff_size, last);
290 bytes_remaining =
291 buff_size + sha1_ctx->saved_buff_indx - bytes_to_write;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800292
Ajay Dudanib01e5062011-12-03 23:23:42 -0800293 tmp_first = first;
294 tmp_saved_buff_indx = sha1_ctx->saved_buff_indx;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800295
Ajay Dudanib01e5062011-12-03 23:23:42 -0800296 do {
297 if ((bytes_to_write - bytes_written) >
Amol Jadi7d3ad062013-01-04 15:30:19 -0800298 crypto_get_max_auth_blk_size()) {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800299 /* Write CRYPTO_MAX_AUTH_BLOCK_SIZE bytes at a time to the CE */
Amol Jadi7d3ad062013-01-04 15:30:19 -0800300 tmp_bytes = crypto_get_max_auth_blk_size();
Ajay Dudanib01e5062011-12-03 23:23:42 -0800301 tmp_last = FALSE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800302
Ajay Dudanib01e5062011-12-03 23:23:42 -0800303 if (sha1_ctx->saved_buff_indx != 0) {
304 tmp_buff_ptr = buff_ptr;
305 tmp_buff_size =
306 tmp_bytes - sha1_ctx->saved_buff_indx;
307 } else {
308 tmp_buff_ptr =
309 buff_ptr + bytes_written -
310 tmp_saved_buff_indx;
311 tmp_buff_size = tmp_bytes;
312 }
313 } else {
314 /* Since bytes_to_write are less than CRYPTO_MAX_AUTH_BLOCK_SIZE
315 write all remaining bytes now */
316 if (sha1_ctx->saved_buff_indx != 0) {
317 tmp_buff_ptr = buff_ptr;
318 tmp_buff_size =
319 bytes_to_write - bytes_written -
320 sha1_ctx->saved_buff_indx;
321 } else {
322 tmp_buff_ptr =
323 buff_ptr + bytes_written -
324 tmp_saved_buff_indx;
325 tmp_buff_size =
326 bytes_to_write - bytes_written -
327 tmp_saved_buff_indx;
328 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800329
Ajay Dudanib01e5062011-12-03 23:23:42 -0800330 tmp_bytes = (bytes_to_write - bytes_written);
331 tmp_last = last;
332 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800333
Ajay Dudanib01e5062011-12-03 23:23:42 -0800334 /* Set SHAx context in the crypto engine */
335 crypto_set_sha_ctx(ctx_ptr, tmp_bytes, auth_alg, tmp_first,
336 tmp_last);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800337
Ajay Dudanib01e5062011-12-03 23:23:42 -0800338 /* Send data to the crypto engine */
339 crypto_send_data(ctx_ptr, tmp_buff_ptr, tmp_buff_size,
340 tmp_bytes, &ret_val);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800341
Ajay Dudanib01e5062011-12-03 23:23:42 -0800342 if (ret_val != CRYPTO_ERR_NONE) {
343 dprintf(CRITICAL,
344 "do_sha_update returns error from crypto_send_data\n");
345 return CRYPTO_SHA_ERR_FAIL;
346 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800347
Ajay Dudanib01e5062011-12-03 23:23:42 -0800348 /* Get the SHAx digest from the crypto engine */
349 crypto_get_digest((unsigned char *)(sha1_ctx->auth_iv),
350 &ret_val, auth_alg, tmp_last);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800351
Ajay Dudanib01e5062011-12-03 23:23:42 -0800352 if (ret_val != CRYPTO_ERR_NONE) {
353 dprintf(CRITICAL,
354 "do_sha_update returns error from crypto_get_digest\n");
355 return CRYPTO_SHA_ERR_FAIL;
356 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800357
Ajay Dudanib01e5062011-12-03 23:23:42 -0800358 if (!tmp_last) {
359 crypto_get_ctx(ctx_ptr);
360 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800361
Ajay Dudanib01e5062011-12-03 23:23:42 -0800362 bytes_written += tmp_bytes;
363 sha1_ctx->saved_buff_indx = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800364
Ajay Dudanib01e5062011-12-03 23:23:42 -0800365 if (bytes_written != bytes_to_write) {
366 tmp_first = FALSE;
367 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800368
Ajay Dudanib01e5062011-12-03 23:23:42 -0800369 }
370 while ((bytes_to_write - bytes_written) != 0);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800371
Ajay Dudanib01e5062011-12-03 23:23:42 -0800372 /* If there are bytes remaining, copy it to saved_buff */
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800373
Ajay Dudanib01e5062011-12-03 23:23:42 -0800374 if (bytes_remaining) {
375 memcpy(sha1_ctx->saved_buff,
376 (buff_ptr + buff_size - bytes_remaining),
377 bytes_remaining);
378 sha1_ctx->saved_buff_indx = bytes_remaining;
379 } else {
380 sha1_ctx->saved_buff_indx = 0;
381 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800382
Ajay Dudanib01e5062011-12-03 23:23:42 -0800383 return CRYPTO_SHA_ERR_NONE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800384}
385
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800386/*
387 * Function to calculate the number of bytes to be sent to crypto engine.
388 */
389
Ajay Dudanib01e5062011-12-03 23:23:42 -0800390static unsigned int
391calc_num_bytes_to_send(void *ctx_ptr, unsigned int buff_size, bool last)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800392{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800393 unsigned int bytes_to_write = 0;
394 crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800395
Ajay Dudanib01e5062011-12-03 23:23:42 -0800396 if (last) {
397 bytes_to_write = buff_size + sha1_ctx->saved_buff_indx;
398 } else {
399 bytes_to_write = ((buff_size + sha1_ctx->saved_buff_indx) /
400 CRYPTO_SHA_BLOCK_SIZE) *
401 CRYPTO_SHA_BLOCK_SIZE;
402 }
403 return bytes_to_write;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800404}