File: secretstream_test_vector.c

package info (click to toggle)
python-nacl 1.5.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,776 kB
  • sloc: ansic: 45,889; python: 7,249; sh: 6,752; asm: 2,974; makefile: 1,011; cs: 35; xml: 30; pascal: 11
file content (135 lines) | stat: -rw-r--r-- 4,500 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright 2018 Donald Stufft and individual contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Test vector generator/checker for libsodium's crypto_secretstream APIs
 * to build in a unix-like environment, use a command line like
 * $ cc secretstream_test_vector.c \
 *     -I${IPATH} -L${LPATH} -lsodium \
 *     -o secretstream_test_vector
 * with IPATH and LPATH defined to respectively point to libsodium's include path
 * and to the directory containing the link library libsodium.a or libsodium.o
 *
 */
#include <stdio.h>
#include <string.h>
#include <sodium.h>
#include <unistd.h>

#define MAX_AD_SIZE 32
#define MAX_CHUNK_SIZE 512
#define CHK(cmd) \
    do { if ((rc = (cmd)) != 0) { \
        fprintf(stderr, "api call failed, code=%d", rc); \
        exit(1); \
    }} while(0)

int usage(int argc, char **argv) {
    fprintf(stderr, "Usage: %s [-c num_chunks] [-r]\n", argv[0]);
    return 1;
}

int main (int argc, char **argv) {
    int c, rc;
    int num_chunks = 1;
    int rekey = 0;

    crypto_secretstream_xchacha20poly1305_state state;
    unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES];
    unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES];
    unsigned char m[MAX_CHUNK_SIZE];
    unsigned char ad[MAX_AD_SIZE];
    unsigned char ct[MAX_CHUNK_SIZE + crypto_secretstream_xchacha20poly1305_ABYTES];
    char key_hex[sizeof(key) * 2 + 1];
    char header_hex[sizeof(header) * 2 + 1];
    char m_hex[sizeof(m) * 2 + 1];
    char ad_hex[sizeof(ad) * 2 + 1];
    char ct_hex[sizeof(ct) * 2 + 1];
    unsigned long long m_len, ad_len, ct_len;
    unsigned char tag;

    while ((c = getopt(argc, argv, "hc:r")) != -1) {
        switch (c) {
        case 'c':
            num_chunks = atoi(optarg);
            break;
        case 'r':
            rekey = 1;
            break;
        case 'h':
            return usage(argc, argv);
        default:
            return 1;
        }
    }
    if (optind < argc) return usage(argc, argv);

    if (sodium_init() == -1) {
        exit(1);
    }

    /* output format:
     * {
     *   "key": "hex",
     *   "header": "hex",
     *   "chunks": [
     *     {
     *       "tag": 0,
     *       "ad": "hex",
     *       "message": "hex",
     *       "ciphertext": "hex"
     *     },
     *     ...
     *   ]
     * }
     */

    crypto_secretstream_xchacha20poly1305_keygen(key);
    CHK(crypto_secretstream_xchacha20poly1305_init_push(&state, header, key));
    sodium_bin2hex(key_hex, sizeof key_hex, key, sizeof key);
    sodium_bin2hex(header_hex, sizeof header_hex, header, sizeof header);
    printf("{\n  \"key\": \"%s\",\n  \"header\": \"%s\",\n  \"chunks\": [\n",
           key_hex, header_hex);
    for (c = 1 ; c <= num_chunks ; ++c) {
        tag =
            c == num_chunks ? crypto_secretstream_xchacha20poly1305_TAG_FINAL
            : rekey ? crypto_secretstream_xchacha20poly1305_TAG_REKEY
            : crypto_secretstream_xchacha20poly1305_TAG_MESSAGE;
        ad_len = randombytes_uniform(MAX_AD_SIZE);
        m_len = randombytes_uniform(MAX_CHUNK_SIZE - 1) + 1;
        randombytes_buf(m, m_len);
        randombytes_buf(ad, ad_len);
        CHK(crypto_secretstream_xchacha20poly1305_push(
            &state, ct, &ct_len, m, m_len, ad, ad_len, tag));
        sodium_bin2hex(m_hex, m_len * 2 + 1, m, m_len);
        if (ad_len > 0) {
            sodium_bin2hex(ad_hex, ad_len * 2 + 1, ad, ad_len);
        }
        sodium_bin2hex(ct_hex, ct_len * 2 + 1, ct, ct_len);
        printf("    {\n"
               "      \"tag\": %d,\n      \"ad\": %s%s%s,\n"
               "      \"message\": \"%s\",\n      \"ciphertext\": \"%s\"\n"
               "    }%s\n",
               tag,
               ad_len > 0 ? "\"" : "",
               ad_len > 0 ? ad_hex : "null",
               ad_len > 0 ? "\"" : "",
               m_hex,
               ct_hex,
               c < num_chunks ? "," : "");
    }
    printf("  ]\n}\n");

    return 0;
}