cose

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2022 License: MIT Imports: 5 Imported by: 3

Documentation

Index

Constants

View Source
const (
	HeaderLabelReserved          key.IntKey = 0
	HeaderLabelAlgorithm         key.IntKey = 1 // protected header or unprotected header
	HeaderLabelCritical          key.IntKey = 2 // protected header
	HeaderLabelContentType       key.IntKey = 3 // unprotected header
	HeaderLabelKeyID             key.IntKey = 4 // unprotected header
	HeaderLabelIV                key.IntKey = 5 // unprotected header
	HeaderLabelPartialIV         key.IntKey = 6 // unprotected header
	HeaderLabelCounterSignature  key.IntKey = 7 // unprotected header
	HeaderLabelCounterSignature0 key.IntKey = 9 // unprotected header
)

COSE Header labels registered in the IANA "COSE Header Parameters" registry.

Reference https://www.iana.org/assignments/cose/cose.xhtml#header-parameters

View Source
const (
	HeaderAlgEphemeralKey key.IntKey = -1
	HeaderAlgStaticKey    key.IntKey = -2
	HeaderAlgStaticKeyID  key.IntKey = -3
	HeaderAlgSalt         key.IntKey = -20
)

COSE Header Algorithm Parameters Reference https://www.iana.org/assignments/cose/cose.xhtml#header-algorithm-parameters

Variables

This section is empty.

Functions

This section is empty.

Types

type Encrypt0Message

type Encrypt0Message[T any] struct {
	Protected   Headers
	Unprotected Headers
	Payload     T
	// contains filtered or unexported fields
}

Encrypt0Message represents a COSE_Encrypt0 object.

Reference https://datatracker.ietf.org/doc/html/rfc9052#name-single-recipient-encrypted

func DecryptEncrypt0Message added in v0.4.0

func DecryptEncrypt0Message[T any](encryptor key.Encryptor, coseData, externalData []byte) (*Encrypt0Message[T], error)

DecryptEncrypt0Message decrypts and decodes a COSE_Encrypt0 message with a Encryptor and returns a *Encrypt0Message. `externalData` should be the same as the one used when encrypting.

func (*Encrypt0Message[T]) Bytesify

func (m *Encrypt0Message[T]) Bytesify() []byte

Bytesify returns a CBOR-encoded byte slice. It returns nil if MarshalCBOR failed.

func (*Encrypt0Message[T]) Decrypt

func (m *Encrypt0Message[T]) Decrypt(encryptor key.Encryptor, externalData []byte) error

Decrypt decrypts a COSE_Encrypt0 message with a Encryptor. It should call `Encrypt0Message.UnmarshalCBOR` before calling this method. `externalData` should be the same as the one used when encrypting.

func (*Encrypt0Message[T]) Encrypt

func (m *Encrypt0Message[T]) Encrypt(encryptor key.Encryptor, externalData []byte) error

Encrypt encrypt a COSE_Encrypt0 message with a Encryptor. `externalData` can be nil. https://datatracker.ietf.org/doc/html/rfc9052#name-externally-supplied-data

func (*Encrypt0Message[T]) EncryptAndEncode added in v0.4.0

func (m *Encrypt0Message[T]) EncryptAndEncode(encryptor key.Encryptor, externalData []byte) ([]byte, error)

EncryptAndEncode encrypts and encodes a COSE_Encrypt0 message with a Encryptor. `externalData` can be nil. https://datatracker.ietf.org/doc/html/rfc9052#name-externally-supplied-data

func (*Encrypt0Message[T]) MarshalCBOR

func (m *Encrypt0Message[T]) MarshalCBOR() ([]byte, error)

MarshalCBOR implements the CBOR Marshaler interface for Encrypt0Message. It should call `Encrypt0Message.Encrypt` before calling this method.

func (*Encrypt0Message[T]) UnmarshalCBOR

func (m *Encrypt0Message[T]) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements the CBOR Unmarshaler interface for Mac0Message.

type EncryptMessage added in v0.5.0

type EncryptMessage[T any] struct {
	Protected   Headers
	Unprotected Headers
	Payload     T
	// contains filtered or unexported fields
}

EncryptMessage represents a COSE_Encrypt object.

