Skip to content

Commit

Permalink
Fix error defines to not mix styles
Browse files Browse the repository at this point in the history
Previous error defines used E4ERR_SomeFailure which manages to mix
uppercase preprocessor CONSTANT_NAMING and CamelCase. Fix this for
consistency.

Moved the file API into e4.h, as it should belong there without
knowing the nature of the underlying storage.
  • Loading branch information
diagprov committed Jul 2, 2019
1 parent 0ad67b4 commit 09bf3c8
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 63 deletions.
51 changes: 36 additions & 15 deletions include/e4/e4.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,25 @@

/* E4 Library Error codes */
/** Operation succeeded without error \ingroup e4 */
#define E4ERR_Ok 0
#define E4_ERROR_OK 0
/** A control message was handled. Applications may discard the resulting buffer */
#define E4_ERROR_OK_CONTROL -1
/** Invalid authentication tag indicates corrupted ciphertext \ingroup e4 */
#define E4ERR_InvalidTag -101
#define E4_ERROR_INVALID_TAG -101
/** Message received outside of error window for clock. \ingroup e4 */
#define E4ERR_TimestampInFuture -102
#define E4_ERROR_TIMESTAMP_IN_FUTURE -102
/** Message received outside of error window for clock. \ingroup e4 */
#define E4ERR_TooOldTimestamp -103
#define E4_ERROR_TIMESTAMP_TOO_OLD -103
/** Ciphertext too short. \ingroup e4 */
#define E4ERR_TooShortCiphertext -104
#define E4_ERROR_CIPHERTEXT_TOO_SHORT -104
/** Unable to find key for topic; could not decrypt.\ingroup e4 */
#define E4ERR_TopicKeyMissing -105
#define E4_ERROR_TOPICKEY_MISSING -105
/** Message has already been seen. \ingroup e4 */
#define E4ERR_ReplayedMessage -106
#define E4_ERROR_MESSAGE_REPLAYED -106
/** E4 Protocol command invalid. Internal error. \ingroup e4 */
#define E4ERR_InvalidCommand -107
#define E4_ERROR_INVALID_COMMAND -107
/** E4 Persistence layer reported an error. \ingroup e4 */
#define E4ERR_PersistenceError -108
#define E4_ERROR_PERSISTENCE_ERROR -108

/** Size of the ID, truncated sha3(alias)
\ingroup e4
Expand Down Expand Up @@ -68,10 +70,10 @@ typedef struct _e4storage e4storage;
transmission \param[out] cptr Pointer to a ciphertext buffer \param[in] cmax
Maximum length of ciphertext buffer to be written. \param[out] clen Actual
length of ciphertext buffer written. \param[in] mptr Pointer to a message
(plaintext) buffer. \param[in] mlen Length of message to be encrypted. \param[in]
topic Pointer to a topic hash for topic associated with this message. \param[inout]
storage Pointer to the structure representing storage. \return 0 on success.
Non-zero return values indicate errors. \ingroup e4
(plaintext) buffer. \param[in] mlen Length of message to be encrypted.
\param[in] topic Pointer to a topic hash for topic associated with this
message. \param[inout] storage Pointer to the structure representing storage.
\return 0 on success. Non-zero return values indicate errors. \ingroup e4
*/
int e4c_protect_message (uint8_t *cptr,
size_t cmax,
Expand All @@ -89,8 +91,8 @@ was encrypted by E4.
\param[in] cptr Pointer to ciphertext buffer
\param[in] clen Length of ciphertext buffer
\param[in] topic Pointer to a topic hash for topic associated with this
message. \param[inout] storage Pointer to the structure representing storage. \return
0 on success. Non-zero return values indicate errors. \ingroup e4
message. \param[inout] storage Pointer to the structure representing storage.
\return 0 on success. Non-zero return values indicate errors. \ingroup e4
*/
int e4c_unprotect_message (uint8_t *mptr,
size_t mmax,
Expand All @@ -100,6 +102,25 @@ int e4c_unprotect_message (uint8_t *mptr,
const char *topic,
e4storage *storage);


/** the e4storage type pre-defined above implements these API calls */
int e4c_init (e4storage *store);
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);

//#ifdef DEBUG
void e4c_debug_print (e4storage *store);
//#endif

#ifdef E4_STORE_FILE
#include "e4/internal/e4c_store_file.h"
#endif
Expand Down
17 changes: 0 additions & 17 deletions include/e4/internal/e4c_store_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,4 @@ struct _e4storage
uint8_t ctrltopic[E4_TOPICHASH_LEN];
};

