1
0

buffalo-lib.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. struct buffalo_tag3 {
  64. unsigned char product[TAG_PRODUCT_LEN];
  65. unsigned char brand[TAG_BRAND_LEN];
  66. unsigned char ver_major[TAG_VERSION_LEN];
  67. unsigned char ver_minor[TAG_VERSION_LEN];
  68. unsigned char region_code[2];
  69. uint32_t region_mask;
  70. unsigned char unknown0[2];
  71. unsigned char language[TAG_LANGUAGE_LEN];
  72. unsigned char platform[TAG_PLATFORM_LEN];
  73. unsigned char hwv[TAG_HWVER_LEN];
  74. unsigned char hwv_val[TAG_HWVER_VAL_LEN];
  75. uint8_t unknown1[24];
  76. uint32_t total_len;
  77. uint32_t crc;
  78. uint32_t len1;
  79. uint32_t base2;
  80. } __attribute ((packed));
  81. #define ENC_PRODUCT_LEN 32
  82. #define ENC_VERSION_LEN 8
  83. #define ENC_MAGIC_LEN 6
  84. unsigned long enc_compute_header_len(char *product, char *version);
  85. unsigned long enc_compute_buf_len(char *product, char *version,
  86. unsigned long datalen);
  87. struct enc_param {
  88. unsigned char *key;
  89. unsigned char magic[ENC_MAGIC_LEN];
  90. unsigned char product[ENC_PRODUCT_LEN];
  91. unsigned char version[ENC_VERSION_LEN];
  92. unsigned char seed;
  93. int longstate;
  94. unsigned datalen;
  95. uint32_t csum;
  96. };
  97. int encrypt_buf(struct enc_param *ep, unsigned char *hdr,
  98. unsigned char *data);
  99. int decrypt_buf(struct enc_param *ep, unsigned char *data,
  100. unsigned long datalen);
  101. #define BCRYPT_DEFAULT_STATE_LEN 256
  102. #define BCRYPT_MAX_KEYLEN 254
  103. struct bcrypt_ctx {
  104. unsigned long i;
  105. unsigned long j;
  106. unsigned char *state;
  107. unsigned long state_len;
  108. };
  109. int bcrypt_init(struct bcrypt_ctx *ctx, void *key, int keylen,
  110. unsigned long state_len);
  111. int bcrypt_process(struct bcrypt_ctx *ctx, unsigned char *src,
  112. unsigned char *dst, unsigned long len);
  113. void bcrypt_finish(struct bcrypt_ctx *ctx);
  114. int bcrypt_buf(unsigned char seed, unsigned char *key, unsigned char *src,
  115. unsigned char *dst, unsigned long len, int longstate);
  116. uint32_t buffalo_csum(uint32_t csum, void *buf, unsigned long len);
  117. uint32_t buffalo_crc(void *buf, unsigned long len);
  118. ssize_t get_file_size(char *name);
  119. int read_file_to_buf(char *name, void *buf, ssize_t buflen);
  120. int write_buf_to_file(char *name, void *buf, ssize_t buflen);
  121. #endif /* _BUFFALO_LIB_H */