buffalo-enc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (C) 2009-2011 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. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include <libgen.h>
  14. #include <getopt.h> /* for getopt() */
  15. #include <stdarg.h>
  16. #include "buffalo-lib.h"
  17. #define ERR(fmt, args...) do { \
  18. fflush(0); \
  19. fprintf(stderr, "[%s] *** error: " fmt "\n", \
  20. progname, ## args ); \
  21. } while (0)
  22. static char *progname;
  23. static char *ifname;
  24. static char *ofname;
  25. static char *crypt_key = "Buffalo";
  26. static char *magic = "start";
  27. static int longstate;
  28. static unsigned char seed = 'O';
  29. static char *product;
  30. static char *version;
  31. static int do_decrypt;
  32. static int offset;
  33. void usage(int status)
  34. {
  35. FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
  36. fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
  37. fprintf(stream,
  38. "\n"
  39. "Options:\n"
  40. " -d decrypt instead of encrypt\n"
  41. " -i <file> read input from the file <file>\n"
  42. " -o <file> write output to the file <file>\n"
  43. " -l use longstate {en,de}cryption method\n"
  44. " -k <key> use <key> for encryption (default: Buffalo)\n"
  45. " -m <magic> set magic to <magic>\n"
  46. " -p <product> set product name to <product>\n"
  47. " -v <version> set version to <version>\n"
  48. " -h show this screen\n"
  49. " -O Offset of encrypted data in file (decryption)\n"
  50. );
  51. exit(status);
  52. }
  53. static int decrypt_file(void)
  54. {
  55. struct enc_param ep;
  56. ssize_t src_len;
  57. unsigned char *buf = NULL;
  58. int err;
  59. int ret = -1;
  60. src_len = get_file_size(ifname);
  61. if (src_len < 0) {
  62. ERR("unable to get size of '%s'", ifname);
  63. goto out;
  64. }
  65. buf = malloc(src_len);
  66. if (buf == NULL) {
  67. ERR("no memory for the buffer");
  68. goto out;
  69. }
  70. err = read_file_to_buf(ifname, buf, src_len);
  71. if (err) {
  72. ERR("unable to read from file '%s'", ifname);
  73. goto out;
  74. }
  75. memset(&ep, '\0', sizeof(ep));
  76. ep.key = (unsigned char *) crypt_key;
  77. ep.longstate = longstate;
  78. err = decrypt_buf(&ep, buf + offset, src_len - offset);
  79. if (err) {
  80. ERR("unable to decrypt '%s'", ifname);
  81. goto out;
  82. }
  83. printf("Magic\t\t: '%s'\n", ep.magic);
  84. printf("Seed\t\t: 0x%02x\n", ep.seed);
  85. printf("Product\t\t: '%s'\n", ep.product);
  86. printf("Version\t\t: '%s'\n", ep.version);
  87. printf("Data len\t: %u\n", ep.datalen);
  88. printf("Checksum\t: 0x%08x\n", ep.csum);
  89. err = write_buf_to_file(ofname, buf + offset, ep.datalen);
  90. if (err) {
  91. ERR("unable to write to file '%s'", ofname);
  92. goto out;
  93. }
  94. ret = 0;
  95. out:
  96. free(buf);
  97. return ret;
  98. }
  99. static int encrypt_file(void)
  100. {
  101. struct enc_param ep;
  102. ssize_t src_len;
  103. unsigned char *buf;
  104. uint32_t hdrlen;
  105. ssize_t totlen = 0;
  106. int err;
  107. int ret = -1;
  108. src_len = get_file_size(ifname);
  109. if (src_len < 0) {
  110. ERR("unable to get size of '%s'", ifname);
  111. goto out;
  112. }
  113. totlen = enc_compute_buf_len(product, version, src_len);
  114. hdrlen = enc_compute_header_len(product, version);
  115. buf = malloc(totlen);
  116. if (buf == NULL) {
  117. ERR("no memory for the buffer");
  118. goto out;
  119. }
  120. err = read_file_to_buf(ifname, &buf[hdrlen], src_len);
  121. if (err) {
  122. ERR("unable to read from file '%s'", ofname);
  123. goto free_buf;
  124. }
  125. memset(&ep, '\0', sizeof(ep));
  126. ep.key = (unsigned char *) crypt_key;
  127. ep.seed = seed;
  128. ep.longstate = longstate;
  129. ep.csum = buffalo_csum(src_len, &buf[hdrlen], src_len);
  130. ep.datalen = src_len;
  131. strcpy((char *) ep.magic, magic);
  132. strcpy((char *) ep.product, product);
  133. strcpy((char *) ep.version, version);
  134. err = encrypt_buf(&ep, buf, &buf[hdrlen]);
  135. if (err) {
  136. ERR("invalid input file");
  137. goto free_buf;
  138. }
  139. err = write_buf_to_file(ofname, buf, totlen);
  140. if (err) {
  141. ERR("unable to write to file '%s'", ofname);
  142. goto free_buf;
  143. }
  144. ret = 0;
  145. free_buf:
  146. free(buf);
  147. out:
  148. return ret;
  149. }
  150. static int check_params(void)
  151. {
  152. int ret = -1;
  153. if (ifname == NULL) {
  154. ERR("no input file specified");
  155. goto out;
  156. }
  157. if (ofname == NULL) {
  158. ERR("no output file specified");
  159. goto out;
  160. }
  161. if (crypt_key == NULL) {
  162. ERR("no key specified");
  163. goto out;
  164. } else if (strlen(crypt_key) > BCRYPT_MAX_KEYLEN) {
  165. ERR("key '%s' is too long", crypt_key);
  166. goto out;
  167. }
  168. if (strlen(magic) != (ENC_MAGIC_LEN - 1)) {
  169. ERR("length of magic must be %d", ENC_MAGIC_LEN - 1);
  170. goto out;
  171. }
  172. if (!do_decrypt) {
  173. if (product == NULL) {
  174. ERR("no product specified");
  175. goto out;
  176. }
  177. if (version == NULL) {
  178. ERR("no version specified");
  179. goto out;
  180. }
  181. if (strlen(product) > (ENC_PRODUCT_LEN - 1)) {
  182. ERR("product name '%s' is too long", product);
  183. goto out;
  184. }
  185. if (strlen(version) > (ENC_VERSION_LEN - 1)) {
  186. ERR("version '%s' is too long", version);
  187. goto out;
  188. }
  189. }
  190. ret = 0;
  191. out:
  192. return ret;
  193. }
  194. int main(int argc, char *argv[])
  195. {
  196. int res = EXIT_FAILURE;
  197. int err;
  198. progname = basename(argv[0]);
  199. while ( 1 ) {
  200. int c;
  201. c = getopt(argc, argv, "adi:m:o:hlp:v:k:O:r:s:");
  202. if (c == -1)
  203. break;
  204. switch (c) {
  205. case 'd':
  206. do_decrypt = 1;
  207. break;
  208. case 'i':
  209. ifname = optarg;
  210. break;
  211. case 'l':
  212. longstate = 1;
  213. break;
  214. case 'm':
  215. magic = optarg;
  216. break;
  217. case 'o':
  218. ofname = optarg;
  219. break;
  220. case 'p':
  221. product = optarg;
  222. break;
  223. case 'v':
  224. version = optarg;
  225. break;
  226. case 'k':
  227. crypt_key = optarg;
  228. break;
  229. case 's':
  230. seed = strtoul(optarg, NULL, 16);
  231. break;
  232. case 'O':
  233. offset = strtoul(optarg, NULL, 0);
  234. break;
  235. case 'h':
  236. usage(EXIT_SUCCESS);
  237. break;
  238. default:
  239. usage(EXIT_FAILURE);
  240. break;
  241. }
  242. }
  243. err = check_params();
  244. if (err)
  245. goto out;
  246. if (do_decrypt)
  247. err = decrypt_file();
  248. else
  249. err = encrypt_file();
  250. if (err)
  251. goto out;
  252. res = EXIT_SUCCESS;
  253. out:
  254. return res;
  255. }