Botan 3.13.0
Crypto and TLS for C&
ffi_block.cpp
Go to the documentation of this file.
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#include <utility>
12
13extern "C" {
14
15using namespace Botan_FFI;
16
17BOTAN_FFI_DECLARE_STRUCT(botan_block_cipher_struct, Botan::BlockCipher, 0x64C29716);
18
19int botan_block_cipher_init(botan_block_cipher_t* bc, const char* bc_name) {
20 return ffi_guard_thunk(__func__, [=]() -> int {
21 if(bc == nullptr || bc_name == nullptr || *bc_name == 0) {
23 }
24
25 *bc = nullptr;
26
27 auto cipher = Botan::BlockCipher::create(bc_name);
28 if(cipher == nullptr) {
30 }
31
32 return ffi_new_object(bc, std::move(cipher));
33 });
34}
35
36/**
37* Destroy a block cipher object
38*/
42
44 return BOTAN_FFI_VISIT(bc, [](auto& b) { b.clear(); });
45}
46
47/**
48* Set the key for a block cipher instance
49*/
50int botan_block_cipher_set_key(botan_block_cipher_t bc, const uint8_t key[], size_t len) {
51 if(key == nullptr) {
53 }
54 return BOTAN_FFI_VISIT(bc, [=](auto& b) { b.set_key(key, len); });
55}
56
57/**
58* Return the positive block size of this block cipher, or negative to
59* indicate an error
60*/
62 return BOTAN_FFI_VISIT(bc, [](const auto& b) -> int {
63 const size_t bs = b.block_size();
64 if(bs == 0) {
66 }
67
68 if(std::in_range<int>(bs)) {
69 return static_cast<int>(bs);
70 } else {
72 }
73 });
74}
75
76int botan_block_cipher_encrypt_blocks(botan_block_cipher_t bc, const uint8_t in[], uint8_t out[], size_t blocks) {
77 if(any_null_pointers(in, out)) {
79 }
80 return BOTAN_FFI_VISIT(bc, [=](const auto& b) { b.encrypt_n(in, out, blocks); });
81}
82
83int botan_block_cipher_decrypt_blocks(botan_block_cipher_t bc, const uint8_t in[], uint8_t out[], size_t blocks) {
84 if(any_null_pointers(in, out)) {
86 }
87 return BOTAN_FFI_VISIT(bc, [=](const auto& b) { b.decrypt_n(in, out, blocks); });
88}
89
90int botan_block_cipher_name(botan_block_cipher_t cipher, char* name, size_t* name_len) {
91 if(name_len == nullptr) {
93 }
94
95 return BOTAN_FFI_VISIT(cipher, [=](const auto& bc) { return write_str_output(name, name_len, bc.name()); });
96}
97
99 size_t* out_minimum_keylength,
100 size_t* out_maximum_keylength,
101 size_t* out_keylength_modulo) {
102 return BOTAN_FFI_VISIT(cipher, [=](const auto& bc) {
103 if(out_minimum_keylength) {
104 *out_minimum_keylength = bc.minimum_keylength();
105 }
106 if(out_maximum_keylength) {
107 *out_maximum_keylength = bc.maximum_keylength();
108 }
109 if(out_keylength_modulo) {
110 *out_keylength_modulo = bc.key_spec().keylength_multiple();
111 }
112 });
113}
114}
static std::unique_ptr< BlockCipher > create(std::string_view algo_spec, std::string_view provider="")
struct botan_block_cipher_struct * botan_block_cipher_t
Definition ffi.h:974
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131
@ BOTAN_FFI_ERROR_INVALID_OBJECT_STATE
Definition ffi.h:135
int botan_block_cipher_get_keyspec(botan_block_cipher_t cipher, size_t *out_minimum_keylength, size_t *out_maximum_keylength, size_t *out_keylength_modulo)
Definition ffi_block.cpp:98
int botan_block_cipher_clear(botan_block_cipher_t bc)
Definition ffi_block.cpp:43
int botan_block_cipher_init(botan_block_cipher_t *bc, const char *bc_name)
Definition ffi_block.cpp:19
int botan_block_cipher_block_size(botan_block_cipher_t bc)
Definition ffi_block.cpp:61
int botan_block_cipher_decrypt_blocks(botan_block_cipher_t bc, const uint8_t in[], uint8_t out[], size_t blocks)
Definition ffi_block.cpp:83
int botan_block_cipher_name(botan_block_cipher_t cipher, char *name, size_t *name_len)
Definition ffi_block.cpp:90
int botan_block_cipher_destroy(botan_block_cipher_t bc)
Definition ffi_block.cpp:39
int botan_block_cipher_encrypt_blocks(botan_block_cipher_t bc, const uint8_t in[], uint8_t out[], size_t blocks)
Definition ffi_block.cpp:76
int botan_block_cipher_set_key(botan_block_cipher_t bc, const uint8_t key[], size_t len)
Definition ffi_block.cpp:50
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:158
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:188
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition ffi_util.h:61
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
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:271