pysvp64db: fix traversal
[openpower-isa.git] / crypto / chacha20 / src / test.c
1 /*************************************************************************
2 * This is a simple program to calculate test vectors and compare them *
3 * to known good values for XChaCha20.
4 *************************************************************************/
5 #include <stdio.h>
6 #include <errno.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include "xchacha20.h"
11 #include "xchacha20_wrapper.h"
12
13
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
17 * found here:
18 * https://www.cryptopp.com/wiki/XChaCha20
19 * @returns 0 on success, -1 on failure or error
20 *
21 */
22 int check_cpp(void){
23 XChaCha_ctx ctx;
24 uint8_t buffer[128];
25 uint8_t counter[8] = {0x1};
26
27 /* Test values from Crypto++ documentation */
28 uint8_t key[] = {
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
33 };
34
35 uint8_t iv[] = {
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
39 };
40
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,
46 0x6C, 0x72
47 };
48
49 uint8_t plaintext[] = "My Plaintext!! My Dear plaintext!!";
50 uint32_t msglen = strlen((char *)plaintext);
51
52 xchacha_keysetup(&ctx, key, iv);
53
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);
58
59 /* Compare our ciphertext to the correct ciphertext */
60 if(memcmp(buffer, correct_ciphertext, msglen) != 0){
61 printf("msglen: %d\n", msglen);
62 printf("correct:\n");
63 for (uint32_t i=0; i < msglen; i++) {
64 printf("%02x ", correct_ciphertext[i]);
65 }
66 printf("\nsvp64:\n");
67 for (uint32_t i=0; i < msglen; i++) {
68 printf("%02x ", buffer[i]);
69 }
70 printf("\n");
71 return(-1);
72 }
73
74 return(0);
75 }
76
77 int main(void){
78 if ((check_cpp()) == 0) {
79 printf("Cryptographic tests passed\n");
80 } else {
81 printf("Cryptographic tests failed!\n");
82 }
83
84 return(0);
85 }