OLD | NEW |
1 /* SHA-256 and SHA-512 implementation based on code by Oliver Gay | 1 /* SHA-256 and SHA-512 implementation based on code by Oliver Gay |
2 * <olivier.gay@a3.epfl.ch> under a BSD-style license. See below. | 2 * <olivier.gay@a3.epfl.ch> under a BSD-style license. See below. |
3 */ | 3 */ |
4 | 4 |
5 /* | 5 /* |
6 * FIPS 180-2 SHA-224/256/384/512 implementation | 6 * FIPS 180-2 SHA-224/256/384/512 implementation |
7 * Last update: 02/02/2007 | 7 * Last update: 02/02/2007 |
8 * Issue date: 04/30/2005 | 8 * Issue date: 04/30/2005 |
9 * | 9 * |
10 * Copyright (C) 2005, 2007 Olivier Gay <olivier.gay@a3.epfl.ch> | 10 * Copyright (C) 2005, 2007 Olivier Gay <olivier.gay@a3.epfl.ch> |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 #define PACK32(str, x) \ | 65 #define PACK32(str, x) \ |
66 { \ | 66 { \ |
67 *(x) = ((uint32_t) *((str) + 3) ) \ | 67 *(x) = ((uint32_t) *((str) + 3) ) \ |
68 | ((uint32_t) *((str) + 2) << 8) \ | 68 | ((uint32_t) *((str) + 2) << 8) \ |
69 | ((uint32_t) *((str) + 1) << 16) \ | 69 | ((uint32_t) *((str) + 1) << 16) \ |
70 | ((uint32_t) *((str) + 0) << 24); \ | 70 | ((uint32_t) *((str) + 0) << 24); \ |
71 } | 71 } |
72 | 72 |
73 #define UNPACK64(x, str) \ | 73 #define UNPACK64(x, str) \ |
74 { \ | 74 { \ |
75 *((str) + 7) = (uint8_t) ((x) ); \ | 75 *((str) + 7) = (uint8_t) x; \ |
76 *((str) + 6) = (uint8_t) ((x) >> 8); \ | 76 *((str) + 6) = (uint8_t) UINT64_RSHIFT(x, 8); \ |
77 *((str) + 5) = (uint8_t) ((x) >> 16); \ | 77 *((str) + 5) = (uint8_t) UINT64_RSHIFT(x, 16); \ |
78 *((str) + 4) = (uint8_t) ((x) >> 24); \ | 78 *((str) + 4) = (uint8_t) UINT64_RSHIFT(x, 24); \ |
79 *((str) + 3) = (uint8_t) ((x) >> 32); \ | 79 *((str) + 3) = (uint8_t) UINT64_RSHIFT(x, 32); \ |
80 *((str) + 2) = (uint8_t) ((x) >> 40); \ | 80 *((str) + 2) = (uint8_t) UINT64_RSHIFT(x, 40); \ |
81 *((str) + 1) = (uint8_t) ((x) >> 48); \ | 81 *((str) + 1) = (uint8_t) UINT64_RSHIFT(x, 48); \ |
82 *((str) + 0) = (uint8_t) ((x) >> 56); \ | 82 *((str) + 0) = (uint8_t) UINT64_RSHIFT(x, 56); \ |
83 } | 83 } |
84 | 84 |
85 #define PACK64(str, x) \ | 85 #define PACK64(str, x) \ |
86 { \ | 86 { \ |
87 *(x) = ((uint64_t) *((str) + 7) ) \ | 87 *(x) = ((uint64_t) *((str) + 7) ) \ |
88 | ((uint64_t) *((str) + 6) << 8) \ | 88 | ((uint64_t) *((str) + 6) << 8) \ |
89 | ((uint64_t) *((str) + 5) << 16) \ | 89 | ((uint64_t) *((str) + 5) << 16) \ |
90 | ((uint64_t) *((str) + 4) << 24) \ | 90 | ((uint64_t) *((str) + 4) << 24) \ |
91 | ((uint64_t) *((str) + 3) << 32) \ | 91 | ((uint64_t) *((str) + 3) << 32) \ |
92 | ((uint64_t) *((str) + 2) << 40) \ | 92 | ((uint64_t) *((str) + 2) << 40) \ |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 } | 331 } |
332 | 332 |
333 | 333 |
334 | 334 |
335 void SHA256_update(SHA256_CTX* ctx, const uint8_t* data, uint64_t len) { | 335 void SHA256_update(SHA256_CTX* ctx, const uint8_t* data, uint64_t len) { |
336 unsigned int block_nb; | 336 unsigned int block_nb; |
337 unsigned int new_len, rem_len, tmp_len; | 337 unsigned int new_len, rem_len, tmp_len; |
338 const uint8_t *shifted_data; | 338 const uint8_t *shifted_data; |
339 | 339 |
340 tmp_len = SHA256_BLOCK_SIZE - ctx->len; | 340 tmp_len = SHA256_BLOCK_SIZE - ctx->len; |
341 rem_len = len < tmp_len ? len : tmp_len; | 341 rem_len = len < tmp_len ? (unsigned int)len : tmp_len; |
342 | 342 |
343 Memcpy(&ctx->block[ctx->len], data, rem_len); | 343 Memcpy(&ctx->block[ctx->len], data, rem_len); |
344 | 344 |
345 if (ctx->len + len < SHA256_BLOCK_SIZE) { | 345 if (ctx->len + len < SHA256_BLOCK_SIZE) { |
346 ctx->len += len; | 346 ctx->len += (uint32_t)len; |
347 return; | 347 return; |
348 } | 348 } |
349 | 349 |
350 new_len = len - rem_len; | 350 new_len = (unsigned int)len - rem_len; |
351 block_nb = new_len / SHA256_BLOCK_SIZE; | 351 block_nb = new_len / SHA256_BLOCK_SIZE; |
352 | 352 |
353 shifted_data = data + rem_len; | 353 shifted_data = data + rem_len; |
354 | 354 |
355 SHA256_transform(ctx, ctx->block, 1); | 355 SHA256_transform(ctx, ctx->block, 1); |
356 SHA256_transform(ctx, shifted_data, block_nb); | 356 SHA256_transform(ctx, shifted_data, block_nb); |
357 | 357 |
358 rem_len = new_len % SHA256_BLOCK_SIZE; | 358 rem_len = new_len % SHA256_BLOCK_SIZE; |
359 | 359 |
360 Memcpy(ctx->block, &shifted_data[block_nb << 6], | 360 Memcpy(ctx->block, &shifted_data[block_nb << 6], |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 } | 519 } |
520 | 520 |
521 | 521 |
522 void SHA512_update(SHA512_CTX* ctx, const uint8_t* data, | 522 void SHA512_update(SHA512_CTX* ctx, const uint8_t* data, |
523 uint64_t len) { | 523 uint64_t len) { |
524 unsigned int block_nb; | 524 unsigned int block_nb; |
525 unsigned int new_len, rem_len, tmp_len; | 525 unsigned int new_len, rem_len, tmp_len; |
526 const uint8_t* shifted_data; | 526 const uint8_t* shifted_data; |
527 | 527 |
528 tmp_len = SHA512_BLOCK_SIZE - ctx->len; | 528 tmp_len = SHA512_BLOCK_SIZE - ctx->len; |
529 rem_len = len < tmp_len ? len : tmp_len; | 529 rem_len = len < tmp_len ? (unsigned int)len : tmp_len; |
530 | 530 |
531 Memcpy(&ctx->block[ctx->len], data, rem_len); | 531 Memcpy(&ctx->block[ctx->len], data, rem_len); |
532 | 532 |
533 if (ctx->len + len < SHA512_BLOCK_SIZE) { | 533 if (ctx->len + len < SHA512_BLOCK_SIZE) { |
534 ctx->len += len; | 534 ctx->len += (uint32_t)len; |
535 return; | 535 return; |
536 } | 536 } |
537 | 537 |
538 new_len = len - rem_len; | 538 new_len = (unsigned int)len - rem_len; |
539 block_nb = new_len / SHA512_BLOCK_SIZE; | 539 block_nb = new_len / SHA512_BLOCK_SIZE; |
540 | 540 |
541 shifted_data = data + rem_len; | 541 shifted_data = data + rem_len; |
542 | 542 |
543 SHA512_transform(ctx, ctx->block, 1); | 543 SHA512_transform(ctx, ctx->block, 1); |
544 SHA512_transform(ctx, shifted_data, block_nb); | 544 SHA512_transform(ctx, shifted_data, block_nb); |
545 | 545 |
546 rem_len = new_len % SHA512_BLOCK_SIZE; | 546 rem_len = new_len % SHA512_BLOCK_SIZE; |
547 | 547 |
548 Memcpy(ctx->block, &shifted_data[block_nb << 7], | 548 Memcpy(ctx->block, &shifted_data[block_nb << 7], |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 int i; | 614 int i; |
615 SHA512_CTX ctx; | 615 SHA512_CTX ctx; |
616 SHA512_init(&ctx); | 616 SHA512_init(&ctx); |
617 SHA512_update(&ctx, data, len); | 617 SHA512_update(&ctx, data, len); |
618 p = SHA512_final(&ctx); | 618 p = SHA512_final(&ctx); |
619 for (i = 0; i < SHA512_DIGEST_SIZE; ++i) { | 619 for (i = 0; i < SHA512_DIGEST_SIZE; ++i) { |
620 digest[i] = *p++; | 620 digest[i] = *p++; |
621 } | 621 } |
622 return digest; | 622 return digest; |
623 } | 623 } |
OLD | NEW |