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

ca16be
ca16be
#define TEST_NAME "auth"
ca16be
#include "cmptest.h"
ca16be
ca16be
/* "Test Case 2" from RFC 4231 */
ca16be
static unsigned char key[32] = "Jefe";
ca16be
static unsigned char c[]     = "what do ya want for nothing?";
ca16be
ca16be
/* Hacker manifesto */
ca16be
static unsigned char key2[] =
ca16be
    "Another one got caught today, it's all over the papers. \"Teenager "
ca16be
    "Arrested in Computer Crime Scandal\", \"Hacker Arrested after Bank "
ca16be
    "Tampering\"... Damn kids. They're all alike.";
ca16be
ca16be
static unsigned char a[crypto_auth_BYTES];
ca16be
static unsigned char a2[crypto_auth_hmacsha512_BYTES];
ca16be
static unsigned char a3[crypto_auth_hmacsha512_BYTES];
ca16be
ca16be
int
ca16be
main(void)
ca16be
{
ca16be
    crypto_auth_hmacsha512_state st;
ca16be
    crypto_auth_hmacsha256_state st256;
ca16be
    crypto_auth_hmacsha512256_state st512_256;
ca16be
    size_t                       i;
ca16be
ca16be
    assert(crypto_auth_hmacsha512_statebytes() ==
ca16be
           sizeof(crypto_auth_hmacsha512_state));
ca16be
    crypto_auth(a, c, sizeof c - 1U, key);
ca16be
    for (i = 0; i < sizeof a; ++i) {
ca16be
        printf(",0x%02x", (unsigned int) a[i]);
ca16be
        if (i % 8 == 7)
ca16be
            printf("\n");
ca16be
    }
ca16be
    printf("\n");
ca16be
ca16be
    crypto_auth_hmacsha512_init(&st, key, sizeof key);
ca16be
    crypto_auth_hmacsha512_update(&st, c, 1U);
ca16be
    crypto_auth_hmacsha512_update(&st, c, sizeof c - 2U);
ca16be
    crypto_auth_hmacsha512_final(&st, a2);
ca16be
    for (i = 0; i < sizeof a2; ++i) {
ca16be
        printf(",0x%02x", (unsigned int) a2[i]);
ca16be
        if (i % 8 == 7)
ca16be
            printf("\n");
ca16be
    }
ca16be
    printf("\n");
ca16be
ca16be
    crypto_auth_hmacsha512_init(&st, key2, sizeof key2);
ca16be
    crypto_auth_hmacsha512_update(&st, c, 1U);
ca16be
    crypto_auth_hmacsha512_update(&st, c, sizeof c - 2U);
ca16be
    crypto_auth_hmacsha512_final(&st, a2);
ca16be
    for (i = 0; i < sizeof a2; ++i) {
ca16be
        printf(",0x%02x", (unsigned int) a2[i]);
ca16be
        if (i % 8 == 7)
ca16be
            printf("\n");
ca16be
    }
ca16be
ca16be
    memset(a2, 0, sizeof a2);
ca16be
    crypto_auth_hmacsha256_init(&st256, key2, sizeof key2);
ca16be
    crypto_auth_hmacsha256_update(&st256, guard_page, 0U);
ca16be
    crypto_auth_hmacsha256_update(&st256, c, 1U);
ca16be
    crypto_auth_hmacsha256_update(&st256, c, sizeof c - 2U);
ca16be
    crypto_auth_hmacsha256_final(&st256, a2);
ca16be
    for (i = 0; i < sizeof a2; ++i) {
ca16be
        printf(",0x%02x", (unsigned int) a2[i]);
ca16be
        if (i % 8 == 7)
ca16be
            printf("\n");
ca16be
    }
ca16be
ca16be
    /* Empty message tests: HMAC-SHA512 */
ca16be
    memset(a2, 0, sizeof a2);
ca16be
    crypto_auth_hmacsha512_init(&st, key, sizeof key);
ca16be
    crypto_auth_hmacsha512_final(&st, a2);
ca16be
ca16be
    memset(a3, 0, sizeof a3);
ca16be
    crypto_auth_hmacsha512_init(&st, key, sizeof key);
ca16be
    crypto_auth_hmacsha512_update(&st, a2, 0U);
ca16be
    crypto_auth_hmacsha512_final(&st, a3);
ca16be
    assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
ca16be
ca16be
    memset(a3, 0, sizeof a3);
ca16be
    crypto_auth_hmacsha512_init(&st, key, sizeof key);
ca16be
    crypto_auth_hmacsha512_update(&st, guard_page, 0U);
ca16be
    crypto_auth_hmacsha512_final(&st, a3);
ca16be
    assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
ca16be
ca16be
    /* Empty message tests: HMAC-SHA512-256 */
ca16be
    memset(a2, 0, sizeof a2);
ca16be
    crypto_auth_hmacsha512256_init(&st512_256, key, sizeof key);
ca16be
    crypto_auth_hmacsha512256_final(&st512_256, a2);
ca16be
ca16be
    memset(a3, 0, sizeof a3);
ca16be
    crypto_auth_hmacsha512256_init(&st512_256, key, sizeof key);
ca16be
    crypto_auth_hmacsha512256_update(&st512_256, a2, 0U);
ca16be
    crypto_auth_hmacsha512256_final(&st512_256, a3);
ca16be
    assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
ca16be
ca16be
    memset(a3, 0, sizeof a3);
ca16be
    crypto_auth_hmacsha512256_init(&st512_256, key, sizeof key);
ca16be
    crypto_auth_hmacsha512256_update(&st512_256, guard_page, 0U);
ca16be
    crypto_auth_hmacsha512256_final(&st512_256, a3);
ca16be
    assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
ca16be
ca16be
    /* Empty message tests: HMAC-SHA256 */
ca16be
ca16be
    memset(a2, 0, sizeof a2);
ca16be
    crypto_auth_hmacsha256_init(&st256, key, sizeof key);
ca16be
    crypto_auth_hmacsha256_final(&st256, a2);
ca16be
ca16be
    memset(a3, 0, sizeof a3);
ca16be
    crypto_auth_hmacsha256_init(&st256, key, sizeof key);
ca16be
    crypto_auth_hmacsha256_update(&st256, a2, 0U);
ca16be
    crypto_auth_hmacsha256_final(&st256, a3);
ca16be
    assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
ca16be
ca16be
    memset(a3, 0, sizeof a3);
ca16be
    crypto_auth_hmacsha256_init(&st256, key, sizeof key);
ca16be
    crypto_auth_hmacsha256_update(&st256, guard_page, 0U);
ca16be
    crypto_auth_hmacsha256_final(&st256, a3);
ca16be
    assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
ca16be
ca16be
    /* --- */
ca16be
ca16be
    assert(crypto_auth_bytes() > 0U);
ca16be
    assert(crypto_auth_keybytes() > 0U);
ca16be
    assert(strcmp(crypto_auth_primitive(), "hmacsha512256") == 0);
ca16be
    assert(crypto_auth_hmacsha256_bytes() > 0U);
ca16be
    assert(crypto_auth_hmacsha256_keybytes() > 0U);
ca16be
    assert(crypto_auth_hmacsha512_bytes() > 0U);
ca16be
    assert(crypto_auth_hmacsha512_keybytes() > 0U);
ca16be
    assert(crypto_auth_hmacsha512256_bytes() == crypto_auth_bytes());
ca16be
    assert(crypto_auth_hmacsha512256_keybytes() == crypto_auth_keybytes());
ca16be
    assert(crypto_auth_hmacsha512256_statebytes() >=
ca16be
           crypto_auth_hmacsha512256_keybytes());
ca16be
    assert(crypto_auth_hmacsha256_statebytes() ==
ca16be
           sizeof(crypto_auth_hmacsha256_state));
ca16be
    assert(crypto_auth_hmacsha512_statebytes() ==
ca16be
           sizeof(crypto_auth_hmacsha512_state));
ca16be
ca16be
    return 0;
ca16be
}