test-stream.c revision b671974c5aafc99ff3dc9c6718aff4260aee4ba3
8b1f525acd20f36c836e827de3c251088961c5d9Stephen Gallagherstatic const char key_v1_priv[] = "-----BEGIN PRIVATE KEY-----\n" \
8b1f525acd20f36c836e827de3c251088961c5d9Stephen Gallagher"MIGpAgEAMBAGByqGSM49AgEGBSuBBAAjBIGRMIGOAgEBBEGz2V2VMi/5s+Z+GJh7\n" \
84ae5edab16ad6be5e3be956cb6fa031c1428eb5Stephen Gallagher"4WfqZjZUpqqm+NJWojm6BbrZMY+9ZComlTGVcUZ007acFxV93oMmrfmtRUb5ynrb\n" \
428db8a58c0c149d5efccc6d788f70916c1d34d7Jakub Hrozek"MRFskKFGA0QAAwHrAJc8TvyPzspOoz6UH1C1YRmaUVm8tsLu2d0dYtZeOKJUl52J\n" \
428db8a58c0c149d5efccc6d788f70916c1d34d7Jakub Hrozek"4o8MKIg+ce4q0mTNFrhj+glKj29ppWti6JGAQA==\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"-----END PRIVATE KEY-----";
df4cc3a83c5d6700b6a09ff96cb4a6b1949b1aa9Stephen Gallagherstatic const char key_v1_pub[] = "-----BEGIN PUBLIC KEY-----\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"MFgwEAYHKoZIzj0CAQYFK4EEACMDRAADAesAlzxO/I/Oyk6jPpQfULVhGZpRWby2\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"wu7Z3R1i1l44olSXnYnijwwoiD5x7irSZM0WuGP6CUqPb2mla2LokYBA\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"-----END PUBLIC KEY-----";
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic const char key_v2_priv[] = "-----BEGIN PRIVATE KEY-----\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgtuQJA+uboZWVwgHc\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"DciyVdrovAPwlMqshDK3s78IDDuhRANCAAQm0VEdzLB9PtD0HA8JK1zifWnj8M00\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"FQzedfp9SQsWyA8dzs5/NFR5MTe6Xbh/ndKEs1zZH3vZ4FlNrilZc0st\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"-----END PRIVATE KEY-----";
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic const char key_v2_pub[] = "-----BEGIN PUBLIC KEY-----\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEJtFRHcywfT7Q9BwPCStc4n1p4/DN\n" \
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher"NBUM3nX6fUkLFsgPHc7OfzRUeTE3ul24f53ShLNc2R972eBZTa4pWXNLLQ==\n" \
8a5e793a0576250da80371e53aa3e7eba15cdb63Sumit Bose"-----END PUBLIC KEY-----";
8a5e793a0576250da80371e53aa3e7eba15cdb63Sumit Bosestatic const char test_sample_v1_hash[] = "1d7cc2cc1f1983f76241cc42389911e88590ad58cf9d54cafeb5b198d3723dd1";
90fd1bbd6035cdab46faa3a695a2fb2be6508b17Sumit Bosestatic const char test_sample_v1_short_hash[] = "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c";
90fd1bbd6035cdab46faa3a695a2fb2be6508b17Sumit Bosestatic const char test_sample_v2_hash[] = "2e31218656dd34db65b321688bf418dee4ee785e99eb9c21e0d29b4af27a863e";
2a552e43581c74f51205c7141ec9f6e9542509f8Stephen Gallagher const struct hash_method *hash = hash_method_lookup("sha256");
2a552e43581c74f51205c7141ec9f6e9542509f8Stephen Gallagher unsigned char hash_ctx[hash->context_size];
2cbdd12983eb85eddb90f64cfafb24eae5b448f4Jakub Hrozek struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v1.asc", IO_BLOCK_SIZE);
b9c8ce2bdd4045782c243605a1b999098bedcffcNoam Meltzer struct istream *is_2 = i_stream_create_base64_decoder(is_1);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is_3 = i_stream_create_decrypt(is_2, test_v1_kp.priv);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is_4 = i_stream_create_hash(is_3, hash, hash_ctx);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher while((siz = i_stream_read(is_4))>0) { i_stream_skip(is_4, siz); }
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_debug("error: %s", i_stream_get_error(is_4));
b32159300fea63222d8dd9200ed634087704ea74Stephen Gallagher test_assert(strcmp(test_sample_v1_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
bc13c352ba9c2877f1e9bc62e55ad60fc000a55dJakub Hrozek const struct hash_method *hash = hash_method_lookup("sha256");
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher unsigned char hash_dgst[hash->digest_size];
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v1_short.asc", IO_BLOCK_SIZE);
054b5d4bb98973698f74d66b14ccd14394b53f10Lukas Slebodnik struct istream *is_2 = i_stream_create_base64_decoder(is_1);
a3d176d116ceccd6a7547c128fab5df5cdd2c2b6Michal Zidek struct istream *is_3 = i_stream_create_decrypt(is_2, test_v1_kp.priv);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is_4 = i_stream_create_hash(is_3, hash, hash_ctx);
4dd615c01357b8715711aad6820ba9595d3ad377Stephen Gallagher while((siz = i_stream_read(is_4))>0) { i_stream_skip(is_4, siz); }
558998ce664055a75595371118f818084d8f2b23Jan Cholasta i_debug("error: %s", i_stream_get_error(is_4));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert(strcmp(test_sample_v1_short_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const struct hash_method *hash = hash_method_lookup("sha256");
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher unsigned char hash_ctx[hash->context_size];
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher unsigned char hash_dgst[hash->digest_size];
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v2.asc", IO_BLOCK_SIZE);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is_2 = i_stream_create_base64_decoder(is_1);
1746e8b8399da2a7a8da4aace186f66055ccfec1Jakub Hrozek struct istream *is_3 = i_stream_create_decrypt(is_2, test_v2_kp.priv);
2827b0d03f7b6bafa504d22a5d7ca39cbda048b3Pavel Březina struct istream *is_4 = i_stream_create_hash(is_3, hash, hash_ctx);
9e80079370ff3b943832adc3c5ef430e64be0a0cJakub Hrozek while((amt = i_stream_read(is_4))>0) { i_stream_skip(is_4, amt); }
e7311aec8d691e5427317442387af1bc8fff3742Jan Cholasta i_debug("error: %s", i_stream_get_error(is_4));
19d3aba12c70528708be9440aca66038a291f29eYassir Elley test_assert(strcmp(test_sample_v2_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
cb4d5b588e704114b7090678752d33512baa718eJakub Hrozek/** this code is left here to show how the sample file is created
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is = i_stream_create_file("../lib-fts/udhr_fra.txt", 8192);
b9e5bd09a5ff7009537a18914dbebcf10498f592Sumit Bose struct istream *is_2 = i_stream_create_hash(is, hash, hash_ctx);
b9e5bd09a5ff7009537a18914dbebcf10498f592Sumit Bose int fd = open("sample-v2.bin", O_CREAT|O_TRUNC|O_WRONLY, S_IRWXU);
b9e5bd09a5ff7009537a18914dbebcf10498f592Sumit Bose struct ostream *os = o_stream_create_fd_file(fd, 0, TRUE);
b9e5bd09a5ff7009537a18914dbebcf10498f592Sumit Bose struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v2_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const unsigned char *ptr;
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher while(i_stream_read_data(is_2, &ptr, &siz, 0)>0) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher o_stream_nsend(os_2, ptr, siz);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_stream_skip(is_2, siz);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_assert(o_stream_nfinish(os_2)==0);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher o_stream_close(os_2);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_stream_close(is_2);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher hash->result(hash_ctx, hash_dgst);
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher printf("%s\n", binary_to_hex(hash_dgst, sizeof(hash_dgst)));
e5e8252ec48bfdd4e7529debc705c8e090264b9aSumit Bose const unsigned char *ptr;
71e7918be3ca5d38794a16a17f6b4f19a24d51fcPavel Březina buffer_t *buf = buffer_create_dynamic(default_pool, sizeof(payload));
bbaba8b3ef9bc101863b8687f234f4ee956caacdPavel Březina struct ostream *os = o_stream_create_buffer(buf);
80314a6f3ea8d81abe73d501d5b953a256cb2167Pavel Březina struct ostream *os_2 = o_stream_create_encrypt(os, "<unused>", test_v2_kp.pub, IO_STREAM_ENC_VERSION_1);
80314a6f3ea8d81abe73d501d5b953a256cb2167Pavel Březina o_stream_nsend(os_2, payload, sizeof(payload));
80314a6f3ea8d81abe73d501d5b953a256cb2167Pavel Březina i_debug("error: %s", o_stream_get_error(os_2));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is = test_istream_create_data(buf->data, buf->used);
150b76e13b7c4f3ccf1d709bf517ca2af6b2c9a2Jakub Hrozek struct istream *is_2 = i_stream_create_decrypt(is, test_v2_kp.priv);
ae6c1596225c65bec2a2dabff9eee4e3e0691181Abhishek Singh while(i_stream_read_data(is_2, &ptr, &siz, 0)>=0) {
777374243e15c53e7b0a7345e190c1018920be18Jakub Hrozek test_assert_idx(pos + siz <= sizeof(payload), pos);
e046ae03d0f55b1c8b0ec2fa6139bf86a3449adfPavel Březina test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
a2e417f38c57ed87c956ddcecf4dafca93842b65Lukas Slebodnik const unsigned char *ptr;
a2e417f38c57ed87c956ddcecf4dafca93842b65Lukas Slebodnik buffer_t *buf = buffer_create_dynamic(default_pool, 64);
a2e417f38c57ed87c956ddcecf4dafca93842b65Lukas Slebodnik struct ostream *os = o_stream_create_buffer(buf);
f92ace4a52602e8c38a34f2392bec3deeac2ddddJakub Hrozek struct ostream *os_2 = o_stream_create_encrypt(os, "<unused>", test_v2_kp.pub, IO_STREAM_ENC_VERSION_1);
150b76e13b7c4f3ccf1d709bf517ca2af6b2c9a2Jakub Hrozek o_stream_nsend(os_2, payload, sizeof(payload));
f232789430a080384188d5da89b19d874cf17513Jakub Hrozek i_debug("error: %s", o_stream_get_error(os_2));
fa551077410019fb34460dc730950e93b62b2963Jakub Hrozek struct istream *is = test_istream_create_data(buf->data, buf->used);
fa551077410019fb34460dc730950e93b62b2963Jakub Hrozek struct istream *is_2 = i_stream_create_decrypt(is, test_v2_kp.priv);
03a071399ab5fb58d4bb4fa38928413a21ae4d61Nikolai Kondrashov while(i_stream_read_data(is_2, &ptr, &siz, 0)>=0) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert_idx(pos + siz <= sizeof(payload), pos);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const unsigned char *ptr;
8a5e793a0576250da80371e53aa3e7eba15cdb63Sumit Bose buffer_t *buf = buffer_create_dynamic(default_pool, 64);
8a5e793a0576250da80371e53aa3e7eba15cdb63Sumit Bose struct ostream *os = o_stream_create_buffer(buf);
8a5e793a0576250da80371e53aa3e7eba15cdb63Sumit Bose struct ostream *os_2 = o_stream_create_encrypt(os, "<unused>", test_v1_kp.pub, IO_STREAM_ENC_VERSION_1);
90fd1bbd6035cdab46faa3a695a2fb2be6508b17Sumit Bose /* this should've been enough */
af4ffe1001adcc0a96897e426d26444f07af9aa1Benjamin Franzke struct istream *is = test_istream_create_data(buf->data, buf->used);
af4ffe1001adcc0a96897e426d26444f07af9aa1Benjamin Franzke struct istream *is_2 = i_stream_create_decrypt(is, test_v1_kp.priv);
af4ffe1001adcc0a96897e426d26444f07af9aa1Benjamin Franzke /* read should not fail */
96453f402831275a39d5fb89c33c9776e148d03fStephen Gallagher while ((ret = i_stream_read_data(is_2, &ptr, &siz, 0)) >= 0) {
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek i_debug("error: %s", i_stream_get_error(is_2));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const unsigned char *ptr;
3b1df539835367cb81cd5ff0f9959947d5642e55Stephen Gallagher buffer_t *buf = buffer_create_dynamic(default_pool, sizeof(payload));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct ostream *os = o_stream_create_buffer(buf);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v1_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
96453f402831275a39d5fb89c33c9776e148d03fStephen Gallagher o_stream_nsend(os_2, payload, sizeof(payload));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_debug("error: %s", o_stream_get_error(os_2));
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose struct istream *is = test_istream_create_data(buf->data, buf->used);
c080a11e9e88f35e40aff4e476cabbd971833019Sumit Bose struct istream *is_2 = i_stream_create_decrypt(is, test_v1_kp.priv);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher while(i_stream_read_data(is_2, &ptr, &siz, 0)>=0) {
15b266d9f14dad26da8678a79019749d0f69532eStephen Gallagher test_assert_idx(pos + siz <= sizeof(payload), pos);
b97595ae059c69b1960a6e7e56d74660388a683bJan Zeleny test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov i_debug("error: %s", i_stream_get_error(is_2));
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov const unsigned char *ptr;
6398f22526303343193a18e514602f1af6fb29cbNikolai Kondrashov buffer_t *buf = buffer_create_dynamic(default_pool, 64);
6398f22526303343193a18e514602f1af6fb29cbNikolai Kondrashov struct ostream *os = o_stream_create_buffer(buf);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v1_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher o_stream_nsend(os_2, payload, sizeof(payload));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_debug("error: %s", o_stream_get_error(os_2));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is = test_istream_create_data(buf->data, buf->used);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is_2 = i_stream_create_decrypt(is, test_v1_kp.priv);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher while(i_stream_read_data(is_2, &ptr, &siz, 0)>=0) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert_idx(pos + siz <= sizeof(payload), pos);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher const unsigned char *ptr;
e7311aec8d691e5427317442387af1bc8fff3742Jan Cholasta buffer_t *buf = buffer_create_dynamic(default_pool, 64);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct ostream *os = o_stream_create_buffer(buf);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v1_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_debug("error: %s", o_stream_get_error(os_2));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher /* this should've been enough */
c0f9698cd951b7223f251ff2511c4b22a6e4ba60Jan Zeleny struct istream *is = test_istream_create_data(buf->data, buf->used);
769347ad4d35d43488eb98f980143495b0db415dStef Walter struct istream *is_2 = i_stream_create_decrypt(is, test_v1_kp.priv);
769347ad4d35d43488eb98f980143495b0db415dStef Walter /* read should not fail */
769347ad4d35d43488eb98f980143495b0db415dStef Walter while ((ret = i_stream_read_data(is_2, &ptr, &siz, 0)) >= 0) {
543676afec3c08fdc0a5a794976adc8dfdca974bJakub Hrozek i_debug("error: %s", i_stream_get_error(is_2));
ca261795ce61c41d7e62217ccb2ee913923040ffPavel Březinastatic int no_op_cb(const char *digest ATTR_UNUSED,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct dcrypt_private_key **priv_key_r ATTR_UNUSED,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagherstatic void test_read_0_to_400_byte_garbage(void)
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_begin("test_read_0_to_100_byte_garbage");
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *is = test_istream_create_data(data, s);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher struct istream *ds = i_stream_create_decrypt_callback(is,
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher for (size_t offset = 0; offset <= s && siz == 0; offset++) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher dcrypt_key_unref_private(&test_v1_kp.priv);
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher if (!dcrypt_initialize(NULL, NULL, NULL)) {
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher i_error("No functional dcrypt backend found - skipping tests");
fe2091327ff44f80d6681c261494e4432404e9baStephen Gallagher test_assert(dcrypt_key_load_private(&test_v1_kp.priv, key_v1_priv, NULL, NULL, NULL));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert(dcrypt_key_load_public(&test_v1_kp.pub, key_v1_pub, NULL));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher test_assert(dcrypt_key_load_private(&test_v2_kp.priv, key_v2_priv, NULL, NULL, NULL));
2e6087c6cc903d5164b9a1d5e3d791fd046001d9Jakub Hrozek test_assert(dcrypt_key_load_public(&test_v2_kp.pub, key_v2_pub, NULL));
551aa6c36797ed720487f5974dcadabf19e6ff9fStephen Gallagher static void (*test_functions[])(void) = {