|
| 1 | +/* LibTomCrypt, modular cryptographic library -- Tom St Denis */ |
| 2 | +/* SPDX-License-Identifier: Unlicense */ |
| 3 | + |
| 4 | +/* Interoperability test between libtomcrypt and libsodium for: |
| 5 | + 1. secretbox (XSalsa20-Poly1305) |
| 6 | + 2. cryptobox (X25519 + HSalsa20 + XSalsa20-Poly1305) |
| 7 | + 3. sealbox (ephemeral X25519 + BLAKE2b nonce + cryptobox) |
| 8 | +
|
| 9 | + Build (run from the repo root, after `make`): |
| 10 | + cc -Isrc/headers -o demos/libsodium-interop demos/libsodium-interop.c libtomcrypt.a -lsodium |
| 11 | +
|
| 12 | + Run: |
| 13 | + ./demos/libsodium-interop |
| 14 | +*/ |
| 15 | + |
| 16 | +#include <stdio.h> |
| 17 | +#include <string.h> |
| 18 | +#include <tomcrypt.h> |
| 19 | +#include <sodium.h> |
| 20 | + |
| 21 | +#define CHECK(e) do { if ((e) != 0) { fprintf(stderr, "FAIL line %d\n", __LINE__); return 1; } } while(0) |
| 22 | + |
| 23 | +static int test_secretbox(void) |
| 24 | +{ |
| 25 | + const unsigned char key[32] = { |
| 26 | + 0x1b,0x27,0x55,0x64,0x73,0xe9,0x85,0xd4,0x62,0xcd,0x51,0x19,0x7a,0x9a,0x46,0xc7, |
| 27 | + 0x60,0x09,0x54,0x9e,0xac,0x64,0x74,0xf2,0x06,0xc4,0xee,0x08,0x44,0xf6,0x83,0x89 |
| 28 | + }; |
| 29 | + const unsigned char nonce[24] = { |
| 30 | + 0x69,0x69,0x6e,0xe9,0x55,0xb6,0x2b,0x73,0xcd,0x62,0xbd,0xa8,0x75,0xfc,0x73,0xd6, |
| 31 | + 0x82,0x19,0xe0,0x03,0x6b,0x7a,0x0b,0x37 |
| 32 | + }; |
| 33 | + const char *msg = "Hello from the interop test!"; |
| 34 | + unsigned long msglen = strlen(msg); |
| 35 | + unsigned char sbox[256], lbox[256], dec[256]; |
| 36 | + unsigned long boxlen, declen; |
| 37 | + |
| 38 | + printf(" sodium create -> ltc open ... "); |
| 39 | + CHECK(crypto_secretbox_easy(sbox, (const unsigned char *)msg, msglen, nonce, key)); |
| 40 | + declen = sizeof(dec); |
| 41 | + CHECK(ltc_secretbox_open(sbox, msglen + 16, nonce, 24, key, 32, dec, &declen)); |
| 42 | + CHECK(declen != msglen); |
| 43 | + CHECK(memcmp(dec, msg, msglen)); |
| 44 | + printf("OK\n"); |
| 45 | + printf(" ltc create -> sodium open ... "); |
| 46 | + boxlen = sizeof(lbox); |
| 47 | + CHECK(ltc_secretbox_create((const unsigned char *)msg, msglen, nonce, 24, key, 32, lbox, &boxlen)); |
| 48 | + CHECK(boxlen != msglen + 16); |
| 49 | + CHECK(crypto_secretbox_open_easy(dec, lbox, boxlen, nonce, key)); |
| 50 | + CHECK(memcmp(dec, msg, msglen)); |
| 51 | + printf("OK\n"); |
| 52 | + printf(" identical ciphertext ... "); |
| 53 | + CHECK(memcmp(sbox, lbox, msglen + 16)); |
| 54 | + printf("OK\n"); |
| 55 | + return 0; |
| 56 | +} |
| 57 | + |
| 58 | +static int test_cryptobox(void) |
| 59 | +{ |
| 60 | + unsigned char alice_pk[32], alice_sk[32]; |
| 61 | + unsigned char bob_pk[32], bob_sk[32]; |
| 62 | + const unsigned char nonce[24] = { |
| 63 | + 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c, |
| 64 | + 0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18 |
| 65 | + }; |
| 66 | + const char *msg = "Testing crypto_box interop between libtomcrypt and libsodium."; |
| 67 | + unsigned long msglen = strlen(msg); |
| 68 | + unsigned char sbox[256], lbox[256], dec[256]; |
| 69 | + unsigned long boxlen, declen; |
| 70 | + |
| 71 | + crypto_box_keypair(alice_pk, alice_sk); |
| 72 | + crypto_box_keypair(bob_pk, bob_sk); |
| 73 | + printf(" sodium create -> ltc open ... "); |
| 74 | + CHECK(crypto_box_easy(sbox, (const unsigned char *)msg, msglen, nonce, bob_pk, alice_sk)); |
| 75 | + declen = sizeof(dec); |
| 76 | + CHECK(ltc_cryptobox_open(sbox, msglen + 16, nonce, 24, alice_pk, 32, bob_sk, 32, dec, &declen)); |
| 77 | + CHECK(declen != msglen); |
| 78 | + CHECK(memcmp(dec, msg, msglen)); |
| 79 | + printf("OK\n"); |
| 80 | + printf(" ltc create -> sodium open ... "); |
| 81 | + boxlen = sizeof(lbox); |
| 82 | + CHECK(ltc_cryptobox_create((const unsigned char *)msg, msglen, nonce, 24, bob_pk, 32, alice_sk, 32, lbox, &boxlen)); |
| 83 | + CHECK(boxlen != msglen + 16); |
| 84 | + CHECK(crypto_box_open_easy(dec, lbox, boxlen, nonce, alice_pk, bob_sk)); |
| 85 | + CHECK(memcmp(dec, msg, msglen)); |
| 86 | + printf("OK\n"); |
| 87 | + printf(" identical ciphertext ... "); |
| 88 | + CHECK(memcmp(sbox, lbox, msglen + 16)); |
| 89 | + printf("OK\n"); |
| 90 | + return 0; |
| 91 | +} |
| 92 | + |
| 93 | +static int test_sealbox(void) |
| 94 | +{ |
| 95 | + unsigned char pk[32], sk[32]; |
| 96 | + const char *msg = "Sealed box: anonymous public-key authenticated encryption!"; |
| 97 | + unsigned long msglen = strlen(msg); |
| 98 | + unsigned long sealedlen_expected = msglen + 48; /* 32 eph_pk + 16 tag */ |
| 99 | + unsigned char sealed[256], dec[256]; |
| 100 | + unsigned long sealedlen, declen; |
| 101 | + prng_state prng; |
| 102 | + |
| 103 | + crypto_box_keypair(pk, sk); |
| 104 | + printf(" sodium create -> ltc open ... "); |
| 105 | + CHECK(crypto_box_seal(sealed, (const unsigned char *)msg, msglen, pk)); |
| 106 | + declen = sizeof(dec); |
| 107 | + CHECK(ltc_sealedbox_open(sealed, sealedlen_expected, sk, 32, dec, &declen)); |
| 108 | + CHECK(declen != msglen); |
| 109 | + CHECK(memcmp(dec, msg, msglen)); |
| 110 | + printf("OK\n"); |
| 111 | + printf(" ltc create -> sodium open ... "); |
| 112 | + sealedlen = sizeof(sealed); |
| 113 | + CHECK(ltc_sealedbox_create((const unsigned char *)msg, msglen, pk, 32, &prng, find_prng("sprng"), sealed, &sealedlen)); |
| 114 | + CHECK(sealedlen != sealedlen_expected); |
| 115 | + CHECK(crypto_box_seal_open(dec, sealed, sealedlen, pk, sk)); |
| 116 | + CHECK(memcmp(dec, msg, msglen)); |
| 117 | + printf("OK\n"); |
| 118 | + return 0; |
| 119 | +} |
| 120 | + |
| 121 | +static int test_secretbox_ck(void) |
| 122 | +{ |
| 123 | + /* Demonstrates the keyed (_ck) cryptobox variants and the size-query path. */ |
| 124 | + unsigned char alice_pk_raw[32], alice_sk_raw[32]; |
| 125 | + unsigned char bob_pk_raw[32], bob_sk_raw[32]; |
| 126 | + curve25519_key alice_pk, alice_sk, bob_pk, bob_sk; |
| 127 | + const unsigned char nonce[24] = { |
| 128 | + 0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x00,0x11,0x22,0x33,0x44,0x55, |
| 129 | + 0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0x00,0x11 |
| 130 | + }; |
| 131 | + const char *msg = "_ck variants accept already-imported curve25519_key objects."; |
| 132 | + unsigned long msglen = strlen(msg); |
| 133 | + unsigned char sbox[256], lbox[256], dec[256]; |
| 134 | + unsigned long boxlen, declen, need; |
| 135 | + |
| 136 | + crypto_box_keypair(alice_pk_raw, alice_sk_raw); |
| 137 | + crypto_box_keypair(bob_pk_raw, bob_sk_raw); |
| 138 | + CHECK(x25519_import_raw(alice_pk_raw, 32, PK_PUBLIC, &alice_pk)); |
| 139 | + CHECK(x25519_import_raw(alice_sk_raw, 32, PK_PRIVATE, &alice_sk)); |
| 140 | + CHECK(x25519_import_raw(bob_pk_raw, 32, PK_PUBLIC, &bob_pk)); |
| 141 | + CHECK(x25519_import_raw(bob_sk_raw, 32, PK_PRIVATE, &bob_sk)); |
| 142 | + |
| 143 | + printf(" size-query (out=NULL) returns required length ... "); |
| 144 | + need = 0; |
| 145 | + if (ltc_cryptobox_create_ck((const unsigned char *)msg, msglen, nonce, 24, &bob_pk, &alice_sk, NULL, &need) != CRYPT_BUFFER_OVERFLOW) { |
| 146 | + fprintf(stderr, "FAIL line %d\n", __LINE__); |
| 147 | + return 1; |
| 148 | + } |
| 149 | + CHECK(need != msglen + 16); |
| 150 | + printf("OK\n"); |
| 151 | + |
| 152 | + printf(" sodium create -> ltc_cryptobox_open_ck ... "); |
| 153 | + CHECK(crypto_box_easy(sbox, (const unsigned char *)msg, msglen, nonce, bob_pk_raw, alice_sk_raw)); |
| 154 | + declen = sizeof(dec); |
| 155 | + CHECK(ltc_cryptobox_open_ck(sbox, msglen + 16, nonce, 24, &alice_pk, &bob_sk, dec, &declen)); |
| 156 | + CHECK(declen != msglen); |
| 157 | + CHECK(memcmp(dec, msg, msglen)); |
| 158 | + printf("OK\n"); |
| 159 | + |
| 160 | + printf(" ltc_cryptobox_create_ck -> sodium open ... "); |
| 161 | + boxlen = sizeof(lbox); |
| 162 | + CHECK(ltc_cryptobox_create_ck((const unsigned char *)msg, msglen, nonce, 24, &bob_pk, &alice_sk, lbox, &boxlen)); |
| 163 | + CHECK(boxlen != msglen + 16); |
| 164 | + CHECK(crypto_box_open_easy(dec, lbox, boxlen, nonce, alice_pk_raw, bob_sk_raw)); |
| 165 | + CHECK(memcmp(dec, msg, msglen)); |
| 166 | + printf("OK\n"); |
| 167 | + |
| 168 | + printf(" identical ciphertext ... "); |
| 169 | + CHECK(memcmp(sbox, lbox, msglen + 16)); |
| 170 | + printf("OK\n"); |
| 171 | + return 0; |
| 172 | +} |
| 173 | + |
| 174 | +int main(void) |
| 175 | +{ |
| 176 | + if (sodium_init() < 0) { |
| 177 | + fprintf(stderr, "sodium_init() failed\n"); |
| 178 | + return 1; |
| 179 | + } |
| 180 | + if (register_prng(&sprng_desc) == -1) { |
| 181 | + fprintf(stderr, "register_prng(sprng) failed\n"); |
| 182 | + return 1; |
| 183 | + } |
| 184 | + printf("Test 1: secretbox (XSalsa20-Poly1305)\n"); |
| 185 | + if (test_secretbox() != 0) return 1; |
| 186 | + printf("Test 2: cryptobox (X25519 + HSalsa20 + secretbox)\n"); |
| 187 | + if (test_cryptobox() != 0) return 1; |
| 188 | + printf("Test 3: sealbox (ephemeral X25519 + BLAKE2b + cryptobox)\n"); |
| 189 | + if (test_sealbox() != 0) return 1; |
| 190 | + printf("Test 4: cryptobox _ck variants and size-query path\n"); |
| 191 | + if (test_secretbox_ck() != 0) return 1; |
| 192 | + printf("All tests passed.\n"); |
| 193 | + return 0; |
| 194 | +} |
0 commit comments