xtotp.h (7044B)
1 /** 2 * @file xtotp.h 3 * @author Adrian STEINER (steia19@bfh.ch) 4 * @brief xTOTP application data and interface for initialising and processing 5 * @version 0.1 6 * @date 13-02-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_H 25 #define XTOTP_H 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 #include "autoPayInterface.h" 32 #include "batMeasInterface.h" 33 #include "clockInterface.h" 34 #include "cryptoInterface.h" 35 #include "deviceInterface.h" 36 #include "displayInterface.h" 37 #include "inputInterface.h" 38 #include "syncInterface.h" 39 40 /** 41 * @brief Base state-machine states 42 * 43 */ 44 typedef enum { 45 XTOTP_SLEEP = 0, 46 XTOTP_STARTUP, 47 XTOTP_DISPATCHER, 48 XTOTP_ENTER_AMOUNT, 49 XTOTP_AUTOPAY_START, 50 XTOTP_AUTOPAY_WAIT_RESPONSE, 51 XTOTP_AUTOPAY_VERIFY, 52 XTOTP_SHOW_XTOTP, 53 XTOTP_SHOW_TOTP, 54 XTOTP_GO_TO_SETTINGS, 55 XTOTP_SETTINGS, 56 XTOTP_MAX_STATES 57 } xtotp_BaseStates; 58 59 /** 60 * @brief Device settings states 61 * 62 */ 63 typedef enum { 64 SETTINGS_ENTRY = 0, 65 SETTINGS_SELECT_KEY, 66 SETTINGS_REGISTER_KEY, 67 SETTINGS_ADDITIONAL, 68 SETTINGS_MAX_STATES 69 } xtotp_SettingStates; 70 71 /** 72 * @brief Additional settings states 73 * 74 */ 75 typedef enum { 76 ADDITIONAL_CLOCK = 0, 77 ADDITIONAL_BATTERY_STATE = 1, 78 ADDITIONAL_LAST_SYNC_TIME = 2, 79 ADDITIONAL_DIFFERENCE_SYNC = 3, 80 ADDITIONAL_TIMEOUT_TIME = 4, 81 ADDITIONAL_SCREEN_MODE = 5, 82 ADDITIONAL_DEVICE_ID = 6, 83 ADDITIONAL_UNUSED_7 = 7, 84 ADDITIONAL_UNSUED_8 = 8, 85 ADDITIONAL_FIRMWARE = 9, 86 ADDITIONAL_MAX_STATES 87 } xtotp_AdditionalSettingStates; 88 89 /** 90 * @brief Register secret states 91 * 92 */ 93 typedef enum { 94 XTOTP_REGISTER_SECRET_DISPATCHER = 0, 95 XTOTP_REGISTER_SECRET_SELECT, 96 XTOTP_REGISTER_SECRET_SET_ALGORITHM, 97 XTOTP_REGISTER_SECRET_SET_SECRET, 98 XTOTP_REGISTER_SECRET_SET_CODE_LENGTH, 99 XTOTP_REGISTER_SECRET_SET_INTERVAL, 100 XTOTP_REGISTER_SECRET_SET_CURRENCY, 101 XTOTP_REGISTER_SECRET_SET_FRACTION, 102 XTOTP_REGISTER_SECRET_MAX_STATES 103 } xtotp_RegisterSecretStates; 104 105 /** 106 * @brief Structure of used state machine states 107 * 108 */ 109 typedef struct { 110 xtotp_BaseStates base; ///< Base state machine state 111 xtotp_SettingStates settings; ///< Settings state 112 xtotp_AdditionalSettingStates 113 additionalSettings; ///< Additional settings state 114 xtotp_RegisterSecretStates registerSecret; ///< Register secret state 115 } xtotp_States; 116 117 /** 118 * @brief Process data during runtime 119 * 120 */ 121 typedef struct { 122 uint64_t enteredAmount; ///< Stores the current entered amount 123 uint32_t lastCode; ///< Saves the last generated code 124 uint64_t 125 lastNeededTime; ///< Stores the last needed time to reduce clock reading 126 bool updateDisplayData; ///< Flag to force a possible updated data field (i.e. 127 ///< for settings/information windows) 128 } xtotp_ProcessData; 129 130 /** 131 * @brief xTOTP Generator data 132 * Stores all data centralized in one structure. 133 * 134 */ 135 typedef struct { 136 /* State machine states*/ 137 xtotp_States 138 currentStates; ///< Saves the current states of the state machines 139 /* Control Data */ 140 uint32_t 141 timeOutTime; ///< Increment the unused time to shutdown for power saving 142 uint32_t stopTimeEnterButton; ///< Increment the time the stop button is 143 ///< pressed to shutdown the device 144 bool secFlag; 145 /* TOTP data*/ 146 xtotp_ProcessData processData; ///< Saves the processing data 147 xtotpCryptoHandler cryptoHandler; ///< Handles the crypto data 148 /* Cryptographic*/ 149 cryptoInterface iCrypto; ///< Crypto interface to hash (x)TOTP functions 150 /* Device Interface */ 151 deviceInterface iDevice; ///< Device interface for system functions 152 /* HW Interface*/ 153 autoPayInterface 154 iAutoPay; ///< Interface for automated paying with given hardware 155 displayInterface iDisplay; ///< Interface for the display 156 inputInterface iInput; ///< Interface for the keyboard 157 clockInterface iClock; ///< Interface for the real time clock 158 syncInterface iSync; ///< Interface for synchronizing the time 159 batMeasInterface iBatMeas; ///< Interface for battery measurement 160 } xtotp_Data; 161 162 /** 163 * @brief Initialise the xtotp_Data structure and link all used callback 164 * functions. 165 * 166 */ 167 uint8_t xtotp_init(xtotp_Data *appData, 168 /* Crypto interface */ 169 cryptoCalcTotpCB computeTotpCB, 170 cryptoCalcXTotpCB computeXTotpCB, 171 /* Device interface */ 172 deviceGetTickCB getTickCB, 173 deviceShutdownCB shutdownCB, 174 /* auto pay*/ 175 autoPayPwrCB autoPayPowerOnCB, 176 autoPayPwrCB autoPayPowerOffCB, 177 autoPayGetPasscodes autoPayGetPasscodesCB, 178 autoPayArePasscodesReceived autoPayArePasscodesReceivedCB, 179 autoPaySendURL autoPaySendUrlCB, 180 autoPayIsURLRead autoPayIsURLReadCB, 181 /* display interface */ 182 pbm_image *displayImage, 183 pbm_font *smallFont, 184 pbm_font *bigFont, 185 displayPwrCB displayPowerOnCB, 186 displayPwrCB displayPowerOffCB, 187 displayShowCB dispShowCB, 188 uint8_t symbolCharge, 189 uint8_t symbolSynchronize, 190 uint8_t symbolBatteryStart, 191 /* input interface */ 192 inputPwrCB inputPowerOnCB, 193 inputPwrCB inputPowerOffCB, 194 inputSampleCB inputReadInputCB, 195 /* clock interface */ 196 clockPwrCB clockPowerOnCB, 197 clockPwrCB clockPowerOffCB, 198 clockGetTimeCB clockGetUnixTimeCB, 199 clockSetTimeCB clockSetUnixTimeCB, 200 /* sync interface */ 201 syncPwrCB syncPowerOnCB, 202 syncPwrCB syncpowerOffCB, 203 synchronizeCB syncCB, 204 /* bat meas interface */ 205 batMeasPwrCB batMeasPowerOnCB, 206 batMeasPwrCB batMeasPowerOffCB, 207 getBatteryStateCB batMeasGetStateCB); 208 209 /** 210 * @brief Process the state machine of the device. 211 * Must be called faster than than @ref XTOTP_SAMPLE_TIME 212 * 213 * @param appData The user data 214 * @return uint8_t EXIT_SUCCES in success, EXIT_FAILURE otherwise 215 */ 216 uint8_t xtotp_process(xtotp_Data *appData); 217 218 #ifdef __cplusplus 219 } 220 #endif 221 222 #endif /* XTOTP_H*/