Coverage Report

Created: 2026-05-22 16:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/md5.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "util/md5.h"
19
20
#include <algorithm>
21
#include <cstring>
22
#include <vector>
23
24
#ifdef __AVX2__
25
#include <immintrin.h>
26
#endif
27
28
#include "exec/common/endian.h"
29
30
namespace doris {
31
32
namespace {
33
34
constexpr uint32_t MD5_A0 = 0x67452301;
35
constexpr uint32_t MD5_B0 = 0xefcdab89;
36
constexpr uint32_t MD5_C0 = 0x98badcfe;
37
constexpr uint32_t MD5_D0 = 0x10325476;
38
constexpr unsigned char MD5_DUMMY_INPUT = 0;
39
40
1.02k
void md5_to_hex(const unsigned char* digest, char* out) {
41
1.02k
    static constexpr char DIGITS[] = "0123456789abcdef";
42
17.4k
    for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
43
16.4k
        *out++ = DIGITS[digest[i] >> 4];
44
16.4k
        *out++ = DIGITS[digest[i] & 0x0F];
45
16.4k
    }
46
1.02k
}
47
48
2.38k
size_t md5_num_blocks(size_t len) {
49
2.38k
    return (len + 9 + 63) / 64;
50
2.38k
}
51
52
1.87k
size_t md5_pad_final_blocks(const unsigned char* data, size_t len, unsigned char* out) {
53
1.87k
    size_t full_blocks = len / 64;
54
1.87k
    size_t tail = len % 64;
55
1.87k
    size_t num_blocks = md5_num_blocks(len);
56
1.87k
    size_t final_count = num_blocks - full_blocks;
57
58
1.87k
    std::memset(out, 0, final_count * 64);
59
1.87k
    std::memcpy(out, data + full_blocks * 64, tail);
60
1.87k
    out[tail] = 0x80;
61
1.87k
    LittleEndian::Store64(out + final_count * 64 - 8, static_cast<uint64_t>(len) * 8);
62
63
1.87k
    return final_count;
64
1.87k
}
65
66
#ifdef __AVX2__
67
68
struct AVX2MD5Ops {
69
    using Vec = __m256i;
70
    static constexpr size_t LANES = 8;
71
72
215k
    static Vec add(Vec a, Vec b) { return _mm256_add_epi32(a, b); }
73
74
27.4k
    static Vec set1(uint32_t v) { return _mm256_set1_epi32(static_cast<int>(v)); }
75
76
13.2k
    static Vec loadu(const void* p) {
77
13.2k
        return _mm256_loadu_si256(reinterpret_cast<const __m256i*>(p));
78
13.2k
    }
79
80
2.06k
    static void storeu(void* p, Vec v) { _mm256_storeu_si256(reinterpret_cast<__m256i*>(p), v); }
81
82
    template <int N>
83
53.1k
    static Vec rotl(Vec x) {
84
53.1k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
53.1k
    }
md5.cpp:_ZN5doris12_GLOBAL__N_110AVX2MD5Ops4rotlILi7EEEDv4_xS3_
Line
Count
Source
83
3.32k
    static Vec rotl(Vec x) {
84
3.32k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
3.32k
    }
md5.cpp:_ZN5doris12_GLOBAL__N_110AVX2MD5Ops4rotlILi12EEEDv4_xS3_
Line
Count
Source
83
3.32k
    static Vec rotl(Vec x) {
84
3.32k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
3.32k
    }
md5.cpp:_ZN5doris12_GLOBAL__N_110AVX2MD5Ops4rotlILi17EEEDv4_xS3_
Line
Count
Source
83
3.32k
    static Vec rotl(Vec x) {
84
3.32k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
3.32k
    }
md5.cpp:_ZN5doris12_GLOBAL__N_110AVX2MD5Ops4rotlILi22EEEDv4_xS3_
Line
Count
Source
83
3.32k
    static Vec rotl(Vec x) {
84
3.32k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
3.32k
    }
md5.cpp:_ZN5doris12_GLOBAL__N_110AVX2MD5Ops4rotlILi5EEEDv4_xS3_
Line
Count
Source
83
3.32k
    static Vec rotl(Vec x) {
84
3.32k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
3.32k
    }
md5.cpp:_ZN5doris12_GLOBAL__N_110AVX2MD5Ops4rotlILi9EEEDv4_xS3_
Line
Count
Source
83
3.32k
    static Vec rotl(Vec x) {
84
3.32k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
3.32k
    }
md5.cpp:_ZN5doris12_GLOBAL__N_110AVX2MD5Ops4rotlILi14EEEDv4_xS3_
Line
Count
Source
83
3.32k
    static Vec rotl(Vec x) {
84
3.32k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
3.32k
    }
md5.cpp:_ZN5doris12_GLOBAL__N_110AVX2MD5Ops4rotlILi20EEEDv4_xS3_
Line
Count
Source
83
3.32k
    static Vec rotl(Vec x) {
84
3.32k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
3.32k
    }
md5.cpp:_ZN5doris12_GLOBAL__N_110AVX2MD5Ops4rotlILi4EEEDv4_xS3_
Line
Count
Source
83
3.32k
    static Vec rotl(Vec x) {
84
3.32k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
3.32k
    }
md5.cpp:_ZN5doris12_GLOBAL__N_110AVX2MD5Ops4rotlILi11EEEDv4_xS3_
Line
Count
Source
83
3.32k
    static Vec rotl(Vec x) {
84
3.32k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
3.32k
    }
md5.cpp:_ZN5doris12_GLOBAL__N_110AVX2MD5Ops4rotlILi16EEEDv4_xS3_
Line
Count
Source
83
3.32k
    static Vec rotl(Vec x) {
84
3.32k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
3.32k
    }
md5.cpp:_ZN5doris12_GLOBAL__N_110AVX2MD5Ops4rotlILi23EEEDv4_xS3_
Line
Count
Source
83
3.32k
    static Vec rotl(Vec x) {
84
3.32k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
3.32k
    }
md5.cpp:_ZN5doris12_GLOBAL__N_110AVX2MD5Ops4rotlILi6EEEDv4_xS3_
Line
Count
Source
83
3.32k
    static Vec rotl(Vec x) {
84
3.32k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
3.32k
    }
md5.cpp:_ZN5doris12_GLOBAL__N_110AVX2MD5Ops4rotlILi10EEEDv4_xS3_
Line
Count
Source
83
3.32k
    static Vec rotl(Vec x) {
84
3.32k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
3.32k
    }
md5.cpp:_ZN5doris12_GLOBAL__N_110AVX2MD5Ops4rotlILi15EEEDv4_xS3_
Line
Count
Source
83
3.32k
    static Vec rotl(Vec x) {
84
3.32k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
3.32k
    }
md5.cpp:_ZN5doris12_GLOBAL__N_110AVX2MD5Ops4rotlILi21EEEDv4_xS3_
Line
Count
Source
83
3.32k
    static Vec rotl(Vec x) {
84
3.32k
        return _mm256_or_si256(_mm256_slli_epi32(x, N), _mm256_srli_epi32(x, 32 - N));
85
3.32k
    }
86
87
13.2k
    static Vec F(Vec b, Vec c, Vec d) {
88
13.2k
        return _mm256_xor_si256(d, _mm256_and_si256(b, _mm256_xor_si256(c, d)));
89
13.2k
    }
90
91
13.2k
    static Vec G(Vec b, Vec c, Vec d) {
92
13.2k
        return _mm256_xor_si256(c, _mm256_and_si256(d, _mm256_xor_si256(b, c)));
93
13.2k
    }
94
95
13.2k
    static Vec H(Vec b, Vec c, Vec d) { return _mm256_xor_si256(b, _mm256_xor_si256(c, d)); }
96
97
13.2k
    static Vec I(Vec b, Vec c, Vec d) {
98
13.2k
        return _mm256_xor_si256(c, _mm256_or_si256(b, _mm256_xor_si256(d, _mm256_set1_epi32(-1))));
99
13.2k
    }
100
101
830
    static void gather_all_message_words(const unsigned char* const block_ptrs[], Vec msg[16]) {
102
2.49k
        for (int half = 0; half < 2; ++half) {
103
1.66k
            size_t off = half * 32;
104
1.66k
            Vec r0 = loadu(block_ptrs[0] + off);
105
1.66k
            Vec r1 = loadu(block_ptrs[1] + off);
106
1.66k
            Vec r2 = loadu(block_ptrs[2] + off);
107
1.66k
            Vec r3 = loadu(block_ptrs[3] + off);
108
1.66k
            Vec r4 = loadu(block_ptrs[4] + off);
109
1.66k
            Vec r5 = loadu(block_ptrs[5] + off);
110
1.66k
            Vec r6 = loadu(block_ptrs[6] + off);
111
1.66k
            Vec r7 = loadu(block_ptrs[7] + off);
112
113
1.66k
            Vec t0 = _mm256_unpacklo_epi32(r0, r1);
114
1.66k
            Vec t1 = _mm256_unpackhi_epi32(r0, r1);
115
1.66k
            Vec t2 = _mm256_unpacklo_epi32(r2, r3);
116
1.66k
            Vec t3 = _mm256_unpackhi_epi32(r2, r3);
117
1.66k
            Vec t4 = _mm256_unpacklo_epi32(r4, r5);
118
1.66k
            Vec t5 = _mm256_unpackhi_epi32(r4, r5);
119
1.66k
            Vec t6 = _mm256_unpacklo_epi32(r6, r7);
120
1.66k
            Vec t7 = _mm256_unpackhi_epi32(r6, r7);
121
122
1.66k
            Vec u0 = _mm256_unpacklo_epi64(t0, t2);
123
1.66k
            Vec u1 = _mm256_unpackhi_epi64(t0, t2);
124
1.66k
            Vec u2 = _mm256_unpacklo_epi64(t1, t3);
125
1.66k
            Vec u3 = _mm256_unpackhi_epi64(t1, t3);
126
1.66k
            Vec u4 = _mm256_unpacklo_epi64(t4, t6);
127
1.66k
            Vec u5 = _mm256_unpackhi_epi64(t4, t6);
128
1.66k
            Vec u6 = _mm256_unpacklo_epi64(t5, t7);
129
1.66k
            Vec u7 = _mm256_unpackhi_epi64(t5, t7);
130
131
1.66k
            size_t base = half * 8;
132
1.66k
            msg[base + 0] = _mm256_permute2x128_si256(u0, u4, 0x20);
133
1.66k
            msg[base + 4] = _mm256_permute2x128_si256(u0, u4, 0x31);
134
1.66k
            msg[base + 1] = _mm256_permute2x128_si256(u1, u5, 0x20);
135
1.66k
            msg[base + 5] = _mm256_permute2x128_si256(u1, u5, 0x31);
136
1.66k
            msg[base + 2] = _mm256_permute2x128_si256(u2, u6, 0x20);
137
1.66k
            msg[base + 6] = _mm256_permute2x128_si256(u2, u6, 0x31);
138
1.66k
            msg[base + 3] = _mm256_permute2x128_si256(u3, u7, 0x20);
139
1.66k
            msg[base + 7] = _mm256_permute2x128_si256(u3, u7, 0x31);
140
1.66k
        }
141
830
    }
142
};
143
144
#define MD5_STEP_X2(func, w1, x1, y1, z1, w2, x2, y2, z2, g, s, ti) \
145
26.5k
    {                                                               \
146
26.5k
        Vec t1 = Ops::func(x1, y1, z1);                             \
147
26.5k
        Vec t2 = Ops::func(x2, y2, z2);                             \
148
26.5k
        t1 = Ops::add(t1, w1);                                      \
149
26.5k
        t2 = Ops::add(t2, w2);                                      \
150
26.5k
        Vec k = Ops::set1(ti);                                      \
151
26.5k
        t1 = Ops::add(t1, k);                                       \
152
26.5k
        t2 = Ops::add(t2, k);                                       \
153
26.5k
        t1 = Ops::add(t1, msg1[g]);                                 \
154
26.5k
        t2 = Ops::add(t2, msg2[g]);                                 \
155
26.5k
        (w1) = Ops::add(x1, Ops::template rotl<s>(t1));             \
156
26.5k
        (w2) = Ops::add(x2, Ops::template rotl<s>(t2));             \
157
26.5k
    }
158
159
template <typename Ops>
160
struct MD5X2State {
161
    typename Ops::Vec a1, b1, c1, d1, a2, b2, c2, d2;
162
};
163
164
template <typename Ops>
165
MD5X2State<Ops> md5_multi_buffer_block_x2(typename Ops::Vec a1, typename Ops::Vec b1,
166
                                          typename Ops::Vec c1, typename Ops::Vec d1,
167
                                          typename Ops::Vec a2, typename Ops::Vec b2,
168
                                          typename Ops::Vec c2, typename Ops::Vec d2,
169
                                          const typename Ops::Vec msg1[16],
170
415
                                          const typename Ops::Vec msg2[16]) {
171
415
    using Vec = typename Ops::Vec;
172
415
    Vec aa1 = a1;
173
415
    Vec bb1 = b1;
174
415
    Vec cc1 = c1;
175
415
    Vec dd1 = d1;
176
415
    Vec aa2 = a2;
177
415
    Vec bb2 = b2;
178
415
    Vec cc2 = c2;
179
415
    Vec dd2 = d2;
180
181
415
    MD5_STEP_X2(F, a1, b1, c1, d1, a2, b2, c2, d2, 0, 7, 0xd76aa478)
182
415
    MD5_STEP_X2(F, d1, a1, b1, c1, d2, a2, b2, c2, 1, 12, 0xe8c7b756)
183
415
    MD5_STEP_X2(F, c1, d1, a1, b1, c2, d2, a2, b2, 2, 17, 0x242070db)
184
415
    MD5_STEP_X2(F, b1, c1, d1, a1, b2, c2, d2, a2, 3, 22, 0xc1bdceee)
185
415
    MD5_STEP_X2(F, a1, b1, c1, d1, a2, b2, c2, d2, 4, 7, 0xf57c0faf)
186
415
    MD5_STEP_X2(F, d1, a1, b1, c1, d2, a2, b2, c2, 5, 12, 0x4787c62a)
187
415
    MD5_STEP_X2(F, c1, d1, a1, b1, c2, d2, a2, b2, 6, 17, 0xa8304613)
188
415
    MD5_STEP_X2(F, b1, c1, d1, a1, b2, c2, d2, a2, 7, 22, 0xfd469501)
189
415
    MD5_STEP_X2(F, a1, b1, c1, d1, a2, b2, c2, d2, 8, 7, 0x698098d8)
190
415
    MD5_STEP_X2(F, d1, a1, b1, c1, d2, a2, b2, c2, 9, 12, 0x8b44f7af)
191
415
    MD5_STEP_X2(F, c1, d1, a1, b1, c2, d2, a2, b2, 10, 17, 0xffff5bb1)
192
415
    MD5_STEP_X2(F, b1, c1, d1, a1, b2, c2, d2, a2, 11, 22, 0x895cd7be)
193
415
    MD5_STEP_X2(F, a1, b1, c1, d1, a2, b2, c2, d2, 12, 7, 0x6b901122)
194
415
    MD5_STEP_X2(F, d1, a1, b1, c1, d2, a2, b2, c2, 13, 12, 0xfd987193)
195
415
    MD5_STEP_X2(F, c1, d1, a1, b1, c2, d2, a2, b2, 14, 17, 0xa679438e)
196
415
    MD5_STEP_X2(F, b1, c1, d1, a1, b2, c2, d2, a2, 15, 22, 0x49b40821)
197
198
415
    MD5_STEP_X2(G, a1, b1, c1, d1, a2, b2, c2, d2, 1, 5, 0xf61e2562)
199
415
    MD5_STEP_X2(G, d1, a1, b1, c1, d2, a2, b2, c2, 6, 9, 0xc040b340)
200
415
    MD5_STEP_X2(G, c1, d1, a1, b1, c2, d2, a2, b2, 11, 14, 0x265e5a51)
201
415
    MD5_STEP_X2(G, b1, c1, d1, a1, b2, c2, d2, a2, 0, 20, 0xe9b6c7aa)
202
415
    MD5_STEP_X2(G, a1, b1, c1, d1, a2, b2, c2, d2, 5, 5, 0xd62f105d)
203
415
    MD5_STEP_X2(G, d1, a1, b1, c1, d2, a2, b2, c2, 10, 9, 0x02441453)
204
415
    MD5_STEP_X2(G, c1, d1, a1, b1, c2, d2, a2, b2, 15, 14, 0xd8a1e681)
205
415
    MD5_STEP_X2(G, b1, c1, d1, a1, b2, c2, d2, a2, 4, 20, 0xe7d3fbc8)
206
415
    MD5_STEP_X2(G, a1, b1, c1, d1, a2, b2, c2, d2, 9, 5, 0x21e1cde6)
207
415
    MD5_STEP_X2(G, d1, a1, b1, c1, d2, a2, b2, c2, 14, 9, 0xc33707d6)
208
415
    MD5_STEP_X2(G, c1, d1, a1, b1, c2, d2, a2, b2, 3, 14, 0xf4d50d87)
209
415
    MD5_STEP_X2(G, b1, c1, d1, a1, b2, c2, d2, a2, 8, 20, 0x455a14ed)
210
415
    MD5_STEP_X2(G, a1, b1, c1, d1, a2, b2, c2, d2, 13, 5, 0xa9e3e905)
211
415
    MD5_STEP_X2(G, d1, a1, b1, c1, d2, a2, b2, c2, 2, 9, 0xfcefa3f8)
212
415
    MD5_STEP_X2(G, c1, d1, a1, b1, c2, d2, a2, b2, 7, 14, 0x676f02d9)
213
415
    MD5_STEP_X2(G, b1, c1, d1, a1, b2, c2, d2, a2, 12, 20, 0x8d2a4c8a)
214
215
415
    MD5_STEP_X2(H, a1, b1, c1, d1, a2, b2, c2, d2, 5, 4, 0xfffa3942)
216
415
    MD5_STEP_X2(H, d1, a1, b1, c1, d2, a2, b2, c2, 8, 11, 0x8771f681)
217
415
    MD5_STEP_X2(H, c1, d1, a1, b1, c2, d2, a2, b2, 11, 16, 0x6d9d6122)
218
415
    MD5_STEP_X2(H, b1, c1, d1, a1, b2, c2, d2, a2, 14, 23, 0xfde5380c)
219
415
    MD5_STEP_X2(H, a1, b1, c1, d1, a2, b2, c2, d2, 1, 4, 0xa4beea44)
220
415
    MD5_STEP_X2(H, d1, a1, b1, c1, d2, a2, b2, c2, 4, 11, 0x4bdecfa9)
221
415
    MD5_STEP_X2(H, c1, d1, a1, b1, c2, d2, a2, b2, 7, 16, 0xf6bb4b60)
222
415
    MD5_STEP_X2(H, b1, c1, d1, a1, b2, c2, d2, a2, 10, 23, 0xbebfbc70)
223
415
    MD5_STEP_X2(H, a1, b1, c1, d1, a2, b2, c2, d2, 13, 4, 0x289b7ec6)
224
415
    MD5_STEP_X2(H, d1, a1, b1, c1, d2, a2, b2, c2, 0, 11, 0xeaa127fa)
225
415
    MD5_STEP_X2(H, c1, d1, a1, b1, c2, d2, a2, b2, 3, 16, 0xd4ef3085)
226
415
    MD5_STEP_X2(H, b1, c1, d1, a1, b2, c2, d2, a2, 6, 23, 0x04881d05)
227
415
    MD5_STEP_X2(H, a1, b1, c1, d1, a2, b2, c2, d2, 9, 4, 0xd9d4d039)
228
415
    MD5_STEP_X2(H, d1, a1, b1, c1, d2, a2, b2, c2, 12, 11, 0xe6db99e5)
229
415
    MD5_STEP_X2(H, c1, d1, a1, b1, c2, d2, a2, b2, 15, 16, 0x1fa27cf8)
230
415
    MD5_STEP_X2(H, b1, c1, d1, a1, b2, c2, d2, a2, 2, 23, 0xc4ac5665)
231
232
415
    MD5_STEP_X2(I, a1, b1, c1, d1, a2, b2, c2, d2, 0, 6, 0xf4292244)
233
415
    MD5_STEP_X2(I, d1, a1, b1, c1, d2, a2, b2, c2, 7, 10, 0x432aff97)
234
415
    MD5_STEP_X2(I, c1, d1, a1, b1, c2, d2, a2, b2, 14, 15, 0xab9423a7)
235
415
    MD5_STEP_X2(I, b1, c1, d1, a1, b2, c2, d2, a2, 5, 21, 0xfc93a039)
236
415
    MD5_STEP_X2(I, a1, b1, c1, d1, a2, b2, c2, d2, 12, 6, 0x655b59c3)
237
415
    MD5_STEP_X2(I, d1, a1, b1, c1, d2, a2, b2, c2, 3, 10, 0x8f0ccc92)
238
415
    MD5_STEP_X2(I, c1, d1, a1, b1, c2, d2, a2, b2, 10, 15, 0xffeff47d)
239
415
    MD5_STEP_X2(I, b1, c1, d1, a1, b2, c2, d2, a2, 1, 21, 0x85845dd1)
240
415
    MD5_STEP_X2(I, a1, b1, c1, d1, a2, b2, c2, d2, 8, 6, 0x6fa87e4f)
241
415
    MD5_STEP_X2(I, d1, a1, b1, c1, d2, a2, b2, c2, 15, 10, 0xfe2ce6e0)
242
415
    MD5_STEP_X2(I, c1, d1, a1, b1, c2, d2, a2, b2, 6, 15, 0xa3014314)
243
415
    MD5_STEP_X2(I, b1, c1, d1, a1, b2, c2, d2, a2, 13, 21, 0x4e0811a1)
244
415
    MD5_STEP_X2(I, a1, b1, c1, d1, a2, b2, c2, d2, 4, 6, 0xf7537e82)
245
415
    MD5_STEP_X2(I, d1, a1, b1, c1, d2, a2, b2, c2, 11, 10, 0xbd3af235)
246
415
    MD5_STEP_X2(I, c1, d1, a1, b1, c2, d2, a2, b2, 2, 15, 0x2ad7d2bb)
247
415
    MD5_STEP_X2(I, b1, c1, d1, a1, b2, c2, d2, a2, 9, 21, 0xeb86d391)
248
249
415
    return {Ops::add(a1, aa1), Ops::add(b1, bb1), Ops::add(c1, cc1), Ops::add(d1, dd1),
250
415
            Ops::add(a2, aa2), Ops::add(b2, bb2), Ops::add(c2, cc2), Ops::add(d2, dd2)};
251
415
}
252
253
#undef MD5_STEP_X2
254
255
template <typename Ops>
256
2.06k
uint32_t extract_lane(typename Ops::Vec v, size_t lane) {
257
2.06k
    alignas(32) uint32_t values[Ops::LANES];
258
2.06k
    Ops::storeu(values, v);
259
2.06k
    return values[lane];
260
2.06k
}
261
262
template <typename Ops>
263
void md5_multi_buffer_compute(const unsigned char* const inputs[], const size_t lengths[],
264
117
                              unsigned char* outputs, size_t count) {
265
117
    constexpr size_t N = Ops::LANES;
266
117
    using Vec = typename Ops::Vec;
267
117
    size_t count1 = std::min(count, N);
268
117
    size_t count2 = count > N ? count - N : 0;
269
270
117
    size_t num_blocks[2 * N];
271
117
    size_t max_blocks = 0;
272
632
    for (size_t i = 0; i < count; ++i) {
273
515
        num_blocks[i] = md5_num_blocks(lengths[i]);
274
515
        max_blocks = std::max(max_blocks, num_blocks[i]);
275
515
    }
276
1.47k
    for (size_t i = count; i < 2 * N; ++i) {
277
1.35k
        num_blocks[i] = 1;
278
1.35k
    }
279
280
117
    alignas(32) unsigned char final_buf[2 * N][128];
281
117
    size_t final_block_start[2 * N];
282
117
    size_t final_block_count[2 * N];
283
632
    for (size_t i = 0; i < count; ++i) {
284
515
        final_block_start[i] = lengths[i] / 64;
285
515
        final_block_count[i] = md5_pad_final_blocks(inputs[i], lengths[i], final_buf[i]);
286
515
    }
287
1.47k
    for (size_t i = count; i < 2 * N; ++i) {
288
1.35k
        final_block_start[i] = 0;
289
1.35k
        final_block_count[i] = md5_pad_final_blocks(&MD5_DUMMY_INPUT, 0, final_buf[i]);
290
1.35k
    }
291
292
117
    Vec a1 = Ops::set1(MD5_A0);
293
117
    Vec b1 = Ops::set1(MD5_B0);
294
117
    Vec c1 = Ops::set1(MD5_C0);
295
117
    Vec d1 = Ops::set1(MD5_D0);
296
117
    Vec a2 = Ops::set1(MD5_A0);
297
117
    Vec b2 = Ops::set1(MD5_B0);
298
117
    Vec c2 = Ops::set1(MD5_C0);
299
117
    Vec d2 = Ops::set1(MD5_D0);
300
301
532
    for (size_t block = 0; block < max_blocks; ++block) {
302
415
        const unsigned char* block_ptrs[2 * N];
303
7.05k
        for (size_t i = 0; i < 2 * N; ++i) {
304
6.64k
            if (block < final_block_start[i]) {
305
539
                block_ptrs[i] = inputs[i] + block * 64;
306
6.10k
            } else {
307
6.10k
                size_t final_index = block - final_block_start[i];
308
6.10k
                block_ptrs[i] = final_index < final_block_count[i] ? final_buf[i] + final_index * 64
309
6.10k
                                                                   : final_buf[i];
310
6.10k
            }
311
6.64k
        }
312
313
415
        Vec msg1[16];
314
415
        Vec msg2[16];
315
415
        Ops::gather_all_message_words(block_ptrs, msg1);
316
415
        Ops::gather_all_message_words(block_ptrs + N, msg2);
317
318
415
        auto st = md5_multi_buffer_block_x2<Ops>(a1, b1, c1, d1, a2, b2, c2, d2, msg1, msg2);
319
415
        a1 = st.a1;
320
415
        b1 = st.b1;
321
415
        c1 = st.c1;
322
415
        d1 = st.d1;
323
415
        a2 = st.a2;
324
415
        b2 = st.b2;
325
415
        c2 = st.c2;
326
415
        d2 = st.d2;
327
328
1.99k
        for (size_t lane = 0; lane < count1; ++lane) {
329
1.58k
            if (block + 1 == num_blocks[lane]) {
330
337
                unsigned char* out = outputs + lane * MD5_DIGEST_LENGTH;
331
337
                LittleEndian::Store32(out, extract_lane<Ops>(a1, lane));
332
337
                LittleEndian::Store32(out + 4, extract_lane<Ops>(b1, lane));
333
337
                LittleEndian::Store32(out + 8, extract_lane<Ops>(c1, lane));
334
337
                LittleEndian::Store32(out + 12, extract_lane<Ops>(d1, lane));
335
337
            }
336
1.58k
        }
337
1.08k
        for (size_t lane = 0; lane < count2; ++lane) {
338
670
            if (block + 1 == num_blocks[N + lane]) {
339
178
                unsigned char* out = outputs + (N + lane) * MD5_DIGEST_LENGTH;
340
178
                LittleEndian::Store32(out, extract_lane<Ops>(a2, lane));
341
178
                LittleEndian::Store32(out + 4, extract_lane<Ops>(b2, lane));
342
178
                LittleEndian::Store32(out + 8, extract_lane<Ops>(c2, lane));
343
178
                LittleEndian::Store32(out + 12, extract_lane<Ops>(d2, lane));
344
178
            }
345
670
        }
346
415
    }
347
117
}
348
349
void md5_binary_batch_avx2(const unsigned char* const inputs[], const size_t lengths[],
350
102
                           unsigned char* outputs, size_t count) {
351
102
    constexpr size_t BATCH = 2 * AVX2MD5Ops::LANES;
352
219
    for (size_t base = 0; base < count; base += BATCH) {
353
117
        size_t batch = std::min(BATCH, count - base);
354
117
        const unsigned char* batch_inputs[BATCH];
355
117
        size_t batch_lengths[BATCH];
356
632
        for (size_t i = 0; i < batch; ++i) {
357
515
            batch_inputs[i] = lengths[base + i] == 0 ? &MD5_DUMMY_INPUT : inputs[base + i];
358
515
            batch_lengths[i] = lengths[base + i];
359
515
        }
360
1.47k
        for (size_t i = batch; i < BATCH; ++i) {
361
1.35k
            batch_inputs[i] = &MD5_DUMMY_INPUT;
362
1.35k
            batch_lengths[i] = 0;
363
1.35k
        }
364
117
        md5_multi_buffer_compute<AVX2MD5Ops>(batch_inputs, batch_lengths,
365
117
                                             outputs + base * MD5_DIGEST_LENGTH, batch);
366
117
    }
367
102
}
368
369
#endif
370
371
} // namespace
372
373
511
Md5Digest::Md5Digest() {
374
511
    MD5_Init(&_md5_ctx);
375
511
}
376
377
602
void Md5Digest::update(const void* data, size_t length) {
378
602
    MD5_Update(&_md5_ctx, data, length);
379
602
}
380
381
511
void Md5Digest::digest() {
382
511
    unsigned char buf[MD5_DIGEST_LENGTH];
383
511
    MD5_Final(buf, &_md5_ctx);
384
385
511
    char hex_buf[MD5_HEX_LENGTH];
386
511
    md5_to_hex(buf, hex_buf);
387
511
    _hex.assign(hex_buf, MD5_HEX_LENGTH);
388
511
}
389
390
void md5_hex_batch(const unsigned char* const inputs[], const size_t lengths[], char* outputs,
391
103
                   size_t count) {
392
103
    if (count == 0) {
393
1
        return;
394
1
    }
395
396
102
#ifdef __AVX2__
397
102
    std::vector<unsigned char> digests(count * MD5_DIGEST_LENGTH);
398
102
    md5_binary_batch_avx2(inputs, lengths, digests.data(), count);
399
617
    for (size_t i = 0; i < count; ++i) {
400
515
        md5_to_hex(digests.data() + i * MD5_DIGEST_LENGTH, outputs + i * MD5_HEX_LENGTH);
401
515
    }
402
#else
403
    for (size_t i = 0; i < count; ++i) {
404
        unsigned char digest[MD5_DIGEST_LENGTH];
405
        MD5(lengths[i] == 0 ? &MD5_DUMMY_INPUT : inputs[i], lengths[i], digest);
406
        md5_to_hex(digest, outputs + i * MD5_HEX_LENGTH);
407
    }
408
#endif
409
102
}
410
411
} // namespace doris