[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 1/2] tests: add crypto unittest for chacha20poly1305
[Thread Prev] | [Thread Next]
- Subject: [PATCH 1/2] tests: add crypto unittest for chacha20poly1305
- From: Jussi Kivilinna <jussi.kivilinna@xxxxxx>
- Reply-to: libssh@xxxxxxxxxx
- Date: Sat, 7 Dec 2019 19:17:56 +0200
- To: libssh@xxxxxxxxxx
- Cc: Jussi Kivilinna <jussi.kivilinna@xxxxxx>
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@xxxxxx> --- tests/unittests/torture_crypto.c | 73 ++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/unittests/torture_crypto.c b/tests/unittests/torture_crypto.c index fd5e7753..caa00b72 100644 --- a/tests/unittests/torture_crypto.c +++ b/tests/unittests/torture_crypto.c @@ -110,10 +110,83 @@ static void torture_crypto_aes256_cbc(void **state) ssh_cipher_clear(&cipher); } +#define POLY1305_TAGLEN 16 + +uint8_t chacha20poly1305_encrypted[sizeof(uint32_t) + 144 + POLY1305_TAGLEN] = + "\xed\xaf\xbc\xa3\xf6\x97\x75\xb4\x3b\x8f\xb0\x8e\xb0\x0a\x8e" + "\xb3\x90\x21\x0d\x7a\xb6\xd3\x03\xf6\xbc\x6e\x3a\x32\x67\xe1" + "\x13\x65\x43\x3b\x34\x9d\xcb\x62\x7e\x0a\x80\xb0\x45\x87\x07" + "\x85\x49\x8d\x23\x5f\xac\x9c\x8b\xa8\xd5\x01\x12\xfe\x52\xc6" + "\x99\xb4\xf2\xde\x12\x78\x79\xea\x1c\x5f\x45\xcd\xf7\xe4\xa0" + "\x66\x15\x7f\xe3\xf4\x73\x3b\xe0\x52\xac\x2a\x00\x73\xd0\xd7" + "\x95\xa9\xb9\x3a\xe0\x50\x13\xf4\xdc\xfc\x2a\x64\xb5\xcf\x29" + "\x88\xef\x4c\x56\x10\x30\x28\xbb\x59\xb8\x23\x58\xab\x01\xa2" + "\xab\x6b\xdd\xee\x20\x43\xe1\xec\x7a\xe1\xaa\x8b\x60\x19\xde" + "\x3a\xd1\xd6\x80\x49\x7d\x5c\x81\xb8\x96\xad\x62\x32\xb4\x24" + "\x5c\xcf\xb8\xab\xd7\xa6\xc6\x47\x8d\x73\x9e\x44\xc6\x66"; + +static void torture_crypto_chacha20poly1305(void **state) +{ + const uint64_t seq = (uint64_t)1234567890 * 98765431; + uint8_t input[sizeof(uint32_t) + sizeof(cleartext)]; + uint8_t output[sizeof(input) + POLY1305_TAGLEN] = {0}; + uint8_t *outtag = output + sizeof(input); + struct ssh_cipher_struct cipher = {0}; + uint32_t in_length; + int rc; + (void)state; + + assert_int_equal(sizeof(output), sizeof(chacha20poly1305_encrypted)); + + in_length = htonl(sizeof(cleartext)); + memcpy(input, &in_length, sizeof(uint32_t)); + memcpy(input + sizeof(uint32_t), cleartext, sizeof(cleartext)); + + rc = get_cipher(&cipher, "chacha20-poly1305@xxxxxxxxxxx"); + assert_int_equal(rc, SSH_OK); + + assert_non_null(cipher.set_encrypt_key); + assert_non_null(cipher.aead_encrypt); + + rc = cipher.set_encrypt_key(&cipher, key, NULL); + assert_int_equal(rc, SSH_OK); + + cipher.aead_encrypt(&cipher, input, output, sizeof(input), outtag, seq); + + assert_memory_equal(output, chacha20poly1305_encrypted, + sizeof(chacha20poly1305_encrypted)); + ssh_cipher_clear(&cipher); + + memset(output, '\0', sizeof(output)); + + rc = get_cipher(&cipher, "chacha20-poly1305@xxxxxxxxxxx"); + assert_int_equal(rc, SSH_OK); + + assert_non_null(cipher.set_decrypt_key); + assert_non_null(cipher.aead_decrypt); + assert_non_null(cipher.aead_decrypt_length); + + rc = cipher.set_decrypt_key(&cipher, key, NULL); + assert_int_equal(rc, SSH_OK); + + rc = cipher.aead_decrypt_length(&cipher, chacha20poly1305_encrypted, + output, sizeof(uint32_t), seq); + assert_int_equal(rc, SSH_OK); + + rc = cipher.aead_decrypt(&cipher, chacha20poly1305_encrypted, + output + sizeof(uint32_t), sizeof(cleartext), seq); + assert_int_equal(rc, SSH_OK); + + assert_memory_equal(output, input, sizeof(input)); + + ssh_cipher_clear(&cipher); +} + int torture_run_tests(void) { int rc; const struct CMUnitTest tests[] = { cmocka_unit_test(torture_crypto_aes256_cbc), + cmocka_unit_test(torture_crypto_chacha20poly1305), }; ssh_init(); -- 2.20.1
[PATCH 2/2] libgcrypt: Implement chacha20-poly1305@xxxxxxxxxxx cipher using libgcrypt | Jussi Kivilinna <jussi.kivilinna@xxxxxx> |