1 /*************************************************************************
2 * This is a simple program to calculate test vectors and compare them *
3 * to known good values for XChaCha20.
4 *************************************************************************/
10 #include "xchacha20.h"
11 #include "xchacha20_wrapper.h"
14 /** Compare our output to the output of a known good XChaCha20 library.
15 * The test vectors used here are from examples given of the Crypto++
16 * cryptographic library's XChaCha20 examples. These values can be
18 * https://www.cryptopp.com/wiki/XChaCha20
19 * @returns 0 on success, -1 on failure or error
25 uint8_t counter
[8] = {0x1};
27 /* Test values from Crypto++ documentation */
29 0x5E, 0xC5, 0x8B, 0x6D, 0x51, 0x4F, 0xE0, 0xA5,
30 0x6F, 0x1E, 0x0D, 0xEA, 0x7B, 0xDC, 0x09, 0x5A,
31 0x10, 0xF5, 0xB6, 0x18, 0xBD, 0xB6, 0xF2, 0x26,
32 0x2F, 0xCC, 0x59, 0x7B, 0xB2, 0x30, 0xB3, 0xEF
36 0xA3, 0x45, 0xF5, 0xCF, 0x80, 0x23, 0x51, 0x7C,
37 0xC0, 0xFC, 0xF0, 0x75, 0x74, 0x8C, 0x86, 0x5F,
38 0x7D, 0xE8, 0xCA, 0x0C, 0x72, 0x36, 0xAB, 0xDA
41 uint8_t correct_ciphertext
[] = {
42 0xEE, 0xA7, 0xC2, 0x71, 0x19, 0x10, 0x65, 0x69,
43 0x92, 0xE1, 0xCE, 0xD8, 0x16, 0xE2, 0x0E, 0x62,
44 0x1B, 0x25, 0x17, 0x82, 0x36, 0x71, 0x6A, 0xE4,
45 0x99, 0xF2, 0x97, 0x37, 0xA7, 0x2A, 0xFC, 0xF8,
49 uint8_t plaintext
[] = "My Plaintext!! My Dear plaintext!!";
50 uint32_t msglen
= strlen((char *)plaintext
);
52 xchacha_keysetup(&ctx
, key
, iv
);
54 /* Crypto++ initializes their counter to 1 instead of 0 */
55 xchacha_set_counter(&ctx
, counter
);
56 //xchacha_encrypt_bytes(&ctx, plaintext, buffer, msglen);
57 xchacha_encrypt_bytes_svp64(&ctx
, plaintext
, buffer
, msglen
);
59 /* Compare our ciphertext to the correct ciphertext */
60 if(memcmp(buffer
, correct_ciphertext
, msglen
) != 0){
61 printf("msglen: %d\n", msglen
);
63 for (uint32_t i
=0; i
< msglen
; i
++) {
64 printf("%02x ", correct_ciphertext
[i
]);
67 for (uint32_t i
=0; i
< msglen
; i
++) {
68 printf("%02x ", buffer
[i
]);
78 if ((check_cpp()) == 0) {
79 printf("Cryptographic tests passed\n");
81 printf("Cryptographic tests failed!\n");