This is a question out of curiosity !
A hash function produces an output that is considered computationally infeasible to reverse.
Let's consider, for the sake of illustration purposes a tried and tested hash function like SHA 256, belonging to any of the families ( 2 or 3). Since the block size for SHA 256 is 512 bits or 64 bytes, we could fill it in the following way, inspired by the ChaCha20 stream cipher.
cccc cccc cccc cccc kkkk kkkk kkkk kkkk kkkk kkkk kkkk kkkk bbbb nnnn nnnn nnnn
c
- some fixed constants
k
- key
n
- nonce
b
- counter (32 bits)
Each letter strings corresponds to a byte.
NOTE : The constants could be sacrificed for nonce bytes, hence facilitating a random IV, of bit length >= 128 bits. The counter could also be made to support 64 bits the same way.
We could generate $2^{n} . (blocksize/2)$ bytes of pseudo random key stream, that could be XORed with the plaintext to get ciphertext, where $n$ is the bit length of the counter, $blocksize$ is the block size (in bytes) of the hash function, used. In this case $blocksize$ is 64 for SHA 256...
Let the counter used be $n$ bits. There are $2^{n}$ possible values for the counter. Since output of the hash function is $blocksize / 2 $ bytes for any individual value of the counter, the scheme can generate a maximum of $2^{n} . (blocksize/2)$ bytes.
Here $n = 32$ so we get, $(2^{32}) . (64/2)$ bytes.
I realize that, giving $x$ bytes of input (where $x$ = len(constants) + len(key) + len(nonce) + len(counter)
) and getting $x/2$ bytes as output from the hash function is a kind of inefficiency (SHA - 256), given the fact that the hash function has to run once more (for every input block of size $x$, (additional data of size $x$)) with MD complaint padding information (byte 0x80
followed by $55$ null bytes, and $8$ byte length encoding) . That could cost double CPU clock cycles, for every call to a stream generating function.
In the case of SHA 256 $x = 64$ bytes.
Also, This scheme, is extremely inefficient compared to other stream ciphers available like Chacha20 / Salsa20 !
But,
Is this a good scheme ?
What are the possible security flaws, (theoretical/implementation) in this protocol?
If this is secure, can this scheme offer $256$ bit security ?
What are the possible Quantum attacks on this scheme ?
Since SHA-256 family of hash functions is immune to side channel attacks, will that be an added advantage?
Every advice and guidance, will be greatly appreciated! :)