Botan 3.6.1
Crypto and TLS for C&
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 69 of file ffi_totp.cpp.

69 {
70#if defined(BOTAN_HAS_TOTP)
71 return BOTAN_FFI_VISIT(totp, [=](auto& t) {
72 const bool ok = t.verify_totp(totp_code, timestamp, acceptable_clock_drift);
74 });
75
76#else
77 BOTAN_UNUSED(totp, totp_code, timestamp, acceptable_clock_drift);
79#endif
80}
#define BOTAN_UNUSED
Definition assert.h:118
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:135
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:115
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:124

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_INVALID_VERIFIER, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, and BOTAN_UNUSED.

◆ 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 46 of file ffi_totp.cpp.

46 {
47#if defined(BOTAN_HAS_TOTP)
48 return BOTAN_FFI_CHECKED_DELETE(totp);
49#else
50 BOTAN_UNUSED(totp);
52#endif
53}
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:143

References BOTAN_FFI_CHECKED_DELETE, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, and BOTAN_UNUSED.

◆ 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 55 of file ffi_totp.cpp.

55 {
56#if defined(BOTAN_HAS_TOTP)
57 if(totp == nullptr || totp_code == nullptr) {
59 }
60
61 return BOTAN_FFI_VISIT(totp, [=](auto& t) { *totp_code = t.generate_totp(timestamp); });
62
63#else
64 BOTAN_UNUSED(totp, totp_code, timestamp);
66#endif
67}
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:129

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_VISIT, and BOTAN_UNUSED.

◆ 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 25 of file ffi_totp.cpp.

26 {
27 if(totp == nullptr || key == nullptr || hash_algo == nullptr) {
29 }
30
31 *totp = nullptr;
32
33#if defined(BOTAN_HAS_TOTP)
34 return ffi_guard_thunk(__func__, [=]() -> int {
35 auto otp = std::make_unique<Botan::TOTP>(key, key_len, hash_algo, digits, time_step);
36 *totp = new botan_totp_struct(std::move(otp));
37
38 return BOTAN_FFI_SUCCESS;
39 });
40#else
41 BOTAN_UNUSED(totp, key, key_len, hash_algo, digits, time_step);
43#endif
44}
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition ffi.cpp:118

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