int e4c_init (e4storage *store);
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);

//#ifdef DEBUG
void e4c_debug_print (e4storage *store);
//#endif

#endif
20 changes: 10 additions & 10 deletions src/e4c_store_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ int e4c_load (e4storage *store, const char *path)
if (fd < 0)
{
perror (path);
return E4ERR_PersistenceError;
return E4_ERROR_PERSISTENCE_ERROR;
}

/*size_t filesize = */ lseek (fd, 0, SEEK_END);
Expand Down Expand Up @@ -146,7 +146,7 @@ int e4c_load (e4storage *store, const char *path)
err:
perror (path);
close (fd);
return E4ERR_PersistenceError;
return E4_ERROR_PERSISTENCE_ERROR;
}

int e4c_sync (e4storage *store)
Expand All @@ -156,14 +156,14 @@ int e4c_sync (e4storage *store)

if (strlen (store->filepath) == 0)
{
return E4ERR_PersistenceError;
return E4_ERROR_PERSISTENCE_ERROR;
}

fd = open (store->filepath, O_WRONLY | O_CREAT, 0600);
if (fd < 0)
{
perror (store->filepath);
return E4ERR_PersistenceError;
return E4_ERROR_PERSISTENCE_ERROR;
}

uint32_t zero = 0;
Expand Down Expand Up @@ -194,12 +194,12 @@ int e4c_sync (e4storage *store)
int e4c_set_id (e4storage *store, const uint8_t *id)
{
memmove (store->id, id, sizeof store->id);
return 0;
return E4_ERROR_OK;
}
int e4c_set_idkey (e4storage *store, const uint8_t *key)
{
memmove (store->key, key, sizeof store->key);
return 0;
return E4_ERROR_OK;
}

int e4c_getindex (e4storage *store, const char *topic)
Expand All @@ -217,7 +217,7 @@ int e4c_getindex (e4storage *store, const char *topic)
break;
}
}
if (i >= store->topiccount) return E4ERR_TopicKeyMissing;
if (i >= store->topiccount) return E4_ERROR_TOPICKEY_MISSING;

return i;
}
Expand All @@ -235,7 +235,7 @@ int e4c_is_device_ctrltopic (e4storage *store, const char *topic)
int e4c_gettopickey (uint8_t *key, e4storage *store, const int index)
{

if (index < 0 || index >= store->topiccount) return E4ERR_TopicKeyMissing;
if (index < 0 || index >= store->topiccount) return E4_ERROR_TOPICKEY_MISSING;

memcpy (key, store->topics[index].key, E4_KEY_LEN);

Expand All @@ -252,7 +252,7 @@ int e4c_set_topic_key (e4storage *store, const uint8_t *topic_hash, const uint8_
break;
}
if (i >= E4_TOPICS_MAX) // out of space
return E4ERR_TopicKeyMissing;
return E4_ERROR_TOPICKEY_MISSING;

memcpy (store->topics[i].topic, topic_hash, E4_TOPICHASH_LEN);
memcpy (store->topics[i].key, key, E4_KEY_LEN);
Expand Down Expand Up @@ -287,7 +287,7 @@ int e4c_remove_topic (e4storage *store, const uint8_t *topic_hash)
}
}

return E4ERR_TopicKeyMissing;
return E4_ERROR_TOPICKEY_MISSING;
}

