summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorsiddharth ravikumar <s@ricketyspace.net>2025-11-08 13:24:32 -0500
committersiddharth ravikumar <s@ricketyspace.net>2025-11-08 13:27:20 -0500
commit34190b8764ec54ebaf7a82532b54e976decdc99f (patch)
treea0a3211b534dc47b5770b8ea64f0b31bcfc7d603 /lib
parent2fc8be08ae2f72ebca965decadc191729e6bb32b (diff)
lib: add `RSAPub.LazyVerify`
Diffstat (limited to 'lib')
-rw-r--r--lib/rsa.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/rsa.go b/lib/rsa.go
index 314e8bb..7c6b2fb 100644
--- a/lib/rsa.go
+++ b/lib/rsa.go
@@ -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
}