aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/ffi/ffi_totp.cpp
blob: 7cd5750421661c7a9a9faa98ef0e87767576eecb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* (C) 2018 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/ffi.h>
#include <botan/internal/ffi_util.h>

#if defined(BOTAN_HAS_TOTP)
   #include <botan/totp.h>
#endif

extern "C" {

using namespace Botan_FFI;

#if defined(BOTAN_HAS_TOTP)

BOTAN_FFI_DECLARE_STRUCT(botan_totp_struct, Botan::TOTP, 0x3D9D2CD1);

#endif

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)
   {
   if(totp == nullptr || key == nullptr || hash_algo == nullptr)
      return BOTAN_FFI_ERROR_NULL_POINTER;

   *totp = nullptr;

#if defined(BOTAN_HAS_TOTP)
   return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {

      *totp = new botan_totp_struct(
         new Botan::TOTP(key, key_len, hash_algo, digits, time_step));

      return BOTAN_FFI_SUCCESS;
      });
#else
   BOTAN_UNUSED(totp, key, key_len, hash_algo, digits);
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
#endif
   }

int botan_totp_destroy(botan_totp_t totp)
   {
#if defined(BOTAN_HAS_TOTP)
   return BOTAN_FFI_CHECKED_DELETE(totp);
#else
   BOTAN_UNUSED(totp);
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
#endif
   }

int botan_totp_generate(botan_totp_t totp,
                        uint32_t* totp_code,
                        uint64_t timestamp)
   {
#if defined(BOTAN_HAS_TOTP)
   if(totp == nullptr || totp_code == nullptr)
      return BOTAN_FFI_ERROR_NULL_POINTER;

   return BOTAN_FFI_DO(Botan::TOTP, totp, t, {
      *totp_code = t.generate_totp(timestamp);
      });

#else
   BOTAN_UNUSED(totp, totp_code, timestamp);
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
#endif
   }

int botan_totp_check(botan_totp_t totp,
                     uint32_t totp_code,
                     uint64_t timestamp,
                     size_t acceptable_clock_drift)
   {
#if defined(BOTAN_HAS_TOTP)
   return BOTAN_FFI_DO(Botan::TOTP, totp, t, {
      const bool ok = t.verify_totp(totp_code, timestamp, acceptable_clock_drift);
      return (ok ? BOTAN_FFI_SUCCESS : BOTAN_FFI_INVALID_VERIFIER);
      });

#else
   BOTAN_UNUSED(totp, next_timestamp, totp_code, timestamp, resync_range);
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
#endif
   }

}