]> git.cryptolib.org Git - avr-crypto-lib.git/blob - sha2/sha384.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / sha2 / sha384.c
1 /* sha384.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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
21 #include <stdint.h>
22 #include <string.h>
23 #include <avr/pgmspace.h>
24 #include "sha2_large_common.h"
25 #include "sha384.h"
26
27
28 void sha384_nextBlock (sha384_ctx_t *ctx, const void *block){
29         sha2_large_common_nextBlock(ctx, block);
30 }
31
32 void sha384_lastBlock(sha384_ctx_t *ctx, const void *block, uint16_t length_b){
33         sha2_large_common_lastBlock(ctx, block, length_b);
34 }
35
36 static const
37 uint64_t sha384_init_values[8] PROGMEM = {
38 0xcbbb9d5dc1059ed8, 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939,
39 0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4
40 };
41
42
43 void sha384_init(sha384_ctx_t *ctx){
44         ctx->length = 0;
45         memcpy_P(ctx->h, sha384_init_values, 8*8);
46 }
47
48 void sha384_ctx2hash(void *dest, const sha384_ctx_t *ctx){
49         uint8_t i=6, j, *s = (uint8_t*)(ctx->h);
50         do{
51                 j=7;
52                 do{
53                         *((uint8_t*)dest) = s[j];
54                         dest = (uint8_t*)dest + 1;
55                 }while(j--);
56                 s += 8;
57         }while(--i);
58 }
59
60
61 void sha384(void *dest, const void *msg, uint32_t length_b){
62         sha384_ctx_t ctx;
63         sha384_init(&ctx);
64         while(length_b >= 1024){
65                 sha384_nextBlock(&ctx, msg);
66                 msg = (uint8_t*)msg + 1024/8;
67                 length_b -= 1024;
68         }
69         sha384_lastBlock(&ctx, msg, length_b);
70         sha384_ctx2hash(dest, &ctx);
71 }