Botan 3.9.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/internal/ffi_util.h>
10
11#if defined(BOTAN_HAS_TOTP)
12 #include <botan/otp.h>
13#endif
14
15extern "C" {
16
17using namespace Botan_FFI;
18
19#if defined(BOTAN_HAS_TOTP)
20
21BOTAN_FFI_DECLARE_STRUCT(botan_totp_struct, Botan::TOTP, 0x3D9D2CD1);
22
23#endif
24
26 botan_totp_t* totp, const uint8_t key[], size_t key_len, const char* hash_algo, size_t digits, size_t time_step) {
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}
43
45#if defined(BOTAN_HAS_TOTP)
46 return BOTAN_FFI_CHECKED_DELETE(totp);
47#else
48 BOTAN_UNUSED(totp);
50#endif
51}
52
53int botan_totp_generate(botan_totp_t totp, uint32_t* totp_code, uint64_t timestamp) {
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}
66
67int botan_totp_check(botan_totp_t totp, uint32_t totp_code, uint64_t timestamp, size_t acceptable_clock_drift) {
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}
79}
#define BOTAN_UNUSED
Definition assert.h:144
struct botan_totp_struct * botan_totp_t
Definition ffi.h:2317
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:117
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:132
@ BOTAN_FFI_SUCCESS
Definition ffi.h:115
int botan_totp_generate(botan_totp_t totp, uint32_t *totp_code, uint64_t timestamp)
Definition ffi_totp.cpp:53
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:25
int botan_totp_destroy(botan_totp_t totp)
Definition ffi_totp.cpp:44
int botan_totp_check(botan_totp_t totp, uint32_t totp_code, uint64_t timestamp, size_t acceptable_clock_drift)
Definition ffi_totp.cpp:67
#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