Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 1 | /* 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 33 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 34 | static crypto_SHA256_ctx g_sha256_ctx; |
| 35 | static crypto_SHA1_ctx g_sha1_ctx; |
| 36 | static unsigned char crypto_init_done = FALSE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 37 | |
Shashank Mittal | 1fcde7a | 2011-07-25 13:41:50 -0700 | [diff] [blame] | 38 | extern void ce_clock_init(void); |
| 39 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 40 | /* |
| 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 45 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 46 | void |
| 47 | hash_find(unsigned char *addr, unsigned int size, unsigned char *digest, |
| 48 | unsigned char auth_alg) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 49 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 50 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 51 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 52 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 57 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 58 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 59 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val); |
| 60 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 63 | /* |
| 64 | * Function to reset and init crypto engine. It resets the engine for the |
| 65 | * first time. Used for multiple SHA operations. |
| 66 | */ |
| 67 | |
| 68 | static void crypto_init(void) |
| 69 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 70 | if (crypto_init_done != TRUE) { |
| 71 | ce_clock_init(); |
| 72 | crypto_eng_reset(); |
| 73 | crypto_init_done = TRUE; |
| 74 | } |
| 75 | crypto_eng_init(); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Function to initialize SHA256 context |
| 80 | */ |
| 81 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 82 | static crypto_result_type crypto_sha256_init(crypto_SHA256_ctx * ctx_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 83 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 84 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 91 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 92 | if (ctx_ptr == NULL) { |
| 93 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 94 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 95 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 96 | ctx_ptr->auth_bytecnt[0] = 0; |
| 97 | ctx_ptr->auth_bytecnt[1] = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 98 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 99 | memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 100 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 101 | for (i = 0; i < SHA256_INIT_VECTOR_SIZE; i++) { |
| 102 | ctx_ptr->auth_iv[i] = sha256_init_vector[i]; |
| 103 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 104 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 105 | ctx_ptr->saved_buff_indx = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 106 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 107 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 110 | /* |
| 111 | * Function to initialize SHA1 context |
| 112 | */ |
| 113 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 114 | static crypto_result_type crypto_sha1_init(crypto_SHA1_ctx * ctx_ptr) |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 115 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 116 | unsigned int i; |
| 117 | /* Standard initialization vector for SHA1 */ |
| 118 | unsigned int sha1_init_vector[] = { 0x67452301, 0xEFCDAB89, |
| 119 | 0x98BADCFE, 0x10325476, |
| 120 | 0xC3D2E1F0 |
| 121 | }; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 122 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 123 | if (ctx_ptr == NULL) { |
| 124 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 125 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 126 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 127 | ctx_ptr->auth_bytecnt[0] = 0; |
| 128 | ctx_ptr->auth_bytecnt[1] = 0; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 129 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 130 | memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 131 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 132 | for (i = 0; i < SHA1_INIT_VECTOR_SIZE; i++) { |
| 133 | ctx_ptr->auth_iv[i] = sha1_init_vector[i]; |
| 134 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 135 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 136 | ctx_ptr->saved_buff_indx = 0; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 137 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 138 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 139 | } |
| 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 Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 146 | static crypto_result_type |
| 147 | crypto_sha256(unsigned char *buff_ptr, |
| 148 | unsigned int buff_size, unsigned char *digest_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 149 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 150 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 151 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 152 | if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) { |
| 153 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 154 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 155 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 156 | /* Initialize crypto engine hardware for a new SHA256 operation */ |
| 157 | crypto_init(); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 158 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 159 | /* Now do SHA256 hashing */ |
| 160 | ret_val = |
| 161 | do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA256); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 162 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 163 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 164 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val); |
| 165 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 166 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 167 | return ret_val; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 170 | /* |
| 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 Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 175 | static crypto_result_type |
| 176 | crypto_sha1(unsigned char *buff_ptr, |
| 177 | unsigned int buff_size, unsigned char *digest_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 178 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 179 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 180 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 181 | if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) { |
| 182 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 183 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 184 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 185 | /* Initialize crypto engine hardware for a new SHA1 operation */ |
| 186 | crypto_init(); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 187 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 188 | /* Now do SHA1 hashing */ |
| 189 | ret_val = do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA1); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 190 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 191 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 192 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val); |
| 193 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 194 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 195 | return ret_val; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Common function to calculate SHA1 and SHA256 digest based on auth algorithm. |
| 200 | */ |
| 201 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 202 | static crypto_result_type |
| 203 | do_sha(unsigned char *buff_ptr, |
| 204 | unsigned int buff_size, |
| 205 | unsigned char *digest_ptr, crypto_auth_alg_type auth_alg) |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 206 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 207 | void *ctx_ptr = NULL; |
| 208 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 209 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 210 | /* 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 Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 218 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 219 | ret_val = |
| 220 | do_sha_update(ctx_ptr, buff_ptr, buff_size, auth_alg, TRUE, TRUE); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 221 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 222 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 226 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 227 | /* 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 237 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 238 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 241 | /* |
| 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 Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 247 | static crypto_result_type |
| 248 | do_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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 252 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 253 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 263 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 264 | /* Type casting to SHA1 context as offset is similar for SHA256 context */ |
| 265 | crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 266 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 267 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 270 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 271 | tmp_first = first; |
| 272 | tmp_saved_buff_indx = sha1_ctx->saved_buff_indx; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 273 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 274 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 280 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 281 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 307 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 308 | tmp_bytes = (bytes_to_write - bytes_written); |
| 309 | tmp_last = last; |
| 310 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 311 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 312 | /* Set SHAx context in the crypto engine */ |
| 313 | crypto_set_sha_ctx(ctx_ptr, tmp_bytes, auth_alg, tmp_first, |
| 314 | tmp_last); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 315 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 316 | /* Send data to the crypto engine */ |
| 317 | crypto_send_data(ctx_ptr, tmp_buff_ptr, tmp_buff_size, |
| 318 | tmp_bytes, &ret_val); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 319 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 320 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 325 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 326 | /* 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 329 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 330 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 335 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 336 | if (!tmp_last) { |
| 337 | crypto_get_ctx(ctx_ptr); |
| 338 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 339 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 340 | bytes_written += tmp_bytes; |
| 341 | sha1_ctx->saved_buff_indx = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 342 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 343 | if (bytes_written != bytes_to_write) { |
| 344 | tmp_first = FALSE; |
| 345 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 346 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 347 | } |
| 348 | while ((bytes_to_write - bytes_written) != 0); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 349 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 350 | /* If there are bytes remaining, copy it to saved_buff */ |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 351 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 352 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 360 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 361 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 364 | /* |
| 365 | * Function to calculate the number of bytes to be sent to crypto engine. |
| 366 | */ |
| 367 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 368 | static unsigned int |
| 369 | calc_num_bytes_to_send(void *ctx_ptr, unsigned int buff_size, bool last) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 370 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 371 | unsigned int bytes_to_write = 0; |
| 372 | crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 373 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 374 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 382 | } |