#include "journal-authenticate.h"
#include "fsprg.h"
-static void *fsprg_state(JournalFile *f) {
- uint64_t a, b;
- assert(f);
-
- if (!f->authenticate)
- return NULL;
-
- a = le64toh(f->fsprg_header->header_size);
- b = le64toh(f->fsprg_header->state_size);
-
- if (a + b > f->fsprg_size)
- return NULL;
-
- return (uint8_t*) f->fsprg_header + a;
-}
-
static uint64_t journal_file_tag_seqnum(JournalFile *f) {
uint64_t r;
if (!f->hmac_running)
return 0;
- log_debug("Writing tag for epoch %llu\n", (unsigned long long) FSPRG_GetEpoch(fsprg_state(f)));
+ log_debug("Writing tag for epoch %llu\n", (unsigned long long) FSPRG_GetEpoch(f->fsprg_state));
assert(f->hmac);
/* Prepare HMAC for next cycle */
gcry_md_reset(f->hmac);
- FSPRG_GetKey(fsprg_state(f), key, sizeof(key), 0);
+ FSPRG_GetKey(f->fsprg_state, key, sizeof(key), 0);
gcry_md_setkey(f->hmac, key, sizeof(key));
f->hmac_running = true;
assert(epoch);
assert(f->authenticate);
- if (le64toh(f->fsprg_header->fsprg_start_usec) == 0 ||
- le64toh(f->fsprg_header->fsprg_interval_usec) == 0)
+ if (f->fsprg_start_usec == 0 ||
+ f->fsprg_interval_usec == 0)
return -ENOTSUP;
- if (realtime < le64toh(f->fsprg_header->fsprg_start_usec))
+ if (realtime < f->fsprg_start_usec)
return -ESTALE;
- t = realtime - le64toh(f->fsprg_header->fsprg_start_usec);
- t = t / le64toh(f->fsprg_header->fsprg_interval_usec);
+ t = realtime - f->fsprg_start_usec;
+ t = t / f->fsprg_interval_usec;
*epoch = t;
return 0;
if (r < 0)
return r;
- epoch = FSPRG_GetEpoch(fsprg_state(f));
+ epoch = FSPRG_GetEpoch(f->fsprg_state);
if (epoch > goal)
return -ESTALE;
if (r < 0)
return r;
- epoch = FSPRG_GetEpoch(fsprg_state(f));
+ epoch = FSPRG_GetEpoch(f->fsprg_state);
if (epoch < goal)
log_debug("Evolving FSPRG key from epoch %llu to %llu.", (unsigned long long) epoch, (unsigned long long) goal);
if (epoch == goal)
return 0;
- FSPRG_Evolve(fsprg_state(f));
- epoch = FSPRG_GetEpoch(fsprg_state(f));
+ FSPRG_Evolve(f->fsprg_state);
+ epoch = FSPRG_GetEpoch(f->fsprg_state);
}
}
goto finish;
}
- f->fsprg_size = le64toh(m->header_size) + le64toh(m->state_size);
- if ((uint64_t) st.st_size < f->fsprg_size) {
+ f->fsprg_file_size = le64toh(m->header_size) + le64toh(m->state_size);
+ if ((uint64_t) st.st_size < f->fsprg_file_size) {
r = -ENODATA;
goto finish;
}
goto finish;
}
- f->fsprg_header = mmap(NULL, PAGE_ALIGN(f->fsprg_size), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
- if (f->fsprg_header == MAP_FAILED) {
- f->fsprg_header = NULL;
+ f->fsprg_file = mmap(NULL, PAGE_ALIGN(f->fsprg_file_size), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+ if (f->fsprg_file == MAP_FAILED) {
+ f->fsprg_file = NULL;
r = -errno;
goto finish;
}
+ f->fsprg_start_usec = le64toh(f->fsprg_file->fsprg_start_usec);
+ f->fsprg_interval_usec = le64toh(f->fsprg_file->fsprg_interval_usec);
+
+ f->fsprg_state = (uint8_t*) f->fsprg_file + le64toh(f->fsprg_file->header_size);
+ f->fsprg_state_size = le64toh(f->fsprg_file->state_size);
+
r = 0;
finish:
#include "journal-verify.h"
#include "lookup3.h"
#include "compress.h"
+#include "fsprg.h"
/* FIXME:
*
return 0;
}
-int journal_file_verify(JournalFile *f, const char *key) {
+static int journal_file_parse_seed(JournalFile *f, const char *s) {
+ uint8_t *seed;
+ size_t seed_size, c;
+ const char *k;
+ int r;
+ unsigned long long start, interval;
+
+ seed_size = FSPRG_RECOMMENDED_SEEDLEN;
+ seed = malloc(seed_size);
+ if (!seed)
+ return -ENOMEM;
+
+ k = s;
+ for (c = 0; c < seed_size; c++) {
+ int x, y;
+
+ while (*k == '-')
+ k++;
+
+ x = unhexchar(*k);
+ if (x < 0) {
+ free(seed);
+ return -EINVAL;
+ }
+ k++;
+ y = unhexchar(*k);
+ if (y < 0) {
+ free(seed);
+ return -EINVAL;
+ }
+ k++;
+
+ seed[c] = (uint8_t) (x * 16 + y);
+ }
+
+ if (*k != '/') {
+ free(seed);
+ return -EINVAL;
+ }
+ k++;
+
+ r = sscanf(k, "%llx-%llx", &start, &interval);
+ if (r != 2) {
+ free(seed);
+ return -EINVAL;
+ }
+
+ f->fsprg_seed = seed;
+ f->fsprg_seed_size = seed_size;
+ f->fsprg_start_usec = start;
+ f->fsprg_interval_usec = interval;
+
+ return 0;
+}
+
+int journal_file_verify(JournalFile *f, const char *seed) {
int r;
Object *o;
uint64_t p = 0;
assert(f);
+ if (seed) {
+ r = journal_file_parse_seed(f, seed);
+ if (r < 0) {
+ log_error("Failed to parse seed.");
+ goto fail;
+ }
+ }
+
data_fd = mkostemp(data_path, O_CLOEXEC);
if (data_fd < 0) {
log_error("Failed to create data file: %m");