test-chacha20.c (1857B)
1 #include "chacha20.h" 2 3 #include <assert.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 8 int 9 main(void) 10 { 11 char msg[] = "Ladies and Gentlemen of the class of '99: " 12 "If I could offer you only one tip for the future, " 13 "sunscreen would be it."; 14 15 size_t len = strlen(msg); 16 unsigned char *enc = malloc(len); 17 18 uint8_t key[CHACHA20_KEY_SIZE] = { 19 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 20 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 21 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 22 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 23 }; 24 25 uint32_t ctr = 1; 26 27 uint8_t iv[CHACHA20_IV_SIZE] = { 28 0x00, 0x00, 0x00, 0x00, 29 0x00, 0x00, 0x00, 0x4a, 30 0x00, 0x00, 0x00, 0x00, 31 }; 32 33 chacha20(key, ctr, iv, msg, len, enc); 34 35 uint8_t expected[] = { 36 0x6e, 0x2e, 0x35, 0x9a, 0x25, 0x68, 0xf9, 0x80, 37 0x41, 0xba, 0x07, 0x28, 0xdd, 0x0d, 0x69, 0x81, 38 0xe9, 0x7e, 0x7a, 0xec, 0x1d, 0x43, 0x60, 0xc2, 39 0x0a, 0x27, 0xaf, 0xcc, 0xfd, 0x9f, 0xae, 0x0b, 40 0xf9, 0x1b, 0x65, 0xc5, 0x52, 0x47, 0x33, 0xab, 41 0x8f, 0x59, 0x3d, 0xab, 0xcd, 0x62, 0xb3, 0x57, 42 0x16, 0x39, 0xd6, 0x24, 0xe6, 0x51, 0x52, 0xab, 43 0x8f, 0x53, 0x0c, 0x35, 0x9f, 0x08, 0x61, 0xd8, 44 0x07, 0xca, 0x0d, 0xbf, 0x50, 0x0d, 0x6a, 0x61, 45 0x56, 0xa3, 0x8e, 0x08, 0x8a, 0x22, 0xb6, 0x5e, 46 0x52, 0xbc, 0x51, 0x4d, 0x16, 0xcc, 0xf8, 0x06, 47 0x81, 0x8c, 0xe9, 0x1a, 0xb7, 0x79, 0x37, 0x36, 48 0x5a, 0xf9, 0x0b, 0xbf, 0x74, 0xa3, 0x5b, 0xe6, 49 0xb4, 0x0b, 0x8e, 0xed, 0xf2, 0x78, 0x5e, 0x42, 50 0x87, 0x4d, 51 }; 52 53 assert(sizeof expected == len); 54 55 printf("msg: \"%.*s\" (%lu bytes)\n", sizeof msg, msg, strlen(msg)); 56 57 printf("enc: "); 58 for (size_t i = 0; i < len; i++) { 59 printf("%.2x", enc[i]); 60 } 61 printf("\n"); 62 63 printf("exp: "); 64 for (size_t i = 0; i < len; i++) { 65 printf("%.2x", expected[i]); 66 } 67 printf("\n"); 68 69 return memcmp(expected, enc, len); 70 } 71 72 #include "chacha20.c"