/src/Botan-3.4.0/src/lib/ffi/ffi_fpe.cpp
Line | Count | Source |
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 | | |
17 | | extern "C" { |
18 | | |
19 | | using namespace Botan_FFI; |
20 | | |
21 | | #if defined(BOTAN_HAS_FPE_FE1) |
22 | | |
23 | | BOTAN_FFI_DECLARE_STRUCT(botan_fpe_struct, Botan::FPE_FE1, 0xD49FB820); |
24 | | |
25 | | #endif |
26 | | |
27 | | int botan_fpe_fe1_init( |
28 | 0 | 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) { |
32 | | return BOTAN_FFI_ERROR_NULL_POINTER; |
33 | | } |
34 | | |
35 | | *fpe = nullptr; |
36 | | |
37 | | if(flags != 0 && flags != BOTAN_FPE_FLAG_FE1_COMPAT_MODE) { |
38 | | return BOTAN_FFI_ERROR_BAD_FLAG; |
39 | | } |
40 | | |
41 | | const bool compat_mode = (flags & BOTAN_FPE_FLAG_FE1_COMPAT_MODE); |
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 | | *fpe = new botan_fpe_struct(std::move(fpe_obj)); |
48 | | return BOTAN_FFI_SUCCESS; |
49 | | }); |
50 | | #else |
51 | 0 | BOTAN_UNUSED(fpe, n, key, key_len, rounds, flags); |
52 | 0 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
53 | 0 | #endif |
54 | 0 | } |
55 | | |
56 | 0 | int botan_fpe_destroy(botan_fpe_t fpe) { |
57 | | #if defined(BOTAN_HAS_FPE_FE1) |
58 | | return BOTAN_FFI_CHECKED_DELETE(fpe); |
59 | | #else |
60 | 0 | BOTAN_UNUSED(fpe); |
61 | 0 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
62 | 0 | #endif |
63 | 0 | } |
64 | | |
65 | 0 | int 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 | | 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 | 0 | BOTAN_UNUSED(fpe, x, tweak, tweak_len); |
74 | 0 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
75 | 0 | #endif |
76 | 0 | } |
77 | | |
78 | 0 | int 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 | | 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 | 0 | BOTAN_UNUSED(fpe, x, tweak, tweak_len); |
88 | 0 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
89 | 0 | #endif |
90 | 0 | } |
91 | | } |