]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/md5_mb/md5_ref.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / md5_mb / md5_ref.c
1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
12 distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29
30 #include <stdint.h>
31 #include <string.h>
32
33 ////////////////////////////////////////////////////////////////////////
34 ////////////////////////////////////////////////////////////////////////
35 // Reference MD5 Functions
36 ////////////////////////////////////////////////////////////////////////
37 ////////////////////////////////////////////////////////////////////////
38
39 void md5_single(const uint8_t * data, uint32_t digest[4]);
40
41 #define H0 0x67452301
42 #define H1 0xefcdab89
43 #define H2 0x98badcfe
44 #define H3 0x10325476
45
46 void md5_ref(uint8_t * input_data, uint32_t * digest, uint32_t len)
47 {
48 uint32_t i, j;
49 uint8_t buf[128];
50 union {
51 uint64_t uint;
52 uint8_t uchar[8];
53 } convert;
54 uint8_t *p;
55
56 digest[0] = H0;
57 digest[1] = H1;
58 digest[2] = H2;
59 digest[3] = H3;
60
61 i = len;
62 while (i >= 64) {
63 md5_single(input_data, digest);
64 input_data += 64;
65 i -= 64;
66 }
67 // 0 <= i < 64
68
69 memcpy(buf, input_data, i);
70 buf[i++] = 0x80;
71 for (j = i; j < 120; j++)
72 buf[j] = 0;
73
74 if (i > 64 - 8)
75 i = 128;
76 else
77 i = 64;
78
79 convert.uint = 8 * len;
80 p = buf + i - 8;
81 p[7] = convert.uchar[7];
82 p[6] = convert.uchar[6];
83 p[5] = convert.uchar[5];
84 p[4] = convert.uchar[4];
85 p[3] = convert.uchar[3];
86 p[2] = convert.uchar[2];
87 p[1] = convert.uchar[1];
88 p[0] = convert.uchar[0];
89
90 md5_single(buf, digest);
91 if (i == 128)
92 md5_single(buf + 64, digest);
93 }
94
95 #define F1(b,c,d) (d ^ (b & (c ^ d)))
96 #define F2(b,c,d) (c ^ (d & (b ^ c)))
97 #define F3(b,c,d) (b ^ c ^ d)
98 #define F4(b,c,d) (c ^ (b | ~d))
99
100 #define rol32(x, r) (((x)<<(r)) ^ ((x)>>(32-(r))))
101
102 #define step(i,a,b,c,d,f,k,w,r) \
103 if (i < 16) {f = F1(b,c,d); } else \
104 if (i < 32) {f = F2(b,c,d); } else \
105 if (i < 48) {f = F3(b,c,d); } else \
106 {f = F4(b,c,d); } \
107 f = a + f + k + w; \
108 a = b + rol32(f, r);
109
110 void md5_single(const uint8_t * data, uint32_t digest[4])
111 {
112 uint32_t a, b, c, d;
113 uint32_t f;
114 uint32_t *w = (uint32_t *) data;
115
116 a = digest[0];
117 b = digest[1];
118 c = digest[2];
119 d = digest[3];
120
121 step(0, a, b, c, d, f, 0xd76aa478, w[0], 7);
122 step(1, d, a, b, c, f, 0xe8c7b756, w[1], 12);
123 step(2, c, d, a, b, f, 0x242070db, w[2], 17);
124 step(3, b, c, d, a, f, 0xc1bdceee, w[3], 22);
125 step(4, a, b, c, d, f, 0xf57c0faf, w[4], 7);
126 step(5, d, a, b, c, f, 0x4787c62a, w[5], 12);
127 step(6, c, d, a, b, f, 0xa8304613, w[6], 17);
128 step(7, b, c, d, a, f, 0xfd469501, w[7], 22);
129 step(8, a, b, c, d, f, 0x698098d8, w[8], 7);
130 step(9, d, a, b, c, f, 0x8b44f7af, w[9], 12);
131 step(10, c, d, a, b, f, 0xffff5bb1, w[10], 17);
132 step(11, b, c, d, a, f, 0x895cd7be, w[11], 22);
133 step(12, a, b, c, d, f, 0x6b901122, w[12], 7);
134 step(13, d, a, b, c, f, 0xfd987193, w[13], 12);
135 step(14, c, d, a, b, f, 0xa679438e, w[14], 17);
136 step(15, b, c, d, a, f, 0x49b40821, w[15], 22);
137
138 step(16, a, b, c, d, f, 0xf61e2562, w[1], 5);
139 step(17, d, a, b, c, f, 0xc040b340, w[6], 9);
140 step(18, c, d, a, b, f, 0x265e5a51, w[11], 14);
141 step(19, b, c, d, a, f, 0xe9b6c7aa, w[0], 20);
142 step(20, a, b, c, d, f, 0xd62f105d, w[5], 5);
143 step(21, d, a, b, c, f, 0x02441453, w[10], 9);
144 step(22, c, d, a, b, f, 0xd8a1e681, w[15], 14);
145 step(23, b, c, d, a, f, 0xe7d3fbc8, w[4], 20);
146 step(24, a, b, c, d, f, 0x21e1cde6, w[9], 5);
147 step(25, d, a, b, c, f, 0xc33707d6, w[14], 9);
148 step(26, c, d, a, b, f, 0xf4d50d87, w[3], 14);
149 step(27, b, c, d, a, f, 0x455a14ed, w[8], 20);
150 step(28, a, b, c, d, f, 0xa9e3e905, w[13], 5);
151 step(29, d, a, b, c, f, 0xfcefa3f8, w[2], 9);
152 step(30, c, d, a, b, f, 0x676f02d9, w[7], 14);
153 step(31, b, c, d, a, f, 0x8d2a4c8a, w[12], 20);
154
155 step(32, a, b, c, d, f, 0xfffa3942, w[5], 4);
156 step(33, d, a, b, c, f, 0x8771f681, w[8], 11);
157 step(34, c, d, a, b, f, 0x6d9d6122, w[11], 16);
158 step(35, b, c, d, a, f, 0xfde5380c, w[14], 23);
159 step(36, a, b, c, d, f, 0xa4beea44, w[1], 4);
160 step(37, d, a, b, c, f, 0x4bdecfa9, w[4], 11);
161 step(38, c, d, a, b, f, 0xf6bb4b60, w[7], 16);
162 step(39, b, c, d, a, f, 0xbebfbc70, w[10], 23);
163 step(40, a, b, c, d, f, 0x289b7ec6, w[13], 4);
164 step(41, d, a, b, c, f, 0xeaa127fa, w[0], 11);
165 step(42, c, d, a, b, f, 0xd4ef3085, w[3], 16);
166 step(43, b, c, d, a, f, 0x04881d05, w[6], 23);
167 step(44, a, b, c, d, f, 0xd9d4d039, w[9], 4);
168 step(45, d, a, b, c, f, 0xe6db99e5, w[12], 11);
169 step(46, c, d, a, b, f, 0x1fa27cf8, w[15], 16);
170 step(47, b, c, d, a, f, 0xc4ac5665, w[2], 23);
171
172 step(48, a, b, c, d, f, 0xf4292244, w[0], 6);
173 step(49, d, a, b, c, f, 0x432aff97, w[7], 10);
174 step(50, c, d, a, b, f, 0xab9423a7, w[14], 15);
175 step(51, b, c, d, a, f, 0xfc93a039, w[5], 21);
176 step(52, a, b, c, d, f, 0x655b59c3, w[12], 6);
177 step(53, d, a, b, c, f, 0x8f0ccc92, w[3], 10);
178 step(54, c, d, a, b, f, 0xffeff47d, w[10], 15);
179 step(55, b, c, d, a, f, 0x85845dd1, w[1], 21);
180 step(56, a, b, c, d, f, 0x6fa87e4f, w[8], 6);
181 step(57, d, a, b, c, f, 0xfe2ce6e0, w[15], 10);
182 step(58, c, d, a, b, f, 0xa3014314, w[6], 15);
183 step(59, b, c, d, a, f, 0x4e0811a1, w[13], 21);
184 step(60, a, b, c, d, f, 0xf7537e82, w[4], 6);
185 step(61, d, a, b, c, f, 0xbd3af235, w[11], 10);
186 step(62, c, d, a, b, f, 0x2ad7d2bb, w[2], 15);
187 step(63, b, c, d, a, f, 0xeb86d391, w[9], 21);
188
189 digest[0] += a;
190 digest[1] += b;
191 digest[2] += c;
192 digest[3] += d;
193 }