buffalo-enc.c 5.5 KB

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