1313import org .bouncycastle .crypto .OutputLengthException ;
1414import org .bouncycastle .crypto .Xof ;
1515import org .bouncycastle .crypto .digests .EncodableDigest ;
16+ import org .bouncycastle .crypto .digests .TupleHash ;
1617import org .bouncycastle .test .TestResourceFinder ;
1718import org .bouncycastle .util .Arrays ;
1819import org .bouncycastle .util .Memoable ;
@@ -71,71 +72,115 @@ public void performTest()
7172
7273 private void testEncodedState (byte [] resBuf , byte [] input , byte [] expected )
7374 {
74- // test state encoding;
75- digest .update (input , 0 , input .length / 2 );
75+ if (digest instanceof TupleHash )
76+ {
77+ digest .update (input , 0 , input .length );
78+ Digest copy1 = cloneDigest (((EncodableDigest )digest ).getEncodedState ());
79+ Digest copy2 = cloneDigest (((EncodableDigest )copy1 ).getEncodedState ());
7680
77- // copy the Digest
78- Digest copy1 = cloneDigest (((EncodableDigest )digest ).getEncodedState ());
79- Digest copy2 = cloneDigest (((EncodableDigest )copy1 ).getEncodedState ());
81+ digest .doFinal (resBuf , 0 );
8082
81- digest .update (input , input .length / 2 , input .length - input .length / 2 );
83+ if (!areEqual (expected , resBuf ))
84+ {
85+ fail ("failing TupleHash state vector test" , expected , new String (Hex .encode (resBuf )));
86+ }
8287
83- digest .doFinal (resBuf , 0 );
88+ copy2 .doFinal (resBuf , 0 );
8489
85- if (!areEqual (expected , resBuf ))
86- {
87- fail ("failing state vector test" , expected , new String (Hex .encode (resBuf )));
90+ if (!areEqual (expected , resBuf ))
91+ {
92+ fail ("failing TupleHash state copy2 vector test" , expected , new String (Hex .encode (resBuf )));
93+ }
8894 }
95+ else
96+ {
97+ // test state encoding;
98+ digest .update (input , 0 , input .length / 2 );
8999
90- copy1 .update (input , input .length / 2 , input .length - input .length / 2 );
91- copy1 .doFinal (resBuf , 0 );
100+ // copy the Digest
101+ Digest copy1 = cloneDigest (((EncodableDigest )digest ).getEncodedState ());
102+ Digest copy2 = cloneDigest (((EncodableDigest )copy1 ).getEncodedState ());
92103
93- if (!areEqual (expected , resBuf ))
94- {
95- fail ("failing state copy1 vector test" , expected , new String (Hex .encode (resBuf )));
96- }
104+ digest .update (input , input .length / 2 , input .length - input .length / 2 );
97105
98- copy2 .update (input , input .length / 2 , input .length - input .length / 2 );
99- copy2 .doFinal (resBuf , 0 );
106+ digest .doFinal (resBuf , 0 );
100107
101- if (!areEqual (expected , resBuf ))
102- {
103- fail ("failing state copy2 vector test" , expected , new String (Hex .encode (resBuf )));
108+ if (!areEqual (expected , resBuf ))
109+ {
110+ fail ("failing state vector test" , expected , new String (Hex .encode (resBuf )));
111+ }
112+
113+ copy1 .update (input , input .length / 2 , input .length - input .length / 2 );
114+ copy1 .doFinal (resBuf , 0 );
115+
116+ if (!areEqual (expected , resBuf ))
117+ {
118+ fail ("failing state copy1 vector test" , expected , new String (Hex .encode (resBuf )));
119+ }
120+
121+ copy2 .update (input , input .length / 2 , input .length - input .length / 2 );
122+ copy2 .doFinal (resBuf , 0 );
123+
124+ if (!areEqual (expected , resBuf ))
125+ {
126+ fail ("failing state copy2 vector test" , expected , new String (Hex .encode (resBuf )));
127+ }
104128 }
105129 }
106130
107131 private void testMemo (byte [] resBuf , byte [] input , byte [] expected )
108132 {
109- Memoable m = (Memoable )digest ;
133+ if (digest instanceof TupleHash )
134+ {
135+ Memoable m = (Memoable )digest ;
110136
111- digest .update (input , 0 , input .length / 2 );
137+ digest .update (input , 0 , input .length );
112138
113- // copy the Digest
114- Memoable copy1 = m .copy ();
115- Memoable copy2 = copy1 .copy ();
139+ Memoable copy1 = m .copy ();
140+ Memoable copy2 = copy1 .copy ();
116141
117- digest .update (input , input .length / 2 , input .length - input .length / 2 );
118- digest .doFinal (resBuf , 0 );
142+ digest .doFinal (resBuf , 0 );
143+ if (!areEqual (expected , resBuf ))
144+ {
145+ fail ("failing tuplehash memo vector test 1" , results [results .length - 1 ], new String (Hex .encode (resBuf )));
146+ }
119147
120- if (!areEqual (expected , resBuf ))
121- {
122- fail ("failing memo vector test" , results [results .length - 1 ], new String (Hex .encode (resBuf )));
148+ Digest d = (Digest )copy2 ;
149+ d .doFinal (resBuf , 0 );
123150 }
151+ else
152+ {
153+ Memoable m = (Memoable )digest ;
124154
125- m . reset ( copy1 );
155+ digest . update ( input , 0 , input . length / 2 );
126156
127- digest .update (input , input .length / 2 , input .length - input .length / 2 );
128- digest .doFinal (resBuf , 0 );
157+ // copy the Digest
158+ Memoable copy1 = m .copy ();
159+ Memoable copy2 = copy1 .copy ();
129160
130- if (!areEqual (expected , resBuf ))
131- {
132- fail ("failing memo reset vector test" , results [results .length - 1 ], new String (Hex .encode (resBuf )));
133- }
161+ digest .update (input , input .length / 2 , input .length - input .length / 2 );
162+ digest .doFinal (resBuf , 0 );
134163
135- Digest md = (Digest )copy2 ;
164+ if (!areEqual (expected , resBuf ))
165+ {
166+ fail ("failing memo vector test" , results [results .length - 1 ], new String (Hex .encode (resBuf )));
167+ }
168+
169+ m .reset (copy1 );
170+
171+ digest .update (input , input .length / 2 , input .length - input .length / 2 );
172+ digest .doFinal (resBuf , 0 );
173+
174+ if (!areEqual (expected , resBuf ))
175+ {
176+ fail ("failing memo reset vector test" , results [results .length - 1 ], new String (Hex .encode (resBuf )));
177+ }
136178
137- md .update (input , input .length / 2 , input .length - input .length / 2 );
138- md .doFinal (resBuf , 0 );
179+ Digest md = (Digest )copy2 ;
180+
181+ md .update (input , input .length / 2 , input .length - input .length / 2 );
182+ md .doFinal (resBuf , 0 );
183+ }
139184
140185 if (!areEqual (expected , resBuf ))
141186 {
@@ -145,21 +190,32 @@ private void testMemo(byte[] resBuf, byte[] input, byte[] expected)
145190
146191 private void testClone (byte [] resBuf , byte [] input , byte [] expected )
147192 {
148- digest .update (input , 0 , input .length / 2 );
193+ if (digest instanceof TupleHash )
194+ {
195+ // can't support multiple updates like the others - it's the whole point!
196+ Digest d = cloneDigest (digest );
149197
150- // clone the Digest
151- Digest d = cloneDigest (digest );
198+ d .update (input , 0 , input .length );
199+ d .doFinal (resBuf , 0 );
200+ }
201+ else
202+ {
203+ digest .update (input , 0 , input .length / 2 );
152204
153- digest . update ( input , input . length / 2 , input . length - input . length / 2 );
154- digest . doFinal ( resBuf , 0 );
205+ // clone the Digest
206+ Digest d = cloneDigest ( digest );
155207
156- if (!areEqual (expected , resBuf ))
157- {
158- fail ("failing clone vector test" , results [results .length - 1 ], new String (Hex .encode (resBuf )));
159- }
208+ digest .update (input , input .length / 2 , input .length - input .length / 2 );
209+ digest .doFinal (resBuf , 0 );
210+
211+ if (!areEqual (expected , resBuf ))
212+ {
213+ fail ("failing clone vector test" , results [results .length - 1 ], new String (Hex .encode (resBuf )));
214+ }
160215
161- d .update (input , input .length / 2 , input .length - input .length / 2 );
162- d .doFinal (resBuf , 0 );
216+ d .update (input , input .length / 2 , input .length - input .length / 2 );
217+ d .doFinal (resBuf , 0 );
218+ }
163219
164220 if (!areEqual (expected , resBuf ))
165221 {
0 commit comments