jdk

Форк
0
/
macroAssembler_x86_md5.cpp 
209 строк · 8.1 Кб
1
/*
2
 * Copyright (c) 2020 Microsoft Corporation. All rights reserved.
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
 *
5
 * This code is free software; you can redistribute it and/or modify it
6
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.
8
 *
9
 * This code is distributed in the hope that it will be useful, but WITHOUT
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
 * version 2 for more details (a copy is included in the LICENSE file that
13
 * accompanied this code).
14
 *
15
 * You should have received a copy of the GNU General Public License version
16
 * 2 along with this work; if not, write to the Free Software Foundation,
17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
 * or visit www.oracle.com if you need additional information or have any
21
 * questions.
22
 *
23
 */
24

25
/*
26
 * Copyright (c) 2017 Project Nayuki. (MIT License)
27
 * https://www.nayuki.io/page/fast-md5-hash-implementation-in-x86-assembly
28
 *
29
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
30
 * this software and associated documentation files (the "Software"), to deal in
31
 * the Software without restriction, including without limitation the rights to
32
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
33
 * the Software, and to permit persons to whom the Software is furnished to do so,
34
 * subject to the following conditions:
35
 * - The above copyright notice and this permission notice shall be included in
36
 *   all copies or substantial portions of the Software.
37
 * - The Software is provided "as is", without warranty of any kind, express or
38
 *   implied, including but not limited to the warranties of merchantability,
39
 *   fitness for a particular purpose and noninfringement. In no event shall the
40
 *   authors or copyright holders be liable for any claim, damages or other
41
 *   liability, whether in an action of contract, tort or otherwise, arising from,
42
 *   out of or in connection with the Software or the use or other dealings in the
43
 *   Software.
44
 */
45

46
#include "precompiled.hpp"
47
#include "asm/assembler.hpp"
48
#include "asm/assembler.inline.hpp"
49
#include "runtime/stubRoutines.hpp"
50
#include "macroAssembler_x86.hpp"
51

