Botan  2.12.1
Crypto and TLS for C++11
Functions
ffi_totp.cpp File Reference
#include <botan/ffi.h>
#include <botan/internal/ffi_util.h>

Go to the source code of this file.

Functions

int botan_totp_check (botan_totp_t totp, uint32_t totp_code, uint64_t timestamp, size_t acceptable_clock_drift)
 
int botan_totp_destroy (botan_totp_t totp)
 
int botan_totp_generate (botan_totp_t totp, uint32_t *totp_code, uint64_t timestamp)
 
int botan_totp_init (botan_totp_t *totp, const uint8_t key[], size_t key_len, const char *hash_algo, size_t digits, size_t time_step)
 

Function Documentation

◆ botan_totp_check()

int botan_totp_check ( botan_totp_t  totp,
uint32_t  totp_code,
uint64_t  timestamp,
size_t  acceptable_clock_drift 
)

Verify a TOTP code

Parameters
totpthe TOTP object
totp_codethe presented OTP
timestampthe current local timestamp
acceptable_clock_driftspecifies the acceptable amount of clock drift (in terms of time steps) between the two hosts.

Definition at line 77 of file ffi_totp.cpp.

References BOTAN_FFI_DO, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_INVALID_VERIFIER, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, and Botan::TOTP::verify_totp().

81  {
82 #if defined(BOTAN_HAS_TOTP)
83  return BOTAN_FFI_DO(Botan::TOTP, totp, t, {
84  const bool ok = t.verify_totp(totp_code, timestamp, acceptable_clock_drift);
86  });
87 
88 #else
89  BOTAN_UNUSED(totp, totp_code, timestamp, acceptable_clock_drift);
91 #endif
92  }
bool verify_totp(uint32_t otp, std::chrono::system_clock::time_point time, size_t clock_drift_accepted=0)
Definition: totp.cpp:39
#define BOTAN_UNUSED(...)
Definition: assert.h:142
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:92

◆ botan_totp_destroy()

int botan_totp_destroy ( botan_totp_t  totp)

Destroy a TOTP instance

Returns
0 if success, error if invalid object handle

Definition at line 49 of file ffi_totp.cpp.

References BOTAN_FFI_CHECKED_DELETE, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, and BOTAN_UNUSED.

50  {
51 #if defined(BOTAN_HAS_TOTP)
52  return BOTAN_FFI_CHECKED_DELETE(totp);
53 #else
54  BOTAN_UNUSED(totp);
56 #endif
57  }
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:120
#define BOTAN_UNUSED(...)
Definition: assert.h:142

◆ botan_totp_generate()

int botan_totp_generate ( botan_totp_t  totp,
uint32_t *  totp_code,
uint64_t  timestamp 
)

Generate a TOTP code for the provided timestamp

Parameters
totpthe TOTP object
totp_codethe OTP code will be written here
timestampthe current local timestamp

Definition at line 59 of file ffi_totp.cpp.

References BOTAN_FFI_DO, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, and Botan::TOTP::generate_totp().

62  {
63 #if defined(BOTAN_HAS_TOTP)
64  if(totp == nullptr || totp_code == nullptr)
66 
67  return BOTAN_FFI_DO(Botan::TOTP, totp, t, {
68  *totp_code = t.generate_totp(timestamp);
69  });
70 
71 #else
72  BOTAN_UNUSED(totp, totp_code, timestamp);
74 #endif
75  }
#define BOTAN_UNUSED(...)
Definition: assert.h:142
uint32_t generate_totp(std::chrono::system_clock::time_point time_point)
Definition: totp.cpp:27
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:92

◆ botan_totp_init()

int botan_totp_init ( botan_totp_t totp,
const uint8_t  key[],
size_t  key_len,
const char *  hash_algo,
size_t  digits,
size_t  time_step 
)

Initialize a TOTP instance

Definition at line 24 of file ffi_totp.cpp.

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), and hash_algo.

29  {
30  if(totp == nullptr || key == nullptr || hash_algo == nullptr)
32 
33  *totp = nullptr;
34 
35 #if defined(BOTAN_HAS_TOTP)
36  return ffi_guard_thunk(__func__, [=]() -> int {
37 
38  *totp = new botan_totp_struct(
39  new Botan::TOTP(key, key_len, hash_algo, digits, time_step));
40 
41  return BOTAN_FFI_SUCCESS;
42  });
43 #else
44  BOTAN_UNUSED(totp, key, key_len, hash_algo, digits, time_step);
46 #endif
47  }
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:86
#define BOTAN_UNUSED(...)
Definition: assert.h:142
AlgorithmIdentifier hash_algo
Definition: x509_obj.cpp:22