Skip to content

Commit

Permalink
Missing e4c_getindex call in the encrypt function
Browse files Browse the repository at this point in the history
Cannot believe I spent so long looking for this bug.
  • Loading branch information
diagprov committed Jun 27, 2019
1 parent 1d43edc commit aecb3fb
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ build/*
.*.swo
.vscode/

util/sivkat/sivkat
6 changes: 0 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ dist: $(LIB)
tar cfvj $(DISTDIR)/$(LIBNAME)-$(NOW)-$(GITCOMMIT).tar.bz2 $(LIBDIR)/* $(INCDIR)/*

test: clean setup $(LIB) $(TESTS)
@echo "=== TESTS ==="
@echo "Executing test: testaessiv"; ./build/test/testaessiv
@echo "Executing test: testsha3"; ./build/test/testsha3
@echo "Executing test: testutil"; ./build/test/testutil
@echo "Executing test: teste4file"; ./build/test/teste4file


# Generic test rule.
build/test/%: test/%.c $(LIB)
Expand Down
Empty file added scripts/release.sh
Empty file.
8 changes: 8 additions & 0 deletions scripts/unittest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

make test

./build/test/crypto; echo "== RETURNCODE=$?"
./build/test/util; echo "== RETURNCODE=$?"
./build/test/e4file; echo "== RETURNCODE=$?"

13 changes: 0 additions & 13 deletions src/e4c_store_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ int e4c_set_storagelocation(e4storage* store, const char *path)
if ( pathlen >= copied_bytes ) {
return 1;
}
printf("Set storage location to: %s\n", store->filepath);
return 0;
}

Expand Down Expand Up @@ -130,7 +129,6 @@ int e4c_load(e4storage* store, const char *path)
close(fd);
return 0;
err:
printf("An error occurred during load.\n");
perror(path);
close(fd);
return E4ERR_PersistenceError;
Expand Down Expand Up @@ -192,19 +190,8 @@ int e4c_getindex(e4storage* store, const char *topic)

// hash the topic
sha3(topic, strlen(topic), hash, E4_TOPICHASH_LEN);
printf("Looking for topic %s\n", topic);
printf("Has hash ");
for (int j = 0; j < E4_TOPICHASH_LEN; j++ ) {
printf("%02x", hash[j]);
}
printf("\n");
// look for it
for (i = 0; i < store->topiccount; i++) { // find the key
printf("Comparing with ");
for (int j = 0; j < E4_TOPICHASH_LEN; j++ ) {
printf("%02x", store->topics[i].topic[j]);
}
printf("\n");
if (memcmp(store->topics[i].topic, hash, E4_TOPICHASH_LEN) == 0) {
break;
}
Expand Down
16 changes: 12 additions & 4 deletions src/e4client.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ int e4c_protect_message(uint8_t *cptr, size_t cmax, size_t *clen,
*clen = mlen + E4_MSGHDR_LEN;

// get the key
i = e4c_getindex(storage, topic);
if (i >= 0) {
e4c_gettopickey(key, storage, i);
} else {
if (e4c_is_device_ctrltopic(storage, topic)!=0) {
return -1;
return E4ERR_TopicKeyMissing;
}
// control topic being used:
memcpy(key, storage->key, E4_KEY_LEN);
Expand Down Expand Up @@ -83,12 +84,14 @@ int e4c_unprotect_message(uint8_t *mptr, size_t mmax, size_t *mlen,
#endif

// bounds checking
if (clen < E4_MSGHDR_LEN || mmax < clen - E4_MSGHDR_LEN)

if (clen < E4_MSGHDR_LEN || mmax < clen - E4_MSGHDR_LEN)
{
return E4ERR_TooShortCiphertext;
}

// get the key
i = e4c_getindex(storage, topic);
printf("i=%d\n", i);
if (i >= 0) {
e4c_gettopickey(key, storage, i);
} else {
Expand All @@ -108,9 +111,14 @@ int e4c_unprotect_message(uint8_t *mptr, size_t mmax, size_t *mlen,
}

// decrypt
if (aes256_decrypt_siv(mptr, mlen, cptr, 8, cptr + 8, clen - 8, key) != 0)
if (aes256_decrypt_siv(mptr, mlen, cptr, 8, cptr + 8, clen - 8, key) != 0) {
return E4ERR_InvalidTag;
}

// TODO: this is only valuable for string-type data
// we should consider removing it, as it requires that
// the plaintext buffer be 1 byte bigger than that which was
// encrypted, which is very unnecessary.
if (*mlen + 1 > mmax) // zero-pad it in place..
return E4ERR_TooShortCiphertext;
mptr[*mlen] = 0;
Expand Down
17 changes: 10 additions & 7 deletions test/e4.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ int main(int argc, char** argv, char** envp) {
size_t recovered_len = 0;
unsigned char plaintext_buffer[PT_MAX+1];
unsigned char ciphertext_buffer[PT_MAX + E4_MSGHDR_LEN + 1];
unsigned char recovered_buffer[PT_MAX + + 1];
unsigned char recovered_buffer[PT_MAX+1];
uint8_t topicindex = 0;

char* topicname;
Expand All @@ -132,10 +132,10 @@ int main(int argc, char** argv, char** envp) {
}
topicindex = topicindex % NUM_TOPICS;
topicname = topics[topicindex];
printf("Using topic: %s\n", topicname);
memset(plaintext_buffer, 0, sizeof plaintext_buffer);
memset(ciphertext_buffer, 0, sizeof ciphertext_buffer);
memset(recovered_buffer, 0, sizeof recovered_buffer);

memset(plaintext_buffer, 0, PT_MAX+1);
memset(ciphertext_buffer, 0, PT_MAX+E4_MSGHDR_LEN+1);
memset(recovered_buffer, 0, PT_MAX+1);

bytes_read = fread(plaintext_buffer, 1, PT_MAX, urand_fd);
if ( bytes_read < PT_MAX ) {
Expand All @@ -159,9 +159,12 @@ int main(int argc, char** argv, char** envp) {
goto exit_close;
}

e4retcode = e4c_unprotect_message(recovered_buffer, PT_MAX, &recovered_len,
/* e4c_unprotect_message zero-pads the output buffer. Perhaps we should
get rid of this functionality and leave it to the user. For now,
we fix it by passing the correct length of the recovered buffer. */
e4retcode = e4c_unprotect_message(recovered_buffer, PT_MAX+1, &recovered_len,
ciphertext_buffer, PT_MAX+E4_MSGHDR_LEN, topicname, &store);

if (e4retcode != E4ERR_Ok) {
returncode = 13;
printf("Failed: E4 Error %d\n", e4retcode);
Expand Down

0 comments on commit aecb3fb

Please sign in to comment.