main.c 3.57 KB
Newer Older
Enrico Pozzobon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include "crypto_aead.h"

#ifndef KEY_LENGTH
#define KEY_LENGTH 16
#endif

#ifndef NSEC_LENGTH
#define NSEC_LENGTH 0
#endif

#ifndef NPUB_LENGTH
#define NPUB_LENGTH 0
#endif

unsigned char *c = NULL;
unsigned long long clen = 0;
unsigned char *m = NULL;
unsigned long long mlen = 0;
unsigned char *ad = NULL;
unsigned long long adlen = 0;
unsigned char *nsec = NULL;

unsigned long long nslen = NSEC_LENGTH;
unsigned char *npub = NULL;
unsigned long long nplen = NPUB_LENGTH;
unsigned char *k = NULL;
unsigned long long klen = KEY_LENGTH;

static void read_variable(unsigned char **target, unsigned long long *lenp) {
    if (*target != NULL) {
        free(*target);
    }
    
    uint32_t len;
    if (1 != fread(&len, sizeof(len), 1, stdin)) {
        fprintf(stderr, "ERROR: couldn't read length\r\n");
        exit(1);
    }

    *lenp = len;
    if (*lenp == 0) {
        *target = NULL;
        return;
    }

    *target = malloc(*lenp);

    if (NULL == *target) {
        fprintf(stderr, "ERROR: couldn't malloc %llu bytes\r\n", *lenp);
        exit(2);
    }

    if (1 != fread(*target, *lenp, 1, stdin)) {
        fprintf(stderr, "ERROR: didn't read %llu bytes of data\r\n", *lenp);
        exit(1);
    }
}


static void write_variable(unsigned char *target, unsigned long long len) {
    uint32_t olen = len;
    if (1 != fwrite(&olen, sizeof(olen), 1, stdout)) {
        fprintf(stderr, "ERROR: didn't write length\r\n", len);
        exit(1);
    }
    if (1 != fwrite(target, olen, 1, stdout)) {
        fprintf(stderr, "ERROR: didn't write %llu bytes of data\r\n", len);
        exit(1);
    }
}


static bool is_ready() {
    return !(k == NULL || m == NULL || c == NULL
            || ad == NULL || nsec == NULL || npub == NULL);
}


int main() {
    int res;
    int8_t action;

    setvbuf(stdout, NULL, _IONBF, 0);
    printf("Hello, World!\n");
    while (1) {
        if (1 != fread(&action, sizeof(action), 1, stdin))
            return 1;

        switch (action) {
            case 'c': read_variable(&c, &clen); break;
            case 'm': read_variable(&m, &mlen); break;
            case 'a': read_variable(&ad, &adlen); break;
            case 'k': read_variable(&k, &klen); break;

            case 's': read_variable(&nsec, &nslen); break;
            case 'p': read_variable(&npub, &nplen); break;

            case 'C': write_variable(c, clen); break;
            case 'M': write_variable(m, mlen); break;
            case 'A': write_variable(ad, adlen); break;
            case 'K': write_variable(k, klen); break;
            case 'S': write_variable(nsec, nslen); break;
            case 'P': write_variable(npub, nplen); break;


            case 'e':
                if (!is_ready()) {
                    return 3;
                }
                fprintf(stderr, "Starting encryption\r\n");
                res = crypto_aead_encrypt(c, &clen,
                        m, mlen, ad, adlen, nsec, npub, k);
                fprintf(stderr, "encryption finished\r\n");
                break;

            case 'd':
                if (!is_ready()) {
                    return 3;
                }
                fprintf(stderr, "Starting decryption\r\n");
                res = crypto_aead_decrypt(m, &mlen,
                        nsec, c, clen, ad, adlen, npub, k);
                fprintf(stderr, "decryption finished\r\n");
                break;

            default:
                fprintf(stderr, "ERROR: unexpected action 0x%02x\r\n", action);
                return 2;
        }
        
    }

}