xtotpCryptoHandler.h (5603B)
1 /** 2 * @file xtotpCryptoHandler.h 3 * @author Adrian STEINER (steia19@bfh.ch) 4 * @brief Handler the (x)TOTP crypto data and generates the current codes 5 * @version 0.1 6 * @date 03-08-2025 7 * 8 * @copyright (C) 2025 Adrian STEINER 9 * This program is free software: you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation, either version 3 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program. If not, see <https: //www.gnu.org/licenses/>. 21 * 22 */ 23 24 #ifndef XTOTP_CRYPTO_HANDLER_H 25 #define XTOTP_CRYPTO_HANDLER_H 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 #include "talerAmount.h" 32 #include "xtotpConfig.h" 33 34 #define CRYPTO_NEW_SECRET_SEGMENT (0) /// Position of new registered secret 35 36 /** 37 * @brief Crypto algorithmen 38 * 39 */ 40 typedef enum { 41 ALG_NONE = 0, ///< No algortithm set 42 ALG_TOTP_SHA1, ///< TOTP-SHA1 43 ALG_MAX_TOTP_ALGORITHM, ///< Max TOTP algorithms 44 ALG_CHANGE_VALUE = 0x10, ///< Change value for xTOTP algs 45 ALG_XTOTP_SHA1, ///< xTOTP-SHA1 46 ALG_MAX_XTOTP_ALGORITHM ///< Max xTOTP algorithms 47 } crypto_algorithms; 48 49 /** 50 * @brief Crypto algorithm info type 51 * 52 */ 53 typedef enum { 54 ALGO_UNINITIALIZED = 0, ///< Uninitialised algorithm 55 ALGO_TOTP, ///< Base TOTP algorithm 56 ALGO_XTOTP ///< extendend TOTP algorithm 57 } crypto_algoInfo; 58 59 /** 60 * @brief Secret basis to register 61 * 62 */ 63 typedef enum { 64 SECRET_BASE_BYTE = 0, ///< Secret interpreted as byte 65 SECRET_BASE_8, ///< Secret interpreted as base-8 representation 66 SECRET_BASE_32 ///< Secret interpreted as base-32 representation 67 } crypto_secretBase; 68 69 /** 70 * @brief Includes all needed data to create an (x)TOTP passcode 71 * 72 */ 73 typedef struct { 74 uint8_t secret[TALER_SECRET_LEN]; ///< Secret 75 uint8_t secretLen; ///< Secret length 76 uint8_t interval; ///< Interval in s 77 uint8_t digits; ///< Number of digits 78 crypto_algorithms algorithm; ///< Used algorithm 79 TALER_xData xTalerData; ///< Additiontal data for extended(x)TOTP generation 80 } xtotpAlgoSettingsType; 81 82 /** 83 * @brief Crypto handler structure 84 * 85 */ 86 typedef struct { 87 uint8_t usedAlgorithm; ///< Current used secret 88 uint8_t processedAlgorithm; ///< Selected secret to change/add 89 char 90 deviceID[XTOTP_DEVICE_ID_SIZE]; ///< Device ID named in the taler merchant 91 char merchandBackend[XTOTP_MERCHANT_BACKEND_SIZE]; ///< Merchant backend link 92 xtotpAlgoSettingsType 93 algorithms[XTOTP_STORABLE_SECRETS + 1]; ///< Algorithms and one config 94 } xtotpCryptoHandler; 95 96 /** 97 * @brief Get algorithm algorithm information 98 * 99 * @param algoInfo The algorithm settings 100 * @return crypto_algoInfo algorithm info 101 */ 102 crypto_algoInfo crypto_getAlgoInfo(xtotpAlgoSettingsType *algoSettings); 103 104 /** 105 * @brief Get algorithm as text linked to a constant c-string array 106 * 107 * @param algoInfo The algorithm settings 108 * @return const char* name of algorithm 109 */ 110 const char *crypto_getAlgoName(xtotpAlgoSettingsType *algoSettings); 111 112 /** 113 * @brief initialise a new algorithm with the given settings 114 * 115 * @param algoSettings The structure to initialise 116 * @param secret The secret 117 * @param secretLen Length of the secret 118 * @param secretBase Secret base of @ref crypto_secretBase 119 * @param interval Valid interval time 120 * @param passcodeDigits Number of digits to display 121 * @param algorithm The used algorithm from @ref crypto_algorithms 122 * @param currency The used currency, unused in case of a base TOTP algorithm 123 * @param fraction The fraction of the currency, unused in case of a base TOTP 124 * algorithm 125 * @param merchantTemplate Template name, used for auto-paying. Unused in case 126 * of a base TOTP algorithm or not used auto-pay function 127 * @return crypto_algoInfo The crypto information, ALGO_UNINITIALIZED in case of 128 * an error 129 */ 130 crypto_algoInfo crypto_initAlgoSettings(xtotpAlgoSettingsType *algoSettings, 131 const uint8_t *secret, 132 uint8_t secretLen, 133 crypto_secretBase secretBase, 134 uint8_t interval, 135 uint8_t passcodeDigits, 136 crypto_algorithms algorithm, 137 const uint8_t *currency, 138 uint32_t fraction, 139 const uint8_t *merchantTemplate); 140 141 /** 142 * @brief Sets in the crypto handler the merchant data 143 * 144 * @param cryptoHandler The crypto handler 145 * @param deviceID Device ID or NULL to not change 146 * @param merchantBackend Merchant backend URL or NULL to not change 147 */ 148 void crypto_setMerchantData(xtotpCryptoHandler *cryptoHandler, 149 const char *deviceID, 150 const char *merchantBackend); 151 152 /** 153 * @brief Get the current used algorithm 154 * 155 * @param cryptoHandler The crypto handler 156 * @return xtotpAlgoSettingsType* pointer to the used crypto algorithm 157 */ 158 xtotpAlgoSettingsType * 159 crypto_getCurrentAlgorithm(xtotpCryptoHandler *cryptoHandler); 160 #ifdef __cplusplus 161 } 162 #endif 163 164 #endif /* XTOTP_CRYPTO_HANDLER_H */