pc1crypt.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published
  6. * by the Free Software Foundation.
  7. *
  8. * This code was based on:
  9. * PC1 Cipher Algorithm ( Pukall Cipher 1 )
  10. * By Alexander PUKALL 1991
  11. * free code no restriction to use
  12. * please include the name of the Author in the final software
  13. * the Key is 128 bits
  14. * http://membres.lycos.fr/pc1/
  15. *
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <unistd.h> /* for unlink() */
  22. #include <libgen.h>
  23. #include <getopt.h> /* for getopt() */
  24. #include <stdarg.h>
  25. #include <errno.h>
  26. #include <sys/stat.h>
  27. struct pc1_ctx {
  28. unsigned short ax;
  29. unsigned short bx;
  30. unsigned short cx;
  31. unsigned short dx;
  32. unsigned short si;
  33. unsigned short tmp;
  34. unsigned short x1a2;
  35. unsigned short x1a0[8];
  36. unsigned short res;
  37. unsigned short i;
  38. unsigned short inter;
  39. unsigned short cfc;
  40. unsigned short cfd;
  41. unsigned short compte;
  42. unsigned char cle[17];
  43. short c;
  44. };
  45. static void pc1_finish(struct pc1_ctx *pc1)
  46. {
  47. /* erase all variables */
  48. memset(pc1, 0, sizeof(struct pc1_ctx));
  49. }
  50. static void pc1_code(struct pc1_ctx *pc1)
  51. {
  52. pc1->dx = pc1->x1a2 + pc1->i;
  53. pc1->ax = pc1->x1a0[pc1->i];
  54. pc1->cx = 0x015a;
  55. pc1->bx = 0x4e35;
  56. pc1->tmp = pc1->ax;
  57. pc1->ax = pc1->si;
  58. pc1->si = pc1->tmp;
  59. pc1->tmp = pc1->ax;
  60. pc1->ax = pc1->dx;
  61. pc1->dx = pc1->tmp;
  62. if (pc1->ax != 0) {
  63. pc1->ax = pc1->ax * pc1->bx;
  64. }
  65. pc1->tmp = pc1->ax;
  66. pc1->ax = pc1->cx;
  67. pc1->cx = pc1->tmp;
  68. if (pc1->ax != 0) {
  69. pc1->ax = pc1->ax * pc1->si;
  70. pc1->cx = pc1->ax + pc1->cx;
  71. }
  72. pc1->tmp = pc1->ax;
  73. pc1->ax = pc1->si;
  74. pc1->si = pc1->tmp;
  75. pc1->ax = pc1->ax * pc1->bx;
  76. pc1->dx = pc1->cx + pc1->dx;
  77. pc1->ax = pc1->ax + 1;
  78. pc1->x1a2 = pc1->dx;
  79. pc1->x1a0[pc1->i] = pc1->ax;
  80. pc1->res = pc1->ax ^ pc1->dx;
  81. pc1->i = pc1->i + 1;
  82. }
  83. static void pc1_assemble(struct pc1_ctx *pc1)
  84. {
  85. pc1->x1a0[0] = (pc1->cle[0] * 256) + pc1->cle[1];
  86. pc1_code(pc1);
  87. pc1->inter = pc1->res;
  88. pc1->x1a0[1] = pc1->x1a0[0] ^ ((pc1->cle[2]*256) + pc1->cle[3]);
  89. pc1_code(pc1);
  90. pc1->inter = pc1->inter ^ pc1->res;
  91. pc1->x1a0[2] = pc1->x1a0[1] ^ ((pc1->cle[4]*256) + pc1->cle[5]);
  92. pc1_code(pc1);
  93. pc1->inter = pc1->inter ^ pc1->res;
  94. pc1->x1a0[3] = pc1->x1a0[2] ^ ((pc1->cle[6]*256) + pc1->cle[7]);
  95. pc1_code(pc1);
  96. pc1->inter = pc1->inter ^ pc1->res;
  97. pc1->x1a0[4] = pc1->x1a0[3] ^ ((pc1->cle[8]*256) + pc1->cle[9]);
  98. pc1_code(pc1);
  99. pc1->inter = pc1->inter ^ pc1->res;
  100. pc1->x1a0[5] = pc1->x1a0[4] ^ ((pc1->cle[10]*256) + pc1->cle[11]);
  101. pc1_code(pc1);
  102. pc1->inter = pc1->inter ^ pc1->res;
  103. pc1->x1a0[6] = pc1->x1a0[5] ^ ((pc1->cle[12]*256) + pc1->cle[13]);
  104. pc1_code(pc1);
  105. pc1->inter = pc1->inter ^ pc1->res;
  106. pc1->x1a0[7] = pc1->x1a0[6] ^ ((pc1->cle[14]*256) + pc1->cle[15]);
  107. pc1_code(pc1);
  108. pc1->inter = pc1->inter ^ pc1->res;
  109. pc1->i = 0;
  110. }
  111. static unsigned char pc1_decrypt(struct pc1_ctx *pc1, short c)
  112. {
  113. pc1_assemble(pc1);
  114. pc1->cfc = pc1->inter >> 8;
  115. pc1->cfd = pc1->inter & 255; /* cfc^cfd = random byte */
  116. c = c ^ (pc1->cfc ^ pc1->cfd);
  117. for (pc1->compte = 0; pc1->compte <= 15; pc1->compte++) {
  118. /* we mix the plaintext byte with the key */
  119. pc1->cle[pc1->compte] = pc1->cle[pc1->compte] ^ c;
  120. }
  121. return c;
  122. }
  123. static unsigned char pc1_encrypt(struct pc1_ctx *pc1, short c)
  124. {
  125. pc1_assemble(pc1);
  126. pc1->cfc = pc1->inter >> 8;
  127. pc1->cfd = pc1->inter & 255; /* cfc^cfd = random byte */
  128. for (pc1->compte = 0; pc1->compte <= 15; pc1->compte++) {
  129. /* we mix the plaintext byte with the key */
  130. pc1->cle[pc1->compte] = pc1->cle[pc1->compte] ^ c;
  131. }
  132. c = c ^ (pc1->cfc ^ pc1->cfd);
  133. return c;
  134. }
  135. static void pc1_init(struct pc1_ctx *pc1)
  136. {
  137. memset(pc1, 0, sizeof(struct pc1_ctx));
  138. /* ('Remsaalps!123456') is the key used, you can change it */
  139. strcpy(pc1->cle, "Remsaalps!123456");
  140. }
  141. static void pc1_decrypt_buf(struct pc1_ctx *pc1, unsigned char *buf,
  142. unsigned len)
  143. {
  144. unsigned i;
  145. for (i = 0; i < len; i++)
  146. buf[i] = pc1_decrypt(pc1, buf[i]);
  147. }
  148. static void pc1_encrypt_buf(struct pc1_ctx *pc1, unsigned char *buf,
  149. unsigned len)
  150. {
  151. unsigned i;
  152. for (i = 0; i < len; i++)
  153. buf[i] = pc1_encrypt(pc1, buf[i]);
  154. }
  155. /*
  156. * Globals
  157. */
  158. static char *ifname;
  159. static char *progname;
  160. static char *ofname;
  161. static int decrypt;
  162. /*
  163. * Message macros
  164. */
  165. #define ERR(fmt, ...) do { \
  166. fflush(0); \
  167. fprintf(stderr, "[%s] *** error: " fmt "\n", \
  168. progname, ## __VA_ARGS__ ); \
  169. } while (0)
  170. #define ERRS(fmt, ...) do { \
  171. int save = errno; \
  172. fflush(0); \
  173. fprintf(stderr, "[%s] *** error: " fmt "\n", \
  174. progname, ## __VA_ARGS__, strerror(save)); \
  175. } while (0)
  176. void usage(int status)
  177. {
  178. FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
  179. struct board_info *board;
  180. fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
  181. fprintf(stream,
  182. "\n"
  183. "Options:\n"
  184. " -d decrypt instead of encrypt"
  185. " -i <file> read input from the file <file>\n"
  186. " -o <file> write output to the file <file>\n"
  187. " -h show this screen\n"
  188. );
  189. exit(status);
  190. }
  191. #define BUFSIZE (64 * 1024)
  192. int main(int argc, char *argv[])
  193. {
  194. struct pc1_ctx pc1;
  195. int res = EXIT_FAILURE;
  196. int err;
  197. struct stat st;
  198. char *buf;
  199. unsigned total;
  200. FILE *outfile, *infile;
  201. progname = basename(argv[0]);
  202. while ( 1 ) {
  203. int c;
  204. c = getopt(argc, argv, "di:o:h");
  205. if (c == -1)
  206. break;
  207. switch (c) {
  208. case 'd':
  209. decrypt = 1;
  210. break;
  211. case 'i':
  212. ifname = optarg;
  213. break;
  214. case 'o':
  215. ofname = optarg;
  216. break;
  217. case 'h':
  218. usage(EXIT_SUCCESS);
  219. break;
  220. default:
  221. usage(EXIT_FAILURE);
  222. break;
  223. }
  224. }
  225. if (ifname == NULL) {
  226. ERR("no input file specified");
  227. goto err;
  228. }
  229. if (ofname == NULL) {
  230. ERR("no output file specified");
  231. goto err;
  232. }
  233. err = stat(ifname, &st);
  234. if (err){
  235. ERRS("stat failed on %s", ifname);
  236. goto err;
  237. }
  238. total = st.st_size;
  239. buf = malloc(BUFSIZE);
  240. if (!buf) {
  241. ERR("no memory for buffer\n");
  242. goto err;
  243. }
  244. infile = fopen(ifname, "r");
  245. if (infile == NULL) {
  246. ERRS("could not open \"%s\" for reading", ifname);
  247. goto err_free;
  248. }
  249. outfile = fopen(ofname, "w");
  250. if (outfile == NULL) {
  251. ERRS("could not open \"%s\" for writing", ofname);
  252. goto err_close_in;
  253. }
  254. pc1_init(&pc1);
  255. while (total > 0) {
  256. unsigned datalen;
  257. if (total > BUFSIZE)
  258. datalen = BUFSIZE;
  259. else
  260. datalen = total;
  261. errno = 0;
  262. fread(buf, datalen, 1, infile);
  263. if (errno != 0) {
  264. ERRS("unable to read from file %s", ifname);
  265. goto err_close_out;
  266. }
  267. if (decrypt)
  268. pc1_decrypt_buf(&pc1, buf, datalen);
  269. else
  270. pc1_encrypt_buf(&pc1, buf, datalen);
  271. errno = 0;
  272. fwrite(buf, datalen, 1, outfile);
  273. if (errno) {
  274. ERRS("unable to write to file %s", ofname);
  275. goto err_close_out;
  276. }
  277. total -= datalen;
  278. }
  279. pc1_finish(&pc1);
  280. res = EXIT_SUCCESS;
  281. out_flush:
  282. fflush(outfile);
  283. err_close_out:
  284. fclose(outfile);
  285. if (res != EXIT_SUCCESS) {
  286. unlink(ofname);
  287. }
  288. err_close_in:
  289. fclose(infile);
  290. err_free:
  291. free(buf);
  292. err:
  293. return res;
  294. }