buffalo-lib.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #ifndef _BUFFALO_LIB_H
  10. #define _BUFFALO_LIB_H
  11. #include <stdint.h>
  12. #define ARRAY_SIZE(_a) (sizeof((_a)) / sizeof((_a)[0]))
  13. #define BIT(_x) (1UL << (_x))
  14. #define TAG_BRAND_LEN 32
  15. #define TAG_PRODUCT_LEN 32
  16. #define TAG_VERSION_LEN 8
  17. #define TAG_REGION_LEN 2
  18. #define TAG_LANGUAGE_LEN 8
  19. #define TAG_PLATFORM_LEN 8
  20. #define TAG_HWVER_LEN 4
  21. #define TAG_HWVER_VAL_LEN 4
  22. struct buffalo_tag {
  23. unsigned char product[TAG_PRODUCT_LEN];
  24. unsigned char brand[TAG_BRAND_LEN];
  25. unsigned char ver_major[TAG_VERSION_LEN];
  26. unsigned char ver_minor[TAG_VERSION_LEN];
  27. unsigned char region_code[2];
  28. uint32_t region_mask;
  29. unsigned char unknown0[2];
  30. unsigned char language[TAG_LANGUAGE_LEN];
  31. unsigned char platform[TAG_PLATFORM_LEN];
  32. unsigned char hwv[TAG_HWVER_LEN];
  33. unsigned char hwv_val[TAG_HWVER_VAL_LEN];
  34. uint8_t unknown1[24];
  35. uint32_t len;
  36. uint32_t crc;
  37. uint32_t base1;
  38. uint32_t base2;
  39. uint32_t data_len;
  40. uint8_t flag;
  41. uint8_t unknown2[3];
  42. } __attribute ((packed));
  43. struct buffalo_tag2 {
  44. unsigned char product[TAG_PRODUCT_LEN];
  45. unsigned char brand[TAG_BRAND_LEN];
  46. unsigned char ver_major[TAG_VERSION_LEN];
  47. unsigned char ver_minor[TAG_VERSION_LEN];
  48. unsigned char region_code[2];
  49. uint32_t region_mask;
  50. unsigned char unknown0[2];
  51. unsigned char language[TAG_LANGUAGE_LEN];
  52. unsigned char platform[TAG_PLATFORM_LEN];
  53. unsigned char hwv[TAG_HWVER_LEN];
  54. unsigned char hwv_val[TAG_HWVER_VAL_LEN];
  55. uint8_t unknown1[24];
  56. uint32_t total_len;
  57. uint32_t crc;
  58. uint32_t len1;
  59. uint32_t len2;
  60. uint8_t flag;
  61. uint8_t unknown2[3];
  62. } __attribute ((packed));
  63. #define ENC_PRODUCT_LEN 32
  64. #define ENC_VERSION_LEN 8
  65. #define ENC_MAGIC_LEN 6
  66. unsigned long enc_compute_header_len(char *product, char *version);
  67. unsigned long enc_compute_buf_len(char *product, char *version,
  68. unsigned long datalen);
  69. struct enc_param {
  70. unsigned char *key;
  71. unsigned char magic[ENC_MAGIC_LEN];
  72. unsigned char product[ENC_PRODUCT_LEN];
  73. unsigned char version[ENC_VERSION_LEN];
  74. unsigned char seed;
  75. int longstate;
  76. unsigned datalen;
  77. uint32_t csum;
  78. };
  79. int encrypt_buf(struct enc_param *ep, unsigned char *hdr,
  80. unsigned char *data);
  81. int decrypt_buf(struct enc_param *ep, unsigned char *data,
  82. unsigned long datalen);
  83. #define BCRYPT_DEFAULT_STATE_LEN 256
  84. #define BCRYPT_MAX_KEYLEN 254
  85. struct bcrypt_ctx {
  86. unsigned long i;
  87. unsigned long j;
  88. unsigned char *state;
  89. unsigned long state_len;
  90. };
  91. int bcrypt_init(struct bcrypt_ctx *ctx, void *key, int keylen,
  92. unsigned long state_len);
  93. int bcrypt_process(struct bcrypt_ctx *ctx, unsigned char *src,
  94. unsigned char *dst, unsigned long len);
  95. void bcrypt_finish(struct bcrypt_ctx *ctx);
  96. int bcrypt_buf(unsigned char seed, unsigned char *key, unsigned char *src,
  97. unsigned char *dst, unsigned long len, int longstate);
  98. uint32_t buffalo_csum(uint32_t csum, void *buf, unsigned long len);
  99. uint32_t buffalo_crc(void *buf, unsigned long len);
  100. ssize_t get_file_size(char *name);
  101. int read_file_to_buf(char *name, void *buf, ssize_t buflen);
  102. int write_buf_to_file(char *name, void *buf, ssize_t buflen);
  103. #endif /* _BUFFALO_LIB_H */