Botan 3.11.0
Crypto and TLS for C&
ffi_totp.cpp
Go to the documentation of this file.
1/*
2* (C) 2018 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/ffi.h>
8
9#include <botan/assert.h>
10#include <botan/internal/ffi_util.h>
11
12#if defined(BOTAN_HAS_TOTP)
13 #include <botan/otp.h>
14#endif
15
16extern "C" {
17
18using namespace Botan_FFI;
19
20#if defined(BOTAN_HAS_TOTP)
21
22BOTAN_FFI_DECLARE_STRUCT(botan_totp_struct, Botan::TOTP, 0x3D9D2CD1);
23
24#endif
25
27 botan_totp_t* totp, const uint8_t key[], size_t key_len, const char* hash_algo, size_t digits, size_t time_step) {
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}
44
46#if defined(BOTAN_HAS_TOTP)
47 return BOTAN_FFI_CHECKED_DELETE(totp);
48#else
49 BOTAN_UNUSED(totp);
51#endif
52}
53
54int botan_totp_generate(botan_totp_t totp, uint32_t* totp_code, uint64_t timestamp) {
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}
67
68int botan_totp_check(botan_totp_t totp, uint32_t totp_code, uint64_t timestamp, size_t acceptable_clock_drift) {
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}
80}
#define BOTAN_UNUSED
Definition assert.h:144
struct botan_totp_struct * botan_totp_t
Definition ffi.h:2810
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:140
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:118
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:133
@ BOTAN_FFI_SUCCESS
Definition ffi.h:116
int botan_totp_generate(botan_totp_t totp, uint32_t *totp_code, uint64_t timestamp)
Definition ffi_totp.cpp:54
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)
Definition ffi_totp.cpp:26
int botan_totp_destroy(botan_totp_t totp)
Definition ffi_totp.cpp:45
int botan_totp_check(botan_totp_t totp, uint32_t totp_code, uint64_t timestamp, size_t acceptable_clock_drift)
Definition ffi_totp.cpp:68
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:158
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:185
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition ffi_util.h:61
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