Blame SOURCES/libsodium-1.0.18/test/default/codecs.c

ca16be
#define TEST_NAME "codecs"
ca16be
#include "cmptest.h"
ca16be
ca16be
int
ca16be
main(void)
ca16be
{
ca16be
    unsigned char  buf1[1000];
ca16be
    char           buf3[33];
ca16be
    unsigned char  buf4[4];
ca16be
    const char    *b64;
ca16be
    char          *b64_;
ca16be
    const char    *b64_end;
ca16be
    unsigned char *bin;
ca16be
    const char    *hex;
ca16be
    const char    *hex_end;
ca16be
    size_t         b64_len;
ca16be
    size_t         bin_len;
ca16be
    unsigned int   i;
ca16be
ca16be
    printf("%s\n",
ca16be
           sodium_bin2hex(buf3, 33U, (const unsigned char *) "0123456789ABCDEF",
ca16be
                          16U));
ca16be
    printf("bin2hex(..., guard_page, 0):%s\n",
ca16be
           sodium_bin2hex(buf3, sizeof buf3, guard_page, 0U));
ca16be
    printf("bin2hex(..., \"\", 0):%s\n",
ca16be
           sodium_bin2hex(buf3, sizeof buf3, (const unsigned char *) "", 0U));
ca16be
ca16be
    hex = "Cafe : 6942";
ca16be
    sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len,
ca16be
                   &hex_end);
ca16be
    printf("%lu:%02x%02x%02x%02x\n", (unsigned long) bin_len,
ca16be
           buf4[0], buf4[1], buf4[2], buf4[3]);
ca16be
    printf("dt1: %ld\n", (long) (hex_end - hex));
ca16be
ca16be
    hex = "Cafe : 6942";
ca16be
    sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len, NULL);
ca16be
    printf("%lu:%02x%02x%02x%02x\n", (unsigned long) bin_len,
ca16be
           buf4[0], buf4[1], buf4[2], buf4[3]);
ca16be
ca16be
    hex = "deadbeef";
ca16be
    if (sodium_hex2bin(buf1, 1U, hex, 8U, NULL, &bin_len, &hex_end) != -1) {
ca16be
        printf("sodium_hex2bin() overflow not detected\n");
ca16be
    }
ca16be
    printf("dt2: %ld\n", (long) (hex_end - hex));
ca16be
ca16be
    hex = "de:ad:be:eff";
ca16be
    if (sodium_hex2bin(buf1, 4U, hex, 12U, ":", &bin_len, &hex_end) != -1) {
ca16be
        printf(
ca16be
            "sodium_hex2bin() with an odd input length and a short output "
ca16be
            "buffer\n");
ca16be
    }
ca16be
    printf("dt3: %ld\n", (long) (hex_end - hex));
ca16be
ca16be
    hex = "de:ad:be:eff";
ca16be
    if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
ca16be
                       &bin_len, &hex_end) != -1) {
ca16be
        printf("sodium_hex2bin() with an odd input length\n");
ca16be
    }
ca16be
    printf("dt4: %ld\n", (long) (hex_end - hex));
ca16be
ca16be
    hex = "de:ad:be:eff";
ca16be
    if (sodium_hex2bin(buf1, sizeof buf1, hex, 13U, ":",
ca16be
                       &bin_len, &hex_end) != -1) {
ca16be
        printf("sodium_hex2bin() with an odd input length (2)\n");
ca16be
    }
ca16be
    printf("dt5: %ld\n", (long) (hex_end - hex));
ca16be
ca16be
    hex = "de:ad:be:eff";
ca16be
    if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
ca16be
                       &bin_len, NULL) != -1) {
ca16be
        printf("sodium_hex2bin() with an odd input length and no end pointer\n");
ca16be
    }
ca16be
ca16be
    hex = "de:ad:be:ef*";
ca16be
    if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
ca16be
                       &bin_len, &hex_end) != 0) {
ca16be
        printf("sodium_hex2bin() with an extra character and an end pointer\n");
ca16be
    }
ca16be
    printf("dt6: %ld\n", (long) (hex_end - hex));
ca16be
ca16be
    hex = "de:ad:be:ef*";
ca16be
    if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":",
ca16be
                       &bin_len, NULL) != -1) {
ca16be
        printf("sodium_hex2bin() with an extra character and no end pointer\n");
ca16be
    }
ca16be
ca16be
    assert(sodium_hex2bin(buf4, sizeof buf4, (const char *) guard_page, 0U,
ca16be
                          NULL, &bin_len, NULL) == 0);
ca16be
    assert(bin_len == 0);
ca16be
ca16be
    assert(sodium_hex2bin(buf4, sizeof buf4, "", 0U, NULL, &bin_len, NULL) == 0);
