Skip to content

Commit 622d0a6

Browse files
committed
MSVC x64 support
Hashes implented in MASM inspired by Project Nayuki Requires Build Tools for Visual Studio to cargo build https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools&rel=17 Fixes RustCrypto#17
1 parent 1ea2ff7 commit 622d0a6

File tree

8 files changed

+655
-7
lines changed

8 files changed

+655
-7
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ For more information, see [#45].
2121
All crates are tested on the following platforms:
2222

2323
- Linux (32-bit and 64-bit x86)
24-
- Windows (64-bit x86, GNU only)
24+
- Windows (64-bit x86)
2525
- ARM64 (except `md5`, which is x86 only)
2626

27-
Windows MSVC builds are known to be broken. See [#17].
28-
2927
## Minimum Supported Rust Version
3028

3129
All crates in this repository support **Rust 1.43** or higher.

md5/build.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
fn main() {
22
let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
3+
let target_family = std::env::var("CARGO_CFG_TARGET_FAMILY").unwrap_or_default();
34

45
let asm_path = if target_arch == "x86" {
56
"src/x86.S"
6-
} else if target_arch == "x86_64" {
7+
} else if target_arch == "x86_64" && target_family == "unix" {
78
"src/x64.S"
9+
} else if target_arch == "x86_64" && target_family == "windows" {
10+
"src/x64_masm.asm"
811
} else {
912
panic!("Unsupported target architecture");
1013
};

md5/src/x64_masm.asm

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
;
2+
; MD5 hash in x64 MASM
3+
;
4+
; Copyright (c) 2023 Chong Yeol Nah (MIT License)
5+
;
6+
; Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
; this software and associated documentation files (the "Software"), to deal in
8+
; the Software without restriction, including without limitation the rights to
9+
; use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
; the Software, and to permit persons to whom the Software is furnished to do so,
11+
; subject to the following conditions:
12+
; - The above copyright notice and this permission notice shall be included in
13+
; all copies or substantial portions of the Software.
14+
; - The Software is provided "as is", without warranty of any kind, express or
15+
; implied, including but not limited to the warranties of merchantability,
16+
; fitness for a particular purpose and noninfringement. In no event shall the
17+
; authors or copyright holders be liable for any claim, damages or other
18+
; liability, whether in an action of contract, tort or otherwise, arising from,
19+
; out of or in connection with the Software or the use or other dealings in the
20+
; Software.
21+
;
22+
;
23+
; Storage usage:
24+
; Bytes Location Volatile Description
25+
; 4 eax yes Temporary w-bit word used in the hash computation
26+
; 8 rcx yes Base address of message block array argument (read-only)
27+
; 8 rdx yes Base address of hash value array argument (read-only)
28+
; 4 r8d yes MD5 working variable A
29+
; 4 r9d yes MD5 working variable B
30+
; 4 r10d yes MD5 working variable C
31+
; 4 r11d yes MD5 working variable D
32+
33+
option casemap:none
34+
35+
.const
36+
ROUND macro i, a, b, c, d, k, s, t
37+
38+
if i LT 16
39+
40+
; eax = F(b,c,d) = (b & c) | (!b & d) = d ^ (b & (c ^ d))
41+
mov eax, c
42+
xor eax, d
43+
and eax, b
44+
xor eax, d
45+
46+
elseif i LT 32
47+
48+
; eax = G(b,c,d) = (b & d) | (c & !d) = c ^ (d & (b ^ c))
49+
mov eax, c
50+
xor eax, b
51+
and eax, d
52+
xor eax, c
53+
54+
elseif i LT 48
55+
56+
; eax = H(b,c,d) = b ^ c ^ d
57+
mov eax, c
58+
xor eax, d
59+
xor eax, b
60+
61+
else
62+
63+
; eax = I(b,c,d) = c ^ (b | !d)
64+
mov eax, d
65+
not eax
66+
or eax, b
67+
xor eax, c
68+
69+
endif
70+
71+
lea a, [eax + a + t]
72+
add a, [rcx + k*4]
73+
rol a, s
74+
add a, b
75+
endm
76+
77+
.code
78+
; void md5_compress(const uint8_t block[64], uint32_t state[4])
79+
public md5_compress
80+
md5_compress proc
81+
; Initialize working variables with previous hash value
82+
mov r8d, [rdx] ; a
83+
mov r9d, [rdx + 4] ; b
84+
mov r10d, [rdx + 8] ; c
85+
mov r11d, [rdx + 12] ; d
86+
87+
; 64 rounds of hashing
88+
ROUND 0, r8d, r9d, r10d, r11d, 0, 7, -28955B88h
89+
ROUND 1, r11d, r8d, r9d, r10d, 1, 12, -173848AAh
90+
ROUND 2, r10d, r11d, r8d, r9d, 2, 17, 242070DBh
91+
ROUND 3, r9d, r10d, r11d, r8d, 3, 22, -3E423112h
92+
ROUND 4, r8d, r9d, r10d, r11d, 4, 7, -0A83F051h
93+
ROUND 5, r11d, r8d, r9d, r10d, 5, 12, 4787C62Ah
94+
ROUND 6, r10d, r11d, r8d, r9d, 6, 17, -57CFB9EDh
95+
ROUND 7, r9d, r10d, r11d, r8d, 7, 22, -02B96AFFh
96+
ROUND 8, r8d, r9d, r10d, r11d, 8, 7, 698098D8h
97+
ROUND 9, r11d, r8d, r9d, r10d, 9, 12, -74BB0851h
98+
ROUND 10, r10d, r11d, r8d, r9d, 10, 17, -0000A44Fh
99+
ROUND 11, r9d, r10d, r11d, r8d, 11, 22, -76A32842h
100+
ROUND 12, r8d, r9d, r10d, r11d, 12, 7, 6B901122h
101+
ROUND 13, r11d, r8d, r9d, r10d, 13, 12, -02678E6Dh
102+
ROUND 14, r10d, r11d, r8d, r9d, 14, 17, -5986BC72h
103+
ROUND 15, r9d, r10d, r11d, r8d, 15, 22, 49B40821h
104+
ROUND 16, r8d, r9d, r10d, r11d, 1, 5, -09E1DA9Eh
105+
ROUND 17, r11d, r8d, r9d, r10d, 6, 9, -3FBF4CC0h
106+
ROUND 18, r10d, r11d, r8d, r9d, 11, 14, 265E5A51h
107+
ROUND 19, r9d, r10d, r11d, r8d, 0, 20, -16493856h
108+
ROUND 20, r8d, r9d, r10d, r11d, 5, 5, -29D0EFA3h
109+
ROUND 21, r11d, r8d, r9d, r10d, 10, 9, 02441453h
110+
ROUND 22, r10d, r11d, r8d, r9d, 15, 14, -275E197Fh
111+
ROUND 23, r9d, r10d, r11d, r8d, 4, 20, -182C0438h
112+
ROUND 24, r8d, r9d, r10d, r11d, 9, 5, 21E1CDE6h
113+
ROUND 25, r11d, r8d, r9d, r10d, 14, 9, -3CC8F82Ah
114+
ROUND 26, r10d, r11d, r8d, r9d, 3, 14, -0B2AF279h
115+
ROUND 27, r9d, r10d, r11d, r8d, 8, 20, 455A14EDh
116+
ROUND 28, r8d, r9d, r10d, r11d, 13, 5, -561C16FBh
117+
ROUND 29, r11d, r8d, r9d, r10d, 2, 9, -03105C08h
118+
ROUND 30, r10d, r11d, r8d, r9d, 7, 14, 676F02D9h
119+
ROUND 31, r9d, r10d, r11d, r8d, 12, 20, -72D5B376h
120+
ROUND 32, r8d, r9d, r10d, r11d, 5, 4, -0005C6BEh
121+
ROUND 33, r11d, r8d, r9d, r10d, 8, 11, -788E097Fh
122+
ROUND 34, r10d, r11d, r8d, r9d, 11, 16, 6D9D6122h
123+
ROUND 35, r9d, r10d, r11d, r8d, 14, 23, -021AC7F4h
124+
ROUND 36, r8d, r9d, r10d, r11d, 1, 4, -5B4115BCh
125+
ROUND 37, r11d, r8d, r9d, r10d, 4, 11, 4BDECFA9h
126+
ROUND 38, r10d, r11d, r8d, r9d, 7, 16, -0944B4A0h
127+
ROUND 39, r9d, r10d, r11d, r8d, 10, 23, -41404390h
128+
ROUND 40, r8d, r9d, r10d, r11d, 13, 4, 289B7EC6h
129+
ROUND 41, r11d, r8d, r9d, r10d, 0, 11, -155ED806h
130+
ROUND 42, r10d, r11d, r8d, r9d, 3, 16, -2B10CF7Bh
131+
ROUND 43, r9d, r10d, r11d, r8d, 6, 23, 04881D05h
132+
ROUND 44, r8d, r9d, r10d, r11d, 9, 4, -262B2FC7h
133+
ROUND 45, r11d, r8d, r9d, r10d, 12, 11, -1924661Bh
134+
ROUND 46, r10d, r11d, r8d, r9d, 15, 16, 1FA27CF8h
135+
ROUND 47, r9d, r10d, r11d, r8d, 2, 23, -3B53A99Bh
136+
ROUND 48, r8d, r9d, r10d, r11d, 0, 6, -0BD6DDBCh
137+
ROUND 49, r11d, r8d, r9d, r10d, 7, 10, 432AFF97h
138+
ROUND 50, r10d, r11d, r8d, r9d, 14, 15, -546BDC59h
139+
ROUND 51, r9d, r10d, r11d, r8d, 5, 21, -036C5FC7h
140+
ROUND 52, r8d, r9d, r10d, r11d, 12, 6, 655B59C3h
141+
ROUND 53, r11d, r8d, r9d, r10d, 3, 10, -70F3336Eh
142+
ROUND 54, r10d, r11d, r8d, r9d, 10, 15, -00100B83h
143+
ROUND 55, r9d, r10d, r11d, r8d, 1, 21, -7A7BA22Fh
144+
ROUND 56, r8d, r9d, r10d, r11d, 8, 6, 6FA87E4Fh
145+
ROUND 57, r11d, r8d, r9d, r10d, 15, 10, -01D31920h
146+
ROUND 58, r10d, r11d, r8d, r9d, 6, 15, -5CFEBCECh
147+
ROUND 59, r9d, r10d, r11d, r8d, 13, 21, 4E0811A1h
148+
ROUND 60, r8d, r9d, r10d, r11d, 4, 6, -08AC817Eh
149+
ROUND 61, r11d, r8d, r9d, r10d, 11, 10, -42C50DCBh
150+
ROUND 62, r10d, r11d, r8d, r9d, 2, 15, 2AD7D2BBh
151+
ROUND 63, r9d, r10d, r11d, r8d, 9, 21, -14792C6Fh
152+
153+
; Compute intermediate hash value
154+
add [rdx] , r8d
155+
add [rdx + 4], r9d
156+
add [rdx + 8], r10d
157+
add [rdx + 12], r11d
158+
ret
159+
md5_compress endp
160+
end

sha1/build.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
fn main() {
22
let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
33
let target_vendor = std::env::var("CARGO_CFG_TARGET_VENDOR").unwrap_or_default();
4+
let target_family = std::env::var("CARGO_CFG_TARGET_FAMILY").unwrap_or_default();
45

56
let asm_path = if target_arch == "x86" {
67
"src/x86.S"
7-
} else if target_arch == "x86_64" {
8+
} else if target_arch == "x86_64" && target_family == "unix" {
89
"src/x64.S"
10+
} else if target_arch == "x86_64" && target_family == "windows" {
11+
"src/x64_masm.asm"
912
} else if target_arch == "aarch64" && target_vendor == "apple" {
1013
"src/aarch64_apple.S"
1114
} else if target_arch == "aarch64" {

0 commit comments

Comments
 (0)