Botan 3.0.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#include <botan/internal/ffi_util.h>
9
10#if defined(BOTAN_HAS_TOTP)
11 #include <botan/otp.h>
12#endif
13
14extern "C" {
15
16using namespace Botan_FFI;
17
18#if defined(BOTAN_HAS_TOTP)
19
20BOTAN_FFI_DECLARE_STRUCT(botan_totp_struct, Botan::TOTP, 0x3D9D2CD1);
21
22#endif
23
25 const uint8_t key[], size_t key_len,
26 const char* hash_algo,
27 size_t digits,
28 size_t time_step)
29 {
30 if(totp == nullptr || key == nullptr || hash_algo == nullptr)
32
33 *totp = nullptr;
34
35#if defined(BOTAN_HAS_TOTP)
36 return ffi_guard_thunk(__func__, [=]() -> int {
37 auto otp = std::make_unique<Botan::TOTP>(key, key_len, hash_algo, digits, time_step);
38 *totp = new botan_totp_struct(std::move(otp));
39
40 return BOTAN_FFI_SUCCESS;
41 });
42#else
43 BOTAN_UNUSED(totp, key, key_len, hash_algo, digits, time_step);
45#endif
46 }
47
49 {
50#if defined(BOTAN_HAS_TOTP)
51 return BOTAN_FFI_CHECKED_DELETE(totp);
52#else
53 BOTAN_UNUSED(totp);
55#endif
56 }
57
59 uint32_t* totp_code,
60 uint64_t timestamp)
61 {
62#if defined(BOTAN_HAS_TOTP)
63 if(totp == nullptr || totp_code == nullptr)
65
66 return BOTAN_FFI_VISIT(totp, [=](auto& t) {
67 *totp_code = t.generate_totp(timestamp);
68 });
69
70#else
71 BOTAN_UNUSED(totp, totp_code, timestamp);
73#endif
74 }
75
77 uint32_t totp_code,
78 uint64_t timestamp,
79 size_t acceptable_clock_drift)
80 {
81#if defined(BOTAN_HAS_TOTP)
82 return BOTAN_FFI_VISIT(totp, [=](auto& t) {
83 const bool ok = t.verify_totp(totp_code, timestamp, acceptable_clock_drift);
85 });
86
87#else
88 BOTAN_UNUSED(totp, totp_code, timestamp, acceptable_clock_drift);
90#endif
91 }
92
93}
#define BOTAN_UNUSED(...)
Definition: assert.h:141
struct botan_totp_struct * botan_totp_t
Definition: ffi.h:1970
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:91
@ BOTAN_FFI_INVALID_VERIFIER
Definition: ffi.h:71
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:85
@ BOTAN_FFI_SUCCESS
Definition: ffi.h:70
int botan_totp_generate(botan_totp_t totp, uint32_t *totp_code, uint64_t timestamp)
Definition: ffi_totp.cpp:58
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:24
int botan_totp_destroy(botan_totp_t totp)
Definition: ffi_totp.cpp:48
int botan_totp_check(botan_totp_t totp, uint32_t totp_code, uint64_t timestamp, size_t acceptable_clock_drift)
Definition: ffi_totp.cpp:76
#define BOTAN_FFI_VISIT(obj, lambda)
Definition: ffi_util.h:126
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:145
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition: ffi_util.h:55
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition: ffi.cpp:120