v2 / vlib / x / crypto / poly1305 / usage_test.v
85 lines · 75 sloc · 4.19 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1import encoding.hex
2import x.crypto.poly1305
3
4// perform test for oneshot function for poly1305 mac creation and verifying it.
5fn test_fn_create_and_verify_tag() ! {
6 // Sample messages from RFC A.3 Test vector 2
7 // set secure key
8 key := hex.decode('0000000000000000000000000000000036e5f6b5c5e06070f0efca96227a863e')!
9 // messages to be authenticated
10 msg := 'Any submission to the IETF intended by the Contributor for publication as all or part of an IETF Internet-Draft or RFC and any statement made within the context of an IETF activity is considered an "IETF Contribution". Such statements include oral statements in IETF sessions, as well as written and electronic communications made at any time or place, which are addressed to'
11 .bytes()
12
13 // lets creates buffers output and build the tag
14 mut out := []u8{len: poly1305.tag_size}
15 poly1305.create_tag(mut out, msg, key)!
16 // we have expected tag
17 expected_tag := hex.decode('36e5f6b5c5e06070f0efca96227a863e')!
18 assert out == expected_tag
19
20 // lets assume we have a tag, and want to verify this
21 status := poly1305.verify_tag(expected_tag, msg, key)
22 assert status == true
23}
24
25// perform test for instance based method.
26fn test_create_and_verify_tag_with_poly1305_instance() ! {
27 // Sample messages from RFC A.3 Test vector 2
28 // set secure key
29 key := hex.decode('0000000000000000000000000000000036e5f6b5c5e06070f0efca96227a863e')!
30 // messages to be authenticated
31 msg := 'Any submission to the IETF intended by the Contributor for publication as all or part of an IETF Internet-Draft or RFC and any statement made within the context of an IETF activity is considered an "IETF Contribution". Such statements include oral statements in IETF sessions, as well as written and electronic communications made at any time or place, which are addressed to'
32 .bytes()
33
34 // lets creates buffers output and build the tag
35 mut out := []u8{len: poly1305.tag_size}
36 // we have expected tag
37 expected_tag := hex.decode('36e5f6b5c5e06070f0efca96227a863e')!
38
39 // create a new Poly1305 instance
40 mut po := poly1305.new(key)!
41 po.update(msg)
42 // you can check for the current state, we have expected tag
43 // dont call .verify after calling .finish, when its happens,
44 // you should reinit it with new key to make it usable.
45 valid_current_tag := po.verify(expected_tag)
46 assert valid_current_tag == true
47
48 // after call to .finish, you should not use the instance
49 po.finish(mut out)
50 assert out == expected_tag
51}
52
53// In this test, we perform test for methods based Poly1305 instance in incremental manner,
54// updates state by multiples block of messages by calling .update method of the Poly1305 instance
55fn test_create_and_verify_tag_with_poly1305_instance_in_incremental_updates() ! {
56 // Sample messages from RFC A.3 Test vector 2
57 // set secure key
58 key := hex.decode('0000000000000000000000000000000036e5f6b5c5e06070f0efca96227a863e')!
59 // messages to be authenticated
60 // msg := 'Any submission to the IETF intended by the Contributor for publication as all or part of an IETF Internet-Draft or RFC and any statement made within the context of an IETF activity is considered an "IETF Contribution". Such statements include oral statements in IETF sessions, as well as written and electronic communications made at any time or place, which are addressed to'
61 // .bytes()
62
63 // we split above message to five msg block
64 msg0 := 'Any submission to the IETF intended by the Contributor for publication as all '.bytes()
65 msg1 := 'or part of an IETF Internet-Draft or RFC and any statement made within '.bytes()
66 msg2 :=
67 'the context of an IETF activity is considered an "IETF Contribution". Such statements '.bytes()
68 msg3 := 'include oral statements in IETF sessions, as well as written and electronic '.bytes()
69 msg4 := 'communications made at any time or place, which are addressed to'.bytes()
70 // lets creates buffers output and build the tag
71 mut out := []u8{len: poly1305.tag_size}
72 // we have expected tag
73 expected_tag := hex.decode('36e5f6b5c5e06070f0efca96227a863e')!
74
75 // create a new Poly1305 instance
76 mut po := poly1305.new(key)!
77 po.update(msg0)
78 po.update(msg1)
79 po.update(msg2)
80 po.update(msg3)
81 po.update(msg4)
82 // after call to .finish, you should not use the instance
83 po.finish(mut out)
84 assert out == expected_tag
85}
86