| 1 |
/* |
| 2 |
* Copyright 2018 Donald Stufft and individual contributors |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
* |
| 16 |
* Test vector generator/checker for libsodium's crypto_secretstream APIs |
| 17 |
* to build in a unix-like environment, use a command line like |
| 18 |
* $ cc secretstream_test_vector.c \ |
| 19 |
* -I${IPATH} -L${LPATH} -lsodium \ |
| 20 |
* -o secretstream_test_vector |
| 21 |
* with IPATH and LPATH defined to respectively point to libsodium's include path |
| 22 |
* and to the directory containing the link library libsodium.a or libsodium.o |
| 23 |
* |
| 24 |
*/ |
| 25 |
#include <stdio.h> |
| 26 |
#include <string.h> |
| 27 |
#include <sodium.h> |
| 28 |
#include <unistd.h> |
| 29 |
|
| 30 |
#define MAX_AD_SIZE 32 |
| 31 |
#define MAX_CHUNK_SIZE 512 |
| 32 |
#define CHK(cmd) \ |
| 33 |
do { if ((rc = (cmd)) != 0) { \ |
| 34 |
fprintf(stderr, "api call failed, code=%d", rc); \ |
| 35 |
exit(1); \ |
| 36 |
}} while(0) |
| 37 |
|
| 38 |
int usage(int argc, char **argv) { |
| 39 |
fprintf(stderr, "Usage: %s [-c num_chunks] [-r]\n", argv[0]); |
| 40 |
return 1; |
| 41 |
} |
| 42 |
|
| 43 |
int main (int argc, char **argv) { |
| 44 |
int c, rc; |
| 45 |
int num_chunks = 1; |
| 46 |
int rekey = 0; |
| 47 |
|
| 48 |
crypto_secretstream_xchacha20poly1305_state state; |
| 49 |
unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; |
| 50 |
unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]; |
| 51 |
unsigned char m[MAX_CHUNK_SIZE]; |
| 52 |
unsigned char ad[MAX_AD_SIZE]; |
| 53 |
unsigned char ct[MAX_CHUNK_SIZE + crypto_secretstream_xchacha20poly1305_ABYTES]; |
| 54 |
char key_hex[sizeof(key) * 2 + 1]; |
| 55 |
char header_hex[sizeof(header) * 2 + 1]; |
| 56 |
char m_hex[sizeof(m) * 2 + 1]; |
| 57 |
char ad_hex[sizeof(ad) * 2 + 1]; |
| 58 |
char ct_hex[sizeof(ct) * 2 + 1]; |
| 59 |
unsigned long long m_len, ad_len, ct_len; |
| 60 |
unsigned char tag; |
| 61 |
|
| 62 |
while ((c = getopt(argc, argv, "hc:r")) != -1) { |
| 63 |
switch (c) { |
| 64 |
case 'c': |
| 65 |
num_chunks = atoi(optarg); |
| 66 |
break; |
| 67 |
case 'r': |
| 68 |
rekey = 1; |
| 69 |
break; |
| 70 |
case 'h': |
| 71 |
return usage(argc, argv); |
| 72 |
default: |
| 73 |
return 1; |
| 74 |
} |
| 75 |
} |
| 76 |
if (optind < argc) return usage(argc, argv); |
| 77 |
|
| 78 |
if (sodium_init() == -1) { |
| 79 |
exit(1); |
| 80 |
} |
| 81 |
|
| 82 |
/* output format: |
| 83 |
* { |
| 84 |
* "key": "hex", |
| 85 |
* "header": "hex", |
| 86 |
* "chunks": [ |
| 87 |
* { |
| 88 |
* "tag": 0, |
| 89 |
* "ad": "hex", |
| 90 |
* "message": "hex", |
| 91 |
* "ciphertext": "hex" |
| 92 |
* }, |
| 93 |
* ... |
| 94 |
* ] |
| 95 |
* } |
| 96 |
*/ |
| 97 |
|
| 98 |
crypto_secretstream_xchacha20poly1305_keygen(key); |
| 99 |
CHK(crypto_secretstream_xchacha20poly1305_init_push(&state, header, key)); |
| 100 |
sodium_bin2hex(key_hex, sizeof key_hex, key, sizeof key); |
| 101 |
sodium_bin2hex(header_hex, sizeof header_hex, header, sizeof header); |
| 102 |
printf("{\n \"key\": \"%s\",\n \"header\": \"%s\",\n \"chunks\": [\n", |
| 103 |
key_hex, header_hex); |
| 104 |
for (c = 1 ; c <= num_chunks ; ++c) { |
| 105 |
tag = |
| 106 |
c == num_chunks ? crypto_secretstream_xchacha20poly1305_TAG_FINAL |
| 107 |
: rekey ? crypto_secretstream_xchacha20poly1305_TAG_REKEY |
| 108 |
: crypto_secretstream_xchacha20poly1305_TAG_MESSAGE; |
| 109 |
ad_len = randombytes_uniform(MAX_AD_SIZE); |
| 110 |
m_len = randombytes_uniform(MAX_CHUNK_SIZE - 1) + 1; |
| 111 |
randombytes_buf(m, m_len); |
| 112 |
randombytes_buf(ad, ad_len); |
| 113 |
CHK(crypto_secretstream_xchacha20poly1305_push( |
| 114 |
&state, ct, &ct_len, m, m_len, ad, ad_len, tag)); |
| 115 |
sodium_bin2hex(m_hex, m_len * 2 + 1, m, m_len); |
| 116 |
if (ad_len > 0) { |
| 117 |
sodium_bin2hex(ad_hex, ad_len * 2 + 1, ad, ad_len); |
| 118 |
} |
| 119 |
sodium_bin2hex(ct_hex, ct_len * 2 + 1, ct, ct_len); |
| 120 |
printf(" {\n" |
| 121 |
" \"tag\": %d,\n \"ad\": %s%s%s,\n" |
| 122 |
" \"message\": \"%s\",\n \"ciphertext\": \"%s\"\n" |
| 123 |
" }%s\n", |
| 124 |
tag, |
| 125 |
ad_len > 0 ? "\"" : "", |
| 126 |
ad_len > 0 ? ad_hex : "null", |
| 127 |
ad_len > 0 ? "\"" : "", |
| 128 |
m_hex, |
| 129 |
ct_hex, |
| 130 |
c < num_chunks ? "," : ""); |
| 131 |
} |
| 132 |
printf(" ]\n}\n"); |
| 133 |
|
| 134 |
return 0; |
| 135 |
} |