authme-unix/src/mpass_openssl.c
authme_err_t
mpass_openssl_cipher_file_to_file(mpass_handle mh, unsigned char * key, FILE * in, FILE * out, int encrypt)
{
	EVP_CIPHER_CTX *ctx;
	unsigned char aes_iv[EVP_MAX_IV_LENGTH];
	unsigned char in_buf[1024];
	unsigned char out_buf[1024 + EVP_MAX_BLOCK_LENGTH];
	int bytes_read, i;
	int aes_iv_len;
	authme_master_password_t * amp = (authme_master_password_t *)mh;

	// Now start creating keys and things
	if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
	{
		// Also bad
		MPASS_SET_ERROR(amp, strdup(ERR_error_string(ERR_get_error(), NULL)));
		return AUTHME_ERR_CRYPTO_INIT;
	}

	/* Set up the IV */
	aes_iv_len = 16;
	if (encrypt)
	{
		if (RAND_bytes(aes_iv, sizeof(aes_iv)) != 1)
		{
			// A bad start!
			MPASS_SET_ERROR(amp, strdup(ERR_error_string(ERR_get_error(), NULL)));
			return AUTHME_ERR_CRYPTO_INIT;
		}
		else
		{
			fwrite(aes_iv, 1, aes_iv_len, out);
		}
	}
	else
	{
		if (fread(aes_iv, 1, aes_iv_len, in) != aes_iv_len)
		{
			MPASS_SET_ERROR(amp, "Not enough bytes to load IV from input file");
			return AUTHME_ERR_CRYPTO_INIT;
		}
	}

	/* Set up the cipher */
	EVP_CipherInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, aes_iv, encrypt);

	/* Now we loop */
	while (!feof(in))
	{
		bytes_read = (int)fread(in_buf, 1, 1024, in);
		i = 1024 + EVP_MAX_BLOCK_LENGTH;

		if (!EVP_CipherUpdate(ctx, out_buf, &i, in_buf, bytes_read))
		{
			EVP_CIPHER_CTX_free(ctx);
			MPASS_SET_ERROR(amp, strdup(ERR_error_string(ERR_get_error(), NULL)));
			return AUTHME_ERR_CRYPTO_OPERATION;
		}

		fwrite(out_buf, 1, i, out);
	}

	i = 1024 + EVP_MAX_BLOCK_LENGTH;
	if (!EVP_CipherFinal_ex(ctx, out_buf, &i))
	{
		EVP_CIPHER_CTX_free(ctx);
		MPASS_SET_ERROR(amp, (ERR_error_string(ERR_get_error(), NULL)));
		return AUTHME_ERR_CRYPTO_OPERATION;
	}

	fwrite(out_buf, 1, i, out);

	// Done the AES part
	EVP_CIPHER_CTX_free(ctx);

	return AUTHME_ERR_OK;
}
/* ---------------------------------------------------------------- *
* Read from in, encrypt and write to out  AES 256 is used
* ---------------------------------------------------------------- */
Read from in, encrypt and write to out  AES 256 is used
Read from in encrypt and write to out AES 256 is used
authme_err_t
mpass_openssl_cipher_file_to_file(mpass_handle mh, unsigned char * key, FILE * in, FILE * out, int encrypt)
{
	EVP_CIPHER_CTX *ctx;
	unsigned char aes_iv[EVP_MAX_IV_LENGTH];
	unsigned char in_buf[1024];
	unsigned char out_buf[1024 + EVP_MAX_BLOCK_LENGTH];
	int bytes_read, i;
	int aes_iv_len;
	authme_master_password_t * amp = (authme_master_password_t *)mh;
	if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
	{
		MPASS_SET_ERROR(amp, strdup(ERR_error_string(ERR_get_error(), NULL)));
		return AUTHME_ERR_CRYPTO_INIT;
	}
	aes_iv_len = 16;
	if (encrypt)
	{
		if (RAND_bytes(aes_iv, sizeof(aes_iv)) != 1)
		{
			MPASS_SET_ERROR(amp, strdup(ERR_error_string(ERR_get_error(), NULL)));
			return AUTHME_ERR_CRYPTO_INIT;
		}
		else
		{
			fwrite(aes_iv, 1, aes_iv_len, out);
		}
	}
	else
	{
		if (fread(aes_iv, 1, aes_iv_len, in) != aes_iv_len)
		{
			MPASS_SET_ERROR(amp, "Not enough bytes to load IV from input file");
			return AUTHME_ERR_CRYPTO_INIT;
		}
	}
	EVP_CipherInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, aes_iv, encrypt);
	while (!feof(in))
	{
		bytes_read = (int)fread(in_buf, 1, 1024, in);
		i = 1024 + EVP_MAX_BLOCK_LENGTH;
		if (!EVP_CipherUpdate(ctx, out_buf, &i, in_buf, bytes_read))
		{
			EVP_CIPHER_CTX_free(ctx);
			MPASS_SET_ERROR(amp, strdup(ERR_error_string(ERR_get_error(), NULL)));
			return AUTHME_ERR_CRYPTO_OPERATION;
		}
		fwrite(out_buf, 1, i, out);
	}
	i = 1024 + EVP_MAX_BLOCK_LENGTH;
	if (!EVP_CipherFinal_ex(ctx, out_buf, &i))
	{
		EVP_CIPHER_CTX_free(ctx);
		MPASS_SET_ERROR(amp, (ERR_error_string(ERR_get_error(), NULL)));
		return AUTHME_ERR_CRYPTO_OPERATION;
	}
	fwrite(out_buf, 1, i, out);
	EVP_CIPHER_CTX_free(ctx);
	return AUTHME_ERR_OK;
}
authme_err_t mpass_openssl_cipher_file_to_file ( mpass_handle mh , unsigned char * key , FILE * in , FILE * out , int encrypt ) { EVP_CIPHER_CTX * ctx ; unsigned char aes_iv [ EVP_MAX_IV_LENGTH ] ; unsigned char in_buf [ 1024 ] ; unsigned char out_buf [ 1024 + EVP_MAX_BLOCK_LENGTH ] ; int bytes_read , i ; int aes_iv_len ; authme_master_password_t * amp = ( authme_master_password_t * ) mh ; if ( ( ctx = EVP_CIPHER_CTX_new ( ) ) == NULL ) { MPASS_SET_ERROR ( amp , strdup ( ERR_error_string ( ERR_get_error ( ) , NULL ) ) ) ; return AUTHME_ERR_CRYPTO_INIT ; } aes_iv_len = 16 ; if ( encrypt ) { if ( RAND_bytes ( aes_iv , sizeof ( aes_iv ) ) != 1 ) { MPASS_SET_ERROR ( amp , strdup ( ERR_error_string ( ERR_get_error ( ) , NULL ) ) ) ; return AUTHME_ERR_CRYPTO_INIT ; } else { fwrite ( aes_iv , 1 , aes_iv_len , out ) ; } } else { if ( fread ( aes_iv , 1 , aes_iv_len , in ) != aes_iv_len ) { MPASS_SET_ERROR ( amp , " " ) ; return AUTHME_ERR_CRYPTO_INIT ; } } EVP_CipherInit_ex ( ctx , EVP_aes_256_cbc ( ) , NULL , key , aes_iv , encrypt ) ; while ( ! feof ( in ) ) { bytes_read = ( int ) fread ( in_buf , 1 , 1024 , in ) ; i = 1024 + EVP_MAX_BLOCK_LENGTH ; if ( ! EVP_CipherUpdate ( ctx , out_buf , & i , in_buf , bytes_read ) ) { EVP_CIPHER_CTX_free ( ctx ) ; MPASS_SET_ERROR ( amp , strdup ( ERR_error_string ( ERR_get_error ( ) , NULL ) ) ) ; return AUTHME_ERR_CRYPTO_OPERATION ; } fwrite ( out_buf , 1 , i , out ) ; } i = 1024 + EVP_MAX_BLOCK_LENGTH ; if ( ! EVP_CipherFinal_ex ( ctx , out_buf , & i ) ) { EVP_CIPHER_CTX_free ( ctx ) ; MPASS_SET_ERROR ( amp , ( ERR_error_string ( ERR_get_error ( ) , NULL ) ) ) ; return AUTHME_ERR_CRYPTO_OPERATION ; } fwrite ( out_buf , 1 , i , out ) ; EVP_CIPHER_CTX_free ( ctx ) ; return AUTHME_ERR_OK ; }
Read from in, encrypt and write to out  AES 256 is used
Read from in encrypt and write to out AES 256 is used
// Now start creating keys and things // Also bad /* Set up the IV */ // A bad start! /* Set up the cipher */ /* Now we loop */ // Done the AES part