Amol Jadi | 7d3ad06 | 2013-01-04 15:30:19 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved. |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 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. |
Amol Jadi | 7d3ad06 | 2013-01-04 15:30:19 -0800 | [diff] [blame] | 12 | * * Neither the name of The Linux Foundation nor the names of its |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 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; |
Channagoud Kadabi | 317df2e | 2013-12-10 12:22:04 -0800 | [diff] [blame] | 36 | static bool crypto_init_done; |
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; |
Deepa Dinamani | 5e5c21a | 2012-02-16 18:59:57 -0800 | [diff] [blame] | 51 | crypto_engine_type platform_ce_type = board_ce_type(); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 52 | |
Sundarajan Srinivasan | 5bba3de | 2013-11-11 18:48:27 -0800 | [diff] [blame] | 53 | if (auth_alg == CRYPTO_AUTH_ALG_SHA1) { |
Deepa Dinamani | 5e5c21a | 2012-02-16 18:59:57 -0800 | [diff] [blame] | 54 | 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; |
Sundarajan Srinivasan | 5bba3de | 2013-11-11 18:48:27 -0800 | [diff] [blame] | 61 | } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) { |
Deepa Dinamani | 5e5c21a | 2012-02-16 18:59:57 -0800 | [diff] [blame] | 62 | 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 Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 69 | } |
Sundarajan Srinivasan | 5bba3de | 2013-11-11 18:48:27 -0800 | [diff] [blame] | 70 | else |
| 71 | ret_val = CRYPTO_SHA_ERR_FAIL; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 72 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 73 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 74 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val); |
| 75 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 78 | /* |
| 79 | * Function to reset and init crypto engine. It resets the engine for the |
| 80 | * first time. Used for multiple SHA operations. |
| 81 | */ |
| 82 | |
| 83 | static void crypto_init(void) |
| 84 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 85 | if (crypto_init_done != TRUE) { |
| 86 | ce_clock_init(); |
| 87 | crypto_eng_reset(); |
| 88 | crypto_init_done = TRUE; |
| 89 | } |
| 90 | crypto_eng_init(); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* |
Channagoud Kadabi | 1d3e9b4 | 2013-12-04 12:35:46 -0800 | [diff] [blame] | 94 | * Function to return if crypto is initialized |
| 95 | */ |
| 96 | |
| 97 | bool crypto_initialized() |
| 98 | { |
| 99 | return crypto_init_done; |
| 100 | } |
| 101 | |
| 102 | /* |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 103 | * Function to initialize SHA256 context |
| 104 | */ |
| 105 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 106 | static crypto_result_type crypto_sha256_init(crypto_SHA256_ctx * ctx_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 107 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 108 | unsigned int i; |
| 109 | /* Standard initialization vector for SHA256 */ |
| 110 | unsigned int sha256_init_vector[] = { 0x6A09E667, 0xBB67AE85, |
| 111 | 0x3C6EF372, 0xA54FF53A, |
| 112 | 0x510E527F, 0x9B05688C, |
| 113 | 0x1F83D9AB, 0x5BE0CD19 |
| 114 | }; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 115 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 116 | if (ctx_ptr == NULL) { |
| 117 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 118 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 119 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 120 | ctx_ptr->auth_bytecnt[0] = 0; |
| 121 | ctx_ptr->auth_bytecnt[1] = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 122 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 123 | memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 124 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 125 | for (i = 0; i < SHA256_INIT_VECTOR_SIZE; i++) { |
| 126 | ctx_ptr->auth_iv[i] = sha256_init_vector[i]; |
| 127 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 128 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 129 | ctx_ptr->saved_buff_indx = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 130 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 131 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 134 | /* |
| 135 | * Function to initialize SHA1 context |
| 136 | */ |
| 137 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 138 | static crypto_result_type crypto_sha1_init(crypto_SHA1_ctx * ctx_ptr) |
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 | unsigned int i; |
| 141 | /* Standard initialization vector for SHA1 */ |
| 142 | unsigned int sha1_init_vector[] = { 0x67452301, 0xEFCDAB89, |
| 143 | 0x98BADCFE, 0x10325476, |
| 144 | 0xC3D2E1F0 |
| 145 | }; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 146 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 147 | if (ctx_ptr == NULL) { |
| 148 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 149 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 150 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 151 | ctx_ptr->auth_bytecnt[0] = 0; |
| 152 | ctx_ptr->auth_bytecnt[1] = 0; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 153 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 154 | memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 155 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 156 | for (i = 0; i < SHA1_INIT_VECTOR_SIZE; i++) { |
| 157 | ctx_ptr->auth_iv[i] = sha1_init_vector[i]; |
| 158 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 159 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 160 | ctx_ptr->saved_buff_indx = 0; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 161 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 162 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Function to calculate SHA256 digest of given data buffer. |
| 167 | * It works on contiguous data and gives digest in single pass. |
| 168 | */ |
| 169 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 170 | static crypto_result_type |
| 171 | crypto_sha256(unsigned char *buff_ptr, |
| 172 | unsigned int buff_size, unsigned char *digest_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 173 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 174 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 175 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 176 | if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) { |
| 177 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 178 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 179 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 180 | /* Initialize crypto engine hardware for a new SHA256 operation */ |
| 181 | crypto_init(); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 182 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 183 | /* Now do SHA256 hashing */ |
| 184 | ret_val = |
| 185 | do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA256); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 186 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 187 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 188 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val); |
| 189 | } |
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 | return ret_val; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 194 | /* |
| 195 | * Function to calculate SHA1 digest of given data buffer. |
| 196 | * It works on contiguous data and gives digest in single pass. |
| 197 | */ |
| 198 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 199 | static crypto_result_type |
| 200 | crypto_sha1(unsigned char *buff_ptr, |
| 201 | unsigned int buff_size, unsigned char *digest_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 202 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 203 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 204 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 205 | if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) { |
| 206 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 207 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 208 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 209 | /* Initialize crypto engine hardware for a new SHA1 operation */ |
| 210 | crypto_init(); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 211 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 212 | /* Now do SHA1 hashing */ |
| 213 | 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] | 214 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 215 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 216 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val); |
| 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 | return ret_val; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Common function to calculate SHA1 and SHA256 digest based on auth algorithm. |
| 224 | */ |
| 225 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 226 | static crypto_result_type |
| 227 | do_sha(unsigned char *buff_ptr, |
| 228 | unsigned int buff_size, |
| 229 | unsigned char *digest_ptr, crypto_auth_alg_type auth_alg) |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 230 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 231 | void *ctx_ptr = NULL; |
| 232 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 233 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 234 | /* Initialize SHA context based on algorithm */ |
| 235 | if (auth_alg == CRYPTO_AUTH_ALG_SHA1) { |
| 236 | crypto_sha1_init(&g_sha1_ctx); |
| 237 | ctx_ptr = (void *)&g_sha1_ctx; |
| 238 | } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) { |
| 239 | crypto_sha256_init(&g_sha256_ctx); |
| 240 | ctx_ptr = (void *)&g_sha256_ctx; |
| 241 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 242 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 243 | ret_val = |
| 244 | 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] | 245 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 246 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 247 | dprintf(CRITICAL, "do_sha_update returns error %d\n", ret_val); |
| 248 | return ret_val; |
| 249 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 250 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 251 | /* Copy the digest value from context pointer to digest pointer */ |
| 252 | if (auth_alg == CRYPTO_AUTH_ALG_SHA1) { |
| 253 | memcpy(digest_ptr, |
| 254 | (unsigned char *)(((crypto_SHA1_ctx *) ctx_ptr)-> |
| 255 | auth_iv), 20); |
| 256 | } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) { |
| 257 | memcpy(digest_ptr, |
| 258 | (unsigned char *)(((crypto_SHA256_ctx *) ctx_ptr)-> |
| 259 | auth_iv), 32); |
| 260 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 261 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 262 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 265 | /* |
| 266 | * Common function to calculate SHA1 and SHA256 digest based on auth algorithm. |
| 267 | * Calls crypto engine APIs to setup SHAx registers, send the data and gets |
| 268 | * the digest. |
| 269 | */ |
| 270 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 271 | static crypto_result_type |
| 272 | do_sha_update(void *ctx_ptr, |
| 273 | unsigned char *buff_ptr, |
| 274 | unsigned int buff_size, |
| 275 | crypto_auth_alg_type auth_alg, bool first, bool last) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 276 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 277 | unsigned int ret_val = CRYPTO_ERR_NONE; |
| 278 | unsigned int bytes_to_write = 0; |
| 279 | unsigned int bytes_remaining = 0; |
| 280 | unsigned int tmp_bytes = 0; |
| 281 | unsigned int bytes_written = 0; |
| 282 | unsigned int tmp_buff_size = 0; |
| 283 | unsigned char *tmp_buff_ptr = NULL; |
| 284 | unsigned char tmp_saved_buff_indx = 0; |
| 285 | bool tmp_first; |
| 286 | bool tmp_last; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 287 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 288 | /* Type casting to SHA1 context as offset is similar for SHA256 context */ |
| 289 | crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 290 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 291 | bytes_to_write = calc_num_bytes_to_send(ctx_ptr, buff_size, last); |
| 292 | bytes_remaining = |
| 293 | buff_size + sha1_ctx->saved_buff_indx - bytes_to_write; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 294 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 295 | tmp_first = first; |
| 296 | tmp_saved_buff_indx = sha1_ctx->saved_buff_indx; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 297 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 298 | do { |
| 299 | if ((bytes_to_write - bytes_written) > |
Amol Jadi | 7d3ad06 | 2013-01-04 15:30:19 -0800 | [diff] [blame] | 300 | crypto_get_max_auth_blk_size()) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 301 | /* Write CRYPTO_MAX_AUTH_BLOCK_SIZE bytes at a time to the CE */ |
Amol Jadi | 7d3ad06 | 2013-01-04 15:30:19 -0800 | [diff] [blame] | 302 | tmp_bytes = crypto_get_max_auth_blk_size(); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 303 | tmp_last = FALSE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 304 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 305 | if (sha1_ctx->saved_buff_indx != 0) { |
| 306 | tmp_buff_ptr = buff_ptr; |
| 307 | tmp_buff_size = |
| 308 | tmp_bytes - 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 = tmp_bytes; |
| 314 | } |
| 315 | } else { |
| 316 | /* Since bytes_to_write are less than CRYPTO_MAX_AUTH_BLOCK_SIZE |
| 317 | write all remaining bytes now */ |
| 318 | if (sha1_ctx->saved_buff_indx != 0) { |
| 319 | tmp_buff_ptr = buff_ptr; |
| 320 | tmp_buff_size = |
| 321 | bytes_to_write - bytes_written - |
| 322 | sha1_ctx->saved_buff_indx; |
| 323 | } else { |
| 324 | tmp_buff_ptr = |
| 325 | buff_ptr + bytes_written - |
| 326 | tmp_saved_buff_indx; |
| 327 | tmp_buff_size = |
| 328 | bytes_to_write - bytes_written - |
| 329 | tmp_saved_buff_indx; |
| 330 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 331 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 332 | tmp_bytes = (bytes_to_write - bytes_written); |
| 333 | tmp_last = last; |
| 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 | /* Set SHAx context in the crypto engine */ |
| 337 | crypto_set_sha_ctx(ctx_ptr, tmp_bytes, auth_alg, tmp_first, |
| 338 | 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 | /* Send data to the crypto engine */ |
| 341 | crypto_send_data(ctx_ptr, tmp_buff_ptr, tmp_buff_size, |
| 342 | tmp_bytes, &ret_val); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 343 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 344 | if (ret_val != CRYPTO_ERR_NONE) { |
| 345 | dprintf(CRITICAL, |
| 346 | "do_sha_update returns error from crypto_send_data\n"); |
| 347 | return CRYPTO_SHA_ERR_FAIL; |
| 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 | /* Get the SHAx digest from the crypto engine */ |
| 351 | crypto_get_digest((unsigned char *)(sha1_ctx->auth_iv), |
| 352 | &ret_val, auth_alg, tmp_last); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 353 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 354 | if (ret_val != CRYPTO_ERR_NONE) { |
| 355 | dprintf(CRITICAL, |
| 356 | "do_sha_update returns error from crypto_get_digest\n"); |
| 357 | return CRYPTO_SHA_ERR_FAIL; |
| 358 | } |
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 (!tmp_last) { |
| 361 | crypto_get_ctx(ctx_ptr); |
| 362 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 363 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 364 | bytes_written += tmp_bytes; |
| 365 | sha1_ctx->saved_buff_indx = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 366 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 367 | if (bytes_written != bytes_to_write) { |
| 368 | tmp_first = FALSE; |
| 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 | } |
| 372 | while ((bytes_to_write - bytes_written) != 0); |
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 there are bytes remaining, copy it to saved_buff */ |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 375 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 376 | if (bytes_remaining) { |
| 377 | memcpy(sha1_ctx->saved_buff, |
| 378 | (buff_ptr + buff_size - bytes_remaining), |
| 379 | bytes_remaining); |
| 380 | sha1_ctx->saved_buff_indx = bytes_remaining; |
| 381 | } else { |
| 382 | sha1_ctx->saved_buff_indx = 0; |
| 383 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 384 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 385 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 388 | /* |
| 389 | * Function to calculate the number of bytes to be sent to crypto engine. |
| 390 | */ |
| 391 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 392 | static unsigned int |
| 393 | 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] | 394 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 395 | unsigned int bytes_to_write = 0; |
| 396 | crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 397 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 398 | if (last) { |
| 399 | bytes_to_write = buff_size + sha1_ctx->saved_buff_indx; |
| 400 | } else { |
| 401 | bytes_to_write = ((buff_size + sha1_ctx->saved_buff_indx) / |
| 402 | CRYPTO_SHA_BLOCK_SIZE) * |
| 403 | CRYPTO_SHA_BLOCK_SIZE; |
| 404 | } |
| 405 | return bytes_to_write; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 406 | } |