Botan 3.11.0
Crypto and TLS for C&
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
9#include <botan/assert.h>
10#include <botan/internal/ffi_mp.h>
11#include <botan/internal/ffi_util.h>
12#include <memory>
13
14#if defined(BOTAN_HAS_FPE_FE1)
15 #include <botan/fpe_fe1.h>
16#endif
17
18extern "C" {
19
20using namespace Botan_FFI;
21
22#if defined(BOTAN_HAS_FPE_FE1)
23
24BOTAN_FFI_DECLARE_STRUCT(botan_fpe_struct, Botan::FPE_FE1, 0xD49FB820);
25
26#endif
27
29 botan_fpe_t* fpe, botan_mp_t n, const uint8_t key[], size_t key_len, size_t rounds, uint32_t flags) {
30#if defined(BOTAN_HAS_FPE_FE1)
31 return ffi_guard_thunk(__func__, [=]() {
32 if(fpe == nullptr || key == nullptr) {
34 }
35
36 *fpe = nullptr;
37
38 if(flags != 0 && flags != BOTAN_FPE_FLAG_FE1_COMPAT_MODE) {
40 }
41
42 const bool compat_mode = (flags & BOTAN_FPE_FLAG_FE1_COMPAT_MODE) != 0;
43
44 auto fpe_obj = std::make_unique<Botan::FPE_FE1>(safe_get(n), rounds, compat_mode);
45
46 fpe_obj->set_key(key, key_len);
47
48 return ffi_new_object(fpe, std::move(fpe_obj));
49 });
50#else
51 BOTAN_UNUSED(fpe, n, key, key_len, rounds, flags);
53#endif
54}
55
57#if defined(BOTAN_HAS_FPE_FE1)
58 return BOTAN_FFI_CHECKED_DELETE(fpe);
59#else
60 BOTAN_UNUSED(fpe);
62#endif
63}
64
65int botan_fpe_encrypt(botan_fpe_t fpe, botan_mp_t x, const uint8_t tweak[], size_t tweak_len) {
66#if defined(BOTAN_HAS_FPE_FE1)
67 return ffi_guard_thunk(__func__, [=]() {
68 const Botan::BigInt r = safe_get(fpe).encrypt(safe_get(x), tweak, tweak_len);
69 safe_get(x) = r;
70 return BOTAN_FFI_SUCCESS;
71 });
72#else
73 BOTAN_UNUSED(fpe, x, tweak, tweak_len);
75#endif
76}
77
78int botan_fpe_decrypt(botan_fpe_t fpe, botan_mp_t x, const uint8_t tweak[], size_t tweak_len) {
79#if defined(BOTAN_HAS_FPE_FE1)
80 return ffi_guard_thunk(__func__, [=]() {
81 const Botan::BigInt r = safe_get(fpe).decrypt(safe_get(x), tweak, tweak_len);
82 safe_get(x) = r;
83 return BOTAN_FFI_SUCCESS;
84 });
85
86#else
87 BOTAN_UNUSED(fpe, x, tweak, tweak_len);
89#endif
90}
91}
#define BOTAN_UNUSED
Definition assert.h:144
struct botan_mp_struct * botan_mp_t
Definition ffi.h:1003
#define BOTAN_FPE_FLAG_FE1_COMPAT_MODE
Definition ffi.h:2852
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:140
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:132
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:133
@ BOTAN_FFI_SUCCESS
Definition ffi.h:116
struct botan_fpe_struct * botan_fpe_t
Definition ffi.h:2850
int botan_fpe_decrypt(botan_fpe_t fpe, botan_mp_t x, const uint8_t tweak[], size_t tweak_len)
Definition ffi_fpe.cpp:78
int botan_fpe_destroy(botan_fpe_t fpe)
Definition ffi_fpe.cpp:56
int botan_fpe_encrypt(botan_fpe_t fpe, botan_mp_t x, const uint8_t tweak[], size_t tweak_len)
Definition ffi_fpe.cpp:65
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:28
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:185
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition ffi_util.h:61
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:79
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:178
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:95