Skip to content

Commit

Permalink
Implement e4c_protect and e4c_unprotect for pubkey variant.
Browse files Browse the repository at this point in the history
Does not yet contain tests.
  • Loading branch information
diagprov committed Dec 21, 2019
1 parent d7867a7 commit d625986
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 69 deletions.
16 changes: 14 additions & 2 deletions include/e4/e4.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ extern "C" {

/* Unable to find public key for device;e4 */
#define E4_ERROR_DEVICEPK_MISSING -109
/* Signature verification failed */
#define E4_ERROR_PK_SIGVERIF_FAILED -110


/* Size of the timestamp field */
#define E4_TS_LEN 8
/* Size of the ID, truncated sha3(alias) */
#define E4_ID_LEN 16

Expand All @@ -71,6 +76,10 @@ extern "C" {
#define E4_PK_EDDSA_PRIVKEY_LEN 64
#define E4_PK_EDDSA_PUBKEY_LEN 32
#define E4_PK_EDDSA_SIG_LEN 32
#define E4_PK_X25519_PUBKEY_LEN 32
#define E4_PK_X25519_PRIVKEY_LEN 32

#define E4_PK_TOPICMSGHDR_LEN (E4_TAG_LEN + E4_TIMESTAMP_LEN + E4_ID_LEN)

struct _e4storage;
/* This structure represents storage-specific data to be passed to the e4c
Expand Down Expand Up @@ -151,21 +160,24 @@ int e4c_set_storagelocation(e4storage *store, const char *path);
int e4c_load(e4storage *store, const char *path);
int e4c_sync(e4storage *store);
int e4c_set_id(e4storage *store, const uint8_t *id);
int e4c_set_idkey(e4storage *store, const uint8_t *key);
int e4c_is_device_ctrltopic(e4storage *store, const char *topic);
int e4c_getindex(e4storage *store, const char *topic);
int e4c_gettopickey(uint8_t *key, e4storage *store, const int index);
int e4c_set_topic_key(e4storage *store, const uint8_t *topic_hash, const uint8_t *key);
int e4c_remove_topic(e4storage *store, const uint8_t *topic_hash);
int e4c_reset_topics(e4storage *store);

int e4c_set_idkey(e4storage *store, const uint8_t *key);
#ifdef E4_MODE_PUBKEY
/* pubkey storage apis */
int e4c_set_idpubkey(e4storage *store, const uint8_t *pubkey);
int e4c_getdeviceindex(e4storage *store, const uint8_t* id);
int e4c_getdevicekey(uint8_t* key, e4storage *store, const int index);
int e4c_set_device_key(e4storage *store, const uint8_t *id, const uint8_t *key);
int e4c_remove_devices(e4storage* store, const uint8_t* id);
int e4c_remove_device(e4storage* store, const uint8_t* id);
int e4c_reset_devices(e4storage* store);
int e4c_set_c2_pubkey(e4storage* store, const uint8_t* key);
int e4c_get_c2_pubkey(e4storage* store, uint8_t* key);
#endif

/*#ifdef DEBUG*/
Expand Down
2 changes: 2 additions & 0 deletions include/e4/internal/e4c_pk_store_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct _e4storage
/* These fields are persisted by the sync command */
uint8_t id[E4_ID_LEN];
uint8_t privkey[E4_PK_EDDSA_PRIVKEY_LEN];
uint8_t pubkey[E4_PK_EDDSA_PUBKEY_LEN];
uint8_t c2key[E4_PK_X25519_PUBKEY_LEN];

uint16_t devicecount;
uint16_t topiccount;
Expand Down
2 changes: 0 additions & 2 deletions mk/pubkey/objects.mk
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ OBJS = \
$(OBJDIR)/strlcpy.$O \
$(OBJDIR)/crypto/curve25519/curve25519-donna.$O \
$(OBJDIR)/crypto/ed25519/add_scalar.$O \
$(OBJDIR)/crypto/ed25519/ed25519_test.$O \
$(OBJDIR)/crypto/ed25519/fe.$O \
$(OBJDIR)/crypto/ed25519/ge.$O \
$(OBJDIR)/crypto/ed25519/key_exchange.$O \
$(OBJDIR)/crypto/ed25519/keypair.$O \
$(OBJDIR)/crypto/ed25519/sc.$O \
$(OBJDIR)/crypto/ed25519/seed.$O \
Expand Down
19 changes: 18 additions & 1 deletion mk/pubkey/tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ $(TESTOBJDIR)/pubkey_file.$O: test/pubkey/pubkey_filestore_test.c
$(TESTOBJDIR)/pubkey_crypto_test.$O: test/pubkey/pubkey_crypto_test.c
$(CC) $(TESTCFLAGS) $(INCLUDES) -c $< -o $@

$(TESTOBJDIR)/ed25519_test.$O: test/pubkey/ed25519_test.c
$(CC) $(TESTCFLAGS) $(INCLUDES) -c $< -o $@

$(TESTDIR)/util: $(TESTOBJDIR)/util.$O
$(CC) $(TESTLDFLAGS) $< $(LIB) -o $@

Expand All @@ -27,10 +30,24 @@ $(TESTDIR)/pubkey_file: $(TESTOBJDIR)/pubkey_file.$O
$(TESTDIR)/pubkey_crypto_test: $(TESTOBJDIR)/pubkey_crypto_test.$O
$(CC) $(TESTLDFLAGS) $< $(LIB) -o $@

$(TESTDIR)/ed25519_test: $(TESTOBJDIR)/ed25519_test.$O
$(CC) $(TESTLDFLAGS) $< $(LIB) -o $@

PUBKEY_TESTS = \
$(TESTDIR)/util \
$(TESTDIR)/crypto \
$(TESTDIR)/pubkey_file \
$(TESTDIR)/pubkey_crypto_test
$(TESTDIR)/pubkey_crypto_test \
$(TESTDIR)/ed25519_test

E4TESTS += $(PUBKEY_TESTS)

testexec_pk:
./$(TESTDIR)/util
./$(TESTDIR)/crypto
./$(TESTDIR)/pubkey_file
./$(TESTDIR)/pubkey_crypto_test
./$(TESTDIR)/ed25519_test

E4TESTEXEC += testexec_pk

4 changes: 3 additions & 1 deletion mk/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ clean:
rm -rf $(LIBDIR)


test: clean setup lib $(E4TESTS)
testbuild: clean setup lib $(E4TESTS)

test: testbuild $(E4TESTEXEC)

format:
clang-format -i src/*.c src/crypto/*.c include/e4/*.h include/e4/crypto/*.h include/e4/internal/*.h
Expand Down
8 changes: 8 additions & 0 deletions mk/symkey/tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ SYMKEY_TESTS = \
$(TESTDIR)/symkey_file

E4TESTS += $(SYMKEY_TESTS)

testexec_sk:
./$(TESTDIR)/util
./$(TESTDIR)/crypto
./$(TESTDIR)/symkey_file

E4TESTEXEC += testexec_sk

45 changes: 41 additions & 4 deletions src/e4c_pk_store_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,19 @@ int e4c_load(e4storage *store, const char *path)
/* derive a topichash for the control topic. */
e4c_derive_topichash(store->ctrltopic, E4_TOPICHASH_LEN, controltopic);

/* read in key material */
rlen = read(fd, store->privkey, sizeof store->privkey);
if (rlen != sizeof store->privkey)
{
goto err;
}

lseek(fd, 4, SEEK_CUR);

rlen = read(fd, store->pubkey, sizeof store->pubkey);
if (rlen != sizeof store->pubkey)
{
goto err;
}
lseek(fd, 4, SEEK_CUR);

rlen = read(fd, &store->topiccount, sizeof store->topiccount);
Expand Down Expand Up @@ -205,6 +212,8 @@ int e4c_sync(e4storage *store)
write(fd, &zero, sizeof zero);
write(fd, store->privkey, sizeof store->privkey);
write(fd, &zero, sizeof zero);
write(fd, store->pubkey, sizeof store->pubkey);
write(fd, &zero, sizeof zero);
write(fd, &store->topiccount, sizeof store->topiccount);
write(fd, &zero, sizeof zero);

Expand Down Expand Up @@ -370,6 +379,11 @@ int e4c_reset_topics(e4storage *store)
return 0;
}

int e4c_set_idpubkey(e4storage *store, const uint8_t *pubkey) {
memmove(store->pubkey, pubkey, sizeof store->pubkey);
return E4_RESULT_OK;
}

int e4c_getdeviceindex(e4storage *store, const uint8_t* id)
{
int i;
Expand Down Expand Up @@ -421,7 +435,7 @@ int e4c_set_device_key(e4storage *store, const uint8_t *id, const uint8_t *pubke
return e4c_sync(store);
}

int e4c_remove_devices(e4storage* store, const uint8_t* id)
int e4c_remove_device(e4storage* store, const uint8_t* id)
{
int i, j;
device_key *devicekeys = store->devices;
Expand Down Expand Up @@ -462,6 +476,17 @@ int e4c_reset_devices(e4storage* store)
return 0;
}

int e4c_set_c2_pubkey(e4storage* store, const uint8_t* key) {
memcpy(store->c2key, key, E4_PK_X25519_PUBKEY_LEN);
e4c_sync(store);
return 0;
}

int e4c_get_c2_pubkey(e4storage* store, uint8_t* key) {
memcpy(key, store->c2key, E4_PK_X25519_PUBKEY_LEN);
return 0;
}

/*#ifdef DEBUG */

void e4c_debug_print(e4storage *store)
Expand All @@ -476,12 +501,24 @@ void e4c_debug_print(e4storage *store)
printf("%02x", store->id[j]);
}
printf("\n");
printf(" Key=");
for (j = 0; j < E4_KEY_LEN; j++)
printf(" PrivKey=");
for (j = 0; j < E4_PK_EDDSA_PRIVKEY_LEN; j++)
{
printf("%02x", store->privkey[j]);
}
printf("\n");
printf(" PubKey=");
for (j = 0; j < E4_PK_EDDSA_PUBKEY_LEN; j++)
{
printf("%02x", store->privkey[j]);
}
printf("\n");
printf(" C2PubKey=");
for (j = 0; j < E4_PK_X25519_PUBKEY_LEN; j++)
{
printf("%02x", store->c2key[j]);
}
printf("\n");
e4c_derive_control_topic(controltopic, E4_CTRLTOPIC_LEN + 1, store->id);
printf(" ControlTopic=%s\n", controltopic);

Expand Down
Loading

0 comments on commit d625986

Please sign in to comment.