Skip to content

Commit 466047c

Browse files
committed
poly1305: add tests
1 parent 6c7868f commit 466047c

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

src/rust-crypto/poly1305.rs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,138 @@ impl Mac for Poly1305 {
219219

220220
fn output_bytes(&self) -> uint { 16 }
221221
}
222+
223+
#[cfg(test)]
224+
mod test {
225+
use poly1305::Poly1305;
226+
use mac::Mac;
227+
228+
fn poly1305(key: &[u8], msg: &[u8], mac: &mut [u8]) {
229+
let mut poly = Poly1305::new(key);
230+
poly.input(msg);
231+
poly.raw_result(mac);
232+
}
233+
234+
#[test]
235+
fn test_nacl_vector() {
236+
let key = [
237+
0xee,0xa6,0xa7,0x25,0x1c,0x1e,0x72,0x91,
238+
0x6d,0x11,0xc2,0xcb,0x21,0x4d,0x3c,0x25,
239+
0x25,0x39,0x12,0x1d,0x8e,0x23,0x4e,0x65,
240+
0x2d,0x65,0x1f,0xa4,0xc8,0xcf,0xf8,0x80,
241+
];
242+
243+
let msg = [
244+
0x8e,0x99,0x3b,0x9f,0x48,0x68,0x12,0x73,
245+
0xc2,0x96,0x50,0xba,0x32,0xfc,0x76,0xce,
246+
0x48,0x33,0x2e,0xa7,0x16,0x4d,0x96,0xa4,
247+
0x47,0x6f,0xb8,0xc5,0x31,0xa1,0x18,0x6a,
248+
0xc0,0xdf,0xc1,0x7c,0x98,0xdc,0xe8,0x7b,
249+
0x4d,0xa7,0xf0,0x11,0xec,0x48,0xc9,0x72,
250+
0x71,0xd2,0xc2,0x0f,0x9b,0x92,0x8f,0xe2,
251+
0x27,0x0d,0x6f,0xb8,0x63,0xd5,0x17,0x38,
252+
0xb4,0x8e,0xee,0xe3,0x14,0xa7,0xcc,0x8a,
253+
0xb9,0x32,0x16,0x45,0x48,0xe5,0x26,0xae,
254+
0x90,0x22,0x43,0x68,0x51,0x7a,0xcf,0xea,
255+
0xbd,0x6b,0xb3,0x73,0x2b,0xc0,0xe9,0xda,
256+
0x99,0x83,0x2b,0x61,0xca,0x01,0xb6,0xde,
257+
0x56,0x24,0x4a,0x9e,0x88,0xd5,0xf9,0xb3,
258+
0x79,0x73,0xf6,0x22,0xa4,0x3d,0x14,0xa6,
259+
0x59,0x9b,0x1f,0x65,0x4c,0xb4,0x5a,0x74,
260+
0xe3,0x55,0xa5,
261+
];
262+
263+
let expected = [
264+
0xf3,0xff,0xc7,0x70,0x3f,0x94,0x00,0xe5,
265+
0x2a,0x7d,0xfb,0x4b,0x3d,0x33,0x05,0xd9,
266+
];
267+
268+
let mut mac = [0u8, ..16];
269+
poly1305(key, msg, mac.as_mut_slice());
270+
assert_eq!(mac.as_slice(), expected.as_slice());
271+
272+
let mut poly = Poly1305::new(key);
273+
poly.input(msg.slice( 0, 32));
274+
poly.input(msg.slice( 32, 96));
275+
poly.input(msg.slice( 96, 112));
276+
poly.input(msg.slice(112, 120));
277+
poly.input(msg.slice(120, 124));
278+
poly.input(msg.slice(124, 126));
279+
poly.input(msg.slice(126, 127));
280+
poly.input(msg.slice(127, 128));
281+
poly.input(msg.slice(128, 129));
282+
poly.input(msg.slice(129, 130));
283+
poly.input(msg.slice(130, 131));
284+
poly.raw_result(mac.as_mut_slice());
285+
assert_eq!(mac.as_slice(), expected.as_slice());
286+
}
287+
288+
#[test]
289+
fn donna_self_test() {
290+
let wrap_key = [
291+
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
292+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
293+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
294+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
295+
];
296+
297+
let wrap_msg = [
298+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
299+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
300+
];
301+
302+
let wrap_mac = [
303+
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
304+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
305+
];
306+
307+
let mut mac = [0u8, ..16];
308+
poly1305(wrap_key, wrap_msg, mac.as_mut_slice());
309+
assert_eq!(mac.as_slice(), wrap_mac.as_slice());
310+
311+
let total_key = [
312+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xff,
313+
0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xff, 0xff,
314+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
315+
0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
316+
];
317+
318+
let total_mac = [
319+
0x64, 0xaf, 0xe2, 0xe8, 0xd6, 0xad, 0x7b, 0xbd,
320+
0xd2, 0x87, 0xf9, 0x7c, 0x44, 0x62, 0x3d, 0x39,
321+
];
322+
323+
let mut tpoly = Poly1305::new(total_key);
324+
for i in range(0u, 256) {
325+
let key = Vec::from_elem(32, i as u8);
326+
let msg = Vec::from_elem(256, i as u8);
327+
let mut mac = [0u8, ..16];
328+
poly1305(key.as_slice(), msg.slice(0, i), mac);
329+
tpoly.input(mac);
330+
}
331+
tpoly.raw_result(mac);
332+
assert_eq!(mac.as_slice(), total_mac.as_slice());
333+
}
334+
335+
#[test]
336+
fn test_tls_vectors() {
337+
// from http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04
338+
let key = bytes!("this is 32-byte key for Poly1305");
339+
let msg = [0u8, ..32];
340+
let expected = [
341+
0x49, 0xec, 0x78, 0x09, 0x0e, 0x48, 0x1e, 0xc6,
342+
0xc2, 0x6b, 0x33, 0xb9, 0x1c, 0xcc, 0x03, 0x07,
343+
];
344+
let mut mac = [0u8, ..16];
345+
poly1305(key, msg, mac.as_mut_slice());
346+
assert_eq!(mac.as_slice(), expected.as_slice());
347+
348+
let msg = bytes!("Hello world!");
349+
let expected= [
350+
0xa6, 0xf7, 0x45, 0x00, 0x8f, 0x81, 0xc9, 0x16,
351+
0xa2, 0x0d, 0xcc, 0x74, 0xee, 0xf2, 0xb2, 0xf0,
352+
];
353+
poly1305(key, msg, mac.as_mut_slice());
354+
assert_eq!(mac.as_slice(), expected.as_slice());
355+
}
356+
}

src/rust-crypto/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub mod hmac;
2727
pub mod mac;
2828
pub mod md5;
2929
pub mod pbkdf2;
30+
pub mod poly1305;
3031
pub mod rc4;
3132
pub mod salsa20;
3233
pub mod scrypt;

0 commit comments

Comments
 (0)