main.c 4.41 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
#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

20 21
#define FPRINTF_HEX(fd, m, mlen) for (unsigned long long int i = 0; i < mlen; i++) fprintf(fd, "%02x ", m[i]);

Enrico Pozzobon committed
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
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;
47

Enrico Pozzobon committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    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)) {
70
        fprintf(stderr, "ERROR: didn't write length\r\n");
Enrico Pozzobon committed
71 72 73 74 75 76 77 78 79 80
        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() {
81
    return !(k == NULL || m == NULL || c == NULL || ad == NULL);
Enrico Pozzobon committed
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
}


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;
                }
116 117 118 119 120 121 122 123 124 125 126
                fprintf(stderr, "mlen = %llu\r\n", mlen);
                fprintf(stderr, "m = "); FPRINTF_HEX(stderr, m, mlen); fprintf(stderr, "\r\n");
                fprintf(stderr, "adlen = %llu\r\n", adlen);
                fprintf(stderr, "ad = "); FPRINTF_HEX(stderr, ad, adlen); fprintf(stderr, "\r\n");
                fprintf(stderr, "nplen = %llu\r\n", nplen);
                fprintf(stderr, "np = "); FPRINTF_HEX(stderr, npub, nplen); fprintf(stderr, "\r\n");
                fprintf(stderr, "nslen = %llu\r\n", nslen);
                fprintf(stderr, "ns = "); FPRINTF_HEX(stderr, nsec, nslen); fprintf(stderr, "\r\n");
                fprintf(stderr, "klen = %llu\r\n", klen);
                fprintf(stderr, "k = "); FPRINTF_HEX(stderr, k, klen); fprintf(stderr, "\r\n");

Enrico Pozzobon committed
127 128 129
                fprintf(stderr, "Starting encryption\r\n");
                res = crypto_aead_encrypt(c, &clen,
                        m, mlen, ad, adlen, nsec, npub, k);
130
                fprintf(stderr, "encryption finished %d\r\n", res);
Enrico Pozzobon committed
131 132 133 134 135 136 137 138 139
                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);
140
                fprintf(stderr, "decryption finished %d\r\n", res);
Enrico Pozzobon committed
141 142 143 144 145 146 147 148 149 150 151
                break;

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

}