| OLD | NEW |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 * | 4 * |
| 5 * SHA-1 implementation largely based on libmincrypt in the the Android | 5 * SHA-1 implementation largely based on libmincrypt in the the Android |
| 6 * Open Source Project (platorm/system/core.git/libmincrypt/sha.c | 6 * Open Source Project (platorm/system/core.git/libmincrypt/sha.c |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "cryptolib.h" | 9 #include "cryptolib.h" |
| 10 #include "utility.h" | 10 #include "utility.h" |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 } | 217 } |
| 218 | 218 |
| 219 ctx->state[0] += A; | 219 ctx->state[0] += A; |
| 220 ctx->state[1] += B; | 220 ctx->state[1] += B; |
| 221 ctx->state[2] += C; | 221 ctx->state[2] += C; |
| 222 ctx->state[3] += D; | 222 ctx->state[3] += D; |
| 223 ctx->state[4] += E; | 223 ctx->state[4] += E; |
| 224 } | 224 } |
| 225 | 225 |
| 226 void SHA1_update(SHA1_CTX *ctx, const uint8_t *data, uint64_t len) { | 226 void SHA1_update(SHA1_CTX *ctx, const uint8_t *data, uint64_t len) { |
| 227 int i = ctx->count % sizeof(ctx->buf); | 227 int i = (int)(ctx->count % sizeof(ctx->buf)); |
| 228 const uint8_t* p = (const uint8_t*) data; | 228 const uint8_t* p = (const uint8_t*) data; |
| 229 | 229 |
| 230 ctx->count += len; | 230 ctx->count += len; |
| 231 | 231 |
| 232 while (len--) { | 232 while (len--) { |
| 233 ctx->buf[i++] = *p++; | 233 ctx->buf[i++] = *p++; |
| 234 if (i == sizeof(ctx->buf)) { | 234 if (i == sizeof(ctx->buf)) { |
| 235 SHA1_transform(ctx); | 235 SHA1_transform(ctx); |
| 236 i = 0; | 236 i = 0; |
| 237 } | 237 } |
| 238 } | 238 } |
| 239 } | 239 } |
| 240 uint8_t* SHA1_final(SHA1_CTX *ctx) { | 240 uint8_t* SHA1_final(SHA1_CTX *ctx) { |
| 241 uint8_t *p = ctx->buf; | 241 uint8_t *p = ctx->buf; |
| 242 uint64_t cnt = ctx->count * 8; | 242 uint64_t cnt = ctx->count << 3; |
| 243 int i; | 243 int i; |
| 244 | 244 |
| 245 SHA1_update(ctx, (uint8_t*)"\x80", 1); | 245 SHA1_update(ctx, (uint8_t*)"\x80", 1); |
| 246 while ((ctx->count % sizeof(ctx->buf)) != (sizeof(ctx->buf) - 8)) { | 246 while ((ctx->count % sizeof(ctx->buf)) != (sizeof(ctx->buf) - 8)) { |
| 247 SHA1_update(ctx, (uint8_t*)"\0", 1); | 247 SHA1_update(ctx, (uint8_t*)"\0", 1); |
| 248 } | 248 } |
| 249 for (i = 0; i < 8; ++i) { | 249 for (i = 0; i < 8; ++i) { |
| 250 uint8_t tmp = cnt >> ((7 - i) * 8); | 250 uint8_t tmp = (uint8_t)UINT64_RSHIFT(cnt, (7 - i) * 8); |
| 251 SHA1_update(ctx, &tmp, 1); | 251 SHA1_update(ctx, &tmp, 1); |
| 252 } | 252 } |
| 253 | 253 |
| 254 for (i = 0; i < 5; i++) { | 254 for (i = 0; i < 5; i++) { |
| 255 uint32_t tmp = ctx->state[i]; | 255 uint32_t tmp = ctx->state[i]; |
| 256 *p++ = tmp >> 24; | 256 *p++ = (uint8_t)(tmp >> 24); |
| 257 *p++ = tmp >> 16; | 257 *p++ = (uint8_t)(tmp >> 16); |
| 258 *p++ = tmp >> 8; | 258 *p++ = (uint8_t)(tmp >> 8); |
| 259 *p++ = tmp >> 0; | 259 *p++ = (uint8_t)(tmp >> 0); |
| 260 } | 260 } |
| 261 | 261 |
| 262 return ctx->buf; | 262 return ctx->buf; |
| 263 } | 263 } |
| 264 | 264 |
| 265 #endif /* endianness */ | 265 #endif /* endianness */ |
| 266 | 266 |
| 267 void SHA1_init(SHA1_CTX* ctx) { | 267 void SHA1_init(SHA1_CTX* ctx) { |
| 268 ctx->state[0] = 0x67452301; | 268 ctx->state[0] = 0x67452301; |
| 269 ctx->state[1] = 0xEFCDAB89; | 269 ctx->state[1] = 0xEFCDAB89; |
| 270 ctx->state[2] = 0x98BADCFE; | 270 ctx->state[2] = 0x98BADCFE; |
| 271 ctx->state[3] = 0x10325476; | 271 ctx->state[3] = 0x10325476; |
| 272 ctx->state[4] = 0xC3D2E1F0; | 272 ctx->state[4] = 0xC3D2E1F0; |
| 273 ctx->count = 0; | 273 ctx->count = 0; |
| 274 } | 274 } |
| 275 | 275 |
| 276 uint8_t* SHA1(const uint8_t *data, uint64_t len, uint8_t *digest) { | 276 uint8_t* SHA1(const uint8_t *data, uint64_t len, uint8_t *digest) { |
| 277 const uint8_t *p; | 277 const uint8_t *p; |
| 278 int i; | 278 int i; |
| 279 SHA1_CTX ctx; | 279 SHA1_CTX ctx; |
| 280 SHA1_init(&ctx); | 280 SHA1_init(&ctx); |
| 281 SHA1_update(&ctx, data, len); | 281 SHA1_update(&ctx, data, len); |
| 282 p = SHA1_final(&ctx); | 282 p = SHA1_final(&ctx); |
| 283 for (i = 0; i < SHA1_DIGEST_SIZE; ++i) { | 283 for (i = 0; i < SHA1_DIGEST_SIZE; ++i) { |
| 284 digest[i] = *p++; | 284 digest[i] = *p++; |
| 285 } | 285 } |
| 286 return digest; | 286 return digest; |
| 287 } | 287 } |
| OLD | NEW |