I am trying to understand RSA digital signature algorithm. I am planning to follow these steps:
Sign algorithm:
- Take some message $m$
- Create digital signature using my private key $[d,n]$: $s = S_a(m) = m^d \bmod n$
- Pass my message and signature $[m,s]$
Verification algorithm:
- Accept $[m,s]$
- Take public key $[e,n]$
- Retrieve message: $m' = P_a(s) = s^e \bmod n$
- Check authenticity of signature and message immutability by comparing $m$ and $m'$
But I also want to use hashing. I plan to sign not the message $m$, but its hash: $m_{\rm hashed}$. Sign algorithm will probably be changed to:
Sign algorithm (with hashing):
- Take some message $m$
- Generate hash of message: $m_{\rm hashed}$
- Create digital signature using my private key $[d,n]$: $s = S_a(m_{\rm hashed}) = m_{\rm hashed}^d \bmod n$
- Pass my message and signature $[m,s]$
I have question about verification steps. Specifically about checking message authenticity in step 4.
Verification algorithm (with hashing):
- Accept $[m,s]$
- Take public key $[e,n]$
- Retrieve message: $m' = P_a(s) = s^e \bmod n$
- Check authenticity of signature and message immutability by comparing $m$ and $m'$ ???
Before using hashing we could check authenticity and immutability by comparing $m$ and $m'$. But in this case after step 3, we will get $m'$ as a hashed version (not as original message). How can we compare $m$ and $m'$ if $m$ is original message and $m'$ is hashed version (taking into account that hashing is irreversible and there is no way to decrypt hash back)?
My question: what is the correct algorithm? How to correctly use RSA for digital signature with hashing?