1 /** Practical One-time Pad Library
12 #define MARKER_TO "to:"
13 #define MARKER_BEGIN "--EMOTP_BEGIN--"
14 #define MARKER_END "--EMOTP_END--"
16 #define OFFSET_FILE_EXTENSION ".off"
17 #define OFFSET_SIZE 11 /* strlen("4294967296") + 1 */
19 /** Packaged up encrypted message, ready for transport. */
20 typedef struct _PACKAGE
{
36 /** Show a list of all loaded pads. */
41 for (p
= pads
; p
; p
= p
->next
) {
42 printf("Pad: %s: %s\n", p
->name
, p
->local_filename
);
46 /** Open a companion offset file for given pad. Caller must close. */
47 FILE *open_offset_file(PAD
*p
)
50 char *offset_filename
;
51 size_t filename_length
;
53 /* Offset is stored in a separate file; pad plus OFFSET_FILE_NAME_EXTENSION. */
54 filename_length
= strlen(p
->local_filename
) + strlen(OFFSET_FILE_EXTENSION
) + 1;
55 offset_filename
= malloc(filename_length
);
56 if (!offset_filename
) {
60 snprintf(offset_filename
, filename_length
, "%s" OFFSET_FILE_EXTENSION
, p
->local_filename
);
62 /* Read offset from file. */
63 ofp
= fopen(offset_filename
, "rt");
65 fprintf(stderr
, "opening offset file %s failed\n", offset_filename
);
67 free(offset_filename
);
70 free(offset_filename
);
75 /** Read the pad offset of a given pad. */
76 unsigned long read_offset(PAD
*p
)
79 char buffer
[OFFSET_SIZE
];
82 ofp
= open_offset_file(p
);
84 memset(buffer
, 0, OFFSET_SIZE
);
85 if (fread(buffer
, 1, OFFSET_SIZE
- 1, ofp
) < 1) {
86 fprintf(stderr
, "could not read offset file for %s\n", p
->local_filename
);
92 /* We finally got it! */
93 offset
= atol(buffer
);
98 void write_offset(unsigned long offset
)
102 /** Load a pad file from disk, adding to 'pads' global. */
103 void load_pad(char *local_filename
)
108 fp
= fopen("/Volumes/Not Backed Up/otp/otp-dazzlement", "rb");
114 new_pad
= malloc(sizeof(PAD
));
117 exit(EX_UNAVAILABLE
);
120 new_pad
->local_filename
= strdup(local_filename
);
121 new_pad
->name
= strdup("dc"); /* TODO */
122 new_pad
->offset
= -1; /* TODO */
124 new_pad
->next
= NULL
;
126 /* Add to linked list. */
133 for (p
= pads
; p
; p
= p
->next
)
135 tail
->next
= new_pad
;
139 /** Close all pads and free allocated memory. */
144 for (p
= pads
; p
; p
= next
) {
146 free(p
->local_filename
);
155 load_pad("/Volumes/Not Backed Up/otp/otp-dazzlement");
156 load_pad("/Volumes/Not Backed Up/otp/otp-dazzlement");
157 load_pad("/Volumes/Not Backed Up/otp/otp-dazzlement");
159 printf("offset=%ld\n", read_offset(pads
));