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> |
vijay kumar | 4f4405f | 2014-08-08 11:49:53 +0530 | [diff] [blame] | 30 | #include <sha.h> |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 31 | #include <debug.h> |
| 32 | #include <sys/types.h> |
| 33 | #include "crypto_hash.h" |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 34 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 35 | static crypto_SHA256_ctx g_sha256_ctx; |
| 36 | static crypto_SHA1_ctx g_sha1_ctx; |
Channagoud Kadabi | d7df33d | 2013-12-04 12:35:46 -0800 | [diff] [blame] | 37 | static bool crypto_init_done; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 38 | |
Shashank Mittal | 1fcde7a | 2011-07-25 13:41:50 -0700 | [diff] [blame] | 39 | extern void ce_clock_init(void); |
| 40 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 41 | /* |
| 42 | * Top level function which calculates SHAx digest with given data and size. |
| 43 | * Digest varies based on the authentication algorithm. |
| 44 | * It works on contiguous data and does single pass calculation. |
| 45 | */ |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 46 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 47 | void |
| 48 | hash_find(unsigned char *addr, unsigned int size, unsigned char *digest, |
| 49 | unsigned char auth_alg) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 50 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 51 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
Deepa Dinamani | 5e5c21a | 2012-02-16 18:59:57 -0800 | [diff] [blame] | 52 | crypto_engine_type platform_ce_type = board_ce_type(); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 53 | |
Sundarajan Srinivasan | 6a2bad2 | 2013-11-11 18:48:27 -0800 | [diff] [blame] | 54 | if (auth_alg == CRYPTO_AUTH_ALG_SHA1) { |
Deepa Dinamani | 5e5c21a | 2012-02-16 18:59:57 -0800 | [diff] [blame] | 55 | if(platform_ce_type == CRYPTO_ENGINE_TYPE_SW) |
| 56 | /* Hardware CE is not present , use software hashing */ |
| 57 | digest = SHA1(addr, size, digest); |
| 58 | else if (platform_ce_type == CRYPTO_ENGINE_TYPE_HW) |
| 59 | ret_val = crypto_sha1(addr, size, digest); |
| 60 | else |
| 61 | ret_val = CRYPTO_SHA_ERR_FAIL; |
Sundarajan Srinivasan | 6a2bad2 | 2013-11-11 18:48:27 -0800 | [diff] [blame] | 62 | } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) { |
Deepa Dinamani | 5e5c21a | 2012-02-16 18:59:57 -0800 | [diff] [blame] | 63 | if(platform_ce_type == CRYPTO_ENGINE_TYPE_SW) |
| 64 | /* Hardware CE is not present , use software hashing */ |
| 65 | digest = SHA256(addr, size, digest); |
| 66 | else if (platform_ce_type == CRYPTO_ENGINE_TYPE_HW) |
| 67 | ret_val = crypto_sha256(addr, size, digest); |
| 68 | else |
| 69 | ret_val = CRYPTO_SHA_ERR_FAIL; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 70 | } |
Sundarajan Srinivasan | 6a2bad2 | 2013-11-11 18:48:27 -0800 | [diff] [blame] | 71 | else |
| 72 | ret_val = CRYPTO_SHA_ERR_FAIL; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 73 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 74 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 75 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val); |
| 76 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 79 | /* |
| 80 | * Function to reset and init crypto engine. It resets the engine for the |
| 81 | * first time. Used for multiple SHA operations. |
| 82 | */ |
| 83 | |
| 84 | static void crypto_init(void) |
| 85 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 86 | if (crypto_init_done != TRUE) { |
| 87 | ce_clock_init(); |
| 88 | crypto_eng_reset(); |
| 89 | crypto_init_done = TRUE; |
| 90 | } |
| 91 | crypto_eng_init(); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* |
Channagoud Kadabi | d7df33d | 2013-12-04 12:35:46 -0800 | [diff] [blame] | 95 | * Function to return if crypto is initialized |
| 96 | */ |
| 97 | |
| 98 | bool crypto_initialized() |
| 99 | { |
| 100 | return crypto_init_done; |
| 101 | } |
| 102 | |
| 103 | /* |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 104 | * Function to initialize SHA256 context |
| 105 | */ |
| 106 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 107 | static crypto_result_type crypto_sha256_init(crypto_SHA256_ctx * ctx_ptr) |
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 | unsigned int i; |
| 110 | /* Standard initialization vector for SHA256 */ |
| 111 | unsigned int sha256_init_vector[] = { 0x6A09E667, 0xBB67AE85, |
| 112 | 0x3C6EF372, 0xA54FF53A, |
| 113 | 0x510E527F, 0x9B05688C, |
| 114 | 0x1F83D9AB, 0x5BE0CD19 |
| 115 | }; |
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 | if (ctx_ptr == NULL) { |
| 118 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 119 | } |
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 | ctx_ptr->auth_bytecnt[0] = 0; |
| 122 | ctx_ptr->auth_bytecnt[1] = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 123 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 124 | memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 125 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 126 | for (i = 0; i < SHA256_INIT_VECTOR_SIZE; i++) { |
| 127 | ctx_ptr->auth_iv[i] = sha256_init_vector[i]; |
| 128 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 129 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 130 | ctx_ptr->saved_buff_indx = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 131 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 132 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 135 | /* |
| 136 | * Function to initialize SHA1 context |
| 137 | */ |
| 138 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 139 | static crypto_result_type crypto_sha1_init(crypto_SHA1_ctx * ctx_ptr) |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 140 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 141 | unsigned int i; |
| 142 | /* Standard initialization vector for SHA1 */ |
| 143 | unsigned int sha1_init_vector[] = { 0x67452301, 0xEFCDAB89, |
| 144 | 0x98BADCFE, 0x10325476, |
| 145 | 0xC3D2E1F0 |
| 146 | }; |
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 | if (ctx_ptr == NULL) { |
| 149 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 150 | } |
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 | ctx_ptr->auth_bytecnt[0] = 0; |
| 153 | ctx_ptr->auth_bytecnt[1] = 0; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 154 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 155 | memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE); |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 156 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 157 | for (i = 0; i < SHA1_INIT_VECTOR_SIZE; i++) { |
| 158 | ctx_ptr->auth_iv[i] = sha1_init_vector[i]; |
| 159 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 160 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 161 | ctx_ptr->saved_buff_indx = 0; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 162 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 163 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Function to calculate SHA256 digest of given data buffer. |
| 168 | * It works on contiguous data and gives digest in single pass. |
| 169 | */ |
| 170 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 171 | static crypto_result_type |
| 172 | crypto_sha256(unsigned char *buff_ptr, |
| 173 | unsigned int buff_size, unsigned char *digest_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 174 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 175 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
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 | if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) { |
| 178 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 179 | } |
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 | /* Initialize crypto engine hardware for a new SHA256 operation */ |
| 182 | crypto_init(); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 183 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 184 | /* Now do SHA256 hashing */ |
| 185 | ret_val = |
| 186 | do_sha(buff_ptr, buff_size, digest_ptr, CRYPTO_AUTH_ALG_SHA256); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 187 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 188 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 189 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val); |
| 190 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 191 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 192 | return ret_val; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 195 | /* |
| 196 | * Function to calculate SHA1 digest of given data buffer. |
| 197 | * It works on contiguous data and gives digest in single pass. |
| 198 | */ |
| 199 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 200 | static crypto_result_type |
| 201 | crypto_sha1(unsigned char *buff_ptr, |
| 202 | unsigned int buff_size, unsigned char *digest_ptr) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 203 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 204 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 205 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 206 | if ((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) { |
| 207 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 208 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 209 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 210 | /* Initialize crypto engine hardware for a new SHA1 operation */ |
| 211 | crypto_init(); |
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 | /* Now do SHA1 hashing */ |
| 214 | 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] | 215 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 216 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 217 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n", ret_val); |
| 218 | } |
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 | return ret_val; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Common function to calculate SHA1 and SHA256 digest based on auth algorithm. |
| 225 | */ |
| 226 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 227 | static crypto_result_type |
| 228 | do_sha(unsigned char *buff_ptr, |
| 229 | unsigned int buff_size, |
| 230 | unsigned char *digest_ptr, crypto_auth_alg_type auth_alg) |
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 | void *ctx_ptr = NULL; |
| 233 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 234 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 235 | /* Initialize SHA context based on algorithm */ |
| 236 | if (auth_alg == CRYPTO_AUTH_ALG_SHA1) { |
| 237 | crypto_sha1_init(&g_sha1_ctx); |
| 238 | ctx_ptr = (void *)&g_sha1_ctx; |
| 239 | } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) { |
| 240 | crypto_sha256_init(&g_sha256_ctx); |
| 241 | ctx_ptr = (void *)&g_sha256_ctx; |
| 242 | } |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 243 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 244 | ret_val = |
| 245 | 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] | 246 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 247 | if (ret_val != CRYPTO_SHA_ERR_NONE) { |
| 248 | dprintf(CRITICAL, "do_sha_update returns error %d\n", ret_val); |
| 249 | return ret_val; |
| 250 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 251 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 252 | /* Copy the digest value from context pointer to digest pointer */ |
| 253 | if (auth_alg == CRYPTO_AUTH_ALG_SHA1) { |
| 254 | memcpy(digest_ptr, |
| 255 | (unsigned char *)(((crypto_SHA1_ctx *) ctx_ptr)-> |
| 256 | auth_iv), 20); |
| 257 | } else if (auth_alg == CRYPTO_AUTH_ALG_SHA256) { |
| 258 | memcpy(digest_ptr, |
| 259 | (unsigned char *)(((crypto_SHA256_ctx *) ctx_ptr)-> |
| 260 | auth_iv), 32); |
| 261 | } |
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 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 266 | /* |
| 267 | * Common function to calculate SHA1 and SHA256 digest based on auth algorithm. |
| 268 | * Calls crypto engine APIs to setup SHAx registers, send the data and gets |
| 269 | * the digest. |
| 270 | */ |
| 271 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 272 | static crypto_result_type |
| 273 | do_sha_update(void *ctx_ptr, |
| 274 | unsigned char *buff_ptr, |
| 275 | unsigned int buff_size, |
| 276 | crypto_auth_alg_type auth_alg, bool first, bool last) |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 277 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 278 | unsigned int ret_val = CRYPTO_ERR_NONE; |
| 279 | unsigned int bytes_to_write = 0; |
| 280 | unsigned int bytes_remaining = 0; |
| 281 | unsigned int tmp_bytes = 0; |
| 282 | unsigned int bytes_written = 0; |
| 283 | unsigned int tmp_buff_size = 0; |
| 284 | unsigned char *tmp_buff_ptr = NULL; |
| 285 | unsigned char tmp_saved_buff_indx = 0; |
| 286 | bool tmp_first; |
| 287 | bool tmp_last; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 288 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 289 | /* Type casting to SHA1 context as offset is similar for SHA256 context */ |
| 290 | crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr; |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 291 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 292 | bytes_to_write = calc_num_bytes_to_send(ctx_ptr, buff_size, last); |
| 293 | bytes_remaining = |
| 294 | buff_size + sha1_ctx->saved_buff_indx - bytes_to_write; |
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 | tmp_first = first; |
| 297 | tmp_saved_buff_indx = sha1_ctx->saved_buff_indx; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 298 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 299 | do { |
| 300 | if ((bytes_to_write - bytes_written) > |
Amol Jadi | 7d3ad06 | 2013-01-04 15:30:19 -0800 | [diff] [blame] | 301 | crypto_get_max_auth_blk_size()) { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 302 | /* 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] | 303 | tmp_bytes = crypto_get_max_auth_blk_size(); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 304 | tmp_last = FALSE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 305 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 306 | if (sha1_ctx->saved_buff_indx != 0) { |
| 307 | tmp_buff_ptr = buff_ptr; |
| 308 | tmp_buff_size = |
| 309 | tmp_bytes - sha1_ctx->saved_buff_indx; |
| 310 | } else { |
| 311 | tmp_buff_ptr = |
| 312 | buff_ptr + bytes_written - |
| 313 | tmp_saved_buff_indx; |
| 314 | tmp_buff_size = tmp_bytes; |
| 315 | } |
| 316 | } else { |
| 317 | /* Since bytes_to_write are less than CRYPTO_MAX_AUTH_BLOCK_SIZE |
| 318 | write all remaining bytes now */ |
| 319 | if (sha1_ctx->saved_buff_indx != 0) { |
| 320 | tmp_buff_ptr = buff_ptr; |
| 321 | tmp_buff_size = |
| 322 | bytes_to_write - bytes_written - |
| 323 | sha1_ctx->saved_buff_indx; |
| 324 | } else { |
| 325 | tmp_buff_ptr = |
| 326 | buff_ptr + bytes_written - |
| 327 | tmp_saved_buff_indx; |
| 328 | tmp_buff_size = |
| 329 | bytes_to_write - bytes_written - |
| 330 | tmp_saved_buff_indx; |
| 331 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 332 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 333 | tmp_bytes = (bytes_to_write - bytes_written); |
| 334 | tmp_last = last; |
| 335 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 336 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 337 | /* Set SHAx context in the crypto engine */ |
| 338 | crypto_set_sha_ctx(ctx_ptr, tmp_bytes, auth_alg, tmp_first, |
| 339 | tmp_last); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 340 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 341 | /* Send data to the crypto engine */ |
| 342 | crypto_send_data(ctx_ptr, tmp_buff_ptr, tmp_buff_size, |
| 343 | tmp_bytes, &ret_val); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 344 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 345 | if (ret_val != CRYPTO_ERR_NONE) { |
| 346 | dprintf(CRITICAL, |
| 347 | "do_sha_update returns error from crypto_send_data\n"); |
| 348 | return CRYPTO_SHA_ERR_FAIL; |
| 349 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 350 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 351 | /* Get the SHAx digest from the crypto engine */ |
| 352 | crypto_get_digest((unsigned char *)(sha1_ctx->auth_iv), |
| 353 | &ret_val, auth_alg, tmp_last); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 354 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 355 | if (ret_val != CRYPTO_ERR_NONE) { |
| 356 | dprintf(CRITICAL, |
| 357 | "do_sha_update returns error from crypto_get_digest\n"); |
| 358 | return CRYPTO_SHA_ERR_FAIL; |
| 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 | if (!tmp_last) { |
| 362 | crypto_get_ctx(ctx_ptr); |
| 363 | } |
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 | bytes_written += tmp_bytes; |
| 366 | sha1_ctx->saved_buff_indx = 0; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 367 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 368 | if (bytes_written != bytes_to_write) { |
| 369 | tmp_first = FALSE; |
| 370 | } |
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 | } |
| 373 | while ((bytes_to_write - bytes_written) != 0); |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 374 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 375 | /* If there are bytes remaining, copy it to saved_buff */ |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 376 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 377 | if (bytes_remaining) { |
| 378 | memcpy(sha1_ctx->saved_buff, |
| 379 | (buff_ptr + buff_size - bytes_remaining), |
| 380 | bytes_remaining); |
| 381 | sha1_ctx->saved_buff_indx = bytes_remaining; |
| 382 | } else { |
| 383 | sha1_ctx->saved_buff_indx = 0; |
| 384 | } |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 385 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 386 | return CRYPTO_SHA_ERR_NONE; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Subbaraman Narayanamurthy | 8fcccbd | 2011-01-28 13:26:00 -0800 | [diff] [blame] | 389 | /* |
| 390 | * Function to calculate the number of bytes to be sent to crypto engine. |
| 391 | */ |
| 392 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 393 | static unsigned int |
| 394 | 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] | 395 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 396 | unsigned int bytes_to_write = 0; |
| 397 | crypto_SHA1_ctx *sha1_ctx = (crypto_SHA1_ctx *) ctx_ptr; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 398 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 399 | if (last) { |
| 400 | bytes_to_write = buff_size + sha1_ctx->saved_buff_indx; |
| 401 | } else { |
| 402 | bytes_to_write = ((buff_size + sha1_ctx->saved_buff_indx) / |
| 403 | CRYPTO_SHA_BLOCK_SIZE) * |
| 404 | CRYPTO_SHA_BLOCK_SIZE; |
| 405 | } |
| 406 | return bytes_to_write; |
Subbaraman Narayanamurthy | 9b7276c | 2011-01-25 17:25:30 -0800 | [diff] [blame] | 407 | } |