return a descriptive error upon a hash size mismatch

This commit is contained in:
eyedeekay
2025-04-03 17:00:10 -04:00
parent e7e26ae021
commit 4577408d6d
3 changed files with 9 additions and 8 deletions

View File

@ -30,13 +30,12 @@ func (r RSA2048PublicKey) VerifyHash(h []byte, sig []byte) error {
} }
// For RSA2048, we use SHA-256 // For RSA2048, we use SHA-256
hashed := h
if len(h) != sha256.Size { if len(h) != sha256.Size {
// If we received a different hash size, warn but continue return oops.Errorf("RSA2048 verification requires SHA-256 hash (expected %d bytes, got %d)",
log.Warnf("RSA2048 verification received unexpected hash size: %d", len(h)) sha256.Size, len(h))
} }
err = rsa.VerifyPKCS1v15(pubKey, crypto.SHA256, hashed, sig) err = rsa.VerifyPKCS1v15(pubKey, crypto.SHA256, h, sig)
if err != nil { if err != nil {
return oops.Errorf("RSA signature verification failed: %w", err) return oops.Errorf("RSA signature verification failed: %w", err)
} }

View File

@ -3,6 +3,7 @@ package rsa
import ( import (
"crypto" "crypto"
"crypto/rsa" "crypto/rsa"
"crypto/sha256"
"crypto/sha512" "crypto/sha512"
"github.com/go-i2p/go-i2p/lib/crypto/types" "github.com/go-i2p/go-i2p/lib/crypto/types"
@ -30,8 +31,8 @@ func (r RSA3072PublicKey) VerifyHash(h []byte, sig []byte) error {
// For RSA3072, SHA512 is often used // For RSA3072, SHA512 is often used
hashed := h hashed := h
if len(h) != sha512.Size { if len(h) != sha512.Size {
// If we received a different hash size, warn but continue return oops.Errorf("RSA3072 verification requires SHA-256 hash (expected %d bytes, got %d)",
log.Warnf("RSA3072 verification received unexpected hash size: %d", len(h)) sha256.Size, len(h))
} }
err = rsa.VerifyPKCS1v15(pubKey, crypto.SHA512, hashed, sig) err = rsa.VerifyPKCS1v15(pubKey, crypto.SHA512, hashed, sig)

View File

@ -3,6 +3,7 @@ package rsa
import ( import (
"crypto" "crypto"
"crypto/rsa" "crypto/rsa"
"crypto/sha256"
"crypto/sha512" "crypto/sha512"
"github.com/go-i2p/go-i2p/lib/crypto/types" "github.com/go-i2p/go-i2p/lib/crypto/types"
@ -35,8 +36,8 @@ func (r RSA4096PublicKey) VerifyHash(h []byte, sig []byte) error {
// Verify the signature using PKCS1v15 // Verify the signature using PKCS1v15
err = rsa.VerifyPKCS1v15(pubKey, crypto.SHA512, h, sig) err = rsa.VerifyPKCS1v15(pubKey, crypto.SHA512, h, sig)
if err != nil { if err != nil {
log.WithError(err).Error("RSA-4096 signature verification failed") return oops.Errorf("RSA4096 verification requires SHA-256 hash (expected %d bytes, got %d)",
return oops.Errorf("invalid RSA-4096 signature: %w", err) sha256.Size, len(h))
} }
log.Debug("RSA-4096 signature verified successfully") log.Debug("RSA-4096 signature verified successfully")