File: test_scram.c

package info (click to toggle)
libstrophe 0.14.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,356 kB
  • sloc: ansic: 16,695; makefile: 301; sh: 61
file content (216 lines) | stat: -rw-r--r-- 6,990 bytes parent folder | download | duplicates (3)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* SPDX-License-Identifier: MIT OR GPL-3.0-only */
/* test_scram.c
 * strophe XMPP client library -- test vectors for SCRAM-SHA1
 *
 * Copyright (C) 2014 Dmitry Podgorny <pasis.ua@gmail.com>
 *
 *  This software is provided AS-IS with no warranty, either express
 *  or implied.
 *
 *  This program is dual licensed under the MIT or GPLv3 licenses.
 */

/* gcc -o test_scram -I./src tests/test_scram.c tests/test.c src/sha1.c */

#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "test.h"

/* include scram.c to access static functions */
#include "scram.c"

/*
 * Test vectors for derivation function (RFC6070).
 */
static const struct {
    char *P; /* text */
    char *S; /* salt */
    size_t P_len;
    size_t S_len;
    uint32_t c; /* i */
    char *DK;   /* resulting digest */
} df_vectors[] = {
    {
        .P = "password",
        .S = "salt",
        .P_len = 8,
        .S_len = 4,
        .c = 1,
        .DK = "0c60c80f961f0e71f3a9b524af6012062fe037a6",
    },
    {
        .P = "password",
        .S = "salt",
        .P_len = 8,
        .S_len = 4,
        .c = 2,
        .DK = "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957",
    },
    {
        .P = "password",
        .S = "salt",
        .P_len = 8,
        .S_len = 4,
        .c = 4096,
        .DK = "4b007901b765489abead49d926f721d065a429c1",
    },
};

static void test_df(const struct hash_alg *alg)
{
    size_t i;
    const char *s;
    uint8_t dk[SCRAM_DIGEST_SIZE];

    printf("Derivation function SCRAM_Hi tests for %s.\n", alg->scram_name);
    for (i = 0; i < ARRAY_SIZE(df_vectors); ++i) {
        printf("Test #%d: ", (int)i + 1);
        SCRAM_Hi(alg, (uint8_t *)df_vectors[i].P, df_vectors[i].P_len,
                 (uint8_t *)df_vectors[i].S, df_vectors[i].S_len,
                 df_vectors[i].c, dk);
        s = test_bin_to_hex(dk, alg->digest_size);
        COMPARE(df_vectors[i].DK, s);
        printf("ok\n");
    }
}

/* RFC6120 */
static const struct {
    char *password;
    char *initial;
    char *challenge;
    char *response;
    char *salt;
    uint32_t i;
    char *sign;
} scram_vectors[] = {
    {
        .password = "r0m30myr0m30",
        .initial = "n,,n=juliet,r=oMsTAAwAAAAMAAAANP0TAAAAAABPU0AA",
        .challenge = "r=oMsTAAwAAAAMAAAANP0TAAAAAABPU0AAe124695b-69a9-4de6-9c30"
                     "-b51b3808c59e,s=NjhkYTM0MDgtNGY0Zi00NjdmLTkxMmUtNDlmNTNmN"
                     "DNkMDMz,i=4096",
        .response = "c=biws,r=oMsTAAwAAAAMAAAANP0TAAAAAABPU0AAe124695b-69a9-4de"
                    "6-9c30-b51b3808c59e",
        .salt = "36386461333430382d346634662d34363766"
                "2d393132652d343966353366343364303333",
        .i = 4096,
        .sign = "500e7bb4cfd2be90130641f6157b345835ef258c",
    },
};