ca16be
    assert(bin_len == 0);
ca16be
ca16be
    printf("%s\n",
ca16be
           sodium_bin2base64(buf3, 31U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFab",
ca16be
                             21U, sodium_base64_VARIANT_ORIGINAL));
ca16be
    printf("%s\n",
ca16be
           sodium_bin2base64(buf3, 33U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFabc",
ca16be
                             22U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
ca16be
    printf("%s\n",
ca16be
           sodium_bin2base64(buf3, 31U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFab",
ca16be
                             21U, sodium_base64_VARIANT_URLSAFE));
ca16be
    printf("%s\n",
ca16be
           sodium_bin2base64(buf3, 33U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFabc",
ca16be
                             22U, sodium_base64_VARIANT_URLSAFE_NO_PADDING));
ca16be
    printf("%s\n",
ca16be
           sodium_bin2base64(buf3, 1U, guard_page,
ca16be
                             0U, sodium_base64_VARIANT_ORIGINAL));
ca16be
    printf("%s\n",
ca16be
           sodium_bin2base64(buf3, 5U, (const unsigned char *) "a",
ca16be
                             1U, sodium_base64_VARIANT_ORIGINAL));
ca16be
    printf("%s\n",
ca16be
           sodium_bin2base64(buf3, 5U, (const unsigned char *) "ab",
ca16be
                             2U, sodium_base64_VARIANT_ORIGINAL));
ca16be
    printf("%s\n",
ca16be
           sodium_bin2base64(buf3, 5U, (const unsigned char *) "abc",
ca16be
                             3U, sodium_base64_VARIANT_ORIGINAL));
ca16be
    printf("%s\n",
ca16be
           sodium_bin2base64(buf3, 1U, guard_page,
ca16be
                             0U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
ca16be
    printf("%s\n",
ca16be
           sodium_bin2base64(buf3, 3U, (const unsigned char *) "a",
ca16be
                             1U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
ca16be
    printf("%s\n",
ca16be
           sodium_bin2base64(buf3, 4U, (const unsigned char *) "ab",
ca16be
                             2U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
ca16be
    printf("%s\n",
ca16be
           sodium_bin2base64(buf3, 5U, (const unsigned char *) "abc",
ca16be
                             3U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
ca16be
ca16be
    b64 = "VGhpcyBpcyBhIGpvdXJu" "\n" "ZXkgaW50by" " " "Bzb3VuZA==";
ca16be
    memset(buf4, '*', sizeof buf4);
ca16be
    assert(sodium_base642bin(buf4, sizeof buf4, b64, strlen(b64), "\n\r ", &bin_len,
ca16be
                             &b64_end, sodium_base64_VARIANT_ORIGINAL) == -1);
ca16be
    buf4[bin_len] = 0;
ca16be
    printf("[%s]\n", (const char *) buf4);
ca16be
    printf("[%s]\n", b64_end);
ca16be
ca16be
    memset(buf1, '*', sizeof buf1);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), "\n\r ", &bin_len,
ca16be
                             &b64_end, sodium_base64_VARIANT_ORIGINAL) == 0);
ca16be
    buf1[bin_len] = 0;
ca16be
    printf("[%s]\n", (const char *) buf1);
ca16be
    assert(*b64_end == 0);
ca16be
ca16be
    memset(buf1, '*', sizeof buf1);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, &bin_len,
ca16be
                             &b64_end, sodium_base64_VARIANT_ORIGINAL) == 0);
ca16be
    buf1[bin_len] = 0;
ca16be
    printf("[%s]\n", (const char *) buf1);
ca16be
    printf("[%s]\n", b64_end);
ca16be
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
ca16be
                             &b64_end, sodium_base64_VARIANT_ORIGINAL) == 0);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
ca16be
                             &b64_end, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
ca16be
                             &b64_end, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
ca16be
                             &b64_end, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == 0);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
ca16be
                             &b64_end, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == 0);
ca16be
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
ca16be
                             NULL, sodium_base64_VARIANT_ORIGINAL) == -1);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
ca16be
                             NULL, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
ca16be
                             NULL, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
ca16be
                             NULL, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == -1);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
ca16be
                             NULL, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == -1);
ca16be
ca16be
    assert(sodium_base642bin(guard_page, (size_t) 10U, "a=", (size_t) 2U, NULL, NULL, NULL,
ca16be
                             sodium_base64_VARIANT_URLSAFE) == -1);
ca16be
    assert(sodium_base642bin(guard_page, (size_t) 10U, "a*", (size_t) 2U, NULL, NULL, NULL,
ca16be
                             sodium_base64_VARIANT_URLSAFE) == -1);
