#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <openssl/evp.h>

int main(int argc, char **argv)
{
	EVP_CIPHER_CTX *ctx;
	int res, outlen, finallen;
	unsigned char final[16];
	unsigned char out[16];
	const unsigned inlen = 16;
	const unsigned char key[16] = {
		0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x23, 0x45,
		0x67, 0x89, 0x01, 0x23, 0x45, 0x67, 0x89, 0x01
	};
	const unsigned char crypttext[16] = {
		0xad, 0xc2, 0xcd, 0x9e, 0x6e, 0x8a, 0xda, 0x0c,
		0xe7, 0x71, 0xc8, 0x75, 0x52, 0xf9, 0x7d, 0xd5
	};

	fputs(OPENSSL_VERSION_TEXT "\n", stderr);

	memset(out, '*', sizeof(out));
	memset(final, '*', sizeof(final));

	ctx = EVP_CIPHER_CTX_new();
	assert(ctx != NULL);
	fprintf(stderr, "EVP_CIPHER_CTX_new returns %p\n", ctx);

	EVP_CIPHER_CTX_set_padding(ctx, 0);

	res = EVP_CipherInit(ctx, EVP_aes_128_ecb(), key, NULL, 0);
	assert(res == 1);
	fprintf(stderr, "EVP_CipherInit returns %d\n", res);

	outlen = -1;		// for debugging
	res = EVP_CipherUpdate(ctx, out, &outlen, crypttext, sizeof(crypttext));
	assert(res == 1);
	fprintf(stderr, "EVP_CipherUpdate returns %d, outlen = %d\n", res, outlen);

	fprintf(stderr, "buf: '%.*s'\n", (int)sizeof(out), out);

	res = EVP_CipherFinal(ctx, final, &finallen);
	assert(res == 1);
	fprintf(stderr, "EVP_CipherFinal returns %d, finallen = %d\n", res, finallen);

	fprintf(stderr, "buf: '%.*s'\n", (int)sizeof(out), out);
	fprintf(stderr, "final: '%.*s'\n", (int)sizeof(final), final);

	EVP_CIPHER_CTX_free(ctx);

	exit(0);
}