Reference https://datatracker.ietf.org/doc/html/rfc9052#name-single-recipient-encrypted

func DecryptEncryptMessage added in v0.5.0

func DecryptEncryptMessage[T any](encryptor key.Encryptor, coseData, externalData []byte) (*EncryptMessage[T], error)

DecryptEncryptMessage decrypts and decodes a COSE_Encrypt message with a Encryptor and returns a *EncryptMessage. `externalData` should be the same as the one used when encrypting.

func (*EncryptMessage[T]) AddRecipient added in v0.5.0

func (m *EncryptMessage[T]) AddRecipient(recipient *Recipient) error

AddRecipient add a Recipient to the COSE_Encrypt message.

func (*EncryptMessage[T]) Bytesify added in v0.5.0

func (m *EncryptMessage[T]) Bytesify() []byte

Bytesify returns a CBOR-encoded byte slice. It returns nil if MarshalCBOR failed.

func (*EncryptMessage[T]) Decrypt added in v0.5.0

func (m *EncryptMessage[T]) Decrypt(encryptor key.Encryptor, externalData []byte) error

Decrypt decrypts a COSE_Encrypt message with a Encryptor. It should call `EncryptMessage.UnmarshalCBOR` before calling this method. `externalData` should be the same as the one used when encrypting.

func (*EncryptMessage[T]) Encrypt added in v0.5.0

func (m *EncryptMessage[T]) Encrypt(encryptor key.Encryptor, externalData []byte) error

Encrypt encrypt a COSE_Encrypt message with a Encryptor. `externalData` can be nil. https://datatracker.ietf.org/doc/html/rfc9052#name-externally-supplied-data

func (*EncryptMessage[T]) EncryptAndEncode added in v0.5.0

func (m *EncryptMessage[T]) EncryptAndEncode(encryptor key.Encryptor, externalData []byte) ([]byte, error)

EncryptAndEncode encrypts and encodes a COSE_Encrypt message with a Encryptor. `externalData` can be nil. https://datatracker.ietf.org/doc/html/rfc9052#name-externally-supplied-data

func (*EncryptMessage[T]) MarshalCBOR added in v0.5.0

func (m *EncryptMessage[T]) MarshalCBOR() ([]byte, error)

MarshalCBOR implements the CBOR Marshaler interface for EncryptMessage. It should call `EncryptMessage.Encrypt` before calling this method.

func (*EncryptMessage[T]) Recipients added in v0.5.0

func (m *EncryptMessage[T]) Recipients() []*Recipient

Recipients returns recipients in the COSE_Encrypt message

func (*EncryptMessage[T]) UnmarshalCBOR added in v0.5.0

func (m *EncryptMessage[T]) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements the CBOR Unmarshaler interface for Mac0Message.

type Headers

type Headers key.IntMap

Headers represents a COSE Generic_Headers structure.

func (Headers) Bytesify

func (h Headers) Bytesify() []byte

Bytesify returns a CBOR-encoded byte slice. It returns nil if MarshalCBOR failed.

func (Headers) GetBytes

func (h Headers) GetBytes(k key.IntKey) ([]byte, error)

GetBytes returns the value for the key as an []byte. If the key is not present, it returns (nil, nil). If the underlying value is not a slice of bytes or an addressable array of bytes, it returns (nil, error).

func (Headers) GetInt

func (h Headers) GetInt(k key.IntKey) (int64, error)

GetInt returns the value for the key as an int64. If the key is not present, it returns (0, nil). If the underlying value's Kind is not Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Int64, or the value is overflows, it returns (0, error).

func (Headers) GetSmallInt

func (h Headers) GetSmallInt(k key.IntKey) (int, error)

GetSmallInt returns the value for the key as an int in [-65536, 65536]. If the key is not present, it returns (0, nil). If the underlying value's Kind is not Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Int64, or the value's range is out of [-65536, 65536], it returns (0, error).

func (Headers) GetString

func (h Headers) GetString(k key.IntKey) (string, error)

GetString returns the value for the key as an string. If the key is not present, it returns ("", nil). If the underlying value is not a string, it returns ("", error).

func (Headers) GetUint

func (h Headers) GetUint(k key.IntKey) (uint64, error)

GetUint returns the value for the key as an uint64. If the key is not present, it returns (0, nil). If the underlying value's Kind is not Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Int64, or the value is overflows, it returns (0, error).

