Botan 3.12.0
Crypto and TLS for C&
ffi_keywrap.cpp File Reference
#include <botan/ffi.h>
#include <botan/assert.h>
#include <botan/internal/ffi_util.h>

Go to the source code of this file.

Functions

int botan_key_unwrap3394 (const uint8_t wrapped_key[], size_t wrapped_key_len, const uint8_t kek[], size_t kek_len, uint8_t key[], size_t *key_len)
int botan_key_wrap3394 (const uint8_t key[], size_t key_len, const uint8_t kek[], size_t kek_len, uint8_t wrapped_key[], size_t *wrapped_key_len)
int botan_nist_kw_dec (const char *cipher_algo, int padded, const uint8_t wrapped_key[], size_t wrapped_key_len, const uint8_t kek[], size_t kek_len, uint8_t key[], size_t *key_len)
int botan_nist_kw_enc (const char *cipher_algo, int padded, const uint8_t key[], size_t key_len, const uint8_t kek[], size_t kek_len, uint8_t wrapped_key[], size_t *wrapped_key_len)

Function Documentation

◆ botan_key_unwrap3394()

int botan_key_unwrap3394 ( const uint8_t wrapped_key[],
size_t wrapped_key_len,
const uint8_t kek[],
size_t kek_len,
uint8_t key[],
size_t * key_len )

Definition at line 104 of file ffi_keywrap.cpp.

109 {
110 const std::string cipher_name = "AES-" + std::to_string(8 * kek_len);
111
112 return botan_nist_kw_dec(cipher_name.c_str(), 0, wrapped_key, wrapped_key_len, kek, kek_len, key, key_len);
113}
int botan_nist_kw_dec(const char *cipher_algo, int padded, const uint8_t wrapped_key[], size_t wrapped_key_len, const uint8_t kek[], size_t kek_len, uint8_t key[], size_t *key_len)

References botan_nist_kw_dec().

◆ botan_key_wrap3394()

int botan_key_wrap3394 ( const uint8_t key[],
size_t key_len,
const uint8_t kek[],
size_t kek_len,
uint8_t wrapped_key[],
size_t * wrapped_key_len )

Key wrapping as per RFC 3394

Definition at line 93 of file ffi_keywrap.cpp.

98 {
99 const std::string cipher_name = "AES-" + std::to_string(8 * kek_len);
100
101 return botan_nist_kw_enc(cipher_name.c_str(), 0, key, key_len, kek, kek_len, wrapped_key, wrapped_key_len);
102}
int botan_nist_kw_enc(const char *cipher_algo, int padded, const uint8_t key[], size_t key_len, const uint8_t kek[], size_t kek_len, uint8_t wrapped_key[], size_t *wrapped_key_len)

References botan_nist_kw_enc().

◆ botan_nist_kw_dec()

int botan_nist_kw_dec ( const char * cipher_algo,
int padded,
const uint8_t wrapped_key[],
size_t wrapped_key_len,
const uint8_t kek[],
size_t kek_len,
uint8_t key[],
size_t * key_len )

Definition at line 57 of file ffi_keywrap.cpp.

64 {
65 if(any_null_pointers(cipher_algo, wrapped_key, kek)) {
67 }
68#if defined(BOTAN_HAS_NIST_KEYWRAP)
69 return ffi_guard_thunk(__func__, [=]() -> int {
70 if(padded != 0 && padded != 1) {
72 }
73
74 auto bc = Botan::BlockCipher::create_or_throw(cipher_algo);
75 bc->set_key(kek, kek_len);
76
78
79 if(padded == 0) {
80 output = Botan::nist_key_unwrap(wrapped_key, wrapped_key_len, *bc);
81 } else {
82 output = Botan::nist_key_unwrap_padded(wrapped_key, wrapped_key_len, *bc);
83 }
84
85 return write_vec_output(key, key_len, output);
86 });
87#else
88 BOTAN_UNUSED(cipher_algo, padded, key, key_len, kek, kek_len, wrapped_key, wrapped_key_len);
90#endif
91}
#define BOTAN_UNUSED
Definition assert.h:144
static std::unique_ptr< BlockCipher > create_or_throw(std::string_view algo_spec, std::string_view provider="")
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:140
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:133
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:95
int write_vec_output(uint8_t out[], size_t *out_len, std::span< const uint8_t > buf)
Definition ffi_util.h:264
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54
secure_vector< uint8_t > nist_key_unwrap_padded(const uint8_t input[], size_t input_len, const BlockCipher &bc)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68
secure_vector< uint8_t > nist_key_unwrap(const uint8_t input[], size_t input_len, const BlockCipher &bc)

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan::BlockCipher::create_or_throw(), Botan_FFI::ffi_guard_thunk(), Botan::nist_key_unwrap(), Botan::nist_key_unwrap_padded(), and Botan_FFI::write_vec_output().

Referenced by botan_key_unwrap3394().

◆ botan_nist_kw_enc()

int botan_nist_kw_enc ( const char * cipher_algo,
int padded,
const uint8_t key[],
size_t key_len,
const uint8_t kek[],
size_t kek_len,
uint8_t wrapped_key[],
size_t * wrapped_key_len )

Definition at line 22 of file ffi_keywrap.cpp.

29 {
30 if(any_null_pointers(cipher_algo, key, kek)) {
32 }
33#if defined(BOTAN_HAS_NIST_KEYWRAP)
34 return ffi_guard_thunk(__func__, [=]() -> int {
35 if(padded != 0 && padded != 1) {
37 }
38 auto bc = Botan::BlockCipher::create_or_throw(cipher_algo);
39 bc->set_key(kek, kek_len);
40
41 std::vector<uint8_t> output;
42
43 if(padded == 0) {
44 output = Botan::nist_key_wrap(key, key_len, *bc);
45 } else {
46 output = Botan::nist_key_wrap_padded(key, key_len, *bc);
47 }
48
49 return write_vec_output(wrapped_key, wrapped_key_len, output);
50 });
51#else
52 BOTAN_UNUSED(cipher_algo, padded, key, key_len, kek, kek_len, wrapped_key, wrapped_key_len);
54#endif
55}
std::vector< uint8_t > nist_key_wrap(const uint8_t input[], size_t input_len, const BlockCipher &bc)
std::vector< uint8_t > nist_key_wrap_padded(const uint8_t input[], size_t input_len, const BlockCipher &bc)

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan::BlockCipher::create_or_throw(), Botan_FFI::ffi_guard_thunk(), Botan::nist_key_wrap(), Botan::nist_key_wrap_padded(), and Botan_FFI::write_vec_output().

Referenced by botan_key_wrap3394().