Botan 3.13.0
Crypto and TLS for C&
ffi_fpe.cpp File Reference
#include <botan/ffi.h>
#include <botan/assert.h>
#include <botan/internal/ffi_mp.h>
#include <botan/internal/ffi_util.h>
#include <memory>

Go to the source code of this file.

Functions

int botan_fpe_decrypt (botan_fpe_t fpe, botan_mp_t x, const uint8_t tweak[], size_t tweak_len)
int botan_fpe_destroy (botan_fpe_t fpe)
int botan_fpe_encrypt (botan_fpe_t fpe, botan_mp_t x, const uint8_t tweak[], size_t tweak_len)
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)

Function Documentation

◆ botan_fpe_decrypt()

int botan_fpe_decrypt ( botan_fpe_t fpe,
botan_mp_t x,
const uint8_t tweak[],
size_t tweak_len )

Decrypt an integer, replacing it with the result

Parameters
fpethe FPE object
xthe value to decrypt, modified in place; must be less than n
tweakthe tweak, may be NULL if tweak_len is 0
tweak_lenlength of tweak in bytes
Returns
0 on success, a negative value on failure

Definition at line 82 of file ffi_fpe.cpp.

82 {
83 if(tweak_len > 0 && tweak == nullptr) {
85 }
86
87#if defined(BOTAN_HAS_FPE_FE1)
88 return ffi_guard_thunk(__func__, [=]() {
89 const Botan::BigInt r = safe_get(fpe).decrypt(safe_get(x), tweak, tweak_len);
90 safe_get(x) = r;
91 return BOTAN_FFI_SUCCESS;
92 });
93
94#else
95 BOTAN_UNUSED(fpe, x, tweak, tweak_len);
97#endif
98}
#define BOTAN_UNUSED
Definition assert.h:144
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:79
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:95

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

◆ botan_fpe_destroy()

int botan_fpe_destroy ( botan_fpe_t fpe)

Frees all resources of the FPE object

Parameters
fpethe FPE object to destroy
Returns
0 if success, error if invalid object handle

Definition at line 56 of file ffi_fpe.cpp.

56 {
57#if defined(BOTAN_HAS_FPE_FE1)
58 return BOTAN_FFI_CHECKED_DELETE(fpe);
59#else
60 BOTAN_UNUSED(fpe);
62#endif
63}
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:188

References BOTAN_FFI_CHECKED_DELETE, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, and BOTAN_UNUSED.

◆ botan_fpe_encrypt()

int botan_fpe_encrypt ( botan_fpe_t fpe,
botan_mp_t x,
const uint8_t tweak[],
size_t tweak_len )

Encrypt an integer, replacing it with the result

Parameters
fpethe FPE object
xthe value to encrypt, modified in place; must be less than n
tweakthe tweak, may be NULL if tweak_len is 0
tweak_lenlength of tweak in bytes
Returns
0 on success, a negative value on failure

Definition at line 65 of file ffi_fpe.cpp.

65 {
66 if(tweak_len > 0 && tweak == nullptr) {
68 }
69
70#if defined(BOTAN_HAS_FPE_FE1)
71 return ffi_guard_thunk(__func__, [=]() {
72 const Botan::BigInt r = safe_get(fpe).encrypt(safe_get(x), tweak, tweak_len);
73 safe_get(x) = r;
74 return BOTAN_FFI_SUCCESS;
75 });
76#else
77 BOTAN_UNUSED(fpe, x, tweak, tweak_len);
79#endif
80}

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

◆ botan_fpe_fe1_init()

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 )

Initialize an FE1 format preserving encryption object, which encrypts integers in the range [0, n)

Parameters
fpethe new object will be placed here
nthe modulus defining the range of the permutation
keythe key
key_lenlength of key in bytes
roundsthe number of Feistel rounds; at least 16 is recommended
flags0, or BOTAN_FPE_FLAG_FE1_COMPAT_MODE to match the encoding used by Botan versions prior to 2.5
Returns
0 on success, a negative value on failure

Definition at line 28 of file ffi_fpe.cpp.

29 {
30#if defined(BOTAN_HAS_FPE_FE1)
31 return ffi_guard_thunk(__func__, [=]() {
32 if(any_null_pointers(fpe, key)) {
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}
#define BOTAN_FPE_FLAG_FE1_COMPAT_MODE
Definition ffi.h:4504
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:130
Flags flags(Flag flags)
Definition p11.h:1235
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:178
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FPE_FLAG_FE1_COMPAT_MODE, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan_FFI::ffi_new_object(), and Botan_FFI::safe_get().