Coverage Report

Created: 2024-07-27 06:39

/src/openssl30/include/crypto/md32_common.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-2018 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
475M
#define ROTATE(a,n)     (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
97
98
#if defined(DATA_ORDER_IS_BIG_ENDIAN)
99
100
1.23M
# define HOST_c2l(c,l)  (l =(((unsigned long)(*((c)++)))<<24),          \
101
1.23M
                         l|=(((unsigned long)(*((c)++)))<<16),          \
102
1.23M
                         l|=(((unsigned long)(*((c)++)))<< 8),          \
103
1.23M
                         l|=(((unsigned long)(*((c)++)))    )           )
104
6.23M
# define HOST_l2c(l,c)  (*((c)++)=(unsigned char)(((l)>>24)&0xff),      \
105
6.23M
                         *((c)++)=(unsigned char)(((l)>>16)&0xff),      \
106
6.23M
                         *((c)++)=(unsigned char)(((l)>> 8)&0xff),      \
107
6.23M
                         *((c)++)=(unsigned char)(((l)    )&0xff),      \
108
6.23M
                         l)
109
110
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
111
112
21.7M
# define HOST_c2l(c,l)  (l =(((unsigned long)(*((c)++)))    ),          \
113
21.7M
                         l|=(((unsigned long)(*((c)++)))<< 8),          \
114
21.7M
                         l|=(((unsigned long)(*((c)++)))<<16),          \
115
21.7M
                         l|=(((unsigned long)(*((c)++)))<<24)           )
116
11.0M
# define HOST_l2c(l,c)  (*((c)++)=(unsigned char)(((l)    )&0xff),      \
117
11.0M
                         *((c)++)=(unsigned char)(((l)>> 8)&0xff),      \
118
11.0M
                         *((c)++)=(unsigned char)(((l)>>16)&0xff),      \
119
11.0M
                         *((c)++)=(unsigned char)(((l)>>24)&0xff),      \
120
11.0M
                         l)
121
122
#endif
123
124
/*
125
 * Time for some action :-)
126
 */
127
128
int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len)
129
4.57M
{
130
4.57M
    const unsigned char *data = data_;
131
4.57M
    unsigned char *p;
132
4.57M
    HASH_LONG l;
133
4.57M
    size_t n;
134
135
4.57M
    if (len == 0)
136
0
        return 1;
137
138
4.57M
    l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL;
139
4.57M
    if (l < c->Nl)              /* overflow */
140
0
        c->Nh++;
141
4.57M
    c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on
142
                                       * 16-bit */
143
4.57M
    c->Nl = l;
144
145
4.57M
    n = c->num;
146
4.57M
    if (n != 0) {
147
757k
        p = (unsigned char *)c->data;
148
149
757k
        if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
150
236k
            memcpy(p + n, data, HASH_CBLOCK - n);
151
236k
            HASH_BLOCK_DATA_ORDER(c, p, 1);
152
236k
            n = HASH_CBLOCK - n;
153
236k
            data += n;
154
236k
            len -= n;
155
236k
            c->num = 0;
156
            /*
157
             * We use memset rather than OPENSSL_cleanse() here deliberately.
158
             * Using OPENSSL_cleanse() here could be a performance issue. It
159
             * will get properly cleansed on finalisation so this isn't a
160
             * security problem.
161
             */
162
236k
            memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
163
521k
        } else {
164
521k
            memcpy(p + n, data, len);
165
521k
            c->num += (unsigned int)len;
166
521k
            return 1;
167
521k
        }
168
757k
    }
169
170
4.05M
    n = len / HASH_CBLOCK;
171
4.05M
    if (n > 0) {
172
915k
        HASH_BLOCK_DATA_ORDER(c, data, n);
173
915k
        n *= HASH_CBLOCK;
174
915k
        data += n;
175
915k
        len -= n;
176
915k
    }
177
178
4.05M
    if (len != 0) {
179
3.42M
        p = (unsigned char *)c->data;
180
3.42M
        c->num = (unsigned int)len;
181
3.42M
        memcpy(p, data, len);
182
3.42M
    }
183
4.05M
    return 1;
