/src/openssl41/include/crypto/md32_common.inc
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1999-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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 |
8 | | */ |
9 | | |
10 | | /*- |
11 | | * This is a generic 32 bit "collector" for message digest algorithms. |
12 | | * Whenever needed it collects input character stream into chunks of |
13 | | * 32 bit values and invokes a block function that performs actual hash |
14 | | * calculations. |
15 | | * |
16 | | * Porting guide. |
17 | | * |
18 | | * Obligatory macros: |
19 | | * |
20 | | * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN |
21 | | * this macro defines byte order of input stream. |
22 | | * HASH_CBLOCK |
23 | | * size of a unit chunk HASH_BLOCK operates on. |
24 | | * HASH_LONG |
25 | | * has to be at least 32 bit wide. |
26 | | * HASH_CTX |
27 | | * context structure that at least contains following |
28 | | * members: |
29 | | * typedef struct { |
30 | | * ... |
31 | | * HASH_LONG Nl,Nh; |
32 | | * either { |
33 | | * HASH_LONG data[HASH_LBLOCK]; |
34 | | * unsigned char data[HASH_CBLOCK]; |
35 | | * }; |
36 | | * unsigned int num; |
37 | | * ... |
38 | | * } HASH_CTX; |
39 | | * data[] vector is expected to be zeroed upon first call to |
40 | | * HASH_UPDATE. |
41 | | * HASH_UPDATE |
42 | | * name of "Update" function, implemented here. |
43 | | * HASH_TRANSFORM |
44 | | * name of "Transform" function, implemented here. |
45 | | * HASH_FINAL |
46 | | * name of "Final" function, implemented here. |
47 | | * HASH_BLOCK_DATA_ORDER |
48 | | * name of "block" function capable of treating *unaligned* input |
49 | | * message in original (data) byte order, implemented externally. |
50 | | * HASH_MAKE_STRING |
51 | | * macro converting context variables to an ASCII hash string. |
52 | | * |
53 | | * MD5 example: |
54 | | * |
55 | | * #define DATA_ORDER_IS_LITTLE_ENDIAN |
56 | | * |
57 | | * #define HASH_LONG MD5_LONG |
58 | | * #define HASH_CTX MD5_CTX |
59 | | * #define HASH_CBLOCK MD5_CBLOCK |
60 | | * #define HASH_UPDATE MD5_Update |
61 | | * #define HASH_TRANSFORM MD5_Transform |
62 | | * #define HASH_FINAL MD5_Final |
63 | | * #define HASH_BLOCK_DATA_ORDER md5_block_data_order |
64 | | */ |
65 | | |
66 | | #ifndef OSSL_CRYPTO_MD32_COMMON_H |
67 | | #define OSSL_CRYPTO_MD32_COMMON_H |
68 | | #pragma once |
69 | | |
70 | | #include <openssl/crypto.h> |
71 | | /* |
72 | | * For ossl_(un)likely |
73 | | */ |
74 | | #include <internal/common.h> |
75 | | |
76 | | #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN) |
77 | | #error "DATA_ORDER must be defined!" |
78 | | #endif |
79 | | |
80 | | #ifndef HASH_CBLOCK |
81 | | #error "HASH_CBLOCK must be defined!" |
82 | | #endif |
83 | | #ifndef HASH_LONG |
84 | | #error "HASH_LONG must be defined!" |
85 | | #endif |
86 | | #ifndef HASH_CTX |
87 | | #error "HASH_CTX must be defined!" |
88 | | #endif |
89 | | |
90 | | #ifndef HASH_UPDATE |
91 | | #error "HASH_UPDATE must be defined!" |
92 | | #endif |
93 | | #ifndef HASH_TRANSFORM |
94 | | #error "HASH_TRANSFORM must be defined!" |
95 | | #endif |
96 | | #ifndef HASH_FINAL |
97 | | #error "HASH_FINAL must be defined!" |
98 | | #endif |
99 | | |
100 | | #ifndef HASH_BLOCK_DATA_ORDER |
101 | | #error "HASH_BLOCK_DATA_ORDER must be defined!" |
102 | | #endif |
103 | | |
104 | 2.31G | #define ROTATE(a, n) (((a) << (n)) | (((a) & 0xffffffff) >> (32 - (n)))) |
105 | | |
106 | | #ifndef PEDANTIC |
107 | | #if defined(__GNUC__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) |
108 | | #if defined(__riscv_zbb) || defined(__riscv_zbkb) |
109 | | #if __riscv_xlen == 64 |
110 | | #undef ROTATE |
111 | | #define ROTATE(x, n) ({ MD32_REG_T ret; \ |
112 | | asm ("roriw %0, %1, %2" \ |
113 | | : "=r"(ret) \ |
114 | | : "r"(x), "i"(32 - (n))); ret; }) |
115 | | #endif |
116 | | #if __riscv_xlen == 32 |
117 | | #undef ROTATE |
118 | | #define ROTATE(x, n) ({ MD32_REG_T ret; \ |
119 | | asm ("rori %0, %1, %2" \ |
120 | | : "=r"(ret) \ |
121 | | : "r"(x), "i"(32 - (n))); ret; }) |
122 | | #endif |
123 | | # elif defined(__e2k__) |
124 | | # undef ROTATE |
125 | | # define ROTATE(a,n) ( (__builtin_constant_p(n) && (n) > 16) \ |
126 | | ? __builtin_e2k_scrs((a), 32 - (n)) \ |
127 | | : __builtin_e2k_scls((a), (n)) ) |
128 | | #endif |
129 | | #endif |
130 | | #endif |
131 | | |
132 | | #if defined(DATA_ORDER_IS_BIG_ENDIAN) |
133 | | |
134 | 31.7M | #define HOST_c2l(c, l) (l = (((unsigned long)(*((c)++))) << 24), \ |
135 | 31.7M | l |= (((unsigned long)(*((c)++))) << 16), \ |
136 | 31.7M | l |= (((unsigned long)(*((c)++))) << 8), \ |
137 | 31.7M | l |= (((unsigned long)(*((c)++))))) |
138 | 7.47G | #define HOST_l2c(l, c) (*((c)++) = (unsigned char)(((l) >> 24) & 0xff), \ |
139 | 7.47G | *((c)++) = (unsigned char)(((l) >> 16) & 0xff), \ |
140 | 7.47G | *((c)++) = (unsigned char)(((l) >> 8) & 0xff), \ |
141 | 7.47G | *((c)++) = (unsigned char)(((l)) & 0xff), \ |
142 | 7.47G | l) |
143 | | |
144 | | #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) |
145 | | |
146 | 62.2M | #define HOST_c2l(c, l) (l = (((unsigned long)(*((c)++)))), \ |
147 | 62.2M | l |= (((unsigned long)(*((c)++))) << 8), \ |
148 | 62.2M | l |= (((unsigned long)(*((c)++))) << 16), \ |
149 | 62.2M | l |= (((unsigned long)(*((c)++))) << 24)) |
150 | 25.3M | #define HOST_l2c(l, c) (*((c)++) = (unsigned char)(((l)) & 0xff), \ |
151 | 25.3M | *((c)++) = (unsigned char)(((l) >> 8) & 0xff), \ |
152 | 25.3M | *((c)++) = (unsigned char)(((l) >> 16) & 0xff), \ |
153 | 25.3M | *((c)++) = (unsigned char)(((l) >> 24) & 0xff), \ |
154 | 25.3M | l) |
155 | | |
156 | | #endif |
157 | | |
158 | | /* |
159 | | * Time for some action :-) |
160 | | */ |
161 | | |
162 | | #ifdef HASH_UPDATE_THUNK |
163 | | int HASH_UPDATE(void *cp, const unsigned char *data_, size_t len); |
164 | | int HASH_UPDATE(void *cp, const unsigned char *data_, size_t len) |
165 | | #else |
166 | | int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) |
167 | | #endif |
168 | 843M | { |
169 | | #ifdef HASH_UPDATE_THUNK |
170 | 835M | HASH_CTX *c = (HASH_CTX *)cp; |
171 | | #endif |
172 | 843M | const unsigned char *data = data_; |
173 | 843M | unsigned char *p; |
174 | 843M | HASH_LONG l; |
175 | 843M | size_t n; |
176 | | |
177 | 843M | if (ossl_unlikely(len == 0)) |
178 | 0 | return 1; |
179 | | |
180 | 843M | l = (c->Nl + (((HASH_LONG)len) << 3)) & 0xffffffffUL; |
181 | 843M | if (ossl_unlikely(l < c->Nl)) /* overflow */ |
182 | 0 | c->Nh++; |
183 | 843M | c->Nh += (HASH_LONG)(len >> 29); /* might cause compiler warning on |
184 | | * 16-bit */ |
185 | 843M | c->Nl = l; |
186 | | |
187 | 843M | n = c->num; |
188 | 843M | if (ossl_likely(n != 0)) { |
189 | | /* Gets here if we already have buffered input data */ |
190 | 423M | p = (unsigned char *)c->data; |
191 | | |
192 | 423M | if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) { |
193 | | /* |
194 | | * If there is enough input to fill the buffer then fill the |
195 | | * buffer and process a single chunk. |
196 | | */ |
197 | 4.10M | memcpy(p + n, data, HASH_CBLOCK - n); |
198 | 4.10M | HASH_BLOCK_DATA_ORDER(c, p, 1); |
199 | 4.10M | n = HASH_CBLOCK - n; |
200 | 4.10M | data += n; |
201 | 4.10M | len -= n; |
202 | 4.10M | c->num = 0; |
203 | | /* |
204 | | * We use memset rather than OPENSSL_cleanse() here deliberately. |
205 | | * Using OPENSSL_cleanse() here could be a performance issue. It |
206 | | * will get properly cleansed on finalisation so this isn't a |
207 | | * security problem. |
208 | | */ |
209 | 4.10M | memset(p, 0, HASH_CBLOCK); /* keep it zeroed */ |
210 | 418M | } else { |
211 | | /* Otherwise just keep filling the buffer */ |
212 | 418M | memcpy(p + n, data, len); |
213 | 418M | c->num += (unsigned int)len; |
214 | 418M | return 1; |
215 | 418M | } |
216 | 423M | } |
217 | | |
218 | 424M | n = len / HASH_CBLOCK; /* Get number of input chunks (e.g. multiple of 512 bits for SHA256) */ |
219 | 424M | if (n > 0) { |
220 | | /* Process chunks */ |
221 | 2.08M | HASH_BLOCK_DATA_ORDER(c, data, n); |
222 | 2.08M | n *= HASH_CBLOCK; |
223 | 2.08M | data += n; |
224 | 2.08M | len -= n; |
225 | 2.08M | } |
226 | | /* Buffer any left over data */ |
227 | 424M | if (len != 0) { |
228 | 423M | p = (unsigned char *)c->data; |
229 | 423M | c->num = (unsigned int)len; |
230 | 423M | memcpy(p, data, len); |
231 | 423M | } |
232 | 424M | return 1; |
233 | 843M | } Line | Count | Source | 168 | 4.07M | { | 169 | | #ifdef HASH_UPDATE_THUNK | 170 | | HASH_CTX *c = (HASH_CTX *)cp; | 171 | | #endif | 172 | 4.07M | const unsigned char *data = data_; | 173 | 4.07M | unsigned char *p; | 174 | 4.07M | HASH_LONG l; | 175 | 4.07M | size_t n; | 176 | | | 177 | 4.07M | if (ossl_unlikely(len == 0)) | 178 | 0 | return 1; | 179 | | | 180 | 4.07M | l = (c->Nl + (((HASH_LONG)len) << 3)) & 0xffffffffUL; | 181 | 4.07M | if (ossl_unlikely(l < c->Nl)) /* overflow */ | 182 | 0 | c->Nh++; | 183 | 4.07M | c->Nh += (HASH_LONG)(len >> 29); /* might cause compiler warning on | 184 | | * 16-bit */ | 185 | 4.07M | c->Nl = l; | 186 | | | 187 | 4.07M | n = c->num; | 188 | 4.07M | if (ossl_likely(n != 0)) { | 189 | | /* Gets here if we already have buffered input data */ | 190 | 1.96M | p = (unsigned char *)c->data; | 191 | | | 192 | 1.96M | if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) { | 193 | | /* | 194 | | * If there is enough input to fill the buffer then fill the | 195 | | * buffer and process a single chunk. | 196 | | */ | 197 | 608k | memcpy(p + n, data, HASH_CBLOCK - n); | 198 | 608k | HASH_BLOCK_DATA_ORDER(c, p, 1); | 199 | 608k | n = HASH_CBLOCK - n; | 200 | 608k | data += n; | 201 | 608k | len -= n; | 202 | 608k | c->num = 0; | 203 | | /* | 204 | | * We use memset rather than OPENSSL_cleanse() here deliberately. | 205 | | * Using OPENSSL_cleanse() here could be a performance issue. It | 206 | | * will get properly cleansed on finalisation so this isn't a | 207 | | * security problem. | 208 | | */ | 209 | 608k | memset(p, 0, HASH_CBLOCK); /* keep it zeroed */ | 210 | 1.36M | } else { | 211 | | /* Otherwise just keep filling the buffer */ | 212 | 1.36M | memcpy(p + n, data, len); | 213 | 1.36M | c->num += (unsigned int)len; | 214 | 1.36M | return 1; | 215 | 1.36M | } | 216 | 1.96M | } | 217 | | | 218 | 2.71M | n = len / HASH_CBLOCK; /* Get number of input chunks (e.g. multiple of 512 bits for SHA256) */ | 219 | 2.71M | if (n > 0) { | 220 | | /* Process chunks */ | 221 | 127k | HASH_BLOCK_DATA_ORDER(c, data, n); | 222 | 127k | n *= HASH_CBLOCK; | 223 | 127k | data += n; | 224 | 127k | len -= n; | 225 | 127k | } | 226 | | /* Buffer any left over data */ | 227 | 2.71M | if (len != 0) { | 228 | 2.50M | p = (unsigned char *)c->data; | 229 | 2.50M | c->num = (unsigned int)len; | 230 | 2.50M | memcpy(p, data, len); | 231 | 2.50M | } | 232 | 2.71M | return 1; | 233 | 4.07M | } |
Line | Count | Source | 168 | 2.38M | { | 169 | | #ifdef HASH_UPDATE_THUNK | 170 | | HASH_CTX *c = (HASH_CTX *)cp; | 171 | | #endif | 172 | 2.38M | const unsigned char *data = data_; | 173 | 2.38M | unsigned char *p; | 174 | 2.38M | HASH_LONG l; | 175 | 2.38M | size_t n; | 176 | | | 177 | 2.38M | if (ossl_unlikely(len == 0)) | 178 | 0 | return 1; | 179 | | | 180 | 2.38M | l = (c->Nl + (((HASH_LONG)len) << 3)) & 0xffffffffUL; | 181 | 2.38M | if (ossl_unlikely(l < c->Nl)) /* overflow */ | 182 | 0 | c->Nh++; | 183 | 2.38M | c->Nh += (HASH_LONG)(len >> 29); /* might cause compiler warning on | 184 | | * 16-bit */ | 185 | 2.38M | c->Nl = l; | 186 | | | 187 | 2.38M | n = c->num; | 188 | 2.38M | if (ossl_likely(n != 0)) { | 189 | | /* Gets here if we already have buffered input data */ | 190 | 1.93M | p = (unsigned char *)c->data; | 191 | | | 192 | 1.93M | if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) { | 193 | | /* | 194 | | * If there is enough input to fill the buffer then fill the | 195 | | * buffer and process a single chunk. | 196 | | */ | 197 | 959k | memcpy(p + n, data, HASH_CBLOCK - n); | 198 | 959k | HASH_BLOCK_DATA_ORDER(c, p, 1); | 199 | 959k | n = HASH_CBLOCK - n; | 200 | 959k | data += n; | 201 | 959k | len -= n; | 202 | 959k | c->num = 0; | 203 | | /* | 204 | | * We use memset rather than OPENSSL_cleanse() here deliberately. | 205 | | * Using OPENSSL_cleanse() here could be a performance issue. It | 206 | | * will get properly cleansed on finalisation so this isn't a | 207 | | * security problem. | 208 | | */ | 209 | 959k | memset(p, 0, HASH_CBLOCK); /* keep it zeroed */ | 210 | 976k | } else { | 211 | | /* Otherwise just keep filling the buffer */ | 212 | 976k | memcpy(p + n, data, len); | 213 | 976k | c->num += (unsigned int)len; | 214 | 976k | return 1; | 215 | 976k | } | 216 | 1.93M | } | 217 | | | 218 | 1.40M | n = len / HASH_CBLOCK; /* Get number of input chunks (e.g. multiple of 512 bits for SHA256) */ | 219 | 1.40M | if (n > 0) { | 220 | | /* Process chunks */ | 221 | 282k | HASH_BLOCK_DATA_ORDER(c, data, n); | 222 | 282k | n *= HASH_CBLOCK; | 223 | 282k | data += n; | 224 | 282k | len -= n; | 225 | 282k | } | 226 | | /* Buffer any left over data */ | 227 | 1.40M | if (len != 0) { | 228 | 1.22M | p = (unsigned char *)c->data; | 229 | 1.22M | c->num = (unsigned int)len; | 230 | 1.22M | memcpy(p, data, len); | 231 | 1.22M | } | 232 | 1.40M | return 1; | 233 | 2.38M | } |
Line | Count | Source | 168 | 4.65M | { | 169 | 4.65M | #ifdef HASH_UPDATE_THUNK | 170 | 4.65M | HASH_CTX *c = (HASH_CTX *)cp; | 171 | 4.65M | #endif | 172 | 4.65M | const unsigned char *data = data_; | 173 | 4.65M | unsigned char *p; | 174 | 4.65M | HASH_LONG l; | 175 | 4.65M | size_t n; | 176 | | | 177 | 4.65M | if (ossl_unlikely(len == 0)) | 178 | 0 | return 1; | 179 | | | 180 | 4.65M | l = (c->Nl + (((HASH_LONG)len) << 3)) & 0xffffffffUL; | 181 | 4.65M | if (ossl_unlikely(l < c->Nl)) /* overflow */ | 182 | 0 | c->Nh++; | 183 | 4.65M | c->Nh += (HASH_LONG)(len >> 29); /* might cause compiler warning on | 184 | | * 16-bit */ | 185 | 4.65M | c->Nl = l; | 186 | | | 187 | 4.65M | n = c->num; | 188 | 4.65M | if (ossl_likely(n != 0)) { | 189 | | /* Gets here if we already have buffered input data */ | 190 | 1.48M | p = (unsigned char *)c->data; | 191 | | | 192 | 1.48M | if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) { | 193 | | /* | 194 | | * If there is enough input to fill the buffer then fill the | 195 | | * buffer and process a single chunk. | 196 | | */ | 197 | 612k | memcpy(p + n, data, HASH_CBLOCK - n); | 198 | 612k | HASH_BLOCK_DATA_ORDER(c, p, 1); | 199 | 612k | n = HASH_CBLOCK - n; | 200 | 612k | data += n; | 201 | 612k | len -= n; | 202 | 612k | c->num = 0; | 203 | | /* | 204 | | * We use memset rather than OPENSSL_cleanse() here deliberately. | 205 | | * Using OPENSSL_cleanse() here could be a performance issue. It | 206 | | * will get properly cleansed on finalisation so this isn't a | 207 | | * security problem. | 208 | | */ | 209 | 612k | memset(p, 0, HASH_CBLOCK); /* keep it zeroed */ | 210 | 874k | } else { | 211 | | /* Otherwise just keep filling the buffer */ | 212 | 874k | memcpy(p + n, data, len); | 213 | 874k | c->num += (unsigned int)len; | 214 | 874k | return 1; | 215 | 874k | } | 216 | 1.48M | } | 217 | | | 218 | 3.78M | n = len / HASH_CBLOCK; /* Get number of input chunks (e.g. multiple of 512 bits for SHA256) */ | 219 | 3.78M | if (n > 0) { | 220 | | /* Process chunks */ | 221 | 360k | HASH_BLOCK_DATA_ORDER(c, data, n); | 222 | 360k | n *= HASH_CBLOCK; | 223 | 360k | data += n; | 224 | 360k | len -= n; | 225 | 360k | } | 226 | | /* Buffer any left over data */ | 227 | 3.78M | if (len != 0) { | 228 | 3.49M | p = (unsigned char *)c->data; | 229 | 3.49M | c->num = (unsigned int)len; | 230 | 3.49M | memcpy(p, data, len); | 231 | 3.49M | } | 232 | 3.78M | return 1; | 233 | 4.65M | } |
Line | Count | Source | 168 | 830M | { | 169 | 830M | #ifdef HASH_UPDATE_THUNK | 170 | 830M | HASH_CTX *c = (HASH_CTX *)cp; | 171 | 830M | #endif | 172 | 830M | const unsigned char *data = data_; | 173 | 830M | unsigned char *p; | 174 | 830M | HASH_LONG l; | 175 | 830M | size_t n; | 176 | | | 177 | 830M | if (ossl_unlikely(len == 0)) | 178 | 0 | return 1; | 179 | | | 180 | 830M | l = (c->Nl + (((HASH_LONG)len) << 3)) & 0xffffffffUL; | 181 | 830M | if (ossl_unlikely(l < c->Nl)) /* overflow */ | 182 | 0 | c->Nh++; | 183 | 830M | c->Nh += (HASH_LONG)(len >> 29); /* might cause compiler warning on | 184 | | * 16-bit */ | 185 | 830M | c->Nl = l; | 186 | | | 187 | 830M | n = c->num; | 188 | 830M | if (ossl_likely(n != 0)) { | 189 | | /* Gets here if we already have buffered input data */ | 190 | 415M | p = (unsigned char *)c->data; | 191 | | | 192 | 415M | if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) { | 193 | | /* | 194 | | * If there is enough input to fill the buffer then fill the | 195 | | * buffer and process a single chunk. | 196 | | */ | 197 | 1.09M | memcpy(p + n, data, HASH_CBLOCK - n); | 198 | 1.09M | HASH_BLOCK_DATA_ORDER(c, p, 1); | 199 | 1.09M | n = HASH_CBLOCK - n; | 200 | 1.09M | data += n; | 201 | 1.09M | len -= n; | 202 | 1.09M | c->num = 0; | 203 | | /* | 204 | | * We use memset rather than OPENSSL_cleanse() here deliberately. | 205 | | * Using OPENSSL_cleanse() here could be a performance issue. It | 206 | | * will get properly cleansed on finalisation so this isn't a | 207 | | * security problem. | 208 | | */ | 209 | 1.09M | memset(p, 0, HASH_CBLOCK); /* keep it zeroed */ | 210 | 414M | } else { | 211 | | /* Otherwise just keep filling the buffer */ | 212 | 414M | memcpy(p + n, data, len); | 213 | 414M | c->num += (unsigned int)len; | 214 | 414M | return 1; | 215 | 414M | } | 216 | 415M | } | 217 | | | 218 | 415M | n = len / HASH_CBLOCK; /* Get number of input chunks (e.g. multiple of 512 bits for SHA256) */ | 219 | 415M | if (n > 0) { | 220 | | /* Process chunks */ | 221 | 1.09M | HASH_BLOCK_DATA_ORDER(c, data, n); | 222 | 1.09M | n *= HASH_CBLOCK; | 223 | 1.09M | data += n; | 224 | 1.09M | len -= n; | 225 | 1.09M | } | 226 | | /* Buffer any left over data */ | 227 | 415M | if (len != 0) { | 228 | 415M | p = (unsigned char *)c->data; | 229 | 415M | c->num = (unsigned int)len; | 230 | 415M | memcpy(p, data, len); | 231 | 415M | } | 232 | 415M | return 1; | 233 | 830M | } |
Line | Count | Source | 168 | 1.86M | { | 169 | | #ifdef HASH_UPDATE_THUNK | 170 | | HASH_CTX *c = (HASH_CTX *)cp; | 171 | | #endif | 172 | 1.86M | const unsigned char *data = data_; | 173 | 1.86M | unsigned char *p; | 174 | 1.86M | HASH_LONG l; | 175 | 1.86M | size_t n; | 176 | | | 177 | 1.86M | if (ossl_unlikely(len == 0)) | 178 | 0 | return 1; | 179 | | | 180 | 1.86M | l = (c->Nl + (((HASH_LONG)len) << 3)) & 0xffffffffUL; | 181 | 1.86M | if (ossl_unlikely(l < c->Nl)) /* overflow */ | 182 | 0 | c->Nh++; | 183 | 1.86M | c->Nh += (HASH_LONG)(len >> 29); /* might cause compiler warning on | 184 | | * 16-bit */ | 185 | 1.86M | c->Nl = l; | 186 | | | 187 | 1.86M | n = c->num; | 188 | 1.86M | if (ossl_likely(n != 0)) { | 189 | | /* Gets here if we already have buffered input data */ | 190 | 1.71M | p = (unsigned char *)c->data; | 191 | | | 192 | 1.71M | if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) { | 193 | | /* | 194 | | * If there is enough input to fill the buffer then fill the | 195 | | * buffer and process a single chunk. | 196 | | */ | 197 | 826k | memcpy(p + n, data, HASH_CBLOCK - n); | 198 | 826k | HASH_BLOCK_DATA_ORDER(c, p, 1); | 199 | 826k | n = HASH_CBLOCK - n; | 200 | 826k | data += n; | 201 | 826k | len -= n; | 202 | 826k | c->num = 0; | 203 | | /* | 204 | | * We use memset rather than OPENSSL_cleanse() here deliberately. | 205 | | * Using OPENSSL_cleanse() here could be a performance issue. It | 206 | | * will get properly cleansed on finalisation so this isn't a | 207 | | * security problem. | 208 | | */ | 209 | 826k | memset(p, 0, HASH_CBLOCK); /* keep it zeroed */ | 210 | 891k | } else { | 211 | | /* Otherwise just keep filling the buffer */ | 212 | 891k | memcpy(p + n, data, len); | 213 | 891k | c->num += (unsigned int)len; | 214 | 891k | return 1; | 215 | 891k | } | 216 | 1.71M | } | 217 | | | 218 | 974k | n = len / HASH_CBLOCK; /* Get number of input chunks (e.g. multiple of 512 bits for SHA256) */ | 219 | 974k | if (n > 0) { | 220 | | /* Process chunks */ | 221 | 217k | HASH_BLOCK_DATA_ORDER(c, data, n); | 222 | 217k | n *= HASH_CBLOCK; | 223 | 217k | data += n; | 224 | 217k | len -= n; | 225 | 217k | } | 226 | | /* Buffer any left over data */ | 227 | 974k | if (len != 0) { | 228 | 832k | p = (unsigned char *)c->data; | 229 | 832k | c->num = (unsigned int)len; | 230 | 832k | memcpy(p, data, len); | 231 | 832k | } | 232 | 974k | return 1; | 233 | 1.86M | } |
|
234 | | |
235 | | void HASH_TRANSFORM(HASH_CTX *c, const unsigned char *data) |
236 | 1.76M | { |
237 | 1.76M | HASH_BLOCK_DATA_ORDER(c, data, 1); /* Process a single chunk */ |
238 | 1.76M | } Unexecuted instantiation: MD5_Transform Unexecuted instantiation: RIPEMD160_Transform Line | Count | Source | 236 | 1.50M | { | 237 | 1.50M | HASH_BLOCK_DATA_ORDER(c, data, 1); /* Process a single chunk */ | 238 | 1.50M | } |
Line | Count | Source | 236 | 262k | { | 237 | 262k | HASH_BLOCK_DATA_ORDER(c, data, 1); /* Process a single chunk */ | 238 | 262k | } |
Unexecuted instantiation: ossl_sm3_transform |
239 | | |
240 | | int HASH_FINAL(unsigned char *md, HASH_CTX *c) |
241 | 843M | { |
242 | 843M | unsigned char *p = (unsigned char *)c->data; |
243 | 843M | size_t n = c->num; |
244 | | |
245 | | /* |
246 | | * Pad the input by adding a 1 bit + K zero bits + input length (L) |
247 | | * as a 64 bit value. K must align the data to a chunk boundary. |
248 | | */ |
249 | 843M | p[n] = 0x80; /* there is always room for one */ |
250 | 843M | n++; |
251 | | |
252 | 843M | if (n > (HASH_CBLOCK - 8)) { |
253 | | /* |
254 | | * If there is not enough room in the buffer to add L, then fill the |
255 | | * current buffer with zeros, and process the chunk |
256 | | */ |
257 | 103k | memset(p + n, 0, HASH_CBLOCK - n); |
258 | 103k | n = 0; |
259 | 103k | HASH_BLOCK_DATA_ORDER(c, p, 1); |
260 | 103k | } |
261 | | /* Add zero padding - but leave enough room for L */ |
262 | 843M | memset(p + n, 0, HASH_CBLOCK - 8 - n); |
263 | | |
264 | | /* Add the 64 bit L value to the end of the buffer */ |
265 | 843M | p += HASH_CBLOCK - 8; |
266 | | #if defined(DATA_ORDER_IS_BIG_ENDIAN) |
267 | 839M | (void)HOST_l2c(c->Nh, p); |
268 | 839M | (void)HOST_l2c(c->Nl, p); |
269 | | #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) |
270 | 3.96M | (void)HOST_l2c(c->Nl, p); |
271 | 3.96M | (void)HOST_l2c(c->Nh, p); |
272 | | #endif |
273 | 843M | p -= HASH_CBLOCK; |
274 | | /* Process the final padded chunk */ |
275 | 843M | HASH_BLOCK_DATA_ORDER(c, p, 1); |
276 | 843M | c->num = 0; |
277 | 843M | OPENSSL_cleanse(p, HASH_CBLOCK); |
278 | | |
279 | | #ifndef HASH_MAKE_STRING |
280 | | #error "HASH_MAKE_STRING must be defined!" |
281 | | #else |
282 | 843M | HASH_MAKE_STRING(c, md); |
283 | 835M | #endif |
284 | | |
285 | 835M | return 1; |
286 | 843M | } Line | Count | Source | 241 | 2.36M | { | 242 | 2.36M | unsigned char *p = (unsigned char *)c->data; | 243 | 2.36M | size_t n = c->num; | 244 | | | 245 | | /* | 246 | | * Pad the input by adding a 1 bit + K zero bits + input length (L) | 247 | | * as a 64 bit value. K must align the data to a chunk boundary. | 248 | | */ | 249 | 2.36M | p[n] = 0x80; /* there is always room for one */ | 250 | 2.36M | n++; | 251 | | | 252 | 2.36M | if (n > (HASH_CBLOCK - 8)) { | 253 | | /* | 254 | | * If there is not enough room in the buffer to add L, then fill the | 255 | | * current buffer with zeros, and process the chunk | 256 | | */ | 257 | 17.4k | memset(p + n, 0, HASH_CBLOCK - n); | 258 | 17.4k | n = 0; | 259 | 17.4k | HASH_BLOCK_DATA_ORDER(c, p, 1); | 260 | 17.4k | } | 261 | | /* Add zero padding - but leave enough room for L */ | 262 | 2.36M | memset(p + n, 0, HASH_CBLOCK - 8 - n); | 263 | | | 264 | | /* Add the 64 bit L value to the end of the buffer */ | 265 | 2.36M | p += HASH_CBLOCK - 8; | 266 | | #if defined(DATA_ORDER_IS_BIG_ENDIAN) | 267 | | (void)HOST_l2c(c->Nh, p); | 268 | | (void)HOST_l2c(c->Nl, p); | 269 | | #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) | 270 | 2.36M | (void)HOST_l2c(c->Nl, p); | 271 | 2.36M | (void)HOST_l2c(c->Nh, p); | 272 | 2.36M | #endif | 273 | 2.36M | p -= HASH_CBLOCK; | 274 | | /* Process the final padded chunk */ | 275 | 2.36M | HASH_BLOCK_DATA_ORDER(c, p, 1); | 276 | 2.36M | c->num = 0; | 277 | 2.36M | OPENSSL_cleanse(p, HASH_CBLOCK); | 278 | | | 279 | | #ifndef HASH_MAKE_STRING | 280 | | #error "HASH_MAKE_STRING must be defined!" | 281 | | #else | 282 | 2.36M | HASH_MAKE_STRING(c, md); | 283 | 2.36M | #endif | 284 | | | 285 | 2.36M | return 1; | 286 | 2.36M | } |
Line | Count | Source | 241 | 1.59M | { | 242 | 1.59M | unsigned char *p = (unsigned char *)c->data; | 243 | 1.59M | size_t n = c->num; | 244 | | | 245 | | /* | 246 | | * Pad the input by adding a 1 bit + K zero bits + input length (L) | 247 | | * as a 64 bit value. K must align the data to a chunk boundary. | 248 | | */ | 249 | 1.59M | p[n] = 0x80; /* there is always room for one */ | 250 | 1.59M | n++; | 251 | | | 252 | 1.59M | if (n > (HASH_CBLOCK - 8)) { | 253 | | /* | 254 | | * If there is not enough room in the buffer to add L, then fill the | 255 | | * current buffer with zeros, and process the chunk | 256 | | */ | 257 | 307 | memset(p + n, 0, HASH_CBLOCK - n); | 258 | 307 | n = 0; | 259 | 307 | HASH_BLOCK_DATA_ORDER(c, p, 1); | 260 | 307 | } | 261 | | /* Add zero padding - but leave enough room for L */ | 262 | 1.59M | memset(p + n, 0, HASH_CBLOCK - 8 - n); | 263 | | | 264 | | /* Add the 64 bit L value to the end of the buffer */ | 265 | 1.59M | p += HASH_CBLOCK - 8; | 266 | | #if defined(DATA_ORDER_IS_BIG_ENDIAN) | 267 | | (void)HOST_l2c(c->Nh, p); | 268 | | (void)HOST_l2c(c->Nl, p); | 269 | | #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) | 270 | 1.59M | (void)HOST_l2c(c->Nl, p); | 271 | 1.59M | (void)HOST_l2c(c->Nh, p); | 272 | 1.59M | #endif | 273 | 1.59M | p -= HASH_CBLOCK; | 274 | | /* Process the final padded chunk */ | 275 | 1.59M | HASH_BLOCK_DATA_ORDER(c, p, 1); | 276 | 1.59M | c->num = 0; | 277 | 1.59M | OPENSSL_cleanse(p, HASH_CBLOCK); | 278 | | | 279 | | #ifndef HASH_MAKE_STRING | 280 | | #error "HASH_MAKE_STRING must be defined!" | 281 | | #else | 282 | 1.59M | HASH_MAKE_STRING(c, md); | 283 | 1.59M | #endif | 284 | | | 285 | 1.59M | return 1; | 286 | 1.59M | } |
Line | Count | Source | 241 | 3.87M | { | 242 | 3.87M | unsigned char *p = (unsigned char *)c->data; | 243 | 3.87M | size_t n = c->num; | 244 | | | 245 | | /* | 246 | | * Pad the input by adding a 1 bit + K zero bits + input length (L) | 247 | | * as a 64 bit value. K must align the data to a chunk boundary. | 248 | | */ | 249 | 3.87M | p[n] = 0x80; /* there is always room for one */ | 250 | 3.87M | n++; | 251 | | | 252 | 3.87M | if (n > (HASH_CBLOCK - 8)) { | 253 | | /* | 254 | | * If there is not enough room in the buffer to add L, then fill the | 255 | | * current buffer with zeros, and process the chunk | 256 | | */ | 257 | 75.2k | memset(p + n, 0, HASH_CBLOCK - n); | 258 | 75.2k | n = 0; | 259 | 75.2k | HASH_BLOCK_DATA_ORDER(c, p, 1); | 260 | 75.2k | } | 261 | | /* Add zero padding - but leave enough room for L */ | 262 | 3.87M | memset(p + n, 0, HASH_CBLOCK - 8 - n); | 263 | | | 264 | | /* Add the 64 bit L value to the end of the buffer */ | 265 | 3.87M | p += HASH_CBLOCK - 8; | 266 | 3.87M | #if defined(DATA_ORDER_IS_BIG_ENDIAN) | 267 | 3.87M | (void)HOST_l2c(c->Nh, p); | 268 | 3.87M | (void)HOST_l2c(c->Nl, p); | 269 | | #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) | 270 | | (void)HOST_l2c(c->Nl, p); | 271 | | (void)HOST_l2c(c->Nh, p); | 272 | | #endif | 273 | 3.87M | p -= HASH_CBLOCK; | 274 | | /* Process the final padded chunk */ | 275 | 3.87M | HASH_BLOCK_DATA_ORDER(c, p, 1); | 276 | 3.87M | c->num = 0; | 277 | 3.87M | OPENSSL_cleanse(p, HASH_CBLOCK); | 278 | | | 279 | | #ifndef HASH_MAKE_STRING | 280 | | #error "HASH_MAKE_STRING must be defined!" | 281 | | #else | 282 | 3.87M | HASH_MAKE_STRING(c, md); | 283 | 3.87M | #endif | 284 | | | 285 | 3.87M | return 1; | 286 | 3.87M | } |
Line | Count | Source | 241 | 835M | { | 242 | 835M | unsigned char *p = (unsigned char *)c->data; | 243 | 835M | size_t n = c->num; | 244 | | | 245 | | /* | 246 | | * Pad the input by adding a 1 bit + K zero bits + input length (L) | 247 | | * as a 64 bit value. K must align the data to a chunk boundary. | 248 | | */ | 249 | 835M | p[n] = 0x80; /* there is always room for one */ | 250 | 835M | n++; | 251 | | | 252 | 835M | if (n > (HASH_CBLOCK - 8)) { | 253 | | /* | 254 | | * If there is not enough room in the buffer to add L, then fill the | 255 | | * current buffer with zeros, and process the chunk | 256 | | */ | 257 | 10.5k | memset(p + n, 0, HASH_CBLOCK - n); | 258 | 10.5k | n = 0; | 259 | 10.5k | HASH_BLOCK_DATA_ORDER(c, p, 1); | 260 | 10.5k | } | 261 | | /* Add zero padding - but leave enough room for L */ | 262 | 835M | memset(p + n, 0, HASH_CBLOCK - 8 - n); | 263 | | | 264 | | /* Add the 64 bit L value to the end of the buffer */ | 265 | 835M | p += HASH_CBLOCK - 8; | 266 | 835M | #if defined(DATA_ORDER_IS_BIG_ENDIAN) | 267 | 835M | (void)HOST_l2c(c->Nh, p); | 268 | 835M | (void)HOST_l2c(c->Nl, p); | 269 | | #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) | 270 | | (void)HOST_l2c(c->Nl, p); | 271 | | (void)HOST_l2c(c->Nh, p); | 272 | | #endif | 273 | 835M | p -= HASH_CBLOCK; | 274 | | /* Process the final padded chunk */ | 275 | 835M | HASH_BLOCK_DATA_ORDER(c, p, 1); | 276 | 835M | c->num = 0; | 277 | 835M | OPENSSL_cleanse(p, HASH_CBLOCK); | 278 | | | 279 | | #ifndef HASH_MAKE_STRING | 280 | | #error "HASH_MAKE_STRING must be defined!" | 281 | | #else | 282 | 835M | HASH_MAKE_STRING(c, md); | 283 | 835M | #endif | 284 | | | 285 | 835M | return 1; | 286 | 835M | } |
Line | Count | Source | 241 | 6.08k | { | 242 | 6.08k | unsigned char *p = (unsigned char *)c->data; | 243 | 6.08k | size_t n = c->num; | 244 | | | 245 | | /* | 246 | | * Pad the input by adding a 1 bit + K zero bits + input length (L) | 247 | | * as a 64 bit value. K must align the data to a chunk boundary. | 248 | | */ | 249 | 6.08k | p[n] = 0x80; /* there is always room for one */ | 250 | 6.08k | n++; | 251 | | | 252 | 6.08k | if (n > (HASH_CBLOCK - 8)) { | 253 | | /* | 254 | | * If there is not enough room in the buffer to add L, then fill the | 255 | | * current buffer with zeros, and process the chunk | 256 | | */ | 257 | 181 | memset(p + n, 0, HASH_CBLOCK - n); | 258 | 181 | n = 0; | 259 | 181 | HASH_BLOCK_DATA_ORDER(c, p, 1); | 260 | 181 | } | 261 | | /* Add zero padding - but leave enough room for L */ | 262 | 6.08k | memset(p + n, 0, HASH_CBLOCK - 8 - n); | 263 | | | 264 | | /* Add the 64 bit L value to the end of the buffer */ | 265 | 6.08k | p += HASH_CBLOCK - 8; | 266 | 6.08k | #if defined(DATA_ORDER_IS_BIG_ENDIAN) | 267 | 6.08k | (void)HOST_l2c(c->Nh, p); | 268 | 6.08k | (void)HOST_l2c(c->Nl, p); | 269 | | #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN) | 270 | | (void)HOST_l2c(c->Nl, p); | 271 | | (void)HOST_l2c(c->Nh, p); | 272 | | #endif | 273 | 6.08k | p -= HASH_CBLOCK; | 274 | | /* Process the final padded chunk */ | 275 | 6.08k | HASH_BLOCK_DATA_ORDER(c, p, 1); | 276 | 6.08k | c->num = 0; | 277 | 6.08k | OPENSSL_cleanse(p, HASH_CBLOCK); | 278 | | | 279 | | #ifndef HASH_MAKE_STRING | 280 | | #error "HASH_MAKE_STRING must be defined!" | 281 | | #else | 282 | 6.08k | HASH_MAKE_STRING(c, md); | 283 | 6.08k | #endif | 284 | | | 285 | 6.08k | return 1; | 286 | 6.08k | } |
|
287 | | |
288 | | #ifndef MD32_REG_T |
289 | | #if defined(__alpha) || defined(__sparcv9) || defined(__mips) |
290 | | #define MD32_REG_T long |
291 | | /* |
292 | | * This comment was originally written for MD5, which is why it |
293 | | * discusses A-D. But it basically applies to all 32-bit digests, |
294 | | * which is why it was moved to common header file. |
295 | | * |
296 | | * In case you wonder why A-D are declared as long and not |
297 | | * as MD5_LONG. Doing so results in slight performance |
298 | | * boost on LP64 architectures. The catch is we don't |
299 | | * really care if 32 MSBs of a 64-bit register get polluted |
300 | | * with eventual overflows as we *save* only 32 LSBs in |
301 | | * *either* case. Now declaring 'em long excuses the compiler |
302 | | * from keeping 32 MSBs zeroed resulting in 13% performance |
303 | | * improvement under SPARC Solaris7/64 and 5% under AlphaLinux. |
304 | | * Well, to be honest it should say that this *prevents* |
305 | | * performance degradation. |
306 | | */ |
307 | | #else |
308 | | /* |
309 | | * Above is not absolute and there are LP64 compilers that |
310 | | * generate better code if MD32_REG_T is defined int. The above |
311 | | * pre-processor condition reflects the circumstances under which |
312 | | * the conclusion was made and is subject to further extension. |
313 | | */ |
314 | | #define MD32_REG_T int |
315 | | #endif |
316 | | #endif |
317 | | |
318 | | #endif |