ca16be
    assert(sodium_base642bin(guard_page, (size_t) 10U, "a*", (size_t) 2U, "~", NULL, NULL,
ca16be
                             sodium_base64_VARIANT_URLSAFE) == -1);
ca16be
    assert(sodium_base642bin(guard_page, (size_t) 10U, "a*", (size_t) 2U, "*", NULL, NULL,
ca16be
                             sodium_base64_VARIANT_URLSAFE) == -1);
ca16be
    assert(sodium_base642bin(guard_page, (size_t) 10U, "a==", (size_t) 3U, NULL, NULL, NULL,
ca16be
                             sodium_base64_VARIANT_URLSAFE) == -1);
ca16be
    assert(sodium_base642bin(guard_page, (size_t) 10U, "a=*", (size_t) 3U, NULL, NULL, NULL,
ca16be
                             sodium_base64_VARIANT_URLSAFE) == -1);
ca16be
    assert(sodium_base642bin(guard_page, (size_t) 10U, "a=*", (size_t) 3U, "~", NULL, NULL,
ca16be
                             sodium_base64_VARIANT_URLSAFE) == -1);
ca16be
    assert(sodium_base642bin(guard_page, (size_t) 10U, "a=*", (size_t) 3U, "*", NULL, NULL,
ca16be
                             sodium_base64_VARIANT_URLSAFE) == -1);
ca16be
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, "O1R", (size_t) 3U, NULL, NULL, NULL,
ca16be
                             sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, "O1Q", (size_t) 3U, NULL, NULL, NULL,
ca16be
                             sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, "O1", (size_t) 2U, NULL, NULL, NULL,
ca16be
                             sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, "Ow", (size_t) 2U, NULL, NULL, NULL,
ca16be
                             sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, "O", (size_t) 1U, NULL, NULL, NULL,
ca16be
                             sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == -1);
ca16be
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, "kaw", (size_t) 3U, NULL, NULL, NULL,
ca16be
                             sodium_base64_VARIANT_ORIGINAL) == -1);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, "kQ*", (size_t) 3U, "@", NULL, NULL,
ca16be
                             sodium_base64_VARIANT_ORIGINAL) == -1);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, "kQ*", (size_t) 3U, "*", NULL, NULL,
ca16be
                             sodium_base64_VARIANT_ORIGINAL) == -1);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, "kaw=**", (size_t) 6U, "*", NULL, NULL,
ca16be
                             sodium_base64_VARIANT_ORIGINAL) == 0);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, "kaw*=*", (size_t) 6U, "~*", NULL, NULL,
ca16be
                             sodium_base64_VARIANT_ORIGINAL) == 0);
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, "ka*w*=*", (size_t) 7U, "*~", NULL, NULL,
ca16be
                             sodium_base64_VARIANT_ORIGINAL) == 0);
ca16be
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, (const char *) guard_page, 0U,
ca16be
                             NULL, &bin_len, NULL, sodium_base64_VARIANT_ORIGINAL) == 0);
ca16be
    assert(bin_len == 0);
ca16be
ca16be
    assert(sodium_base642bin(buf1, sizeof buf1, "", 0U, NULL, &bin_len, NULL,
ca16be
                             sodium_base64_VARIANT_ORIGINAL) == 0);
ca16be
    assert(bin_len == 0);
ca16be
ca16be
    for (i = 0; i < 1000; i++) {
ca16be
        assert(sizeof buf1 >= 100);
ca16be
        bin_len = (size_t) randombytes_uniform(100);
ca16be
        bin = (unsigned char *) sodium_malloc(bin_len);
ca16be
        b64_len = (bin_len + 2U) / 3U * 4U + 1U;
ca16be
        assert(b64_len == sodium_base64_encoded_len(bin_len, sodium_base64_VARIANT_URLSAFE));
ca16be
        b64_ = (char *) sodium_malloc(b64_len);
ca16be
        randombytes_buf(bin, bin_len);
ca16be
        memcpy(buf1, bin, bin_len);
ca16be
        b64 = sodium_bin2base64(b64_, b64_len, bin, bin_len,
ca16be
                                sodium_base64_VARIANT_URLSAFE);
ca16be
        assert(b64 != NULL);
ca16be
        assert(sodium_base642bin(bin, bin_len + 10, b64, b64_len,
ca16be
                                 NULL, NULL, &b64_end,
ca16be
                                 sodium_base64_VARIANT_URLSAFE) == 0);
ca16be
        assert(b64_end == &b64[b64_len - 1]);
ca16be
        assert(memcmp(bin, buf1, bin_len) == 0);
ca16be
        sodium_free(bin);
ca16be
        sodium_free(b64_);
ca16be
    }
ca16be
    return 0;
ca16be
}