xref: /freebsd/sys/contrib/libsodium/test/default/keygen.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 
2 #define TEST_NAME "keygen"
3 #include "cmptest.h"
4 
5 typedef struct KeygenTV_ {
6     void (*fn)(unsigned char *k);
7     size_t key_len;
8 } KeygenTV;
9 
10 static void
11 tv_keygen(void)
12 {
13     static const KeygenTV tvs[] = {
14         { crypto_auth_keygen, crypto_auth_KEYBYTES },
15         { crypto_auth_hmacsha256_keygen, crypto_auth_hmacsha256_KEYBYTES },
16         { crypto_aead_aes256gcm_keygen, crypto_aead_aes256gcm_KEYBYTES },
17         { crypto_auth_hmacsha512_keygen, crypto_auth_hmacsha512_KEYBYTES },
18         { crypto_auth_hmacsha512256_keygen, crypto_auth_hmacsha512256_KEYBYTES },
19         { crypto_generichash_keygen, crypto_generichash_KEYBYTES },
20         { crypto_generichash_blake2b_keygen, crypto_generichash_blake2b_KEYBYTES },
21         { crypto_kdf_keygen, crypto_kdf_KEYBYTES },
22         { crypto_onetimeauth_keygen, crypto_onetimeauth_KEYBYTES },
23         { crypto_onetimeauth_poly1305_keygen, crypto_onetimeauth_poly1305_KEYBYTES },
24         { crypto_aead_chacha20poly1305_ietf_keygen, crypto_aead_chacha20poly1305_ietf_KEYBYTES },
25         { crypto_aead_chacha20poly1305_keygen, crypto_aead_chacha20poly1305_KEYBYTES },
26         { crypto_aead_chacha20poly1305_ietf_keygen, crypto_aead_chacha20poly1305_ietf_KEYBYTES },
27         { crypto_aead_xchacha20poly1305_ietf_keygen, crypto_aead_xchacha20poly1305_ietf_KEYBYTES },
28         { crypto_secretbox_xsalsa20poly1305_keygen, crypto_secretbox_xsalsa20poly1305_KEYBYTES },
29         { crypto_secretbox_keygen, crypto_secretbox_KEYBYTES },
30         { crypto_secretstream_xchacha20poly1305_keygen, crypto_secretstream_xchacha20poly1305_KEYBYTES },
31         { crypto_shorthash_keygen, crypto_shorthash_KEYBYTES },
32         { crypto_stream_keygen, crypto_stream_KEYBYTES },
33         { crypto_stream_chacha20_keygen, crypto_stream_chacha20_KEYBYTES },
34         { crypto_stream_chacha20_ietf_keygen, crypto_stream_chacha20_ietf_KEYBYTES },
35         { crypto_stream_salsa20_keygen, crypto_stream_salsa20_KEYBYTES },
36         { crypto_stream_xsalsa20_keygen, crypto_stream_xsalsa20_KEYBYTES }
37     };
38     const KeygenTV *tv;
39     unsigned char  *key;
40     size_t          i;
41     int             j;
42 
43     for (i = 0; i < (sizeof tvs) / (sizeof tvs[0]); i++) {
44         tv = &tvs[i];
45         key = (unsigned char *) sodium_malloc(tv->key_len);
46         key[tv->key_len - 1U] = 0;
47         for (j = 0; j < 10000; j++) {
48             tv->fn(key);
49             if (key[tv->key_len - 1U] != 0) {
50                 break;
51             }
52         }
53         sodium_free(key);
54         if (j >= 10000) {
55             printf("Buffer underflow with test vector %u\n", (unsigned int) i);
56         }
57     }
58     printf("tv_keygen: ok\n");
59 }
60 
61 int
62 main(void)
63 {
64     tv_keygen();
65 
66     return 0;
67 }
68