test-crypto.c revision fadd4c92940c10a01556e1ebcb2f17890b35d7bc
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen#include "lib.h"
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen#include "buffer.h"
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen#include "str.h"
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen#include "dcrypt.h"
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen#include "ostream.h"
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen#include "ostream-encrypt.h"
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen#include "istream.h"
11ead0d848f1751c8274e3422fa48fc7d9aee2e9Timo Sirainen#include "istream-decrypt.h"
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen#include "iostream-temp.h"
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen#include "randgen.h"
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen#include "test-common.h"
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen#include "hex-binary.h"
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen#include <fcntl.h>
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen#include <sys/stat.h>
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen#include <stdio.h>
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainenstatic
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainenvoid test_cipher_test_vectors(void)
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen{
5e4c116a411967f1a012ac4f79732a724b5f6cc1Timo Sirainen static struct {
5e4c116a411967f1a012ac4f79732a724b5f6cc1Timo Sirainen const char *key;
5e4c116a411967f1a012ac4f79732a724b5f6cc1Timo Sirainen const char *iv;
5e4c116a411967f1a012ac4f79732a724b5f6cc1Timo Sirainen const char *pt;
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen const char *ct;
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen } vectors[] =
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen {
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen { "2b7e151628aed2a6abf7158809cf4f3c", "000102030405060708090a0b0c0d0e0f", "6bc1bee22e409f96e93d7e117393172a", "7649abac8119b246cee98e9b12e9197d" },
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen { "2b7e151628aed2a6abf7158809cf4f3c", "7649ABAC8119B246CEE98E9B12E9197D", "ae2d8a571e03ac9c9eb76fac45af8e51", "5086cb9b507219ee95db113a917678b2" }
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen };
e101ed4afa1cfe5bb478491cbb5b8e7ff3de36ceTimo Sirainen
e101ed4afa1cfe5bb478491cbb5b8e7ff3de36ceTimo Sirainen
e101ed4afa1cfe5bb478491cbb5b8e7ff3de36ceTimo Sirainen test_begin("test_cipher_test_vectors");
e101ed4afa1cfe5bb478491cbb5b8e7ff3de36ceTimo Sirainen
4c74f086031dc6ce5c65cc0f949753a03a00c686Timo Sirainen buffer_t *key,*iv,*pt,*ct,*res_enc,*res_dec;
4c74f086031dc6ce5c65cc0f949753a03a00c686Timo Sirainen
2e0e52f27469bf4eba8d7e88f1716f0a19824ba5Timo Sirainen key = buffer_create_dynamic(pool_datastack_create(), 16);
4c74f086031dc6ce5c65cc0f949753a03a00c686Timo Sirainen iv = buffer_create_dynamic(pool_datastack_create(), 16);
2e0e52f27469bf4eba8d7e88f1716f0a19824ba5Timo Sirainen pt = buffer_create_dynamic(pool_datastack_create(), 16);
2e0e52f27469bf4eba8d7e88f1716f0a19824ba5Timo Sirainen ct = buffer_create_dynamic(pool_datastack_create(), 16);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen res_enc = buffer_create_dynamic(pool_datastack_create(), 32);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen res_dec = buffer_create_dynamic(pool_datastack_create(), 32);
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen for(size_t i = 0; i < N_ELEMENTS(vectors); i++) {
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen struct dcrypt_context_symmetric *ctx;
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen buffer_set_used_size(key, 0);
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen buffer_set_used_size(iv, 0);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen buffer_set_used_size(pt, 0);
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen buffer_set_used_size(ct, 0);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen buffer_set_used_size(res_enc, 0);
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen buffer_set_used_size(res_dec, 0);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen hex_to_binary(vectors[i].key, key);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen hex_to_binary(vectors[i].iv, iv);
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen hex_to_binary(vectors[i].pt, pt);
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen hex_to_binary(vectors[i].ct, ct);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen if (!dcrypt_ctx_sym_create("AES-128-CBC", DCRYPT_MODE_ENCRYPT, &ctx, NULL)) {
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen test_assert_failed("dcrypt_ctx_sym_create", __FILE__, __LINE__-1);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen continue;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen }
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen dcrypt_ctx_sym_set_padding(ctx, FALSE);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen dcrypt_ctx_sym_set_key(ctx, key->data, key->used);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen dcrypt_ctx_sym_set_iv(ctx, iv->data, iv->used);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen test_assert_idx(dcrypt_ctx_sym_init(ctx, NULL), i);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen test_assert_idx(dcrypt_ctx_sym_update(ctx, pt->data, pt->used, res_enc, NULL), i);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen test_assert_idx(dcrypt_ctx_sym_final(ctx, res_enc, NULL), i);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen test_assert_idx(buffer_cmp(ct, res_enc), i);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen dcrypt_ctx_sym_destroy(&ctx);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen if (!dcrypt_ctx_sym_create("AES-128-CBC", DCRYPT_MODE_DECRYPT, &ctx, NULL)) {
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen test_assert_failed("dcrypt_ctx_sym_create", __FILE__, __LINE__-1);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen continue;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen }
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen dcrypt_ctx_sym_set_padding(ctx, FALSE);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen dcrypt_ctx_sym_set_key(ctx, key->data, key->used);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen dcrypt_ctx_sym_set_iv(ctx, iv->data, iv->used);
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen test_assert_idx(dcrypt_ctx_sym_init(ctx, NULL), i);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen test_assert_idx(dcrypt_ctx_sym_update(ctx, res_enc->data, res_enc->used, res_dec, NULL), i);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen test_assert_idx(dcrypt_ctx_sym_final(ctx, res_dec, NULL), i);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen test_assert_idx(buffer_cmp(pt, res_dec), i);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen dcrypt_ctx_sym_destroy(&ctx);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen }
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen test_end();
11ead0d848f1751c8274e3422fa48fc7d9aee2e9Timo Sirainen}
11ead0d848f1751c8274e3422fa48fc7d9aee2e9Timo Sirainen
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainenstatic
11ead0d848f1751c8274e3422fa48fc7d9aee2e9Timo Sirainenvoid test_cipher_aead_test_vectors(void)
11ead0d848f1751c8274e3422fa48fc7d9aee2e9Timo Sirainen{
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen struct dcrypt_context_symmetric *ctx;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen const char *error = NULL;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
11ead0d848f1751c8274e3422fa48fc7d9aee2e9Timo Sirainen test_begin("test_cipher_aead_test_vectors");
11ead0d848f1751c8274e3422fa48fc7d9aee2e9Timo Sirainen
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen if (!dcrypt_ctx_sym_create("aes-128-gcm", DCRYPT_MODE_ENCRYPT, &ctx, &error)) {
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen test_assert_failed("dcrypt_ctx_sym_create", __FILE__, __LINE__-1);
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen return;
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen buffer_t *key, *iv, *aad, *pt, *ct, *tag, *tag_res, *res;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen key = buffer_create_dynamic(pool_datastack_create(), 16);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen iv = buffer_create_dynamic(pool_datastack_create(), 16);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen aad = buffer_create_dynamic(pool_datastack_create(), 16);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen pt = buffer_create_dynamic(pool_datastack_create(), 16);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen ct = buffer_create_dynamic(pool_datastack_create(), 16);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen tag = buffer_create_dynamic(pool_datastack_create(), 16);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen res = buffer_create_dynamic(pool_datastack_create(), 16);
3fe44a0df5a0bdd80c495f79cbf0e384441d6fccTimo Sirainen tag_res = buffer_create_dynamic(pool_datastack_create(), 16);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen hex_to_binary("feffe9928665731c6d6a8f9467308308", key);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen hex_to_binary("cafebabefacedbaddecaf888", iv);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen hex_to_binary("d9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a721c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255", pt);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen hex_to_binary("42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f5985", ct);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen hex_to_binary("4d5c2af327cd64a62cf35abd2ba6fab4", tag);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen dcrypt_ctx_sym_set_key(ctx, key->data, key->used);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen dcrypt_ctx_sym_set_iv(ctx, iv->data, iv->used);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen dcrypt_ctx_sym_set_aad(ctx, aad->data, aad->used);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen test_assert(dcrypt_ctx_sym_init(ctx, &error));
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen test_assert(dcrypt_ctx_sym_update(ctx, pt->data, pt->used, res, &error));
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen test_assert(dcrypt_ctx_sym_final(ctx, res, &error));
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen test_assert(dcrypt_ctx_sym_get_tag(ctx, tag_res));
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen test_assert(buffer_cmp(ct, res) == TRUE);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen test_assert(buffer_cmp(tag, tag_res) == TRUE);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen dcrypt_ctx_sym_destroy(&ctx);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen if (!dcrypt_ctx_sym_create("aes-128-gcm", DCRYPT_MODE_DECRYPT, &ctx, &error)) {
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen test_assert_failed("dcrypt_ctx_sym_create", __FILE__, __LINE__-1);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen } else {
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen buffer_set_used_size(res, 0);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen dcrypt_ctx_sym_set_key(ctx, key->data, key->used);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen dcrypt_ctx_sym_set_iv(ctx, iv->data, iv->used);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen dcrypt_ctx_sym_set_aad(ctx, aad->data, aad->used);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen dcrypt_ctx_sym_set_tag(ctx, tag->data, tag->used);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen test_assert(dcrypt_ctx_sym_init(ctx, &error));
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen test_assert(dcrypt_ctx_sym_update(ctx, ct->data, ct->used, res, &error));
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen test_assert(dcrypt_ctx_sym_final(ctx, res, &error));
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen test_assert(buffer_cmp(pt, res) == TRUE);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen dcrypt_ctx_sym_destroy(&ctx);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen }
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen test_end();
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen}
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenstatic
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainenvoid test_hmac_test_vectors(void)
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen{
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen test_begin("test_hmac_test_vectors");
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen buffer_t *pt, *ct, *key, *res;
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen pt = buffer_create_dynamic(pool_datastack_create(), 50);
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen key = buffer_create_dynamic(pool_datastack_create(), 20);
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen ct = buffer_create_dynamic(pool_datastack_create(), 32);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen res = buffer_create_dynamic(pool_datastack_create(), 32);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen hex_to_binary("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", key);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen hex_to_binary("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", pt);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen hex_to_binary("773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe", res);
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen struct dcrypt_context_hmac *hctx;
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen if (!dcrypt_ctx_hmac_create("sha256", &hctx, NULL)) {
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen test_assert_failed("dcrypt_ctx_hmac_create", __FILE__, __LINE__-1);
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen } else {
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen dcrypt_ctx_hmac_set_key(hctx, key->data, key->used);
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen test_assert(dcrypt_ctx_hmac_init(hctx, NULL));
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen test_assert(dcrypt_ctx_hmac_update(hctx, pt->data, pt->used, NULL));
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen test_assert(dcrypt_ctx_hmac_final(hctx, ct, NULL));
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen test_assert(buffer_cmp(ct, res));
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen dcrypt_ctx_hmac_destroy(&hctx);
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen }
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen test_end();
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen}
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainenstatic
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainenvoid test_load_v1_keys(void)
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen{
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen test_begin("test_load_v1_keys");
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen const char *error = NULL;
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen const char *data1 = "1\t716\t1\t0567e6bf9579813ae967314423b0fceb14bda24749303923de9a9bb9370e0026f995901a57e63113eeb2baf0c940e978d00686cbb52bd5014bc318563375876255\t0300E46DA2125427BE968EB3B649910CDC4C405E5FFDE18D433A97CABFEE28CEEFAE9EE356C792004FFB80981D67E741B8CC036A34235A8D2E1F98D1658CFC963D07EB\td0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0\t7c9a1039ea2e4fed73e81dd3ffc3fa22ea4a28352939adde7bf8ea858b00fa4f";
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen enum dcrypt_key_format format;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen enum dcrypt_key_version version;
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen enum dcrypt_key_kind kind;
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen enum dcrypt_key_encryption_type encryption_type;
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen const char *encryption_key_hash = NULL;
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen const char *key_hash = NULL;
5e4c116a411967f1a012ac4f79732a724b5f6cc1Timo Sirainen
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen bool ret = dcrypt_key_string_get_info(data1, &format, &version,
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen &kind, &encryption_type, &encryption_key_hash,
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen &key_hash, &error);
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen test_assert(ret == TRUE);
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen test_assert(error == NULL);
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen test_assert(format == DCRYPT_FORMAT_DOVECOT);
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen test_assert(version == DCRYPT_KEY_VERSION_1);
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen test_assert(kind == DCRYPT_KEY_KIND_PRIVATE);
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen test_assert(encryption_type == DCRYPT_KEY_ENCRYPTION_TYPE_KEY);
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen test_assert(strcmp(encryption_key_hash, "d0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0") == 0);
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen test_assert(strcmp(key_hash, "7c9a1039ea2e4fed73e81dd3ffc3fa22ea4a28352939adde7bf8ea858b00fa4f") == 0);
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen const char* data2 = "1\t716\t0301EB00973C4EFC8FCECA4EA33E941F50B561199A5159BCB6C2EED9DD1D62D65E38A254979D89E28F0C28883E71EE2AD264CD16B863FA094A8F6F69A56B62E8918040\t7c9a1039ea2e4fed73e81dd3ffc3fa22ea4a28352939adde7bf8ea858b00fa4f";
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen error = NULL;
92654af7b19ff1bfc0df427e5ffcb19750fbf79aTimo Sirainen encryption_key_hash = NULL;
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen key_hash = NULL;
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen ret = dcrypt_key_string_get_info(data2, &format, &version,
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen &kind, &encryption_type, &encryption_key_hash,
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen &key_hash, &error);
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen test_assert(ret == TRUE);
2e0e52f27469bf4eba8d7e88f1716f0a19824ba5Timo Sirainen test_assert(error == NULL);
2e0e52f27469bf4eba8d7e88f1716f0a19824ba5Timo Sirainen test_assert(format == DCRYPT_FORMAT_DOVECOT);
2e0e52f27469bf4eba8d7e88f1716f0a19824ba5Timo Sirainen test_assert(version == DCRYPT_KEY_VERSION_1);
2e0e52f27469bf4eba8d7e88f1716f0a19824ba5Timo Sirainen test_assert(kind == DCRYPT_KEY_KIND_PUBLIC);
2e0e52f27469bf4eba8d7e88f1716f0a19824ba5Timo Sirainen test_assert(encryption_type == DCRYPT_KEY_ENCRYPTION_TYPE_NONE);
2e0e52f27469bf4eba8d7e88f1716f0a19824ba5Timo Sirainen test_assert(encryption_key_hash == NULL);
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen test_assert(strcmp(key_hash, "7c9a1039ea2e4fed73e81dd3ffc3fa22ea4a28352939adde7bf8ea858b00fa4f") == 0);
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen /* This is the key that should be able to decrypt key1 */
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen const char *data3 = "1\t716\t0\t048FD04FD3612B22D32790C592CF21CEF417EFD2EA34AE5F688FA5B51BED29E05A308B68DA78E16E90B47A11E133BD9A208A2894FD01B0BEE865CE339EA3FB17AC\td0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0";
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen error = NULL;
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen encryption_key_hash = NULL;
a2871d937a75288bcd7177b2680d4e5096a7f3cbTimo Sirainen key_hash = NULL;
87c7e8404e2534e90cf212043e3c2d9245884003Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen ret = dcrypt_key_string_get_info(data3, &format, &version,
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen &kind, &encryption_type, &encryption_key_hash,
87c7e8404e2534e90cf212043e3c2d9245884003Timo Sirainen &key_hash, &error);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen test_assert(ret == TRUE);
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen test_assert(error == NULL);
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen test_assert(format == DCRYPT_FORMAT_DOVECOT);
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen test_assert(version == DCRYPT_KEY_VERSION_1);
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen test_assert(kind == DCRYPT_KEY_KIND_PRIVATE);
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen test_assert(encryption_type == DCRYPT_KEY_ENCRYPTION_TYPE_NONE);
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen test_assert(encryption_key_hash == NULL);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen test_assert(strcmp(key_hash, "d0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0") == 0);
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen /* key3's key_hash should and does match key1's encryption_key_hash */
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen struct dcrypt_private_key *pkey = NULL;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen struct dcrypt_private_key *pkey2 = NULL;
94a54cd65ff887eefefb213a2df5577dc658f186Timo Sirainen pkey = NULL;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen error = NULL;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen ret = dcrypt_key_load_private(&pkey2, data3, NULL, NULL, &error);
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen test_assert(ret == TRUE);
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen test_assert(error == NULL);
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen
979e2379225b05df63ca3a0c55064edbc03d7afbTimo Sirainen ret = dcrypt_key_load_private(&pkey, data1, NULL, pkey2, &error);
ac756824dc2be28eda7e9f70f1a694469f88d8bfTimo Sirainen test_assert(ret == TRUE);
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen test_assert(error == NULL);
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen dcrypt_key_unref_private(&pkey2);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen dcrypt_key_unref_private(&pkey);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen test_end();
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen}
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainenstatic
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainenvoid test_load_v1_key(void)
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen{
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen test_begin("test_load_v1_key");
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen buffer_t *key_1 = buffer_create_dynamic(pool_datastack_create(), 128);
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct dcrypt_private_key *pkey = NULL, *pkey2 = NULL;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen const char *error = NULL;
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen test_assert(dcrypt_key_load_private(&pkey, "1\t716\t0\t048FD04FD3612B22D32790C592CF21CEF417EFD2EA34AE5F688FA5B51BED29E05A308B68DA78E16E90B47A11E133BD9A208A2894FD01B0BEE865CE339EA3FB17AC\td0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0", NULL, NULL, &error));
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen if (pkey != NULL) {
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen buffer_set_used_size(key_1, 0);
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen /* check that key_id matches */
3ccfcf0856958cb9208a9fc51c3bdf13c58ad52aTimo Sirainen struct dcrypt_public_key *pubkey = NULL;
82995cc154a929f37aa486a72a6485e9f8d34a30Timo Sirainen dcrypt_key_convert_private_to_public(pkey, &pubkey);
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen test_assert(dcrypt_key_store_public(pubkey, DCRYPT_FORMAT_DOVECOT, key_1, NULL));
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen buffer_set_used_size(key_1, 0);
a35cbba04d0a2823da98e693bd09a051addffdb2Timo Sirainen dcrypt_key_id_public(pubkey, "sha256", key_1, &error);
test_assert(strcmp("792caad4d38c9eb2134a0cbc844eae386116de096a0ccafc98479825fc99b6a1", binary_to_hex(key_1->data, key_1->used)) == 0);
dcrypt_key_unref_public(&pubkey);
pkey2 = NULL;
test_assert(dcrypt_key_load_private(&pkey2, "1\t716\t1\t0567e6bf9579813ae967314423b0fceb14bda24749303923de9a9bb9370e0026f995901a57e63113eeb2baf0c940e978d00686cbb52bd5014bc318563375876255\t0300E46DA2125427BE968EB3B649910CDC4C405E5FFDE18D433A97CABFEE28CEEFAE9EE356C792004FFB80981D67E741B8CC036A34235A8D2E1F98D1658CFC963D07EB\td0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0\t7c9a1039ea2e4fed73e81dd3ffc3fa22ea4a28352939adde7bf8ea858b00fa4f", NULL, pkey, &error));
if (pkey2 != NULL) {
buffer_set_used_size(key_1, 0);
/* check that key_id matches */
struct dcrypt_public_key *pubkey = NULL;
dcrypt_key_convert_private_to_public(pkey2, &pubkey);
test_assert(dcrypt_key_store_public(pubkey, DCRYPT_FORMAT_DOVECOT, key_1, NULL));
buffer_set_used_size(key_1, 0);
test_assert(dcrypt_key_id_public_old(pubkey, key_1, &error));
test_assert(strcmp("7c9a1039ea2e4fed73e81dd3ffc3fa22ea4a28352939adde7bf8ea858b00fa4f", binary_to_hex(key_1->data, key_1->used)) == 0);
dcrypt_key_unref_public(&pubkey);
dcrypt_key_unref_private(&pkey2);
}
dcrypt_key_unref_private(&pkey);
}
test_end();
}
static
void test_load_v1_public_key(void)
{
test_begin("test_load_v1_public_key");
const char* data1 = "1\t716\t030131D8A5FD5167947A0AE9CB112ADED6526654635AA5887051EE2364414B60FF32EBA8FA0BBE9485DBDE8794BBBCB44BBFC0D662A4287A848BA570D4E5E45A11FE0F\td0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0";
const char* error = NULL;
const char* key_hash = NULL;
const char* encryption_key_hash = NULL;
enum dcrypt_key_format format;
enum dcrypt_key_version version;
enum dcrypt_key_kind kind;
enum dcrypt_key_encryption_type encryption_type;
bool ret = dcrypt_key_string_get_info(data1, &format, &version,
&kind, &encryption_type, &encryption_key_hash,
&key_hash, &error);
test_assert(ret == TRUE);
test_assert(error == NULL);
test_assert(format == DCRYPT_FORMAT_DOVECOT);
test_assert(version == DCRYPT_KEY_VERSION_1);
test_assert(kind == DCRYPT_KEY_KIND_PUBLIC);
test_assert(encryption_type == DCRYPT_KEY_ENCRYPTION_TYPE_NONE);
test_assert(key_hash != NULL && strcmp(key_hash, "d0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0") == 0);
test_assert(encryption_key_hash == NULL);
struct dcrypt_public_key *pub_key = NULL;
ret = dcrypt_key_load_public(&pub_key, data1, &error);
test_assert(ret == TRUE);
test_assert(error == NULL);
test_assert(dcrypt_key_type_public(pub_key) == DCRYPT_KEY_EC);
dcrypt_key_unref_public(&pub_key);
test_assert(pub_key == NULL);
test_end();
}
static
void test_load_v2_key(void)
{
const char *keys[] = {
"-----BEGIN PRIVATE KEY-----\n" \
"MGcCAQAwEwYHKoZIzj0CAQYIKoZIzj0DAQcETTBLAgEBBCC25AkD65uhlZXCAdwN\n" \
"yLJV2ui8A/CUyqyEMrezvwgMO6EkAyIAAybRUR3MsH0+0PQcDwkrXOJ9aePwzTQV\n" \
"DN51+n1JCxbI\n" \
"-----END PRIVATE KEY-----\n",
"2:1.2.840.10045.3.1.7:0:0000002100b6e40903eb9ba19595c201dc0dc8b255dae8bc03f094caac8432b7b3bf080c3b:ab13d251976dedab546b67354e7678821740dd534b749c2857f66bf62bbaddfd",
"2:1.2.840.10045.3.1.7:2:aes-256-ctr:2b19763d4bbf7754:sha256:2048:c36fa194669a1aec400eae32fbadaa7c58b14f53c464cfbb0a4b61fbe24ab7750637c4025d:ab13d251976dedab546b67354e7678821740dd534b749c2857f66bf62bbaddfd",
"2:1.2.840.10045.3.1.7:1:aes-256-ctr:7c7f1d12a7c011de:sha256:2048:f5d1de11d58a81b141cf038012a618623e9d7b18062deeb3a4e35872c62ca0837db8688370:021abfbc5bc4f6cf49c40b9fc388c4616ea079941675f477ee4557df1919626d35:ab13d251976dedab546b67354e7678821740dd534b749c2857f66bf62bbaddfd:ab13d251976dedab546b67354e7678821740dd534b749c2857f66bf62bbaddfd"
};
test_begin("test_load_v2_key");
const char *error = NULL;
buffer_t *tmp = buffer_create_dynamic(default_pool, 256);
struct dcrypt_private_key *priv,*priv2;
test_assert_idx(dcrypt_key_load_private(&priv2, keys[0], NULL, NULL, &error), 0);
test_assert_idx(dcrypt_key_store_private(priv2, DCRYPT_FORMAT_PEM, NULL, tmp, NULL, NULL, &error), 0);
test_assert_idx(strcmp(str_c(tmp), keys[0])==0, 0);
buffer_set_used_size(tmp, 0);
test_assert_idx(dcrypt_key_load_private(&priv, keys[1], NULL, NULL, &error), 1);
test_assert_idx(dcrypt_key_store_private(priv, DCRYPT_FORMAT_DOVECOT, NULL, tmp, NULL, NULL, &error), 1);
test_assert_idx(strcmp(str_c(tmp), keys[1])==0, 1);
buffer_set_used_size(tmp, 0);
dcrypt_key_unref_private(&priv);
test_assert_idx(dcrypt_key_load_private(&priv, keys[2], "This Is Sparta", NULL, &error), 2);
test_assert_idx(dcrypt_key_store_private(priv, DCRYPT_FORMAT_DOVECOT, "aes-256-ctr", tmp, "This Is Sparta", NULL, &error), 2);
buffer_set_used_size(tmp, 0);
dcrypt_key_unref_private(&priv);
struct dcrypt_public_key *pub = NULL;
dcrypt_key_convert_private_to_public(priv2, &pub);
test_assert_idx(dcrypt_key_load_private(&priv, keys[3], NULL, priv2, &error), 3);
test_assert_idx(dcrypt_key_store_private(priv, DCRYPT_FORMAT_DOVECOT, "ecdh-aes-256-ctr", tmp, NULL, pub, &error), 3);
buffer_set_used_size(tmp, 0);
dcrypt_key_unref_private(&priv2);
dcrypt_key_unref_private(&priv);
dcrypt_key_unref_public(&pub);
buffer_free(&tmp);
if (error != NULL) error = NULL;
test_end();
}
static
void test_load_v2_public_key(void)
{
struct dcrypt_public_key *pub = NULL;
const char *error;
test_begin("test_load_v2_public_key");
const char *key = "2:3058301006072a8648ce3d020106052b810400230344000301c50954e734dd8b410a607764a7057065a45510da52f2c6e28e0cb353b9c389fa8cb786943ae991fce9befed78fb162fbbc615415f06af06c8cc80c37f4e94ff6c7:185a7212542782e239111f9c19d126ad55b18ddaf4883d66afe8d9627c3607d8";
test_assert(dcrypt_key_load_public(&pub, key, &error));
buffer_t *tmp = buffer_create_dynamic(default_pool, 256);
if (pub != NULL) {
test_assert(dcrypt_key_store_public(pub, DCRYPT_FORMAT_DOVECOT, tmp, &error));
test_assert(strcmp(key, str_c(tmp))==0);
buffer_free(&tmp);
dcrypt_key_unref_public(&pub);
}
test_end();
}
static
void test_get_info_v2_key(void) {
test_begin("test_get_info_v2_key");
const char *key = "2:305e301006072a8648ce3d020106052b81040026034a000203fcc90034fa03d6fb79a0fc8b3b43c3398f68e76029307360cdcb9e27bb7e84b3c19dfb7244763bc4d442d216f09b7b7945ed9d182f3156550e9ee30b237a0217dbf79d28975f31:86706b69d1f640011a65d26a42f2ba20a619173644e1cc7475eb1d90966e84dc";
enum dcrypt_key_format format;
enum dcrypt_key_version version = DCRYPT_KEY_VERSION_NA;
enum dcrypt_key_kind kind;
enum dcrypt_key_encryption_type encryption_type;
const char *encryption_key_hash = NULL;
const char *key_hash = NULL;
const char *error = NULL;
test_assert(dcrypt_key_string_get_info(key, &format, &version,
&kind, &encryption_type, &encryption_key_hash,
&key_hash, &error));
test_assert(error == NULL);
test_assert(format == DCRYPT_FORMAT_DOVECOT);
test_assert(version == DCRYPT_KEY_VERSION_2);
test_assert(kind == DCRYPT_KEY_KIND_PUBLIC);
test_assert(encryption_type == DCRYPT_KEY_ENCRYPTION_TYPE_NONE);
test_assert(encryption_key_hash == NULL);
test_assert(key_hash != NULL && strcmp(key_hash,
"86706b69d1f640011a65d26a42f2ba20a619173644e1cc7475eb1d90966e84dc") == 0);
test_end();
}
static
void test_gen_and_get_info_rsa_pem(void)
{
test_begin("test_gen_and_get_info_rsa_pem");
const char *error = NULL;
bool ret = FALSE;
struct dcrypt_keypair pair;
string_t* buf = str_new(default_pool, 4096);
ret = dcrypt_keypair_generate(&pair, DCRYPT_KEY_RSA, 1024, NULL, NULL);
test_assert(ret == TRUE);
/* test public key */
enum dcrypt_key_format format;
enum dcrypt_key_version version;
enum dcrypt_key_kind kind;
enum dcrypt_key_encryption_type encryption_type;
const char *encryption_key_hash;
const char *key_hash;
ret = dcrypt_key_store_public(pair.pub, DCRYPT_FORMAT_PEM, buf,
&error);
test_assert(ret == TRUE);
ret = dcrypt_key_string_get_info(str_c(buf), &format, &version,
&kind, &encryption_type, &encryption_key_hash,
&key_hash, &error);
test_assert(ret == TRUE);
test_assert(format == DCRYPT_FORMAT_PEM);
test_assert(version == DCRYPT_KEY_VERSION_NA);
test_assert(kind == DCRYPT_KEY_KIND_PUBLIC);
test_assert(encryption_type == DCRYPT_KEY_ENCRYPTION_TYPE_NONE);
test_assert(encryption_key_hash == NULL);
test_assert(key_hash == NULL);
/* test private key */
buffer_set_used_size(buf, 0);
ret = dcrypt_key_store_private(pair.priv, DCRYPT_FORMAT_PEM, NULL,
buf, NULL, NULL, &error);
test_assert(ret == TRUE);
ret = dcrypt_key_string_get_info(str_c(buf), &format, &version,
&kind, &encryption_type, &encryption_key_hash,
&key_hash, &error);
test_assert(ret == TRUE);
test_assert(format == DCRYPT_FORMAT_PEM);
test_assert(version == DCRYPT_KEY_VERSION_NA);
test_assert(kind == DCRYPT_KEY_KIND_PRIVATE);
test_assert(encryption_type == DCRYPT_KEY_ENCRYPTION_TYPE_NONE);
test_assert(encryption_key_hash == NULL);
test_assert(key_hash == NULL);
dcrypt_keypair_unref(&pair);
buffer_free(&buf);
test_end();
}
static
void test_get_info_rsa_private_key(void)
{
test_begin("test_get_info_rsa_private_key");
const char *key = "-----BEGIN RSA PRIVATE KEY-----\n"
"MIICXQIBAAKBgQC89q02I9NezBLQ+otn5XLYE7S+GsKUz59ogr45DA/6MI9jey0W\n"
"56SeWQ1FJD1vDhAx/TRBMfOmhcIPsBjc5sakYOawPdoiqLjOIlO+iHwnbbmLuMsq\n"
"ue09vgvZsKjuTr2F5DOFQY43Bq/Nd+4bjHJItdOM58+xwA2I/8vDbtI8jwIDAQAB\n"
"AoGBAJCUrTMfdjqyKjN7f+6ewKBTc5eBIiB6O53ba3B6qj7jqNKVDIrZ8jq2KFEe\n"
"yWKPgBS/h5vafHKNJU6bjmp2qMUJPB7PTA876eDo0cq9PplUqihiTlXJFwNQYtF+\n"
"o27To5t25+5qdSAj657+lQfFT9Xn9fzYHDmotURxH10FgFkBAkEA+7Ny6lBTeb3W\n"
"LnP0UPfPzQLilEr8u81PLWe69RGtsEaMQHGpHOl4e+bvvVYbG1cgxwxI1m01uR9r\n"
"qpD3qLUdrQJBAMAw6UvN8R+opYTZzwqK7Nliil2QZMPmXM04SV1iFq26NM60w2Fm\n"
"HqOOh0EbpSWsFtIgxJFWoZOtrguxqCJuUqsCQF3EoXf3StHczhDqM8eCOpD2lTCH\n"
"qxXPy8JvlW+9EUbNUWykq0rRE4idJQ0VKe4KjHR6+Buh/dSkhvi5Hvpj1tUCQHRv\n"
"LWeXZLVhXqWVrzEb6VHpuRnmGKX2MdLCfu/sNQEbBlMUgCnJzFYaSybOsMaZ81lq\n"
"MKw8Z7coSYEcKFhzrfECQQD7l+4Bhy8Zuz6VoGGIZwIhxkJrImBFmaUwx8N6jg20\n"
"sgDRYwCoGkGd7B8uIHZLJoWzSSutHiu5i5PYUy5VT1yT\n"
"-----END RSA PRIVATE KEY-----\n";
const char *error = NULL;
test_assert(!dcrypt_key_string_get_info(key, NULL, NULL,
NULL, NULL, NULL, NULL, &error));
test_assert(error != NULL && strstr(error, "pkey") != NULL);
test_end();
}
static
void test_get_info_invalid_keys(void) {
test_begin("test_get_info_invalid_keys");
const char *key = "1:716:030131D8A5FD5167947A0AE9CB112ADED6526654635AA5887051EE2364414B60FF32EBA8FA0BBE9485DBDE8794BBBCB44BBFC0D662A4287A848BA570D4E5E45A11FE0F:d0cfaca5d335f9edc41c84bb47465184cb0e2ec3931bebfcea4dd433615e77a0";
const char *error = NULL;
test_assert(dcrypt_key_string_get_info(key, NULL, NULL,
NULL, NULL, NULL, NULL, &error) == FALSE);
test_assert(error != NULL);
test_assert(strstr(error, "tab") != NULL);
key = "2\t305e301006072a8648ce3d020106052b81040026034a000203fcc90034fa03d6fb79a0fc8b3b43c3398f68e76029307360cdcb9e27bb7e84b3c19dfb7244763bc4d442d216f09b7b7945ed9d182f3156550e9ee30b237a0217dbf79d28975f31\t86706b69d1f640011a65d26a42f2ba20a619173644e1cc7475eb1d90966e84dc";
error = NULL;
test_assert(dcrypt_key_string_get_info(key, NULL, NULL,
NULL, NULL, NULL, NULL, &error) == FALSE);
test_assert(error != NULL);
test_assert(strstr(error, "colon") != NULL);
key = "2";
error = NULL;
test_assert(dcrypt_key_string_get_info(key, NULL, NULL,
NULL, NULL, NULL, NULL, &error) == FALSE);
test_assert(error != NULL);
test_assert(strstr(error, "Unknown") != NULL);
test_end();
}
static
void test_load_invalid_keys(void) {
test_begin("test_load_invalid_keys");
const char *error = NULL;
const char *key = "1:716:0301EB00973C4EFC8FCECA4EA33E941F50B561199A5159BCB6C2EED9DD1D62D65E38A254979D89E28F0C28883E71EE2AD264CD16B863FA094A8F6F69A56B62E8918040:7c9a1039ea2e4fed73e81dd3ffc3fa22ea4a28352939adde7bf8ea858b00fa4f";
struct dcrypt_public_key *pub_key = NULL;
bool ret = dcrypt_key_load_public(&pub_key, key, &error);
test_assert(ret == FALSE);
test_assert(error != NULL);
error = NULL;
key = "2:305e301006072a8648ce3d020106052b81040026034a000203fcc90034fa03d6fb79a0fc8b3b43c3398f68e76029307360cdcb9e27bb7e84b3c19dfb7244763bc4d442d216f09b7b7945ed9d182f3156550e9ee30b237a0217dbf79d28975f31:86706b69d1f640011a65d26a42f2ba20a619173644e1cc7475eb1d90966e84dc";
struct dcrypt_private_key *priv_key = NULL;
ret = dcrypt_key_load_private(&priv_key, key, NULL, NULL, &error);
test_assert(ret == FALSE);
test_assert(error != NULL);
test_end();
}
int main(void) {
random_init();
dcrypt_initialize("openssl", NULL, NULL);
static void (*test_functions[])(void) = {
test_cipher_test_vectors,
test_cipher_aead_test_vectors,
test_hmac_test_vectors,
test_load_v1_keys,
test_load_v1_key,
test_load_v1_public_key,
test_load_v2_key,
test_load_v2_public_key,
test_get_info_v2_key,
test_gen_and_get_info_rsa_pem,
test_get_info_rsa_private_key,
test_get_info_invalid_keys,
test_load_invalid_keys,
NULL
};
int ret = test_run(test_functions);
dcrypt_deinitialize();
random_deinit();
return ret;
}