Coverage Report

Created: 2026-05-24 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/include/crypto/md32_common.h
Line
Count
Source
1
/*
2
 * Copyright 1999-2022 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
#if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
73
#error "DATA_ORDER must be defined!"
74
#endif
75
76
#ifndef HASH_CBLOCK
77
#error "HASH_CBLOCK must be defined!"
78
#endif
79
#ifndef HASH_LONG
80
#error "HASH_LONG must be defined!"
81
#endif
82
#ifndef HASH_CTX
83
#error "HASH_CTX must be defined!"
84
#endif
85
86
#ifndef HASH_UPDATE
87
#error "HASH_UPDATE must be defined!"
88
#endif
89
#ifndef HASH_TRANSFORM
90
#error "HASH_TRANSFORM must be defined!"
91
#endif
92
#ifndef HASH_FINAL
93
#error "HASH_FINAL must be defined!"
94
#endif
95
96
#ifndef HASH_BLOCK_DATA_ORDER
97
#error "HASH_BLOCK_DATA_ORDER must be defined!"
98
#endif
99
100
1.36G
#define ROTATE(a, n) (((a) << (n)) | (((a) & 0xffffffff) >> (32 - (n))))
101
102
#ifndef PEDANTIC
103
#if defined(__GNUC__) && __GNUC__ >= 2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
104
#if defined(__riscv_zbb) || defined(__riscv_zbkb)
105
#if __riscv_xlen == 64
106
#undef ROTATE
107
#define ROTATE(x, n) ({ MD32_REG_T ret;            \
108
                       asm ("roriw %0, %1, %2"        \
109
                       : "=r"(ret)                    \
110
                       : "r"(x), "i"(32 - (n))); ret; })
111
#endif
112
#if __riscv_xlen == 32
113
#undef ROTATE
114
#define ROTATE(x, n) ({ MD32_REG_T ret;            \
115
                       asm ("rori %0, %1, %2"         \
116
                       : "=r"(ret)                    \
117
                       : "r"(x), "i"(32 - (n))); ret; })
118
#endif
119
#endif
120
#endif
121
#endif
122
123
#if defined(DATA_ORDER_IS_BIG_ENDIAN)
124
125
9.55M
#define HOST_c2l(c, l) (l = (((unsigned long)(*((c)++))) << 24), \
126
9.55M
    l |= (((unsigned long)(*((c)++))) << 16),                    \
127
9.55M
    l |= (((unsigned long)(*((c)++))) << 8),                     \
128
9.55M
    l |= (((unsigned long)(*((c)++)))))
129
7.02G
#define HOST_l2c(l, c) (*((c)++) = (unsigned char)(((l) >> 24) & 0xff), \
130
7.02G
    *((c)++) = (unsigned char)(((l) >> 16) & 0xff),                     \
131
7.02G
    *((c)++) = (unsigned char)(((l) >> 8) & 0xff),                      \
132
7.02G
    *((c)++) = (unsigned char)(((l)) & 0xff),                           \
133
7.02G
    l)
134
135
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
136
137
52.1M
#define HOST_c2l(c, l) (l = (((unsigned long)(*((c)++)))), \
138
52.1M
    l |= (((unsigned long)(*((c)++))) << 8),               \
139
52.1M
    l |= (((unsigned long)(*((c)++))) << 16),              \
140
52.1M
    l |= (((unsigned long)(*((c)++))) << 24))
141
24.4M
#define HOST_l2c(l, c) (*((c)++) = (unsigned char)(((l)) & 0xff), \
142
24.4M
    *((c)++) = (unsigned char)(((l) >> 8) & 0xff),                \
143
24.4M
    *((c)++) = (unsigned char)(((l) >> 16) & 0xff),               \
144
24.4M
    *((c)++) = (unsigned char)(((l) >> 24) & 0xff),               \
145
24.4M
    l)
146
147
#endif
148
149
/*
150
 * Time for some action :-)
151
 */
