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