184
4.57M
}
Unexecuted instantiation: MD4_Update
MD5_Update
Line
Count
Source
129
463k
{
130
463k
    const unsigned char *data = data_;
131
463k
    unsigned char *p;
132
463k
    HASH_LONG l;
133
463k
    size_t n;
134
135
463k
    if (len == 0)
136
0
        return 1;
137
138
463k
    l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL;
139
463k
    if (l < c->Nl)              /* overflow */
140
0
        c->Nh++;
141
463k
    c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on
142
                                       * 16-bit */
143
463k
    c->Nl = l;
144
145
463k
    n = c->num;
146
463k
    if (n != 0) {
147
153k
        p = (unsigned char *)c->data;
148
149
153k
        if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
150
66.2k
            memcpy(p + n, data, HASH_CBLOCK - n);
151
66.2k
            HASH_BLOCK_DATA_ORDER(c, p, 1);
152
66.2k
            n = HASH_CBLOCK - n;
153
66.2k
            data += n;
154
66.2k
            len -= n;
155
66.2k
            c->num = 0;
156
            /*
157
             * We use memset rather than OPENSSL_cleanse() here deliberately.
158
             * Using OPENSSL_cleanse() here could be a performance issue. It
159
             * will get properly cleansed on finalisation so this isn't a
160
             * security problem.
161
             */
162
66.2k
            memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
163
87.0k
        } else {
164
87.0k
            memcpy(p + n, data, len);
165
87.0k
            c->num += (unsigned int)len;
166
87.0k
            return 1;
167
87.0k
        }
168
153k
    }
169
170
376k
    n = len / HASH_CBLOCK;
171
376k
    if (n > 0) {
172
41.7k
        HASH_BLOCK_DATA_ORDER(c, data, n);
173
41.7k
        n *= HASH_CBLOCK;
174
41.7k
        data += n;
175
41.7k
        len -= n;
176
41.7k
    }
177
178
376k
    if (len != 0) {
179
350k
        p = (unsigned char *)c->data;
180
350k
        c->num = (unsigned int)len;
181
350k
        memcpy(p, data, len);
182
350k
    }
183
376k
    return 1;
184
463k
}
RIPEMD160_Update
Line
Count
Source
129
1.31M
{
130
1.31M
    const unsigned char *data = data_;
131
1.31M
    unsigned char *p;
132
1.31M
    HASH_LONG l;
133
1.31M
    size_t n;
134
135
1.31M
    if (len == 0)
136
0
        return 1;
137
138
1.31M
    l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL;
139
1.31M
    if (l < c->Nl)              /* overflow */
140
0
        c->Nh++;
141
1.31M
    c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on
142
                                       * 16-bit */
143
1.31M
    c->Nl = l;
144
145
1.31M
    n = c->num;
146
1.31M
    if (n != 0) {
147
253
        p = (unsigned char *)c->data;
148
149
253
        if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
150
119
            memcpy(p + n, data, HASH_CBLOCK - n);
151
119
            HASH_BLOCK_DATA_ORDER(c, p, 1);
152
119
            n = HASH_CBLOCK - n;
153
119
            data += n;
154
119
            len -= n;
155
119
            c->num = 0;
156
            /*
157
             * We use memset rather than OPENSSL_cleanse() here deliberately.
158
             * Using OPENSSL_cleanse() here could be a performance issue. It
159
             * will get properly cleansed on finalisation so this isn't a
160
             * security problem.
161
             */
162
119
            memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
163
134
        } else {
164
134
            memcpy(p + n, data, len);
165
134
            c->num += (unsigned int)len;
166
134
            return 1;
167
134
        }
168
253
    }
169
170
1.31M
    n = len / HASH_CBLOCK;
171
1.31M
    if (n > 0) {
172
483
        HASH_BLOCK_DATA_ORDER(c, data, n);
173
483
        n *= HASH_CBLOCK;
174
483
        data += n;
175
483
        len -= n;
176
483
    }
177
178
1.31M
    if (len != 0) {
179
1.31M
        p = (unsigned char *)c->data;
180
1.31M
        c->num = (unsigned int)len;
181
1.31M
        memcpy(p, data, len);
182
1.31M
    }
183
1.31M
    return 1;