int e4c_reset_topics (e4storage *store)
Expand Down
42 changes: 23 additions & 19 deletions src/e4client.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int e4c_protect_message (uint8_t *cptr,
uint64_t time_now = 0;

if (mlen + E4_MSGHDR_LEN > cmax) // actually: not enough space
return E4ERR_TooShortCiphertext;
return E4_ERROR_CIPHERTEXT_TOO_SHORT;
*clen = mlen + E4_MSGHDR_LEN;

// get the key
Expand All @@ -52,7 +52,7 @@ int e4c_protect_message (uint8_t *cptr,
{
if (e4c_is_device_ctrltopic (storage, topic) != 0)
{
return E4ERR_TopicKeyMissing;
return E4_ERROR_TOPICKEY_MISSING;
}
// control topic being used:
memcpy (key, storage->key, E4_KEY_LEN);
Expand Down Expand Up @@ -89,7 +89,7 @@ int e4c_unprotect_message (uint8_t *mptr,
e4storage *storage)
{
uint8_t control = 0;
int i = 0, j = 0;
int i = 0, j = 0, r = 0;
uint8_t key[E4_KEY_LEN];
uint64_t tstamp;
#ifndef __AVR__
Expand All @@ -102,7 +102,7 @@ int e4c_unprotect_message (uint8_t *mptr,

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

// get the key
Expand All @@ -115,7 +115,7 @@ int e4c_unprotect_message (uint8_t *mptr,
{
if (e4c_is_device_ctrltopic (storage, topic) != 0)
{
return E4ERR_TopicKeyMissing;
return E4_ERROR_TOPICKEY_MISSING;
}
// control topic being used:
memcpy (key, storage->key, E4_KEY_LEN);
Expand All @@ -133,15 +133,15 @@ int e4c_unprotect_message (uint8_t *mptr,
// decrypt
if (aes256_decrypt_siv (mptr, mlen, cptr, 8, cptr + 8, clen - 8, key) != 0)
{
return E4ERR_InvalidTag;
return E4_ERROR_INVALID_TAG;
}

// 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;
return E4_ERROR_CIPHERTEXT_TOO_SHORT;
mptr[*mlen] = 0;


Expand All @@ -157,39 +157,43 @@ int e4c_unprotect_message (uint8_t *mptr,
if (tstamp >= secs1970)
{
if (tstamp - secs1970 > E4C_TIME_FUTURE)
return E4ERR_TimestampInFuture;
return E4_ERROR_TIMESTAMP_IN_FUTURE;
}
else
{
if (secs1970 - tstamp > E4C_TIME_TOO_OLD)
return E4ERR_TooOldTimestamp;
return E4_ERROR_TIMESTAMP_TOO_OLD;
}
}

// if not control channel, we can exit now; no command to process.
if (!(control)) return 0;
if (!(control)) return E4_ERROR_OK;

// execute commands

if (*mlen == 0) return E4ERR_InvalidCommand;
if (*mlen == 0) return E4_ERROR_INVALID_COMMAND;

switch (mptr[0])
{
case 0x00: // RemoveTopic(topic);
return e4c_remove_topic (storage, (const uint8_t *)mptr + 1);
r = e4c_remove_topic (storage, (const uint8_t *)mptr + 1);
return r == 0 ? E4_ERROR_OK_CONTROL : r;

case 0x01: // ResetTopics();
if (*mlen != 1) return E4ERR_InvalidCommand;
return e4c_reset_topics (storage);
if (*mlen != 1) return E4_ERROR_INVALID_COMMAND;
r = e4c_reset_topics (storage);
return r == 0 ? E4_ERROR_OK_CONTROL : r;

case 0x02: // SetIdKey(key)
if (*mlen != (1 + E4_KEY_LEN)) return E4ERR_InvalidCommand;
return e4c_set_idkey (storage, mptr + 1);
if (*mlen != (1 + E4_KEY_LEN)) return E4_ERROR_INVALID_COMMAND;
r = e4c_set_idkey (storage, mptr + 1);
return r == 0 ? E4_ERROR_OK_CONTROL : r;

case 0x03: // SetTopicKey(topic, key)
if (*mlen != (1 + E4_KEY_LEN + E4_ID_LEN)) return E4ERR_InvalidCommand;
return e4c_set_topic_key (storage, (const uint8_t *)mptr + E4_KEY_LEN + 1, mptr + 1);
if (*mlen != (1 + E4_KEY_LEN + E4_ID_LEN)) return E4_ERROR_INVALID_COMMAND;
r = e4c_set_topic_key (storage, (const uint8_t *)mptr + E4_KEY_LEN + 1, mptr + 1);
return r == 0 ? E4_ERROR_OK_CONTROL : r;
}

return E4ERR_InvalidCommand;
return E4_ERROR_INVALID_COMMAND;
}
4 changes: 2 additions & 2 deletions test/e4.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ int main(int argc, char** argv, char** envp) {
e4retcode = e4c_protect_message(ciphertext_buffer, PT_MAX+E4_MSGHDR_LEN, &ciphertext_len,
plaintext_buffer, PT_MAX, topicname, &store);

if (e4retcode != E4ERR_Ok) {
if (e4retcode != E4_ERROR_OK) {
returncode = 12;
printf("Failed: E4 Error %d\n", e4retcode);
goto exit_close;
Expand All @@ -165,7 +165,7 @@ int main(int argc, char** argv, char** envp) {
e4retcode = e4c_unprotect_message(recovered_buffer, PT_MAX+1, &recovered_len,
ciphertext_buffer, PT_MAX+E4_MSGHDR_LEN, topicname, &store);

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

0 comments on commit 09bf3c8

Please sign in to comment.