diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/rsa.go | 50 |
1 files changed, 50 insertions, 0 deletions
@@ -140,6 +140,56 @@ func (r *RSAPub) Encrypt(msg []byte) []byte { return c.Bytes() } +// LazyVerify verifies the RSA signature sig with the given message +// msg. +// +// Returns true if the verification is successful; false otherwise. +func (r *RSAPub) LazyVerify(msg, sig []byte) bool { + // Hash message using md4. + var ( + dgst Md4 = Md4{} + vd []byte // Data to verify signature with. + ) + dgst.Init([]uint32{}) + dgst.Message(msg) + vd = dgst.Hash() + + var ( + y *big.Int // Signature as in integer. + x *big.Int // Encryption block as an integer. + eb []byte // Encryption block. + ) + + // Convert signature to integer. + y = new(big.Int).SetBytes(sig) + + // Get encryption block. + x = new(big.Int).Exp(y, r.e, r.n) + eb = x.Bytes() + + if eb[0] != 0x01 { + return false + } + if eb[1] != 0xFF { + return false + } + var ( + ffd bool + lb byte = eb[0] + d []byte + ) + for _, b := range eb[1:] { + if ffd { + d = append(d, b) + } + if lb == 0xFF && b == 0x00 { + ffd = true + } + lb = b + } + return BytesEqual(vd, d[:len(vd)]) +} + func (r *RSAPub) E() *big.Int { return r.e } |
