DiceKeys Seeded Cryptography Library
SigningKey Class Reference

SigningKeys generate signatures of messages which can then be used by the corresponding SignatureVerificationKey to verify that a message was signed by can confirm that the message was indeed signed by the SigningKey and has not since been tampered with. More...

#include <signing-key.hpp>

Public Member Functions

 SigningKey (const SigningKey &other)
 Construct a copy of another SigningKey.
 
 SigningKey (const SodiumBuffer &signingKeyBytes, const std::string &recipe)
 Construct from the objects members. More...
 
 SigningKey (const std::string &seedString, const std::string &recipe)
 Construct a new SigningKey by deriving a signing key pair from a seed string and a set of recipe in JSON Format for Recipes. More...
 
const SodiumBuffer getSeedBytes () const
 Extract the 32-byte private seed (the compact representation of the private key) from the 64-byte sodium private key (which contains a copy of the public key, which sodium stores so as to avoid unnecessary computation when the public key is needed).
 
const std::vector< unsigned char > getSignatureVerificationKeyBytes () const
 Get the raw binary representation of the signature-verification key, re-deriving them from the signing key if signatureVerificationKeyBytes is a zero-length vector.
 
const SignatureVerificationKey getSignatureVerificationKey () const
 Get a SignatureVerificationKey which is used to verify signatures generated with this SigningKey.
 
const std::vector< unsigned char > generateSignature (const unsigned char *message, const size_t messageLength) const
 Generate a signature for a message which can be used by the corresponding public SignatureVerificationKey to verify that this message was, in fact, signed by this key. More...
 
const std::vector< unsigned char > generateSignature (const std::vector< unsigned char > &message) const
 Generate a signature for a message, which can be used by the corresponding public SignatureVerificationKey to verify that this message was, in fact, signed by this key. More...
 
const std::string toJson (int indent=-1, const char indent_char=' ') const
 Serialize this object to a JSON-formatted string. More...
 
const SodiumBuffer toSerializedBinaryForm () const
 Serialize to byte array as a list of: (keyBytes, signatureVerificationKeyBytes, recipe) More...
 
const std::string toOpenSshPemPrivateKey (const std::string &comment) const
 Convert to an OpenSSH-format private key binary writiable to a key file.
 
const std::string toOpenSshPublicKey () const
 Convert the signature-verification key to an OpenSSH public key string.
 
const std::string toOpenPgpPemFormatSecretKey (const std::string &UserIdPacketContent, uint32_t timestamp) const
 Convert to an OpenPGP PEM formatted (string) private key. More...
 

Static Public Member Functions

static SigningKey deriveFromSeed (const std::string &seedString, const std::string &recipe)
 Construct a new SigningKey by deriving a signing key pair from a seed string and a set of recipe in JSON Format for Recipes. More...
 
static SigningKey fromJson (const std::string &signingKeyAsJson)
 Construct (reconsitute) the SigningKey from JSON format. The JSON object may or may not contain the signatureVerificationKeyBytes. If it does not, an empty byte vector will be stored and the verification key bytes will be re-derived from the signing key by getSignatureVerificationKeyBytes if they are needed. More...
 
static SigningKey fromSerializedBinaryForm (const SodiumBuffer &serializedBinaryForm)
 Deserialize from a byte array stored as a list of: (keyBytes, signatureVerificationKeyBytes, recipe) More...
 

Public Attributes

const SodiumBuffer signingKeyBytes
 The raw binary representation of the cryptographic signing key.
 
const std::string recipe
 A JSON Format for Recipes string used to specify how this key is derived.
 

Detailed Description

SigningKeys generate signatures of messages which can then be used by the corresponding SignatureVerificationKey to verify that a message was signed by can confirm that the message was indeed signed by the SigningKey and has not since been tampered with.

The corresponding SignatureVerificationKey can be obtained by calling getSignatureVerificationKey.

The key pair of the SigningKey and SignatureVerificationKey is generated from a seed and a set of options in JSON Format for Recipes.

Constructor & Destructor Documentation

◆ SigningKey() [1/2]

SigningKey::SigningKey ( const SodiumBuffer signingKeyBytes,
const std::string &  recipe 
)

Construct from the objects members.

Parameters
signingKeyBytesmay either be a 32-byte ED25519 seed or a 64-byte sodium-style private signing key which embeds the public key so that it doesn't have to be re-computed. If the 32-byte seed is provided, the constructor will compute the 64-byte sodium-style key.

◆ SigningKey() [2/2]

SigningKey::SigningKey ( const std::string &  seedString,
const std::string &  recipe 
)

Construct a new SigningKey by deriving a signing key pair from a seed string and a set of recipe in JSON Format for Recipes.

