test-stream.c revision 6b136bb200a5f803d0ef5af225ad891e862b6b75
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-----";
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 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-----";
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 Sirainenstatic const char test_sample_v1_hash[] = "1d7cc2cc1f1983f76241cc42389911e88590ad58cf9d54cafeb5b198d3723dd1";
7bd72e4deca3cbf757dd1ea298486d9f3bc24226Timo Sirainenstatic const char test_sample_v2_hash[] = "2e31218656dd34db65b321688bf418dee4ee785e99eb9c21e0d29b4af27a863e";
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen const struct hash_method *hash = hash_method_lookup("sha256");
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 struct istream *is_3 = i_stream_create_decrypt(is_2, test_v1_kp.priv);
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen struct istream *is_4 = i_stream_create_hash(is_3, hash, hash_ctx);
fefd62f9aaccecbc6011ecae359a3389668cbff5Timo Sirainen while((siz = i_stream_read(is_4))>0) { i_stream_skip(is_4, siz); }
e3fabe8d0faa9aab7cae2d0eee9653f581a3061dTimo Sirainen i_debug("error: %s", i_stream_get_error(is_4));
080a75584cfbe21ffd2d23c6bbb4cd8fdfd0990cTimo Sirainen test_assert(strcmp(test_sample_v1_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen const struct hash_method *hash = hash_method_lookup("sha256");
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 struct istream *is_3 = i_stream_create_decrypt(is_2, test_v2_kp.priv);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen struct istream *is_4 = i_stream_create_hash(is_3, hash, hash_ctx);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen while((amt = i_stream_read(is_4))>0) { i_stream_skip(is_4, amt); }
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen i_debug("error: %s", i_stream_get_error(is_4));
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen test_assert(strcmp(test_sample_v2_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
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;
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 i_assert(o_stream_nfinish(os_2)==0);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen o_stream_close(os_2);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen i_stream_close(is_2);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen hash->result(hash_ctx, hash_dgst);
9c7f6dbf65ca01026e5f9c8c8b67c7e629c0b5e7Timo Sirainen printf("%s\n", binary_to_hex(hash_dgst, sizeof(hash_dgst)));
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen const unsigned char *ptr;
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));
e3237982a4e6346c2fec4b8f8fb946c826a363fdTimo Sirainen i_debug("error: %s", o_stream_get_error(os_2));
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);
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 test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
d3fce898d31ad40b554c91f3035a7f4d7d52ed52Timo Sirainen const unsigned char *ptr;
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);
1dc6f277f5ac6a3dd5cd6aa75a7ef691de9acb7aTimo Sirainen i_debug("error: %s", o_stream_get_error(os_2));
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);
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen while(i_stream_read_data(is_2, &ptr, &siz, 0)>0) {
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen test_assert_idx(pos + siz <= sizeof(payload), pos);
28c75d59f1d1a7caeb85635964f3881c0038eb23Timo Sirainen test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
fd1a8db8fa61f9c38f063f62753d1bfef0261e19Timo Sirainen i_debug("error: %s", i_stream_get_error(is_2));
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 static void (*test_functions[])(void) = {