aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tests
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2022-12-31 13:26:58 -0900
committerDavid M. Syzdek <david@syzdek.net>2022-12-31 14:09:54 -0900
commita12ecc484992f2a147fa4800ffc81b764641ef42 (patch)
treedb84e35baae673b085124e949c67dea23158d429 /tests
parent3889e08cec923cb9431c9623fde0260d1a790264 (diff)
downloadotputils-a12ecc484992f2a147fa4800ffc81b764641ef42.tar.gz
otputils-a12ecc484992f2a147fa4800ffc81b764641ef42.tar.bz2
otputils-a12ecc484992f2a147fa4800ffc81b764641ef42.tar.xz
otputils-a12ecc484992f2a147fa4800ffc81b764641ef42.zip
test-totp: refactoring to include test vectors from RFC6238
Diffstat (limited to 'tests')
-rw-r--r--tests/test-totp.c136
1 files changed, 119 insertions, 17 deletions
diff --git a/tests/test-totp.c b/tests/test-totp.c
index 3e1274b..1dfc973 100644
--- a/tests/test-totp.c
+++ b/tests/test-totp.c
@@ -47,8 +47,12 @@
47#include <assert.h> 47#include <assert.h>
48#include <getopt.h> 48#include <getopt.h>
49#include <stdio.h> 49#include <stdio.h>
50#include <string.h>
51#include <strings.h>
50#include <inttypes.h> 52#include <inttypes.h>
51 53
54#include <otputil.h>
55
52 56
53/////////////////// 57///////////////////
54// // 58// //
@@ -84,7 +88,12 @@
84typedef struct _test_data testdata_t; 88typedef struct _test_data testdata_t;
85struct _test_data 89struct _test_data
86{ 90{
87 const uint8_t * test_key; 91 const char * totp_kb32;
92 uint64_t totp_t0;
93 uint64_t totp_tx;
94 uint64_t totp_time;
95 int totp_code;
96 int totp_digits;
88}; 97};
89 98
90 99
@@ -98,29 +107,71 @@ struct _test_data
98static int verbose = 0; 107static int verbose = 0;
99static int quiet = 0; 108static int quiet = 0;
100 109
101static struct { 110
102 const char * totp_kb32; 111static testdata_t test_data[] =
103 uint64_t totp_t0;
104 uint64_t totp_tx;
105 uint64_t totp_time;
106 uint64_t totp_hmac;
107 uint64_t totp_code;
108} test_data[] =
109{ 112{
113 // RFC 6238 Appendix B. Test Vectors
114 {
115 // 1970-01-01 00:00:59
116 .totp_kb32 = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ",
117 .totp_t0 = 0ULL,
118 .totp_tx = 30ULL,
119 .totp_time = 59,
120 .totp_code = 94287082,
121 .totp_digits = 8,
122 },
123 {
124 // 2005-03-18 01:58:29
125 .totp_kb32 = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ",
126 .totp_t0 = 0ULL,
127 .totp_tx = 30ULL,
128 .totp_time = 1111111109,
129 .totp_code = 7081804, // RFC states 07081804
130 .totp_digits = 8,
131 },
132 {
133 // 2005-03-18 01:58:29
134 .totp_kb32 = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ",
135 .totp_t0 = 0ULL,
136 .totp_tx = 30ULL,
137 .totp_time = 1111111111,
138 .totp_code = 14050471,
139 .totp_digits = 8,
140 },
141 {
142 // 2009-02-13 23:31:30
143 .totp_kb32 = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ",
144 .totp_t0 = 0ULL,
145 .totp_tx = 30ULL,
146 .totp_time = 1234567890,
147 .totp_code = 89005924,
148 .totp_digits = 8,
149 },
110 { 150 {
111 .totp_kb32 = "G7DZ-YJJU-LOIZ-MREO", 151 // 2033-05-18 03:33:20
152 .totp_kb32 = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ",
112 .totp_t0 = 0ULL, 153 .totp_t0 = 0ULL,
113 .totp_tx = 30ULL, 154 .totp_tx = 30ULL,
114 .totp_time = 1672297762, 155 .totp_time = 2000000000,
115 .totp_hmac = 0, 156 .totp_code = 69279037,
116 .totp_code = 268123, 157 .totp_digits = 8,
158 },
159 {
160 // 2603-10-11 11:33:20
161 .totp_kb32 = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ",
162 .totp_t0 = 0ULL,
163 .totp_tx = 30ULL,
164 .totp_time = 20000000000,
165 .totp_code = 65353130,
166 .totp_digits = 8,
117 }, 167 },
118 { 168 {
119 .totp_kb32 = NULL, 169 .totp_kb32 = NULL,
120 .totp_t0 = 0, 170 .totp_t0 = 0,
121 .totp_tx = 0, 171 .totp_tx = 0,
122 .totp_time = 0, 172 .totp_time = 0,
123 .totp_hmac = 0, 173 .totp_code = 0,
174 .totp_digits = 0,
124 } 175 }
125}; 176};
126 177
@@ -150,9 +201,16 @@ main(
150 int argc, 201 int argc,
151 char * argv[] ) 202 char * argv[] )
152{ 203{
153 int c; 204 int c;
154 int opt_index; 205 int opt_index;
155 int idx; 206 int idx;
207 otputil_bv_t * totp_k;
208 int64_t totp_code;
209 testdata_t * p;
210 int len;
211 int k_len;
212 int code_len;
213 char code_str[128];
156 214
157 // getopt options 215 // getopt options
158 static const char * short_opt = "hqVv"; 216 static const char * short_opt = "hqVv";
@@ -208,8 +266,52 @@ main(
208 }; 266 };
209 }; 267 };
210 268
269 k_len = 0;
270 code_len = 0;
271 if ((verbose))
272 {
273 for(idx = 0; ((test_data[idx].totp_kb32)); idx++)
274 {
275 if (k_len < (len = (int)strlen(test_data[idx].totp_kb32)))
276 k_len = len;
277 if (code_len < test_data[idx].totp_digits)
278 code_len = test_data[idx].totp_digits;
279 };
280 printf("%-*s : %-2s : %-4s : %-12s : digits : %-*s\n", k_len, "key", "t0", "tx", "t", code_len, "code");
281 };
282
211 for(idx = 0; ((test_data[idx].totp_kb32)); idx++) 283 for(idx = 0; ((test_data[idx].totp_kb32)); idx++)
212 { 284 {
285 p = &test_data[idx];
286
287 if ((verbose))
288 {
289 snprintf(code_str, sizeof(code_str), "%0*i", p->totp_digits, p->totp_code);
290 printf("%s : %2" PRIu64 " : %4" PRIu64 " : %12" PRIu64 " : %6i : %*s\n", p->totp_kb32, p->totp_t0, p->totp_tx, p->totp_time, p->totp_digits, code_len, code_str);
291 };
292
293 otputil_set_param(NULL, OTPUTIL_OPT_TOTP_DIGITS, &p->totp_digits);
294
295 if ((totp_k = otputil_base32bv(p->totp_kb32)) == NULL)
296 {
297 fprintf(stderr, "%s: otputil_base32bv(): internal error\n", PROGRAM_NAME);
298 return(1);
299 };
300
301 if ((totp_code = otputil_totp_code(totp_k, p->totp_t0, p->totp_tx, p->totp_time)) == -1)
302 {
303 fprintf(stderr, "%s: otputil_totp_code(): internal error\n", PROGRAM_NAME);
304 otputil_bvfree(totp_k);
305 return(1);
306 };
307
308 otputil_bvfree(totp_k);
309
310 if (totp_code != p->totp_code)
311 {
312 fprintf(stderr, "%s: expected %0*i but generated %0*" PRIi64 "\n", PROGRAM_NAME, p->totp_digits, p->totp_code, p->totp_digits, totp_code);
313 return(1);
314 };
213 }; 315 };
214 316
215 return(0); 317 return(0);