184
1.31M
}
SHA1_Update
Line
Count
Source
129
977k
{
130
977k
    const unsigned char *data = data_;
131
977k
    unsigned char *p;
132
977k
    HASH_LONG l;
133
977k
    size_t n;
134
135
977k
    if (len == 0)
136
0
        return 1;
137
138
977k
    l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL;
139
977k
    if (l < c->Nl)              /* overflow */
140
0
        c->Nh++;
141
977k
    c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on
142
                                       * 16-bit */
143
977k
    c->Nl = l;
144
145
977k
    n = c->num;
146
977k
    if (n != 0) {
147
280k
        p = (unsigned char *)c->data;
148
149
280k
        if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
150
104k
            memcpy(p + n, data, HASH_CBLOCK - n);
151
104k
            HASH_BLOCK_DATA_ORDER(c, p, 1);
152
104k
            n = HASH_CBLOCK - n;
153
104k
            data += n;
154
104k
            len -= n;
155
104k
            c->num = 0;
156
            /*
157
             * We use memset rather than OPENSSL_cleanse() here deliberately.
158
             * Using OPENSSL_cleanse() here could be a performance issue. It
159
             * will get properly cleansed on finalisation so this isn't a
160
             * security problem.
161
             */
162
104k
            memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
163
175k
        } else {
164
175k
            memcpy(p + n, data, len);
165
175k
            c->num += (unsigned int)len;
166
175k
            return 1;
167
175k
        }
168
280k
    }
169
170
801k
    n = len / HASH_CBLOCK;
171
801k
    if (n > 0) {
172
271k
        HASH_BLOCK_DATA_ORDER(c, data, n);
173
271k
        n *= HASH_CBLOCK;
174
271k
        data += n;
175
271k
        len -= n;
176
271k
    }
177
178
801k
    if (len != 0) {
179
748k
        p = (unsigned char *)c->data;
180
748k
        c->num = (unsigned int)len;
181
748k
        memcpy(p, data, len);
182
748k
    }
183
801k
    return 1;
184
977k
}
SHA256_Update
Line
Count
Source
129
1.81M
{
130
1.81M
    const unsigned char *data = data_;
131
1.81M
    unsigned char *p;
132
1.81M
    HASH_LONG l;
133
1.81M
    size_t n;
134
135
1.81M
    if (len == 0)
136
0
        return 1;
137
138
1.81M
    l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL;
139
1.81M
    if (l < c->Nl)              /* overflow */
140
0
        c->Nh++;
141
1.81M
    c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on
142
                                       * 16-bit */
143
1.81M
    c->Nl = l;
144
145
1.81M
    n = c->num;
146
1.81M
    if (n != 0) {
147
323k
        p = (unsigned char *)c->data;
148
149
323k
        if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
150
65.1k
            memcpy(p + n, data, HASH_CBLOCK - n);
151
65.1k
            HASH_BLOCK_DATA_ORDER(c, p, 1);
152
65.1k
            n = HASH_CBLOCK - n;
153
65.1k
            data += n;
154
65.1k
            len -= n;
155
65.1k
            c->num = 0;
156
            /*
157
             * We use memset rather than OPENSSL_cleanse() here deliberately.
158
             * Using OPENSSL_cleanse() here could be a performance issue. It
159
             * will get properly cleansed on finalisation so this isn't a
160
             * security problem.
161
             */
162
65.1k
            memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
163
258k
        } else {
164
258k
            memcpy(p + n, data, len);
165
258k
            c->num += (unsigned int)len;
166
258k
            return 1;
167
258k
        }
168
323k
    }
169
170
1.56M
    n = len / HASH_CBLOCK;
171
1.56M
    if (n > 0) {
172
600k
        HASH_BLOCK_DATA_ORDER(c, data, n);
173
600k
        n *= HASH_CBLOCK;
174
600k
        data += n;
175
600k
        len -= n;
176
600k
    }
177
178
1.56M
    if (len != 0) {
179
1.01M
        p = (unsigned char *)c->data;
180
1.01M
        c->num = (unsigned int)len;
181
1.01M
        memcpy(p, data, len);
182
1.01M
    }
183
1.56M
    return 1;