152
153
int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len)
154
873M
{
155
873M
    const unsigned char *data = data_;
156
873M
    unsigned char *p;
157
873M
    HASH_LONG l;
158
873M
    size_t n;
159
160
873M
    if (len == 0)
161
0
        return 1;
162
163
873M
    l = (c->Nl + (((HASH_LONG)len) << 3)) & 0xffffffffUL;
164
873M
    if (l < c->Nl) /* overflow */
165
0
        c->Nh++;
166
873M
    c->Nh += (HASH_LONG)(len >> 29); /* might cause compiler warning on
167
                                      * 16-bit */
168
873M
    c->Nl = l;
169
170
873M
    n = c->num;
171
873M
    if (n != 0) {
172
434M
        p = (unsigned char *)c->data;
173
174
434M
        if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
175
217M
            memcpy(p + n, data, HASH_CBLOCK - n);
176
217M
            HASH_BLOCK_DATA_ORDER(c, p, 1);
177
217M
            n = HASH_CBLOCK - n;
178
217M
            data += n;
179
217M
            len -= n;
180
217M
            c->num = 0;
181
            /*
182
             * We use memset rather than OPENSSL_cleanse() here deliberately.
183
             * Using OPENSSL_cleanse() here could be a performance issue. It
184
             * will get properly cleansed on finalisation so this isn't a
185
             * security problem.
186
             */
187
217M
            memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
188
217M
        } else {
189
217M
            memcpy(p + n, data, len);
190
217M
            c->num += (unsigned int)len;
191
217M
            return 1;
192
217M
        }
193
434M
    }
194
195
656M
    n = len / HASH_CBLOCK;
196
656M
    if (n > 0) {
197
1.69M
        HASH_BLOCK_DATA_ORDER(c, data, n);
198
1.69M
        n *= HASH_CBLOCK;
199
1.69M
        data += n;
200
1.69M
        len -= n;
201
1.69M
    }
202
203
656M
    if (len != 0) {
204
439M
        p = (unsigned char *)c->data;
205
439M
        c->num = (unsigned int)len;
206
439M
        memcpy(p, data, len);
207
439M
    }
208
656M
    return 1;
209
873M
}
Unexecuted instantiation: MD4_Update
MD5_Update
Line
Count
Source
154
788k
{
155
788k
    const unsigned char *data = data_;
156
788k
    unsigned char *p;
157
788k
    HASH_LONG l;
158
788k
    size_t n;
159
160
788k
    if (len == 0)
161
0
        return 1;
162
163
788k
    l = (c->Nl + (((HASH_LONG)len) << 3)) & 0xffffffffUL;
164
788k
    if (l < c->Nl) /* overflow */
165
0
        c->Nh++;
166
788k
    c->Nh += (HASH_LONG)(len >> 29); /* might cause compiler warning on
167
                                      * 16-bit */
168
788k
    c->Nl = l;
169
170
788k
    n = c->num;
171
788k
    if (n != 0) {
172
203k
        p = (unsigned char *)c->data;
173
174
203k
        if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
175
86.0k
            memcpy(p + n, data, HASH_CBLOCK - n);
176
86.0k
            HASH_BLOCK_DATA_ORDER(c, p, 1);
177
86.0k
            n = HASH_CBLOCK - n;
178
86.0k
            data += n;
179
86.0k
            len -= n;
180
86.0k
            c->num = 0;
181
            /*
182
             * We use memset rather than OPENSSL_cleanse() here deliberately.
183
             * Using OPENSSL_cleanse() here could be a performance issue. It
184
             * will get properly cleansed on finalisation so this isn't a
185
             * security problem.
186
             */
187
86.0k
            memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
188
117k
        } else {
189
117k
            memcpy(p + n, data, len);
190
117k
            c->num += (unsigned int)len;
191
117k
            return 1;
192
117k
        }
193
203k
    }
194
195
670k
    n = len / HASH_CBLOCK;
196
670k
    if (n > 0) {
197
78.9k
        HASH_BLOCK_DATA_ORDER(c, data, n);
198
78.9k
        n *= HASH_CBLOCK;
199
78.9k
        data += n;
200
78.9k
        len -= n;
201
78.9k
    }
202
203
670k
    if (len != 0) {
204
622k
        p = (unsigned char *)c->data;
205
622k
        c->num = (unsigned int)len;
206
622k
        memcpy(p, data, len);
207
622k
    }
208
670k
    return 1;
