Botan 3.9.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/internal/ffi_mp.h>
10#include <botan/internal/ffi_util.h>
11#include <memory>
12
13#if defined(BOTAN_HAS_FPE_FE1)
14 #include <botan/fpe_fe1.h>
15#endif
16
17extern "C" {
18
19using namespace Botan_FFI;
20
21#if defined(BOTAN_HAS_FPE_FE1)
22
23BOTAN_FFI_DECLARE_STRUCT(botan_fpe_struct, Botan::FPE_FE1, 0xD49FB820);
24
25#endif
26
28 botan_fpe_t* fpe, botan_mp_t n, const uint8_t key[], size_t key_len, size_t rounds, uint32_t flags) {
29#if defined(BOTAN_HAS_FPE_FE1)
30 return ffi_guard_thunk(__func__, [=]() {
31 if(fpe == nullptr || key == nullptr) {
33 }
34
35 *fpe = nullptr;
36
37 if(flags != 0 && flags != BOTAN_FPE_FLAG_FE1_COMPAT_MODE) {
39 }
40
41 const bool compat_mode = (flags & BOTAN_FPE_FLAG_FE1_COMPAT_MODE) != 0;
42
43 std::unique_ptr<Botan::FPE_FE1> fpe_obj(new Botan::FPE_FE1(safe_get(n), rounds, compat_mode));
44
45 fpe_obj->set_key(key, key_len);
46
47 return ffi_new_object(fpe, std::move(fpe_obj));
48 });
49#else
50 BOTAN_UNUSED(fpe, n, key, key_len, rounds, flags);
52#endif
53}
54
56#if defined(BOTAN_HAS_FPE_FE1)
57 return BOTAN_FFI_CHECKED_DELETE(fpe);
58#else
59 BOTAN_UNUSED(fpe);
61#endif
62}
63
64int botan_fpe_encrypt(botan_fpe_t fpe, botan_mp_t x, const uint8_t tweak[], size_t tweak_len) {
65#if defined(BOTAN_HAS_FPE_FE1)
66 return ffi_guard_thunk(__func__, [=]() {
67 Botan::BigInt r = safe_get(fpe).encrypt(safe_get(x), tweak, tweak_len);
68 safe_get(x) = r;
69 return BOTAN_FFI_SUCCESS;
70 });
71#else
72 BOTAN_UNUSED(fpe, x, tweak, tweak_len);
74#endif
75}
76
77int botan_fpe_decrypt(botan_fpe_t fpe, botan_mp_t x, const uint8_t tweak[], size_t tweak_len) {
78#if defined(BOTAN_HAS_FPE_FE1)
79 return ffi_guard_thunk(__func__, [=]() {
80 Botan::BigInt r = safe_get(fpe).decrypt(safe_get(x), tweak, tweak_len);
81 safe_get(x) = r;
82 return BOTAN_FFI_SUCCESS;
83 });
84
85#else
86 BOTAN_UNUSED(fpe, x, tweak, tweak_len);
88#endif
89}
90}
#define BOTAN_UNUSED
Definition assert.h:144
struct botan_mp_struct * botan_mp_t
Definition ffi.h:921
#define BOTAN_FPE_FLAG_FE1_COMPAT_MODE
Definition ffi.h:2359
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:131
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:132
@ BOTAN_FFI_SUCCESS
Definition ffi.h:115
struct botan_fpe_struct * botan_fpe_t
Definition ffi.h:2357
int botan_fpe_decrypt(botan_fpe_t fpe, botan_mp_t x, const uint8_t tweak[], size_t tweak_len)
Definition ffi_fpe.cpp:77
int botan_fpe_destroy(botan_fpe_t fpe)
Definition ffi_fpe.cpp:55
int botan_fpe_encrypt(botan_fpe_t fpe, botan_mp_t x, const uint8_t tweak[], size_t tweak_len)
Definition ffi_fpe.cpp:64
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:27
#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