/src/hostap/src/crypto/sha384-internal.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * SHA-384 hash implementation and interface functions |
3 | | * Copyright (c) 2015, Pali Rohár <pali.rohar@gmail.com> |
4 | | * |
5 | | * This software may be distributed under the terms of the BSD license. |
6 | | * See README for more details. |
7 | | */ |
8 | | |
9 | | #include "includes.h" |
10 | | |
11 | | #include "common.h" |
12 | | #include "sha384_i.h" |
13 | | #include "crypto.h" |
14 | | |
15 | | |
16 | | /** |
17 | | * sha384_vector - SHA384 hash for data vector |
18 | | * @num_elem: Number of elements in the data vector |
19 | | * @addr: Pointers to the data areas |
20 | | * @len: Lengths of the data blocks |
21 | | * @mac: Buffer for the hash |
22 | | * Returns: 0 on success, -1 of failure |
23 | | */ |
24 | | int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len, |
25 | | u8 *mac) |
26 | 0 | { |
27 | 0 | struct sha384_state ctx; |
28 | 0 | size_t i; |
29 | |
|
30 | 0 | sha384_init(&ctx); |
31 | 0 | for (i = 0; i < num_elem; i++) |
32 | 0 | if (sha384_process(&ctx, addr[i], len[i])) |
33 | 0 | return -1; |
34 | 0 | if (sha384_done(&ctx, mac)) |
35 | 0 | return -1; |
36 | 0 | return 0; |
37 | 0 | } |
38 | | |
39 | | |
40 | | /* ===== start - public domain SHA384 implementation ===== */ |
41 | | |
42 | | /* This is based on SHA384 implementation in LibTomCrypt that was released into |
43 | | * public domain by Tom St Denis. */ |
44 | | |
45 | 992 | #define CONST64(n) n ## ULL |
46 | | |
47 | | /** |
48 | | Initialize the hash state |
49 | | @param md The hash state you wish to initialize |
50 | | @return CRYPT_OK if successful |
51 | | */ |
52 | | void sha384_init(struct sha384_state *md) |
53 | 124 | { |
54 | 124 | md->curlen = 0; |
55 | 124 | md->length = 0; |
56 | 124 | md->state[0] = CONST64(0xcbbb9d5dc1059ed8); |
57 | 124 | md->state[1] = CONST64(0x629a292a367cd507); |
58 | 124 | md->state[2] = CONST64(0x9159015a3070dd17); |
59 | 124 | md->state[3] = CONST64(0x152fecd8f70e5939); |
60 | 124 | md->state[4] = CONST64(0x67332667ffc00b31); |
61 | 124 | md->state[5] = CONST64(0x8eb44a8768581511); |
62 | 124 | md->state[6] = CONST64(0xdb0c2e0d64f98fa7); |
63 | 124 | md->state[7] = CONST64(0x47b5481dbefa4fa4); |
64 | 124 | } |
65 | | |
66 | | int sha384_process(struct sha384_state *md, const unsigned char *in, |
67 | | unsigned long inlen) |
68 | 372 | { |
69 | 372 | return sha512_process(md, in, inlen); |
70 | 372 | } |
71 | | |
72 | | /** |
73 | | Terminate the hash to get the digest |
74 | | @param md The hash state |
75 | | @param out [out] The destination of the hash (48 bytes) |
76 | | @return CRYPT_OK if successful |
77 | | */ |
78 | | int sha384_done(struct sha384_state *md, unsigned char *out) |
79 | 124 | { |
80 | 124 | unsigned char buf[64]; |
81 | | |
82 | 124 | if (md->curlen >= sizeof(md->buf)) |
83 | 0 | return -1; |
84 | | |
85 | 124 | if (sha512_done(md, buf) != 0) |
86 | 0 | return -1; |
87 | | |
88 | 124 | os_memcpy(out, buf, 48); |
89 | 124 | return 0; |
90 | 124 | } |
91 | | |
92 | | /* ===== end - public domain SHA384 implementation ===== */ |