209
788k
}
RIPEMD160_Update
Line
Count
Source
154
1.90M
{
155
1.90M
    const unsigned char *data = data_;
156
1.90M
    unsigned char *p;
157
1.90M
    HASH_LONG l;
158
1.90M
    size_t n;
159
160
1.90M
    if (len == 0)
161
0
        return 1;
162
163
1.90M
    l = (c->Nl + (((HASH_LONG)len) << 3)) & 0xffffffffUL;
164
1.90M
    if (l < c->Nl) /* overflow */
165
0
        c->Nh++;
166
1.90M
    c->Nh += (HASH_LONG)(len >> 29); /* might cause compiler warning on
167
                                      * 16-bit */
168
1.90M
    c->Nl = l;
169
170
1.90M
    n = c->num;
171
1.90M
    if (n != 0) {
172
551
        p = (unsigned char *)c->data;
173
174
551
        if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
175
197
            memcpy(p + n, data, HASH_CBLOCK - n);
176
197
            HASH_BLOCK_DATA_ORDER(c, p, 1);
177
197
            n = HASH_CBLOCK - n;
178
197
            data += n;
179
197
            len -= n;
180
197
            c->num = 0;
181
            /*
182
             * We use memset rather than OPENSSL_cleanse() here deliberately.
183
             * Using OPENSSL_cleanse() here could be a performance issue. It
184
             * will get properly cleansed on finalisation so this isn't a
185
             * security problem.
186
             */
187
197
            memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
188
354
        } else {
189
354
            memcpy(p + n, data, len);
190
354
            c->num += (unsigned int)len;
191
354
            return 1;
192
354
        }
193
551
    }
194
195
1.90M
    n = len / HASH_CBLOCK;
196
1.90M
    if (n > 0) {
197
1.89k
        HASH_BLOCK_DATA_ORDER(c, data, n);
198
1.89k
        n *= HASH_CBLOCK;
199
1.89k
        data += n;
200
1.89k
        len -= n;
201
1.89k
    }
202
203
1.90M
    if (len != 0) {
204
1.90M
        p = (unsigned char *)c->data;
205
1.90M
        c->num = (unsigned int)len;
206
1.90M
        memcpy(p, data, len);
207
1.90M
    }
208
1.90M
    return 1;
209
1.90M
}
SHA1_Update
Line
Count
Source
154
1.59M
{
155
1.59M
    const unsigned char *data = data_;
156
1.59M
    unsigned char *p;
157
1.59M
    HASH_LONG l;
158
1.59M
    size_t n;
159
160
1.59M
    if (len == 0)
161
0
        return 1;
162
163
1.59M
    l = (c->Nl + (((HASH_LONG)len) << 3)) & 0xffffffffUL;
164
1.59M
    if (l < c->Nl) /* overflow */
165
0
        c->Nh++;
166
1.59M
    c->Nh += (HASH_LONG)(len >> 29); /* might cause compiler warning on
167
                                      * 16-bit */
168
1.59M
    c->Nl = l;
169
170
1.59M
    n = c->num;
171
1.59M
    if (n != 0) {
172
168k
        p = (unsigned char *)c->data;
173
174
168k
        if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
175
74.2k
            memcpy(p + n, data, HASH_CBLOCK - n);
176
74.2k
            HASH_BLOCK_DATA_ORDER(c, p, 1);
177
74.2k
            n = HASH_CBLOCK - n;
178
74.2k
            data += n;
179
74.2k
            len -= n;
180
74.2k
            c->num = 0;
181
            /*
182
             * We use memset rather than OPENSSL_cleanse() here deliberately.
183
             * Using OPENSSL_cleanse() here could be a performance issue. It
184
             * will get properly cleansed on finalisation so this isn't a
185
             * security problem.
186
             */
187
74.2k
            memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
188
94.2k
        } else {
189
94.2k
            memcpy(p + n, data, len);
190
94.2k
            c->num += (unsigned int)len;
191
94.2k
            return 1;
192
94.2k
        }
193
168k
    }
194
195
1.50M
    n = len / HASH_CBLOCK;
196
1.50M
    if (n > 0) {
197
476k
        HASH_BLOCK_DATA_ORDER(c, data, n);
198
476k
        n *= HASH_CBLOCK;
199
476k
        data += n;
200
476k
        len -= n;
201
476k
    }
