Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

Functions
ffi_totp.cpp File Reference
`#include <botan/ffi.h>`
`#include <botan/internal/ffi_util.h>`

Go to the source code of this file.

## Functions
int | botan_totp_check (botan_totp_t totp, uint32_t totp_code, uint64_t timestamp, size_t acceptable_clock_drift)
int | botan_totp_destroy (botan_totp_t totp)
int | botan_totp_generate (botan_totp_t totp, uint32_t *totp_code, uint64_t timestamp)
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)

## Function Documentation

int botan_totp_check | ( | botan_totp_t | totp,
|  | uint32_t | totp_code,
|  | uint64_t | timestamp,
|  | size_t | acceptable_clock_drift
| ) |  |

Verify a TOTP code

Parameters

  totp | the TOTP object
  totp_code | the presented OTP
  timestamp | the current local timestamp
  acceptable_clock_drift | specifies the acceptable amount of clock drift (in terms of time steps) between the two hosts.

Definition at line 77 of file ffi_totp.cpp.

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_INVALID_VERIFIER, BOTAN_FFI_RETURNING, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, and Botan::TOTP::verify_totp().

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);
85 return (ok ? BOTAN_FFI_SUCCESS : BOTAN_FFI_INVALID_VERIFIER);
86 });
87
88 #else
89 BOTAN_UNUSED(totp, totp_code, timestamp, acceptable_clock_drift);
90 return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
91 #endif
92 }
Botan::TOTP::verify_totp
bool verify_totp(uint32_t otp, std::chrono::system_clock::time_point time, size_t clock_drift_accepted=0)
Definition: totp.cpp:39
BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:82
Botan::TOTP
Definition: totp.h:18
BOTAN_FFI_RETURNING
#define BOTAN_FFI_RETURNING(T, obj, param, block)
Definition: ffi_util.h:101
BOTAN_UNUSED
#define BOTAN_UNUSED(...)
Definition: assert.h:142
BOTAN_FFI_SUCCESS
Definition: ffi.h:62
BOTAN_FFI_INVALID_VERIFIER
Definition: ffi.h:63

int botan_totp_destroy | ( | botan_totp_t | totp | ) |

Destroy a TOTP instance

Returns
  0 if success, error if invalid object handle

Definition at line 49 of file ffi_totp.cpp.

References BOTAN_FFI_CHECKED_DELETE, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, and BOTAN_UNUSED.

50 {
51 #if defined(BOTAN_HAS_TOTP)
52 return BOTAN_FFI_CHECKED_DELETE(totp);
53 #else
54 BOTAN_UNUSED(totp);
55 return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
56 #endif
57 }
BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:82
BOTAN_FFI_CHECKED_DELETE
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:129
BOTAN_UNUSED
#define BOTAN_UNUSED(...)
Definition: assert.h:142

int botan_totp_generate | ( | botan_totp_t | totp,
|  | uint32_t * | totp_code,
|  | uint64_t | timestamp
| ) |  |

Generate a TOTP code for the provided timestamp

Parameters

  totp | the TOTP object
  totp_code | the OTP code will be written here
  timestamp | the current local timestamp

Definition at line 59 of file ffi_totp.cpp.

References BOTAN_FFI_DO, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, and Botan::TOTP::generate_totp().

62 {
63 #if defined(BOTAN_HAS_TOTP)
64 if(totp == nullptr || totp_code == nullptr)
65 return BOTAN_FFI_ERROR_NULL_POINTER;
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);
73 return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
74 #endif
75 }
BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:82
Botan::TOTP
Definition: totp.h:18
BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:76
BOTAN_UNUSED
#define BOTAN_UNUSED(...)
Definition: assert.h:142
Botan::TOTP::generate_totp
uint32_t generate_totp(std::chrono::system_clock::time_point time_point)
Definition: totp.cpp:27
BOTAN_FFI_DO
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:92

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
| ) |  |

Initialize a TOTP instance

Definition at line 24 of file ffi_totp.cpp.

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, and Botan_FFI::ffi_guard_thunk().

29 {
30 if(totp == nullptr || key == nullptr || hash_algo == nullptr)
31 return BOTAN_FFI_ERROR_NULL_POINTER;
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);
45 return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
46 #endif
47 }
Botan_FFI::ffi_guard_thunk
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:89
BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:82
Botan::TOTP
Definition: totp.h:18
BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:76
BOTAN_UNUSED
#define BOTAN_UNUSED(...)
Definition: assert.h:142
hash_algo
AlgorithmIdentifier hash_algo
Definition: x509_obj.cpp:22
BOTAN_FFI_SUCCESS
Definition: ffi.h:62