type Mac0Message

type Mac0Message[T any] struct {
	Protected   Headers
	Unprotected Headers
	Payload     T
	// contains filtered or unexported fields
}

Mac0Message represents a COSE_Mac0 object.

Reference https://datatracker.ietf.org/doc/html/rfc9052#name-signing-with-one-signer

func VerifyMac0Message

func VerifyMac0Message[T any](macer key.MACer, coseData, externalData []byte) (*Mac0Message[T], error)

VerifyMac0Message verifies and decodes a COSE_Mac0 message with a MACer and returns a *Mac0Message. `externalData` should be the same as the one used when computing.

func (*Mac0Message[T]) Bytesify

func (m *Mac0Message[T]) Bytesify() []byte

Bytesify returns a CBOR-encoded byte slice. It returns nil if MarshalCBOR failed.

func (*Mac0Message[T]) Compute

func (m *Mac0Message[T]) Compute(macer key.MACer, externalData []byte) error

Compute computes a COSE_Mac0 message' MAC with a MACer. `externalData` can be nil. https://datatracker.ietf.org/doc/html/rfc9052#name-externally-supplied-data

func (*Mac0Message[T]) ComputeAndEncode

func (m *Mac0Message[T]) ComputeAndEncode(macer key.MACer, externalData []byte) ([]byte, error)

ComputeAndEncode computes and encodes a COSE_Mac0 message with a MACer. `externalData` can be nil. https://datatracker.ietf.org/doc/html/rfc9052#name-externally-supplied-data

func (*Mac0Message[T]) MarshalCBOR

func (m *Mac0Message[T]) MarshalCBOR() ([]byte, error)

MarshalCBOR implements the CBOR Marshaler interface for Mac0Message. It should call `Mac0Message.WithSign` before calling this method.

func (*Mac0Message[T]) Tag

func (m *Mac0Message[T]) Tag() []byte

Tag returns the MAC tag of the Mac0Message. If the MAC is not computed, it returns nil.

func (*Mac0Message[T]) UnmarshalCBOR

func (m *Mac0Message[T]) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements the CBOR Unmarshaler interface for Mac0Message.

func (*Mac0Message[T]) Verify

func (m *Mac0Message[T]) Verify(macer key.MACer, externalData []byte) error

Verify verifies a COSE_Mac0 message' MAC with a MACer. It should call `Mac0Message.UnmarshalCBOR` before calling this method. `externalData` should be the same as the one used when computing.

type MacMessage added in v0.5.0

type MacMessage[T any] struct {
	Protected   Headers
	Unprotected Headers
	Payload     T
	// contains filtered or unexported fields
}

MacMessage represents a COSE_Mac object.

Reference https://datatracker.ietf.org/doc/html/rfc9052#name-maced-message-with-recipien

func VerifyMacMessage added in v0.5.0

func VerifyMacMessage[T any](macer key.MACer, coseData, externalData []byte) (*MacMessage[T], error)

VerifyMacMessage verifies and decodes a COSE_Mac message with a MACer and returns a *MacMessage. `externalData` should be the same as the one used when computing.

func (*MacMessage[T]) AddRecipient added in v0.5.0

func (m *MacMessage[T]) AddRecipient(recipient *Recipient) error

AddRecipient add a Recipient to the COSE_Mac message.

func (*MacMessage[T]) Bytesify added in v0.5.0

func (m *MacMessage[T]) Bytesify() []byte

Bytesify returns a CBOR-encoded byte slice. It returns nil if MarshalCBOR failed.

func (*MacMessage[T]) Compute added in v0.5.0

func (m *MacMessage[T]) Compute(macer key.MACer, externalData []byte) error

Compute computes a COSE_Mac message' MAC with a MACer. `externalData` can be nil. https://datatracker.ietf.org/doc/html/rfc9052#name-externally-supplied-data

func (*MacMessage[T]) ComputeAndEncode added in v0.5.0

func (m *MacMessage[T]) ComputeAndEncode(macer key.MACer, externalData []byte) ([]byte, error)

ComputeAndEncode computes and encodes a COSE_Mac message with a MACer. `externalData` can be nil. https://datatracker.ietf.org/doc/html/rfc9052#name-externally-supplied-data

