From 30c2e6bdbeef4bd4dbabf06cf359bd8578af4628 Mon Sep 17 00:00:00 2001 From: Jeff Connelly Date: Tue, 10 Jun 2008 22:50:42 -0700 Subject: [PATCH] Add pad rewriting, in otp_replace. --- SecureMail.py | 5 +++-- cotp.c | 28 +++++++++++++++++++++------- libotp.c | 39 ++++++++++++++++++++++++++++++++++++++- libotp.h | 1 + 4 files changed, 63 insertions(+), 10 deletions(-) diff --git a/SecureMail.py b/SecureMail.py index 06721c7..04d4938 100644 --- a/SecureMail.py +++ b/SecureMail.py @@ -15,7 +15,8 @@ import time # Every X seconds, send a message, and queue messages to be sent at in this # interval. -FILL_INTERVAL = 10 +#FILL_INTERVAL = 10 +FILL_INTERVAL = None # Real subject used for all encrypted messages # Note: make a Gmail filter that filters (Secure Message) into the Secure tag @@ -178,7 +179,7 @@ class SecureMail(threading.Thread): if channel filling is disabled.""" if FILL_INTERVAL is None: print "Sending %s bytes now" % (len(body,)) - return self.send(to, subject, body) + return self.send_now(to, subject, body) else: self.sendq.put((to, subject, body)) print "Enqueued to send at next interval, pending: %s" % (self.sendq.qsize(),) diff --git a/cotp.c b/cotp.c index 3c5b3b7..0213020 100644 --- a/cotp.c +++ b/cotp.c @@ -56,10 +56,11 @@ void test() free_pads(); } -/* Operation mode - trinary :) */ +/* Operation mode */ #define ENCRYPT -1 #define AUTO 0 #define DECRYPT 1 +#define REPLACE 2 void usage() { @@ -67,7 +68,8 @@ void usage() "\n" "Mode selection:\n" "-e Encrypt\n" - "-d Decrypt with \n" + "-d Decrypt\n" + "-r Replace what an existing encrypted message decrypts to\n" "Default is automatic.\n" "\n" "-t Specify pad name to encrypt with. Default: first pad.\n"); @@ -114,14 +116,14 @@ char *read_input(unsigned int *size) int main(int argc, char **argv) { - int ch, mode; + int ch, mode, i; unsigned int length, output_length; char *to, *input, *output; mode = AUTO; to = NULL; - while((ch = getopt(argc, argv, "edt:")) != -1) { + while((ch = getopt(argc, argv, "erdt:")) != -1) { switch(ch) { case 'e': @@ -133,6 +135,9 @@ int main(int argc, char **argv) case 't': to = optarg; break; + case 'r': + mode = REPLACE; + break; case '?': default: usage(); @@ -141,8 +146,6 @@ int main(int argc, char **argv) argc -= optind; argv += optind; - input = read_input(&length); - /* Automatic mode - guess based on magic markers. * Explicit modes still allowed so can encrypt data with * magic markers in it (if you ever want to). */ @@ -155,13 +158,24 @@ int main(int argc, char **argv) load_config("otp.conf"); + input = read_input(&length); + if (mode == ENCRYPT) { output = otp_encrypt(input, length, to, &output_length); } else if (mode == DECRYPT) { output_length = otp_decrypt(input, &output); + } else if (mode == REPLACE) { + if (!strstr(input, MARKER_END)) { + fprintf(stderr, "need encrypted pad, trailed by new message\n"); + exit(EXIT_FAILURE); + } + + otp_replace(input, strstr(input, MARKER_END) + strlen(MARKER_END) + 1); + output_length = 0; } - printf("%s", output); + for (i = 0; i < output_length; ++i) + putchar(output[i]); free(output); free_pads(); diff --git a/libotp.c b/libotp.c index f7f63e3..defe403 100644 --- a/libotp.c +++ b/libotp.c @@ -114,7 +114,7 @@ void load_pad(char *local_filename, char *pad_name) FILE *fp; PAD *new_pad; - fp = fopen("/Volumes/Not Backed Up/otp/otp-dazzlement", "rb"); + fp = fopen(local_filename, "rb+"); if (!fp) { perror("fopen"); exit(EXIT_FAILURE); @@ -459,6 +459,43 @@ unsigned int otp_decrypt(char *input, char **out) return length; } +/** Replace part of the pad corresponding to an encrypted message + * so that it encrypts to something else. + * + * @param input A packaged, encrypted message. + * @param with What to make 'input' decrypt to by changing the pad. + */ +unsigned int otp_replace(char *input, char *with) +{ + unsigned int length; + int i; + char c; + + MESSAGE *msg; + msg = unpackage(input); + + if (fseek(msg->pad->fp, msg->offset, SEEK_SET) < 0) { + perror("fseek"); + exit(EXIT_FAILURE); + } + + for (i = 0; i < msg->length; ++i) { + char with_c; + + /* What to make the message decrypt to. */ + if (i < strlen(with)) + with_c = with[i]; + else + with_c = '\0'; + + c = msg->cipher_text[i] ^ with_c; + + fputc(c, msg->pad->fp); + } + + return length; +} + /** Package up a message for transport. * * @return Packaged message. Caller frees. diff --git a/libotp.h b/libotp.h index 86d64a3..89a0b92 100644 --- a/libotp.h +++ b/libotp.h @@ -53,4 +53,5 @@ MESSAGE *unpackage(char *input); void free_message(MESSAGE *); char *otp_encrypt(char *input, unsigned int length, char *to, unsigned int *out_length); unsigned int otp_decrypt(char *input, char **out); +unsigned int otp_replace(char *input, char *with); -- 2.11.4.GIT