From 5476ce31d628a871bc88f9ef018cbebc05b3aa85 Mon Sep 17 00:00:00 2001 From: Jeff Connelly Date: Thu, 15 May 2008 13:16:05 -0700 Subject: [PATCH] Output packaging works. --- libotp.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- libotp.h | 3 +++ try.c | 6 +++++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/libotp.c b/libotp.c index 5f4fb73..804b2ba 100644 --- a/libotp.c +++ b/libotp.c @@ -433,7 +433,51 @@ unsigned int otp_decrypt(char *input, char **out) return length; } -char *otp_encrypt(char *input) +/** Encrypt and package a message. + * + * @return Encrypted, packaged message. Caller must free. + */ +char *otp_encrypt(char *input, unsigned int length, char *to) { - return NULL; + PAD *p; + char *out; + unsigned int out_length; + unsigned long offset; + char *b64_data; + + p = find_pad(to); + if (!p) { + fprintf(stderr, "otp_encrypt: no such pad %s\n", to); + exit(EX_DATAERR); + } + + /* Packaged messages are base64'd encoded so they take + * 4/3 as much space, plus a fixed amount for markers, plus + * up to OFFSET_SIZE bytes for the offset. To avoid possible + * rounding errors in calculating the size, just multiply by 2. + * This overestimates and should be more than enough space. + */ + out_length = strlen(MARKER_BEGIN) + 1 + OFFSET_SIZE + 1 + + 1 + length * 2 + 1 + strlen(MARKER_END) + 1; + out = malloc(out_length); + if (!out) { + perror("malloc otp_decrypt output"); + exit(EX_DATAERR); + } + + /* Use current offset. */ + offset = read_offset(p); + + b64_data = "(todo)"; + + snprintf(out, out_length, "%s%d,%s,\n" + "%s\n" + "%s\n", + MARKER_BEGIN, + offset, + to, + b64_data, + MARKER_END); + + return out; } diff --git a/libotp.h b/libotp.h index 69a49ad..72720a8 100644 --- a/libotp.h +++ b/libotp.h @@ -33,6 +33,7 @@ typedef struct _MESSAGE { char *cipher_text; } MESSAGE; +void load_config(char *config_filename); void show_pads(); FILE *open_offset_file(PAD *p, char *mode); unsigned long read_offset(PAD *p); @@ -41,4 +42,6 @@ void load_pad(char *local_filename, char *pad_name); void free_pads(); MESSAGE *unpackage(char *input); void free_message(MESSAGE *); +char *otp_encrypt(char *input, unsigned int length, char *to); unsigned int otp_decrypt(char *input, char **out); + diff --git a/try.c b/try.c index e72cff5..e8d172d 100644 --- a/try.c +++ b/try.c @@ -6,6 +6,7 @@ #include #include +#include #include "libotp.h" extern PAD *pads; @@ -15,6 +16,7 @@ int main() char *o; load_config("otp.conf"); + show_pads(); printf("offset=%ld\n", read_offset(pads)); write_offset(pads, 1213475); @@ -32,7 +34,9 @@ int main() puts(o); free(o); - show_pads(); + o = otp_encrypt("hello world", strlen("hello world"), "dc"); + printf("encrypt = %s\n", o); + free_pads(); return 0; -- 2.11.4.GIT