mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 03:50:57 +01:00
changed invalid // c++ comments to valid /* .. */ C comments
This commit is contained in:
@@ -61,7 +61,7 @@ int decode_85(char *dst, const char *buffer, int len)
|
||||
0xffffffff - de < (acc *= 85))
|
||||
return error("invalid base85 sequence %.5s => %08x\n", buffer-5, acc);
|
||||
acc += de;
|
||||
// say1(" %08x\n", acc);
|
||||
/* say1(" %08x\n", acc); */
|
||||
say1("%.5s", buffer-5);
|
||||
say2("=> %08x (len: %d)\n", acc, len);
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ size_t pcp_sodium_box(unsigned char **cipher,
|
||||
pad_cipher = ucmalloc(crypto_box_ZEROBYTES + clearsize);
|
||||
pcp_pad_prepend(&pad_clear, cleartext, crypto_box_ZEROBYTES, clearsize);
|
||||
|
||||
// crypto_box(c,m,mlen,n,pk,sk);
|
||||
/* crypto_box(c,m,mlen,n,pk,sk); */
|
||||
crypto_box(pad_cipher, pad_clear,
|
||||
clearsize + crypto_box_ZEROBYTES, nonce, pub, secret);
|
||||
|
||||
@@ -55,7 +55,7 @@ size_t pcp_sodium_box(unsigned char **cipher,
|
||||
int pcp_sodium_verify_box(unsigned char **cleartext, unsigned char* message,
|
||||
size_t messagesize, unsigned char *nonce,
|
||||
unsigned char *secret, unsigned char *pub) {
|
||||
// verify/decrypt the box
|
||||
/* verify/decrypt the box */
|
||||
unsigned char *pad_cipher;
|
||||
unsigned char *pad_clear;
|
||||
int success = -1;
|
||||
@@ -63,7 +63,7 @@ int pcp_sodium_verify_box(unsigned char **cleartext, unsigned char* message,
|
||||
pcp_pad_prepend(&pad_cipher, message, crypto_box_BOXZEROBYTES, messagesize);
|
||||
pad_clear = (unsigned char *)ucmalloc((crypto_box_ZEROBYTES+ messagesize));
|
||||
|
||||
// crypto_box_open(m,c,clen,n,pk,sk);
|
||||
/* crypto_box_open(m,c,clen,n,pk,sk); */
|
||||
if (crypto_box_open(pad_clear, pad_cipher,
|
||||
messagesize + crypto_box_BOXZEROBYTES,
|
||||
nonce, pub, secret) == 0) {
|
||||
@@ -97,13 +97,13 @@ unsigned char *pcp_box_encrypt(pcp_key_t *secret, pcp_pubkey_t *pub,
|
||||
goto errbec;
|
||||
}
|
||||
|
||||
// scip
|
||||
//fprintf(stderr, "public: "); pcpprint_bin(stderr, pub->pub, 32); fprintf(stderr, "\n");
|
||||
//fprintf(stderr, "secret: "); pcpprint_bin(stderr, secret->secret, 32); fprintf(stderr, "\n");
|
||||
//fprintf(stderr, "cipher: "); pcpprint_bin(stderr, cipher, es); fprintf(stderr, "\n");
|
||||
//fprintf(stderr, " nonce: "); pcpprint_bin(stderr, nonce, crypto_secretbox_NONCEBYTES); fprintf(stderr, "\n");
|
||||
/* scip */
|
||||
/* fprintf(stderr, "public: "); pcpprint_bin(stderr, pub->pub, 32); fprintf(stderr, "\n"); */
|
||||
/* fprintf(stderr, "secret: "); pcpprint_bin(stderr, secret->secret, 32); fprintf(stderr, "\n"); */
|
||||
/* fprintf(stderr, "cipher: "); pcpprint_bin(stderr, cipher, es); fprintf(stderr, "\n"); */
|
||||
/* fprintf(stderr, " nonce: "); pcpprint_bin(stderr, nonce, crypto_secretbox_NONCEBYTES); fprintf(stderr, "\n"); */
|
||||
|
||||
// put nonce and cipher together
|
||||
/* put nonce and cipher together */
|
||||
unsigned char *combined = ucmalloc(es + crypto_secretbox_NONCEBYTES);
|
||||
memcpy(combined, nonce, crypto_secretbox_NONCEBYTES);
|
||||
memcpy(&combined[crypto_secretbox_NONCEBYTES], cipher, es);
|
||||
@@ -147,8 +147,8 @@ unsigned char *pcp_box_decrypt(pcp_key_t *secret, pcp_pubkey_t *pub,
|
||||
free(nonce);
|
||||
free(cipheronly);
|
||||
|
||||
// resulting size:
|
||||
// ciphersize - crypto_secretbox_ZEROBYTES
|
||||
/* resulting size: */
|
||||
/* ciphersize - crypto_secretbox_ZEROBYTES */
|
||||
*dsize = ciphersize - crypto_secretbox_NONCEBYTES - PCP_CRYPTO_ADD;
|
||||
return message;
|
||||
|
||||
@@ -179,12 +179,12 @@ size_t pcp_decrypt_file(FILE *in, FILE* out, pcp_key_t *s, unsigned char *symkey
|
||||
int self = 0;
|
||||
|
||||
if(ftell(in) == 1) {
|
||||
// header has already been determined outside the lib
|
||||
/* header has already been determined outside the lib */
|
||||
if(symkey != NULL)
|
||||
self = 1;
|
||||
}
|
||||
else {
|
||||
// step 1, check header
|
||||
/* step 1, check header */
|
||||
cur_bufsize = fread(head, 1, 1, in);
|
||||
if(cur_bufsize != 1 && !feof(in) && !ferror(in)) {
|
||||
if(head[0] == PCP_SYM_CIPHER) {
|
||||
@@ -202,12 +202,12 @@ size_t pcp_decrypt_file(FILE *in, FILE* out, pcp_key_t *s, unsigned char *symkey
|
||||
}
|
||||
|
||||
if(self) {
|
||||
// just decrypt symetrically and go outa here
|
||||
/* just decrypt symetrically and go outa here */
|
||||
return pcp_decrypt_file_sym(in, out, symkey, NULL);
|
||||
}
|
||||
|
||||
#ifdef PCP_ASYM_ADD_SENDER_PUB
|
||||
// step 2, sender's pubkey
|
||||
/* step 2, sender's pubkey */
|
||||
cur_bufsize = fread(&in_buf, 1, crypto_box_PUBLICKEYBYTES, in);
|
||||
if(cur_bufsize != crypto_box_PUBLICKEYBYTES && !feof(in) && !ferror(in)) {
|
||||
fatal("Error: input file doesn't contain senders public key\n");
|
||||
@@ -215,7 +215,7 @@ size_t pcp_decrypt_file(FILE *in, FILE* out, pcp_key_t *s, unsigned char *symkey
|
||||
}
|
||||
#endif
|
||||
|
||||
// step 3, check len recipients
|
||||
/* step 3, check len recipients */
|
||||
cur_bufsize = fread(&lenrec, 1, 4, in);
|
||||
if(cur_bufsize != 4 && !feof(in) && !ferror(in)) {
|
||||
fatal("Error: input file doesn't contain recipient count\n");
|
||||
@@ -227,7 +227,7 @@ size_t pcp_decrypt_file(FILE *in, FILE* out, pcp_key_t *s, unsigned char *symkey
|
||||
reccipher = ucmalloc(lenrec * PCP_ASYM_RECIPIENT_SIZE);
|
||||
}
|
||||
|
||||
// step 4, fetch recipient list and try to decrypt it for us
|
||||
/* step 4, fetch recipient list and try to decrypt it for us */
|
||||
for(nrec=0; nrec<lenrec; nrec++) {
|
||||
cur_bufsize = fread(&rec_buf, 1, PCP_ASYM_RECIPIENT_SIZE, in);
|
||||
if(cur_bufsize != PCP_ASYM_RECIPIENT_SIZE && !feof(in) && !ferror(in)) {
|
||||
@@ -239,7 +239,7 @@ size_t pcp_decrypt_file(FILE *in, FILE* out, pcp_key_t *s, unsigned char *symkey
|
||||
unsigned char *recipient;
|
||||
recipient = pcp_box_decrypt(s, cur, rec_buf, PCP_ASYM_RECIPIENT_SIZE, &rec_size);
|
||||
if(recipient != NULL && rec_size == crypto_secretbox_KEYBYTES) {
|
||||
// found a match
|
||||
/* found a match */
|
||||
recmatch = 1;
|
||||
sender = cur;
|
||||
symkey = ucmalloc(crypto_secretbox_KEYBYTES);
|
||||
@@ -259,7 +259,7 @@ size_t pcp_decrypt_file(FILE *in, FILE* out, pcp_key_t *s, unsigned char *symkey
|
||||
}
|
||||
|
||||
|
||||
// step 5, actually decrypt the file, finally
|
||||
/* step 5, actually decrypt the file, finally */
|
||||
if(verify) {
|
||||
pcp_rec_t *rec = pcp_rec_new(reccipher, nrec * PCP_ASYM_RECIPIENT_SIZE, NULL, cur);
|
||||
return pcp_decrypt_file_sym(in, out, symkey, rec);
|
||||
@@ -290,11 +290,11 @@ size_t pcp_encrypt_file(FILE *in, FILE* out, pcp_key_t *s, pcp_pubkey_t *p, int
|
||||
random_nonce|box(temp_keypair.privkey, recipient crypto pk, random_nonce, packet key)
|
||||
*/
|
||||
|
||||
// preparation
|
||||
// A, generate sym key
|
||||
/* preparation */
|
||||
/* A, generate sym key */
|
||||
symkey = urmalloc(crypto_secretbox_KEYBYTES);
|
||||
|
||||
// B, encrypt it asymetrically for each recipient
|
||||
/* B, encrypt it asymetrically for each recipient */
|
||||
recipient_count = HASH_COUNT(p);
|
||||
rec_size = PCP_ASYM_RECIPIENT_SIZE;
|
||||
recipients_cipher = ucmalloc(rec_size * recipient_count);
|
||||
@@ -308,45 +308,45 @@ size_t pcp_encrypt_file(FILE *in, FILE* out, pcp_key_t *s, pcp_pubkey_t *p, int
|
||||
free(rec_cipher);
|
||||
goto errec1;
|
||||
}
|
||||
memcpy(&recipients_cipher[nrec * rec_size], rec_cipher, rec_size); // already includes the nonce
|
||||
memcpy(&recipients_cipher[nrec * rec_size], rec_cipher, rec_size); /* already includes the nonce */
|
||||
nrec++;
|
||||
free(rec_cipher);
|
||||
}
|
||||
|
||||
// step 1, file header
|
||||
/* step 1, file header */
|
||||
head[0] = PCP_ASYM_CIPHER;
|
||||
fwrite(head, 1, 1, out);
|
||||
//fprintf(stderr, "D: header - 1\n");
|
||||
/* fprintf(stderr, "D: header - 1\n"); */
|
||||
if(ferror(out) != 0) {
|
||||
fatal("Failed to write encrypted output!\n");
|
||||
goto errec1;
|
||||
}
|
||||
|
||||
#ifdef PCP_ASYM_ADD_SENDER_PUB
|
||||
// step 2, sender's pubkey
|
||||
/* step 2, sender's pubkey */
|
||||
fwrite(s->pub, crypto_box_PUBLICKEYBYTES, 1, out);
|
||||
//fprintf(stderr, "D: sender pub - %d\n", crypto_box_PUBLICKEYBYTES);
|
||||
/* fprintf(stderr, "D: sender pub - %d\n", crypto_box_PUBLICKEYBYTES); */
|
||||
if(ferror(out) != 0)
|
||||
goto errec1;
|
||||
#endif
|
||||
|
||||
// step 3, len recipients, big endian
|
||||
/* step 3, len recipients, big endian */
|
||||
lenrec = recipient_count;
|
||||
lenrec = htobe32(lenrec);
|
||||
fwrite(&lenrec, 4, 1, out);
|
||||
//fprintf(stderr, "D: %d recipients - 4\n", recipient_count);
|
||||
/* fprintf(stderr, "D: %d recipients - 4\n", recipient_count); */
|
||||
if(ferror(out) != 0)
|
||||
goto errec1;
|
||||
|
||||
// step 4, recipient list
|
||||
/* step 4, recipient list */
|
||||
fwrite(recipients_cipher, rec_size * recipient_count, 1, out);
|
||||
//fprintf(stderr, "D: recipients - %ld * %d\n", rec_size, recipient_count);
|
||||
/* fprintf(stderr, "D: recipients - %ld * %d\n", rec_size, recipient_count); */
|
||||
if(ferror(out) != 0)
|
||||
goto errec1;
|
||||
|
||||
out_size = 5 + (rec_size * recipient_count) + crypto_box_PUBLICKEYBYTES;
|
||||
|
||||
// step 5, actual encrypted data
|
||||
/* step 5, actual encrypted data */
|
||||
size_t sym_size = 0;
|
||||
if(sign) {
|
||||
pcp_rec_t *rec = pcp_rec_new(recipients_cipher, rec_size * recipient_count, s, NULL);
|
||||
@@ -409,16 +409,16 @@ size_t pcp_encrypt_file_sym(FILE *in, FILE* out, unsigned char *symkey, int have
|
||||
}
|
||||
|
||||
#ifdef PCP_CBC
|
||||
// write the IV, pad it with rubbish, since pcp_decrypt_file_sym
|
||||
// reads in with PCP_BLOCK_SIZE_IN buffersize and uses the last
|
||||
// PCP_BLOCK_SIZE as IV.
|
||||
/* write the IV, pad it with rubbish, since pcp_decrypt_file_sym */
|
||||
/* reads in with PCP_BLOCK_SIZE_IN buffersize and uses the last */
|
||||
/* PCP_BLOCK_SIZE as IV. */
|
||||
unsigned char *iv = urmalloc(PCP_BLOCK_SIZE);
|
||||
unsigned char *ivpad = urmalloc(PCP_BLOCK_SIZE_IN - PCP_BLOCK_SIZE);
|
||||
fwrite(ivpad, PCP_BLOCK_SIZE_IN - PCP_BLOCK_SIZE, 1, out);
|
||||
fwrite(iv, PCP_BLOCK_SIZE, 1, out);
|
||||
#endif
|
||||
|
||||
// 32k-ECB-mode. FIXME: maybe support CBC as well or only use CBC?
|
||||
/* 32k-ECB-mode. FIXME: maybe support CBC as well or only use CBC? */
|
||||
while(!feof(in)) {
|
||||
cur_bufsize = fread(&in_buf, 1, PCP_BLOCK_SIZE, in);
|
||||
if(cur_bufsize <= 0)
|
||||
@@ -426,7 +426,7 @@ size_t pcp_encrypt_file_sym(FILE *in, FILE* out, unsigned char *symkey, int have
|
||||
buf_nonce = pcp_gennonce();
|
||||
|
||||
#ifdef PCP_CBC
|
||||
// apply IV to current clear
|
||||
/* apply IV to current clear */
|
||||
_xorbuf(iv, in_buf, cur_bufsize);
|
||||
#endif
|
||||
|
||||
@@ -442,7 +442,7 @@ size_t pcp_encrypt_file_sym(FILE *in, FILE* out, unsigned char *symkey, int have
|
||||
crypto_generichash_update(st, in_buf, cur_bufsize);
|
||||
|
||||
#ifdef PCP_CBC
|
||||
// make current cipher to next IV, ignore nonce and pad
|
||||
/* make current cipher to next IV, ignore nonce and pad */
|
||||
memcpy(iv, &buf_cipher[PCP_CRYPTO_ADD], PCP_BLOCK_SIZE);
|
||||
#endif
|
||||
}
|
||||
@@ -514,17 +514,17 @@ size_t pcp_decrypt_file_sym(FILE *in, FILE* out, unsigned char *symkey, pcp_rec_
|
||||
}
|
||||
|
||||
#ifdef PCP_CBC
|
||||
unsigned char *iv = NULL; // will be filled during 1st loop
|
||||
unsigned char *iv = NULL; /* will be filled during 1st loop */
|
||||
#endif
|
||||
|
||||
while(!feof(in)) {
|
||||
cur_bufsize = fread(&in_buf, 1, PCP_BLOCK_SIZE_IN, in);
|
||||
if(cur_bufsize <= PCP_CRYPTO_ADD)
|
||||
break; // no valid cipher block
|
||||
break; /* no valid cipher block */
|
||||
|
||||
if(recverify != NULL) {
|
||||
if(cur_bufsize < PCP_BLOCK_SIZE_IN || feof(in)) {
|
||||
// pull out signature
|
||||
/* pull out signature */
|
||||
memcpy(signature_cr, &in_buf[cur_bufsize - siglen_cr], siglen_cr);
|
||||
cur_bufsize -= siglen_cr;
|
||||
}
|
||||
@@ -532,7 +532,7 @@ size_t pcp_decrypt_file_sym(FILE *in, FILE* out, unsigned char *symkey, pcp_rec_
|
||||
|
||||
#ifdef PCP_CBC
|
||||
if(iv == NULL) {
|
||||
// first block is the IV, don't write it out and skip to the next block
|
||||
/* first block is the IV, don't write it out and skip to the next block */
|
||||
iv = ucmalloc(PCP_BLOCK_SIZE);
|
||||
memcpy(iv, &in_buf[PCP_CRYPTO_ADD + crypto_secretbox_NONCEBYTES], PCP_BLOCK_SIZE);
|
||||
continue;
|
||||
@@ -546,7 +546,7 @@ size_t pcp_decrypt_file_sym(FILE *in, FILE* out, unsigned char *symkey, pcp_rec_
|
||||
es = pcp_sodium_verify_mac(&buf_clear, buf_cipher, ciphersize, buf_nonce, symkey);
|
||||
|
||||
#ifdef PCP_CBC
|
||||
// take last IV and apply it to current clear
|
||||
/* take last IV and apply it to current clear */
|
||||
_xorbuf(iv, buf_clear, cur_bufsize - (PCP_CRYPTO_ADD + crypto_secretbox_NONCEBYTES));
|
||||
#endif
|
||||
|
||||
@@ -573,7 +573,7 @@ size_t pcp_decrypt_file_sym(FILE *in, FILE* out, unsigned char *symkey, pcp_rec_
|
||||
break;
|
||||
}
|
||||
#ifdef PCP_CBC
|
||||
// use last cipher as next IV
|
||||
/* use last cipher as next IV */
|
||||
memcpy(iv, &in_buf[PCP_CRYPTO_ADD + crypto_secretbox_NONCEBYTES], PCP_BLOCK_SIZE);
|
||||
#endif
|
||||
}
|
||||
@@ -597,7 +597,7 @@ size_t pcp_decrypt_file_sym(FILE *in, FILE* out, unsigned char *symkey, pcp_rec_
|
||||
out_size = 0;
|
||||
else {
|
||||
if(memcmp(verifiedhash, hash, crypto_generichash_BYTES_MAX) != 0) {
|
||||
// sig verified, but the hash doesn't match
|
||||
/* sig verified, but the hash doesn't match */
|
||||
fatal("signed hash doesn't match actual hash of signed decrypted file content\n");
|
||||
out_size = 0;
|
||||
}
|
||||
|
||||
@@ -30,8 +30,8 @@
|
||||
#include <digital_crc32.h>
|
||||
|
||||
|
||||
// Automatically generated CRC function
|
||||
// polynomial: 0x104C11DB7
|
||||
/* Automatically generated CRC function */
|
||||
/* polynomial: 0x104C11DB7 */
|
||||
unsigned int
|
||||
digital_update_crc32(unsigned int crc, const unsigned char *data, size_t len)
|
||||
{
|
||||
|
||||
62
libpcp/ed.c
62
libpcp/ed.c
@@ -23,7 +23,7 @@
|
||||
|
||||
unsigned char * pcp_ed_verify(unsigned char *signature, size_t siglen, pcp_pubkey_t *p) {
|
||||
unsigned char *message = ucmalloc(siglen - crypto_sign_BYTES);
|
||||
size_t mlen;
|
||||
unsigned long long mlen;
|
||||
|
||||
if(crypto_sign_open(message, &mlen, signature, siglen, p->edpub) != 0) {
|
||||
fatal("Failed to open the signature using the public key 0x%s!\n", p->id);
|
||||
@@ -38,7 +38,7 @@ unsigned char * pcp_ed_verify(unsigned char *signature, size_t siglen, pcp_pubke
|
||||
}
|
||||
|
||||
unsigned char *pcp_ed_sign(unsigned char *message, size_t messagesize, pcp_key_t *s) {
|
||||
size_t mlen = messagesize + crypto_sign_BYTES;
|
||||
unsigned long long mlen = messagesize + crypto_sign_BYTES;
|
||||
unsigned char *signature = ucmalloc(mlen);
|
||||
|
||||
crypto_sign(signature, &mlen, message, messagesize, s->edsecret);
|
||||
@@ -114,9 +114,9 @@ pcp_pubkey_t *pcp_ed_verify_buffered(FILE *in, pcp_pubkey_t *p) {
|
||||
unsigned char hash[crypto_generichash_BYTES_MAX];
|
||||
char zhead[] = PCP_SIG_HEADER;
|
||||
size_t hlen = strlen(PCP_SIG_HEADER);
|
||||
size_t hlen2 = 15; // hash: blake2\n\n
|
||||
size_t hlen2 = 15; /* hash: blake2\n\n */
|
||||
size_t mlen = + crypto_sign_BYTES + crypto_generichash_BYTES_MAX;
|
||||
size_t zlen = 262; // FIXME: calculate
|
||||
size_t zlen = 262; /* FIXME: calculate */
|
||||
unsigned char z85encoded[zlen];
|
||||
unsigned char sighash[mlen];
|
||||
char z85sigstart[] = PCP_SIG_START;
|
||||
@@ -131,20 +131,20 @@ pcp_pubkey_t *pcp_ed_verify_buffered(FILE *in, pcp_pubkey_t *p) {
|
||||
/* use two half blocks, to overcome sigs spanning block boundaries */
|
||||
cur_bufsize = fread(&in_buf, 1, PCP_BLOCK_SIZE/2, in);
|
||||
|
||||
// look for z85 header and cut it out
|
||||
/* look for z85 header and cut it out */
|
||||
if(_findoffset(in_buf, cur_bufsize, zhead, hlen) == 0) {
|
||||
// it is armored
|
||||
next_bufsize = cur_bufsize - (hlen+hlen2); // size - the header
|
||||
memcpy(in_next, &in_buf[hlen+hlen2], next_bufsize); // tmp save
|
||||
memcpy(in_buf, in_next, next_bufsize); // put into inbuf without header
|
||||
/* it is armored */
|
||||
next_bufsize = cur_bufsize - (hlen+hlen2); /* size - the header */
|
||||
memcpy(in_next, &in_buf[hlen+hlen2], next_bufsize); /* tmp save */
|
||||
memcpy(in_buf, in_next, next_bufsize); /* put into inbuf without header */
|
||||
if(cur_bufsize == PCP_BLOCK_SIZE/2) {
|
||||
// more to come
|
||||
/* more to come */
|
||||
cur_bufsize = fread(&in_buf[next_bufsize], 1, ((PCP_BLOCK_SIZE/2) - next_bufsize), in);
|
||||
cur_bufsize += next_bufsize;
|
||||
next_bufsize = 0;
|
||||
// now we've got the 1st half block in in_buf
|
||||
// unless the file was smaller than blocksize/2,
|
||||
// in which case it contains all the rest til eof
|
||||
/* now we've got the 1st half block in in_buf */
|
||||
/* unless the file was smaller than blocksize/2, */
|
||||
/* in which case it contains all the rest til eof */
|
||||
}
|
||||
z85 = 1;
|
||||
}
|
||||
@@ -163,13 +163,13 @@ pcp_pubkey_t *pcp_ed_verify_buffered(FILE *in, pcp_pubkey_t *p) {
|
||||
|
||||
while (cur_bufsize > 0) {
|
||||
if(cur_bufsize == PCP_BLOCK_SIZE/2) {
|
||||
// probably not eof
|
||||
/* probably not eof */
|
||||
next_bufsize = fread(&in_next, 1, PCP_BLOCK_SIZE/2, in);
|
||||
}
|
||||
else
|
||||
next_bufsize = 0; // <= this is eof
|
||||
next_bufsize = 0; /* <= this is eof */
|
||||
|
||||
// concatenate previous and current buffer
|
||||
/* concatenate previous and current buffer */
|
||||
if(next_bufsize == 0)
|
||||
memcpy(in_full, in_buf, cur_bufsize);
|
||||
else {
|
||||
@@ -178,14 +178,14 @@ pcp_pubkey_t *pcp_ed_verify_buffered(FILE *in, pcp_pubkey_t *p) {
|
||||
}
|
||||
full_bufsize = cur_bufsize+next_bufsize;
|
||||
|
||||
// find signature offset
|
||||
/* find signature offset */
|
||||
offset = _findoffset(in_full, full_bufsize, sigstart, startlen);
|
||||
|
||||
//printf("offset: %ld, full: %ld, cur: %ld\n", offset, full_bufsize, cur_bufsize);
|
||||
/* printf("offset: %ld, full: %ld, cur: %ld\n", offset, full_bufsize, cur_bufsize); */
|
||||
|
||||
if(offset >= 0 && offset <= PCP_BLOCK_SIZE/2) {
|
||||
// sig begins within the first half, adjust in_buf size
|
||||
//printf("1st half\n");
|
||||
/* sig begins within the first half, adjust in_buf size */
|
||||
/* printf("1st half\n"); */
|
||||
next_bufsize = 0;
|
||||
cur_bufsize = offset;
|
||||
gotsig = 1;
|
||||
@@ -197,9 +197,9 @@ pcp_pubkey_t *pcp_ed_verify_buffered(FILE *in, pcp_pubkey_t *p) {
|
||||
memcpy(sighash, &in_full[offset + strlen(binsigstart)], mlen);
|
||||
}
|
||||
else if(full_bufsize - offset == siglen) {
|
||||
// sig fits within the 2nd half
|
||||
// offset: 28279, full: 28413, cur: 16384
|
||||
//printf("2nd half\n");
|
||||
/* sig fits within the 2nd half */
|
||||
/* offset: 28279, full: 28413, cur: 16384 */
|
||||
/* printf("2nd half\n"); */
|
||||
next_bufsize -= siglen;
|
||||
gotsig = 1;
|
||||
if(z85) {
|
||||
@@ -212,17 +212,17 @@ pcp_pubkey_t *pcp_ed_verify_buffered(FILE *in, pcp_pubkey_t *p) {
|
||||
else
|
||||
offset = 0;
|
||||
|
||||
// add previous half block to hash
|
||||
/* add previous half block to hash */
|
||||
crypto_generichash_update(st, in_buf, cur_bufsize);
|
||||
|
||||
// next => in
|
||||
/* next => in */
|
||||
if(next_bufsize > 0) {
|
||||
memcpy(in_buf, in_next, next_bufsize);
|
||||
cur_bufsize = next_bufsize;
|
||||
}
|
||||
else
|
||||
break;
|
||||
} // while
|
||||
} /* while */
|
||||
|
||||
if(gotsig == 0) {
|
||||
fatal("Error, the signature doesn't contain the ed25519 signed hash\n");
|
||||
@@ -244,9 +244,9 @@ pcp_pubkey_t *pcp_ed_verify_buffered(FILE *in, pcp_pubkey_t *p) {
|
||||
}
|
||||
memcpy(sighash, z85decoded, mlen);
|
||||
}
|
||||
// else: if unarmored, sighash is already filled
|
||||
/* else: if unarmored, sighash is already filled */
|
||||
|
||||
// huh, how did we made it til here?
|
||||
/* huh, how did we made it til here? */
|
||||
unsigned char *verifiedhash = NULL;
|
||||
if(p == NULL) {
|
||||
pcphash_iteratepub(p) {
|
||||
@@ -263,7 +263,7 @@ pcp_pubkey_t *pcp_ed_verify_buffered(FILE *in, pcp_pubkey_t *p) {
|
||||
goto errvb1;
|
||||
|
||||
if(memcmp(verifiedhash, hash, crypto_generichash_BYTES_MAX) != 0) {
|
||||
// sig verified, but the hash doesn't
|
||||
/* sig verified, but the hash doesn't */
|
||||
fatal("signed hash doesn't match actual hash of signed file content\n");
|
||||
free(verifiedhash);
|
||||
return NULL;
|
||||
@@ -335,7 +335,7 @@ pcp_pubkey_t *pcp_ed_detachverify_buffered(FILE *in, FILE *sigfd, pcp_pubkey_t *
|
||||
|
||||
crypto_generichash_final(st, hash, crypto_generichash_BYTES_MAX);
|
||||
|
||||
// read the sig
|
||||
/* read the sig */
|
||||
unsigned char *sig = NULL;
|
||||
size_t inputBufSize = 0;
|
||||
unsigned char byte[1];
|
||||
@@ -386,7 +386,7 @@ pcp_pubkey_t *pcp_ed_detachverify_buffered(FILE *in, FILE *sigfd, pcp_pubkey_t *
|
||||
goto errdea4;
|
||||
|
||||
if(memcmp(verifiedhash, hash, crypto_generichash_BYTES_MAX) != 0) {
|
||||
// sig verified, but the hash doesn't
|
||||
/* sig verified, but the hash doesn't */
|
||||
fatal("signed hash doesn't match actual hash of signed file content\n");
|
||||
goto errdea5;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ char *pcp_get_stdin() {
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
line[linelen - 1] = '\0'; // remove newline at end
|
||||
line[linelen - 1] = '\0'; /* remove newline at end */
|
||||
return line;
|
||||
}
|
||||
}
|
||||
|
||||
63
libpcp/key.c
63
libpcp/key.c
@@ -32,13 +32,13 @@ unsigned char *pcp_derivekey(char *passphrase, unsigned char *nonce) {
|
||||
unsigned char *key = ucmalloc(crypto_secretbox_KEYBYTES);
|
||||
size_t plen = strnlen(passphrase, 255);
|
||||
|
||||
// create the scrypt hash
|
||||
/* create the scrypt hash */
|
||||
unsigned char *scrypted = pcp_scrypt(passphrase, plen, nonce, crypto_secretbox_NONCEBYTES);
|
||||
|
||||
// make a hash from the scrypt() result
|
||||
/* make a hash from the scrypt() result */
|
||||
crypto_hash_sha256(key, (unsigned char*)scrypted, 64);
|
||||
|
||||
// turn the 32byte hash into a secret key
|
||||
/* turn the 32byte hash into a secret key */
|
||||
key[0] &= 248;
|
||||
key[31] &= 127;
|
||||
key[31] |= 64;
|
||||
@@ -48,41 +48,6 @@ unsigned char *pcp_derivekey(char *passphrase, unsigned char *nonce) {
|
||||
return key;
|
||||
}
|
||||
|
||||
/*
|
||||
* deprecated
|
||||
unsigned char *pcp_derivekey(char *passphrase) {
|
||||
unsigned char *hash32 = ucmalloc(crypto_hash_sha256_BYTES);
|
||||
unsigned char *key = ucmalloc(crypto_secretbox_KEYBYTES);
|
||||
|
||||
size_t plen = strnlen(passphrase, 255);
|
||||
unsigned char *temp = ucmalloc(crypto_hash_sha256_BYTES);
|
||||
int i;
|
||||
|
||||
// make a hash from the passphrase and then HCYCLES times from the result
|
||||
crypto_hash_sha256(temp, (unsigned char*)passphrase, plen);
|
||||
|
||||
for(i=0; i<HCYCLES; ++i) {
|
||||
if(crypto_hash_sha256(hash32, temp, crypto_hash_sha256_BYTES) == 0) {
|
||||
memcpy(temp, hash32, crypto_hash_sha256_BYTES);
|
||||
}
|
||||
}
|
||||
|
||||
// turn the 32byte hash into a secret key
|
||||
temp[0] &= 248;
|
||||
temp[31] &= 127;
|
||||
temp[31] |= 64;
|
||||
|
||||
memcpy(key, temp, crypto_secretbox_KEYBYTES);
|
||||
|
||||
memset(passphrase, 0, plen);
|
||||
memset(temp, 0, crypto_hash_sha256_BYTES);
|
||||
|
||||
free(temp);
|
||||
free(hash32);
|
||||
|
||||
return key;
|
||||
}
|
||||
*/
|
||||
|
||||
char *pcp_getkeyid(pcp_key_t *k) {
|
||||
uint32_t s, p;
|
||||
@@ -93,7 +58,7 @@ char *pcp_getkeyid(pcp_key_t *k) {
|
||||
return id;
|
||||
}
|
||||
|
||||
// same as above but for imported pbp keys
|
||||
/* same as above but for imported pbp keys */
|
||||
char *pcp_getpubkeyid(pcp_pubkey_t *k) {
|
||||
uint32_t s, p;
|
||||
p = jen_hash(k->pub, 32, JEN_PSALT);
|
||||
@@ -104,20 +69,20 @@ char *pcp_getpubkeyid(pcp_pubkey_t *k) {
|
||||
}
|
||||
|
||||
void pcp_keypairs(byte *csk, byte *cpk, byte *esk, byte *epk) {
|
||||
// generate ed25519 + curve25519 keypair from random seed
|
||||
/* generate ed25519 + curve25519 keypair from random seed */
|
||||
byte *seed = urmalloc(32);
|
||||
byte *tmp = urmalloc(32);
|
||||
|
||||
// ed25519 signing key
|
||||
/* ed25519 signing key */
|
||||
crypto_sign_seed_keypair(epk, esk, seed);
|
||||
|
||||
// curve25519 secret key
|
||||
/* curve25519 secret key */
|
||||
tmp[0] &= 248;
|
||||
tmp[31] &= 63;
|
||||
tmp[31] |= 64;
|
||||
memcpy(csk, tmp, 32);
|
||||
|
||||
// curve25519 public key
|
||||
/* curve25519 public key */
|
||||
crypto_scalarmult_curve25519_base(cpk, csk);
|
||||
memset(tmp, 0, 32);
|
||||
}
|
||||
@@ -130,7 +95,7 @@ pcp_key_t * pcpkey_new () {
|
||||
|
||||
pcp_keypairs(secret, pub, edsec, edpub);
|
||||
|
||||
// fill in our struct
|
||||
/* fill in our struct */
|
||||
pcp_key_t *key = urmalloc(sizeof(pcp_key_t));
|
||||
memcpy (key->pub, pub, 32);
|
||||
memcpy (key->secret, secret, 32);
|
||||
@@ -175,7 +140,7 @@ pcp_key_t *pcpkey_encrypt(pcp_key_t *key, char *passphrase) {
|
||||
free(both);
|
||||
|
||||
if(es == 112) {
|
||||
// success
|
||||
/* success */
|
||||
memcpy(key->encrypted, encrypted, 112);
|
||||
arc4random_buf(key->secret, 32);
|
||||
arc4random_buf(key->edsecret, 64);
|
||||
@@ -203,7 +168,7 @@ pcp_key_t *pcpkey_decrypt(pcp_key_t *key, char *passphrase) {
|
||||
free(encryptkey);
|
||||
|
||||
if(es == 0) {
|
||||
// success
|
||||
/* success */
|
||||
memcpy(key->edsecret, decrypted, 64);
|
||||
memcpy(key->secret, &decrypted[64], 32);
|
||||
}
|
||||
@@ -217,7 +182,7 @@ pcp_key_t *pcpkey_decrypt(pcp_key_t *key, char *passphrase) {
|
||||
}
|
||||
|
||||
pcp_pubkey_t *pcpkey_pub_from_secret(pcp_key_t *key) {
|
||||
//pcp_dumpkey(key);
|
||||
/* pcp_dumpkey(key); */
|
||||
pcp_pubkey_t *pub = urmalloc(sizeof (pcp_pubkey_t));
|
||||
memcpy(pub->pub, key->pub, 32);
|
||||
memcpy(pub->edpub, key->edpub, 32);
|
||||
@@ -365,7 +330,7 @@ int pcp_sanitycheck_pub(pcp_pubkey_t *key) {
|
||||
time_t t = (time_t)key->ctime;
|
||||
c = localtime(&t);
|
||||
if(c->tm_year <= 0 || c->tm_year > 1100) {
|
||||
// well, I'm perhaps overacting here :)
|
||||
/* well, I'm perhaps overacting here :) */
|
||||
fatal("Pubkey sanity check: invalid creation timestamp (got year %04d)!\n", c->tm_year + 1900);
|
||||
return 1;
|
||||
}
|
||||
@@ -416,7 +381,7 @@ int pcp_sanitycheck_key(pcp_key_t *key) {
|
||||
time_t t = (time_t)key->ctime;
|
||||
c = localtime(&t);
|
||||
if(c->tm_year <= 0 || c->tm_year > 1100) {
|
||||
// well, I'm perhaps overacting here :)
|
||||
/* well, I'm perhaps overacting here :) */
|
||||
fatal("Secretkey sanity check: invalid creation timestamp (got year %04d)!\n", c->tm_year + 1900);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -65,13 +65,13 @@ void pcphash_clean() {
|
||||
pcp_key_t *pcphash_keyexists(char *id) {
|
||||
pcp_key_t *key = NULL;
|
||||
HASH_FIND_STR(pcpkey_hash, id, key);
|
||||
return key; // maybe NULL!
|
||||
return key; /* maybe NULL! */
|
||||
}
|
||||
|
||||
pcp_pubkey_t *pcphash_pubkeyexists(char *id) {
|
||||
pcp_pubkey_t *key = NULL;
|
||||
HASH_FIND_STR(pcppubkey_hash, id, key);
|
||||
return key; // maybe NULL!
|
||||
return key; /* maybe NULL! */
|
||||
}
|
||||
|
||||
void pcphash_add(void *key, int type) {
|
||||
|
||||
@@ -51,7 +51,7 @@ size_t pcp_sodium_mac(unsigned char **cipher,
|
||||
int pcp_sodium_verify_mac(unsigned char **cleartext, unsigned char* message,
|
||||
size_t messagesize, unsigned char *nonce,
|
||||
unsigned char *key) {
|
||||
// verify the mac
|
||||
/* verify the mac */
|
||||
unsigned char *pad_cipher;
|
||||
unsigned char *pad_clear;
|
||||
int success = -1;
|
||||
|
||||
@@ -38,7 +38,7 @@ void *ucmalloc(size_t s) {
|
||||
|
||||
memset (value, 0, size);
|
||||
|
||||
//printf("allocated %d bytes at %p\n", (int)size, value);
|
||||
/* printf("allocated %d bytes at %p\n", (int)size, value); */
|
||||
|
||||
return value;
|
||||
}
|
||||
@@ -52,7 +52,3 @@ void *urmalloc(size_t s) {
|
||||
}
|
||||
|
||||
|
||||
void *ucfree(void *ptr) {
|
||||
free(ptr);
|
||||
ptr = NULL;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ void pcp_pad_prepend(unsigned char **padded, unsigned char *unpadded,
|
||||
*padded = ucmalloc(unpadlen + padlen);
|
||||
unsigned char *tmp = ucmalloc(unpadlen + padlen);
|
||||
|
||||
// pcp_append orig
|
||||
/* pcp_append orig */
|
||||
int i;
|
||||
for(i=0; i<unpadlen; ++i) {
|
||||
tmp[i + padlen] = unpadded[i];
|
||||
@@ -60,15 +60,15 @@ int main(int argc, char **argv) {
|
||||
unsigned char *dst;
|
||||
|
||||
pcp_pad_prepend(&dst, argv[1], padlen, unpadlen);
|
||||
//printf(" prev: %s\n after: %s\n", argv[1], dst);
|
||||
/* printf(" prev: %s\n after: %s\n", argv[1], dst); */
|
||||
|
||||
unsigned char *reverse;
|
||||
pcp_pad_remove(&reverse, dst, padlen, unpadlen);
|
||||
//printf("reverse: %s\n", reverse);
|
||||
/* printf("reverse: %s\n", reverse); */
|
||||
|
||||
return 0;
|
||||
}
|
||||
//fprintf(stderr, "Usage: pad <string> <padlen>\n");
|
||||
/* fprintf(stderr, "Usage: pad <string> <padlen>\n"); */
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -22,9 +22,9 @@
|
||||
#include "scrypt.h"
|
||||
|
||||
unsigned char* pcp_scrypt(char *passwd, size_t passwdlen, unsigned char *nonce, size_t noncelen) {
|
||||
uint8_t *dk = ucmalloc(64); // resulting hash
|
||||
uint8_t *dk = ucmalloc(64); /* resulting hash */
|
||||
|
||||
// constants
|
||||
/* constants */
|
||||
uint64_t N = 1 << 14;
|
||||
uint32_t r = 8;
|
||||
uint32_t p = 1;
|
||||
|
||||
@@ -34,7 +34,7 @@ vault_t *pcpvault_init(char *filename) {
|
||||
}
|
||||
else {
|
||||
if(pcpvault_fetchall(vault) != 0) {
|
||||
errno = 0; // weird, something sets it to ENOENT and it's not me
|
||||
errno = 0; /* weird, something sets it to ENOENT and it's not me */
|
||||
pcpvault_close(vault);
|
||||
return NULL;
|
||||
}
|
||||
@@ -161,7 +161,7 @@ int pcpvault_addkey(vault_t *vault, void *item, uint8_t type) {
|
||||
saveitem = ucmalloc(sizeof(pcp_pubkey_t));
|
||||
memcpy(saveitem, item, sizeof(pcp_pubkey_t));
|
||||
pubkey2be((pcp_pubkey_t *)item);
|
||||
//pcp_dumppubkey((pcp_pubkey_t *)saveitem);
|
||||
/* pcp_dumppubkey((pcp_pubkey_t *)saveitem); */
|
||||
}
|
||||
else {
|
||||
itemsize = PCP_RAW_KEYSIZE;
|
||||
@@ -172,9 +172,9 @@ int pcpvault_addkey(vault_t *vault, void *item, uint8_t type) {
|
||||
|
||||
void *blob = pcp_keyblob(item, type);
|
||||
|
||||
// scip
|
||||
//printf("BLOB (%d):\n", (int)itemsize);
|
||||
//pcpprint_bin(stdout, saveitem, itemsize); printf("\n");
|
||||
/* scip */
|
||||
/* printf("BLOB (%d):\n", (int)itemsize); */
|
||||
/* pcpprint_bin(stdout, saveitem, itemsize); printf("\n"); */
|
||||
|
||||
if(tmp != NULL) {
|
||||
if(pcpvault_copy(vault, tmp) != 0)
|
||||
@@ -253,7 +253,7 @@ void pcpvault_update_checksum(vault_t *vault) {
|
||||
memcpy(header->checksum, checksum, 32);
|
||||
memcpy(vault->checksum, checksum, 32);
|
||||
|
||||
//printf("write checksum: "); pcpprint_bin(stdout, checksum, 32); printf("\n");
|
||||
/* printf("write checksum: "); pcpprint_bin(stdout, checksum, 32); printf("\n"); */
|
||||
|
||||
vh2be(header);
|
||||
|
||||
@@ -282,16 +282,16 @@ unsigned char *pcpvault_create_checksum(vault_t *vault) {
|
||||
|
||||
pcp_pubkey_t *p = NULL;
|
||||
pcphash_iteratepub(p) {
|
||||
//pcp_dumppubkey(p);
|
||||
/* pcp_dumppubkey(p); */
|
||||
pubkey2be(p);
|
||||
memcpy(&data[datapos], p, PCP_RAW_PUBKEYSIZE);
|
||||
pubkey2native(p);
|
||||
datapos += PCP_RAW_PUBKEYSIZE;
|
||||
}
|
||||
|
||||
// scip
|
||||
//printf("DATA (%d) (s: %d, p: %d):\n", (int)datasize, numskeys, numpkeys);
|
||||
//pcpprint_bin(stdout, data, datasize); printf("\n");
|
||||
/* scip */
|
||||
/* printf("DATA (%d) (s: %d, p: %d):\n", (int)datasize, numskeys, numpkeys); */
|
||||
/* pcpprint_bin(stdout, data, datasize); printf("\n"); */
|
||||
|
||||
crypto_hash_sha256(checksum, data, datasize);
|
||||
|
||||
@@ -303,14 +303,14 @@ unsigned char *pcpvault_create_checksum(vault_t *vault) {
|
||||
|
||||
|
||||
int pcpvault_copy(vault_t *tmp, vault_t *vault) {
|
||||
// fetch tmp content
|
||||
/* fetch tmp content */
|
||||
fseek(tmp->fd, 0, SEEK_END);
|
||||
int tmpsize = ftell(tmp->fd);
|
||||
fseek(tmp->fd, 0, SEEK_SET);
|
||||
unsigned char *in = ucmalloc(tmpsize);
|
||||
fread(in, tmpsize, 1, tmp->fd);
|
||||
|
||||
// and put it into the new file
|
||||
/* and put it into the new file */
|
||||
vault->fd = freopen(vault->filename, "wb+", vault->fd);
|
||||
if(fwrite(in, tmpsize, 1, vault->fd) != 1) {
|
||||
fatal("Failed to copy %s to %s (write) [keeping %s]\n",
|
||||
@@ -405,12 +405,12 @@ int pcpvault_fetchall(vault_t *vault) {
|
||||
vh2native(header);
|
||||
|
||||
if(header->fileid == PCP_VAULT_ID && header->version == PCP_VAULT_VERSION) {
|
||||
// loop over the file and slurp everything in
|
||||
/* loop over the file and slurp everything in */
|
||||
int readpos = 0;
|
||||
pcp_key_t *key;
|
||||
pcp_pubkey_t *pubkey;
|
||||
int bytesleft = 0;
|
||||
int ksize = PCP_RAW_PUBKEYSIZE; // smallest possbile item
|
||||
int ksize = PCP_RAW_PUBKEYSIZE; /* smallest possbile item */
|
||||
|
||||
pcphash_init();
|
||||
|
||||
@@ -420,28 +420,28 @@ int pcpvault_fetchall(vault_t *vault) {
|
||||
for(;;) {
|
||||
readpos = ftell(vault->fd);
|
||||
if(vault->size - readpos >= sizeof(vault_item_header_t)) {
|
||||
// an item header follows
|
||||
/* an item header follows */
|
||||
fread(item, sizeof(vault_item_header_t), 1, vault->fd);
|
||||
ih2native(item);
|
||||
|
||||
if(item->size > 0) {
|
||||
// item is valid
|
||||
/* item is valid */
|
||||
readpos = ftell(vault->fd);
|
||||
bytesleft = vault->size - readpos;
|
||||
if(bytesleft >= ksize) {
|
||||
// a key follows
|
||||
/* a key follows */
|
||||
if(item->type == PCP_KEY_TYPE_MAINSECRET ||
|
||||
item->type == PCP_KEY_TYPE_SECRET) {
|
||||
// read a secret key
|
||||
/* read a secret key */
|
||||
key = ucmalloc(sizeof(pcp_key_t));
|
||||
fread(key, PCP_RAW_KEYSIZE, 1, vault->fd);
|
||||
key2native(key);
|
||||
//pcp_dumpkey(key);
|
||||
//pcpprint_bin(stdout, key, sizeof(pcp_key_t));printf("\n");
|
||||
/* pcp_dumpkey(key); */
|
||||
/* pcpprint_bin(stdout, key, sizeof(pcp_key_t));printf("\n"); */
|
||||
pcphash_add((void *)key, item->type);
|
||||
}
|
||||
else if(item->type == PCP_KEY_TYPE_PUBLIC) {
|
||||
// read a public key
|
||||
/* read a public key */
|
||||
pubkey = ucmalloc(sizeof(pcp_pubkey_t));
|
||||
fread(pubkey, PCP_RAW_PUBKEYSIZE, 1, vault->fd);
|
||||
pubkey2native(pubkey);
|
||||
@@ -464,7 +464,7 @@ int pcpvault_fetchall(vault_t *vault) {
|
||||
}
|
||||
}
|
||||
else {
|
||||
// no more items
|
||||
/* no more items */
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -476,9 +476,9 @@ int pcpvault_fetchall(vault_t *vault) {
|
||||
|
||||
unsigned char *checksum = NULL;
|
||||
checksum = pcpvault_create_checksum(vault);
|
||||
//printf(" calc checksum: "); pcpprint_bin(stdout, checksum, 32); printf("\n");
|
||||
/* printf(" calc checksum: "); pcpprint_bin(stdout, checksum, 32); printf("\n"); */
|
||||
if(pcphash_count() + pcphash_countpub() > 0) {
|
||||
// only validate the checksum if there are keys
|
||||
/* only validate the checksum if there are keys */
|
||||
if(memcmp(checksum, vault->checksum, 32) != 0) {
|
||||
fatal("Error: the checksum of the key vault doesn't match its contents!\n");
|
||||
goto err;
|
||||
@@ -493,7 +493,7 @@ int pcpvault_fetchall(vault_t *vault) {
|
||||
err:
|
||||
free(item);
|
||||
free(header);
|
||||
//pcphash_clean();
|
||||
/* pcphash_clean(); */
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
28
libpcp/z85.c
28
libpcp/z85.c
@@ -26,14 +26,14 @@ unsigned char *pcp_padfour(unsigned char *src, size_t srclen, size_t *dstlen) {
|
||||
size_t outlen, zerolen;
|
||||
unsigned char *dst;
|
||||
|
||||
outlen = srclen + 1; // 1 for the pad flag
|
||||
outlen = srclen + 1; /* 1 for the pad flag */
|
||||
while (outlen % 4 != 0) outlen++;
|
||||
zerolen = outlen - (srclen + 1);
|
||||
|
||||
dst = (unsigned char*)ucmalloc(outlen);
|
||||
dst[0] = zerolen; // add the number of zeros we add
|
||||
memcpy(&dst[1], src, srclen); // add the original
|
||||
memset(&dst[srclen+1], 0, zerolen); // pad with zeroes
|
||||
dst[0] = zerolen; /* add the number of zeros we add */
|
||||
memcpy(&dst[1], src, srclen); /* add the original */
|
||||
memset(&dst[srclen+1], 0, zerolen); /* pad with zeroes */
|
||||
|
||||
*dstlen = outlen;
|
||||
|
||||
@@ -45,7 +45,7 @@ unsigned char *pcp_unpadfour(unsigned char *src, size_t srclen, size_t *dstlen)
|
||||
size_t numzeroes;
|
||||
unsigned char *dst;
|
||||
|
||||
numzeroes = src[0]; // first byte tells us how many zeroes we've got
|
||||
numzeroes = src[0]; /* first byte tells us how many zeroes we've got */
|
||||
outlen = srclen - 1 - numzeroes;
|
||||
|
||||
dst = malloc(outlen);
|
||||
@@ -65,7 +65,7 @@ unsigned char *pcp_z85_decode(char *z85block, size_t *dstlen) {
|
||||
zlen = strlen(z85block);
|
||||
char *z85 = ucmalloc(zlen+1);
|
||||
|
||||
// remove newlines
|
||||
/* remove newlines */
|
||||
pos = 0;
|
||||
for(i=0; i<zlen+1; ++i) {
|
||||
if(z85block[i] != '\r' && z85block[i] != '\n') {
|
||||
@@ -94,21 +94,21 @@ char *pcp_z85_encode(unsigned char *raw, size_t srclen, size_t *dstlen) {
|
||||
int pos, b;
|
||||
size_t outlen, blocklen, zlen;
|
||||
|
||||
// make z85 happy (size % 4)
|
||||
/* make z85 happy (size % 4) */
|
||||
unsigned char *padded = pcp_padfour(raw, srclen, &outlen);
|
||||
|
||||
// encode to z85
|
||||
/* encode to z85 */
|
||||
zlen = (outlen * 5 / 4) + 1;
|
||||
char *z85 = ucmalloc(zlen);
|
||||
z85 = zmq_z85_encode(z85, padded, outlen);
|
||||
|
||||
|
||||
// make it a 72 chars wide block
|
||||
/* make it a 72 chars wide block */
|
||||
blocklen = (zlen + ((zlen / 72) * 2)) + 1;
|
||||
char *z85block = ucmalloc(blocklen);
|
||||
|
||||
//fprintf(stderr, "zlen: %d, outlen: %d, srclen: %d, blocklen: %d\n",
|
||||
// zlen, outlen, srclen, blocklen);
|
||||
/* fprintf(stderr, "zlen: %d, outlen: %d, srclen: %d, blocklen: %d\n", */
|
||||
/* zlen, outlen, srclen, blocklen); */
|
||||
|
||||
pos = b = 0;
|
||||
/*
|
||||
@@ -141,8 +141,8 @@ char *pcp_z85_encode(unsigned char *raw, size_t srclen, size_t *dstlen) {
|
||||
}
|
||||
*B = '\0';
|
||||
|
||||
//fprintf(stderr, "z85block len: %d\n", blocklen, strlen(z85block));
|
||||
//fprintf(stderr, "z85block: <%s>\n", z85block);
|
||||
/* fprintf(stderr, "z85block len: %d\n", blocklen, strlen(z85block)); */
|
||||
/* fprintf(stderr, "z85block: <%s>\n", z85block); */
|
||||
|
||||
*dstlen = blocklen;
|
||||
free(z85);
|
||||
@@ -189,7 +189,7 @@ char *pcp_readz85string(unsigned char *input, size_t bufsize) {
|
||||
|
||||
for(i=0; i<bufsize; ++i) {
|
||||
if(lpos > MAXLINE) {
|
||||
// huh, now that's suspicious
|
||||
/* huh, now that's suspicious */
|
||||
fatal("Invalid input, line is too long (%d bytes so far)!\n", lpos);
|
||||
goto rferr;
|
||||
}
|
||||
|
||||
@@ -19,17 +19,17 @@
|
||||
|
||||
#include "zmq_z85.h"
|
||||
|
||||
// Z85 codec, taken from 0MQ RFC project, implements RFC32 Z85 encoding
|
||||
/* Z85 codec, taken from 0MQ RFC project, implements RFC32 Z85 encoding */
|
||||
|
||||
// Maps base 256 to base 85
|
||||
/* Maps base 256 to base 85 */
|
||||
static char encoder [85 + 1] = {
|
||||
"0123456789" "abcdefghij" "klmnopqrst" "uvwxyzABCD"
|
||||
"EFGHIJKLMN" "OPQRSTUVWX" "YZ.-:+=^!/" "*?&<>()[]{"
|
||||
"}@%$#"
|
||||
};
|
||||
|
||||
// Maps base 85 to base 256
|
||||
// We chop off lower 32 and higher 128 ranges
|
||||
/* Maps base 85 to base 256 */
|
||||
/* We chop off lower 32 and higher 128 ranges */
|
||||
static uint8_t decoder [96] = {
|
||||
0x00, 0x44, 0x00, 0x54, 0x53, 0x52, 0x48, 0x00,
|
||||
0x4B, 0x4C, 0x46, 0x41, 0x00, 0x3F, 0x3E, 0x45,
|
||||
@@ -45,24 +45,24 @@ static uint8_t decoder [96] = {
|
||||
0x21, 0x22, 0x23, 0x4F, 0x00, 0x50, 0x00, 0x00
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Encode a binary frame as a string; destination string MUST be at least
|
||||
// size * 5 / 4 bytes long plus 1 byte for the null terminator. Returns
|
||||
// dest. Size must be a multiple of 4.
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Encode a binary frame as a string; destination string MUST be at least */
|
||||
/* size * 5 / 4 bytes long plus 1 byte for the null terminator. Returns */
|
||||
/* dest. Size must be a multiple of 4. */
|
||||
|
||||
char *zmq_z85_encode (char *dest, uint8_t *data, size_t size)
|
||||
{
|
||||
if (size % 4 != 0)
|
||||
return NULL; // !assert
|
||||
return NULL; /* !assert */
|
||||
|
||||
unsigned int char_nbr = 0;
|
||||
unsigned int byte_nbr = 0;
|
||||
uint32_t value = 0;
|
||||
while (byte_nbr < size) {
|
||||
// Accumulate value in base 256 (binary)
|
||||
/* Accumulate value in base 256 (binary) */
|
||||
value = value * 256 + data [byte_nbr++];
|
||||
if (byte_nbr % 4 == 0) {
|
||||
// Output value in base 85
|
||||
/* Output value in base 85 */
|
||||
unsigned int divisor = 85 * 85 * 85 * 85;
|
||||
while (divisor) {
|
||||
dest [char_nbr++] = encoder [value / divisor % 85];
|
||||
@@ -72,31 +72,31 @@ char *zmq_z85_encode (char *dest, uint8_t *data, size_t size)
|
||||
}
|
||||
}
|
||||
if (char_nbr != size * 5 / 4)
|
||||
return NULL; // !assert
|
||||
return NULL; /* !assert */
|
||||
|
||||
dest [char_nbr] = 0;
|
||||
return dest;
|
||||
}
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Decode an encoded string into a binary frame; dest must be at least
|
||||
// strlen (string) * 4 / 5 bytes long. Returns dest. strlen (string)
|
||||
// must be a multiple of 5.
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Decode an encoded string into a binary frame; dest must be at least */
|
||||
/* strlen (string) * 4 / 5 bytes long. Returns dest. strlen (string) */
|
||||
/* must be a multiple of 5. */
|
||||
|
||||
uint8_t *zmq_z85_decode (uint8_t *dest, char *string)
|
||||
{
|
||||
if (strlen (string) % 5 != 0)
|
||||
return NULL; // !assert
|
||||
return NULL; /* !assert */
|
||||
unsigned int byte_nbr = 0;
|
||||
unsigned int char_nbr = 0;
|
||||
uint32_t value = 0;
|
||||
size_t string_len = strlen (string);
|
||||
while (char_nbr < string_len) {
|
||||
// Accumulate value in base 85
|
||||
/* Accumulate value in base 85 */
|
||||
value = value * 85 + decoder [(uint8_t) string [char_nbr++] - 32];
|
||||
if (char_nbr % 5 == 0) {
|
||||
// Output value in base 256
|
||||
/* Output value in base 256 */
|
||||
unsigned int divisor = 256 * 256 * 256;
|
||||
while (divisor) {
|
||||
dest [byte_nbr++] = value / divisor % 256;
|
||||
@@ -106,6 +106,6 @@ uint8_t *zmq_z85_decode (uint8_t *dest, char *string)
|
||||
}
|
||||
}
|
||||
if (byte_nbr != strlen (string) * 4 / 5)
|
||||
return NULL; //!assert
|
||||
return NULL; /* !assert */
|
||||
return dest;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user