error in encrypt / Decrypt

lokesh chakka <[email protected]>
Newsgroups gmane.comp.encryption.openssl.user
Message-ID <[email protected]>
hello,

Please refer to the attached sample code.

EVP_PKEY_encrypt_init is giving following error :

==============================================================
40F755D58C730000:error:03000096:digital envelope 
routines:evp_pkey_asym_cipher_init:operation not supported for this 
keytype:../crypto/evp/asymcipher.c:189:
encrypt_using_DER_public_key 52
==============================================================

Can someone help me understand how to fix the issue ?

Thanks.
Lokesh.

-- 
You received this message because you are subscribed to the Google Groups "openssl-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openssl-users+unsubscribe-MCmKBN63+Bmbup2nOX2J7Q@public.gmane.org
To view this discussion on the web visit https://groups.google.com/a/openssl.org/d/msgid/openssl-users/8785d9b5-ded1-4dad-9e9a-8dbffb346cd1n%40openssl.org.
encrypt_decrypt.c (text/x-csrc, 3.2 KB)
//gcc source/encrypt_decrypt.c -lcrypto
#include<openssl/evp.h>
#include<openssl/err.h>
#include<openssl/ec.h>

uint8_t *private_key = NULL;
int private_key_len;
uint8_t *public_key = NULL;
int public_key_len;
uint8_t * const plain_text_input = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
uint8_t cipher_text[200];
size_t cipher_text_len;
uint8_t plain_text_output[200];
size_t plain_text_output_len;

void handle_errors( char const * const function, const int line )
{
	ERR_print_errors_fp( stderr );
	fprintf( stderr, "%s %d\n", function, line );
	exit(1);
}

void generate_public_private_keys_DER()
{
	OpenSSL_add_all_algorithms();
	ERR_load_crypto_strings();
	EVP_PKEY_CTX * pctx = EVP_PKEY_CTX_new_id( EVP_PKEY_EC, NULL );
	if( pctx == NULL )														handle_errors( __func__, __LINE__ );
	if( EVP_PKEY_paramgen_init( pctx ) <= 0 )									handle_errors( __func__, __LINE__ );
	if( EVP_PKEY_CTX_set_ec_paramgen_curve_nid( pctx, NID_X9_62_prime256v1 ) <= 0 )		handle_errors( __func__, __LINE__ );
	EVP_PKEY *pkey = NULL;
	if( EVP_PKEY_paramgen( pctx, &pkey ) <= 0 )									handle_errors( __func__, __LINE__ );
	EVP_PKEY_CTX_free( pctx );
	pctx = EVP_PKEY_CTX_new( pkey, NULL );
	if( pctx == NULL )														handle_errors( __func__, __LINE__ );
	if( EVP_PKEY_keygen_init( pctx ) <= 0 )										handle_errors( __func__, __LINE__ );
	if( EVP_PKEY_keygen( pctx, &pkey ) <= 0 )									handle_errors( __func__, __LINE__ );
	private_key_len = i2d_PrivateKey( pkey, &private_key );
	if( private_key_len < 0 )												handle_errors( __func__, __LINE__ );
	public_key_len = i2d_PublicKey( pkey, &public_key );
	if( public_key_len < 0 )													handle_errors( __func__, __LINE__ );
}

void encrypt_using_DER_public_key()
{
	uint8_t const *pub_key_DER = public_key;
	EVP_PKEY *pub_key_EVP;
	if( d2i_PublicKey( EVP_PKEY_EC, &pub_key_EVP, &pub_key_DER, public_key_len ) == NULL )	handle_errors( __func__, __LINE__ );
	EVP_PKEY_CTX * const ctx = EVP_PKEY_CTX_new( pub_key_EVP, NULL );
	if( ctx == NULL )														handle_errors( __func__, __LINE__ );
	if( EVP_PKEY_encrypt_init( ctx ) <= 0 )										handle_errors( __func__, __LINE__ );
	if( EVP_PKEY_encrypt( ctx, cipher_text, &cipher_text_len, plain_text_input, strlen( plain_text_input ) ) <= 0 )
																		handle_errors( __func__, __LINE__ );
	EVP_PKEY_CTX_free( ctx );
	EVP_PKEY_free( pub_key_EVP );
}

void decrypt_using_DER_private_key()
{
	uint8_t const *pvt_key_DER = private_key;
	EVP_PKEY *pvt_key_EVP;
	if( d2i_PublicKey( EVP_PKEY_EC, &pvt_key_EVP, &pvt_key_DER, private_key_len ) == NULL )	handle_errors( __func__, __LINE__ );
	EVP_PKEY_CTX * const ctx = EVP_PKEY_CTX_new( pvt_key_EVP, NULL );
	if( ctx == NULL )														handle_errors( __func__, __LINE__ );
	if( EVP_PKEY_decrypt_init( ctx ) <= 0 )										handle_errors( __func__, __LINE__ );
	if( EVP_PKEY_decrypt( ctx, plain_text_output, &plain_text_output_len, cipher_text, cipher_text_len ) <= 0 )
																		handle_errors( __func__, __LINE__ );
	EVP_PKEY_CTX_free( ctx );
	EVP_PKEY_free( pvt_key_EVP );
	fprintf( stderr, "%s %d decrypted text is :%s\n", __func__, __LINE__, plain_text_output );
}

int main()
{
	generate_public_private_keys_DER();
	encrypt_using_DER_public_key();
	decrypt_using_DER_private_key();
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.