aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/ffi
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-08-10 15:18:44 -0400
committerJack Lloyd <[email protected]>2018-08-10 15:18:44 -0400
commitf868b8d31e91a0ae4fbc065621c2136ba898b538 (patch)
tree8e60fdd96f1c3ed31e75b6bb22a38e0b434fac3e /src/lib/ffi
parent36a7ec51b23b5d30e42480ef8a8a16468804065f (diff)
Add a function to query output length of symmetric cipher
Diffstat (limited to 'src/lib/ffi')
-rw-r--r--src/lib/ffi/ffi.h2
-rw-r--r--src/lib/ffi/ffi_cipher.cpp8
-rw-r--r--src/lib/ffi/ffi_pk_op.cpp4
-rw-r--r--src/lib/ffi/ffi_util.h5
4 files changed, 18 insertions, 1 deletions
diff --git a/src/lib/ffi/ffi.h b/src/lib/ffi/ffi.h
index 19a65f946..89ac30e27 100644
--- a/src/lib/ffi/ffi.h
+++ b/src/lib/ffi/ffi.h
@@ -416,6 +416,8 @@ BOTAN_PUBLIC_API(2,0) int botan_cipher_init(botan_cipher_t* cipher, const char*
BOTAN_PUBLIC_API(2,8) int botan_cipher_name(botan_cipher_t cipher, char* name, size_t* name_len);
+BOTAN_PUBLIC_API(2,8) int botan_cipher_output_length(botan_cipher_t cipher, size_t in_len, size_t* out_len);
+
BOTAN_PUBLIC_API(2,0) int botan_cipher_valid_nonce_length(botan_cipher_t cipher, size_t nl);
BOTAN_PUBLIC_API(2,0) int botan_cipher_get_tag_length(botan_cipher_t cipher, size_t* tag_size);
BOTAN_PUBLIC_API(2,0) int botan_cipher_get_default_nonce_length(botan_cipher_t cipher, size_t* nl);
diff --git a/src/lib/ffi/ffi_cipher.cpp b/src/lib/ffi/ffi_cipher.cpp
index 094656fd2..8dee35a81 100644
--- a/src/lib/ffi/ffi_cipher.cpp
+++ b/src/lib/ffi/ffi_cipher.cpp
@@ -41,6 +41,14 @@ int botan_cipher_clear(botan_cipher_t cipher)
return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, { c.clear(); });
}
+int botan_cipher_output_length(botan_cipher_t cipher, size_t in_len, size_t* out_len)
+ {
+ if(out_len == nullptr)
+ return BOTAN_FFI_ERROR_NULL_POINTER;
+
+ return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, { *out_len = c.output_length(in_len); });
+ }
+
int botan_cipher_query_keylen(botan_cipher_t cipher,
size_t* out_minimum_keylength,
size_t* out_maximum_keylength)
diff --git a/src/lib/ffi/ffi_pk_op.cpp b/src/lib/ffi/ffi_pk_op.cpp
index 0f18fca3e..5f2437a10 100644
--- a/src/lib/ffi/ffi_pk_op.cpp
+++ b/src/lib/ffi/ffi_pk_op.cpp
@@ -46,6 +46,8 @@ int botan_pk_op_encrypt_destroy(botan_pk_op_encrypt_t op)
int botan_pk_op_encrypt_output_length(botan_pk_op_encrypt_t op, size_t ptext_len, size_t* ctext_len)
{
+ if(ctext_len == nullptr)
+ return BOTAN_FFI_ERROR_NULL_POINTER;
return BOTAN_FFI_DO(Botan::PK_Encryptor, op, o, { *ctext_len = o.ciphertext_length(ptext_len); });
}
@@ -88,6 +90,8 @@ int botan_pk_op_decrypt_destroy(botan_pk_op_decrypt_t op)
int botan_pk_op_decrypt_output_length(botan_pk_op_decrypt_t op, size_t ctext_len, size_t* ptext_len)
{
+ if(ptext_len == nullptr)
+ return BOTAN_FFI_ERROR_NULL_POINTER;
return BOTAN_FFI_DO(Botan::PK_Decryptor, op, o, { *ptext_len = o.plaintext_length(ctext_len); });
}
diff --git a/src/lib/ffi/ffi_util.h b/src/lib/ffi/ffi_util.h
index afd359dbb..2aa0edd83 100644
--- a/src/lib/ffi/ffi_util.h
+++ b/src/lib/ffi/ffi_util.h
@@ -136,10 +136,13 @@ int ffi_delete_object(botan_struct<T, M>* obj, const char* func_name)
inline int write_output(uint8_t out[], size_t* out_len, const uint8_t buf[], size_t buf_len)
{
+ if(out_len == nullptr)
+ return BOTAN_FFI_ERROR_NULL_POINTER;
+
const size_t avail = *out_len;
*out_len = buf_len;
- if(avail >= buf_len)
+ if((avail >= buf_len) && (out != nullptr))
{
Botan::copy_mem(out, buf, buf_len);
return BOTAN_FFI_SUCCESS;