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" |
| 33 | #include "crypto_eng.h" |
| 34 | |
| 35 | crypto_result_type crypto_sha256(unsigned char *buff_ptr, |
| 36 | unsigned int buff_size, |
| 37 | unsigned char *digest_ptr); |
| 38 | |
| 39 | static unsigned int sha256_init_vector[] = { 0x6A09E667, 0xBB67AE85, |
| 40 | 0x3C6EF372, 0xA54FF53A, |
| 41 | 0x510E527F, 0x9B05688C, |
| 42 | 0x1F83D9AB, 0x5BE0CD19 }; |
| 43 | |
| 44 | crypto_SHA256_ctx g_sha256_ctx; |
| 45 | |
| 46 | void hash_find(unsigned char *addr, unsigned int size, unsigned char *digest) |
| 47 | { |
| 48 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
| 49 | |
| 50 | ret_val = crypto_sha256(addr,size,digest); |
| 51 | |
| 52 | if(ret_val != CRYPTO_SHA_ERR_NONE) |
| 53 | { |
| 54 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n",ret_val); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | crypto_result_type crypto_sha256_init(crypto_SHA256_ctx *ctx_ptr) |
| 59 | { |
| 60 | unsigned int i; |
| 61 | |
| 62 | if(ctx_ptr == NULL) |
| 63 | { |
| 64 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 65 | } |
| 66 | |
| 67 | ctx_ptr->auth_bytecnt[0] = 0; |
| 68 | ctx_ptr->auth_bytecnt[1] = 0; |
| 69 | |
| 70 | memset(ctx_ptr->saved_buff, 0, CRYPTO_SHA_BLOCK_SIZE); |
| 71 | |
| 72 | for(i=0;i<SHA256_INIT_VECTOR_SIZE;i++) |
| 73 | { |
| 74 | ctx_ptr->auth_iv[i] = sha256_init_vector[i]; |
| 75 | } |
| 76 | |
| 77 | ctx_ptr->saved_buff_indx = 0; |
| 78 | |
| 79 | return CRYPTO_SHA_ERR_NONE; |
| 80 | } |
| 81 | |
| 82 | crypto_result_type crypto_sha256(unsigned char *buff_ptr, |
| 83 | unsigned int buff_size, |
| 84 | unsigned char *digest_ptr) |
| 85 | { |
| 86 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
| 87 | |
| 88 | if((!buff_size) || (buff_ptr == NULL) || (digest_ptr == NULL)) |
| 89 | { |
| 90 | return CRYPTO_SHA_ERR_INVALID_PARAM; |
| 91 | } |
| 92 | |
| 93 | crypto_init(); |
| 94 | |
| 95 | ret_val = do_sha(buff_ptr, buff_size, digest_ptr); |
| 96 | |
| 97 | if(ret_val != CRYPTO_SHA_ERR_NONE) |
| 98 | { |
| 99 | dprintf(CRITICAL, "crypto_sha256 returns error %d\n",ret_val); |
| 100 | } |
| 101 | |
| 102 | return ret_val; |
| 103 | } |
| 104 | |
| 105 | static crypto_result_type do_sha(unsigned char *buff_ptr, |
| 106 | unsigned int buff_size, |
| 107 | unsigned char *digest_ptr) |
| 108 | { |
| 109 | crypto_SHA256_ctx *ctx_ptr=NULL; |
| 110 | crypto_result_type ret_val = CRYPTO_SHA_ERR_NONE; |
| 111 | |
| 112 | crypto_sha256_init(&g_sha256_ctx); |
| 113 | ctx_ptr = &g_sha256_ctx; |
| 114 | |
| 115 | ret_val = do_sha_update(ctx_ptr, buff_ptr, buff_size, TRUE, TRUE); |
| 116 | |
| 117 | if(ret_val != CRYPTO_SHA_ERR_NONE) |
| 118 | { |
| 119 | dprintf(CRITICAL, "do_sha_update returns error %d\n",ret_val); |
| 120 | return ret_val; |
| 121 | } |
| 122 | |
| 123 | memcpy(digest_ptr, (unsigned char *)(ctx_ptr->auth_iv), 32); |
| 124 | |
| 125 | return CRYPTO_SHA_ERR_NONE; |
| 126 | } |
| 127 | |
| 128 | static crypto_result_type do_sha_update(crypto_SHA256_ctx *ctx_ptr, |
| 129 | unsigned char *buff_ptr, |
| 130 | unsigned int buff_size, |
| 131 | bool first, bool last) |
| 132 | { |
| 133 | unsigned int ret_val = CRYPTO_ERR_NONE; |
| 134 | unsigned int bytes_to_write = 0; |
| 135 | unsigned int bytes_remaining = 0; |
| 136 | unsigned int tmp_bytes = 0; |
| 137 | unsigned int bytes_written = 0; |
| 138 | unsigned int tmp_buff_size = 0; |
| 139 | unsigned char *tmp_buff_ptr = NULL; |
| 140 | unsigned char tmp_saved_buff_indx = NULL; |
| 141 | bool tmp_first; |
| 142 | bool tmp_last; |
| 143 | |
| 144 | bytes_to_write = calc_num_bytes_to_send(ctx_ptr, buff_size, last); |
| 145 | bytes_remaining = buff_size + ctx_ptr->saved_buff_indx - bytes_to_write; |
| 146 | |
| 147 | tmp_first = first; |
| 148 | tmp_saved_buff_indx = ctx_ptr->saved_buff_indx; |
| 149 | |
| 150 | do |
| 151 | { |
| 152 | if((bytes_to_write - bytes_written) > CRYPTO_MAX_AUTH_BLOCK_SIZE) |
| 153 | { |
| 154 | tmp_bytes = CRYPTO_MAX_AUTH_BLOCK_SIZE; |
| 155 | tmp_last = FALSE; |
| 156 | |
| 157 | if(ctx_ptr->saved_buff_indx != 0) |
| 158 | { |
| 159 | tmp_buff_ptr = buff_ptr; |
| 160 | tmp_buff_size = tmp_bytes - ctx_ptr->saved_buff_indx; |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | tmp_buff_ptr = buff_ptr + bytes_written - tmp_saved_buff_indx; |
| 165 | tmp_buff_size = tmp_bytes; |
| 166 | } |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | if(ctx_ptr->saved_buff_indx != 0) |
| 171 | { |
| 172 | tmp_buff_ptr = buff_ptr; |
| 173 | tmp_buff_size = bytes_to_write - bytes_written - ctx_ptr->saved_buff_indx; |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | tmp_buff_ptr = buff_ptr + bytes_written - tmp_saved_buff_indx; |
| 178 | tmp_buff_size = bytes_to_write - bytes_written - tmp_saved_buff_indx; |
| 179 | } |
| 180 | |
| 181 | tmp_bytes = (bytes_to_write - bytes_written); |
| 182 | tmp_last = last; |
| 183 | } |
| 184 | |
| 185 | crypto_set_sha_ctx(ctx_ptr, tmp_bytes, tmp_first, tmp_last); |
| 186 | |
| 187 | crypto_send_data(ctx_ptr, tmp_buff_ptr, tmp_buff_size, tmp_bytes, &ret_val); |
| 188 | |
| 189 | if(ret_val != CRYPTO_ERR_NONE) |
| 190 | { |
| 191 | return CRYPTO_SHA_ERR_FAIL; |
| 192 | } |
| 193 | |
| 194 | crypto_get_digest((unsigned char *)(ctx_ptr->auth_iv), &ret_val, tmp_last); |
| 195 | |
| 196 | if(ret_val != CRYPTO_ERR_NONE) |
| 197 | { |
| 198 | return CRYPTO_SHA_ERR_FAIL; |
| 199 | } |
| 200 | |
| 201 | if(!tmp_last) |
| 202 | { |
| 203 | crypto_get_ctx(ctx_ptr); |
| 204 | } |
| 205 | |
| 206 | bytes_written += tmp_bytes; |
| 207 | ctx_ptr->saved_buff_indx = 0; |
| 208 | |
| 209 | if(bytes_written != bytes_to_write) |
| 210 | { |
| 211 | tmp_first = FALSE; |
| 212 | } |
| 213 | |
| 214 | }while ((bytes_to_write - bytes_written) != 0); |
| 215 | |
| 216 | if(bytes_remaining) |
| 217 | { |
| 218 | memcpy(ctx_ptr->saved_buff, (buff_ptr + buff_size - bytes_remaining), |
| 219 | bytes_remaining); |
| 220 | ctx_ptr->saved_buff_indx = bytes_remaining; |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | ctx_ptr->saved_buff_indx = 0; |
| 225 | } |
| 226 | |
| 227 | return CRYPTO_SHA_ERR_NONE; |
| 228 | } |
| 229 | |
| 230 | static unsigned int calc_num_bytes_to_send(crypto_SHA256_ctx *ctx_ptr, |
| 231 | unsigned int buff_size, bool last) |
| 232 | { |
| 233 | unsigned int bytes_to_write=0; |
| 234 | |
| 235 | if(last) |
| 236 | { |
| 237 | bytes_to_write = buff_size + ctx_ptr->saved_buff_indx; |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | bytes_to_write = ((buff_size + ctx_ptr->saved_buff_indx)/ |
| 242 | CRYPTO_SHA_BLOCK_SIZE) * CRYPTO_SHA_BLOCK_SIZE; |
| 243 | } |
| 244 | return bytes_to_write; |
| 245 | } |