Botan 2.19.2
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
38 *totp = new botan_totp_struct(
39 new Botan::TOTP(key, key_len, hash_algo, digits, time_step));
40
41 return BOTAN_FFI_SUCCESS;
42 });
43#else
44 BOTAN_UNUSED(totp, key, key_len, hash_algo, digits, time_step);
46#endif
47 }
48
50 {
51#if defined(BOTAN_HAS_TOTP)
52 return BOTAN_FFI_CHECKED_DELETE(totp);
53#else
54 BOTAN_UNUSED(totp);
56#endif
57 }
58
60 uint32_t* totp_code,
61 uint64_t timestamp)
62 {
63#if defined(BOTAN_HAS_TOTP)
64 if(totp == nullptr || totp_code == nullptr)
66
67 return BOTAN_FFI_DO(Botan::TOTP, totp, t, {
68 *totp_code = t.generate_totp(timestamp);
69 });
70
71#else
72 BOTAN_UNUSED(totp, totp_code, timestamp);
74#endif
75 }
76
78 uint32_t totp_code,
79 uint64_t timestamp,
80 size_t acceptable_clock_drift)
81 {
82#if defined(BOTAN_HAS_TOTP)
83 return BOTAN_FFI_RETURNING(Botan::TOTP, totp, t, {
84 const bool ok = t.verify_totp(totp_code, timestamp, acceptable_clock_drift);
86 });
87
88#else
89 BOTAN_UNUSED(totp, totp_code, timestamp, acceptable_clock_drift);
91#endif
92 }
93
94}
#define BOTAN_UNUSED(...)
Definition: assert.h:142
uint32_t generate_totp(std::chrono::system_clock::time_point time_point)
Definition: totp.cpp:27
bool verify_totp(uint32_t otp, std::chrono::system_clock::time_point time, size_t clock_drift_accepted=0)
Definition: totp.cpp:39
struct botan_totp_struct * botan_totp_t
Definition: ffi.h:1704
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:83
@ BOTAN_FFI_INVALID_VERIFIER
Definition: ffi.h:64
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:77
@ BOTAN_FFI_SUCCESS
Definition: ffi.h:63
int botan_totp_generate(botan_totp_t totp, uint32_t *totp_code, uint64_t timestamp)
Definition: ffi_totp.cpp:59
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:49
int botan_totp_check(botan_totp_t totp, uint32_t totp_code, uint64_t timestamp, size_t acceptable_clock_drift)
Definition: ffi_totp.cpp:77
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:92
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:129
#define BOTAN_FFI_RETURNING(T, obj, param, block)
Definition: ffi_util.h:101
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition: ffi_util.h:53
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:89
AlgorithmIdentifier hash_algo
Definition: x509_obj.cpp:22