Botan 3.9.0
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 67 of file ffi_totp.cpp.

67 {
68#if defined(BOTAN_HAS_TOTP)
69 return BOTAN_FFI_VISIT(totp, [=](auto& t) {
70 const bool ok = t.verify_totp(totp_code, timestamp, acceptable_clock_drift);
72 });
73
74#else
75 BOTAN_UNUSED(totp, totp_code, timestamp, acceptable_clock_drift);
77#endif
78}
#define BOTAN_UNUSED
Definition assert.h:144
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:117
@ BOTAN_FFI_SUCCESS
Definition ffi.h:115
#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 44 of file ffi_totp.cpp.

44 {
45#if defined(BOTAN_HAS_TOTP)
46 return BOTAN_FFI_CHECKED_DELETE(totp);
47#else
48 BOTAN_UNUSED(totp);
50#endif
51}
#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 53 of file ffi_totp.cpp.

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

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 return ffi_new_object(totp, std::move(otp));
37 });
38#else
39 BOTAN_UNUSED(totp, key, key_len, hash_algo, digits, time_step);
41#endif
42}
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().