Botan 2.19.5 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#include <botan/internal/ffi_util.h>
9
10#if defined(BOTAN_HAS_HOTP)
11 #include <botan/otp.h>
12#endif
13
14 extern "C" {
15
16 using namespace Botan_FFI;
17
18#if defined(BOTAN_HAS_HOTP)
19
20 BOTAN_FFI_DECLARE_STRUCT(botan_hotp_struct, Botan::HOTP, 0x89CBF191);
21
22#endif
23
24 int botan_hotp_init(botan_hotp_t* hotp,
25 const uint8_t key[], size_t key_len,
26 const char* hash_algo,
27 size_t digits)
28 {
29 if(hotp == nullptr || key == nullptr || hash_algo == nullptr)
30 return BOTAN_FFI_ERROR_NULL_POINTER;
31
32 *hotp = nullptr;
33
34#if defined(BOTAN_HAS_HOTP)
35 return ffi_guard_thunk(__func__, [=]() -> int {
36
37 *hotp = new botan_hotp_struct(
38 new Botan::HOTP(key, key_len, hash_algo, digits));
39
40 return BOTAN_FFI_SUCCESS;
41 });
42#else
43 BOTAN_UNUSED(hotp, key, key_len, hash_algo, digits);
44 return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
45#endif
46 }
47
48 int botan_hotp_destroy(botan_hotp_t hotp)
49 {
50#if defined(BOTAN_HAS_HOTP)
51 return BOTAN_FFI_CHECKED_DELETE(hotp);
52#else
53 BOTAN_UNUSED(hotp);
54 return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
55#endif
56 }
57
58 int botan_hotp_generate(botan_hotp_t hotp,
59 uint32_t* hotp_code,
60 uint64_t hotp_counter)
61 {
62#if defined(BOTAN_HAS_HOTP)
63 if(hotp == nullptr || hotp_code == nullptr)
64 return BOTAN_FFI_ERROR_NULL_POINTER;
65
66 return BOTAN_FFI_DO(Botan::HOTP, hotp, h, {
67 *hotp_code = h.generate_hotp(hotp_counter);
68 });
69
70#else
71 BOTAN_UNUSED(hotp, hotp_code, hotp_counter);
72 return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
73#endif
74 }
75
76 int botan_hotp_check(botan_hotp_t hotp,
77 uint64_t* next_hotp_counter,
78 uint32_t hotp_code,
79 uint64_t hotp_counter,
80 size_t resync_range)
81 {
82#if defined(BOTAN_HAS_HOTP)
83 return BOTAN_FFI_RETURNING(Botan::HOTP, hotp, h, {
84
85 auto resp = h.verify_hotp(hotp_code, hotp_counter, resync_range);
86
87 if(next_hotp_counter)
88 *next_hotp_counter = resp.second;
89
90 return (resp.first == true) ? BOTAN_FFI_SUCCESS : BOTAN_FFI_INVALID_VERIFIER;
91 });
92
93#else
94 BOTAN_UNUSED(hotp, next_hotp_counter, hotp_code, hotp_counter, resync_range);
95 return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
96#endif
97 }
98
99}
BOTAN_UNUSED
#define BOTAN_UNUSED(...)
Definition: assert.h:142
Botan::HOTP
Definition: otp.h:20
Botan::HOTP::generate_hotp
uint32_t generate_hotp(uint64_t counter)
Definition: hotp.cpp:42
Botan::HOTP::verify_hotp
std::pair< bool, uint64_t > verify_hotp(uint32_t otp, uint64_t starting_counter, size_t resync_range=0)
Definition: hotp.cpp:52
botan_hotp_t
struct botan_hotp_struct * botan_hotp_t
Definition: ffi.h:1663
BOTAN_FFI_ERROR_NOT_IMPLEMENTED
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:83
BOTAN_FFI_INVALID_VERIFIER
@ BOTAN_FFI_INVALID_VERIFIER
Definition: ffi.h:64
BOTAN_FFI_ERROR_NULL_POINTER
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:77
BOTAN_FFI_SUCCESS
@ BOTAN_FFI_SUCCESS
Definition: ffi.h:63
botan_hotp_generate
int botan_hotp_generate(botan_hotp_t hotp, uint32_t *hotp_code, uint64_t hotp_counter)
Definition: ffi_hotp.cpp:58
botan_hotp_destroy
int botan_hotp_destroy(botan_hotp_t hotp)
Definition: ffi_hotp.cpp:48
botan_hotp_check
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:76
botan_hotp_init
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:24
BOTAN_FFI_DO
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:92
BOTAN_FFI_CHECKED_DELETE
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:129
BOTAN_FFI_RETURNING
#define BOTAN_FFI_RETURNING(T, obj, param, block)
Definition: ffi_util.h:101
BOTAN_FFI_DECLARE_STRUCT
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition: ffi_util.h:53
Botan_FFI
Definition: ffi.cpp:17
Botan_FFI::ffi_guard_thunk
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:89
hash_algo
AlgorithmIdentifier hash_algo
Definition: x509_obj.cpp:22