Botan  2.12.1
Crypto and TLS for C++11
ffi_fpe.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 #include <botan/internal/ffi_mp.h>
10 #include <memory>
11 
12 #if defined(BOTAN_HAS_FPE_FE1)
13  #include <botan/fpe_fe1.h>
14 #endif
15 
16 extern "C" {
17 
18 using namespace Botan_FFI;
19 
20 #if defined(BOTAN_HAS_FPE_FE1)
21 
22 BOTAN_FFI_DECLARE_STRUCT(botan_fpe_struct, Botan::FPE_FE1, 0xD49FB820);
23 
24 #endif
25 
27  const uint8_t key[], size_t key_len,
28  size_t rounds, uint32_t flags)
29  {
30 #if defined(BOTAN_HAS_FPE_FE1)
31 
32  return ffi_guard_thunk(__func__, [=]() {
33 
34  if(fpe == nullptr || key == nullptr)
36 
37  *fpe = nullptr;
38 
41 
42  const bool compat_mode = (flags & BOTAN_FPE_FLAG_FE1_COMPAT_MODE);
43 
44  std::unique_ptr<Botan::FPE_FE1> fpe_obj(
45  new Botan::FPE_FE1(safe_get(n), rounds, compat_mode));
46 
47  fpe_obj->set_key(key, key_len);
48 
49  *fpe = new botan_fpe_struct(fpe_obj.release());
50  return BOTAN_FFI_SUCCESS;
51  });
52 #else
53  *fpe = nullptr;
55 #endif
56  }
57 
59  {
60 #if defined(BOTAN_HAS_FPE_FE1)
61  return BOTAN_FFI_CHECKED_DELETE(fpe);
62 #else
64 #endif
65  }
66 
67 int botan_fpe_encrypt(botan_fpe_t fpe, botan_mp_t x, const uint8_t tweak[], size_t tweak_len)
68  {
69 #if defined(BOTAN_HAS_FPE_FE1)
70  return ffi_guard_thunk(__func__, [=]() {
71  Botan::BigInt r = safe_get(fpe).encrypt(safe_get(x), tweak, tweak_len);
72  safe_get(x) = r;
73  return BOTAN_FFI_SUCCESS;
74  });
75 #else
77 #endif
78  }
79 
80 int botan_fpe_decrypt(botan_fpe_t fpe, botan_mp_t x, const uint8_t tweak[], size_t tweak_len)
81  {
82 #if defined(BOTAN_HAS_FPE_FE1)
83  return ffi_guard_thunk(__func__, [=]() {
84  Botan::BigInt r = safe_get(fpe).decrypt(safe_get(x), tweak, tweak_len);
85  safe_get(x) = r;
86  return BOTAN_FFI_SUCCESS;
87  });
88 
89 #else
91 #endif
92  }
93 
94 }
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:86
int botan_fpe_destroy(botan_fpe_t fpe)
Definition: ffi_fpe.cpp:58
Flags flags(Flag flags)
Definition: p11.h:858
struct botan_mp_struct * botan_mp_t
Definition: ffi.h:749
#define BOTAN_FPE_FLAG_FE1_COMPAT_MODE
Definition: ffi.h:1701
int botan_fpe_fe1_init(botan_fpe_t *fpe, botan_mp_t n, const uint8_t key[], size_t key_len, size_t rounds, uint32_t flags)
Definition: ffi_fpe.cpp:26
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:120
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:61
struct botan_fpe_struct * botan_fpe_t
Definition: ffi.h:1699
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition: ffi_util.h:53
int botan_fpe_decrypt(botan_fpe_t fpe, botan_mp_t x, const uint8_t tweak[], size_t tweak_len)
Definition: ffi_fpe.cpp:80
int botan_fpe_encrypt(botan_fpe_t fpe, botan_mp_t x, const uint8_t tweak[], size_t tweak_len)
Definition: ffi_fpe.cpp:67