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) { |
Channagoud Kadabi | 2b20456 | 2012-02-02 12:26:46 +0530 | [diff] [blame^] | 53 | #ifdef NO_CRYPTO_ENG |
| 54 | /* Hardware CE is not present , use software hashing */ |
| 55 | digest = SHA1(addr, size, digest); |
| 56 | #else |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 57 | ret_val = crypto_sha1(addr, size, digest); |
Channagoud Kadabi | 2b20456 | 2012-02-02 12:26:46 +0530 | [diff] [blame^] | 58 | #endif |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 59 | } else if (auth_alg == 2) { |
Channagoud Kadabi | 2b20456 | 2012-02-02 12:26:46 +0530 | [diff] [blame^] | 60 | #ifdef NO_CRYPTO_ENG |
| 61 | /* Hardware CE is not present , use software hashing */ |
| 62 | digest = SHA256(addr, size, digest); |
| 63 | #else |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 64 | ret_val = crypto_sha256(addr, size, digest); |
Channagoud Kadabi | 2b20456 | 2012-02-02 12:26:46 +0530 | [diff] [blame^] | 65 | #endif |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 66 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 67 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 68 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 69 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val); |
| 70 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 73 | /* |
| 74 | * Function to reset and init crypto engine. It resets the engine for the |
| 75 | * first time. Used for multiple SHA operations. |
| 76 | */ |
| 77 | |
| 78 | static void crypto_init(void) |
| 79 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 80 | if (crypto_init_done != TRUE) { |
| 81 | ce_clock_init(); |
| 82 | crypto_eng_reset(); |
| 83 | crypto_init_done = TRUE; |
| 84 | } |
| 85 | crypto_eng_init(); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Function to initialize SHA256 context |
| 90 | */ |
| 91 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 92 | static crypto_result_type crypto_sha256_init(crypto_SHA256_ctx * ctx_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 93 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 94 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 101 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 102 | if (ctx_ptr == NULL) { |
| 103 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 104 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 105 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 106 | ctx_ptr->auth_bytecnt[0] = 0; |
| 107 | ctx_ptr->auth_bytecnt[1] = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 108 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 109 | memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 110 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 111 | for (i = 0; i < SHA256_INIT_VECTOR_SIZE; i++) { |
| 112 | ctx_ptr->auth_iv[i] = sha256_init_vector[i]; |
| 113 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 114 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 115 | ctx_ptr->saved_buff_indx = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 116 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 117 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 120 | /* |
| 121 | * Function to initialize SHA1 context |
| 122 | */ |
| 123 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 124 | static crypto_result_type crypto_sha1_init(crypto_SHA1_ctx * ctx_ptr) |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 125 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 126 | unsigned int i; |
| 127 | /* Standard initialization vector for SHA1 */ |
| 128 | unsigned int sha1_init_vector[] = { 0x67452301, 0xEFCDAB89, |
| 129 | 0x98BADCFE, 0x10325476, |
| 130 | 0xC3D2E1F0 |
| 131 | }; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 132 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 133 | if (ctx_ptr == NULL) { |
| 134 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 135 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 136 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 137 | ctx_ptr->auth_bytecnt[0] = 0; |
| 138 | ctx_ptr->auth_bytecnt[1] = 0; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 139 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 140 | memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 141 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 142 | for (i = 0; i < SHA1_INIT_VECTOR_SIZE; i++) { |
| 143 | ctx_ptr->auth_iv[i] = sha1_init_vector[i]; |
| 144 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 145 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 146 | ctx_ptr->saved_buff_indx = 0; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 147 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 148 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 149 | } |
| 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 Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 156 | static crypto_result_type |
| 157 | crypto_sha256(unsigned char *buff_ptr, |
| 158 | unsigned int buff_size, unsigned char *digest_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 159 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 160 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 161 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 162 | if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) { |
| 163 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 164 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 165 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 166 | /* Initialize crypto engine hardware for a new SHA256 operation */ |
| 167 | crypto_init(); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 168 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 169 | /* Now do SHA256 hashing */ |
| 170 | ret_val = |
| 171 | do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA256); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 172 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 173 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 174 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val); |
| 175 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 176 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 177 | return ret_val; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 180 | /* |
| 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 Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 185 | static crypto_result_type |
| 186 | crypto_sha1(unsigned char *buff_ptr, |
| 187 | unsigned int buff_size, unsigned char *digest_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 188 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 189 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 190 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 191 | if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) { |
| 192 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 193 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 194 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 195 | /* Initialize crypto engine hardware for a new SHA1 operation */ |
| 196 | crypto_init(); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 197 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 198 | /* Now do SHA1 hashing */ |
| 199 | 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] | 200 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 201 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 202 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val); |
| 203 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 204 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 205 | return ret_val; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Common function to calculate SHA1 and SHA256 digest based on auth algorithm. |
| 210 | */ |
| 211 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 212 | static crypto_result_type |
| 213 | do_sha(unsigned char *buff_ptr, |
| 214 | unsigned int buff_size, |
| 215 | unsigned char *digest_ptr, crypto_auth_alg_type auth_alg) |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 216 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 217 | void *ctx_ptr = NULL; |
| 218 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 219 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 220 | /* 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 Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 228 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 229 | ret_val = |
| 230 | 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] | 231 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 232 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 236 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 237 | /* 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 247 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 248 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 251 | /* |
| 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 Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 257 | static crypto_result_type |
| 258 | do_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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 262 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 263 | 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 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 | /* Type casting to SHA1 context as offset is similar for SHA256 context */ |
| 275 | crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 276 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 277 | 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 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 | tmp_first = first; |
| 282 | tmp_saved_buff_indx = sha1_ctx->saved_buff_indx; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 283 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 284 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 290 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 291 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 317 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 318 | tmp_bytes = (bytes_to_write - bytes_written); |
| 319 | tmp_last = last; |
| 320 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 321 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 322 | /* Set SHAx context in the crypto engine */ |
| 323 | crypto_set_sha_ctx(ctx_ptr, tmp_bytes, auth_alg, tmp_first, |
| 324 | tmp_last); |
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 | /* Send data to the crypto engine */ |
| 327 | crypto_send_data(ctx_ptr, tmp_buff_ptr, tmp_buff_size, |
| 328 | tmp_bytes, &ret_val); |
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_send_data\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 | /* 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 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 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 345 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 346 | if (!tmp_last) { |
| 347 | crypto_get_ctx(ctx_ptr); |
| 348 | } |
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 | bytes_written += tmp_bytes; |
| 351 | sha1_ctx->saved_buff_indx = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 352 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 353 | if (bytes_written != bytes_to_write) { |
| 354 | tmp_first = FALSE; |
| 355 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 356 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 357 | } |
| 358 | while ((bytes_to_write - bytes_written) != 0); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 359 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 360 | /* If there are bytes remaining, copy it to saved_buff */ |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 361 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 362 | 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 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 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 374 | /* |
| 375 | * Function to calculate the number of bytes to be sent to crypto engine. |
| 376 | */ |
| 377 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 378 | static unsigned int |
| 379 | 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] | 380 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 381 | unsigned int bytes_to_write = 0; |
| 382 | crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 383 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 384 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 392 | } |