/src/Botan-3.4.0/src/lib/ffi/ffi_block.cpp
Line | Count | Source |
1 | | /* |
2 | | * (C) 2015,2017 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/block_cipher.h> |
10 | | #include <botan/internal/ffi_util.h> |
11 | | |
12 | | extern "C" { |
13 | | |
14 | | using namespace Botan_FFI; |
15 | | |
16 | | BOTAN_FFI_DECLARE_STRUCT(botan_block_cipher_struct, Botan::BlockCipher, 0x64C29716); |
17 | | |
18 | 0 | int botan_block_cipher_init(botan_block_cipher_t* bc, const char* bc_name) { |
19 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
20 | 0 | if(bc == nullptr || bc_name == nullptr || *bc_name == 0) { |
21 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
22 | 0 | } |
23 | | |
24 | 0 | *bc = nullptr; |
25 | |
|
26 | 0 | auto cipher = Botan::BlockCipher::create(bc_name); |
27 | 0 | if(cipher == nullptr) { |
28 | 0 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
29 | 0 | } |
30 | | |
31 | 0 | *bc = new botan_block_cipher_struct(std::move(cipher)); |
32 | 0 | return BOTAN_FFI_SUCCESS; |
33 | 0 | }); |
34 | 0 | } |
35 | | |
36 | | /** |
37 | | * Destroy a block cipher object |
38 | | */ |
39 | 0 | int botan_block_cipher_destroy(botan_block_cipher_t bc) { |
40 | 0 | return BOTAN_FFI_CHECKED_DELETE(bc); |
41 | 0 | } |
42 | | |
43 | 0 | int botan_block_cipher_clear(botan_block_cipher_t bc) { |
44 | 0 | return BOTAN_FFI_VISIT(bc, [](auto& b) { b.clear(); }); |
45 | 0 | } |
46 | | |
47 | | /** |
48 | | * Set the key for a block cipher instance |
49 | | */ |
50 | 0 | int botan_block_cipher_set_key(botan_block_cipher_t bc, const uint8_t key[], size_t len) { |
51 | 0 | if(key == nullptr) { |
52 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
53 | 0 | } |
54 | 0 | return BOTAN_FFI_VISIT(bc, [=](auto& b) { b.set_key(key, len); }); |
55 | 0 | } |
56 | | |
57 | | /** |
58 | | * Return the positive block size of this block cipher, or negative to |
59 | | * indicate an error |
60 | | */ |
61 | 0 | int botan_block_cipher_block_size(botan_block_cipher_t bc) { |
62 | 0 | return BOTAN_FFI_VISIT(bc, [](const auto& b) { return static_cast<int>(b.block_size()); }); |
63 | 0 | } |
64 | | |
65 | 0 | int botan_block_cipher_encrypt_blocks(botan_block_cipher_t bc, const uint8_t in[], uint8_t out[], size_t blocks) { |
66 | 0 | if(in == nullptr || out == nullptr) { |
67 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
68 | 0 | } |
69 | 0 | return BOTAN_FFI_VISIT(bc, [=](const auto& b) { b.encrypt_n(in, out, blocks); }); |
70 | 0 | } |
71 | | |
72 | 0 | int botan_block_cipher_decrypt_blocks(botan_block_cipher_t bc, const uint8_t in[], uint8_t out[], size_t blocks) { |
73 | 0 | if(in == nullptr || out == nullptr) { |
74 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
75 | 0 | } |
76 | 0 | return BOTAN_FFI_VISIT(bc, [=](const auto& b) { b.decrypt_n(in, out, blocks); }); |
77 | 0 | } |
78 | | |
79 | 0 | int botan_block_cipher_name(botan_block_cipher_t cipher, char* name, size_t* name_len) { |
80 | 0 | if(name_len == nullptr) { |
81 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
82 | 0 | } |
83 | | |
84 | 0 | return BOTAN_FFI_VISIT(cipher, [=](const auto& bc) { return write_str_output(name, name_len, bc.name()); }); |
85 | 0 | } |
86 | | |
87 | | int botan_block_cipher_get_keyspec(botan_block_cipher_t cipher, |
88 | | size_t* out_minimum_keylength, |
89 | | size_t* out_maximum_keylength, |
90 | 0 | size_t* out_keylength_modulo) { |
91 | 0 | return BOTAN_FFI_VISIT(cipher, [=](const auto& bc) { |
92 | 0 | if(out_minimum_keylength) |
93 | 0 | *out_minimum_keylength = bc.minimum_keylength(); |
94 | 0 | if(out_maximum_keylength) |
95 | 0 | *out_maximum_keylength = bc.maximum_keylength(); |
96 | 0 | if(out_keylength_modulo) |
97 | 0 | *out_keylength_modulo = bc.key_spec().keylength_multiple(); |
98 | 0 | }); |
99 | 0 | } |
100 | | } |