diff options
Diffstat (limited to 'demos/cipher')
-rw-r--r-- | demos/cipher/Makefile | 27 | ||||
-rw-r--r-- | demos/cipher/aesccm.c | 238 | ||||
-rw-r--r-- | demos/cipher/aesgcm.c | 228 | ||||
-rw-r--r-- | demos/cipher/aeskeywrap.c | 181 | ||||
-rw-r--r-- | demos/cipher/ariacbc.c | 178 |
5 files changed, 852 insertions, 0 deletions
diff --git a/demos/cipher/Makefile b/demos/cipher/Makefile new file mode 100644 index 000000000000..81f526535e20 --- /dev/null +++ b/demos/cipher/Makefile @@ -0,0 +1,27 @@ +# Quick instruction: +# To build against an OpenSSL built in the source tree, do this: +# +# make OPENSSL_INCS_LOCATION=-I../../include OPENSSL_LIBS_LOCATION=-L../.. +# +# To run the demos when linked with a shared library (default): +# +# LD_LIBRARY_PATH=../.. ./aesccm +# LD_LIBRARY_PATH=../.. ./aesgcm +# LD_LIBRARY_PATH=../.. ./aeskeywrap +# LD_LIBRARY_PATH=../.. ./ariacbc + +CFLAGS = $(OPENSSL_INCS_LOCATION) +LDFLAGS = $(OPENSSL_LIBS_LOCATION) -lssl -lcrypto + +all: aesccm aesgcm aeskeywrap ariacbc + +aesccm: aesccm.o +aesgcm: aesgcm.o +aeskeywrap: aeskeywrap.o +ariacbc: ariacbc.o + +aesccm aesgcm aeskeywrap ariacbc: + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) + +clean: + $(RM) aesccm aesgcm aeskeywrap ariacbc *.o diff --git a/demos/cipher/aesccm.c b/demos/cipher/aesccm.c new file mode 100644 index 000000000000..5a2d4281506e --- /dev/null +++ b/demos/cipher/aesccm.c @@ -0,0 +1,238 @@ +/* + * Copyright 2013-2021 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * Simple AES CCM authenticated encryption with additional data (AEAD) + * demonstration program. + */ + +#include <stdio.h> +#include <openssl/err.h> +#include <openssl/bio.h> +#include <openssl/evp.h> +#include <openssl/core_names.h> + +/* AES-CCM test data obtained from NIST public test vectors */ + +/* AES key */ +static const unsigned char ccm_key[] = { + 0xce, 0xb0, 0x09, 0xae, 0xa4, 0x45, 0x44, 0x51, 0xfe, 0xad, 0xf0, 0xe6, + 0xb3, 0x6f, 0x45, 0x55, 0x5d, 0xd0, 0x47, 0x23, 0xba, 0xa4, 0x48, 0xe8 +}; + +/* Unique nonce to be used for this message */ +static const unsigned char ccm_nonce[] = { + 0x76, 0x40, 0x43, 0xc4, 0x94, 0x60, 0xb7 +}; + +/* + * Example of Additional Authenticated Data (AAD), i.e. unencrypted data + * which can be authenticated using the generated Tag value. + */ +static const unsigned char ccm_adata[] = { + 0x6e, 0x80, 0xdd, 0x7f, 0x1b, 0xad, 0xf3, 0xa1, 0xc9, 0xab, 0x25, 0xc7, + 0x5f, 0x10, 0xbd, 0xe7, 0x8c, 0x23, 0xfa, 0x0e, 0xb8, 0xf9, 0xaa, 0xa5, + 0x3a, 0xde, 0xfb, 0xf4, 0xcb, 0xf7, 0x8f, 0xe4 +}; + +/* Example plaintext to encrypt */ +static const unsigned char ccm_pt[] = { + 0xc8, 0xd2, 0x75, 0xf9, 0x19, 0xe1, 0x7d, 0x7f, 0xe6, 0x9c, 0x2a, 0x1f, + 0x58, 0x93, 0x9d, 0xfe, 0x4d, 0x40, 0x37, 0x91, 0xb5, 0xdf, 0x13, 0x10 +}; + +/* Expected ciphertext value */ +static const unsigned char ccm_ct[] = { + 0x8a, 0x0f, 0x3d, 0x82, 0x29, 0xe4, 0x8e, 0x74, 0x87, 0xfd, 0x95, 0xa2, + 0x8a, 0xd3, 0x92, 0xc8, 0x0b, 0x36, 0x81, 0xd4, 0xfb, 0xc7, 0xbb, 0xfd +}; + +/* Expected AEAD Tag value */ +static const unsigned char ccm_tag[] = { + 0x2d, 0xd6, 0xef, 0x1c, 0x45, 0xd4, 0xcc, 0xb7, 0x23, 0xdc, 0x07, 0x44, + 0x14, 0xdb, 0x50, 0x6d +}; + +/* + * A library context and property query can be used to select & filter + * algorithm implementations. If they are NULL then the default library + * context and properties are used. + */ +OSSL_LIB_CTX *libctx = NULL; +const char *propq = NULL; + + +int aes_ccm_encrypt(void) +{ + int ret = 0; + EVP_CIPHER_CTX *ctx; + EVP_CIPHER *cipher = NULL; + int outlen, tmplen; + size_t ccm_nonce_len = sizeof(ccm_nonce); + size_t ccm_tag_len = sizeof(ccm_tag); + unsigned char outbuf[1024]; + unsigned char outtag[16]; + OSSL_PARAM params[3] = { + OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END + }; + + printf("AES CCM Encrypt:\n"); + printf("Plaintext:\n"); + BIO_dump_fp(stdout, ccm_pt, sizeof(ccm_pt)); + + /* Create a context for the encrypt operation */ + if ((ctx = EVP_CIPHER_CTX_new()) == NULL) + goto err; + + /* Fetch the cipher implementation */ + if ((cipher = EVP_CIPHER_fetch(libctx, "AES-192-CCM", propq)) == NULL) + goto err; + + /* Set nonce length if default 96 bits is not appropriate */ + params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, + &ccm_nonce_len); + /* Set tag length */ + params[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, + NULL, ccm_tag_len); + + /* + * Initialise encrypt operation with the cipher & mode, + * nonce length and tag length parameters. + */ + if (!EVP_EncryptInit_ex2(ctx, cipher, NULL, NULL, params)) + goto err; + + /* Initialise key and nonce */ + if (!EVP_EncryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce)) + goto err; + + /* Set plaintext length: only needed if AAD is used */ + if (!EVP_EncryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_pt))) + goto err; + + /* Zero or one call to specify any AAD */ + if (!EVP_EncryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata))) + goto err; + + /* Encrypt plaintext: can only be called once */ + if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, ccm_pt, sizeof(ccm_pt))) + goto err; + + /* Output encrypted block */ + printf("Ciphertext:\n"); + BIO_dump_fp(stdout, outbuf, outlen); + + /* Finalise: note get no output for CCM */ + if (!EVP_EncryptFinal_ex(ctx, NULL, &tmplen)) + goto err; + + /* Get tag */ + params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, + outtag, ccm_tag_len); + params[1] = OSSL_PARAM_construct_end(); + + if (!EVP_CIPHER_CTX_get_params(ctx, params)) + goto err; + + /* Output tag */ + printf("Tag:\n"); + BIO_dump_fp(stdout, outtag, ccm_tag_len); + + ret = 1; +err: + if (!ret) + ERR_print_errors_fp(stderr); + + EVP_CIPHER_free(cipher); + EVP_CIPHER_CTX_free(ctx); + + return ret; +} + +int aes_ccm_decrypt(void) +{ + int ret = 0; + EVP_CIPHER_CTX *ctx; + EVP_CIPHER *cipher = NULL; + int outlen, rv; + unsigned char outbuf[1024]; + size_t ccm_nonce_len = sizeof(ccm_nonce); + OSSL_PARAM params[3] = { + OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END + }; + + printf("AES CCM Decrypt:\n"); + printf("Ciphertext:\n"); + BIO_dump_fp(stdout, ccm_ct, sizeof(ccm_ct)); + + if ((ctx = EVP_CIPHER_CTX_new()) == NULL) + goto err; + + /* Fetch the cipher implementation */ + if ((cipher = EVP_CIPHER_fetch(libctx, "AES-192-CCM", propq)) == NULL) + goto err; + + /* Set nonce length if default 96 bits is not appropriate */ + params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, + &ccm_nonce_len); + /* Set tag length */ + params[1] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, + (unsigned char *)ccm_tag, + sizeof(ccm_tag)); + /* + * Initialise decrypt operation with the cipher & mode, + * nonce length and expected tag parameters. + */ + if (!EVP_DecryptInit_ex2(ctx, cipher, NULL, NULL, params)) + goto err; + + /* Specify key and IV */ + if (!EVP_DecryptInit_ex(ctx, NULL, NULL, ccm_key, ccm_nonce)) + goto err; + + /* Set ciphertext length: only needed if we have AAD */ + if (!EVP_DecryptUpdate(ctx, NULL, &outlen, NULL, sizeof(ccm_ct))) + goto err; + + /* Zero or one call to specify any AAD */ + if (!EVP_DecryptUpdate(ctx, NULL, &outlen, ccm_adata, sizeof(ccm_adata))) + goto err; + + /* Decrypt plaintext, verify tag: can only be called once */ + rv = EVP_DecryptUpdate(ctx, outbuf, &outlen, ccm_ct, sizeof(ccm_ct)); + + /* Output decrypted block: if tag verify failed we get nothing */ + if (rv > 0) { + printf("Tag verify successful!\nPlaintext:\n"); + BIO_dump_fp(stdout, outbuf, outlen); + } else { + printf("Tag verify failed!\nPlaintext not available\n"); + goto err; + } + ret = 1; +err: + if (!ret) + ERR_print_errors_fp(stderr); + + EVP_CIPHER_free(cipher); + EVP_CIPHER_CTX_free(ctx); + + return ret; +} + +int main(int argc, char **argv) +{ + if (!aes_ccm_encrypt()) + return 1; + + if (!aes_ccm_decrypt()) + return 1; + + return 0; +} diff --git a/demos/cipher/aesgcm.c b/demos/cipher/aesgcm.c new file mode 100644 index 000000000000..aaf4000d574f --- /dev/null +++ b/demos/cipher/aesgcm.c @@ -0,0 +1,228 @@ +/* + * Copyright 2012-2021 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * Simple AES GCM authenticated encryption with additional data (AEAD) + * demonstration program. + */ + +#include <stdio.h> +#include <openssl/err.h> +#include <openssl/bio.h> +#include <openssl/evp.h> +#include <openssl/core_names.h> + +/* AES-GCM test data obtained from NIST public test vectors */ + +/* AES key */ +static const unsigned char gcm_key[] = { + 0xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66, + 0x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69, + 0xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f +}; + +/* Unique initialisation vector */ +static const unsigned char gcm_iv[] = { + 0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84 +}; + +/* Example plaintext to encrypt */ +static const unsigned char gcm_pt[] = { + 0xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e, 0xeb, 0x31, 0xb2, 0xea, + 0xcc, 0x2b, 0xf2, 0xa5 +}; + +/* + * Example of Additional Authenticated Data (AAD), i.e. unencrypted data + * which can be authenticated using the generated Tag value. + */ +static const unsigned char gcm_aad[] = { + 0x4d, 0x23, 0xc3, 0xce, 0xc3, 0x34, 0xb4, 0x9b, 0xdb, 0x37, 0x0c, 0x43, + 0x7f, 0xec, 0x78, 0xde +}; + +/* Expected ciphertext value */ +static const unsigned char gcm_ct[] = { + 0xf7, 0x26, 0x44, 0x13, 0xa8, 0x4c, 0x0e, 0x7c, 0xd5, 0x36, 0x86, 0x7e, + 0xb9, 0xf2, 0x17, 0x36 +}; + +/* Expected AEAD Tag value */ +static const unsigned char gcm_tag[] = { + 0x67, 0xba, 0x05, 0x10, 0x26, 0x2a, 0xe4, 0x87, 0xd7, 0x37, 0xee, 0x62, + 0x98, 0xf7, 0x7e, 0x0c +}; + +/* + * A library context and property query can be used to select & filter + * algorithm implementations. If they are NULL then the default library + * context and properties are used. + */ +OSSL_LIB_CTX *libctx = NULL; +const char *propq = NULL; + +int aes_gcm_encrypt(void) +{ + int ret = 0; + EVP_CIPHER_CTX *ctx; + EVP_CIPHER *cipher = NULL; + int outlen, tmplen; + size_t gcm_ivlen = sizeof(gcm_iv); + unsigned char outbuf[1024]; + unsigned char outtag[16]; + OSSL_PARAM params[2] = { + OSSL_PARAM_END, OSSL_PARAM_END + }; + + printf("AES GCM Encrypt:\n"); + printf("Plaintext:\n"); + BIO_dump_fp(stdout, gcm_pt, sizeof(gcm_pt)); + + /* Create a context for the encrypt operation */ + if ((ctx = EVP_CIPHER_CTX_new()) == NULL) + goto err; + + /* Fetch the cipher implementation */ + if ((cipher = EVP_CIPHER_fetch(libctx, "AES-256-GCM", propq)) == NULL) + goto err; + + /* Set IV length if default 96 bits is not appropriate */ + params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, + &gcm_ivlen); + + /* + * Initialise an encrypt operation with the cipher/mode, key, IV and + * IV length parameter. + * For demonstration purposes the IV is being set here. In a compliant + * application the IV would be generated internally so the iv passed in + * would be NULL. + */ + if (!EVP_EncryptInit_ex2(ctx, cipher, gcm_key, gcm_iv, params)) + goto err; + + /* Zero or more calls to specify any AAD */ + if (!EVP_EncryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad))) + goto err; + + /* Encrypt plaintext */ + if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, gcm_pt, sizeof(gcm_pt))) + goto err; + + /* Output encrypted block */ + printf("Ciphertext:\n"); + BIO_dump_fp(stdout, outbuf, outlen); + + /* Finalise: note get no output for GCM */ + if (!EVP_EncryptFinal_ex(ctx, outbuf, &tmplen)) + goto err; + + /* Get tag */ + params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, + outtag, 16); + + if (!EVP_CIPHER_CTX_get_params(ctx, params)) + goto err; + + /* Output tag */ + printf("Tag:\n"); + BIO_dump_fp(stdout, outtag, 16); + + ret = 1; +err: + if (!ret) + ERR_print_errors_fp(stderr); + + EVP_CIPHER_free(cipher); + EVP_CIPHER_CTX_free(ctx); + + return ret; +} + +int aes_gcm_decrypt(void) +{ + int ret = 0; + EVP_CIPHER_CTX *ctx; + EVP_CIPHER *cipher = NULL; + int outlen, rv; + size_t gcm_ivlen = sizeof(gcm_iv); + unsigned char outbuf[1024]; + OSSL_PARAM params[2] = { + OSSL_PARAM_END, OSSL_PARAM_END + }; + + printf("AES GCM Decrypt:\n"); + printf("Ciphertext:\n"); + BIO_dump_fp(stdout, gcm_ct, sizeof(gcm_ct)); + + if ((ctx = EVP_CIPHER_CTX_new()) == NULL) + goto err; + + /* Fetch the cipher implementation */ + if ((cipher = EVP_CIPHER_fetch(libctx, "AES-256-GCM", propq)) == NULL) + goto err; + + /* Set IV length if default 96 bits is not appropriate */ + params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, + &gcm_ivlen); + + /* + * Initialise an encrypt operation with the cipher/mode, key, IV and + * IV length parameter. + */ + if (!EVP_DecryptInit_ex2(ctx, cipher, gcm_key, gcm_iv, params)) + goto err; + + /* Zero or more calls to specify any AAD */ + if (!EVP_DecryptUpdate(ctx, NULL, &outlen, gcm_aad, sizeof(gcm_aad))) + goto err; + + /* Decrypt plaintext */ + if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, gcm_ct, sizeof(gcm_ct))) + goto err; + + /* Output decrypted block */ + printf("Plaintext:\n"); + BIO_dump_fp(stdout, outbuf, outlen); + + /* Set expected tag value. */ + params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, + (void*)gcm_tag, sizeof(gcm_tag)); + + if (!EVP_CIPHER_CTX_set_params(ctx, params)) + goto err; + + /* Finalise: note get no output for GCM */ + rv = EVP_DecryptFinal_ex(ctx, outbuf, &outlen); + /* + * Print out return value. If this is not successful authentication + * failed and plaintext is not trustworthy. + */ + printf("Tag Verify %s\n", rv > 0 ? "Successful!" : "Failed!"); + + ret = 1; +err: + if (!ret) + ERR_print_errors_fp(stderr); + + EVP_CIPHER_free(cipher); + EVP_CIPHER_CTX_free(ctx); + + return ret; +} + +int main(int argc, char **argv) +{ + if (!aes_gcm_encrypt()) + return 1; + + if (!aes_gcm_decrypt()) + return 1; + + return 0; +} diff --git a/demos/cipher/aeskeywrap.c b/demos/cipher/aeskeywrap.c new file mode 100644 index 000000000000..f987772e4ffe --- /dev/null +++ b/demos/cipher/aeskeywrap.c @@ -0,0 +1,181 @@ +/* + * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * Simple aes wrap encryption demonstration program. + */ + +#include <stdio.h> +#include <openssl/err.h> +#include <openssl/bio.h> +#include <openssl/evp.h> +#include <openssl/crypto.h> +#include <openssl/core_names.h> + +/* aes key */ +static const unsigned char wrap_key[] = { + 0xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66, + 0x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69, + 0xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f +}; + +/* Unique initialisation vector */ +static const unsigned char wrap_iv[] = { + 0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84, + 0x99, 0xaa, 0x3e, 0x68, +}; + +/* Example plaintext to encrypt */ +static const unsigned char wrap_pt[] = { + 0xad, 0x4f, 0xc9, 0xfc, 0x77, 0x69, 0xc9, 0xea, 0xfc, 0xdf, 0x00, 0xac, + 0x34, 0xec, 0x40, 0xbc, 0x28, 0x3f, 0xa4, 0x5e, 0xd8, 0x99, 0xe4, 0x5d, + 0x5e, 0x7a, 0xc4, 0xe6, 0xca, 0x7b, 0xa5, 0xb7, +}; + +/* Expected ciphertext value */ +static const unsigned char wrap_ct[] = { + 0x97, 0x99, 0x55, 0xca, 0xf6, 0x3e, 0x95, 0x54, 0x39, 0xd6, 0xaf, 0x63, 0xff, 0x2c, 0xe3, 0x96, + 0xf7, 0x0d, 0x2c, 0x9c, 0xc7, 0x43, 0xc0, 0xb6, 0x31, 0x43, 0xb9, 0x20, 0xac, 0x6b, 0xd3, 0x67, + 0xad, 0x01, 0xaf, 0xa7, 0x32, 0x74, 0x26, 0x92, +}; + +/* + * A library context and property query can be used to select & filter + * algorithm implementations. If they are NULL then the default library + * context and properties are used. + */ +OSSL_LIB_CTX *libctx = NULL; +const char *propq = NULL; + +int aes_wrap_encrypt(void) +{ + int ret = 0; + EVP_CIPHER_CTX *ctx; + EVP_CIPHER *cipher = NULL; + int outlen, tmplen; + unsigned char outbuf[1024]; + + printf("aes wrap Encrypt:\n"); + printf("Plaintext:\n"); + BIO_dump_fp(stdout, wrap_pt, sizeof(wrap_pt)); + + /* Create a context for the encrypt operation */ + if ((ctx = EVP_CIPHER_CTX_new()) == NULL) + goto err; + + EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); + + /* Fetch the cipher implementation */ + if ((cipher = EVP_CIPHER_fetch(libctx, "AES-256-WRAP", propq)) == NULL) + goto err; + + /* + * Initialise an encrypt operation with the cipher/mode, key and IV. + * We are not setting any custom params so let params be just NULL. + */ + if (!EVP_EncryptInit_ex2(ctx, cipher, wrap_key, wrap_iv, /* params */ NULL)) + goto err; + + /* Encrypt plaintext */ + if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, wrap_pt, sizeof(wrap_pt))) + goto err; + + /* Finalise: there can be some additional output from padding */ + if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) + goto err; + outlen += tmplen; + + /* Output encrypted block */ + printf("Ciphertext (outlen:%d):\n", outlen); + BIO_dump_fp(stdout, outbuf, outlen); + + if (sizeof(wrap_ct) == outlen && !CRYPTO_memcmp(outbuf, wrap_ct, outlen)) + printf("Final ciphertext matches expected ciphertext\n"); + else + printf("Final ciphertext differs from expected ciphertext\n"); + + ret = 1; +err: + if (!ret) + ERR_print_errors_fp(stderr); + + EVP_CIPHER_free(cipher); + EVP_CIPHER_CTX_free(ctx); + + return ret; +} + +int aes_wrap_decrypt(void) +{ + int ret = 0; + EVP_CIPHER_CTX *ctx; + EVP_CIPHER *cipher = NULL; + int outlen, tmplen; + unsigned char outbuf[1024]; + + printf("aes wrap Decrypt:\n"); + printf("Ciphertext:\n"); + BIO_dump_fp(stdout, wrap_ct, sizeof(wrap_ct)); + + if ((ctx = EVP_CIPHER_CTX_new()) == NULL) + goto err; + + EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); + + /* Fetch the cipher implementation */ + if ((cipher = EVP_CIPHER_fetch(libctx, "aes-256-wrap", propq)) == NULL) + goto err; + + /* + * Initialise an encrypt operation with the cipher/mode, key and IV. + * We are not setting any custom params so let params be just NULL. + */ + if (!EVP_DecryptInit_ex2(ctx, cipher, wrap_key, wrap_iv, /* params */ NULL)) + goto err; + + /* Decrypt plaintext */ + if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, wrap_ct, sizeof(wrap_ct))) + goto err; + + /* Finalise: there can be some additional output from padding */ + if (!EVP_DecryptFinal_ex(ctx, outbuf + outlen, &tmplen)) + goto err; + outlen += tmplen; + + /* Output decrypted block */ + printf("Plaintext (outlen:%d):\n", outlen); + BIO_dump_fp(stdout, outbuf, outlen); + + if (sizeof(wrap_pt) == outlen && !CRYPTO_memcmp(outbuf, wrap_pt, outlen)) + printf("Final plaintext matches original plaintext\n"); + else + printf("Final plaintext differs from original plaintext\n"); + + ret = 1; +err: + if (!ret) + ERR_print_errors_fp(stderr); + + EVP_CIPHER_free(cipher); + EVP_CIPHER_CTX_free(ctx); + + return ret; +} + +int main(int argc, char **argv) +{ + if (!aes_wrap_encrypt()) + return 1; + + if (!aes_wrap_decrypt()) + return 1; + + return 0; +} + diff --git a/demos/cipher/ariacbc.c b/demos/cipher/ariacbc.c new file mode 100644 index 000000000000..8999fe6e701d --- /dev/null +++ b/demos/cipher/ariacbc.c @@ -0,0 +1,178 @@ +/* + * Copyright 2012-2022 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * Simple ARIA CBC encryption demonstration program. + */ + +#include <stdio.h> +#include <openssl/err.h> +#include <openssl/bio.h> +#include <openssl/evp.h> +#include <openssl/crypto.h> +#include <openssl/core_names.h> + +/* ARIA key */ +static const unsigned char cbc_key[] = { + 0xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92, 0x1c, 0x04, 0x65, 0x66, + 0x5f, 0x8a, 0xe6, 0xd1, 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69, + 0xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f +}; + +/* Unique initialisation vector */ +static const unsigned char cbc_iv[] = { + 0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0, 0xee, 0xd0, 0x66, 0x84, + 0x99, 0xaa, 0x3e, 0x68, +}; + +/* Example plaintext to encrypt */ +static const unsigned char cbc_pt[] = { + 0xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e, 0xeb, 0x31, 0xb2, 0xea, + 0xcc, 0x2b, 0xf2, 0xa5 +}; + +/* Expected ciphertext value */ +static const unsigned char cbc_ct[] = { + 0x9a, 0x44, 0xe6, 0x85, 0x94, 0x26, 0xff, 0x30, 0x03, 0xd3, 0x7e, 0xc6, + 0xb5, 0x4a, 0x09, 0x66, 0x39, 0x28, 0xf3, 0x67, 0x14, 0xbc, 0xe8, 0xe2, + 0xcf, 0x31, 0xb8, 0x60, 0x42, 0x72, 0x6d, 0xc8 +}; + +/* + * A library context and property query can be used to select & filter + * algorithm implementations. If they are NULL then the default library + * context and properties are used. + */ +OSSL_LIB_CTX *libctx = NULL; +const char *propq = NULL; + +int aria_cbc_encrypt(void) +{ + int ret = 0; + EVP_CIPHER_CTX *ctx; + EVP_CIPHER *cipher = NULL; + int outlen, tmplen; + size_t cbc_ivlen = sizeof(cbc_iv); + unsigned char outbuf[1024]; + unsigned char outtag[16]; + + printf("ARIA CBC Encrypt:\n"); + printf("Plaintext:\n"); + BIO_dump_fp(stdout, cbc_pt, sizeof(cbc_pt)); + + /* Create a context for the encrypt operation */ + if ((ctx = EVP_CIPHER_CTX_new()) == NULL) + goto err; + + /* Fetch the cipher implementation */ + if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", propq)) == NULL) + goto err; + + /* + * Initialise an encrypt operation with the cipher/mode, key and IV. + * We are not setting any custom params so let params be just NULL. + */ + if (!EVP_EncryptInit_ex2(ctx, cipher, cbc_key, cbc_iv, /* params */ NULL)) + goto err; + + /* Encrypt plaintext */ + if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, cbc_pt, sizeof(cbc_pt))) + goto err; + + /* Finalise: there can be some additional output from padding */ + if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) + goto err; + outlen += tmplen; + + /* Output encrypted block */ + printf("Ciphertext (outlen:%d):\n", outlen); + BIO_dump_fp(stdout, outbuf, outlen); + + if (sizeof(cbc_ct) == outlen && !CRYPTO_memcmp(outbuf, cbc_ct, outlen)) + printf("Final ciphertext matches expected ciphertext\n"); + else + printf("Final ciphertext differs from expected ciphertext\n"); + + ret = 1; +err: + if (!ret) + ERR_print_errors_fp(stderr); + + EVP_CIPHER_free(cipher); + EVP_CIPHER_CTX_free(ctx); + + return ret; +} + +int aria_cbc_decrypt(void) +{ + int ret = 0; + EVP_CIPHER_CTX *ctx; + EVP_CIPHER *cipher = NULL; + int outlen, tmplen, rv; + size_t cbc_ivlen = sizeof(cbc_iv); + unsigned char outbuf[1024]; + + printf("ARIA CBC Decrypt:\n"); + printf("Ciphertext:\n"); + BIO_dump_fp(stdout, cbc_ct, sizeof(cbc_ct)); + + if ((ctx = EVP_CIPHER_CTX_new()) == NULL) + goto err; + + /* Fetch the cipher implementation */ + if ((cipher = EVP_CIPHER_fetch(libctx, "ARIA-256-CBC", propq)) == NULL) + goto err; + + /* + * Initialise an encrypt operation with the cipher/mode, key and IV. + * We are not setting any custom params so let params be just NULL. + */ + if (!EVP_DecryptInit_ex2(ctx, cipher, cbc_key, cbc_iv, /* params */ NULL)) + goto err; + + /* Decrypt plaintext */ + if (!EVP_DecryptUpdate(ctx, outbuf, &outlen, cbc_ct, sizeof(cbc_ct))) + goto err; + + /* Finalise: there can be some additional output from padding */ + if (!EVP_DecryptFinal_ex(ctx, outbuf + outlen, &tmplen)) + goto err; + outlen += tmplen; + + /* Output decrypted block */ + printf("Plaintext (outlen:%d):\n", outlen); + BIO_dump_fp(stdout, outbuf, outlen); + + if (sizeof(cbc_pt) == outlen && !CRYPTO_memcmp(outbuf, cbc_pt, outlen)) + printf("Final plaintext matches original plaintext\n"); + else + printf("Final plaintext differs from original plaintext\n"); + + ret = 1; +err: + if (!ret) + ERR_print_errors_fp(stderr); + + EVP_CIPHER_free(cipher); + EVP_CIPHER_CTX_free(ctx); + + return ret; +} + +int main(int argc, char **argv) +{ + if (!aria_cbc_encrypt()) + return 1; + + if (!aria_cbc_decrypt()) + return 1; + + return 0; +} |