func (*MacMessage[T]) MarshalCBOR added in v0.5.0

func (m *MacMessage[T]) MarshalCBOR() ([]byte, error)

MarshalCBOR implements the CBOR Marshaler interface for MacMessage. It should call `MacMessage.WithSign` before calling this method.

func (*MacMessage[T]) Recipients added in v0.5.0

func (m *MacMessage[T]) Recipients() []*Recipient

Recipients returns recipients in the COSE_Mac message

func (*MacMessage[T]) Tag added in v0.5.0

func (m *MacMessage[T]) Tag() []byte

Tag returns the MAC tag of the MacMessage. If the MAC is not computed, it returns nil.

func (*MacMessage[T]) UnmarshalCBOR added in v0.5.0

func (m *MacMessage[T]) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements the CBOR Unmarshaler interface for MacMessage.

func (*MacMessage[T]) Verify added in v0.5.0

func (m *MacMessage[T]) Verify(macer key.MACer, externalData []byte) error

Verify verifies a COSE_Mac message' MAC with a MACer. It should call `MacMessage.UnmarshalCBOR` before calling this method. `externalData` should be the same as the one used when computing.

type Recipient added in v0.5.0

type Recipient struct {
	Protected   Headers
	Unprotected Headers
	Plaintext   []byte
	// contains filtered or unexported fields
}

Recipient represents a COSE_recipient object.

Reference https://datatracker.ietf.org/doc/html/rfc9052#name-enveloped-cose-structure

func (*Recipient) AddRecipient added in v0.5.0

func (m *Recipient) AddRecipient(recipient *Recipient) error

func (*Recipient) Decrypt added in v0.5.0

func (m *Recipient) Decrypt(encryptor key.Encryptor, externalData []byte) error

Decrypt decrypts a COSE_Encrypt0 message with a Encryptor. It should call `Encrypt0Message.UnmarshalCBOR` before calling this method. `externalData` should be the same as the one used in Encrypt0Message.Encrypt.

func (*Recipient) Encrypt added in v0.5.0

func (m *Recipient) Encrypt(encryptor key.Encryptor, externalData []byte) error

Encrypt encrypt a COSE_Encrypt0 message with a Encryptor. `externalData` can be nil. https://datatracker.ietf.org/doc/html/rfc9052#name-externally-supplied-data

func (*Recipient) MarshalCBOR added in v0.5.0

func (m *Recipient) MarshalCBOR() ([]byte, error)

MarshalCBOR implements the CBOR Marshaler interface for Encrypt0Message. It should call `Encrypt0Message.Encrypt` before calling this method.

func (*Recipient) Recipients added in v0.5.0

func (m *Recipient) Recipients() []*Recipient

func (*Recipient) UnmarshalCBOR added in v0.5.0

func (m *Recipient) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements the CBOR Unmarshaler interface for Mac0Message.

type Sign1Message

type Sign1Message[T any] struct {
	Protected   Headers
	Unprotected Headers
	Payload     T
	// contains filtered or unexported fields
}

Sign1Message represents a COSE_Sign1 object.

Reference https://datatracker.ietf.org/doc/html/rfc9052#name-signing-with-one-signer

func VerifySign1Message

func VerifySign1Message[T any](verifier key.Verifier, coseData, externalData []byte) (*Sign1Message[T], error)

VerifySign1Message verifies and decodes a COSE_Sign1 message with a Verifier and returns a *Sign1Message. `externalData` should be the same as the one used when signing.

func (*Sign1Message[T]) Bytesify

func (m *Sign1Message[T]) Bytesify() []byte

Bytesify returns a CBOR-encoded byte slice. It returns nil if MarshalCBOR failed.

func (*Sign1Message[T]) MarshalCBOR

func (m *Sign1Message[T]) MarshalCBOR() ([]byte, error)

MarshalCBOR implements the CBOR Marshaler interface for Sign1Message. It should call `Sign1Message.WithSign` before calling this method.

func (*Sign1Message[T]) SignAndEncode

func (m *Sign1Message[T]) SignAndEncode(signer key.Signer, externalData []byte) ([]byte, error)

SignAndEncode signs and encodes a COSE_Sign1 message with a Signer. `externalData` can be nil. https://datatracker.ietf.org/doc/html/rfc9052#name-externally-supplied-data

