forked from Nitrokey/nitrokey-hotp-verification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_hotp.cpp
157 lines (129 loc) · 4.26 KB
/
test_hotp.cpp
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
/*
* Copyright (c) 2018 Nitrokey UG
*
* This file is part of Nitrokey HOTP verification project.
*
* Nitrokey HOTP verification is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Nitrokey HOTP verification is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Nitrokey App. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0
*/
#include "catch.hpp"
extern "C" {
#include "../operations.h"
}
const char* base32_secret = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ";
const char* admin_PIN = "12345678";
const char* RFC_HOTP_codes[] = {
"755224", //0
"287082",
"359152",
"969429", //3
"338314",
"254676",
"287922", //6
"162583",
"399871",
"520489", //9
"403154", //10
"481090", //11
};
struct Device dev;
TEST_CASE("Test correct codes", "[HOTP]") {
int res;
res = device_connect(&dev);
REQUIRE(res == true);
res = set_secret_on_device(&dev, base32_secret, admin_PIN);
REQUIRE(res == RET_NO_ERROR);
for (auto c: RFC_HOTP_codes){
res = check_code_on_device(&dev, c);
REQUIRE(res == RET_VALIDATION_PASSED);
}
device_disconnect(&dev);
}
TEST_CASE("Test incorrect codes", "[HOTP]") {
int res;
res = device_connect(&dev);
REQUIRE(res == true);
res = set_secret_on_device(&dev, base32_secret, admin_PIN);
REQUIRE(res == RET_NO_ERROR);
for (int i=0; i<10; i++){
res = check_code_on_device(&dev, "123456");
REQUIRE(res == RET_VALIDATION_FAILED);
}
device_disconnect(&dev);
}
TEST_CASE("Test codes with offset 2", "[HOTP]") {
int res;
res = device_connect(&dev);
REQUIRE(res == true);
res = set_secret_on_device(&dev, base32_secret, admin_PIN);
REQUIRE(res == RET_NO_ERROR);
int i=0;
for (auto c: RFC_HOTP_codes){
if (i++%2 == 0) continue;
res = check_code_on_device(&dev, c);
REQUIRE(res == RET_VALIDATION_PASSED);
}
device_disconnect(&dev);
}
TEST_CASE("Test code with maximum offsets", "[HOTP]") {
int res;
res = device_connect(&dev);
REQUIRE(res == true);
res = set_secret_on_device(&dev, base32_secret, admin_PIN);
REQUIRE(res == RET_NO_ERROR);
res = check_code_on_device(&dev, RFC_HOTP_codes[11]);
REQUIRE(res == RET_VALIDATION_FAILED);
res = check_code_on_device(&dev, RFC_HOTP_codes[10]);
REQUIRE(res == RET_VALIDATION_FAILED);
res = check_code_on_device(&dev, RFC_HOTP_codes[9]);
REQUIRE(res == RET_VALIDATION_PASSED);
res = check_code_on_device(&dev, RFC_HOTP_codes[11]);
REQUIRE(res == RET_VALIDATION_PASSED);
res = device_disconnect(&dev);
REQUIRE(res == RET_NO_ERROR);
}
#include "../device.h"
TEST_CASE("Try to set the HOTP secret with wrong PIN and test PIN counters", "[HOTP]") {
int res;
res = device_connect(&dev);
REQUIRE(res == true);
struct ResponseStatus status = device_get_status(&dev);
REQUIRE(status.retry_admin >= 2);
res = set_secret_on_device(&dev, base32_secret, admin_PIN);
REQUIRE(res == RET_NO_ERROR);
REQUIRE(status.retry_admin == 3);
REQUIRE(check_code_on_device(&dev, RFC_HOTP_codes[0]) == RET_VALIDATION_PASSED);
res = set_secret_on_device(&dev, base32_secret, "wrong_PIN");
REQUIRE(res == dev_wrong_password);
status = device_get_status(&dev);
REQUIRE(status.retry_admin == 2);
REQUIRE(check_code_on_device(&dev, RFC_HOTP_codes[0]) == RET_VALIDATION_FAILED);
res = set_secret_on_device(&dev, base32_secret, admin_PIN);
REQUIRE(res == RET_NO_ERROR);
status = device_get_status(&dev);
REQUIRE(status.retry_admin == 3);
for (auto c: RFC_HOTP_codes){
res = check_code_on_device(&dev, c);
REQUIRE(res == RET_VALIDATION_PASSED);
}
res = device_disconnect(&dev);
REQUIRE(res == RET_NO_ERROR);
}
TEST_CASE("Verify base32 string", "[Helper]") {
std::string invalid_base32 = "111";
std::string valid_base32 = "AAAAA";
REQUIRE_FALSE(verify_base32(invalid_base32.c_str(), invalid_base32.length()));
REQUIRE(verify_base32(valid_base32.c_str(), valid_base32.length()));
}