From 951fa45289c9072f90a005a3ef3476b1c89fe1ea Mon Sep 17 00:00:00 2001 From: Jose Marcial Vieira Bisneto Date: Mon, 31 Oct 2016 21:24:47 -0300 Subject: [PATCH] Created initial prototype. --- Makefile | 12 ++++++++++++ iprs.h | 12 ++++++++++++ sign.c | 21 +++++++++++++++++++++ sign.h | 8 ++++++++ validity.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ validity.h | 9 +++++++++ 6 files changed, 112 insertions(+) create mode 100644 Makefile create mode 100644 iprs.h create mode 100644 sign.c create mode 100644 sign.h create mode 100644 validity.c create mode 100644 validity.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ac8a731 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +CC = gcc +CFLAGS = -O2 -Wall -I. +DEPS = iprs_types.h sign.h validity.h +OBJECTS = sign.o validity.o + +%.o: %.c $(DEPS) + $(CC) $(CFLAGS) -c -o $@ $< + +all: $(OBJECTS) + +clean: + rm -f $(OBJECTS) diff --git a/iprs.h b/iprs.h new file mode 100644 index 0000000..94b0dcc --- /dev/null +++ b/iprs.h @@ -0,0 +1,12 @@ +#ifndef IPRS_H + #define IPRS_H + + #define TRUE 1 + #define FALSE 0 + + typedef struct Record_struct { + uint8_t* Value; + uint32_t Expires; + uint8_t* Signature; + } Record; +#endif /* IPRS_H */ diff --git a/sign.c b/sign.c new file mode 100644 index 0000000..d8f661e --- /dev/null +++ b/sign.c @@ -0,0 +1,21 @@ +#include +#include +#include "iprs.h" +#include "sign.h" + +uint8_t* signablePart(Record *r) { + uint8_t* sigbuf = NULL; + /* TODO */ + return sigbuf; +} + +uint8_t* authorKey_Sign(uint8_t *v) { + uint8_t* sign = NULL; + /* TODO */ + return sign; +} + +int authorKey_Verify(uint8_t *sign, uint8_t *value) { + /* TODO */ + return TRUE; +} diff --git a/sign.h b/sign.h new file mode 100644 index 0000000..72d69fa --- /dev/null +++ b/sign.h @@ -0,0 +1,8 @@ +#ifndef SIGN_H + #define SIGN_H + uint8_t* signablePart(Record *r); + + uint8_t* authorKey_Sign(uint8_t *v); + + int authorKey_Verify(uint8_t *sign, uint8_t *value); +#endif /* SIGN_H */ diff --git a/validity.c b/validity.c new file mode 100644 index 0000000..c90ca4c --- /dev/null +++ b/validity.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include "iprs.h" +#include "validity.h" +#include "sign.h" + +Record* MakeRecord(uint8_t* value, uint8_t* authorKey) { + struct timeval tv; + Record* r = calloc(1, sizeof(Record)); + + if (r) { + if (gettimeofday(&tv, NULL) != 0) { + // gettimeofday failed, cannot continue. + return NULL; + } + r->Value = value; + + /* establish an expiration date */ + r->Expires = tv.tv_sec + EXPIRATION_SECS; + + /* cryptographically sign the record */ + r->Signature = authorKey_Sign(signablePart(r)); + } + + return r; +} + +int VerifyRecord(Record* r, uint8_t* authorKey) { + /* always check the signature first */ + int sigok = authorKey_Verify(r->Signature, signablePart(r)); + struct timeval tv; + + if (!sigok || !r) { + return FALSE; /* sig did not check out! forged record? */ + } + + if (gettimeofday(&tv, NULL) != 0) { + // gettimeofday failed, cannot continue. + return FALSE; + } + + /* check the expiration. */ + if (r->Expires < tv.tv_sec) { + return FALSE; /* not valid anymore :( */ + } + + /* everything seems ok! */ + return TRUE; +} diff --git a/validity.h b/validity.h new file mode 100644 index 0000000..2b252f3 --- /dev/null +++ b/validity.h @@ -0,0 +1,9 @@ +#ifndef VALIDITY_H + #define VALIDITY_H + + #define EXPIRATION_SECS (24 * 60 * 60) /* Record valide for a day */ + + uint8_t* signablePart(Record *r); + Record* MakeRecord(uint8_t* value, uint8_t* authorKey); + int VerifyRecord(Record* rec, uint8_t* authorKey); +#endif /* VALIDITY_H */