Botan 3.6.1
Crypto and TLS for C&
ffi_hotp.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_HOTP)
12 #include <botan/otp.h>
13#endif
14
15extern "C" {
16
17using namespace Botan_FFI;
18
19#if defined(BOTAN_HAS_HOTP)
20
21BOTAN_FFI_DECLARE_STRUCT(botan_hotp_struct, Botan::HOTP, 0x89CBF191);
22
23#endif
24
25int botan_hotp_init(botan_hotp_t* hotp, const uint8_t key[], size_t key_len, const char* hash_algo, size_t digits) {
26 if(hotp == nullptr || key == nullptr || hash_algo == nullptr) {
28 }
29
30 *hotp = nullptr;
31
32#if defined(BOTAN_HAS_HOTP)
33 return ffi_guard_thunk(__func__, [=]() -> int {
34 auto otp = std::make_unique<Botan::HOTP>(key, key_len, hash_algo, digits);
35 *hotp = new botan_hotp_struct(std::move(otp));
36
37 return BOTAN_FFI_SUCCESS;
38 });
39#else
40 BOTAN_UNUSED(hotp, key, key_len, hash_algo, digits);
42#endif
43}
44
46#if defined(BOTAN_HAS_HOTP)
47 return BOTAN_FFI_CHECKED_DELETE(hotp);
48#else
49 BOTAN_UNUSED(hotp);
51#endif
52}
53
54int botan_hotp_generate(botan_hotp_t hotp, uint32_t* hotp_code, uint64_t hotp_counter) {
55#if defined(BOTAN_HAS_HOTP)
56 if(hotp == nullptr || hotp_code == nullptr) {
58 }
59
60 return BOTAN_FFI_VISIT(hotp, [=](auto& h) { *hotp_code = h.generate_hotp(hotp_counter); });
61
62#else
63 BOTAN_UNUSED(hotp, hotp_code, hotp_counter);
65#endif
66}
67
69 botan_hotp_t hotp, uint64_t* next_hotp_counter, uint32_t hotp_code, uint64_t hotp_counter, size_t resync_range) {
70#if defined(BOTAN_HAS_HOTP)
71 return BOTAN_FFI_VISIT(hotp, [=](auto& h) {
72 auto resp = h.verify_hotp(hotp_code, hotp_counter, resync_range);
73
74 if(next_hotp_counter)
75 *next_hotp_counter = resp.second;
76
77 return (resp.first == true) ? BOTAN_FFI_SUCCESS : BOTAN_FFI_INVALID_VERIFIER;
78 });
79
80#else
81 BOTAN_UNUSED(hotp, next_hotp_counter, hotp_code, hotp_counter, resync_range);
83#endif
84}
85}
#define BOTAN_UNUSED
Definition assert.h:118
struct botan_hotp_struct * botan_hotp_t
Definition ffi.h:2008
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:135
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:115
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:129
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
int botan_hotp_generate(botan_hotp_t hotp, uint32_t *hotp_code, uint64_t hotp_counter)
Definition ffi_hotp.cpp:54
int botan_hotp_destroy(botan_hotp_t hotp)
Definition ffi_hotp.cpp:45
int botan_hotp_check(botan_hotp_t hotp, uint64_t *next_hotp_counter, uint32_t hotp_code, uint64_t hotp_counter, size_t resync_range)
Definition ffi_hotp.cpp:68
int botan_hotp_init(botan_hotp_t *hotp, const uint8_t key[], size_t key_len, const char *hash_algo, size_t digits)
Definition ffi_hotp.cpp:25
#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:118