From 3a343cc41a1d6cda60ecff5c7bfacd503701f0cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Sun, 19 Oct 2008 07:56:00 +0000 Subject: [PATCH] add EVP_CIPHER_CTX_ctrl and EVP_CIPHER_CTX_rand_key git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@23949 ec53bebd-3082-4978-b11e-865c3cabbd6b --- lib/hcrypto/evp.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/lib/hcrypto/evp.c b/lib/hcrypto/evp.c index 382ec5d0b..517ca2a2b 100644 --- a/lib/hcrypto/evp.c +++ b/lib/hcrypto/evp.c @@ -1626,6 +1626,43 @@ EVP_BytesToKey(const EVP_CIPHER *type, return EVP_CIPHER_key_length(type); } +/** + * Generate a random key for the specificed EVP_CIPHER. + * + * @param ctx EVP_CIPHER_CTX type to build the key for. + * @param key return key, must be at least EVP_CIPHER_key_length() byte long. + * + * @return 1 for success, 0 for failure. + * + * @ingroup hcrypto_core + */ + +int +EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, void *key) +{ + if (ctx->cipher->flags & EVP_CIPH_RAND_KEY) + return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key); + if (RAND_bytes(key, ctx->key_len) != 1) + return 0; + return 1; +} + +/** + * Perform a operation on a ctx + * + * @return 1 for success, 0 for failure. + * + * @ingroup hcrypto_core + */ + +int +EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *data) +{ + if (ctx->cipher == NULL || ctx->cipher->ctrl == NULL) + return 0; + return (*ctx->cipher->ctrl)(ctx, type, arg, data); +} + /** * Add all algorithms to the crypto core. *