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 | 5 |
6 /* SHA-1 implementation largely based on libmincrypt in the the Android | 6 /* SHA-1 implementation largely based on libmincrypt in the the Android |
7 * Open Source Project (platorm/system/core.git/libmincrypt/sha.c | 7 * Open Source Project (platorm/system/core.git/libmincrypt/sha.c |
8 */ | 8 */ |
9 | 9 |
10 #include "sha.h" | 10 #include "sha.h" |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 120 |
121 #undef SHA_F4 | 121 #undef SHA_F4 |
122 | 122 |
123 ctx->state[0] += A; | 123 ctx->state[0] += A; |
124 ctx->state[1] += B; | 124 ctx->state[1] += B; |
125 ctx->state[2] += C; | 125 ctx->state[2] += C; |
126 ctx->state[3] += D; | 126 ctx->state[3] += D; |
127 ctx->state[4] += E; | 127 ctx->state[4] += E; |
128 } | 128 } |
129 | 129 |
130 void SHA1_update(SHA1_CTX* ctx, const uint8_t* data, size_t len) { | 130 void SHA1_update(SHA1_CTX* ctx, const uint8_t* data, uint64_t len) { |
131 int i = ctx->count % sizeof(ctx->buf); | 131 int i = ctx->count % sizeof(ctx->buf); |
132 const uint8_t* p = (const uint8_t*)data; | 132 const uint8_t* p = (const uint8_t*)data; |
133 | 133 |
134 ctx->count += len; | 134 ctx->count += len; |
135 | 135 |
136 while (len > sizeof(ctx->buf) - i) { | 136 while (len > sizeof(ctx->buf) - i) { |
137 memcpy(&ctx->buf.b[i], p, sizeof(ctx->buf) - i); | 137 memcpy(&ctx->buf.b[i], p, sizeof(ctx->buf) - i); |
138 len -= sizeof(ctx->buf) - i; | 138 len -= sizeof(ctx->buf) - i; |
139 p += sizeof(ctx->buf) - i; | 139 p += sizeof(ctx->buf) - i; |
140 SHA1_Transform(ctx); | 140 SHA1_Transform(ctx); |
141 i = 0; | 141 i = 0; |
142 } | 142 } |
143 | 143 |
144 while (len--) { | 144 while (len--) { |
145 ctx->buf.b[i++] = *p++; | 145 ctx->buf.b[i++] = *p++; |
146 if (i == sizeof(ctx->buf)) { | 146 if (i == sizeof(ctx->buf)) { |
147 SHA1_Transform(ctx); | 147 SHA1_Transform(ctx); |
148 i = 0; | 148 i = 0; |
149 } | 149 } |
150 } | 150 } |
151 } | 151 } |
152 | 152 |
153 | 153 |
154 uint8_t* SHA1_final(SHA_CTX* ctx) { | 154 uint8_t* SHA1_final(SHA1_CTX* ctx) { |
155 uint64_t cnt = ctx->count * 8; | 155 uint64_t cnt = ctx->count * 8; |
156 int i; | 156 int i; |
157 | 157 |
158 SHA1_update(ctx, (uint8_t*)"\x80", 1); | 158 SHA1_update(ctx, (uint8_t*)"\x80", 1); |
159 while ((ctx->count % sizeof(ctx->buf)) != (sizeof(ctx->buf) - 8)) { | 159 while ((ctx->count % sizeof(ctx->buf)) != (sizeof(ctx->buf) - 8)) { |
160 SHA1_update(ctx, (uint8_t*)"\0", 1); | 160 SHA1_update(ctx, (uint8_t*)"\0", 1); |
161 } | 161 } |
162 for (i = 0; i < 8; ++i) { | 162 for (i = 0; i < 8; ++i) { |
163 uint8_t tmp = cnt >> ((7 - i) * 8); | 163 uint8_t tmp = cnt >> ((7 - i) * 8); |
164 SHA1_update(ctx, &tmp, 1); | 164 SHA1_update(ctx, &tmp, 1); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 A = tmp; | 218 A = tmp; |
219 } | 219 } |
220 | 220 |
221 ctx->state[0] += A; | 221 ctx->state[0] += A; |
222 ctx->state[1] += B; | 222 ctx->state[1] += B; |
223 ctx->state[2] += C; | 223 ctx->state[2] += C; |
224 ctx->state[3] += D; | 224 ctx->state[3] += D; |
225 ctx->state[4] += E; | 225 ctx->state[4] += E; |
226 } | 226 } |
227 | 227 |
228 void SHA1_update(SHA1_CTX *ctx, const uint8_t *data, int len) { | 228 void SHA1_update(SHA1_CTX *ctx, const uint8_t *data, uint64_t len) { |
229 int i = ctx->count % sizeof(ctx->buf); | 229 int i = ctx->count % sizeof(ctx->buf); |
230 const uint8_t* p = (const uint8_t*) data; | 230 const uint8_t* p = (const uint8_t*) data; |
231 | 231 |
232 ctx->count += len; | 232 ctx->count += len; |
233 | 233 |
234 while (len--) { | 234 while (len--) { |
235 ctx->buf[i++] = *p++; | 235 ctx->buf[i++] = *p++; |
236 if (i == sizeof(ctx->buf)) { | 236 if (i == sizeof(ctx->buf)) { |
237 SHA1_transform(ctx); | 237 SHA1_transform(ctx); |
238 i = 0; | 238 i = 0; |
(...skipping 29 matching lines...) Expand all Loading... |
268 | 268 |
269 void SHA1_init(SHA1_CTX* ctx) { | 269 void SHA1_init(SHA1_CTX* ctx) { |
270 ctx->state[0] = 0x67452301; | 270 ctx->state[0] = 0x67452301; |
271 ctx->state[1] = 0xEFCDAB89; | 271 ctx->state[1] = 0xEFCDAB89; |
272 ctx->state[2] = 0x98BADCFE; | 272 ctx->state[2] = 0x98BADCFE; |
273 ctx->state[3] = 0x10325476; | 273 ctx->state[3] = 0x10325476; |
274 ctx->state[4] = 0xC3D2E1F0; | 274 ctx->state[4] = 0xC3D2E1F0; |
275 ctx->count = 0; | 275 ctx->count = 0; |
276 } | 276 } |
277 | 277 |
278 uint8_t* SHA1(const uint8_t *data, int len, uint8_t *digest) { | 278 uint8_t* SHA1(const uint8_t *data, uint64_t len, uint8_t *digest) { |
279 const uint8_t *p; | 279 const uint8_t *p; |
280 int i; | 280 int i; |
281 SHA1_CTX ctx; | 281 SHA1_CTX ctx; |
282 SHA1_init(&ctx); | 282 SHA1_init(&ctx); |
283 SHA1_update(&ctx, data, len); | 283 SHA1_update(&ctx, data, len); |
284 p = SHA1_final(&ctx); | 284 p = SHA1_final(&ctx); |
285 for (i = 0; i < SHA1_DIGEST_SIZE; ++i) { | 285 for (i = 0; i < SHA1_DIGEST_SIZE; ++i) { |
286 digest[i] = *p++; | 286 digest[i] = *p++; |
287 } | 287 } |
288 return digest; | 288 return digest; |
289 } | 289 } |
OLD | NEW |