taler-xotp_fw

xOTP generator firmware
Log | Files | Refs | Submodules | README

cryptoInterface.c (2197B)


      1 /**
      2  * @file cryptoInterface.c
      3  * @author Adrian STEINER (steia19@bfh.ch)
      4  * @brief Crypto interface to calculate the hash codes with the desired hash
      5  * methods
      6  * @version 0.1
      7  * @date 19-02-2025
      8  *
      9  * @copyright (C) 2025 Adrian STEINER
     10  * This program is free software: you can redistribute it and/or modify
     11  * it under the terms of the GNU General Public License as published by
     12  * the Free Software Foundation, either version 3 of the License, or
     13  * (at your option) any later version.
     14  *
     15  * This program is distributed in the hope that it will be useful,
     16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18  * GNU General Public License for more details.
     19  *
     20  * You should have received a copy of the GNU General Public License
     21  * along with this program.  If not, see <https: //www.gnu.org/licenses/>.
     22  *
     23  */
     24 #include "cryptoInterface.h"
     25 
     26 #include <stdlib.h>
     27 #include <string.h>
     28 
     29 uint8_t crypto_initInterface(cryptoInterface *interfaceHandler,
     30                              cryptoCalcTotpCB computeTotpCB,
     31                              cryptoCalcXTotpCB computeXTotpCB)
     32 {
     33   if (NULL == interfaceHandler || NULL == computeTotpCB ||
     34       NULL == computeXTotpCB) {
     35     return EXIT_FAILURE;
     36   }
     37   // Register callbacks
     38   interfaceHandler->computeTotp = computeTotpCB;
     39   interfaceHandler->computeXTotp = computeXTotpCB;
     40   return EXIT_SUCCESS;
     41 }
     42 
     43 uint32_t crypto_calc_OTP(xtotpCryptoHandler *cryptoHandler,
     44                          cryptoInterface *iCrypto,
     45                          uint64_t currentTime,
     46                          uint64_t amount)
     47 {
     48   if (NULL == cryptoHandler) {
     49     return 0;
     50   }
     51   xtotpAlgoSettingsType *usedAlgorithm =
     52       crypto_getCurrentAlgorithm(cryptoHandler);
     53   switch (crypto_getAlgoInfo(usedAlgorithm)) {
     54   case ALGO_UNINITIALIZED:
     55     return 0;
     56   case ALGO_TOTP:
     57     return iCrypto->computeTotp(currentTime, usedAlgorithm);
     58   case ALGO_XTOTP: {
     59     TALER_AmountNBO talerAmount;
     60     TALER_amount2AmountNBO(&talerAmount, &usedAlgorithm->xTalerData, amount);
     61     // Get xTOTP code
     62     return iCrypto->computeXTotp(currentTime, usedAlgorithm, &talerAmount);
     63   }
     64   default:
     65     return 0;
     66   }
     67 }