*
* @remarks This function is used to generate symmetric and private asymmetric keys.
*
+ * Supported key lengths:
+ * - RSA: length >= 256bits
+ * - DSA: length >= 512bits, multiple of 64
+ *
* @param[in] key_type Type of the key to be generated
* @param[in] key_bits Length of the key (in bits) to be generated
* @param[out] key Newly generated key (must be freed with yaca_key_free())
/* known errors */
} else {
switch (err) {
+ case ERR_PACK(ERR_LIB_RSA, RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS):
+ case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED):
+ ret = YACA_ERROR_INVALID_ARGUMENT;
+ break;
case ERR_PACK(ERR_LIB_PEM, PEM_F_PEM_DO_HEADER, PEM_R_BAD_DECRYPT):
case ERR_PACK(ERR_LIB_EVP, EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT):
ret = YACA_ERROR_PASSWORD_INVALID;
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/des.h>
-#include <openssl/err.h>
#include <yaca_crypto.h>
#include <yaca_error.h>
return ret;
}
- if (key_data_len > SIZE_MAX - sizeof(struct yaca_key_simple_s)) {
+ /* key_bits has to fit in size_t */
+ if (key_data_len > SIZE_MAX / 8) {
ret = YACA_ERROR_INVALID_ARGUMENT;
goto out;
}
struct yaca_key_simple_s *nk;
size_t key_byte_len = key_bits / 8;
- if (key_byte_len > SIZE_MAX - sizeof(struct yaca_key_simple_s))
- return YACA_ERROR_INVALID_ARGUMENT;
-
nk = yaca_zalloc(sizeof(struct yaca_key_simple_s) + key_byte_len);
if (nk == NULL)
return YACA_ERROR_OUT_OF_MEMORY;
struct yaca_key_simple_s *nk;
size_t key_byte_len = key_bits / 8;
- if (key_byte_len > SIZE_MAX - sizeof(struct yaca_key_simple_s))
- return YACA_ERROR_INVALID_ARGUMENT;
-
nk = yaca_zalloc(sizeof(struct yaca_key_simple_s) + key_byte_len);
if (nk == NULL)
return YACA_ERROR_OUT_OF_MEMORY;
ret = EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, key_bits);
if (ret != 1) {
- ret = YACA_ERROR_INTERNAL;
- ERROR_DUMP(ret);
+ ret = ERROR_HANDLE();
goto free_ctx;
}
assert(key_bits > 0);
assert(key_bits % 8 == 0);
+ /* Openssl generates 512-bit key for key lengths smaller than 512. It also
+ * rounds key size to multiplication of 64. */
+ if(key_bits < 512 || key_bits % 64 != 0)
+ return YACA_ERROR_INVALID_ARGUMENT;
+
int ret;
struct yaca_key_evp_s *nk;
EVP_PKEY_CTX *pctx;
ret = EVP_PKEY_CTX_set_dsa_paramgen_bits(pctx, key_bits);
if (ret != 1) {
- ret = YACA_ERROR_INTERNAL;
- ERROR_DUMP(ret);
+ ret = ERROR_HANDLE();
goto free_pctx;
}
if (key_bits % 8) /* Key length must be multiple of 8-bits */
return YACA_ERROR_INVALID_ARGUMENT;
- if (key_byte_len > SIZE_MAX - sizeof(struct yaca_key_simple_s))
- return YACA_ERROR_INVALID_ARGUMENT;
-
ret = digest_get_algorithm(algo, &md);
if (ret != YACA_ERROR_NONE)
return ret;