blob: 21c15ecc3c98abd91328853f3f22e67ef076f58e [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"
33#include "crypto_eng.h"
34
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080035static crypto_SHA256_ctx g_sha256_ctx;
36static crypto_SHA1_ctx g_sha1_ctx;
37static unsigned char crypto_init_done = FALSE;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080038
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080039/*
40 * Top level function which calculates SHAx digest with given data and size.
41 * Digest varies based on the authentication algorithm.
42 * It works on contiguous data and does single pass calculation.
43 */
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080044
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080045void hash_find(unsigned char *addr, unsigned int size, unsigned char *digest,
46 unsigned char auth_alg)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080047{
48 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
49
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080050 if(auth_alg == 1)
51 {
52 ret_val = crypto_sha1(addr,size,digest);
53 }
54 else if(auth_alg == 2)
55 {
56 ret_val = crypto_sha256(addr,size,digest);
57 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080058
59 if(ret_val != CRYPTO_SHA_ERR_NONE)
60 {
61 dprintf(CRITICAL, "crypto_sha256 returns error %d\n",ret_val);
62 }
63}
64
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080065/*
66 * Function to reset and init crypto engine. It resets the engine for the
67 * first time. Used for multiple SHA operations.
68 */
69
70static void crypto_init(void)
71{
72 if(crypto_init_done != TRUE)
73 {
74 crypto_eng_reset();
75 crypto_init_done = TRUE;
76 }
77 crypto_eng_init();
78}
79
80/*
81 * Function to initialize SHA256 context
82 */
83
84static crypto_result_type crypto_sha256_init(crypto_SHA256_ctx *ctx_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080085{
86 unsigned int i;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -080087 /* Standard initialization vector for SHA256 */
88 unsigned int sha256_init_vector[] = { 0x6A09E667, 0xBB67AE85,
89 0x3C6EF372, 0xA54FF53A,
90 0x510E527F, 0x9B05688C,
91 0x1F83D9AB, 0x5BE0CD19 };
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -080092
93 if(ctx_ptr == NULL)
94 {
95 return CRYPTO_SHA_ERR_INVALID_PARAM;
96 }
97
98 ctx_ptr->auth_bytecnt[0] = 0;
99 ctx_ptr->auth_bytecnt[1] = 0;
100
101 memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE);
102
103 for(i=0;i<SHA256_INIT_VECTOR_SIZE;i++)
104 {
105 ctx_ptr->auth_iv[i] = sha256_init_vector[i];
106 }
107
108 ctx_ptr->saved_buff_indx = 0;
109
110 return CRYPTO_SHA_ERR_NONE;
111}
112
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800113/*
114 * Function to initialize SHA1 context
115 */
116
117static crypto_result_type crypto_sha1_init(crypto_SHA1_ctx *ctx_ptr)
118{
119 unsigned int i;
120 /* Standard initialization vector for SHA1 */
121 unsigned int sha1_init_vector[] = { 0x67452301, 0xEFCDAB89,
122 0x98BADCFE, 0x10325476,
123 0xC3D2E1F0 };
124
125 if(ctx_ptr == NULL)
126 {
127 return CRYPTO_SHA_ERR_INVALID_PARAM;
128 }
129
130 ctx_ptr->auth_bytecnt[0] = 0;
131 ctx_ptr->auth_bytecnt[1] = 0;
132
133 memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE);
134
135 for(i=0;i<SHA1_INIT_VECTOR_SIZE;i++)
136 {
137 ctx_ptr->auth_iv[i] = sha1_init_vector[i];
138 }
139
140 ctx_ptr->saved_buff_indx = 0;
141
142 return CRYPTO_SHA_ERR_NONE;
143}
144
145/*
146 * Function to calculate SHA256 digest of given data buffer.
147 * It works on contiguous data and gives digest in single pass.
148 */
149
150static crypto_result_type crypto_sha256(unsigned char *buff_ptr,
151 unsigned int buff_size,
152 unsigned char *digest_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800153{
154 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
155
156 if((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL))
157 {
158 return CRYPTO_SHA_ERR_INVALID_PARAM;
159 }
160
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800161 /* Initialize crypto engine hardware for a new SHA256 operation */
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800162 crypto_init();
163
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800164 /* Now do SHA256 hashing */
165 ret_val = do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA256);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800166
167 if(ret_val != CRYPTO_SHA_ERR_NONE)
168 {
169 dprintf(CRITICAL, "crypto_sha256 returns error %d\n",ret_val);
170 }
171
172 return ret_val;
173}
174
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800175/*
176 * Function to calculate SHA1 digest of given data buffer.
177 * It works on contiguous data and gives digest in single pass.
178 */
179
180static crypto_result_type crypto_sha1(unsigned char *buff_ptr,
181 unsigned int buff_size,
182 unsigned char *digest_ptr)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800183{
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800184 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
185
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800186 if((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL))
187 {
188 return CRYPTO_SHA_ERR_INVALID_PARAM;
189 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800190
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800191 /* Initialize crypto engine hardware for a new SHA1 operation */
192 crypto_init();
193
194 /* Now do SHA1 hashing */
195 ret_val = do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA1);
196
197 if(ret_val != CRYPTO_SHA_ERR_NONE)
198 {
199 dprintf(CRITICAL, "crypto_sha256 returns error %d\n",ret_val);
200 }
201
202 return ret_val;
203}
204
205/*
206 * Common function to calculate SHA1 and SHA256 digest based on auth algorithm.
207 */
208
209static crypto_result_type do_sha(unsigned char *buff_ptr,
210 unsigned int buff_size,
211 unsigned char *digest_ptr,
212 crypto_auth_alg_type auth_alg)
213{
214 void *ctx_ptr=NULL;
215 crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE;
216
217 /* Initialize SHA context based on algorithm */
218 if(auth_alg == CRYPTO_AUTH_ALG_SHA1)
219 {
220 crypto_sha1_init(&g_sha1_ctx);
221 ctx_ptr = (void*)&g_sha1_ctx;
222 }
223 else if(auth_alg == CRYPTO_AUTH_ALG_SHA256)
224 {
225 crypto_sha256_init(&g_sha256_ctx);
226 ctx_ptr = (void*)&g_sha256_ctx;
227 }
228
229 ret_val = do_sha_update(ctx_ptr, buff_ptr, buff_size, auth_alg, TRUE, TRUE);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800230
231 if(ret_val != CRYPTO_SHA_ERR_NONE)
232 {
233 dprintf(CRITICAL, "do_sha_update returns error %d\n",ret_val);
234 return ret_val;
235 }
236
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800237 /* Copy the digest value from context pointer to digest pointer */
238 if(auth_alg == CRYPTO_AUTH_ALG_SHA1)
239 {
240 memcpy(digest_ptr, (unsigned char*)(((crypto_SHA1_ctx*)ctx_ptr)->auth_iv), 20);
241 }
242 else if(auth_alg == CRYPTO_AUTH_ALG_SHA256)
243 {
244 memcpy(digest_ptr, (unsigned char*)(((crypto_SHA256_ctx*)ctx_ptr)->auth_iv), 32);
245 }
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800246
247 return CRYPTO_SHA_ERR_NONE;
248}
249
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800250/*
251 * Common function to calculate SHA1 and SHA256 digest based on auth algorithm.
252 * Calls crypto engine APIs to setup SHAx registers, send the data and gets
253 * the digest.
254 */
255
256static crypto_result_type do_sha_update(void *ctx_ptr,
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800257 unsigned char *buff_ptr,
258 unsigned int buff_size,
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800259 crypto_auth_alg_type auth_alg,
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800260 bool first, bool last)
261{
262 unsigned int ret_val = CRYPTO_ERR_NONE;
263 unsigned int bytes_to_write = 0;
264 unsigned int bytes_remaining = 0;
265 unsigned int tmp_bytes = 0;
266 unsigned int bytes_written = 0;
267 unsigned int tmp_buff_size = 0;
268 unsigned char *tmp_buff_ptr = NULL;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800269 unsigned char tmp_saved_buff_indx = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800270 bool tmp_first;
271 bool tmp_last;
272
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800273 /* Type casting to SHA1 context as offset is similar for SHA256 context */
274 crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx*)ctx_ptr;
275
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800276 bytes_to_write = calc_num_bytes_to_send(ctx_ptr, buff_size, last);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800277 bytes_remaining = buff_size + sha1_ctx->saved_buff_indx - bytes_to_write;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800278
279 tmp_first = first;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800280 tmp_saved_buff_indx = sha1_ctx->saved_buff_indx;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800281
282 do
283 {
284 if((bytes_to_write - bytes_written) > CRYPTO_MAX_AUTH_BLOCK_SIZE)
285 {
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800286 /* Write CRYPTO_MAX_AUTH_BLOCK_SIZE bytes at a time to the CE */
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800287 tmp_bytes = CRYPTO_MAX_AUTH_BLOCK_SIZE;
288 tmp_last = FALSE;
289
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800290 if(sha1_ctx->saved_buff_indx != 0)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800291 {
292 tmp_buff_ptr = buff_ptr;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800293 tmp_buff_size = tmp_bytes - sha1_ctx->saved_buff_indx;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800294 }
295 else
296 {
297 tmp_buff_ptr = buff_ptr + bytes_written - tmp_saved_buff_indx;
298 tmp_buff_size = tmp_bytes;
299 }
300 }
301 else
302 {
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800303 /* Since bytes_to_write are less than CRYPTO_MAX_AUTH_BLOCK_SIZE
304 write all remaining bytes now */
305 if(sha1_ctx->saved_buff_indx != 0)
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800306 {
307 tmp_buff_ptr = buff_ptr;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800308 tmp_buff_size = bytes_to_write - bytes_written - sha1_ctx->saved_buff_indx;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800309 }
310 else
311 {
312 tmp_buff_ptr = buff_ptr + bytes_written - tmp_saved_buff_indx;
313 tmp_buff_size = bytes_to_write - bytes_written - tmp_saved_buff_indx;
314 }
315
316 tmp_bytes = (bytes_to_write - bytes_written);
317 tmp_last = last;
318 }
319
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800320 /* Set SHAx context in the crypto engine */
321 crypto_set_sha_ctx(ctx_ptr, tmp_bytes, auth_alg, tmp_first, tmp_last);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800322
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800323 /* Send data to the crypto engine */
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800324 crypto_send_data(ctx_ptr, tmp_buff_ptr, tmp_buff_size, tmp_bytes, &ret_val);
325
326 if(ret_val != CRYPTO_ERR_NONE)
327 {
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800328 dprintf(CRITICAL, "do_sha_update returns error from crypto_send_data\n");
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800329 return CRYPTO_SHA_ERR_FAIL;
330 }
331
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800332 /* Get the SHAx digest from the crypto engine */
333 crypto_get_digest((unsigned char *)(sha1_ctx->auth_iv), &ret_val, auth_alg, tmp_last);
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800334
335 if(ret_val != CRYPTO_ERR_NONE)
336 {
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800337 dprintf(CRITICAL, "do_sha_update returns error from crypto_get_digest\n");
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800338 return CRYPTO_SHA_ERR_FAIL;
339 }
340
341 if(!tmp_last)
342 {
343 crypto_get_ctx(ctx_ptr);
344 }
345
346 bytes_written += tmp_bytes;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800347 sha1_ctx->saved_buff_indx = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800348
349 if(bytes_written != bytes_to_write)
350 {
351 tmp_first = FALSE;
352 }
353
354 }while ((bytes_to_write - bytes_written) != 0);
355
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800356 /* If there are bytes remaining, copy it to saved_buff */
357
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800358 if(bytes_remaining)
359 {
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800360 memcpy(sha1_ctx->saved_buff, (buff_ptr + buff_size - bytes_remaining),
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800361 bytes_remaining);
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800362 sha1_ctx->saved_buff_indx = bytes_remaining;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800363 }
364 else
365 {
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800366 sha1_ctx->saved_buff_indx = 0;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800367 }
368
369 return CRYPTO_SHA_ERR_NONE;
370}
371
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800372/*
373 * Function to calculate the number of bytes to be sent to crypto engine.
374 */
375
376static unsigned int calc_num_bytes_to_send(void *ctx_ptr,
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800377 unsigned int buff_size, bool last)
378{
379 unsigned int bytes_to_write=0;
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800380 crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx*)ctx_ptr;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800381
382 if(last)
383 {
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800384 bytes_to_write = buff_size + sha1_ctx->saved_buff_indx;
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800385 }
386 else
387 {
Subbaraman Narayanamurthy8fcccbd2011-01-28 13:26:00 -0800388 bytes_to_write = ((buff_size + sha1_ctx->saved_buff_indx)/
Subbaraman Narayanamurthy9b7276c2011-01-25 17:25:30 -0800389 CRYPTO_SHA_BLOCK_SIZE) * CRYPTO_SHA_BLOCK_SIZE;
390 }
391 return bytes_to_write;
392}