test-stream.c revision 3a2f060811827cd20809537b8364c0d8892e175c
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic const char key_v1_priv[] = "-----BEGIN PRIVATE KEY-----\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"MIGpAgEAMBAGByqGSM49AgEGBSuBBAAjBIGRMIGOAgEBBEGz2V2VMi/5s+Z+GJh7\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"4WfqZjZUpqqm+NJWojm6BbrZMY+9ZComlTGVcUZ007acFxV93oMmrfmtRUb5ynrb\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"MRFskKFGA0QAAwHrAJc8TvyPzspOoz6UH1C1YRmaUVm8tsLu2d0dYtZeOKJUl52J\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"4o8MKIg+ce4q0mTNFrhj+glKj29ppWti6JGAQA==\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"-----END PRIVATE KEY-----";
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic const char key_v1_pub[] = "-----BEGIN PUBLIC KEY-----\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"MFgwEAYHKoZIzj0CAQYFK4EEACMDRAADAesAlzxO/I/Oyk6jPpQfULVhGZpRWby2\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"wu7Z3R1i1l44olSXnYnijwwoiD5x7irSZM0WuGP6CUqPb2mla2LokYBA\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"-----END PUBLIC KEY-----";
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic const char key_v2_priv[] = "-----BEGIN PRIVATE KEY-----\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgtuQJA+uboZWVwgHc\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"DciyVdrovAPwlMqshDK3s78IDDuhRANCAAQm0VEdzLB9PtD0HA8JK1zifWnj8M00\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"FQzedfp9SQsWyA8dzs5/NFR5MTe6Xbh/ndKEs1zZH3vZ4FlNrilZc0st\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"-----END PRIVATE KEY-----";
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic const char key_v2_pub[] = "-----BEGIN PUBLIC KEY-----\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEJtFRHcywfT7Q9BwPCStc4n1p4/DN\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"NBUM3nX6fUkLFsgPHc7OfzRUeTE3ul24f53ShLNc2R972eBZTa4pWXNLLQ==\n" \
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi"-----END PUBLIC KEY-----";
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic const char test_sample_v1_hash[] = "1d7cc2cc1f1983f76241cc42389911e88590ad58cf9d54cafeb5b198d3723dd1";
80449618d545154901b1fc709d23d77986ef4429Aki Tuomistatic const char test_sample_v1_short_hash[] = "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c";
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomistatic const char test_sample_v2_hash[] = "2e31218656dd34db65b321688bf418dee4ee785e99eb9c21e0d29b4af27a863e";
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi const struct hash_method *hash = hash_method_lookup("sha256");
b0c472c7a93dfc869e2124ca738d62f1d6794e43Timo Sirainen struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v1.asc", IO_BLOCK_SIZE);
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi struct istream *is_2 = i_stream_create_base64_decoder(is_1);
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi struct istream *is_3 = i_stream_create_decrypt(is_2, test_v1_kp.priv);
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi struct istream *is_4 = i_stream_create_hash(is_3, hash, hash_ctx);
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi while((siz = i_stream_read(is_4))>0) { i_stream_skip(is_4, siz); }
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert(strcmp(test_sample_v1_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
80449618d545154901b1fc709d23d77986ef4429Aki Tuomi const struct hash_method *hash = hash_method_lookup("sha256");
80449618d545154901b1fc709d23d77986ef4429Aki Tuomi struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v1_short.asc", IO_BLOCK_SIZE);
80449618d545154901b1fc709d23d77986ef4429Aki Tuomi struct istream *is_2 = i_stream_create_base64_decoder(is_1);
80449618d545154901b1fc709d23d77986ef4429Aki Tuomi struct istream *is_3 = i_stream_create_decrypt(is_2, test_v1_kp.priv);
80449618d545154901b1fc709d23d77986ef4429Aki Tuomi struct istream *is_4 = i_stream_create_hash(is_3, hash, hash_ctx);
80449618d545154901b1fc709d23d77986ef4429Aki Tuomi while((siz = i_stream_read(is_4))>0) { i_stream_skip(is_4, siz); }
80449618d545154901b1fc709d23d77986ef4429Aki Tuomi test_assert(strcmp(test_sample_v1_short_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi const struct hash_method *hash = hash_method_lookup("sha256");
b0c472c7a93dfc869e2124ca738d62f1d6794e43Timo Sirainen struct istream *is_1 = i_stream_create_file(DCRYPT_SRC_DIR"/sample-v2.asc", IO_BLOCK_SIZE);
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi struct istream *is_2 = i_stream_create_base64_decoder(is_1);
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi struct istream *is_3 = i_stream_create_decrypt(is_2, test_v2_kp.priv);
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi struct istream *is_4 = i_stream_create_hash(is_3, hash, hash_ctx);
de1c645a79d42c37dffb7f52e9c643b6251fdcd0Aki Tuomi while((amt = i_stream_read(is_4))>0) { i_stream_skip(is_4, amt); }
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert(strcmp(test_sample_v2_hash, binary_to_hex(hash_dgst, sizeof(hash_dgst))) == 0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi/** this code is left here to show how the sample file is created
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct istream *is = i_stream_create_file("../lib-fts/udhr_fra.txt", 8192);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct istream *is_2 = i_stream_create_hash(is, hash, hash_ctx);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi int fd = open("sample-v2.bin", O_CREAT|O_TRUNC|O_WRONLY, S_IRWXU);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct ostream *os = o_stream_create_fd_file(fd, 0, TRUE);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v2_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi const unsigned char *ptr;
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi while(i_stream_read_data(is_2, &ptr, &siz, 0)>0) {
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi o_stream_nsend(os_2, ptr, siz);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_stream_skip(is_2, siz);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_assert(o_stream_nfinish(os_2)==0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi o_stream_close(os_2);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi i_stream_close(is_2);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi hash->result(hash_ctx, hash_dgst);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi printf("%s\n", binary_to_hex(hash_dgst, sizeof(hash_dgst)));
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi const unsigned char *ptr;
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct ostream *os = iostream_temp_create("/tmp", 0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct ostream *os_2 = o_stream_create_encrypt(os, "<unused>", test_v2_kp.pub, IO_STREAM_ENC_VERSION_1);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct istream *is = iostream_temp_finish(&os, IO_BLOCK_SIZE);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct istream *is_2 = i_stream_create_decrypt(is, test_v2_kp.priv);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi while(i_stream_read_data(is_2, &ptr, &siz, 0)>0) {
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert_idx(pos + siz <= sizeof(payload), pos);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi const unsigned char *ptr;
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi struct ostream *os = iostream_temp_create("/tmp", 0);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi struct ostream *os_2 = o_stream_create_encrypt(os, "<unused>", test_v2_kp.pub, IO_STREAM_ENC_VERSION_1);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi struct istream *is = iostream_temp_finish(&os, IO_BLOCK_SIZE);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi struct istream *is_2 = i_stream_create_decrypt(is, test_v2_kp.priv);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi while(i_stream_read_data(is_2, &ptr, &siz, 0)>0) {
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi test_assert_idx(pos + siz <= sizeof(payload), pos);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi const unsigned char *ptr;
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi struct ostream *os = iostream_temp_create("/tmp", 0);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi struct ostream *os_2 = o_stream_create_encrypt(os, "<unused>", test_v1_kp.pub, IO_STREAM_ENC_VERSION_1);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi /* this should've been enough */
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi struct istream *is = iostream_temp_finish(&os, IO_BLOCK_SIZE);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi struct istream *is_2 = i_stream_create_decrypt(is, test_v1_kp.priv);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi /* read should not fail */
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi while(i_stream_read_data(is_2, &ptr, &siz, 0)>0) {
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi const unsigned char *ptr;
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct ostream *os = iostream_temp_create("/tmp", 0);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v1_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct istream *is = iostream_temp_finish(&os, IO_BLOCK_SIZE);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi struct istream *is_2 = i_stream_create_decrypt(is, test_v1_kp.priv);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi while(i_stream_read_data(is_2, &ptr, &siz, 0)>0) {
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert_idx(pos + siz <= sizeof(payload), pos);
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi const unsigned char *ptr;
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi struct ostream *os = iostream_temp_create("/tmp", 0);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v1_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi struct istream *is = iostream_temp_finish(&os, IO_BLOCK_SIZE);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi struct istream *is_2 = i_stream_create_decrypt(is, test_v1_kp.priv);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi while(i_stream_read_data(is_2, &ptr, &siz, 0)>0) {
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi test_assert_idx(pos + siz <= sizeof(payload), pos);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi test_assert_idx(memcmp(ptr, payload + pos, siz) == 0, pos);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi const unsigned char *ptr;
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi struct ostream *os = iostream_temp_create("/tmp", 0);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi struct ostream *os_2 = o_stream_create_encrypt(os, "aes-256-gcm-sha256", test_v1_kp.pub, IO_STREAM_ENC_INTEGRITY_AEAD);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi /* this should've been enough */
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi struct istream *is = iostream_temp_finish(&os, IO_BLOCK_SIZE);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi struct istream *is_2 = i_stream_create_decrypt(is, test_v1_kp.priv);
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi /* read should not fail */
7de7bc1f75c8f481c03984e3aaef9a5ce060c47fAki Tuomi while(i_stream_read_data(is_2, &ptr, &siz, 0)>0) {
6b136bb200a5f803d0ef5af225ad891e862b6b75Timo Sirainen test_assert(dcrypt_key_load_private(&test_v1_kp.priv, DCRYPT_FORMAT_PEM, key_v1_priv, NULL, NULL, NULL));
6b136bb200a5f803d0ef5af225ad891e862b6b75Timo Sirainen test_assert(dcrypt_key_load_public(&test_v1_kp.pub, DCRYPT_FORMAT_PEM, key_v1_pub, NULL));
6b136bb200a5f803d0ef5af225ad891e862b6b75Timo Sirainen test_assert(dcrypt_key_load_private(&test_v2_kp.priv, DCRYPT_FORMAT_PEM, key_v2_priv, NULL, NULL, NULL));
6b136bb200a5f803d0ef5af225ad891e862b6b75Timo Sirainen test_assert(dcrypt_key_load_public(&test_v2_kp.pub, DCRYPT_FORMAT_PEM, key_v2_pub, NULL));
316cbe323513a0f20d1cf519fe9405e231d633e2Aki Tuomi static void (*test_functions[])(void) = {