uint8* generate_passphrase(const char* platform, const char* ramdisk) { SHA256_CTX ctx; uint64 salt[4]; uint32 saltedHash[4]; uint64 totalSize = ramdisk_size(ramdisk); uint64 platformHash = hash_platform(platform); salt[0] = u32_to_u64(0xad79d29d, 0xe5e2ac9e); salt[1] = u32_to_u64(0xe6af2eb1, 0x9e23925b); salt[2] = u32_to_u64(0x3f1375b4, 0xbd88815c); salt[3] = u32_to_u64(0x3bdff4e5, 0x564a9f87); FILE* fd = fopen(ramdisk, "rb"); if (!fd) { fprintf(stderr, "error opening file: %s\n", ramdisk); return NULL; } int i = 0; for (i = 0; i < 4; i++) { salt[i] += platformHash; saltedHash[i] = ((uint32) (salt[i] % totalSize)) & 0xFFFFFE00; } qsort(&saltedHash, 4, 4, (int(*)(const void *, const void *)) &compare); SHA256_Init(&ctx); SHA256_Update(&ctx, salt, 32);//SHA256_DIGEST_LENGTH); int count = 0; uint8* buffer = malloc(BUF_SIZE); uint8* passphrase = malloc(SHA256_DIGEST_LENGTH); while (count < totalSize) { unsigned int bytes = fread(buffer, 1, BUF_SIZE, fd); SHA256_Update(&ctx, buffer, bytes); for (i = 0; i < 4; i++) { //some salts remain if (count < saltedHash[i] && saltedHash[i] < (count + bytes)) { if ((saltedHash[i] + 0x4000) < count) { SHA256_Update(&ctx, buffer, saltedHash[i] - count); } else { SHA256_Update(&ctx, buffer + (saltedHash[i] - count), ((bytes - (saltedHash[i] - count)) < 0x4000) ? (bytes - (saltedHash[i] - count)) : 0x4000); } } } count += bytes; } fclose(fd); SHA256_Final(passphrase, &ctx); return passphrase; }
static inline u64 u16_to_u64(u16 a14) { u16 r15 = a14; u32 r16 = u16_to_u32(r15); return u32_to_u64(r16); }
static uint64 hash_platform(const char* platform) { uint8 md[SHA_DIGEST_LENGTH]; SHA1((const unsigned char*) platform, strlen(platform), (unsigned char*) &md); uint64 hash = u32_to_u64(((md[0] << 24) | (md[1] << 16) | (md[2] << 8) | md[3]), ((md[4] << 24) | (md[5] << 16) | (md[6] << 8) | md[7])); return hash; }
uint64 hash_platform(const char* platform) { uint8* md = malloc(SHA_DIGEST_LENGTH); SHA1(platform, strlen(platform), md); uint64 hash = u32_to_u64( ((md[0] << 24) | (md[1] << 16) | (md[2] << 8) | md[3]), ((md[4] << 24) | (md[5] << 16) | (md[6] << 8) | md[7]) ); free(md); return hash; }
static inline u64 dispatch_t189(t189 a190, u32 a191) { return u32_to_u64(a191); }