main.c 4.96 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
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;

37
static void recv_variable(unsigned char **target, unsigned long long *lenp) {
Enrico Pozzobon committed
38 39 40 41 42 43 44 45 46 47
    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

49
    *target = malloc(len);
Enrico Pozzobon committed
50 51

    if (NULL == *target) {
52
        fprintf(stderr, "ERROR: couldn't malloc %u bytes\r\n", len);
Enrico Pozzobon committed
53 54 55
        exit(2);
    }

56 57 58 59
    if (*lenp == 0) {
        return;
    }

Enrico Pozzobon committed
60
    if (1 != fread(*target, *lenp, 1, stdin)) {
61
        fprintf(stderr, "ERROR: didn't read %u bytes of data\r\n", len);
Enrico Pozzobon committed
62 63 64 65 66
        exit(1);
    }
}


67
static void send_variable(unsigned char *target, unsigned long long len) {
Enrico Pozzobon committed
68 69
    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
        exit(1);
    }
Enrico Pozzobon committed
73 74 75 76 77

    if (len == 0) {
        return;
    }

Enrico Pozzobon committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    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;
94
        fprintf(stderr, "DEBUG: received action 0x%02x\r\n", action);
Enrico Pozzobon committed
95 96

        switch (action) {
97 98 99 100
            case 'c': recv_variable(&c, &clen); break;
            case 'm': recv_variable(&m, &mlen); break;
            case 'a': recv_variable(&ad, &adlen); break;
            case 'k': recv_variable(&k, &klen); break;
Enrico Pozzobon committed
101

102 103
            case 's': recv_variable(&nsec, &nslen); break;
            case 'p': recv_variable(&npub, &nplen); break;
Enrico Pozzobon committed
104

105 106 107 108 109 110
            case 'C': send_variable(c, clen); break;
            case 'M': send_variable(m, mlen); break;
            case 'A': send_variable(ad, adlen); break;
            case 'K': send_variable(k, klen); break;
            case 'S': send_variable(nsec, nslen); break;
            case 'P': send_variable(npub, nplen); break;
Enrico Pozzobon committed
111 112 113


            case 'e':
114 115
            case 'd':
/*
116 117 118 119 120 121 122 123 124 125
                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");
126
*/
127

128 129 130 131
                if (k == NULL) {
                    fprintf(stderr, "Missing key\r\n");
                    return 3;
                }
132

133 134
                if (npub == NULL) {
                    fprintf(stderr, "Missing nonce\r\n");
Enrico Pozzobon committed
135 136
                    return 3;
                }
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

                if (action == 'e') {

                    if (c == NULL) {
                        fprintf(stderr, "Missing ciphertext buffer\r\n");
                        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 %d\r\n", res);

                } else if (action == 'd') {

                    if (m == NULL) {
                        fprintf(stderr, "Missing message buffer\r\n");
                        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 %d\r\n", res);

                }
Enrico Pozzobon committed
163 164 165 166 167 168 169 170 171 172 173
                break;

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

}