Skip to content

Commit e70e755

Browse files
committed
tests: added UT for SCRAM-SHA1 functions
1 parent 5e37de2 commit e70e755

File tree

3 files changed

+201
-2
lines changed

3 files changed

+201
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ tests/test_hash
4646
tests/test_jid
4747
tests/test_rand
4848
tests/test_sasl
49+
tests/test_scram
4950
tests/test_sock
5051
m4/
5152
libstrophe.project

Makefile.am

+5-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ examples_component_LDADD = $(STROPHE_LIBS)
6161

6262

6363
## Tests
64-
TESTS = tests/check_parser tests/test_rand
65-
check_PROGRAMS = tests/check_parser tests/test_rand
64+
TESTS = tests/check_parser tests/test_rand tests/test_scram
65+
check_PROGRAMS = tests/check_parser tests/test_rand tests/test_scram
6666

6767
tests_check_parser_SOURCES = tests/check_parser.c tests/test.h
6868
tests_check_parser_CFLAGS = @check_CFLAGS@ $(PARSER_CFLAGS) $(STROPHE_FLAGS) \
@@ -72,3 +72,6 @@ tests_check_parser_LDFLAGS = -static
7272

7373
tests_test_rand_SOURCES = tests/test_rand.c src/sha1.c
7474
tests_test_rand_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src
75+
76+
tests_test_scram_SOURCES = tests/test_scram.c src/sha1.c
77+
tests_test_scram_CFLAGS = $(STROPHE_FLAGS) -I$(top_srcdir)/src

tests/test_scram.c