func (*Sign1Message[T]) Signature

func (m *Sign1Message[T]) Signature() []byte

Signature returns the signature of the Sign1Message. If the Sign1Message is not signed, it returns nil.

func (*Sign1Message[T]) UnmarshalCBOR

func (m *Sign1Message[T]) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements the CBOR Unmarshaler interface for Sign1Message.

func (*Sign1Message[T]) Verify

func (m *Sign1Message[T]) Verify(verifier key.Verifier, externalData []byte) error

Verify verifies a COSE_Sign1 message with a Verifier. It should call `Sign1Message.UnmarshalCBOR` before calling this method. `externalData` should be the same as the one used when signing.

func (*Sign1Message[T]) WithSign

func (m *Sign1Message[T]) WithSign(signer key.Signer, externalData []byte) error

WithSign signs a COSE_Sign1 message with a Signer. `externalData` can be nil. https://datatracker.ietf.org/doc/html/rfc9052#name-externally-supplied-data

type SignMessage

type SignMessage[T any] struct {
	Protected   Headers
	Unprotected Headers
	Payload     T
	// contains filtered or unexported fields
}

SignMessage represents a COSE_Sign object.

Reference https://datatracker.ietf.org/doc/html/rfc9052#name-signing-with-one-or-more-si

func VerifySignMessage

func VerifySignMessage[T any](verifiers key.Verifiers, coseData, externalData []byte) (*SignMessage[T], error)

VerifySignMessage verifies and decodes a COSE_Sign format with some Verifiers and returns a *SignMessage. `externalData` should be the same as the one used when signing.

func (*SignMessage[T]) Bytesify

func (m *SignMessage[T]) Bytesify() []byte

Bytesify returns a CBOR-encoded byte slice. It returns nil if MarshalCBOR failed.

func (*SignMessage[T]) MarshalCBOR

func (m *SignMessage[T]) MarshalCBOR() ([]byte, error)

MarshalCBOR implements the CBOR Marshaler interface for SignMessage. It should call `SignMessage.WithSign` before calling this method.

func (*SignMessage[T]) SignAndEncode

func (m *SignMessage[T]) SignAndEncode(signers key.Signers, externalData []byte) ([]byte, error)

SignAndEncode signs and encodes a COSE_Sign message with some Signers. `externalData` can be nil. https://datatracker.ietf.org/doc/html/rfc9052#name-externally-supplied-data

func (*SignMessage[t]) Signatures

func (m *SignMessage[t]) Signatures() []*Signature

Signatures returns the signatures of the SignMessage. If the SignMessage is not signed, it returns nil.

func (*SignMessage[T]) UnmarshalCBOR

func (m *SignMessage[T]) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements the CBOR Unmarshaler interface for SignMessage.

func (*SignMessage[T]) Verify

func (m *SignMessage[T]) Verify(verifiers key.Verifiers, externalData []byte) error

Verify verifies a COSE_Sign message with some Verifiers. It should call `SignMessage.UnmarshalCBOR` before calling this method. `externalData` should be the same as the one used when signing.

func (*SignMessage[T]) WithSign

func (m *SignMessage[T]) WithSign(signers key.Signers, externalData []byte) error

WithSign signs a COSE_Sign message with some Signers. `externalData` can be nil. https://datatracker.ietf.org/doc/html/rfc9052#name-externally-supplied-data

type Signature

type Signature struct {
	Protected   Headers
	Unprotected Headers
	// contains filtered or unexported fields
}

Signature represents a COSE_Signature object.

func (*Signature) Kid

func (s *Signature) Kid() key.ByteStr

Kid returns the kid of the Signature which key signed. If the SignMessage is not signed, it returns nil.

func (*Signature) MarshalCBOR

func (s *Signature) MarshalCBOR() ([]byte, error)

MarshalCBOR implements the CBOR Marshaler interface for Signature.

func (*Signature) Signature

func (s *Signature) Signature() []byte

Signature returns the signature of the Signature. If the SignMessage is not signed, it returns nil.

func (*Signature) UnmarshalCBOR

func (s *Signature) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements the CBOR Unmarshaler interface for Signature.

Jump to

Keyboard shortcuts

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