Created initial prototype.

This commit is contained in:
Jose Marcial Vieira Bisneto 2016-10-31 21:24:47 -03:00
parent 2e76850cd1
commit 951fa45289
6 changed files with 112 additions and 0 deletions

12
Makefile Normal file
View file

@ -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)

12
iprs.h Normal file
View file

@ -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 */

21
sign.c Normal file
View file

@ -0,0 +1,21 @@
#include <stdlib.h>
#include <stdint.h>
#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;
}

8
sign.h Normal file
View file

@ -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 */

50
validity.c Normal file
View file

@ -0,0 +1,50 @@
#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>
#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;
}

9
validity.h Normal file
View file

@ -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 */