main.c 4.6 KB
Newer Older
Enrico Pozzobon committed
1 2 3 4 5 6
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include "crypto_aead.h"
Enrico Pozzobon committed
7
#include "api.h"
Enrico Pozzobon committed
8 9 10 11 12 13 14 15 16 17 18 19 20

#ifndef KEY_LENGTH
#define KEY_LENGTH 16
#endif

#ifndef NSEC_LENGTH
#define NSEC_LENGTH 0
#endif

#ifndef NPUB_LENGTH
#define NPUB_LENGTH 0
#endif

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

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

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

    if (len == 0) {
        return;
    }

Enrico Pozzobon committed
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
    if (1 != fwrite(target, olen, 1, stdout)) {
        fprintf(stderr, "ERROR: didn't write %llu bytes of data\r\n", len);
        exit(1);
    }
}


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':
Enrico Pozzobon committed
114 115
                if (k == NULL || c == NULL) {
                    fprintf(stderr, c == NULL ? "Missing ciphertext buffer\r\n" : "Missing key\r\n");
Enrico Pozzobon committed
116 117
                    return 3;
                }
118 119 120 121 122 123 124 125 126 127 128
                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
129 130 131
                fprintf(stderr, "Starting encryption\r\n");
                res = crypto_aead_encrypt(c, &clen,
                        m, mlen, ad, adlen, nsec, npub, k);
132
                fprintf(stderr, "encryption finished %d\r\n", res);
Enrico Pozzobon committed
133 134 135
                break;

            case 'd':
Enrico Pozzobon committed
136 137
                if (m == NULL || k == NULL) {
                    fprintf(stderr, m == NULL ? "Missing message buffer\r\n" : "Missing key\r\n");
Enrico Pozzobon committed
138 139
                    return 3;
                }
Enrico Pozzobon committed
140
                fprintf(stderr, "starting decryption\r\n");
Enrico Pozzobon committed
141 142
                res = crypto_aead_decrypt(m, &mlen,
                        nsec, c, clen, ad, adlen, npub, k);
143
                fprintf(stderr, "decryption finished %d\r\n", res);
Enrico Pozzobon committed
144 145 146 147 148 149 150 151 152 153 154
                break;

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

}