1 /*
2  * Author: Copyright (C) Andrzej Surowiec 2012
3  * Copyright (c) 2012-2018 Nitrokey UG
4  *
5  * This file is part of Nitrokey App.
6  *
7  * Nitrokey App is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * any later version.
11  *
12  * Nitrokey App is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Nitrokey App. If not, see <http://www.gnu.org/licenses/>.
19  *
20  * SPDX-License-Identifier: GPL-3.0
21  */
22 
23 #include "hotpslot.h"
24 #include "string.h"
25 
26 
OTPSlot()27 OTPSlot::OTPSlot() {
28   isProgrammed = false;
29   memset(slotName, 0, sizeof(slotName));
30   memset(secret, 0, sizeof(secret));
31   memset(counter, 0, sizeof(counter));
32   memset(tokenID, 0, sizeof(tokenID));
33   config = 0;
34   slotNumber = 0;
35 }
36 
37 
38 #include <cppcodec/base32_crockford.hpp>
39 #include <cppcodec/base32_default_rfc4648.hpp>
40 #include <QDebug>
41 #include <QString>
42 
decodeBase32Secret(const std::string secret,const bool debug_mode)43 std::vector<uint8_t> decodeBase32Secret(const std::string secret, const bool debug_mode) {
44   std::vector<uint8_t> secret_raw;
45   std::string error;
46   try {
47     secret_raw = base32::decode(secret);
48     return secret_raw;
49   }
50   catch (const cppcodec::parse_error &e){
51     if (debug_mode)
52       qDebug() << e.what();
53     error = error + "base32: " + e.what();
54   }
55   try {
56     auto s = QString::fromStdString(secret);
57     s = s.remove('=');
58     secret_raw = cppcodec::base32_crockford::decode(s);
59     return secret_raw;
60   }
61   catch (const cppcodec::parse_error &e){
62     if (debug_mode)
63       qDebug() << e.what();
64     error = error + "; crockford: " + e.what();
65   }
66   throw cppcodec::parse_error(error);
67   return secret_raw;
68 }