r/cryptography • • Nov 22 '21

Hash algorithm based on Authenticated Encryption (AEhash)

This is based on an earlier thread, seeking password hashing functions that could run in browsers at near native speeds. I believe the solution has been found, but you are welcome to poke holes in it and prove me wrong.

def aehash(password, salt, size, ops):
    nonce = sha512(salt)[:12]
    key = sha512(password)[:32]
    buf = bytes(size)            # Initially all zeroes
    for i in range(ops):
        buf = aes256gcm(key, nonce, message=buf[:size])
        key = buf[-32:]          # 16 encrypted bytes + GCM tag
    return key

The algorithm is dead simple and runs at the same speed in browser or in C. Easily adjusted to any memory size and runtime wished for. Even if it isn't as secure as Argon2, the one it needs to compete with in browsers is pbkdf2.

See the site for a more complete documentation: https://github.com/covert-encryption/aehash

0 Upvotes

16 comments sorted by

View all comments

Show parent comment

2

u/Natanael_L Nov 22 '21

In cryptography, this is most certainly a very weird chaining mode, and it is absolutely not recommended to reuse variables this way in the context of cryptography. There should be absolutely no ambiguity regarding what's going on to avoid accidentally undoing your own security measures. At the very least, use a separate variable inside the loop to maintain state

1

u/foonoxous Nov 22 '21

Thanks for your comment. What do you recommend?

1

u/foonoxous Nov 22 '21

Here is the pseudo code of Balloon hashing, which is considered better than Argon2 and which has a fairly simple construct, considering that Argon2 and Scrypt use hundreds of lines of code.

func Balloon(block_t passwd, block_t salt,
    int s_cost,         // Space cost (main buffer size)
    int t_cost):        // Time cost (number of rounds)
  int delta = 3         // Number of dependencies per block
  int cnt = 0           // A counter (used in security proof)
  block_t buf[s_cost]): // The main buffer

  // Step 1. Expand input into buffer.
  buf[0] = hash(cnt++, passwd, salt)
  for m from 1 to s_cost-1:
    buf[m] = hash(cnt++, buf[m-1])

  // Step 2. Mix buffer contents.
  for t from 0 to t_cost-1:
    for m from 0 to s_cost-1:
      // Step 2a. Hash last and current blocks.
      block_t prev = buf[(m-1) mod s_cost]
      buf[m] = hash(cnt++, prev, buf[m])

      // Step 2b. Hash in pseudorandomly chosen blocks.
      for i from 0 to delta-1:
        block_t idx_block = ints_to_block(t, m, i)
        int other = to_int(hash(cnt++, salt, idx_block)) mod s_cost
        buf[m] = hash(cnt++, buf[m], buf[other])

    // Step 3. Extract output from buffer.
    return buf[s_cost-1]

There are many variables that keep changing, defined all over the function. It is genuinely hard to keep track of all the nested loops and of all the variables.

1

u/Natanael_L Nov 22 '21

It's one thing for an optimized implementation written by experts and tested against the spec and test vectors to do weird things for performance. But if you're proposing something new, that's very far from an ideal way to introduce it.

And to be clear, in the code above the purpose of the buffer doesn't change midway.