202
203
1.50M
    if (len != 0) {
204
1.32M
        p = (unsigned char *)c->data;
205
1.32M
        c->num = (unsigned int)len;
206
1.32M
        memcpy(p, data, len);
207
1.32M
    }
208
1.50M
    return 1;
209
1.59M
}
SHA256_Update
Line
Count
Source
154
869M
{
155
869M
    const unsigned char *data = data_;
156
869M
    unsigned char *p;
157
869M
    HASH_LONG l;
158
869M
    size_t n;
159
160
869M
    if (len == 0)
161
0
        return 1;
162
163
869M
    l = (c->Nl + (((HASH_LONG)len) << 3)) & 0xffffffffUL;
164
869M
    if (l < c->Nl) /* overflow */
165
0
        c->Nh++;
166
869M
    c->Nh += (HASH_LONG)(len >> 29); /* might cause compiler warning on
167
                                      * 16-bit */
168
869M
    c->Nl = l;
169
170
869M
    n = c->num;
171
869M
    if (n != 0) {
172
433M
        p = (unsigned char *)c->data;
173
174
433M
        if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
175
216M
            memcpy(p + n, data, HASH_CBLOCK - n);
176
216M
            HASH_BLOCK_DATA_ORDER(c, p, 1);
177
216M
            n = HASH_CBLOCK - n;
178
216M
            data += n;
179
216M
            len -= n;
180
216M
            c->num = 0;
181
            /*
182
             * We use memset rather than OPENSSL_cleanse() here deliberately.
183
             * Using OPENSSL_cleanse() here could be a performance issue. It
184
             * will get properly cleansed on finalisation so this isn't a
185
             * security problem.
186
             */
187
216M
            memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
188
216M
        } else {
189
216M
            memcpy(p + n, data, len);
190
216M
            c->num += (unsigned int)len;
191
216M
            return 1;
192
216M
        }
193
433M
    }
194
195
652M
    n = len / HASH_CBLOCK;
196
652M
    if (n > 0) {
197
1.13M
        HASH_BLOCK_DATA_ORDER(c, data, n);
198
1.13M
        n *= HASH_CBLOCK;
199
1.13M
        data += n;
200
1.13M
        len -= n;
201
1.13M
    }
202
203
652M
    if (len != 0) {
204
435M
        p = (unsigned char *)c->data;
205
435M
        c->num = (unsigned int)len;
206
435M
        memcpy(p, data, len);
207
435M
    }
208
652M
    return 1;
209
869M
}
ossl_sm3_update
Line
Count
Source
154
7.63k
{
155
7.63k
    const unsigned char *data = data_;
156
7.63k
    unsigned char *p;
157
7.63k
    HASH_LONG l;
158
7.63k
    size_t n;
159
160
7.63k
    if (len == 0)
161
0
        return 1;
162
163
7.63k
    l = (c->Nl + (((HASH_LONG)len) << 3)) & 0xffffffffUL;
164
7.63k
    if (l < c->Nl) /* overflow */
165
0
        c->Nh++;
166
7.63k
    c->Nh += (HASH_LONG)(len >> 29); /* might cause compiler warning on
167
                                      * 16-bit */
168
7.63k
    c->Nl = l;
169
170
7.63k
    n = c->num;
171
7.63k
    if (n != 0) {
172
529
        p = (unsigned char *)c->data;
173
174
529
        if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
175
199
            memcpy(p + n, data, HASH_CBLOCK - n);
176
199
            HASH_BLOCK_DATA_ORDER(c, p, 1);
177
199
            n = HASH_CBLOCK - n;
178
199
            data += n;
179
199
            len -= n;
180
199
            c->num = 0;
181
            /*
182
             * We use memset rather than OPENSSL_cleanse() here deliberately.
183
             * Using OPENSSL_cleanse() here could be a performance issue. It
184
             * will get properly cleansed on finalisation so this isn't a
185
             * security problem.
186
             */
187
199
            memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
188
330
        } else {
189
330
            memcpy(p + n, data, len);
190
330
            c->num += (unsigned int)len;
191
330
            return 1;
192
330
        }
193
529
    }
