Home | History | Annotate | Download | only in crypto
      1 /*
      2  * SHA-384 hash implementation and interface functions
      3  * Copyright (c) 2015, Pali Rohr <pali.rohar (at) 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 {
     27 	struct sha384_state ctx;
     28 	size_t i;
     29 
     30 	sha384_init(&ctx);
     31 	for (i = 0; i < num_elem; i++)
     32 		if (sha384_process(&ctx, addr[i], len[i]))
     33 			return -1;
     34 	if (sha384_done(&ctx, mac))
     35 		return -1;
     36 	return 0;
     37 }
     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 #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 {
     54 	md->curlen = 0;
     55 	md->length = 0;
     56 	md->state[0] = CONST64(0xcbbb9d5dc1059ed8);
     57 	md->state[1] = CONST64(0x629a292a367cd507);
     58 	md->state[2] = CONST64(0x9159015a3070dd17);
     59 	md->state[3] = CONST64(0x152fecd8f70e5939);
     60 	md->state[4] = CONST64(0x67332667ffc00b31);
     61 	md->state[5] = CONST64(0x8eb44a8768581511);
     62 	md->state[6] = CONST64(0xdb0c2e0d64f98fa7);
     63 	md->state[7] = CONST64(0x47b5481dbefa4fa4);
     64 }
     65 
     66 int sha384_process(struct sha384_state *md, const unsigned char *in,
     67 		   unsigned long inlen)
     68 {
     69 	return sha512_process(md, in, inlen);
     70 }
     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 {
     80 	unsigned char buf[64];
     81 
     82 	if (md->curlen >= sizeof(md->buf))
     83 		return -1;
     84 
     85 	if (sha512_done(md, buf) != 0)
     86 		return -1;
     87 
     88 	os_memcpy(out, buf, 48);
     89 	return 0;
     90 }
     91 
     92 /* ===== end - public domain SHA384 implementation ===== */
     93