Files
varsig/rsa.go

74 lines
1.6 KiB
Go
Raw Normal View History

package varsig
import (
"encoding/binary"
)
2025-08-05 15:35:12 +02:00
// AlgorithmRSA is the value specifying an RSA signature.
const AlgorithmRSA = Algorithm(0x1205)
var _ Varsig = RSAVarsig{}
// RSAVarsig is a varsig that encodes the parameters required to describe
// an RSA signature.
type RSAVarsig struct {
varsig
hashAlg Hash
2025-07-28 18:40:45 +02:00
keyLen uint64
}
// NewRSAVarsig creates and validates an RSA varsig with the provided
// hash algorithm, key length and payload encoding.
2025-07-28 18:40:45 +02:00
func NewRSAVarsig(hashAlgorithm Hash, keyLen uint64, payloadEncoding PayloadEncoding) RSAVarsig {
return RSAVarsig{
varsig: varsig{
2025-08-05 15:35:12 +02:00
algo: AlgorithmRSA,
payEnc: payloadEncoding,
},
hashAlg: hashAlgorithm,
2025-07-28 18:40:45 +02:00
keyLen: keyLen,
}
}
// Encode returns the encoded byte format of the RSAVarsig.
func (v RSAVarsig) Encode() []byte {
buf := v.encode()
2025-07-28 18:40:45 +02:00
buf = binary.AppendUvarint(buf, uint64(v.hashAlg))
2025-07-28 18:40:45 +02:00
buf = binary.AppendUvarint(buf, v.keyLen)
buf = append(buf, EncodePayloadEncoding(v.payEnc)...)
return buf
}
// Hash returns the value describing the hash algorithm used to hash
// the payload content before the signature is generated.
func (v RSAVarsig) Hash() Hash {
return v.hashAlg
}
// KeyLength returns the length of the RSA key used to sign the payload
// content.
func (v RSAVarsig) KeyLength() uint64 {
2025-07-28 18:40:45 +02:00
return v.keyLen
}
2025-07-28 18:40:45 +02:00
func decodeRSA(r BytesReader) (Varsig, error) {
hashAlg, err := DecodeHashAlgorithm(r)
if err != nil {
return nil, err
}
2025-07-28 18:40:45 +02:00
keyLen, err := binary.ReadUvarint(r)
if err != nil {
return nil, err
}
2025-07-28 18:40:45 +02:00
payEnc, err := DecodePayloadEncoding(r)
if err != nil {
return nil, err
}
2025-07-28 18:40:45 +02:00
return NewRSAVarsig(hashAlg, keyLen, payEnc), nil
}