]> git.cryptolib.org Git - arm-crypto-lib.git/blob - sha2/sha512.c
adding SHA-384
[arm-crypto-lib.git] / sha2 / sha512.c
1 /* sha512.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2011 Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdint.h>
21 #include <string.h>
22 #include "sha2_large_common.h"
23 #include "sha512.h"
24
25
26 void sha512_nextBlock (sha512_ctx_t* ctx, const void* block){
27         sha2_large_common_nextBlock(ctx, block);
28 }
29
30 void sha512_lastBlock(sha512_ctx_t* ctx, const void* block, uint16_t length_b){
31         sha2_large_common_lastBlock(ctx, block, length_b);
32 }
33
34 static const
35 uint64_t sha512_init_values[8] = {
36 0x6a09e667f3bcc908LL, 0xbb67ae8584caa73bLL, 0x3c6ef372fe94f82bLL, 0xa54ff53a5f1d36f1LL,
37 0x510e527fade682d1LL, 0x9b05688c2b3e6c1fLL, 0x1f83d9abfb41bd6bLL, 0x5be0cd19137e2179LL
38 };
39
40
41 void sha512_init(sha512_ctx_t* ctx){
42         ctx->length = 0;
43         memcpy(ctx->h, sha512_init_values, 8*8);
44 }
45
46 void sha512_ctx2hash(void* dest, const sha512_ctx_t* ctx){
47         uint8_t i=8, j, *s = (uint8_t*)(ctx->h);
48         do{
49                 j=7;
50                 do{
51                         *((uint8_t*)dest) = s[j];
52                         dest = (uint8_t*)dest + 1;
53                 }while(j--);
54                 s += 8;
55         }while(--i);
56 }
57
58
59 void sha512(void* dest, const void* msg, uint32_t length_b){
60         sha512_ctx_t ctx;
61         sha512_init(&ctx);
62         while(length_b >= 1024){
63                 sha512_nextBlock(&ctx, msg);
64                 msg = (uint8_t*)msg + 1024/8;
65                 length_b -= 1024;
66         }
67         sha512_lastBlock(&ctx, msg, length_b);
68         sha512_ctx2hash(dest, &ctx);
69 }