Coverage Report

Created: 2025-06-13 06:58

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