Botan 3.4.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 *totp = new botan_totp_struct(std::move(otp));
37
38 return BOTAN_FFI_SUCCESS;
39 });
40#else
41 BOTAN_UNUSED(totp, key, key_len, hash_algo, digits, time_step);
43#endif
44}
45
47#if defined(BOTAN_HAS_TOTP)
48 return BOTAN_FFI_CHECKED_DELETE(totp);
49#else
50 BOTAN_UNUSED(totp);
52#endif
53}
54
55int botan_totp_generate(botan_totp_t totp, uint32_t* totp_code, uint64_t timestamp) {
56#if defined(BOTAN_HAS_TOTP)
57 if(totp == nullptr || totp_code == nullptr) {
59 }
60
61 return BOTAN_FFI_VISIT(totp, [=](auto& t) { *totp_code = t.generate_totp(timestamp); });
62
63#else
64 BOTAN_UNUSED(totp, totp_code, timestamp);
66#endif
67}
68
69int botan_totp_check(botan_totp_t totp, uint32_t totp_code, uint64_t timestamp, size_t acceptable_clock_drift) {
70#if defined(BOTAN_HAS_TOTP)
71 return BOTAN_FFI_VISIT(totp, [=](auto& t) {
72 const bool ok = t.verify_totp(totp_code, timestamp, acceptable_clock_drift);
74 });
75
76#else
77 BOTAN_UNUSED(totp, totp_code, timestamp, acceptable_clock_drift);
79#endif
80}
81}
#define BOTAN_UNUSED
Definition assert.h:118
struct botan_totp_struct * botan_totp_t
Definition ffi.h:1956
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:124
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:104
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:118
@ BOTAN_FFI_SUCCESS
Definition ffi.h:103
int botan_totp_generate(botan_totp_t totp, uint32_t *totp_code, uint64_t timestamp)
Definition ffi_totp.cpp:55
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:46
int botan_totp_check(botan_totp_t totp, uint32_t totp_code, uint64_t timestamp, size_t acceptable_clock_drift)
Definition ffi_totp.cpp:69
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:124
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:143
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition ffi_util.h:51
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition ffi.cpp:116