forked from sbp/tweetnacl-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetnacl-decrypt.c
57 lines (46 loc) · 1.83 KB
/
tweetnacl-decrypt.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tools.h"
#include "tweetnacl.h"
int main(int argc, char *argv[]) {
if (argc != 5) error(2,
"Usage: tweetnacl-decrypt send-key.pub recv-key.sec text.enc text.txt");
// This will also erroneously fail if the file "-" exists
if (file_exists(argv[4])) errorf(1, "File <%s> exists", argv[4]);
// Alice has sent to Bob, not surprisingly
unsigned char a_public_key[crypto_box_PUBLICKEYBYTES];
unsigned char b_secret_key[crypto_box_SECRETKEYBYTES];
read_key(argv[1], a_public_key, crypto_box_PUBLICKEYBYTES);
read_key(argv[2], b_secret_key, crypto_box_SECRETKEYBYTES);
unsigned char nonce[crypto_box_NONCEBYTES];
// Input
Content c = read_file(argv[3]);
memcpy(nonce, c.bytes, crypto_box_NONCEBYTES);
long esize = c.size - crypto_box_NONCEBYTES + crypto_box_BOXZEROBYTES;
unsigned char *encrypted = malloc(esize);
if (encrypted == NULL) error(1, "Malloc failed!");
memset(encrypted, 0, crypto_box_BOXZEROBYTES);
memcpy(encrypted + crypto_box_BOXZEROBYTES,
c.bytes + crypto_box_NONCEBYTES, c.size - crypto_box_NONCEBYTES);
// Equivalently, esize - crypto_box_BOXZEROBYTES
free(c.bytes);
// Output
unsigned char *message = calloc(esize, sizeof(unsigned char));
if (message == NULL) error(1, "Calloc failed!");
// Encrypt
crypto_box_open(message, encrypted, esize,
nonce, a_public_key, b_secret_key);
free(encrypted);
if (strcmp(argv[4], "-") != 0) {
FILE *out = create_file(argv[4]);
fwrite(message + crypto_box_ZEROBYTES,
esize - crypto_box_ZEROBYTES, 1, out);
fclose(out);
} else {
fwrite(message + crypto_box_ZEROBYTES,
esize - crypto_box_ZEROBYTES, 1, stdout);
}
free(message);
return 0;
}