1 /* ====================================================================
2 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * licensing@OpenSSL.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ==================================================================== */
49 #include <openssl/base.h>
53 #include "../../internal.h"
55 #if defined(__cplusplus)
60 // This is a generic 32-bit "collector" for message digest algorithms. It
61 // collects input character stream into chunks of 32-bit values and invokes the
62 // block function that performs the actual hash calculations. To make use of
63 // this mechanism, the following macros must be defined before including
66 // One of |DATA_ORDER_IS_BIG_ENDIAN| or |DATA_ORDER_IS_LITTLE_ENDIAN| must be
67 // defined to specify the byte order of the input stream.
69 // |HASH_CBLOCK| must be defined as the integer block size, in bytes.
71 // |HASH_CTX| must be defined as the name of the context structure, which must
72 // have at least the following members:
74 // typedef struct <name>_state_st {
75 // uint32_t h[<chaining length> / sizeof(uint32_t)];
77 // uint8_t data[HASH_CBLOCK];
82 // <chaining length> is the output length of the hash in bytes, before
83 // any truncation (e.g. 64 for SHA-224 and SHA-256, 128 for SHA-384 and
86 // |HASH_UPDATE| must be defined as the name of the "Update" function to
89 // |HASH_TRANSFORM| must be defined as the the name of the "Transform"
90 // function to generate.
92 // |HASH_FINAL| must be defined as the name of "Final" function to generate.
94 // |HASH_BLOCK_DATA_ORDER| must be defined as the name of the "Block" function.
95 // That function must be implemented manually. It must be capable of operating
96 // on *unaligned* input data in its original (data) byte order. It must have
99 // void HASH_BLOCK_DATA_ORDER(uint32_t *state, const uint8_t *data,
102 // It must update the hash state |state| with |num| blocks of data from |data|,
103 // where each block is |HASH_CBLOCK| bytes; i.e. |data| points to a array of
104 // |HASH_CBLOCK * num| bytes. |state| points to the |h| member of a |HASH_CTX|,
105 // and so will have |<chaining length> / sizeof(uint32_t)| elements.
107 // |HASH_MAKE_STRING(c, s)| must be defined as a block statement that converts
108 // the hash state |c->h| into the output byte order, storing the result in |s|.
110 #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
111 #error "DATA_ORDER must be defined!"
115 #error "HASH_CBLOCK must be defined!"
118 #error "HASH_CTX must be defined!"
122 #error "HASH_UPDATE must be defined!"
124 #ifndef HASH_TRANSFORM
125 #error "HASH_TRANSFORM must be defined!"
128 #error "HASH_FINAL must be defined!"
131 #ifndef HASH_BLOCK_DATA_ORDER
132 #error "HASH_BLOCK_DATA_ORDER must be defined!"
135 #ifndef HASH_MAKE_STRING
136 #error "HASH_MAKE_STRING must be defined!"
139 #if defined(DATA_ORDER_IS_BIG_ENDIAN)
141 #define HOST_c2l(c, l) \
143 (l) = (((uint32_t)(*((c)++))) << 24); \
144 (l) |= (((uint32_t)(*((c)++))) << 16); \
145 (l) |= (((uint32_t)(*((c)++))) << 8); \
146 (l) |= (((uint32_t)(*((c)++)))); \
149 #define HOST_l2c(l, c) \
151 *((c)++) = (uint8_t)(((l) >> 24) & 0xff); \
152 *((c)++) = (uint8_t)(((l) >> 16) & 0xff); \
153 *((c)++) = (uint8_t)(((l) >> 8) & 0xff); \
154 *((c)++) = (uint8_t)(((l)) & 0xff); \
157 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
159 #define HOST_c2l(c, l) \
161 (l) = (((uint32_t)(*((c)++)))); \
162 (l) |= (((uint32_t)(*((c)++))) << 8); \
163 (l) |= (((uint32_t)(*((c)++))) << 16); \
164 (l) |= (((uint32_t)(*((c)++))) << 24); \
167 #define HOST_l2c(l, c) \
169 *((c)++) = (uint8_t)(((l)) & 0xff); \
170 *((c)++) = (uint8_t)(((l) >> 8) & 0xff); \
171 *((c)++) = (uint8_t)(((l) >> 16) & 0xff); \
172 *((c)++) = (uint8_t)(((l) >> 24) & 0xff); \
177 int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) {
178 const uint8_t *data = data_;
184 uint32_t l = c->Nl + (((uint32_t)len) << 3);
189 c->Nh += (uint32_t)(len >> 29);
194 if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
195 OPENSSL_memcpy(c->data + n, data, HASH_CBLOCK - n);
196 HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
201 // Keep |c->data| zeroed when unused.
202 OPENSSL_memset(c->data, 0, HASH_CBLOCK);
204 OPENSSL_memcpy(c->data + n, data, len);
205 c->num += (unsigned)len;
210 n = len / HASH_CBLOCK;
212 HASH_BLOCK_DATA_ORDER(c->h, data, n);
219 c->num = (unsigned)len;
220 OPENSSL_memcpy(c->data, data, len);
226 void HASH_TRANSFORM(HASH_CTX *c, const uint8_t *data) {
227 HASH_BLOCK_DATA_ORDER(c->h, data, 1);
231 int HASH_FINAL(uint8_t *md, HASH_CTX *c) {
232 // |c->data| always has room for at least one byte. A full block would have
235 assert(n < HASH_CBLOCK);
239 // Fill the block with zeros if there isn't room for a 64-bit length.
240 if (n > (HASH_CBLOCK - 8)) {
241 OPENSSL_memset(c->data + n, 0, HASH_CBLOCK - n);
243 HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
245 OPENSSL_memset(c->data + n, 0, HASH_CBLOCK - 8 - n);
247 // Append a 64-bit length to the block and process it.
248 uint8_t *p = c->data + HASH_CBLOCK - 8;
249 #if defined(DATA_ORDER_IS_BIG_ENDIAN)
252 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
256 assert(p == c->data + HASH_CBLOCK);
257 HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
259 OPENSSL_memset(c->data, 0, HASH_CBLOCK);
261 HASH_MAKE_STRING(c, md);
266 #if defined(__cplusplus)