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; |
| 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 | |
Deepa Dinamani | 193874e | 2012-02-07 14:00:04 -0800 | [diff] [blame] | 40 | __WEAK void crypto_eng_cleanup() |
| 41 | { |
| 42 | } |
| 43 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 44 | /* |
| 45 | * Top level function which calculates SHAx digest with given data and size. |
| 46 | * Digest varies based on the authentication algorithm. |
| 47 | * It works on contiguous data and does single pass calculation. |
| 48 | */ |
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 | void |
| 51 | hash_find(unsigned char *addr, unsigned int size, unsigned char *digest, |
| 52 | unsigned char auth_alg) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 53 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 54 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
Deepa Dinamani | 5e5c21a | 2012-02-16 18:59:57 -0800 | [diff] [blame] | 55 | crypto_engine_type platform_ce_type = board_ce_type(); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 56 | |
Sundarajan Srinivasan | 6a2bad2 | 2013-11-11 18:48:27 -0800 | [diff] [blame] | 57 | if (auth_alg == CRYPTO_AUTH_ALG_SHA1) { |
Deepa Dinamani | 5e5c21a | 2012-02-16 18:59:57 -0800 | [diff] [blame] | 58 | if(platform_ce_type == CRYPTO_ENGINE_TYPE_SW) |
| 59 | /* Hardware CE is not present , use software hashing */ |
| 60 | digest = SHA1(addr, size, digest); |
| 61 | else if (platform_ce_type == CRYPTO_ENGINE_TYPE_HW) |
| 62 | ret_val = crypto_sha1(addr, size, digest); |
| 63 | else |
| 64 | ret_val = CRYPTO_SHA_ERR_FAIL; |
Sundarajan Srinivasan | 6a2bad2 | 2013-11-11 18:48:27 -0800 | [diff] [blame] | 65 | } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) { |
Deepa Dinamani | 5e5c21a | 2012-02-16 18:59:57 -0800 | [diff] [blame] | 66 | if(platform_ce_type == CRYPTO_ENGINE_TYPE_SW) |
| 67 | /* Hardware CE is not present , use software hashing */ |
| 68 | digest = SHA256(addr, size, digest); |
| 69 | else if (platform_ce_type == CRYPTO_ENGINE_TYPE_HW) |
| 70 | ret_val = crypto_sha256(addr, size, digest); |
| 71 | else |
| 72 | ret_val = CRYPTO_SHA_ERR_FAIL; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 73 | } |
Sundarajan Srinivasan | 6a2bad2 | 2013-11-11 18:48:27 -0800 | [diff] [blame] | 74 | else |
| 75 | ret_val = CRYPTO_SHA_ERR_FAIL; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 76 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 77 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 78 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val); |
| 79 | } |
Deepa Dinamani | 193874e | 2012-02-07 14:00:04 -0800 | [diff] [blame] | 80 | |
| 81 | crypto_eng_cleanup(); |
| 82 | |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 85 | /* |
| 86 | * Function to reset and init crypto engine. It resets the engine for the |
| 87 | * first time. Used for multiple SHA operations. |
| 88 | */ |
| 89 | |
| 90 | static void crypto_init(void) |
| 91 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 92 | if (crypto_init_done != TRUE) { |
| 93 | ce_clock_init(); |
| 94 | crypto_eng_reset(); |
| 95 | crypto_init_done = TRUE; |
| 96 | } |
| 97 | crypto_eng_init(); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Function to initialize SHA256 context |
| 102 | */ |
| 103 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 104 | static crypto_result_type crypto_sha256_init(crypto_SHA256_ctx * ctx_ptr) |
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 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 113 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 114 | if (ctx_ptr == NULL) { |
| 115 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 116 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 117 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 118 | ctx_ptr->auth_bytecnt[0] = 0; |
| 119 | ctx_ptr->auth_bytecnt[1] = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 120 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 121 | memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE); |
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 | for (i = 0; i < SHA256_INIT_VECTOR_SIZE; i++) { |
| 124 | ctx_ptr->auth_iv[i] = sha256_init_vector[i]; |
| 125 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 126 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 127 | ctx_ptr->saved_buff_indx = 0; |
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 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 132 | /* |
| 133 | * Function to initialize SHA1 context |
| 134 | */ |
| 135 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 136 | static crypto_result_type crypto_sha1_init(crypto_SHA1_ctx * ctx_ptr) |
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 | unsigned int i; |
| 139 | /* Standard initialization vector for SHA1 */ |
| 140 | unsigned int sha1_init_vector[] = { 0x67452301, 0xEFCDAB89, |
| 141 | 0x98BADCFE, 0x10325476, |
| 142 | 0xC3D2E1F0 |
| 143 | }; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 144 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 145 | if (ctx_ptr == NULL) { |
| 146 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 147 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 148 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 149 | ctx_ptr->auth_bytecnt[0] = 0; |
| 150 | ctx_ptr->auth_bytecnt[1] = 0; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 151 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 152 | memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE); |
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 | for (i = 0; i < SHA1_INIT_VECTOR_SIZE; i++) { |
| 155 | ctx_ptr->auth_iv[i] = sha1_init_vector[i]; |
| 156 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 157 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 158 | ctx_ptr->saved_buff_indx = 0; |
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 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 161 | } |
| 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 Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 168 | static crypto_result_type |
| 169 | crypto_sha256(unsigned char *buff_ptr, |
| 170 | unsigned int buff_size, unsigned char *digest_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 171 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 172 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
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 | if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) { |
| 175 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 176 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 177 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 178 | /* Initialize crypto engine hardware for a new SHA256 operation */ |
| 179 | crypto_init(); |
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 | /* Now do SHA256 hashing */ |
| 182 | ret_val = |
| 183 | do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA256); |
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 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 186 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val); |
| 187 | } |
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 | return ret_val; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 192 | /* |
| 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 Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 197 | static crypto_result_type |
| 198 | crypto_sha1(unsigned char *buff_ptr, |
| 199 | unsigned int buff_size, unsigned char *digest_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 200 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 201 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
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 | if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) { |
| 204 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 205 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 206 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 207 | /* Initialize crypto engine hardware for a new SHA1 operation */ |
| 208 | crypto_init(); |
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 | /* Now do SHA1 hashing */ |
| 211 | 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] | 212 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 213 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 214 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val); |
| 215 | } |
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 | return ret_val; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Common function to calculate SHA1 and SHA256 digest based on auth algorithm. |
| 222 | */ |
| 223 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 224 | static crypto_result_type |
| 225 | do_sha(unsigned char *buff_ptr, |
| 226 | unsigned int buff_size, |
| 227 | unsigned char *digest_ptr, crypto_auth_alg_type auth_alg) |
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 | void *ctx_ptr = NULL; |
| 230 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 231 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 232 | /* 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 Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 240 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 241 | ret_val = |
| 242 | 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] | 243 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 244 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 248 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 249 | /* 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 259 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 260 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 263 | /* |
| 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 Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 269 | static crypto_result_type |
| 270 | do_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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 274 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 275 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 285 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 286 | /* Type casting to SHA1 context as offset is similar for SHA256 context */ |
| 287 | crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 288 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 289 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 292 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 293 | tmp_first = first; |
| 294 | tmp_saved_buff_indx = sha1_ctx->saved_buff_indx; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 295 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 296 | do { |
| 297 | if ((bytes_to_write - bytes_written) > |
Amol Jadi | 7d3ad06 | 2013-01-04 15:30:19 -0800 | [diff] [blame] | 298 | crypto_get_max_auth_blk_size()) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 299 | /* 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] | 300 | tmp_bytes = crypto_get_max_auth_blk_size(); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 301 | tmp_last = FALSE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 302 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 303 | 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 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 | tmp_bytes = (bytes_to_write - bytes_written); |
| 331 | tmp_last = last; |
| 332 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 333 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 334 | /* Set SHAx context in the crypto engine */ |
| 335 | crypto_set_sha_ctx(ctx_ptr, tmp_bytes, auth_alg, tmp_first, |
| 336 | tmp_last); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 337 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 338 | /* Send data to the crypto engine */ |
| 339 | crypto_send_data(ctx_ptr, tmp_buff_ptr, tmp_buff_size, |
| 340 | tmp_bytes, &ret_val); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 341 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 342 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 347 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 348 | /* 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 351 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 352 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 357 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 358 | if (!tmp_last) { |
| 359 | crypto_get_ctx(ctx_ptr); |
| 360 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 361 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 362 | bytes_written += tmp_bytes; |
| 363 | sha1_ctx->saved_buff_indx = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 364 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 365 | if (bytes_written != bytes_to_write) { |
| 366 | tmp_first = FALSE; |
| 367 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 368 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 369 | } |
| 370 | while ((bytes_to_write - bytes_written) != 0); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 371 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 372 | /* If there are bytes remaining, copy it to saved_buff */ |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 373 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 374 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 382 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 383 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 386 | /* |
| 387 | * Function to calculate the number of bytes to be sent to crypto engine. |
| 388 | */ |
| 389 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 390 | static unsigned int |
| 391 | 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] | 392 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 393 | unsigned int bytes_to_write = 0; |
| 394 | crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 395 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 396 | 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 Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 404 | } |