184
1.81M
}
ossl_sm3_update
Line
Count
Source
129
1.67k
{
130
1.67k
    const unsigned char *data = data_;
131
1.67k
    unsigned char *p;
132
1.67k
    HASH_LONG l;
133
1.67k
    size_t n;
134
135
1.67k
    if (len == 0)
136
0
        return 1;
137
138
1.67k
    l = (c->Nl + (((HASH_LONG) len) << 3)) & 0xffffffffUL;
139
1.67k
    if (l < c->Nl)              /* overflow */
140
0
        c->Nh++;
141
1.67k
    c->Nh += (HASH_LONG) (len >> 29); /* might cause compiler warning on
142
                                       * 16-bit */
143
1.67k
    c->Nl = l;
144
145
1.67k
    n = c->num;
146
1.67k
    if (n != 0) {
147
241
        p = (unsigned char *)c->data;
148
149
241
        if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
150
82
            memcpy(p + n, data, HASH_CBLOCK - n);
151
82
            HASH_BLOCK_DATA_ORDER(c, p, 1);
152
82
            n = HASH_CBLOCK - n;
153
82
            data += n;
154
82
            len -= n;
155
82
            c->num = 0;
156
            /*
157
             * We use memset rather than OPENSSL_cleanse() here deliberately.
158
             * Using OPENSSL_cleanse() here could be a performance issue. It
159
             * will get properly cleansed on finalisation so this isn't a
160
             * security problem.
161
             */
162
82
            memset(p, 0, HASH_CBLOCK); /* keep it zeroed */
163
159
        } else {
164
159
            memcpy(p + n, data, len);
165
159
            c->num += (unsigned int)len;
166
159
            return 1;
167
159
        }
168
241
    }
169
170
1.51k
    n = len / HASH_CBLOCK;
171
1.51k
    if (n > 0) {
172
790
        HASH_BLOCK_DATA_ORDER(c, data, n);
173
790
        n *= HASH_CBLOCK;
174
790
        data += n;
175
790
        len -= n;
176
790
    }
177
178
1.51k
    if (len != 0) {
179
828
        p = (unsigned char *)c->data;
180
828
        c->num = (unsigned int)len;
181
828
        memcpy(p, data, len);
182
828
    }
183
1.51k
    return 1;
