mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 03:50:57 +01:00
moved all uthash related code into keyhash.c
This commit is contained in:
@@ -28,6 +28,6 @@ pkgconfig_DATA = libpcp1.pc
|
||||
libpcp1_la_SOURCES = mac.c mem.c pad.c version.c \
|
||||
z85.c zmq_z85.c key.c randomart.c \
|
||||
vault.c fatal.c jenhash.c digital_crc32.c \
|
||||
crypto.c ed.c
|
||||
crypto.c ed.c keyhash.c
|
||||
|
||||
include_HEADERS = ../include/pcp.h
|
||||
|
||||
@@ -101,7 +101,7 @@ LTLIBRARIES = $(lib_LTLIBRARIES)
|
||||
libpcp1_la_LIBADD =
|
||||
am_libpcp1_la_OBJECTS = mac.lo mem.lo pad.lo version.lo z85.lo \
|
||||
zmq_z85.lo key.lo randomart.lo vault.lo fatal.lo jenhash.lo \
|
||||
digital_crc32.lo crypto.lo ed.lo
|
||||
digital_crc32.lo crypto.lo ed.lo keyhash.lo
|
||||
libpcp1_la_OBJECTS = $(am_libpcp1_la_OBJECTS)
|
||||
DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/include/pcp
|
||||
depcomp = $(SHELL) $(top_srcdir)/config/depcomp
|
||||
@@ -269,7 +269,7 @@ pkgconfig_DATA = libpcp1.pc
|
||||
libpcp1_la_SOURCES = mac.c mem.c pad.c version.c \
|
||||
z85.c zmq_z85.c key.c randomart.c \
|
||||
vault.c fatal.c jenhash.c digital_crc32.c \
|
||||
crypto.c ed.c
|
||||
crypto.c ed.c keyhash.c
|
||||
|
||||
include_HEADERS = ../include/pcp.h
|
||||
all: all-am
|
||||
@@ -357,6 +357,7 @@ distclean-compile:
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fatal.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/jenhash.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/key.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/keyhash.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mac.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mem.Plo@am__quote@
|
||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pad.Plo@am__quote@
|
||||
|
||||
42
libpcp/key.c
42
libpcp/key.c
@@ -21,7 +21,7 @@
|
||||
|
||||
|
||||
#include "key.h"
|
||||
|
||||
#include "keyhash.h"
|
||||
|
||||
unsigned char *pcp_derivekey(char *passphrase) {
|
||||
unsigned char *hash32 = ucmalloc(crypto_hash_sha256_BYTES);
|
||||
@@ -175,7 +175,6 @@ pcp_key_t *pcpkey_decrypt(pcp_key_t *key, char *passphrase) {
|
||||
if(es == 0) {
|
||||
// success
|
||||
byte secret[32] = { 0 };
|
||||
byte edsec[64] = { 0 };
|
||||
pcp_ed_keypairs(secret, decrypted);
|
||||
memcpy(key->secret, secret, 32);
|
||||
memcpy(key->edsecret, decrypted, 64);
|
||||
@@ -226,45 +225,6 @@ unsigned char *pcpkey_getchecksum(pcp_key_t *k) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
void pcp_inithashes() {
|
||||
pcpkey_hash = NULL;
|
||||
pcppubkey_hash = NULL;
|
||||
}
|
||||
|
||||
void pcp_cleanhashes() {
|
||||
if(pcpkey_hash != NULL) {
|
||||
pcp_key_t *current_key, *tmp;
|
||||
HASH_ITER(hh, pcpkey_hash, current_key, tmp) {
|
||||
HASH_DEL(pcpkey_hash,current_key);
|
||||
memset(current_key, 0, sizeof(pcp_key_t));
|
||||
free(current_key); // FIXME: coredumps here after n-th secret keys has been added
|
||||
}
|
||||
}
|
||||
|
||||
if(pcppubkey_hash != NULL) {
|
||||
pcp_pubkey_t *current_pub, *ptmp;
|
||||
HASH_ITER(hh, pcppubkey_hash, current_pub, ptmp) {
|
||||
HASH_DEL(pcppubkey_hash,current_pub);
|
||||
memset(current_pub, 0, sizeof(pcp_pubkey_t));
|
||||
free(current_pub);
|
||||
}
|
||||
}
|
||||
pcp_inithashes();
|
||||
}
|
||||
|
||||
pcp_key_t *pcpkey_exists(char *id) {
|
||||
pcp_key_t *key = NULL;
|
||||
HASH_FIND_STR(pcpkey_hash, id, key);
|
||||
return key; // maybe NULL!
|
||||
}
|
||||
|
||||
pcp_pubkey_t *pcppubkey_exists(char *id) {
|
||||
pcp_pubkey_t *key = NULL;
|
||||
HASH_FIND_STR(pcppubkey_hash, id, key);
|
||||
return key; // maybe NULL!
|
||||
}
|
||||
|
||||
pcp_key_t * key2be(pcp_key_t *k) {
|
||||
k->version = htobe32(k->version);
|
||||
k->serial = htobe32(k->serial);
|
||||
|
||||
90
libpcp/keyhash.c
Normal file
90
libpcp/keyhash.c
Normal file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
This file is part of Pretty Curved Privacy (pcp1).
|
||||
|
||||
Copyright (C) 2013 T.Linden.
|
||||
|
||||
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/>.
|
||||
|
||||
You can contact me by mail: <tlinden AT cpan DOT org>.
|
||||
*/
|
||||
|
||||
|
||||
#include "keyhash.h"
|
||||
|
||||
void pcphash_init() {
|
||||
pcpkey_hash = NULL;
|
||||
pcppubkey_hash = NULL;
|
||||
}
|
||||
|
||||
void pcphash_del(void *key, int type) {
|
||||
if(type == PCP_KEY_TYPE_SECRET) {
|
||||
HASH_DEL(pcpkey_hash, (pcp_key_t *)key);
|
||||
memset(key, 0, sizeof(pcp_key_t));
|
||||
}
|
||||
else {
|
||||
HASH_DEL(pcppubkey_hash, (pcp_pubkey_t *)key);
|
||||
memset(key, 0, sizeof(pcp_pubkey_t));
|
||||
}
|
||||
free(key);
|
||||
}
|
||||
|
||||
void pcphash_clean() {
|
||||
if(pcpkey_hash != NULL) {
|
||||
pcp_key_t *current_key, *tmp;
|
||||
HASH_ITER(hh, pcpkey_hash, current_key, tmp) {
|
||||
pcphash_del(current_key, PCP_KEY_TYPE_SECRET);
|
||||
}
|
||||
}
|
||||
|
||||
if(pcppubkey_hash != NULL) {
|
||||
pcp_pubkey_t *current_pub, *ptmp;
|
||||
HASH_ITER(hh, pcppubkey_hash, current_pub, ptmp) {
|
||||
pcphash_del(current_pub, PCP_KEY_TYPE_PUBLIC);
|
||||
}
|
||||
}
|
||||
pcphash_init();
|
||||
}
|
||||
|
||||
|
||||
pcp_key_t *pcphash_keyexists(char *id) {
|
||||
pcp_key_t *key = NULL;
|
||||
HASH_FIND_STR(pcpkey_hash, id, key);
|
||||
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!
|
||||
}
|
||||
|
||||
void pcphash_add(void *key, int type) {
|
||||
if(type == PCP_KEY_TYPE_PUBLIC) {
|
||||
pcp_pubkey_t *k = (pcp_pubkey_t *)key;
|
||||
HASH_ADD_STR( pcppubkey_hash, id, k );
|
||||
}
|
||||
else {
|
||||
pcp_key_t *k = (pcp_key_t *)key;
|
||||
HASH_ADD_STR( pcpkey_hash, id, k);
|
||||
}
|
||||
}
|
||||
|
||||
int pcphash_count() {
|
||||
return HASH_COUNT(pcpkey_hash);
|
||||
}
|
||||
|
||||
int pcphash_countpub() {
|
||||
return HASH_COUNT(pcppubkey_hash);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
|
||||
#include "vault.h"
|
||||
#include "keyhash.h"
|
||||
|
||||
vault_t *pcpvault_init(char *filename) {
|
||||
vault_t *vault = pcpvault_new(filename, 0);
|
||||
@@ -151,14 +152,7 @@ int pcpvault_additem(vault_t *vault, void *item, size_t itemsize, uint8_t type,
|
||||
|
||||
if(do_hash == 1) {
|
||||
// we don't re-hash if it's a full update
|
||||
if(type == PCP_KEY_TYPE_PUBLIC) {
|
||||
pcp_pubkey_t *p = (pcp_pubkey_t *)item;
|
||||
HASH_ADD_STR( pcppubkey_hash, id, p );
|
||||
}
|
||||
else {
|
||||
pcp_key_t *s = (pcp_key_t *)item;
|
||||
HASH_ADD_STR( pcpkey_hash, id, s );
|
||||
}
|
||||
pcphash_add(item, type);
|
||||
pcpvault_update_checksum(vault);
|
||||
}
|
||||
|
||||
@@ -204,13 +198,13 @@ int pcpvault_writeall(vault_t *vault) {
|
||||
vault_t *tmp = pcpvault_new(vault->filename, 1);
|
||||
if(tmp != NULL) {
|
||||
if(pcpvault_create(tmp) == 0) {
|
||||
pcp_key_t *k, *kt = NULL;
|
||||
HASH_ITER(hh, pcpkey_hash, k, kt) {
|
||||
pcp_key_t *k = NULL;
|
||||
pcphash_iterate(k) {
|
||||
if(pcpvault_additem(tmp, (void *)k, sizeof(pcp_key_t), PCP_KEY_TYPE_SECRET, 0) != 0)
|
||||
goto errwa;
|
||||
}
|
||||
pcp_pubkey_t *p, *pt = NULL;
|
||||
HASH_ITER(hh, pcppubkey_hash, p, pt) {
|
||||
pcp_pubkey_t *p = NULL;
|
||||
pcphash_iteratepub(p) {
|
||||
if(pcpvault_additem(tmp, (void *)p, sizeof(pcp_pubkey_t), PCP_KEY_TYPE_PUBLIC, 0) != 0)
|
||||
goto errwa;
|
||||
}
|
||||
@@ -253,24 +247,24 @@ unsigned char *pcpvault_create_checksum(vault_t *vault) {
|
||||
size_t skeysize = sizeof(pcp_key_t) - sizeof(UT_hash_handle);
|
||||
size_t pkeysize = sizeof(pcp_pubkey_t) - sizeof(UT_hash_handle);
|
||||
|
||||
int numskeys = HASH_COUNT(pcpkey_hash);
|
||||
int numpkeys = HASH_COUNT(pcppubkey_hash);
|
||||
int numskeys = pcphash_count();
|
||||
int numpkeys = pcphash_countpub();
|
||||
|
||||
size_t datasize = (skeysize * numskeys) + (pkeysize * numpkeys);
|
||||
unsigned char *data = ucmalloc(datasize);
|
||||
unsigned char *checksum = ucmalloc(32);
|
||||
size_t datapos = 0;
|
||||
|
||||
pcp_key_t *k, *kt = NULL;
|
||||
HASH_ITER(hh, pcpkey_hash, k, kt) {
|
||||
pcp_key_t *k = NULL;
|
||||
pcphash_iterate(k) {
|
||||
key2be(k);
|
||||
memcpy(&data[datapos], k, skeysize);
|
||||
key2native(k);
|
||||
datapos += skeysize;
|
||||
}
|
||||
|
||||
pcp_pubkey_t *p, *pt = NULL;
|
||||
HASH_ITER(hh, pcppubkey_hash, p, pt) {
|
||||
pcp_pubkey_t *p = NULL;
|
||||
pcphash_iteratepub(p) {
|
||||
pubkey2be(p);
|
||||
memcpy(&data[datapos], p, pkeysize);
|
||||
pubkey2native(p);
|
||||
@@ -374,14 +368,14 @@ int pcpvault_fetchall(vault_t *vault) {
|
||||
|
||||
if(header->fileid == PCP_VAULT_ID && header->version == PCP_VAULT_VERSION) {
|
||||
// loop over the file and slurp everything in
|
||||
pcpkey_hash = NULL;
|
||||
pcppubkey_hash = NULL;
|
||||
int readpos = 0;
|
||||
pcp_key_t *key;
|
||||
pcp_pubkey_t *pubkey;
|
||||
int bytesleft = 0;
|
||||
int ksize = sizeof(pcp_pubkey_t); // smallest possbile item
|
||||
|
||||
pcphash_init();
|
||||
|
||||
vault->version = header->version;
|
||||
memcpy(vault->checksum, header->checksum, 32);
|
||||
|
||||
@@ -405,14 +399,14 @@ int pcpvault_fetchall(vault_t *vault) {
|
||||
fread(key, sizeof(pcp_key_t), 1, vault->fd);
|
||||
key2native(key);
|
||||
//pcp_dumpkey(key);
|
||||
HASH_ADD_STR( pcpkey_hash, id, key );
|
||||
pcphash_add((void *)key, item->type);
|
||||
}
|
||||
else if(item->type == PCP_KEY_TYPE_PUBLIC) {
|
||||
// read a public key
|
||||
pubkey = ucmalloc(sizeof(pcp_pubkey_t));
|
||||
fread(pubkey, sizeof(pcp_pubkey_t), 1, vault->fd);
|
||||
pubkey2native(pubkey);
|
||||
HASH_ADD_STR( pcppubkey_hash, id, pubkey );
|
||||
pcphash_add((void *)pubkey, item->type);
|
||||
}
|
||||
else {
|
||||
fatal("Failed to read vault - invalid key type: %02X! at %d\n", item->type, readpos);
|
||||
@@ -443,7 +437,7 @@ int pcpvault_fetchall(vault_t *vault) {
|
||||
|
||||
unsigned char *checksum = NULL;
|
||||
checksum = pcpvault_create_checksum(vault);
|
||||
if(HASH_COUNT(pcpkey_hash) + HASH_COUNT(pcppubkey_hash) > 0) {
|
||||
if(pcphash_count() + pcphash_countpub() > 0) {
|
||||
// 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");
|
||||
@@ -459,7 +453,7 @@ int pcpvault_fetchall(vault_t *vault) {
|
||||
err:
|
||||
free(item);
|
||||
free(header);
|
||||
pcp_cleanhashes();
|
||||
//pcphash_clean();
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user