diff options
-rw-r--r-- | include/stick10_commands.h | 10 | ||||
-rw-r--r-- | unittest/test_HOTP.cc | 54 |
2 files changed, 24 insertions, 40 deletions
diff --git a/include/stick10_commands.h b/include/stick10_commands.h index f58ace4..f1903aa 100644 --- a/include/stick10_commands.h +++ b/include/stick10_commands.h @@ -71,11 +71,11 @@ class WriteToHOTPSlot : Command<CommandID::WRITE_TO_SLOT> { std::string dissect() const { std::stringstream ss; ss << "slot_number:\t" << (int)(slot_number) << std::endl; - ss << "slot_name" << slot_name << std::endl; - ss << "slot_secret" << slot_secret << std::endl; - ss << "slot_config" << slot_config << std::endl; - ss << "slot_token_id" << slot_token_id << std::endl; - ss << "slot_counter" << slot_counter << std::endl; + ss << "slot_name:\t" << slot_name << std::endl; + ss << "slot_secret:\t" << slot_secret << std::endl; + ss << "slot_config:\t" << slot_config << std::endl; + ss << "slot_token_id:\t" << slot_token_id << std::endl; + ss << "slot_counter:\t" << slot_counter << std::endl; return ss.str(); } } __packed; diff --git a/unittest/test_HOTP.cc b/unittest/test_HOTP.cc index c4af104..e3a7564 100644 --- a/unittest/test_HOTP.cc +++ b/unittest/test_HOTP.cc @@ -13,36 +13,27 @@ using namespace nitrokey::device; using namespace nitrokey::proto::stick10; using namespace nitrokey::log; -std::string getSlotName(Stick10 &stick, int slotNo) { - ReadSlot::CommandTransaction::CommandPayload slot_req; - slot_req.slot_number = slotNo; - auto slot = ReadSlot::CommandTransaction::run(stick, slot_req); - std::string sName(reinterpret_cast<char *>(slot.slot_name)); - return sName; -} - -void setSecret (uint8_t slot_secret[], const char* secretHex){ - assert(strlen(secretHex)%2==0); - //assert(strlen(secretHex)==(sizeof slot_secret)*2); - char buf[2]; - for(int i=0; i<strlen(secretHex); i++){ - buf[i%2] = secretHex[i]; - if (i%2==1){ - slot_secret[i/2] = strtoul(buf, NULL, 16) & 0xFF; - } - } +void hexStringToByte(uint8_t data[], const char* hexString){ + assert(strlen(hexString)%2==0); + char buf[2]; + for(int i=0; i<strlen(hexString); i++){ + buf[i%2] = hexString[i]; + if (i%2==1){ + data[i/2] = strtoul(buf, NULL, 16) & 0xFF; + } + } }; TEST_CASE("test secret", "[functions]") { uint8_t slot_secret[21]; slot_secret[20] = 0; const char* secretHex = "3132333435363738393031323334353637383930"; - setSecret(slot_secret, secretHex); + hexStringToByte(slot_secret, secretHex); CAPTURE(slot_secret); REQUIRE(strcmp("12345678901234567890",reinterpret_cast<char *>(slot_secret) ) == 0 ); } -TEST_CASE("Slot names are correct", "[slotNames]") { +TEST_CASE("Test HOTP codes according to RFC", "[HOTP]") { Stick10 stick; bool connected = stick.connect(); REQUIRE(connected == true); @@ -59,16 +50,17 @@ TEST_CASE("Slot names are correct", "[slotNames]") { FirstAuthenticate::CommandTransaction::run(stick, authreq); } + //test according to https://tools.ietf.org/html/rfc4226#page-32 { WriteToHOTPSlot::CommandTransaction::CommandPayload hwrite; hwrite.slot_number = 0x10; - strcpy(reinterpret_cast<char *>(hwrite.slot_name), "rfc_test"); + strcpy(reinterpret_cast<char *>(hwrite.slot_name), "rfc4226_libnitro_test"); //strcpy(reinterpret_cast<char *>(hwrite.slot_secret), ""); const char* secretHex = "3132333435363738393031323334353637383930"; - setSecret(hwrite.slot_secret, secretHex); - //hwrite.slot_config; - strcpy(reinterpret_cast<char *>(hwrite.slot_token_id), ""); - strcpy(reinterpret_cast<char *>(hwrite.slot_counter), ""); + hexStringToByte(hwrite.slot_secret, secretHex); + //hwrite.slot_config; //TODO check various configs in separate test cases + //strcpy(reinterpret_cast<char *>(hwrite.slot_token_id), ""); + //strcpy(reinterpret_cast<char *>(hwrite.slot_counter), ""); //authorize writehotp first { @@ -82,16 +74,8 @@ TEST_CASE("Slot names are correct", "[slotNames]") { WriteToHOTPSlot::CommandTransaction::run(stick, hwrite); uint32_t codes[] = { - 755224, - 287082, - 359152, - 969429, - 338314, - 254676, - 287922, - 162583, - 399871, - 520489 + 755224, 287082, 359152, 969429, 338314, + 254676, 287922, 162583, 399871, 520489 }; for( auto code: codes){ |