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