194
195
7.30k
    n = len / HASH_CBLOCK;
196
7.30k
    if (n > 0) {
197
3.49k
        HASH_BLOCK_DATA_ORDER(c, data, n);
198
3.49k
        n *= HASH_CBLOCK;
199
3.49k
        data += n;
200
3.49k
        len -= n;
201
3.49k
    }
202
203
7.30k
    if (len != 0) {
204
3.93k
        p = (unsigned char *)c->data;
205
3.93k
        c->num = (unsigned int)len;
206
3.93k
        memcpy(p, data, len);
207
3.93k
    }
208
7.30k
    return 1;
209
7.63k
}
210
211
void HASH_TRANSFORM(HASH_CTX *c, const unsigned char *data)
212
1.66M
{
213
1.66M
    HASH_BLOCK_DATA_ORDER(c, data, 1);
214
1.66M
}
Unexecuted instantiation: MD4_Transform
Unexecuted instantiation: MD5_Transform
Unexecuted instantiation: RIPEMD160_Transform
SHA1_Transform
Line
Count
Source
212
1.33M
{
213
1.33M
    HASH_BLOCK_DATA_ORDER(c, data, 1);
214
1.33M
}
SHA256_Transform
Line
Count
Source
212
326k
{
213
326k
    HASH_BLOCK_DATA_ORDER(c, data, 1);
214
326k
}
Unexecuted instantiation: ossl_sm3_transform
215
216
int HASH_FINAL(unsigned char *md, HASH_CTX *c)
217
761M
{
218
761M
    unsigned char *p = (unsigned char *)c->data;
219
761M
    size_t n = c->num;
220
221
761M
    p[n] = 0x80; /* there is always room for one */
222
761M
    n++;
223
224
761M
    if (n > (HASH_CBLOCK - 8)) {
225
111k
        memset(p + n, 0, HASH_CBLOCK - n);
226
111k
        n = 0;
227
111k
        HASH_BLOCK_DATA_ORDER(c, p, 1);
228
111k
    }
229
761M
    memset(p + n, 0, HASH_CBLOCK - 8 - n);
230
231
761M
    p += HASH_CBLOCK - 8;
232
#if defined(DATA_ORDER_IS_BIG_ENDIAN)
233
758M
    (void)HOST_l2c(c->Nh, p);
234
758M
    (void)HOST_l2c(c->Nl, p);
235
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
236
3.65M
    (void)HOST_l2c(c->Nl, p);
237
3.65M
    (void)HOST_l2c(c->Nh, p);
238
#endif
239
761M
    p -= HASH_CBLOCK;
240
761M
    HASH_BLOCK_DATA_ORDER(c, p, 1);
241
761M
    c->num = 0;
242
761M
    OPENSSL_cleanse(p, HASH_CBLOCK);
243
244
#ifndef HASH_MAKE_STRING
245
#error "HASH_MAKE_STRING must be defined!"
246
#else
247
761M
    HASH_MAKE_STRING(c, md);
248
755M
#endif
249
250
755M
    return 1;
251
761M
}
Unexecuted instantiation: MD4_Final
MD5_Final
Line
Count
Source
217
1.13M
{
218
1.13M
    unsigned char *p = (unsigned char *)c->data;
219
1.13M
    size_t n = c->num;
220
221
1.13M
    p[n] = 0x80; /* there is always room for one */
222
1.13M
    n++;
223
224
1.13M
    if (n > (HASH_CBLOCK - 8)) {
225
19.1k
        memset(p + n, 0, HASH_CBLOCK - n);
226
19.1k
        n = 0;
227
19.1k
        HASH_BLOCK_DATA_ORDER(c, p, 1);
228
19.1k
    }
229
1.13M
    memset(p + n, 0, HASH_CBLOCK - 8 - n);
230
231
1.13M
    p += HASH_CBLOCK - 8;
232
#if defined(DATA_ORDER_IS_BIG_ENDIAN)
233
    (void)HOST_l2c(c->Nh, p);
234
    (void)HOST_l2c(c->Nl, p);
235
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
236
1.13M
    (void)HOST_l2c(c->Nl, p);
237
1.13M
    (void)HOST_l2c(c->Nh, p);
238
1.13M
#endif
239
1.13M
    p -= HASH_CBLOCK;
240
1.13M
    HASH_BLOCK_DATA_ORDER(c, p, 1);
241
1.13M
    c->num = 0;
242
1.13M
    OPENSSL_cleanse(p, HASH_CBLOCK);
243
244
#ifndef HASH_MAKE_STRING
245
#error "HASH_MAKE_STRING must be defined!"
246
#else
247
1.13M
    HASH_MAKE_STRING(c, md);
248
1.13M
#endif
249
250
1.13M
    return 1;
251
1.13M
}
RIPEMD160_Final
Line
Count
Source
217
2.51M
{
218
2.51M
    unsigned char *p = (unsigned char *)c->data;
219
2.51M
    size_t n = c->num;
220
221
2.51M
    p[n] = 0x80; /* there is always room for one */
222
2.51M
    n++;
223
224
2.51M
    if (n > (HASH_CBLOCK - 8)) {
225
221
        memset(p + n, 0, HASH_CBLOCK - n);
226
221
        n = 0;
227
221
        HASH_BLOCK_DATA_ORDER(c, p, 1);
228
221
    }
229
2.51M
    memset(p + n, 0, HASH_CBLOCK - 8 - n);
230
231
2.51M
    p += HASH_CBLOCK - 8;
232
#if defined(DATA_ORDER_IS_BIG_ENDIAN)
233
    (void)HOST_l2c(c->Nh, p);
234
    (void)HOST_l2c(c->Nl, p);
235
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
236
2.51M
    (void)HOST_l2c(c->Nl, p);
237
2.51M
    (void)HOST_l2c(c->Nh, p);
238
2.51M
#endif
239
2.51M
    p -= HASH_CBLOCK;
240
2.51M
    HASH_BLOCK_DATA_ORDER(c, p, 1);
241
2.51M
    c->num = 0;
242
2.51M
    OPENSSL_cleanse(p, HASH_CBLOCK);
243
244
#ifndef HASH_MAKE_STRING
245
#error "HASH_MAKE_STRING must be defined!"
246
#else
247
2.51M
    HASH_MAKE_STRING(c, md);
248
2.51M
#endif
249
250
2.51M
    return 1;
251
2.51M
}
SHA1_Final
Line
Count
Source
217
2.28M
{
218
2.28M
    unsigned char *p = (unsigned char *)c->data;
219
2.28M
    size_t n = c->num;
220
221
2.28M
    p[n] = 0x80; /* there is always room for one */
222
2.28M
    n++;
223
224
2.28M
    if (n > (HASH_CBLOCK - 8)) {
225
84.1k
        memset(p + n, 0, HASH_CBLOCK - n);
226
84.1k
        n = 0;
227
84.1k
        HASH_BLOCK_DATA_ORDER(c, p, 1);
228
84.1k
    }
229
2.28M
    memset(p + n, 0, HASH_CBLOCK - 8 - n);
230
231
2.28M
    p += HASH_CBLOCK - 8;
232
2.28M
#if defined(DATA_ORDER_IS_BIG_ENDIAN)
233
2.28M
    (void)HOST_l2c(c->Nh, p);
234
2.28M
    (void)HOST_l2c(c->Nl, p);
235
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
236
    (void)HOST_l2c(c->Nl, p);
237
    (void)HOST_l2c(c->Nh, p);
238
#endif
239
2.28M
    p -= HASH_CBLOCK;
240
2.28M
    HASH_BLOCK_DATA_ORDER(c, p, 1);
241
2.28M
    c->num = 0;
242
2.28M
    OPENSSL_cleanse(p, HASH_CBLOCK);
243
244
#ifndef HASH_MAKE_STRING
245
#error "HASH_MAKE_STRING must be defined!"
246
#else
247
2.28M
    HASH_MAKE_STRING(c, md);
248
2.28M
#endif
249
250
2.28M
    return 1;
251
2.28M
}
SHA256_Final
Line
Count
Source
217
755M
{
218
755M
    unsigned char *p = (unsigned char *)c->data;
219
755M
    size_t n = c->num;
220
221
755M
    p[n] = 0x80; /* there is always room for one */
222
755M
    n++;
223
224
755M
    if (n > (HASH_CBLOCK - 8)) {
225
8.26k
        memset(p + n, 0, HASH_CBLOCK - n);
226
8.26k
        n = 0;
227
8.26k
        HASH_BLOCK_DATA_ORDER(c, p, 1);
228
8.26k
    }
229
755M
    memset(p + n, 0, HASH_CBLOCK - 8 - n);
230
231
755M
    p += HASH_CBLOCK - 8;
232
755M
#if defined(DATA_ORDER_IS_BIG_ENDIAN)
233
755M
    (void)HOST_l2c(c->Nh, p);
234
755M
    (void)HOST_l2c(c->Nl, p);
235
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
236
    (void)HOST_l2c(c->Nl, p);
237
    (void)HOST_l2c(c->Nh, p);
238
#endif
239
755M
    p -= HASH_CBLOCK;
240
755M
    HASH_BLOCK_DATA_ORDER(c, p, 1);
241
755M
    c->num = 0;
242
755M
    OPENSSL_cleanse(p, HASH_CBLOCK);
243
244
#ifndef HASH_MAKE_STRING
245
#error "HASH_MAKE_STRING must be defined!"
246
#else
247
755M
    HASH_MAKE_STRING(c, md);
248
755M
#endif
249
250
755M
    return 1;
251
755M
}
ossl_sm3_final
Line
Count
Source
217
3.77k
{
218
3.77k
    unsigned char *p = (unsigned char *)c->data;
219
3.77k
    size_t n = c->num;
220
221
3.77k
    p[n] = 0x80; /* there is always room for one */
222
3.77k
    n++;
223
224
3.77k
    if (n > (HASH_CBLOCK - 8)) {
225
59
        memset(p + n, 0, HASH_CBLOCK - n);
226
59
        n = 0;
227
59
        HASH_BLOCK_DATA_ORDER(c, p, 1);
228
59
    }
229
3.77k
    memset(p + n, 0, HASH_CBLOCK - 8 - n);
230
231
3.77k
    p += HASH_CBLOCK - 8;
232
3.77k
#if defined(DATA_ORDER_IS_BIG_ENDIAN)
233
3.77k
    (void)HOST_l2c(c->Nh, p);
234
3.77k
    (void)HOST_l2c(c->Nl, p);
235
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
236
    (void)HOST_l2c(c->Nl, p);
237
    (void)HOST_l2c(c->Nh, p);
238
#endif
239
3.77k
    p -= HASH_CBLOCK;
240
3.77k
    HASH_BLOCK_DATA_ORDER(c, p, 1);
241
3.77k
    c->num = 0;
242
3.77k
    OPENSSL_cleanse(p, HASH_CBLOCK);
243
244
#ifndef HASH_MAKE_STRING
245
#error "HASH_MAKE_STRING must be defined!"
246
#else
247
3.77k
    HASH_MAKE_STRING(c, md);
248
3.77k
#endif
249
250
3.77k
    return 1;
251
3.77k
}
252
253
#ifndef MD32_REG_T
254
#if defined(__alpha) || defined(__sparcv9) || defined(__mips)
255
#define MD32_REG_T long
256
/*
257
 * This comment was originally written for MD5, which is why it
258
 * discusses A-D. But it basically applies to all 32-bit digests,
259
 * which is why it was moved to common header file.
260
 *
261
 * In case you wonder why A-D are declared as long and not
262
 * as MD5_LONG. Doing so results in slight performance
263
 * boost on LP64 architectures. The catch is we don't
264
 * really care if 32 MSBs of a 64-bit register get polluted
265
 * with eventual overflows as we *save* only 32 LSBs in
266
 * *either* case. Now declaring 'em long excuses the compiler
267
 * from keeping 32 MSBs zeroed resulting in 13% performance
268
 * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
269
 * Well, to be honest it should say that this *prevents*
270
 * performance degradation.
271
 */
272
#else
273
/*
274
 * Above is not absolute and there are LP64 compilers that
275
 * generate better code if MD32_REG_T is defined int. The above
276
 * pre-processor condition reflects the circumstances under which
277
 * the conclusion was made and is subject to further extension.
278
 */
279
#define MD32_REG_T int
280
#endif
281
#endif
282
283
#endif