2013-11-04 17:43:22 +01:00
|
|
|
/*
|
|
|
|
|
This file is part of Pretty Curved Privacy (pcp1).
|
|
|
|
|
|
2016-05-09 22:24:13 +02:00
|
|
|
Copyright (C) 2013-2016 T.v.Dein.
|
2013-11-04 17:43:22 +01:00
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
2014-02-27 13:55:43 +01:00
|
|
|
You can contact me by mail: <tom AT vondein DOT org>.
|
2013-11-04 17:43:22 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2013-10-28 22:50:05 +01:00
|
|
|
#include "encryption.h"
|
|
|
|
|
|
2014-01-27 16:12:43 +01:00
|
|
|
int pcpdecrypt(char *id, int useid, char *infile, char *outfile, char *passwd, int verify) {
|
2013-10-28 22:50:05 +01:00
|
|
|
FILE *in = NULL;
|
|
|
|
|
FILE *out = NULL;
|
|
|
|
|
pcp_key_t *secret = NULL;
|
2014-02-25 11:09:58 +01:00
|
|
|
byte *symkey = NULL;
|
2014-01-22 23:20:30 +01:00
|
|
|
size_t dlen;
|
|
|
|
|
uint8_t head;
|
2014-08-11 15:45:47 +02:00
|
|
|
int anon = 0;
|
2014-01-22 23:20:30 +01:00
|
|
|
|
2013-10-28 22:50:05 +01:00
|
|
|
if(infile == NULL)
|
|
|
|
|
in = stdin;
|
|
|
|
|
else {
|
|
|
|
|
if((in = fopen(infile, "rb")) == NULL) {
|
2014-05-04 17:11:03 +02:00
|
|
|
fatal(ptx, "Could not open input file %s\n", infile);
|
2014-01-20 10:16:05 +01:00
|
|
|
goto errde3;
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(outfile == NULL)
|
|
|
|
|
out = stdout;
|
|
|
|
|
else {
|
|
|
|
|
if((out = fopen(outfile, "wb+")) == NULL) {
|
2014-05-04 17:11:03 +02:00
|
|
|
fatal(ptx, "Could not open output file %s\n", outfile);
|
2014-01-20 10:16:05 +01:00
|
|
|
goto errde3;
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-27 13:55:43 +01:00
|
|
|
Pcpstream *pin = ps_new_file(in);
|
|
|
|
|
Pcpstream *pout = ps_new_file(out);
|
|
|
|
|
|
|
|
|
|
ps_setdetermine(pin, PCP_BLOCK_SIZE/2);
|
|
|
|
|
|
2014-02-05 20:41:16 +01:00
|
|
|
/* determine crypt mode */
|
2014-02-27 13:55:43 +01:00
|
|
|
ps_read(pin, &head, 1);
|
|
|
|
|
|
|
|
|
|
if(!ps_end(pin) && !ps_err(pin)) {
|
2014-01-22 23:20:30 +01:00
|
|
|
if(head == PCP_SYM_CIPHER) {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* symetric mode */
|
2014-02-25 11:09:58 +01:00
|
|
|
byte *salt = ucmalloc(90);
|
2014-01-22 23:20:30 +01:00
|
|
|
char stsalt[] = PBP_COMPAT_SALT;
|
|
|
|
|
memcpy(salt, stsalt, 90);
|
|
|
|
|
|
|
|
|
|
char *passphrase;
|
|
|
|
|
if(passwd == NULL) {
|
2016-05-09 22:24:13 +02:00
|
|
|
pcp_readpass(ptx, &passphrase,
|
|
|
|
|
"Enter passphrase for symetric decryption", NULL, 1, NULL);
|
2014-01-22 23:20:30 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2016-05-09 22:24:13 +02:00
|
|
|
passphrase = smalloc(strlen(passwd)+1);
|
|
|
|
|
memcpy(passphrase, passwd, strlen(passwd) + 1);
|
2014-01-22 23:20:30 +01:00
|
|
|
}
|
|
|
|
|
|
2014-05-04 17:11:03 +02:00
|
|
|
symkey = pcp_scrypt(ptx, passphrase, strlen(passphrase), salt, 90);
|
2015-01-13 13:07:32 +01:00
|
|
|
sfree(passphrase);
|
2014-01-22 23:20:30 +01:00
|
|
|
free(salt);
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
2015-12-07 14:13:27 +01:00
|
|
|
else if(head == PCP_ASYM_CIPHER || head == PCP_ASYM_CIPHER_SIG
|
2016-05-09 22:24:13 +02:00
|
|
|
|| head == PCP_ASYM_CIPHER_ANON || head == PCP_ASYM_CIPHER_ANON_SIG) {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* asymetric mode */
|
2014-01-22 23:20:30 +01:00
|
|
|
if(useid) {
|
2016-05-09 22:24:13 +02:00
|
|
|
secret = pcphash_keyexists(ptx, id);
|
|
|
|
|
if(secret == NULL) {
|
|
|
|
|
fatal(ptx, "Could not find a secret key with id 0x%s in vault %s!\n",
|
|
|
|
|
id, vault->filename);
|
|
|
|
|
goto errde3;
|
|
|
|
|
}
|
2014-01-22 23:20:30 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2016-05-09 22:24:13 +02:00
|
|
|
secret = pcp_find_primary_secret();
|
|
|
|
|
if(secret == NULL) {
|
|
|
|
|
fatal(ptx, "Could not find a secret key in vault %s!\n", id, vault->filename);
|
|
|
|
|
goto errde3;
|
|
|
|
|
}
|
2014-01-22 23:20:30 +01:00
|
|
|
}
|
2014-08-11 15:45:47 +02:00
|
|
|
|
2015-08-19 20:53:46 +02:00
|
|
|
char *passphrase;
|
|
|
|
|
if(passwd == NULL) {
|
2016-05-09 22:24:13 +02:00
|
|
|
pcp_readpass(ptx, &passphrase,
|
|
|
|
|
"Enter passphrase to decrypt your secret key", NULL, 1, NULL);
|
2015-08-19 20:53:46 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2016-05-09 22:24:13 +02:00
|
|
|
passphrase = smalloc(strlen(passwd)+1);
|
|
|
|
|
memcpy(passphrase, passwd, strlen(passwd)+1);
|
2014-01-22 23:20:30 +01:00
|
|
|
}
|
2015-08-19 20:53:46 +02:00
|
|
|
|
|
|
|
|
secret = pcpkey_decrypt(ptx, secret, passphrase);
|
|
|
|
|
sfree(passphrase);
|
|
|
|
|
if(secret == NULL)
|
2016-05-09 22:24:13 +02:00
|
|
|
goto errde3;
|
2015-08-19 20:53:46 +02:00
|
|
|
|
|
|
|
|
if(head == PCP_ASYM_CIPHER_ANON)
|
2016-05-09 22:24:13 +02:00
|
|
|
anon = 1;
|
2015-08-19 20:53:46 +02:00
|
|
|
|
|
|
|
|
if(head == PCP_ASYM_CIPHER_SIG)
|
2016-05-09 22:24:13 +02:00
|
|
|
verify = 1;
|
2015-12-07 14:13:27 +01:00
|
|
|
|
|
|
|
|
if(head == PCP_ASYM_CIPHER_ANON_SIG) {
|
2016-05-09 22:24:13 +02:00
|
|
|
anon = 1;
|
|
|
|
|
verify = 1;
|
2015-12-07 14:13:27 +01:00
|
|
|
}
|
2014-01-22 23:20:30 +01:00
|
|
|
}
|
2014-05-06 11:50:28 +02:00
|
|
|
else {
|
|
|
|
|
fatal(ptx, "Could not determine input file type (got: %02x)\n", head);
|
|
|
|
|
goto errde3;
|
|
|
|
|
}
|
2014-01-22 23:20:30 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2014-05-06 11:50:28 +02:00
|
|
|
fatal(ptx, "Failed to read from file!\n");
|
2014-01-22 23:20:30 +01:00
|
|
|
goto errde3;
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
|
2014-08-08 18:40:53 +02:00
|
|
|
if(symkey == NULL) {
|
2014-08-11 15:45:47 +02:00
|
|
|
dlen = pcp_decrypt_stream(ptx, pin, pout, secret, NULL, verify, anon);
|
2014-08-08 18:40:53 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2014-08-11 15:45:47 +02:00
|
|
|
dlen = pcp_decrypt_stream(ptx, pin, pout, NULL, symkey, verify, 0);
|
2015-01-13 13:07:32 +01:00
|
|
|
sfree(symkey);
|
2015-07-30 11:47:03 +02:00
|
|
|
symkey = NULL;
|
2014-08-08 18:40:53 +02:00
|
|
|
}
|
2014-02-15 13:10:51 +01:00
|
|
|
|
|
|
|
|
ps_close(pin);
|
|
|
|
|
ps_close(pout);
|
2013-10-28 22:50:05 +01:00
|
|
|
|
2014-01-20 10:16:05 +01:00
|
|
|
if(dlen > 0) {
|
2014-01-27 16:12:43 +01:00
|
|
|
if(verify)
|
2014-03-15 17:26:42 +01:00
|
|
|
fprintf(stderr, "Decrypted and Verified %"FMT_SIZE_T" bytes successfully\n", (SIZE_T_CAST)dlen);
|
2014-01-27 16:12:43 +01:00
|
|
|
else
|
2014-03-15 17:26:42 +01:00
|
|
|
fprintf(stderr, "Decrypted %"FMT_SIZE_T" bytes successfully\n", (SIZE_T_CAST)dlen);
|
2014-01-20 10:16:05 +01:00
|
|
|
return 0;
|
2013-11-02 11:02:36 +01:00
|
|
|
}
|
2013-10-28 22:50:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
errde3:
|
2014-08-08 18:40:53 +02:00
|
|
|
if(symkey != NULL)
|
2015-07-30 11:47:03 +02:00
|
|
|
sfree(symkey);
|
2014-08-08 18:40:53 +02:00
|
|
|
|
2013-10-28 22:50:05 +01:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-12-07 14:13:27 +01:00
|
|
|
int pcpencrypt(char *id, char *infile, char *outfile, char *passwd,
|
2016-05-09 22:24:13 +02:00
|
|
|
plist_t *recipient, int signcrypt, int armor, int anon) {
|
2013-10-28 22:50:05 +01:00
|
|
|
FILE *in = NULL;
|
|
|
|
|
FILE *out = NULL;
|
2014-02-05 20:41:16 +01:00
|
|
|
pcp_pubkey_t *pubhash = NULL; /* FIXME: add free() */
|
2014-01-20 16:07:01 +01:00
|
|
|
pcp_pubkey_t *tmp = NULL;
|
2013-11-29 20:01:42 +01:00
|
|
|
pcp_pubkey_t *pub = NULL;
|
2013-10-28 22:50:05 +01:00
|
|
|
pcp_key_t *secret = NULL;
|
2015-12-07 14:13:27 +01:00
|
|
|
pcp_key_t *signsecret = NULL;
|
2014-02-25 11:09:58 +01:00
|
|
|
byte *symkey = NULL;
|
2015-01-17 15:04:07 +01:00
|
|
|
int symmode = 0;
|
2013-10-28 22:50:05 +01:00
|
|
|
|
2014-01-22 23:20:30 +01:00
|
|
|
if(id == NULL && recipient == NULL) {
|
2015-01-17 15:04:07 +01:00
|
|
|
/* sym mode */
|
|
|
|
|
symmode = 1;
|
2014-01-22 23:20:30 +01:00
|
|
|
char *passphrase;
|
|
|
|
|
if(passwd == NULL) {
|
2015-07-30 11:47:03 +02:00
|
|
|
pcp_readpass(ptx, &passphrase,
|
2015-05-30 11:11:20 +02:00
|
|
|
"Enter passphrase for symetric encryption", "Repeat passphrase", 1, NULL);
|
2014-01-22 23:20:30 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2015-01-13 13:07:32 +01:00
|
|
|
passphrase = smalloc(strlen(passwd)+1);
|
|
|
|
|
memcpy(passphrase, passwd, strlen(passwd)+1);
|
2014-01-22 23:20:30 +01:00
|
|
|
}
|
2014-02-25 11:09:58 +01:00
|
|
|
byte *salt = ucmalloc(90); /* FIXME: use random salt, concat it with result afterwards */
|
2014-01-22 23:20:30 +01:00
|
|
|
char stsalt[] = PBP_COMPAT_SALT;
|
|
|
|
|
memcpy(salt, stsalt, 90);
|
2014-05-04 17:11:03 +02:00
|
|
|
symkey = pcp_scrypt(ptx, passphrase, strlen(passphrase), salt, 90);
|
2014-01-22 23:20:30 +01:00
|
|
|
free(salt);
|
2015-01-13 13:07:32 +01:00
|
|
|
sfree(passphrase);
|
2014-01-22 23:20:30 +01:00
|
|
|
}
|
2014-02-05 13:09:20 +01:00
|
|
|
else if(id != NULL && recipient == NULL) {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* lookup by id */
|
2014-05-04 17:11:03 +02:00
|
|
|
tmp = pcphash_pubkeyexists(ptx, id);
|
2014-01-20 16:07:01 +01:00
|
|
|
if(tmp == NULL) {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* self-encryption: look if its a secret one */
|
2014-05-04 17:11:03 +02:00
|
|
|
pcp_key_t *s = pcphash_keyexists(ptx, id);
|
2014-01-20 16:07:01 +01:00
|
|
|
if(s != NULL) {
|
2016-05-09 22:24:13 +02:00
|
|
|
tmp = pcpkey_pub_from_secret(s);
|
|
|
|
|
pub = ucmalloc(sizeof(pcp_pubkey_t));
|
|
|
|
|
memcpy(pub, tmp, sizeof(pcp_pubkey_t));
|
|
|
|
|
HASH_ADD_STR( pubhash, id, pub);
|
2014-01-20 16:07:01 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2016-05-09 22:24:13 +02:00
|
|
|
fatal(ptx, "Could not find a public key with id 0x%s in vault %s!\n",
|
|
|
|
|
id, vault->filename);
|
|
|
|
|
goto erren3;
|
2014-01-20 16:07:01 +01:00
|
|
|
}
|
2013-11-07 13:36:02 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* found one by id, copy into local hash */
|
2014-01-21 16:11:04 +01:00
|
|
|
pub = ucmalloc(sizeof(pcp_pubkey_t));
|
2014-01-20 16:07:01 +01:00
|
|
|
memcpy(pub, tmp, sizeof(pcp_pubkey_t));
|
2014-08-07 21:33:52 +02:00
|
|
|
HASH_ADD_STR( pubhash, id, pub);
|
2013-11-07 13:36:02 +01:00
|
|
|
}
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
2014-01-20 16:07:01 +01:00
|
|
|
else if(recipient != NULL) {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* lookup by recipient list */
|
|
|
|
|
/* iterate through global hashlist */
|
|
|
|
|
/* copy matches into temporary pubhash */
|
2014-01-21 16:11:04 +01:00
|
|
|
plist_t *rec;
|
2014-05-04 17:11:03 +02:00
|
|
|
pcphash_iteratepub(ptx, tmp) {
|
2014-01-21 16:11:04 +01:00
|
|
|
rec = recipient->first;
|
2014-02-05 13:09:20 +01:00
|
|
|
while (rec != NULL) {
|
2016-05-09 22:24:13 +02:00
|
|
|
_lc(rec->value);
|
|
|
|
|
if(strnstr(tmp->mail, rec->value, 255) != NULL
|
|
|
|
|
|| strnstr(tmp->owner, rec->value, 255) != NULL) {
|
|
|
|
|
pub = ucmalloc(sizeof(pcp_pubkey_t));
|
|
|
|
|
memcpy(pub, tmp, sizeof(pcp_pubkey_t));
|
|
|
|
|
HASH_ADD_STR( pubhash, id, pub);
|
|
|
|
|
/* fprintf(stderr, " => found a matching key %s\n", tmp->id); */
|
|
|
|
|
}
|
|
|
|
|
rec = rec->next;
|
2014-01-21 16:11:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
2015-01-17 15:04:07 +01:00
|
|
|
|
|
|
|
|
/* look if we need to add ourselfes */
|
|
|
|
|
rec = recipient->first;
|
|
|
|
|
while (rec != NULL) {
|
|
|
|
|
if(strnstr("__self__", rec->value, 13) != NULL) {
|
2016-05-09 22:24:13 +02:00
|
|
|
pcp_key_t *s = pcp_find_primary_secret();
|
|
|
|
|
pcp_pubkey_t *p = pcpkey_pub_from_secret(s);
|
|
|
|
|
HASH_ADD_STR( pubhash, id, p);
|
|
|
|
|
break;
|
2015-01-17 15:04:07 +01:00
|
|
|
}
|
|
|
|
|
rec = rec->next;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-21 16:11:04 +01:00
|
|
|
if(HASH_COUNT(pubhash) == 0) {
|
2014-05-04 17:11:03 +02:00
|
|
|
fatal(ptx, "no matching key found for specified recipient(s)!\n");
|
2014-01-21 16:11:04 +01:00
|
|
|
goto erren3;
|
|
|
|
|
}
|
2014-01-20 16:07:01 +01:00
|
|
|
}
|
|
|
|
|
|
2013-10-28 22:50:05 +01:00
|
|
|
|
2015-01-17 15:04:07 +01:00
|
|
|
if(symmode != 1) {
|
2014-02-05 20:41:16 +01:00
|
|
|
/* we're using a random secret keypair on our side */
|
2015-12-07 14:13:27 +01:00
|
|
|
if(signcrypt || !anon) {
|
2014-08-11 15:45:47 +02:00
|
|
|
secret = pcp_find_primary_secret();
|
|
|
|
|
if(secret == NULL) {
|
|
|
|
|
fatal(ptx, "Could not find a secret key in vault %s!\n", id, vault->filename);
|
2016-05-09 22:24:13 +02:00
|
|
|
goto erren2;
|
2014-08-11 15:45:47 +02:00
|
|
|
}
|
2014-01-22 23:20:30 +01:00
|
|
|
|
2015-08-19 20:53:46 +02:00
|
|
|
char *passphrase;
|
|
|
|
|
if(passwd == NULL) {
|
2016-05-09 22:24:13 +02:00
|
|
|
pcp_readpass(ptx, &passphrase,
|
|
|
|
|
"Enter passphrase to decrypt your secret key", NULL, 1, NULL);
|
2014-01-22 23:20:30 +01:00
|
|
|
}
|
2015-08-19 20:53:46 +02:00
|
|
|
else {
|
2016-05-09 22:24:13 +02:00
|
|
|
passphrase = smalloc(strlen(passwd)+1);
|
|
|
|
|
memcpy(passphrase, passwd, strlen(passwd)+1);
|
2015-08-19 20:53:46 +02:00
|
|
|
}
|
|
|
|
|
secret = pcpkey_decrypt(ptx, secret, passphrase);
|
|
|
|
|
sfree(passphrase);
|
|
|
|
|
if(secret == NULL)
|
2016-05-09 22:24:13 +02:00
|
|
|
goto erren2;
|
2015-12-07 14:13:27 +01:00
|
|
|
|
|
|
|
|
signsecret = secret;
|
2014-01-22 16:23:30 +01:00
|
|
|
}
|
2015-12-07 14:13:27 +01:00
|
|
|
if(anon)
|
|
|
|
|
secret = pcpkey_new();
|
2014-01-22 23:20:30 +01:00
|
|
|
}
|
2013-10-28 22:50:05 +01:00
|
|
|
|
|
|
|
|
if(infile == NULL)
|
|
|
|
|
in = stdin;
|
|
|
|
|
else {
|
|
|
|
|
if((in = fopen(infile, "rb")) == NULL) {
|
2014-05-04 17:11:03 +02:00
|
|
|
fatal(ptx, "Could not open input file %s\n", infile);
|
2014-01-20 16:07:01 +01:00
|
|
|
goto erren2;
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(outfile == NULL)
|
|
|
|
|
out = stdout;
|
|
|
|
|
else {
|
|
|
|
|
if((out = fopen(outfile, "wb+")) == NULL) {
|
2014-05-04 17:11:03 +02:00
|
|
|
fatal(ptx, "Could not open output file %s\n", outfile);
|
2014-01-20 16:07:01 +01:00
|
|
|
goto erren2;
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-23 15:40:06 +01:00
|
|
|
size_t clen = 0;
|
2014-01-22 23:20:30 +01:00
|
|
|
|
2014-02-15 13:10:51 +01:00
|
|
|
Pcpstream *pin = ps_new_file(in);
|
|
|
|
|
Pcpstream *pout = ps_new_file(out);
|
|
|
|
|
|
2014-02-27 13:55:43 +01:00
|
|
|
if(armor == 1) {
|
|
|
|
|
ps_print(pout, PCP_ENFILE_HEADER);
|
|
|
|
|
ps_armor(pout, PCP_BLOCK_SIZE/2);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-17 15:04:07 +01:00
|
|
|
if(symmode == 1) {
|
2014-05-04 17:11:03 +02:00
|
|
|
clen = pcp_encrypt_stream_sym(ptx, pin, pout, symkey, 0, NULL);
|
2015-01-13 13:07:32 +01:00
|
|
|
sfree(symkey);
|
2014-08-08 18:40:53 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2015-12-07 14:13:27 +01:00
|
|
|
clen = pcp_encrypt_stream(ptx, pin, pout, secret, signsecret, pubhash, signcrypt, anon);
|
2014-08-08 18:40:53 +02:00
|
|
|
}
|
2014-02-15 13:10:51 +01:00
|
|
|
|
2014-02-27 13:55:43 +01:00
|
|
|
if(armor == 1) {
|
|
|
|
|
ps_finish(pout);
|
|
|
|
|
ps_unarmor(pout);
|
|
|
|
|
ps_print(pout, PCP_ENFILE_FOOTER);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-15 13:10:51 +01:00
|
|
|
ps_close(pout);
|
2014-01-19 23:57:11 +01:00
|
|
|
|
2014-02-27 13:55:43 +01:00
|
|
|
ps_close(pin);
|
|
|
|
|
|
2014-01-20 10:16:05 +01:00
|
|
|
if(clen > 0) {
|
2014-08-07 21:33:52 +02:00
|
|
|
if(id == NULL && recipient == NULL) {
|
2014-03-15 17:26:42 +01:00
|
|
|
fprintf(stderr, "Encrypted %"FMT_SIZE_T" bytes symetrically\n", (SIZE_T_CAST)clen);
|
2014-08-07 21:33:52 +02:00
|
|
|
}
|
2014-01-21 16:11:04 +01:00
|
|
|
else {
|
2014-03-15 17:26:42 +01:00
|
|
|
fprintf(stderr, "Encrypted %"FMT_SIZE_T" bytes for:\n", (SIZE_T_CAST)clen);
|
2014-08-07 21:33:52 +02:00
|
|
|
pcp_pubkey_t *cur, *t;
|
|
|
|
|
HASH_ITER(hh, pubhash, cur, t) {
|
2016-05-09 22:24:13 +02:00
|
|
|
fprintf(stderr, " 0x%s - %s <%s>\n", cur->id, cur->owner, cur->mail);
|
2014-01-21 16:11:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-01-27 16:12:43 +01:00
|
|
|
if(signcrypt)
|
|
|
|
|
fprintf(stderr, "Signed encrypted file successfully\n");
|
2014-08-07 21:33:52 +02:00
|
|
|
|
|
|
|
|
pcphash_cleanpub(pubhash);
|
2014-01-20 10:16:05 +01:00
|
|
|
return 0;
|
2014-01-19 23:57:11 +01:00
|
|
|
}
|
2013-10-28 22:50:05 +01:00
|
|
|
|
2014-01-20 16:07:01 +01:00
|
|
|
erren2:
|
2014-08-07 21:33:52 +02:00
|
|
|
pcphash_cleanpub(pubhash);
|
2014-01-20 16:07:01 +01:00
|
|
|
|
2014-08-08 18:40:53 +02:00
|
|
|
if(symkey != NULL)
|
2015-01-13 13:07:32 +01:00
|
|
|
sfree(symkey);
|
2014-08-08 18:40:53 +02:00
|
|
|
|
2013-10-28 22:50:05 +01:00
|
|
|
erren3:
|
|
|
|
|
|
2015-01-17 15:04:07 +01:00
|
|
|
if(tmp != NULL)
|
|
|
|
|
ucfree(tmp, sizeof(pcp_pubkey_t));
|
|
|
|
|
|
2013-10-28 22:50:05 +01:00
|
|
|
return 1;
|
|
|
|
|
}
|
2015-07-17 16:22:12 +02:00
|
|
|
|
2015-07-21 14:18:03 +02:00
|
|
|
void pcpchecksum(char **files, int filenum, char *key) {
|
2015-07-17 16:22:12 +02:00
|
|
|
int i;
|
|
|
|
|
byte *checksum = ucmalloc(crypto_generichash_BYTES_MAX);
|
2015-07-22 07:59:28 +02:00
|
|
|
size_t keylen = 0;
|
|
|
|
|
|
|
|
|
|
if(key != NULL)
|
|
|
|
|
keylen = strlen(key);
|
2015-07-17 16:22:12 +02:00
|
|
|
|
|
|
|
|
for(i=0; i<filenum; i++) {
|
|
|
|
|
FILE *in;
|
2015-07-17 17:27:01 +02:00
|
|
|
if(files[i] == NULL) {
|
2015-07-17 16:22:12 +02:00
|
|
|
in = stdin;
|
2015-07-17 17:27:01 +02:00
|
|
|
files[i] = "stdin";
|
|
|
|
|
}
|
2015-07-17 16:22:12 +02:00
|
|
|
else {
|
|
|
|
|
if((in = fopen(files[i], "rb")) == NULL) {
|
2016-05-09 22:24:13 +02:00
|
|
|
fatal(ptx, "Could not open input file %s\n", files[i]);
|
|
|
|
|
break;
|
2015-07-17 16:22:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Pcpstream *pin = ps_new_file(in);
|
2015-07-22 07:59:28 +02:00
|
|
|
if(pcp_checksum(ptx, pin, checksum, (byte *)key, keylen) > 0) {
|
2015-07-17 16:22:12 +02:00
|
|
|
char *hex = _bin2hex(checksum, crypto_generichash_BYTES_MAX);
|
2015-07-21 14:18:03 +02:00
|
|
|
fprintf(stdout, "BLAKE2b (%s) = %s\n", files[i], hex);
|
2015-07-17 16:22:12 +02:00
|
|
|
free(hex);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(checksum);
|
|
|
|
|
}
|