File: auth.c

package info (click to toggle)
libsodium 1.0.20-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 10,088 kB
  • sloc: ansic: 49,467; sh: 5,638; asm: 2,985; makefile: 917; python: 209; cs: 35; xml: 30; pascal: 11
file content (141 lines) | stat: -rw-r--r-- 5,084 bytes parent folder | download | duplicates (9)
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
136
137
138
139
140
141

#define TEST_NAME "auth"
#include "cmptest.h"

/* "Test Case 2" from RFC 4231 */
static unsigned char key[32] = "Jefe";
static unsigned char c[]     = "what do ya want for nothing?";

/* Hacker manifesto */
static unsigned char key2[] =
    "Another one got caught today, it's all over the papers. \"Teenager "
    "Arrested in Computer Crime Scandal\", \"Hacker Arrested after Bank "
    "Tampering\"... Damn kids. They're all alike.";

static unsigned char a[crypto_auth_BYTES];
static unsigned char a2[crypto_auth_hmacsha512_BYTES];
static unsigned char a3[crypto_auth_hmacsha512_BYTES];

int
main(void)
{
    crypto_auth_hmacsha512_state st;
    crypto_auth_hmacsha256_state st256;
    crypto_auth_hmacsha512256_state st512_256;
    size_t                       i;

    assert(crypto_auth_hmacsha512_statebytes() ==
           sizeof(crypto_auth_hmacsha512_state));
    crypto_auth(a, c, sizeof c - 1U, key);
    for (i = 0; i < sizeof a; ++i) {
        printf(",0x%02x", (unsigned int) a[i]);
        if (i % 8 == 7)
            printf("\n");
    }
    printf("\n");

    crypto_auth_hmacsha512_init(&st, key, sizeof key);
    crypto_auth_hmacsha512_update(&st, c, 1U);
    crypto_auth_hmacsha512_update(&st, c, sizeof c - 2U);
    crypto_auth_hmacsha512_final(&st, a2);
    for (i = 0; i < sizeof a2; ++i) {
        printf(",0x%02x", (unsigned int) a2[i]);
        if (i % 8 == 7)
            printf("\n");
    }
    printf("\n");

    crypto_auth_hmacsha512_init(&st, key2, sizeof key2);
    crypto_auth_hmacsha512_update(&st, c, 1U);
    crypto_auth_hmacsha512_update(&st, c, sizeof c - 2U);
    crypto_auth_hmacsha512_final(&st, a2);
    for (i = 0; i < sizeof a2; ++i) {
        printf(",0x%02x", (unsigned int) a2[i]);
        if (i % 8 == 7)
            printf("\n");
    }

    memset(a2, 0, sizeof a2);
    crypto_auth_hmacsha256_init(&st256, key2, sizeof key2);
    crypto_auth_hmacsha256_update(&st256, guard_page, 0U);
    crypto_auth_hmacsha256_update(&st256, c, 1U);
    crypto_auth_hmacsha256_update(&st256, c, sizeof c - 2U);
    crypto_auth_hmacsha256_final(&st256, a2);
    for (i = 0; i < sizeof a2; ++i) {
        printf(",0x%02x", (unsigned int) a2[i]);
        if (i % 8 == 7)
            printf("\n");
    }

    /* Empty message tests: HMAC-SHA512 */
    memset(a2, 0, sizeof a2);
    crypto_auth_hmacsha512_init(&st, key, sizeof key);
    crypto_auth_hmacsha512_final(&st, a2);

    memset(a3, 0, sizeof a3);
    crypto_auth_hmacsha512_init(&st, key, sizeof key);
    crypto_auth_hmacsha512_update(&st, a2, 0U);
    crypto_auth_hmacsha512_final(&st, a3);
    assert(sodium_memcmp(a2, a3, sizeof a2) == 0);

    memset(a3, 0, sizeof a3);
    crypto_auth_hmacsha512_init(&st, key, sizeof key);
    crypto_auth_hmacsha512_update(&st, guard_page, 0U);
    crypto_auth_hmacsha512_final(&st, a3);
    assert(sodium_memcmp(a2, a3, sizeof a2) == 0);

    /* Empty message tests: HMAC-SHA512-256 */
    memset(a2, 0, sizeof a2);
    crypto_auth_hmacsha512256_init(&st512_256, key, sizeof key);
    crypto_auth_hmacsha512256_final(&st512_256, a2);

    memset(a3, 0, sizeof a3);
    crypto_auth_hmacsha512256_init(&st512_256, key, sizeof key);
    crypto_auth_hmacsha512256_update(&st512_256, a2, 0U);
    crypto_auth_hmacsha512256_final(&st512_256, a3);
    assert(sodium_memcmp(a2, a3, sizeof a2) == 0);

    memset(a3, 0, sizeof a3);
    crypto_auth_hmacsha512256_init(&st512_256, key, sizeof key);
    crypto_auth_hmacsha512256_update(&st512_256, guard_page, 0U);
    crypto_auth_hmacsha512256_final(&st512_256, a3);
    assert(sodium_memcmp(a2, a3, sizeof a2) == 0);

    /* Empty message tests: HMAC-SHA256 */

    memset(a2, 0, sizeof a2);
    crypto_auth_hmacsha256_init(&st256, key, sizeof key);
    crypto_auth_hmacsha256_final(&st256, a2);

    memset(a3, 0, sizeof a3);
    crypto_auth_hmacsha256_init(&st256, key, sizeof key);
    crypto_auth_hmacsha256_update(&st256, a2, 0U);
    crypto_auth_hmacsha256_final(&st256, a3);
    assert(sodium_memcmp(a2, a3, sizeof a2) == 0);

    memset(a3, 0, sizeof a3);
    crypto_auth_hmacsha256_init(&st256, key, sizeof key);
    crypto_auth_hmacsha256_update(&st256, guard_page, 0U);
    crypto_auth_hmacsha256_final(&st256, a3);
    assert(sodium_memcmp(a2, a3, sizeof a2) == 0);

    /* --- */

    assert(crypto_auth_bytes() > 0U);
    assert(crypto_auth_keybytes() > 0U);
    assert(strcmp(crypto_auth_primitive(), "hmacsha512256") == 0);
    assert(crypto_auth_hmacsha256_bytes() > 0U);
    assert(crypto_auth_hmacsha256_keybytes() > 0U);
    assert(crypto_auth_hmacsha512_bytes() > 0U);
    assert(crypto_auth_hmacsha512_keybytes() > 0U);
    assert(crypto_auth_hmacsha512256_bytes() == crypto_auth_bytes());
    assert(crypto_auth_hmacsha512256_keybytes() == crypto_auth_keybytes());
    assert(crypto_auth_hmacsha512256_statebytes() >=
           crypto_auth_hmacsha512256_keybytes());
    assert(crypto_auth_hmacsha256_statebytes() ==
           sizeof(crypto_auth_hmacsha256_state));
    assert(crypto_auth_hmacsha512_statebytes() ==
           sizeof(crypto_auth_hmacsha512_state));

    return 0;
}