Botan 3.11.0
Crypto and TLS for C&
ffi_totp.cpp File Reference
#include <botan/ffi.h>
#include <botan/assert.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 68 of file ffi_totp.cpp.

68 {
69#if defined(BOTAN_HAS_TOTP)
70 return BOTAN_FFI_VISIT(totp, [=](auto& t) {
71 const bool ok = t.verify_totp(totp_code, timestamp, acceptable_clock_drift);
73 });
74
75#else
76 BOTAN_UNUSED(totp, totp_code, timestamp, acceptable_clock_drift);
78#endif
79}
#define BOTAN_UNUSED
Definition assert.h:144
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:140
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:118
@ BOTAN_FFI_SUCCESS
Definition ffi.h:116
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:158

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

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

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

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

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

27 {
28 if(totp == nullptr || key == nullptr || hash_algo == nullptr) {
30 }
31
32 *totp = nullptr;
33
34#if defined(BOTAN_HAS_TOTP)
35 return ffi_guard_thunk(__func__, [=]() -> int {
36 auto otp = std::make_unique<Botan::TOTP>(key, key_len, hash_algo, digits, time_step);
37 return ffi_new_object(totp, std::move(otp));
38 });
39#else
40 BOTAN_UNUSED(totp, key, key_len, hash_algo, digits, time_step);
42#endif
43}
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:178
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:95

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