184
1.67k
}
185
186
void HASH_TRANSFORM(HASH_CTX *c, const unsigned char *data)
187
195k
{
188
195k
    HASH_BLOCK_DATA_ORDER(c, data, 1);
189
195k
}
Unexecuted instantiation: MD4_Transform
Unexecuted instantiation: MD5_Transform
Unexecuted instantiation: RIPEMD160_Transform
SHA1_Transform
Line
Count
Source
187
156k
{
188
156k
    HASH_BLOCK_DATA_ORDER(c, data, 1);
189
156k
}
SHA256_Transform
Line
Count
Source
187
39.3k
{
188
39.3k
    HASH_BLOCK_DATA_ORDER(c, data, 1);
189
39.3k
}
Unexecuted instantiation: ossl_sm3_transform
190
191
int HASH_FINAL(unsigned char *md, HASH_CTX *c)
192
2.44M
{
193
2.44M
    unsigned char *p = (unsigned char *)c->data;
194
2.44M
    size_t n = c->num;
195
196
2.44M
    p[n] = 0x80;                /* there is always room for one */
197
2.44M
    n++;
198
199
2.44M
    if (n > (HASH_CBLOCK - 8)) {
200
64.4k
        memset(p + n, 0, HASH_CBLOCK - n);
201
64.4k
        n = 0;
202
64.4k
        HASH_BLOCK_DATA_ORDER(c, p, 1);
203
64.4k
    }
204
2.44M
    memset(p + n, 0, HASH_CBLOCK - 8 - n);
205
206
2.44M
    p += HASH_CBLOCK - 8;
207
#if   defined(DATA_ORDER_IS_BIG_ENDIAN)
208
827k
    (void)HOST_l2c(c->Nh, p);
209
827k
    (void)HOST_l2c(c->Nl, p);
210
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
211
1.61M
    (void)HOST_l2c(c->Nl, p);
212
1.61M
    (void)HOST_l2c(c->Nh, p);
213
#endif
214
2.44M
    p -= HASH_CBLOCK;
215
2.44M
    HASH_BLOCK_DATA_ORDER(c, p, 1);
216
2.44M
    c->num = 0;
217
2.44M
    OPENSSL_cleanse(p, HASH_CBLOCK);
218
219
#ifndef HASH_MAKE_STRING
220
# error "HASH_MAKE_STRING must be defined!"
221
#else
222
2.44M
    HASH_MAKE_STRING(c, md);
223
156k
#endif
224
225
156k
    return 1;
226
2.44M
}
Unexecuted instantiation: MD4_Final
MD5_Final
Line
Count
Source
192
301k
{
193
301k
    unsigned char *p = (unsigned char *)c->data;
194
301k
    size_t n = c->num;
195
196
301k
    p[n] = 0x80;                /* there is always room for one */
197
301k
    n++;
198
199
301k
    if (n > (HASH_CBLOCK - 8)) {
200
2.98k
        memset(p + n, 0, HASH_CBLOCK - n);
201
2.98k
        n = 0;
202
2.98k
        HASH_BLOCK_DATA_ORDER(c, p, 1);
203
2.98k
    }
204
301k
    memset(p + n, 0, HASH_CBLOCK - 8 - n);
205
206
301k
    p += HASH_CBLOCK - 8;
207
#if   defined(DATA_ORDER_IS_BIG_ENDIAN)
208
    (void)HOST_l2c(c->Nh, p);
209
    (void)HOST_l2c(c->Nl, p);
210
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
211
301k
    (void)HOST_l2c(c->Nl, p);
212
301k
    (void)HOST_l2c(c->Nh, p);
213
301k
#endif
214
301k
    p -= HASH_CBLOCK;
215
301k
    HASH_BLOCK_DATA_ORDER(c, p, 1);
216
301k
    c->num = 0;
217
301k
    OPENSSL_cleanse(p, HASH_CBLOCK);
218
219
#ifndef HASH_MAKE_STRING
220
# error "HASH_MAKE_STRING must be defined!"
221
#else
222
301k
    HASH_MAKE_STRING(c, md);
223
301k
#endif
224
225
301k
    return 1;
226
301k
}
RIPEMD160_Final
Line
Count
Source
192
1.31M
{
193
1.31M
    unsigned char *p = (unsigned char *)c->data;
194
1.31M
    size_t n = c->num;
195
196
1.31M
    p[n] = 0x80;                /* there is always room for one */
197
1.31M
    n++;
198
199
1.31M
    if (n > (HASH_CBLOCK - 8)) {
200
61
        memset(p + n, 0, HASH_CBLOCK - n);
201
61
        n = 0;
202
61
        HASH_BLOCK_DATA_ORDER(c, p, 1);
203
61
    }
204
1.31M
    memset(p + n, 0, HASH_CBLOCK - 8 - n);
205
206
1.31M
    p += HASH_CBLOCK - 8;
207
#if   defined(DATA_ORDER_IS_BIG_ENDIAN)
208
    (void)HOST_l2c(c->Nh, p);
209
    (void)HOST_l2c(c->Nl, p);
210
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
211
1.31M
    (void)HOST_l2c(c->Nl, p);
212
1.31M
    (void)HOST_l2c(c->Nh, p);
213
1.31M
#endif
214
1.31M
    p -= HASH_CBLOCK;
215
1.31M
    HASH_BLOCK_DATA_ORDER(c, p, 1);
216
1.31M
    c->num = 0;
217
1.31M
    OPENSSL_cleanse(p, HASH_CBLOCK);
218
219
#ifndef HASH_MAKE_STRING
220
# error "HASH_MAKE_STRING must be defined!"
221
#else
222
1.31M
    HASH_MAKE_STRING(c, md);
223
1.31M
#endif
224
225
1.31M
    return 1;
226
1.31M
}
SHA1_Final
Line
Count
Source
192
670k
{
193
670k
    unsigned char *p = (unsigned char *)c->data;
194
670k
    size_t n = c->num;
195
196
670k
    p[n] = 0x80;                /* there is always room for one */
197
670k
    n++;
198
199
670k
    if (n > (HASH_CBLOCK - 8)) {
200
60.6k
        memset(p + n, 0, HASH_CBLOCK - n);
201
60.6k
        n = 0;
202
60.6k
        HASH_BLOCK_DATA_ORDER(c, p, 1);
203
60.6k
    }
204
670k
    memset(p + n, 0, HASH_CBLOCK - 8 - n);
205
206
670k
    p += HASH_CBLOCK - 8;
207
670k
#if   defined(DATA_ORDER_IS_BIG_ENDIAN)
208
670k
    (void)HOST_l2c(c->Nh, p);
209
670k
    (void)HOST_l2c(c->Nl, p);
210
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
211
    (void)HOST_l2c(c->Nl, p);
212
    (void)HOST_l2c(c->Nh, p);
213
#endif
214
670k
    p -= HASH_CBLOCK;
215
670k
    HASH_BLOCK_DATA_ORDER(c, p, 1);
216
670k
    c->num = 0;
217
670k
    OPENSSL_cleanse(p, HASH_CBLOCK);
218
219
#ifndef HASH_MAKE_STRING
220
# error "HASH_MAKE_STRING must be defined!"
221
#else
222
670k
    HASH_MAKE_STRING(c, md);
223
670k
#endif
224
225
670k
    return 1;
226
670k
}
SHA256_Final
Line
Count
Source
192
156k
{
193
156k
    unsigned char *p = (unsigned char *)c->data;
194
156k
    size_t n = c->num;
195
196
156k
    p[n] = 0x80;                /* there is always room for one */
197
156k
    n++;
198
199
156k
    if (n > (HASH_CBLOCK - 8)) {
200
697
        memset(p + n, 0, HASH_CBLOCK - n);
201
697
        n = 0;
202
697
        HASH_BLOCK_DATA_ORDER(c, p, 1);
203
697
    }
204
156k
    memset(p + n, 0, HASH_CBLOCK - 8 - n);
205
206
156k
    p += HASH_CBLOCK - 8;
207
156k
#if   defined(DATA_ORDER_IS_BIG_ENDIAN)
208
156k
    (void)HOST_l2c(c->Nh, p);
209
156k
    (void)HOST_l2c(c->Nl, p);
210
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
211
    (void)HOST_l2c(c->Nl, p);
212
    (void)HOST_l2c(c->Nh, p);
213
#endif
214
156k
    p -= HASH_CBLOCK;
215
156k
    HASH_BLOCK_DATA_ORDER(c, p, 1);
216
156k
    c->num = 0;
217
156k
    OPENSSL_cleanse(p, HASH_CBLOCK);
218
219
#ifndef HASH_MAKE_STRING
220
# error "HASH_MAKE_STRING must be defined!"
221
#else
222
156k
    HASH_MAKE_STRING(c, md);
223
156k
#endif
224
225
156k
    return 1;
226
156k
}
ossl_sm3_final
Line
Count
Source
192
775
{
193
775
    unsigned char *p = (unsigned char *)c->data;
194
775
    size_t n = c->num;
195
196
775
    p[n] = 0x80;                /* there is always room for one */
197
775
    n++;
198
199
775
    if (n > (HASH_CBLOCK - 8)) {
200
49
        memset(p + n, 0, HASH_CBLOCK - n);
201
49
        n = 0;
202
49
        HASH_BLOCK_DATA_ORDER(c, p, 1);
203
49
    }
204
775
    memset(p + n, 0, HASH_CBLOCK - 8 - n);
205
206
775
    p += HASH_CBLOCK - 8;
207
775
#if   defined(DATA_ORDER_IS_BIG_ENDIAN)
208
775
    (void)HOST_l2c(c->Nh, p);
209
775
    (void)HOST_l2c(c->Nl, p);
210
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
211
    (void)HOST_l2c(c->Nl, p);
212
    (void)HOST_l2c(c->Nh, p);
213
#endif
214
775
    p -= HASH_CBLOCK;
215
775
    HASH_BLOCK_DATA_ORDER(c, p, 1);
216
775
    c->num = 0;
217
775
    OPENSSL_cleanse(p, HASH_CBLOCK);
218
219
#ifndef HASH_MAKE_STRING
220
# error "HASH_MAKE_STRING must be defined!"
221
#else
222
775
    HASH_MAKE_STRING(c, md);
223
775
#endif
224
225
775
    return 1;
226
775
}
227
228
#ifndef MD32_REG_T
229
# if defined(__alpha) || defined(__sparcv9) || defined(__mips)
230
#  define MD32_REG_T long
231
/*
232
 * This comment was originally written for MD5, which is why it
233
 * discusses A-D. But it basically applies to all 32-bit digests,
234
 * which is why it was moved to common header file.
235
 *
236
 * In case you wonder why A-D are declared as long and not
237
 * as MD5_LONG. Doing so results in slight performance
238
 * boost on LP64 architectures. The catch is we don't
239
 * really care if 32 MSBs of a 64-bit register get polluted
240
 * with eventual overflows as we *save* only 32 LSBs in
241
 * *either* case. Now declaring 'em long excuses the compiler
242
 * from keeping 32 MSBs zeroed resulting in 13% performance
243
 * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
244
 * Well, to be honest it should say that this *prevents*
245
 * performance degradation.
246
 */
247
# else
248
/*
249
 * Above is not absolute and there are LP64 compilers that
250
 * generate better code if MD32_REG_T is defined int. The above
251
 * pre-processor condition reflects the circumstances under which
252
 * the conclusion was made and is subject to further extension.
253
 */
254
#  define MD32_REG_T int
255
# endif
256
#endif