52
// int com.sun.security.provider.MD5.implCompress0(byte[] b, int ofs)
53
void MacroAssembler::fast_md5(Register buf, Address state, Address ofs, Address limit, bool multi_block) {
54

55
  Label done_hash, loop0;
56

57
  movptr(rdi, state);
58
  movl(rax, Address(rdi,  0));
59
  movl(rbx, Address(rdi,  4));
60
  movl(rcx, Address(rdi,  8));
61
  movl(rdx, Address(rdi, 12));
62

63
  bind(loop0);
64

65
#define FF(r1, r2, r3, r4, k, s, t)              \
66
  addl(r1, t);                                   \
67
  movl(rsi, r3);                                 \
68
  addl(r1, Address(buf, k*4));                   \
69
  xorl(rsi, r4);                                 \
70
  andl(rsi, r2);                                 \
71
  xorl(rsi, r4);                                 \
72
  addl(r1, rsi);                                 \
73
  roll(r1, s);                                   \
74
  addl(r1, r2);
75

76
#define GG(r1, r2, r3, r4, k, s, t)              \
77
  addl(r1, t);                                   \
78
  movl(rsi, r4);                                 \
79
  movl(rdi, r4);                                 \
80
  addl(r1, Address(buf, k*4));                   \
81
  notl(rsi);                                     \
82
  andl(rdi, r2);                                 \
83
  andl(rsi, r3);                                 \
84
  orl(rsi, rdi);                                 \
85
  addl(r1, rsi);                                 \
86
  roll(r1, s);                                   \
87
  addl(r1, r2);
88

89
#define HH(r1, r2, r3, r4, k, s, t)              \
90
  addl(r1, t);                                   \
91
  movl(rsi, r3);                                 \
92
  addl(r1, Address(buf, k*4));                   \
93
  xorl(rsi, r4);                                 \
94
  xorl(rsi, r2);                                 \
95
  addl(r1, rsi);                                 \
96
  roll(r1, s);                                   \
97
  addl(r1, r2);
98

99
#define II(r1, r2, r3, r4, k, s, t)              \
100
  addl(r1, t);                                   \
101
  movl(rsi, r4);                                 \
102
  notl(rsi);                                     \
103
  addl(r1, Address(buf, k*4));                   \
104
  orl(rsi, r2);                                  \
105
  xorl(rsi, r3);                                 \
106
  addl(r1, rsi);                                 \
107
  roll(r1, s);                                   \
108
  addl(r1, r2);
109

110
  // Round 1
111
  FF(rax, rbx, rcx, rdx,  0,  7, 0xd76aa478)
112
  FF(rdx, rax, rbx, rcx,  1, 12, 0xe8c7b756)
113
  FF(rcx, rdx, rax, rbx,  2, 17, 0x242070db)
114
  FF(rbx, rcx, rdx, rax,  3, 22, 0xc1bdceee)
115
  FF(rax, rbx, rcx, rdx,  4,  7, 0xf57c0faf)
116
  FF(rdx, rax, rbx, rcx,  5, 12, 0x4787c62a)
117
  FF(rcx, rdx, rax, rbx,  6, 17, 0xa8304613)
118
  FF(rbx, rcx, rdx, rax,  7, 22, 0xfd469501)
119
  FF(rax, rbx, rcx, rdx,  8,  7, 0x698098d8)
120
  FF(rdx, rax, rbx, rcx,  9, 12, 0x8b44f7af)
121
  FF(rcx, rdx, rax, rbx, 10, 17, 0xffff5bb1)
122
  FF(rbx, rcx, rdx, rax, 11, 22, 0x895cd7be)
123
  FF(rax, rbx, rcx, rdx, 12,  7, 0x6b901122)
124
  FF(rdx, rax, rbx, rcx, 13, 12, 0xfd987193)
125
  FF(rcx, rdx, rax, rbx, 14, 17, 0xa679438e)
126
  FF(rbx, rcx, rdx, rax, 15, 22, 0x49b40821)
127

128
  // Round 2
129
  GG(rax, rbx, rcx, rdx,  1,  5, 0xf61e2562)
130
  GG(rdx, rax, rbx, rcx,  6,  9, 0xc040b340)
131
  GG(rcx, rdx, rax, rbx, 11, 14, 0x265e5a51)
132
  GG(rbx, rcx, rdx, rax,  0, 20, 0xe9b6c7aa)
133
  GG(rax, rbx, rcx, rdx,  5,  5, 0xd62f105d)
134
  GG(rdx, rax, rbx, rcx, 10,  9, 0x02441453)
135
  GG(rcx, rdx, rax, rbx, 15, 14, 0xd8a1e681)
136
  GG(rbx, rcx, rdx, rax,  4, 20, 0xe7d3fbc8)
137
  GG(rax, rbx, rcx, rdx,  9,  5, 0x21e1cde6)
138
  GG(rdx, rax, rbx, rcx, 14,  9, 0xc33707d6)
139
  GG(rcx, rdx, rax, rbx,  3, 14, 0xf4d50d87)
140
  GG(rbx, rcx, rdx, rax,  8, 20, 0x455a14ed)
141
  GG(rax, rbx, rcx, rdx, 13,  5, 0xa9e3e905)
142
  GG(rdx, rax, rbx, rcx,  2,  9, 0xfcefa3f8)
143
  GG(rcx, rdx, rax, rbx,  7, 14, 0x676f02d9)
144
  GG(rbx, rcx, rdx, rax, 12, 20, 0x8d2a4c8a)
145

146
  // Round 3
147
  HH(rax, rbx, rcx, rdx,  5,  4, 0xfffa3942)
148
  HH(rdx, rax, rbx, rcx,  8, 11, 0x8771f681)
149
  HH(rcx, rdx, rax, rbx, 11, 16, 0x6d9d6122)
150
  HH(rbx, rcx, rdx, rax, 14, 23, 0xfde5380c)
151
  HH(rax, rbx, rcx, rdx,  1,  4, 0xa4beea44)
152
  HH(rdx, rax, rbx, rcx,  4, 11, 0x4bdecfa9)
153
  HH(rcx, rdx, rax, rbx,  7, 16, 0xf6bb4b60)
154
  HH(rbx, rcx, rdx, rax, 10, 23, 0xbebfbc70)
155
  HH(rax, rbx, rcx, rdx, 13,  4, 0x289b7ec6)
156
  HH(rdx, rax, rbx, rcx,  0, 11, 0xeaa127fa)
157
  HH(rcx, rdx, rax, rbx,  3, 16, 0xd4ef3085)
158
  HH(rbx, rcx, rdx, rax,  6, 23, 0x04881d05)
159
  HH(rax, rbx, rcx, rdx,  9,  4, 0xd9d4d039)
160
  HH(rdx, rax, rbx, rcx, 12, 11, 0xe6db99e5)
161
  HH(rcx, rdx, rax, rbx, 15, 16, 0x1fa27cf8)
162
  HH(rbx, rcx, rdx, rax,  2, 23, 0xc4ac5665)
163

164
  // Round 4
165
  II(rax, rbx, rcx, rdx,  0,  6, 0xf4292244)
166
  II(rdx, rax, rbx, rcx,  7, 10, 0x432aff97)
167
  II(rcx, rdx, rax, rbx, 14, 15, 0xab9423a7)
168
  II(rbx, rcx, rdx, rax,  5, 21, 0xfc93a039)
169
  II(rax, rbx, rcx, rdx, 12,  6, 0x655b59c3)
170
  II(rdx, rax, rbx, rcx,  3, 10, 0x8f0ccc92)
171
  II(rcx, rdx, rax, rbx, 10, 15, 0xffeff47d)
172
  II(rbx, rcx, rdx, rax,  1, 21, 0x85845dd1)
173
  II(rax, rbx, rcx, rdx,  8,  6, 0x6fa87e4f)
174
  II(rdx, rax, rbx, rcx, 15, 10, 0xfe2ce6e0)
175
  II(rcx, rdx, rax, rbx,  6, 15, 0xa3014314)
176
  II(rbx, rcx, rdx, rax, 13, 21, 0x4e0811a1)
177
  II(rax, rbx, rcx, rdx,  4,  6, 0xf7537e82)
178
  II(rdx, rax, rbx, rcx, 11, 10, 0xbd3af235)
179
  II(rcx, rdx, rax, rbx,  2, 15, 0x2ad7d2bb)
180
  II(rbx, rcx, rdx, rax,  9, 21, 0xeb86d391)
181

182
#undef FF
183
#undef GG
184
#undef HH
185
#undef II
186

187
  // write hash values back in the correct order
188
  movptr(rdi, state);
189
  addl(rax, Address(rdi,  0));
190
  movl(Address(rdi,  0), rax);
191
  addl(rbx, Address(rdi,  4));
192
  movl(Address(rdi,  4), rbx);
193
  addl(rcx, Address(rdi,  8));
194
  movl(Address(rdi,  8), rcx);
195
  addl(rdx, Address(rdi, 12));
196
  movl(Address(rdi, 12), rdx);
197

198
  if (multi_block) {
199
    // increment data pointer and loop if more to process
200
    addptr(buf, 64);
201
    addl(ofs, 64);
202
    movl(rsi, ofs);
203
    cmpl(rsi, limit);
204
    jcc(Assembler::belowEqual, loop0);
205
    movptr(rax, rsi); //return ofs
206
  }
207

208
  bind(done_hash);
209
}
210

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.