]> gitweb.factorcode.org Git - factor.git/blob - basis/checksums/md5/md5.factor
d59976fb7e48b5daecb2e6fdbbe3b730780a7728
[factor.git] / basis / checksums / md5 / md5.factor
1 ! Copyright (C) 2006, 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien.c-types kernel io io.binary io.files io.streams.byte-array math
4 math.functions math.parser namespaces splitting grouping strings
5 sequences byte-arrays locals sequences.private macros fry
6 io.encodings.binary math.bitwise checksums accessors
7 checksums.common checksums.stream combinators combinators.smart
8 specialized-arrays.uint literals hints ;
9 IN: checksums.md5
10
11 SINGLETON: md5
12
13 INSTANCE: md5 stream-checksum
14
15 TUPLE: md5-state < checksum-state state old-state ;
16
17 : <md5-state> ( -- md5 )
18     md5-state new-checksum-state
19         64 >>block-size
20         uint-array{ HEX: 67452301 HEX: efcdab89 HEX: 98badcfe HEX: 10325476 }
21         [ clone >>state ] [ >>old-state ] bi ;
22
23 M: md5 initialize-checksum-state drop <md5-state> ;
24
25 <PRIVATE
26
27 : v-w+ ( v1 v2 -- v3 ) [ w+ ] 2map ;
28
29 : update-md5 ( md5 -- )
30     [ state>> ] [ old-state>> v-w+ dup clone ] [ ] tri
31     [ (>>old-state) ] [ (>>state) ] bi ;
32
33 CONSTANT: T
34     $[
35         80 iota [ sin abs 32 2^ * >integer ] uint-array{ } map-as
36     ]
37
38 :: F ( X Y Z -- FXYZ )
39     #! F(X,Y,Z) = XY v not(X) Z
40     X Y bitand X bitnot Z bitand bitor ; inline
41
42 :: G ( X Y Z -- GXYZ )
43     #! G(X,Y,Z) = XZ v Y not(Z)
44     X Z bitand Y Z bitnot bitand bitor ; inline
45
46 : H ( X Y Z -- HXYZ )
47     #! H(X,Y,Z) = X xor Y xor Z
48     bitxor bitxor ; inline
49
50 :: I ( X Y Z -- IXYZ )
51     #! I(X,Y,Z) = Y xor (X v not(Z))
52     Z bitnot X bitor Y bitxor ; inline
53
54 CONSTANT: S11 7
55 CONSTANT: S12 12
56 CONSTANT: S13 17
57 CONSTANT: S14 22
58 CONSTANT: S21 5
59 CONSTANT: S22 9
60 CONSTANT: S23 14
61 CONSTANT: S24 20
62 CONSTANT: S31 4
63 CONSTANT: S32 11
64 CONSTANT: S33 16
65 CONSTANT: S34 23
66 CONSTANT: S41 6
67 CONSTANT: S42 10
68 CONSTANT: S43 15
69 CONSTANT: S44 21
70
71 CONSTANT: a 0
72 CONSTANT: b 1
73 CONSTANT: c 2
74 CONSTANT: d 3
75
76 :: (ABCD) ( x state a b c d k s i quot -- )
77     #! a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s)
78     a state [
79         b state nth-unsafe
80         c state nth-unsafe
81         d state nth-unsafe quot call w+
82         k x nth-unsafe w+
83         i T nth-unsafe w+
84         s bitroll-32
85         b state nth-unsafe w+ 32 bits
86     ] change-nth-unsafe ; inline
87
88 MACRO: with-md5-round ( ops quot -- )
89     '[ [ _ (ABCD) ] compose ] map '[ _ 2cleave ] ;
90
91 : (process-md5-block-F) ( block state -- )
92     {
93         [ a b c d 0  S11 1  ]
94         [ d a b c 1  S12 2  ]
95         [ c d a b 2  S13 3  ]
96         [ b c d a 3  S14 4  ]
97         [ a b c d 4  S11 5  ]
98         [ d a b c 5  S12 6  ]
99         [ c d a b 6  S13 7  ]
100         [ b c d a 7  S14 8  ]
101         [ a b c d 8  S11 9  ]
102         [ d a b c 9  S12 10 ]
103         [ c d a b 10 S13 11 ]
104         [ b c d a 11 S14 12 ]
105         [ a b c d 12 S11 13 ]
106         [ d a b c 13 S12 14 ]
107         [ c d a b 14 S13 15 ]
108         [ b c d a 15 S14 16 ]
109     } [ F ] with-md5-round ;
110
111 : (process-md5-block-G) ( block state -- )
112     {
113         [ a b c d 1  S21 17 ]
114         [ d a b c 6  S22 18 ]
115         [ c d a b 11 S23 19 ]
116         [ b c d a 0  S24 20 ]
117         [ a b c d 5  S21 21 ]
118         [ d a b c 10 S22 22 ]
119         [ c d a b 15 S23 23 ]
120         [ b c d a 4  S24 24 ]
121         [ a b c d 9  S21 25 ]
122         [ d a b c 14 S22 26 ]
123         [ c d a b 3  S23 27 ]
124         [ b c d a 8  S24 28 ]
125         [ a b c d 13 S21 29 ]
126         [ d a b c 2  S22 30 ]
127         [ c d a b 7  S23 31 ]
128         [ b c d a 12 S24 32 ]
129     } [ G ] with-md5-round ;
130
131 : (process-md5-block-H) ( block state -- )
132     {
133         [ a b c d 5  S31 33 ]
134         [ d a b c 8  S32 34 ]
135         [ c d a b 11 S33 35 ]
136         [ b c d a 14 S34 36 ]
137         [ a b c d 1  S31 37 ]
138         [ d a b c 4  S32 38 ]
139         [ c d a b 7  S33 39 ]
140         [ b c d a 10 S34 40 ]
141         [ a b c d 13 S31 41 ]
142         [ d a b c 0  S32 42 ]
143         [ c d a b 3  S33 43 ]
144         [ b c d a 6  S34 44 ]
145         [ a b c d 9  S31 45 ]
146         [ d a b c 12 S32 46 ]
147         [ c d a b 15 S33 47 ]
148         [ b c d a 2  S34 48 ]
149     } [ H ] with-md5-round ;
150
151 : (process-md5-block-I) ( block state -- )
152     {
153         [ a b c d 0  S41 49 ]
154         [ d a b c 7  S42 50 ]
155         [ c d a b 14 S43 51 ]
156         [ b c d a 5  S44 52 ]
157         [ a b c d 12 S41 53 ]
158         [ d a b c 3  S42 54 ]
159         [ c d a b 10 S43 55 ]
160         [ b c d a 1  S44 56 ]
161         [ a b c d 8  S41 57 ]
162         [ d a b c 15 S42 58 ]
163         [ c d a b 6  S43 59 ]
164         [ b c d a 13 S44 60 ]
165         [ a b c d 4  S41 61 ]
166         [ d a b c 11 S42 62 ]
167         [ c d a b 2  S43 63 ]
168         [ b c d a 9  S44 64 ]
169     } [ I ] with-md5-round ;
170
171 HINTS: (process-md5-block-F) { uint-array md5-state } ;
172 HINTS: (process-md5-block-G) { uint-array md5-state } ;
173 HINTS: (process-md5-block-H) { uint-array md5-state } ;
174 HINTS: (process-md5-block-I) { uint-array md5-state } ;
175
176 : byte-array>le ( byte-array -- byte-array )
177     little-endian? [
178         dup 4 <sliced-groups> [
179             [ [ 1 2 ] dip exchange-unsafe ]
180             [ [ 0 3 ] dip exchange-unsafe ] bi
181         ] each
182     ] unless ;
183
184 : byte-array>uint-array-le ( byte-array -- uint-array )
185     byte-array>le byte-array>uint-array ;
186
187 HINTS: byte-array>uint-array-le byte-array ;
188
189 : uint-array>byte-array-le ( uint-array -- byte-array )
190     underlying>> byte-array>le ;
191
192 HINTS: uint-array>byte-array-le uint-array ;
193
194 M: md5-state checksum-block ( block state -- )
195     [
196         [ byte-array>uint-array-le ] [ state>> ] bi* {
197             [ (process-md5-block-F) ]
198             [ (process-md5-block-G) ]
199             [ (process-md5-block-H) ]
200             [ (process-md5-block-I) ]
201         } 2cleave
202     ] [
203         nip update-md5
204     ] 2bi ;
205
206 : md5>checksum ( md5 -- bytes ) state>> uint-array>byte-array-le ;
207
208 M: md5-state clone ( md5 -- new-md5 )
209     call-next-method
210     [ clone ] change-state
211     [ clone ] change-old-state ;
212
213 M: md5-state get-checksum ( md5 -- bytes )
214     clone [ bytes>> f ] [ bytes-read>> pad-last-block ] [ ] tri
215     [ [ checksum-block ] curry each ] [ md5>checksum ] bi ;
216
217 M: md5 checksum-stream ( stream checksum -- byte-array )
218     drop
219     [ <md5-state> ] dip add-checksum-stream get-checksum ;
220
221 PRIVATE>