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