| 1 | import encoding.hex |
| 2 | import x.crypto.chacha20poly1305 |
| 3 | |
| 4 | // test public function based encrypt and decrypt of chacha20poly1305 |
| 5 | fn test_aead_chacha20poly1305_fn_encrypt_and_decrypt_operation() ! { |
| 6 | // the data mostly based on rfc |
| 7 | // gives yours secure key |
| 8 | key := hex.decode('1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0')! |
| 9 | // gives your additional data |
| 10 | aad := hex.decode('f33388860000000000004e91')! |
| 11 | // gives your nonce |
| 12 | nonce := hex.decode('000000000102030405060708')! |
| 13 | |
| 14 | // your message to be encrypted and authenticated |
| 15 | plaintext := |
| 16 | 'Internet-Drafts are draft documents valid for a maximum of six months and may be updated, replaced, or obsoleted by other documents at any time. It is inappropriate to use Internet-Drafts as reference material or to cite them other than as /“work in progress./”'.bytes() |
| 17 | |
| 18 | ciphertext_out := chacha20poly1305.encrypt(plaintext, key, nonce, aad)! |
| 19 | ciphertext := |
| 20 | hex.decode('64a0861575861af460f062c79be643bd5e805cfd345cf389f108670ac76c8cb24c6cfc18755d43eea09ee94e382d26b0bdb7b73c321b0100d4f03b7f355894cf332f830e710b97ce98c8a84abd0b948114ad176e008d33bd60f982b1ff37c8559797a06ef4f0ef61c186324e2b3506383606907b6a7c02b0f9f6157b53c867e4b9166c767b804d46a59b5216cde7a4e99040c5a40433225ee282a1b0a06c523eaf4534d7f83fa1155b0047718cbc546a0d072b04b3564eea1b422273f548271a0bb2316053fa76991955ebd63159434ecebb4e466dae5a1073a6727627097a1049e617d91d361094fa68f0ff77987130305beaba2eda04df997b714d6c6f2c29a6ad5cb4022b02709b')! |
| 21 | // Received Tag |
| 22 | expected_tag := hex.decode('eead9d67890cbb22392336fea1851f38')! |
| 23 | // aead output is concatenation of ciphertext and the tag |
| 24 | mut encrypted_output := []u8{} |
| 25 | encrypted_output << ciphertext |
| 26 | encrypted_output << expected_tag |
| 27 | |
| 28 | assert ciphertext_out == encrypted_output |
| 29 | assert ciphertext_out[ciphertext_out.len - 16..] == expected_tag |
| 30 | // Lets decrypt back |
| 31 | message := chacha20poly1305.decrypt(ciphertext_out, key, nonce, aad)! |
| 32 | |
| 33 | assert message == plaintext |
| 34 | } |
| 35 | |
| 36 | // we going to use same data with the first test, but testing for instance based encrypt |
| 37 | fn test_aead_chacha20poly1305_instance_encrypt_and_decrypt_operation() ! { |
| 38 | // gives yours secure key |
| 39 | key := hex.decode('1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0')! |
| 40 | // gives your additional data |
| 41 | aad := hex.decode('f33388860000000000004e91')! |
| 42 | // gives your nonce |
| 43 | nonce := hex.decode('000000000102030405060708')! |
| 44 | // your message to be encrypted and authenticated |
| 45 | plaintext := |
| 46 | hex.decode('496e7465726e65742d4472616674732061726520647261667420646f63756d656e74732076616c696420666f722061206d6178696d756d206f6620736978206d6f6e74687320616e64206d617920626520757064617465642c207265706c616365642c206f72206f62736f6c65746564206279206f7468657220646f63756d656e747320617420616e792074696d652e20497420697320696e617070726f70726961746520746f2075736520496e7465726e65742d447261667473206173207265666572656e6365206d6174657269616c206f7220746f2063697465207468656d206f74686572207468616e206173202fe2809c776f726b20696e2070726f67726573732e2fe2809d')! |
| 47 | |
| 48 | // lets create chacha20poly1305 AEAD |
| 49 | mut co := chacha20poly1305.new(key, nonce.len)! |
| 50 | ciphertext_out := co.encrypt(plaintext, nonce, aad)! |
| 51 | |
| 52 | // we have expected ciphertext |
| 53 | ciphertext := |
| 54 | hex.decode('64a0861575861af460f062c79be643bd5e805cfd345cf389f108670ac76c8cb24c6cfc18755d43eea09ee94e382d26b0bdb7b73c321b0100d4f03b7f355894cf332f830e710b97ce98c8a84abd0b948114ad176e008d33bd60f982b1ff37c8559797a06ef4f0ef61c186324e2b3506383606907b6a7c02b0f9f6157b53c867e4b9166c767b804d46a59b5216cde7a4e99040c5a40433225ee282a1b0a06c523eaf4534d7f83fa1155b0047718cbc546a0d072b04b3564eea1b422273f548271a0bb2316053fa76991955ebd63159434ecebb4e466dae5a1073a6727627097a1049e617d91d361094fa68f0ff77987130305beaba2eda04df997b714d6c6f2c29a6ad5cb4022b02709b')! |
| 55 | // Received Tag |
| 56 | expected_tag := hex.decode('eead9d67890cbb22392336fea1851f38')! |
| 57 | // aead output is concatenation of ciphertext and the tag |
| 58 | mut encrypted_output := []u8{} |
| 59 | encrypted_output << ciphertext |
| 60 | encrypted_output << expected_tag |
| 61 | |
| 62 | assert ciphertext_out == encrypted_output |
| 63 | assert ciphertext_out[ciphertext_out.len - 16..] == expected_tag |
| 64 | |
| 65 | // Lets decrypt back |
| 66 | message := co.decrypt(ciphertext_out, nonce, aad)! |
| 67 | assert message == plaintext |
| 68 | } |
| 69 | |