static void test_scram(const struct hash_alg *alg)
{
    uint8_t key[SCRAM_DIGEST_SIZE];
    uint8_t sign[SCRAM_DIGEST_SIZE];
    uint8_t salt[256];
    size_t salt_len;
    char auth[512];
    const char *s;
    size_t i;
    size_t j;

    printf("SCRAM_ClientKey and SCRAM_ClientSignature tests for %s.\n",
           alg->scram_name);
    for (i = 0; i < ARRAY_SIZE(scram_vectors); ++i) {
        printf("Test #%d: ", (int)i + 1);
        snprintf(auth, sizeof(auth), "%s,%s,%s", scram_vectors[i].initial + 3,
                 scram_vectors[i].challenge, scram_vectors[i].response);
        test_hex_to_bin(scram_vectors[i].salt, salt, &salt_len);

        SCRAM_ClientKey(alg, (uint8_t *)scram_vectors[i].password,
                        strlen(scram_vectors[i].password), salt, salt_len,
                        scram_vectors[i].i, key);
        SCRAM_ClientSignature(alg, key, (uint8_t *)auth, strlen(auth), sign);
        for (j = 0; j < alg->digest_size; j++) {
            sign[j] ^= key[j];
        }
        s = test_bin_to_hex(sign, alg->digest_size);
        COMPARE(scram_vectors[i].sign, s);
        printf("ok\n");
    }
}

/*
 * Test vectors for HMAC (RFC2202, RFC4231).
 */
static const uint8_t hmac_key1[] = {0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
                                    0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
                                    0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
static const uint8_t hmac_data1[] = {0x48, 0x69, 0x20, 0x54,
                                     0x68, 0x65, 0x72, 0x65};
static const struct {
    const uint8_t *key;
    size_t key_len;
    const uint8_t *data;
    size_t data_len;
    const uint8_t sha1[SHA1_DIGEST_SIZE];
    const uint8_t sha256[SHA256_DIGEST_SIZE];
    const uint8_t sha512[SHA512_DIGEST_SIZE];
} hmac_vectors[] = {
    {
        .key = hmac_key1,
        .key_len = sizeof(hmac_key1),
        .data = hmac_data1,
        .data_len = sizeof(hmac_data1),
        .sha1 = {0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xe2, 0x8b,
                 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e, 0xf1, 0x46, 0xbe, 0x00},
        .sha256 = {0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
                   0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
                   0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
                   0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7},
        .sha512 = {0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d, 0x4f, 0xf0,
                   0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0, 0x23, 0x79, 0xf4, 0xe2,
                   0xce, 0x4e, 0xc2, 0x78, 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1,
                   0x7c, 0xde, 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
                   0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4, 0xbe, 0x9d,
                   0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70, 0x2e, 0x69, 0x6c, 0x20,
                   0x3a, 0x12, 0x68, 0x54},
    },
};

static void test_hmac(const struct hash_alg *alg)
{
    size_t i;
    uint8_t digest[SCRAM_DIGEST_SIZE];

    printf("HMAC tests for %s.\n", alg->scram_name);
    for (i = 0; i < ARRAY_SIZE(hmac_vectors); ++i) {
        printf("Test #%d: ", (int)i + 1);
        memset(digest, 0, sizeof(digest));
        crypto_HMAC(alg, hmac_vectors[i].key, hmac_vectors[i].key_len,
                    hmac_vectors[i].data, hmac_vectors[i].data_len, digest);
        switch (alg->digest_size) {
        case SHA1_DIGEST_SIZE:
            COMPARE_BUF(hmac_vectors[i].sha1, alg->digest_size, digest,
                        alg->digest_size);
            break;
        case SHA256_DIGEST_SIZE:
            COMPARE_BUF(hmac_vectors[i].sha256, alg->digest_size, digest,
                        alg->digest_size);
            break;
        case SHA512_DIGEST_SIZE:
            COMPARE_BUF(hmac_vectors[i].sha512, alg->digest_size, digest,
                        alg->digest_size);
            break;
        default:
            printf("Unknown digest size: %zu\n", alg->digest_size);
            exit(1);
        }
        printf("ok\n");
    }
}

int main()
{
    test_df(&scram_sha1);
    test_scram(&scram_sha1);
    test_hmac(&scram_sha1);
    test_hmac(&scram_sha256);
    test_hmac(&scram_sha512);

    return 0;
}