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