int crypto_core_hchacha20(unsigned char *out, const unsigned char *in, const unsigned char *k, const unsigned char *c) { int i; uint32_t x0, x1, x2, x3, x4, x5, x6, x7; uint32_t x8, x9, x10, x11, x12, x13, x14, x15; if (c == NULL) { x0 = U32C(0x61707865); x1 = U32C(0x3320646e); x2 = U32C(0x79622d32); x3 = U32C(0x6b206574); } else { x0 = LOAD32_LE(c + 0); x1 = LOAD32_LE(c + 4); x2 = LOAD32_LE(c + 8); x3 = LOAD32_LE(c + 12); } x4 = LOAD32_LE(k + 0); x5 = LOAD32_LE(k + 4); x6 = LOAD32_LE(k + 8); x7 = LOAD32_LE(k + 12); x8 = LOAD32_LE(k + 16); x9 = LOAD32_LE(k + 20); x10 = LOAD32_LE(k + 24); x11 = LOAD32_LE(k + 28); x12 = LOAD32_LE(in + 0); x13 = LOAD32_LE(in + 4); x14 = LOAD32_LE(in + 8); x15 = LOAD32_LE(in + 12); for (i = 0; i < 10; i++) { QUARTERROUND(x0, x4, x8, x12); QUARTERROUND(x1, x5, x9, x13); QUARTERROUND(x2, x6, x10, x14); QUARTERROUND(x3, x7, x11, x15); QUARTERROUND(x0, x5, x10, x15); QUARTERROUND(x1, x6, x11, x12); QUARTERROUND(x2, x7, x8, x13); QUARTERROUND(x3, x4, x9, x14); } STORE32_LE(out + 0, x0); STORE32_LE(out + 4, x1); STORE32_LE(out + 8, x2); STORE32_LE(out + 12, x3); STORE32_LE(out + 16, x12); STORE32_LE(out + 20, x13); STORE32_LE(out + 24, x14); STORE32_LE(out + 28, x15); return 0; }
int crypto_core_hsalsa20(unsigned char *out, const unsigned char *in, const unsigned char *k, const unsigned char *c) { uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15; int i; if (c == NULL) { x0 = U32C(0x61707865); x5 = U32C(0x3320646e); x10 = U32C(0x79622d32); x15 = U32C(0x6b206574); } else { x0 = LOAD32_LE(c + 0); x5 = LOAD32_LE(c + 4); x10 = LOAD32_LE(c + 8); x15 = LOAD32_LE(c + 12); } x1 = LOAD32_LE(k + 0); x2 = LOAD32_LE(k + 4); x3 = LOAD32_LE(k + 8); x4 = LOAD32_LE(k + 12); x11 = LOAD32_LE(k + 16); x12 = LOAD32_LE(k + 20); x13 = LOAD32_LE(k + 24); x14 = LOAD32_LE(k + 28); x6 = LOAD32_LE(in + 0); x7 = LOAD32_LE(in + 4); x8 = LOAD32_LE(in + 8); x9 = LOAD32_LE(in + 12); for (i = ROUNDS; i > 0; i -= 2) { x4 ^= ROTL32(x0 + x12, 7); x8 ^= ROTL32(x4 + x0, 9); x12 ^= ROTL32(x8 + x4, 13); x0 ^= ROTL32(x12 + x8, 18); x9 ^= ROTL32(x5 + x1, 7); x13 ^= ROTL32(x9 + x5, 9); x1 ^= ROTL32(x13 + x9, 13); x5 ^= ROTL32(x1 + x13, 18); x14 ^= ROTL32(x10 + x6, 7); x2 ^= ROTL32(x14 + x10, 9); x6 ^= ROTL32(x2 + x14, 13); x10 ^= ROTL32(x6 + x2, 18); x3 ^= ROTL32(x15 + x11, 7); x7 ^= ROTL32(x3 + x15, 9); x11 ^= ROTL32(x7 + x3, 13); x15 ^= ROTL32(x11 + x7, 18); x1 ^= ROTL32(x0 + x3, 7); x2 ^= ROTL32(x1 + x0, 9); x3 ^= ROTL32(x2 + x1, 13); x0 ^= ROTL32(x3 + x2, 18); x6 ^= ROTL32(x5 + x4, 7); x7 ^= ROTL32(x6 + x5, 9); x4 ^= ROTL32(x7 + x6, 13); x5 ^= ROTL32(x4 + x7, 18); x11 ^= ROTL32(x10 + x9, 7); x8 ^= ROTL32(x11 + x10, 9); x9 ^= ROTL32(x8 + x11, 13); x10 ^= ROTL32(x9 + x8, 18); x12 ^= ROTL32(x15 + x14, 7); x13 ^= ROTL32(x12 + x15, 9); x14 ^= ROTL32(x13 + x12, 13); x15 ^= ROTL32(x14 + x13, 18); } STORE32_LE(out + 0, x0); STORE32_LE(out + 4, x5); STORE32_LE(out + 8, x10); STORE32_LE(out + 12, x15); STORE32_LE(out + 16, x6); STORE32_LE(out + 20, x7); STORE32_LE(out + 24, x8); STORE32_LE(out + 28, x9); return 0; }
int main(int argc, char *argv[]) { FILE *fpIn = NULL; FILE *fpOut = NULL; struct edimax_header eh; size_t res; int length; char *buf; struct trx_header *p; if (argc != 3) { printf("Usage: %s <input file> <output file>\n", argv[0]); return -1; } fpIn = fopen(argv[1], "rb"); if (fpIn == NULL) { fprintf(stderr, "Unable to open %s\n", argv[1]); return EXIT_FAILURE; } /* compute the length of the file */ fseek(fpIn, 0, SEEK_END); length = ftell(fpIn); /* alloc enough memory to store the file */ buf = (char *)malloc(length); if (!buf) { fprintf(stderr, "malloc of buffers failed\n"); return EXIT_FAILURE; } rewind(fpIn); /* read the whole file*/ res = fread(buf, 1, length, fpIn); p = (struct trx_header *)buf; if (LOAD32_LE(p->magic) != TRX_MAGIC) { fprintf(stderr, "Not a trx file...%x\n", LOAD32_LE(p->magic)); return EXIT_FAILURE; } fclose(fpIn); fpOut = fopen(argv[2], "wb+"); if (fpOut == NULL) { fprintf(stderr, "Unable to open %s\n", argv[2]); return EXIT_FAILURE; } /* make the 3 partition beeing 12 bytes closer from the header */ memcpy(buf + LOAD32_LE(p->offsets[2]) - EDIMAX_HDR_LEN, buf + LOAD32_LE(p->offsets[2]), length - LOAD32_LE(p->offsets[2])); /* recompute the crc32 check */ p->crc32 = STORE32_LE(crc32buf((char *) &p->flag_version, length - offsetof(struct trx_header, flag_version))); eh.sign = STORE32_LE(EDIMAX_PS16); eh.length = STORE32_LE(length); eh.start_addr = STORE32_LE(0x80500000); /* write the modified file */ fwrite(&eh, sizeof(struct edimax_header), 1, fpOut); fwrite(buf, sizeof(char), length, fpOut); fclose(fpOut); }