#include #include #include #include uint64_t mixer = 6148914691236517205;//010101... uint64_t carry = 1234567890123456789; void stir(uint64_t * state, uint64_t statelen) { uint64_t j; for(j = 0; j < statelen; j++) { if(state[(j+2)%statelen]>state[(j+3)%statelen]) carry ^= state[(j+1)%statelen]; else carry ^= ~state[(j+1)%statelen]; state[j] ^= carry; carry += mixer; } } int PHS(void *out, size_t outlen, const void *in, size_t inlen, const void *salt, size_t saltlen, uint64_t t_cost, uint64_t m_cost) { uint64_t state[256] = {0}; uint64_t * memstate; uint64_t rounds = 4, i; uint64_t statelen = 256, j; memmove(&state[0], in, inlen); memmove(&state[(inlen / 8) + 1], salt, saltlen); state[statelen - 3] = outlen; state[statelen - 2] = inlen; state[statelen - 1] = saltlen; stir(state, statelen * rounds); if(t_cost > 0) stir(state, statelen * t_cost); if(m_cost > 0) { memstate = (uint64_t *) malloc(m_cost * statelen); for(i = 0; i < m_cost; i++) { for(j = 0; j < statelen; j++) memstate[j + (i * statelen)] = state[j]; stir(state, statelen); } for(i = 0; i < m_cost * statelen; i++) { mixer += memstate[carry % m_cost * statelen]; stir(state, statelen); } free(memstate); } //outlen is 32 for(i = 0; i < outlen; i++) memcpy(out + i, &state[i * 8], sizeof(char)); return 0; }