+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/* test_scram.c
2+
* strophe XMPP client library -- test vectors for SCRAM-SHA1
3+
*
4+
* Copyright (C) 2014 Dmitry Podgorny <pasis.ua@gmail.com>
5+
*
6+
* This software is provided AS-IS with no warranty, either express
7+
* or implied.
8+
*
9+
* This software is distributed under license and may not be copied,
10+
* modified or distributed except as expressly authorized under the
11+
* terms of the license contained in the file LICENSE.txt in this
12+
* distribution.
13+
*/
14+
15+
/* gcc -o test_scram -I. -I./src tests/test_scram.c src/sha1.c */
16+
17+
#include <assert.h>
18+
#include <string.h>
19+
#include <stdio.h>
20+
#include <stdlib.h>
21+
22+
/* include scram.c to access static functions */
23+
#include "scram.c"
24+
25+
/* TODO Make common code for tests. */
26+
27+
#ifndef ARRAY_SIZE
28+
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
29+
#endif
30+
31+
static uint8_t char_to_bin(char c)
32+
{
33+
return c <= '9' ? (uint8_t)(c - '0') :
34+
c <= 'Z' ? (uint8_t)(c - 'A' + 10) :
35+
(uint8_t)(c - 'a' + 10);
36+
}
37+
38+
static void hex_to_bin(const char *hex, uint8_t *bin, size_t *bin_len)
39+
{
40+
size_t len = strlen(hex);
41+
size_t i;
42+
43+
assert(len % 2 == 0);
44+
45+
for (i = 0; i < len / 2; ++i) {
46+
bin[i] = char_to_bin(hex[i * 2]) * 16 + char_to_bin(hex[i * 2 + 1]);
47+
}
48+
*bin_len = len / 2;
49+
}
50+
51+
static const char *bin_to_hex(uint8_t *bin, size_t len)
52+
{
53+
static char buf[1024];
54+
size_t i;
55+
56+
assert(ARRAY_SIZE(buf) > len * 2);
57+
58+
for (i = 0; i < len; ++i) {
59+
sprintf(buf + i * 2, "%02x", bin[i]);
60+
}
61+
return buf;
62+
}
63+
64+
#define COMPARE(v1, v2) \
65+
do { \
66+
const char *__v1 = v1; \
67+
const char *__v2 = v2; \
68+
if (strcmp(__v1, __v2) != 0) { \
69+
printf("%s differs!\n" \
70+
"expected: %s\n" \
71+
"got: %s\n", \
72+
#v1, __v1, __v2); \
73+
exit(1); \
74+
} \
75+
} while (0)
76+
77+
/*
78+
* Test vectors for derivation function (RFC6070).
79+
*/
80+
static const struct {
81+
char *P; /* text */
82+
char *S; /* salt */
83+
size_t P_len;
84+
size_t S_len;
85+
uint32_t c; /* i */
86+
char *DK; /* resulting digest */
87+
} df_vectors[] = {
88+
{
89+
.P = "password",
90+
.S = "salt",
91+
.P_len = 8,
92+
.S_len = 4,
93+
.c = 1,
94+
.DK = "0c60c80f961f0e71f3a9b524af6012062fe037a6",
95+
},
96+
{
97+
.P = "password",
98+
.S = "salt",
99+
.P_len = 8,
100+
.S_len = 4,
101+
.c = 2,
102+
.DK = "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957",
103+
},
104+
{
105+
.P = "password",
106+
.S = "salt",
107+
.P_len = 8,
108+
.S_len = 4,
109+
.c = 4096,
110+
.DK = "4b007901b765489abead49d926f721d065a429c1",
111+
},
112+
};
113+
114+
static void test_df(void)
115+
{
116+
size_t i;
117+
const char *s;
118+
uint8_t dk[SHA1_DIGEST_SIZE];
119+
120+
printf("Derivation function tests (SCRAM_SHA1_Hi).\n");
121+
for (i = 0; i < ARRAY_SIZE(df_vectors); ++i) {
122+
printf("Test #%d: ", (int)i + 1);
123+
SCRAM_SHA1_Hi((uint8_t *)df_vectors[i].P, df_vectors[i].P_len,
124+
(uint8_t *)df_vectors[i].S, df_vectors[i].S_len,
125+
df_vectors[i].c, dk);
126+
s = bin_to_hex(dk, sizeof(dk));
127+
COMPARE(df_vectors[i].DK, s);
128+
printf("ok\n");
129+
}
130+
}
131+
132+
/* RFC6120 */
133+
static const struct {
134+
char *password;
135+
char *initial;
136+
char *challenge;
137+
char *response;
138+
char *salt;
139+
uint32_t i;
140+
char *sign;
141+
} scram_vectors[] = {
142+
{
143+
.password = "r0m30myr0m30",
144+
.initial = "n,,n=juliet,r=oMsTAAwAAAAMAAAANP0TAAAAAABPU0AA",
145+
.challenge = "r=oMsTAAwAAAAMAAAANP0TAAAAAABPU0AAe124695b-69a9-4de6-9c30"
146+
"-b51b3808c59e,s=NjhkYTM0MDgtNGY0Zi00NjdmLTkxMmUtNDlmNTNmN"
147+
"DNkMDMz,i=4096",
148+
.response = "c=biws,r=oMsTAAwAAAAMAAAANP0TAAAAAABPU0AAe124695b-69a9-4de"
149+
"6-9c30-b51b3808c59e",
150+
.salt = "36386461333430382d346634662d34363766"
151+
"2d393132652d343966353366343364303333",
152+
.i = 4096,
153+
.sign = "500e7bb4cfd2be90130641f6157b345835ef258c",
154+
},
155+
};
156+
157+
static void test_scram(void)
158+
{
159+
uint8_t key[SHA1_DIGEST_SIZE];
160+
uint8_t sign[SHA1_DIGEST_SIZE];
161+
uint8_t salt[256];
162+
size_t salt_len;
163+
char auth[512];
164+
const char *s;
165+
size_t i;
166+
int j;
167+
168+
printf("SCRAM_SHA1_ClientKey and SCRAM_SHA1_ClientSignature tests.\n");
169+
for (i = 0; i < ARRAY_SIZE(scram_vectors); ++i) {
170+
printf("Test #%d: ", (int)i + 1);
171+
snprintf(auth, sizeof(auth), "%s,%s,%s",
172+
scram_vectors[i].initial + 3, scram_vectors[i].challenge,
173+
scram_vectors[i].response);
174+
hex_to_bin(scram_vectors[i].salt, salt, &salt_len);
175+
176+
SCRAM_SHA1_ClientKey((uint8_t *)scram_vectors[i].password,
177+
strlen(scram_vectors[i].password),
178+
salt, salt_len, scram_vectors[i].i, key);
179+
SCRAM_SHA1_ClientSignature(key, (uint8_t *)auth, strlen(auth), sign);
180+
for (j = 0; j < SHA1_DIGEST_SIZE; j++) {
181+
sign[j] ^= key[j];
182+
}
183+
s = bin_to_hex(sign, SHA1_DIGEST_SIZE);
184+
COMPARE(scram_vectors[i].sign, s);
185+
printf("ok\n");
186+
}
187+
}
188+
189+
int main(int argc, char **argv)
190+
{
191+
test_df();
192+
test_scram();
193+
194+
return 0;
195+
}

0 commit comments

Comments
 (0)