test-stream.c revision 6b136bb200a5f803d0ef5af225ad891e862b6b75
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen#include "lib.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "buffer.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "str.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "dcrypt.h"
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen#include "ostream.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "ostream-encrypt.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "istream.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "istream-decrypt.h"
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen#include "istream-hash.h"
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen#include "istream-base64.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "iostream-temp.h"
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen#include "randgen.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "hash-method.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "test-common.h"
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen#include "hex-binary.h"
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen#include <fcntl.h>
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen#include <sys/stat.h>
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen#include <stdio.h>
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainenstatic const char key_v1_priv[] = "-----BEGIN PRIVATE KEY-----\n" \
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen"MIGpAgEAMBAGByqGSM49AgEGBSuBBAAjBIGRMIGOAgEBBEGz2V2VMi/5s+Z+GJh7\n" \
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen"4WfqZjZUpqqm+NJWojm6BbrZMY+9ZComlTGVcUZ007acFxV93oMmrfmtRUb5ynrb\n" \
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen"MRFskKFGA0QAAwHrAJc8TvyPzspOoz6UH1C1YRmaUVm8tsLu2d0dYtZeOKJUl52J\n" \
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen"4o8MKIg+ce4q0mTNFrhj+glKj29ppWti6JGAQA==\n" \
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen"-----END PRIVATE KEY-----";
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainenstatic const char key_v1_pub[] = "-----BEGIN PUBLIC KEY-----\n" \
91dca97b367c54a139c268b56a0c67f564bd9197Timo Sirainen"MFgwEAYHKoZIzj0CAQYFK4EEACMDRAADAesAlzxO/I/Oyk6jPpQfULVhGZpRWby2\n" \
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen"wu7Z3R1i1l44olSXnYnijwwoiD5x7irSZM0WuGP6CUqPb2mla2LokYBA\n" \
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen"-----END PUBLIC KEY-----";
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainenstatic const char key_v2_priv[] = "-----BEGIN PRIVATE KEY-----\n" \
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgtuQJA+uboZWVwgHc\n" \
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen"DciyVdrovAPwlMqshDK3s78IDDuhRANCAAQm0VEdzLB9PtD0HA8JK1zifWnj8M00\n" \
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen"FQzedfp9SQsWyA8dzs5/NFR5MTe6Xbh/ndKEs1zZH3vZ4FlNrilZc0st\n" \
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen"-----END PRIVATE KEY-----";
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenstatic const char key_v2_pub[] = "-----BEGIN PUBLIC KEY-----\n" \
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEJtFRHcywfT7Q9BwPCStc4n1p4/DN\n" \
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen"NBUM3nX6fUkLFsgPHc7OfzRUeTE3ul24f53ShLNc2R972eBZTa4pWXNLLQ==\n" \
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen"-----END PUBLIC KEY-----";
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainenstatic const char test_sample_v1_hash[] = "1d7cc2cc1f1983f76241cc42389911e88590ad58cf9d54cafeb5b198d3723dd1";
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainenstatic const char test_sample_v2_hash[] = "2e31218656dd34db65b321688bf418dee4ee785e99eb9c21e0d29b4af27a863e";
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenstatic struct dcrypt_keypair test_v1_kp;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenstatic struct dcrypt_keypair test_v2_kp;
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainen
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainenstatic
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenvoid test_static_v1_input(void)
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen{
419cf63077e755935ce105747d6ebc67b7d38a7fTimo Sirainen ssize_t siz;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen const struct hash_method *hash = hash_method_lookup("sha256");
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen unsigned char hash_ctx[hash->context_size];
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen unsigned char hash_dgst[hash->digest_size];
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen hash->init(hash_ctx);
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen test_begin("test_static_v1_input");
fefd62f9aaccecbc6011ecae359a3389668cbff5Timo Sirainen
75bb83681e30d6a86109bbafdfe6b513c11124bcTimo Sirainen struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v1.asc", IO_BLOCK_SIZE);
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen struct istream *is_2 = i_stream_create_base64_decoder(is_1);
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen i_stream_unref(&is_1);
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen struct istream *is_3 = i_stream_create_decrypt(is_2, test_v1_kp.priv);
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen i_stream_unref(&is_2);
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen struct istream *is_4 = i_stream_create_hash(is_3, hash, hash_ctx);
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen i_stream_unref(&is_3);
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen
fefd62f9aaccecbc6011ecae359a3389668cbff5Timo Sirainen while((siz = i_stream_read(is_4))>0) { i_stream_skip(is_4, siz); }
75bb83681e30d6a86109bbafdfe6b513c11124bcTimo Sirainen
31257b47d47510ceb093a6b218810a1a5b830c55Timo Sirainen if (is_4->stream_errno != 0)
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen i_debug("error: %s", i_stream_get_error(is_4));
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen test_assert(is_4->stream_errno == 0);
4073f0dbf3277f981a8fcee3b89ea15aaf380a7fTimo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen i_stream_unref(&is_4);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
9844b5359f5cab77e4c31a7ac9e4a60a0073929eTimo Sirainen hash->result(hash_ctx, hash_dgst);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
080a75584cfbe21ffd2d23c6bbb4cd8fdfd0990cTimo Sirainen test_assert(strcmp(test_sample_v1_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
080a75584cfbe21ffd2d23c6bbb4cd8fdfd0990cTimo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen test_end();
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen}
14cdb8cbe37616464c965aec90a8494b339db538Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenstatic
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainenvoid test_static_v2_input(void)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen{
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen test_begin("test_static_v2_input");
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen ssize_t amt;
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen const struct hash_method *hash = hash_method_lookup("sha256");
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen unsigned char hash_ctx[hash->context_size];
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen unsigned char hash_dgst[hash->digest_size];
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen hash->init(hash_ctx);
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v2.asc", IO_BLOCK_SIZE);
4073f0dbf3277f981a8fcee3b89ea15aaf380a7fTimo Sirainen struct istream *is_2 = i_stream_create_base64_decoder(is_1);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen i_stream_unref(&is_1);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen struct istream *is_3 = i_stream_create_decrypt(is_2, test_v2_kp.priv);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen i_stream_unref(&is_2);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen struct istream *is_4 = i_stream_create_hash(is_3, hash, hash_ctx);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen i_stream_unref(&is_3);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen while((amt = i_stream_read(is_4))>0) { i_stream_skip(is_4, amt); }
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen if (is_4->stream_errno != 0)
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen i_debug("error: %s", i_stream_get_error(is_4));
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen test_assert(is_4->stream_errno == 0);
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen i_stream_unref(&is_4);
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen hash->result(hash_ctx, hash_dgst);
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen test_assert(strcmp(test_sample_v2_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen test_end();
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen/** this code is left here to show how the sample file is created
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen struct istream *is = i_stream_create_file("../lib-fts/udhr_fra.txt", 8192);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen struct istream *is_2 = i_stream_create_hash(is, hash, hash_ctx);
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen int fd = open("sample-v2.bin", O_CREAT|O_TRUNC|O_WRONLY, S_IRWXU);
f8ead0942a9b7c8fcf91414ed1b534d5807ca555Timo Sirainen struct ostream *os = o_stream_create_fd_file(fd, 0, TRUE);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v2_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen const unsigned char *ptr;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen size_t siz;
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen while(i_stream_read_data(is_2, &ptr, &siz, 0)>0) {
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen o_stream_nsend(os_2, ptr, siz);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen i_stream_skip(is_2, siz);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen }
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen i_assert(o_stream_nfinish(os_2)==0);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen o_stream_close(os_2);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen i_stream_close(is_2);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen hash->result(hash_ctx, hash_dgst);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen printf("%s\n", binary_to_hex(hash_dgst, sizeof(hash_dgst)));
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen*/
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen}
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainenstatic
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainenvoid test_write_read_v1(void)
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen{
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen test_begin("test_write_read_v1");
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen unsigned char payload[IO_BLOCK_SIZE];
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen const unsigned char *ptr;
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen size_t pos = 0, siz;
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen random_fill_weak(payload, IO_BLOCK_SIZE);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen struct ostream *os = iostream_temp_create("/tmp", 0);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen struct ostream *os_2 = o_stream_create_encrypt(os, "<unused>", test_v2_kp.pub, IO_STREAM_ENC_VERSION_1);
8d25b6ad05b99e75613cb045a121efd51e6afbb6Timo Sirainen o_stream_nsend(os_2, payload, sizeof(payload));
8d25b6ad05b99e75613cb045a121efd51e6afbb6Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen if (os_2->stream_errno != 0)
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen i_debug("error: %s", o_stream_get_error(os_2));
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen test_assert(os_2->stream_errno == 0);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen test_assert(o_stream_nfinish(os_2) == 0);
8d25b6ad05b99e75613cb045a121efd51e6afbb6Timo Sirainen test_assert(os_2->stream_errno == 0);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen o_stream_unref(&os_2);
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen struct istream *is = iostream_temp_finish(&os, IO_BLOCK_SIZE);
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen struct istream *is_2 = i_stream_create_decrypt(is, test_v2_kp.priv);
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen i_stream_unref(&is);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen while(i_stream_read_data(is_2, &ptr, &siz, 0)>0) {
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen test_assert_idx(pos + siz <= sizeof(payload), pos);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen if (pos + siz > sizeof(payload)) break;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen i_stream_skip(is_2, siz);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen }
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen test_assert(is_2->stream_errno == 0);
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen i_stream_unref(&is_2);
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen test_end();
d3fce898d31ad40b554c91f3035a7f4d7d52ed52Timo Sirainen}
d3fce898d31ad40b554c91f3035a7f4d7d52ed52Timo Sirainen
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainenstatic
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainenvoid test_write_read_v2(void)
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen{
d3fce898d31ad40b554c91f3035a7f4d7d52ed52Timo Sirainen test_begin("test_write_read_v2");
d3fce898d31ad40b554c91f3035a7f4d7d52ed52Timo Sirainen unsigned char payload[IO_BLOCK_SIZE];
d3fce898d31ad40b554c91f3035a7f4d7d52ed52Timo Sirainen const unsigned char *ptr;
d3fce898d31ad40b554c91f3035a7f4d7d52ed52Timo Sirainen size_t pos = 0, siz;
3954326e793bdef1e94e0ad781ed6cc7e48beebbTimo Sirainen random_fill_weak(payload, IO_BLOCK_SIZE);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen struct ostream *os = iostream_temp_create("/tmp", 0);
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v1_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen o_stream_nsend(os_2, payload, IO_BLOCK_SIZE);
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen test_assert(o_stream_nfinish(os_2) == 0);
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen if (os_2->stream_errno != 0)
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen i_debug("error: %s", o_stream_get_error(os_2));
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen o_stream_unref(&os_2);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen struct istream *is = iostream_temp_finish(&os, IO_BLOCK_SIZE);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen struct istream *is_2 = i_stream_create_decrypt(is, test_v1_kp.priv);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen i_stream_unref(&is);
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen while(i_stream_read_data(is_2, &ptr, &siz, 0)>0) {
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen test_assert_idx(pos + siz <= sizeof(payload), pos);
2be7df5df08ac4639ad83559ec5fcf552c84fb4aTimo Sirainen if (pos + siz > sizeof(payload)) break;
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen i_stream_skip(is_2, siz);
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen }
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen test_assert(is_2->stream_errno == 0);
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen if (is_2->stream_errno != 0)
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen i_debug("error: %s", i_stream_get_error(is_2));
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen i_stream_unref(&is_2);
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen
92d1458b00f4f236c4cec96a696253d3bbf8b05aTimo Sirainen test_end();
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen}
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainenint main(void) {
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen dcrypt_initialize("openssl", NULL, NULL);
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen random_init();
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen test_assert(dcrypt_key_load_private(&test_v1_kp.priv, DCRYPT_FORMAT_PEM, key_v1_priv, NULL, NULL, NULL));
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen test_assert(dcrypt_key_load_public(&test_v1_kp.pub, DCRYPT_FORMAT_PEM, key_v1_pub, NULL));
2aed6c2062317d1750f59c5c88e77d9f10967462Timo Sirainen test_assert(dcrypt_key_load_private(&test_v2_kp.priv, DCRYPT_FORMAT_PEM, key_v2_priv, NULL, NULL, NULL));
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen test_assert(dcrypt_key_load_public(&test_v2_kp.pub, DCRYPT_FORMAT_PEM, key_v2_pub, NULL));
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen static void (*test_functions[])(void) = {
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen test_static_v1_input,
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen test_static_v2_input,
2be7df5df08ac4639ad83559ec5fcf552c84fb4aTimo Sirainen test_write_read_v1,
52de9d38fcad80df481667bac821cb8222785fe6Timo Sirainen test_write_read_v2,
2be7df5df08ac4639ad83559ec5fcf552c84fb4aTimo Sirainen NULL
2be7df5df08ac4639ad83559ec5fcf552c84fb4aTimo Sirainen };
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen return test_run(test_functions);
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen}
8971ca621b7a7337947306494731b75d1d3919e5Timo Sirainen