Integrate pad rewriting into cli.
[easyotp.git] / cotp.c
blob0213020d633ad8929a63e3ad3c5ee09ec5f7f85e
1 /** Practical One-time Pad Library
3 * Created:20080514
4 * By Jeff Connelly
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <getopt.h>
11 #include <sysexits.h>
13 #include "libotp.h"
15 extern PAD *pads;
17 extern char *optarg;
18 extern int optind;
20 void test()
22 char *o;
23 unsigned int i;
25 load_config("otp.conf");
26 show_pads();
29 printf("offset=%ld\n", read_offset(pads));
30 write_offset(pads, 1213475);
31 printf("offset=%ld\n", read_offset(pads));
34 otp_decrypt("--EMOTP_BEGIN--1213434,\n"
35 "gK1O22FPbxLmxrROfFHDCsM1LTsOAjjbRlHVM1p+WG+s6yslYVfzvtc=\n"
36 "--EMOTP_END--\n", &o);
37 puts(o);
38 free(o);
40 otp_decrypt("--EMOTP_BEGIN--978,dc,\n"
41 "hUZm1q0gX7pa6Alzbo9OZiT8wA==\n"
42 "--EMOTP_END--\n", &o);
43 puts(o);
44 free(o);
46 o = otp_encrypt("hello world", strlen("hello world"), "dc", &i);
47 printf("encrypt = %s\n", o);
48 free(o);
50 otp_decrypt("junkasdsjdfldjf jdsfjunk--EMOTP_BEGIN--1213475,dc,\n"
51 "v7jlXbCCvPv4S0k=\n"
52 "--EMOTP_END--\n-tasjdktrailingjunkksjdjf", &o);
53 puts(o);
54 free(o);
56 free_pads();
59 /* Operation mode */
60 #define ENCRYPT -1
61 #define AUTO 0
62 #define DECRYPT 1
63 #define REPLACE 2
65 void usage()
67 fprintf(stderr, "usage: otp [-e | -d | ] [-t <pad_name>]\n"
68 "\n"
69 "Mode selection:\n"
70 "-e Encrypt\n"
71 "-d Decrypt\n"
72 "-r Replace what an existing encrypted message decrypts to\n"
73 "Default is automatic.\n"
74 "\n"
75 "-t Specify pad name to encrypt with. Default: first pad.\n");
76 exit(EX_USAGE);
79 /* Read input data.
81 * @param size Set to number of bytes read.
83 * @return Input, caller frees.
85 char *read_input(unsigned int *size)
87 unsigned int i, buffer_size;
88 char *input;
90 i = 0;
91 buffer_size = 1024;
92 input = malloc(buffer_size);
93 if (!input) {
94 perror("malloc input buffer");
95 exit(EX_UNAVAILABLE);
98 while(!feof(stdin)) {
99 input[i++] = fgetc(stdin);
100 if (input[i - 1] == EOF)
101 break;
102 if (i >= buffer_size) {
103 buffer_size += 1024;
104 input = realloc(input, buffer_size);
105 if (!input) {
106 perror("realloc input buffer");
107 exit(EX_UNAVAILABLE);
111 input[i - 1] = 0;
112 *size = i - 1;
114 return input;
117 int main(int argc, char **argv)
119 int ch, mode, i;
120 unsigned int length, output_length;
121 char *to, *input, *output;
123 mode = AUTO;
124 to = NULL;
126 while((ch = getopt(argc, argv, "erdt:")) != -1) {
127 switch(ch)
129 case 'e':
130 mode = ENCRYPT;
131 break;
132 case 'd':
133 mode = DECRYPT;
134 break;
135 case 't':
136 to = optarg;
137 break;
138 case 'r':
139 mode = REPLACE;
140 break;
141 case '?':
142 default:
143 usage();
146 argc -= optind;
147 argv += optind;
149 /* Automatic mode - guess based on magic markers.
150 * Explicit modes still allowed so can encrypt data with
151 * magic markers in it (if you ever want to). */
152 if (mode == AUTO) {
153 if (strstr(input, MARKER_BEGIN))
154 mode = DECRYPT;
155 else
156 mode = ENCRYPT;
159 load_config("otp.conf");
161 input = read_input(&length);
163 if (mode == ENCRYPT) {
164 output = otp_encrypt(input, length, to, &output_length);
165 } else if (mode == DECRYPT) {
166 output_length = otp_decrypt(input, &output);
167 } else if (mode == REPLACE) {
168 if (!strstr(input, MARKER_END)) {
169 fprintf(stderr, "need encrypted pad, trailed by new message\n");
170 exit(EXIT_FAILURE);
173 otp_replace(input, strstr(input, MARKER_END) + strlen(MARKER_END) + 1);
174 output_length = 0;
177 for (i = 0; i < output_length; ++i)
178 putchar(output[i]);
180 free(output);
181 free_pads();
183 return 0;