Parameters
seedStringThe private seed which is used to generate the key pair. Anyone who knows (or can guess) this seed can re-generate the key pair by passing it along with the recipe.
recipeThe recipe in JSON Format for Recipes.

Member Function Documentation

◆ deriveFromSeed()

SigningKey SigningKey::deriveFromSeed ( const std::string &  seedString,
const std::string &  recipe 
)
static

Construct a new SigningKey by deriving a signing key pair from a seed string and a set of recipe in JSON Format for Recipes.

Parameters
seedStringThe private seed which is used to generate the key pair. Anyone who knows (or can guess) this seed can re-generate the key pair by passing it along with the recipe.
recipeThe recipe in JSON Format for Recipes.

◆ fromJson()

SigningKey SigningKey::fromJson ( const std::string &  signingKeyAsJson)
static

Construct (reconsitute) the SigningKey from JSON format. The JSON object may or may not contain the signatureVerificationKeyBytes. If it does not, an empty byte vector will be stored and the verification key bytes will be re-derived from the signing key by getSignatureVerificationKeyBytes if they are needed.

Parameters
signingKeyAsJson

◆ fromSerializedBinaryForm()

SigningKey SigningKey::fromSerializedBinaryForm ( const SodiumBuffer serializedBinaryForm)
static

Deserialize from a byte array stored as a list of: (keyBytes, signatureVerificationKeyBytes, recipe)

Stored in SodiumBuffer's fixed-length list format. Strings are stored as UTF8 byte arrays.

◆ generateSignature() [1/2]

const std::vector< unsigned char > SigningKey::generateSignature ( const std::vector< unsigned char > &  message) const

Generate a signature for a message, which can be used by the corresponding public SignatureVerificationKey to verify that this message was, in fact, signed by this key.

Parameters
messageThe message to sign by generating the signature
Returns
const std::vector<unsigned char> A signature, which can be used with the SignatureVerificationKey to prove that this act of signing (this call to generateSignature) took place.

◆ generateSignature() [2/2]

const std::vector< unsigned char > SigningKey::generateSignature ( const unsigned char *  message,
const size_t  messageLength 
) const

Generate a signature for a message which can be used by the corresponding public SignatureVerificationKey to verify that this message was, in fact, signed by this key.

Parameters
messageThe message to sign by generating the signature
messageLengthThe length of the message.
Returns
const std::vector<unsigned char> A signature, which can be used with the SignatureVerificationKey to prove that this act of signing (this call to generateSignature) took place.

◆ toJson()

const std::string SigningKey::toJson ( int  indent = -1,
const char  indent_char = ' ' 
) const

Serialize this object to a JSON-formatted string.

It can be reconstituted by calling the constructor with this string.

Parameters
minimizeSizeByRemovingTheSignatureVerificationKeyBytesWhichCanBeRegeneratedLaterThe JSON-encoding will always include the binary signing key bytes (in hex format) and the keyDerviationOptionsJson used to derive the key, but the signature-verification key bytes will not be included unless you set this value to false. Rather, if it is elided, the signature-verification key can be reconstituted from the signing-key after the object is reconstituted, which takes a little computation in return for the space saved in the JSON format.
indentThe number of characters to indent the JSON (optional)
indent_charThe character with which to indent the JSON (optional)
Returns
const std::string

◆ toOpenPgpPemFormatSecretKey()

const std::string SigningKey::toOpenPgpPemFormatSecretKey ( const std::string &  UserIdPacketContent,
uint32_t  timestamp 
) const

Convert to an OpenPGP PEM formatted (string) private key.

https://datatracker.ietf.org/doc/html/rfc4880#section-5.11

Parameters
nameThe keyholder's name to appear in the PEM format
emailThe keyholder's email to appear in the PEM format.
timestampseconds elapsed since midnight, 1 January 1970 UTC. https://datatracker.ietf.org/doc/html/rfc4880#section-3.5

◆ toSerializedBinaryForm()

const SodiumBuffer SigningKey::toSerializedBinaryForm ( ) const

Serialize to byte array as a list of: (keyBytes, signatureVerificationKeyBytes, recipe)

Stored in SodiumBuffer's fixed-length list format. Strings are stored as UTF8 byte arrays.

Parameters
minimizeSizeByRemovingTheSignatureVerificationKeyBytesWhichCanBeRegeneratedLaterIf set to true (the default), an empty buffer will be passed for the signatureVerificationKeyBytes. After the object is deserialized, the replica can re-generate a signature-verification key from the signing key, which takes a little computation in return for the 28 bytes saved in this format.

The documentation for this class was generated from the following files: