2 * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
13 * Derived from include/crypto/md32_common.h
15 * HASH_UPDATE and HASH_FINAL have been updated to work with the
16 * auth_hash interface.
19 #if defined(DATA_ORDER_IS_BIG_ENDIAN)
21 # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
22 l|=(((unsigned long)(*((c)++)))<<16), \
23 l|=(((unsigned long)(*((c)++)))<< 8), \
24 l|=(((unsigned long)(*((c)++))) ) )
25 # define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
26 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
27 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
28 *((c)++)=(unsigned char)(((l) )&0xff), \
31 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
33 # define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
34 l|=(((unsigned long)(*((c)++)))<< 8), \
35 l|=(((unsigned long)(*((c)++)))<<16), \
36 l|=(((unsigned long)(*((c)++)))<<24) )
37 # define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
38 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
39 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
40 *((c)++)=(unsigned char)(((l)>>24)&0xff), \
46 * Time for some action :-)
50 HASH_UPDATE(void *c_, const void *data_, unsigned int len)
53 const unsigned char *data = data_;
61 l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL;
62 if (l < c->Nl) /* overflow */
64 c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on
70 p = (unsigned char *)c->data;
72 if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
73 memcpy(p + n, data, HASH_CBLOCK - n);
74 HASH_BLOCK_DATA_ORDER(c, p, 1);
80 * We use memset rather than OPENSSL_cleanse() here deliberately.
81 * Using OPENSSL_cleanse() here could be a performance issue. It
82 * will get properly cleansed on finalisation so this isn't a
85 memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
87 memcpy(p + n, data, len);
88 c->num += (unsigned int)len;
93 n = len / HASH_CBLOCK;
95 HASH_BLOCK_DATA_ORDER(c, data, n);
102 p = (unsigned char *)c->data;
103 c->num = (unsigned int)len;
104 memcpy(p, data, len);
110 HASH_FINAL(uint8_t *md, void *c_)
113 unsigned char *p = (unsigned char *)c->data;
116 p[n] = 0x80; /* there is always room for one */
119 if (n > (HASH_CBLOCK - 8)) {
120 memset(p + n, 0, HASH_CBLOCK - n);
122 HASH_BLOCK_DATA_ORDER(c, p, 1);
124 memset(p + n, 0, HASH_CBLOCK - 8 - n);
126 p += HASH_CBLOCK - 8;
127 #if defined(DATA_ORDER_IS_BIG_ENDIAN)
128 (void)HOST_l2c(c->Nh, p);
129 (void)HOST_l2c(c->Nl, p);
130 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
131 (void)HOST_l2c(c->Nl, p);
132 (void)HOST_l2c(c->Nh, p);
135 HASH_BLOCK_DATA_ORDER(c, p, 1);
137 OPENSSL_cleanse(p, HASH_CBLOCK);
139 #ifndef HASH_MAKE_STRING
140 # error "HASH_MAKE_STRING must be defined!"
142 HASH_MAKE_STRING(c, md);