tweetnacl crypto

Dependencies:   mbed

Committer:
manitou
Date:
Tue Feb 27 00:33:36 2018 +0000
Revision:
0:bed9ac8a7beb
tweetnacl  crypto

Who changed what in which revision?

UserRevisionLine numberNew contents of line
manitou 0:bed9ac8a7beb 1 // https://github.com/rjw245/crypto-in-a-box
manitou 0:bed9ac8a7beb 2 #include "mbed.h"
manitou 0:bed9ac8a7beb 3 #include "tweetnacl.h"
manitou 0:bed9ac8a7beb 4
manitou 0:bed9ac8a7beb 5 // standalone test
manitou 0:bed9ac8a7beb 6 static const uint8_t secretkey[crypto_secretbox_KEYBYTES] = {
manitou 0:bed9ac8a7beb 7 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
manitou 0:bed9ac8a7beb 8 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
manitou 0:bed9ac8a7beb 9 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
manitou 0:bed9ac8a7beb 10 0, 1
manitou 0:bed9ac8a7beb 11 };
manitou 0:bed9ac8a7beb 12
manitou 0:bed9ac8a7beb 13 static const uint8_t nonce[crypto_secretbox_NONCEBYTES] = {
manitou 0:bed9ac8a7beb 14 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
manitou 0:bed9ac8a7beb 15 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
manitou 0:bed9ac8a7beb 16 0, 1, 2, 3
manitou 0:bed9ac8a7beb 17 };
manitou 0:bed9ac8a7beb 18
manitou 0:bed9ac8a7beb 19 // Need crypto_secretbox_ZEROBYTES of room at the beginning
manitou 0:bed9ac8a7beb 20 // AND crypto_secretbox_BOXZEROBYTES at the end.
manitou 0:bed9ac8a7beb 21 // The crypto_secretbox_BOXZEROBYTES allows an encrypted input to fit
manitou 0:bed9ac8a7beb 22 // in the buffer when storage STARTS at index [crypto_secretbox_ZEROBYTES]
manitou 0:bed9ac8a7beb 23 #define BUFFER_SIZE (256 + \
manitou 0:bed9ac8a7beb 24 crypto_secretbox_ZEROBYTES + \
manitou 0:bed9ac8a7beb 25 crypto_secretbox_BOXZEROBYTES)
manitou 0:bed9ac8a7beb 26
manitou 0:bed9ac8a7beb 27 static unsigned char input_buffer[BUFFER_SIZE], outdata[BUFFER_SIZE];
manitou 0:bed9ac8a7beb 28 static uint8_t input_len = 0;
manitou 0:bed9ac8a7beb 29
manitou 0:bed9ac8a7beb 30 static const uint8_t msg[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
manitou 0:bed9ac8a7beb 31
manitou 0:bed9ac8a7beb 32
manitou 0:bed9ac8a7beb 33
manitou 0:bed9ac8a7beb 34 Timer tmr;
manitou 0:bed9ac8a7beb 35
manitou 0:bed9ac8a7beb 36 main() {
manitou 0:bed9ac8a7beb 37 int r;
manitou 0:bed9ac8a7beb 38 uint32_t us;
manitou 0:bed9ac8a7beb 39
manitou 0:bed9ac8a7beb 40 tmr.start();
manitou 0:bed9ac8a7beb 41 memset(input_buffer, 0, sizeof(input_buffer));
manitou 0:bed9ac8a7beb 42 input_len = sizeof(msg);
manitou 0:bed9ac8a7beb 43 memcpy(input_buffer + crypto_secretbox_ZEROBYTES, msg, input_len);
manitou 0:bed9ac8a7beb 44 // encrypt
manitou 0:bed9ac8a7beb 45 us = tmr.read_us();
manitou 0:bed9ac8a7beb 46 r = crypto_secretbox(outdata,
manitou 0:bed9ac8a7beb 47 input_buffer,
manitou 0:bed9ac8a7beb 48 input_len + crypto_secretbox_ZEROBYTES,
manitou 0:bed9ac8a7beb 49 nonce,
manitou 0:bed9ac8a7beb 50 secretkey);
manitou 0:bed9ac8a7beb 51 us = tmr.read_us() - us;
manitou 0:bed9ac8a7beb 52 printf("r= %d %d us\n",r,us);
manitou 0:bed9ac8a7beb 53
manitou 0:bed9ac8a7beb 54
manitou 0:bed9ac8a7beb 55 //decrypt
manitou 0:bed9ac8a7beb 56 memset(input_buffer, 0, sizeof(input_buffer));
manitou 0:bed9ac8a7beb 57 us = tmr.read_us();
manitou 0:bed9ac8a7beb 58 r = crypto_secretbox_open(input_buffer,
manitou 0:bed9ac8a7beb 59 outdata,
manitou 0:bed9ac8a7beb 60 input_len + crypto_secretbox_ZEROBYTES,
manitou 0:bed9ac8a7beb 61 nonce,
manitou 0:bed9ac8a7beb 62 secretkey);
manitou 0:bed9ac8a7beb 63 us = tmr.read_us() - us;
manitou 0:bed9ac8a7beb 64 printf("r= %d %d us\n",r,us);
manitou 0:bed9ac8a7beb 65 for (int i = 0; i < 4; i++) printf("%d ",input_buffer[crypto_secretbox_ZEROBYTES + i]);
manitou 0:bed9ac8a7beb 66 printf("\n");
manitou 0:bed9ac8a7beb 67 r = memcmp(msg, input_buffer + crypto_secretbox_ZEROBYTES, input_len);
manitou 0:bed9ac8a7beb 68 printf("result %d\n",r);
manitou 0:bed9ac8a7beb 69 }