rsa

package
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2023 License: BSD-2-Clause Imports: 11 Imported by: 24

Documentation

Overview

Package rsa pem.go imports and exports to pem files.

Package rsa includes wrappers to sign and verify the signatures of messages with the PKCS#1 RSASSA-PSS signature algorithm:

https://tools.ietf.org/html/rfc3447#page-29

We use this because of the "tighter" security proof and regression to full domain hashing in cases where good RNG is unavailable.

The primary reason for wrapping is to interface with the large Int api used by cMix.

Index

Constants

View Source
const (
	ELength = 4
)

Variables

View Source
var DefaultRSABitLen = 4096

DefaultRSABitLen is the RSA key length used in the system, in bits.

Functions

func CreatePrivateKeyPem

func CreatePrivateKeyPem(k *PrivateKey) []byte

CreatePrivateKeyPem creates a PEM file from a private key

func CreatePublicKeyPem

func CreatePublicKeyPem(k *PublicKey) []byte

CreatePrivateKeyPem creates a PEM file from a private key

func IsValidSignature

func IsValidSignature(pubKey *PublicKey, signature []byte) bool

IsValidSignature approximates whether the signature looks valid by comparing the length of the signature to the length of the public key

func Sign

func Sign(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte,
	opts *Options) ([]byte, error)

Sign uses RSASSA-PSS to calculate the signature of hashed. Note that hashed must be the result of hashing the input message using the given hash function. The opts argument may be nil, in which case the default cMix hash and salt length == size of the hash are used.

func Verify

func Verify(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte,
	opts *Options) error

Verify verifies a PSS signature. hashed is the result of hashing the input message using the given hash function and sig is the signature. A valid signature is indicated by returning a nil error. The opts argument may be nil, in which case the default cMix hash and salt length == size of the hash are used.

Types

type Options

type Options struct {
	gorsa.PSSOptions
}

Options is a direct wrapper for PSSOptions

func NewDefaultOptions

func NewDefaultOptions() *Options

NewDefaultOptions returns signing options that set the salt length equal to the length of the hash and uses the default cMix Hash algorithm.

type PrivateKey

type PrivateKey struct {
	gorsa.PrivateKey
}

PrivateKey is identical to the rsa private key, with additional big int accessors functions.

func GenerateKey

func GenerateKey(random io.Reader, bits int) (*PrivateKey, error)

GenerateKey generates an RSA keypair of the given bit size using the random source random (for example, crypto/rand.Reader).

func LoadPrivateKeyFromPem

func LoadPrivateKeyFromPem(pemBytes []byte) (*PrivateKey, error)

LoadPrivateKeyFromPem decodes and produces an RSA PrivateKey in PKCS#1 PEM format Usage:

pem := ioutil.ReadFile("pemfile.pem")
privateKey, err := LoadPrivateKeyFromPem(pem)

func (*PrivateKey) GetD

func (p *PrivateKey) GetD() *large.Int

GetD returns the private exponent of the RSA Private Key as a large.Int

func (*PrivateKey) GetDp

func (p *PrivateKey) GetDp() *large.Int

GetDp returns D mod (P - 1), or nil if unavailable

func (*PrivateKey) GetDq

func (p *PrivateKey) GetDq() *large.Int

GetDq returns D mod (Q - 1), or nil if unavailable

func (*PrivateKey) GetE

func (p *PrivateKey) GetE() int

GetE returns the RSA Public Key exponent

func (*PrivateKey) GetN

func (p *PrivateKey) GetN() *large.Int

GetN returns the RSA Public Key modulus

func (*PrivateKey) GetPrimes

func (p *PrivateKey) GetPrimes() []*large.Int

GetPrimes returns the prime factors of N, which has >= 2 elements

func (*PrivateKey) GetPublic

func (p *PrivateKey) GetPublic() *PublicKey

GetPublic returns the public key in *rsa.PublicKey format.

func (*PrivateKey) Public

func (p *PrivateKey) Public() crypto.PublicKey

Public returns the public key corresponding to priv.

type PublicKey

type PublicKey struct {
	gorsa.PublicKey
}

PublicKey is identical to the rsa public key, with additonal big int access functions.

func LoadPublicKeyFromPem

func LoadPublicKeyFromPem(pemBytes []byte) (*PublicKey, error)

LoadPublicKeyFromPem decodes and produces an RSA PublicKey in PKCS#1 PEM format

func (*PublicKey) Bytes

func (p *PublicKey) Bytes() []byte

Bytes returns the PublicKey as a byte slice. The first 4 bytes are the exponent (E) as a 4 byte big endian integer, followed by the modulus (N) as a big.Int in Bytes format. We chose the 32 bit integer for E because it should be big enough.

func (*PublicKey) FromBytes

func (p *PublicKey) FromBytes(b []byte) error

FromBytes loads the given byte slice into the PublicKey.

func (*PublicKey) GetE

func (p *PublicKey) GetE() int

GetE returns the RSA Public Key exponent

func (*PublicKey) GetGoRSA

func (p *PublicKey) GetGoRSA() *gorsa.PublicKey

GetGoRSA returns the public key in the standard Go crypto/rsa format.

func (*PublicKey) GetN

func (p *PublicKey) GetN() *large.Int

GetN returns the RSA Public Key modulus

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL