DiceKeys Seeded Cryptography Library
|
A SymmetricKey can be used to seal and unseal messages. This SymmetricKey class can be (re) derived from a seed using set of recipe specified in JSON Format for Recipes. So, you can use this symmetric-key to seal a message, throw the key away, and re-generate the key when you need to unseal the message so long as you still have the original seed and recipe. More...
#include <symmetric-key.hpp>
Public Member Functions | |
SymmetricKey (const SodiumBuffer &keyBytes, std::string recipe) | |
Construct a SymmetricKey from its members. | |
SymmetricKey (const SymmetricKey &other) | |
Construct a SymmetricKey by copying another one. | |
SymmetricKey (const std::string &seedString, const std::string &recipe) | |
Construct a new SymmetricKey by (re)deriving it from a seed string and a set of recipe in JSON Format for Recipes. More... | |
const std::vector< unsigned char > | sealToCiphertextOnly (const unsigned char *message, const size_t messageLength, const std::string &unsealingInstructions={}) const |
Seal a plaintext message. More... | |
const std::vector< unsigned char > | sealToCiphertextOnly (const SodiumBuffer &message, const std::string &unsealingInstructions={}) const |
Seal a plaintext message. More... | |
const PackagedSealedMessage | seal (const SodiumBuffer &message, const std::string &unsealingInstructions={}) const |
Seal a plaintext message. More... | |
const PackagedSealedMessage | seal (const std::string &message, const std::string &unsealingInstructions={}) const |
Seal a plaintext message. More... | |
const PackagedSealedMessage | seal (const std::vector< unsigned char > &message, const std::string &unsealingInstructions={}) const |
Seal a plaintext message. More... | |
const PackagedSealedMessage | seal (const unsigned char *message, const size_t messageLength, const std::string &unsealingInstructions={}) const |
Seal a plaintext message. More... | |
const SodiumBuffer | unseal (const unsigned char *ciphertext, const size_t ciphertextLength, const std::string &unsealingInstructions={}) const |
Unseal a message. More... | |
const SodiumBuffer | unseal (const std::vector< unsigned char > &ciphertext, const std::string &unsealingInstructions={}) const |
Unseal a message. More... | |
const SodiumBuffer | unseal (const PackagedSealedMessage &packagedSealedMessage) const |
Unseal a message by re-deriving the SymmetricKey from a seed. 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, recipe) More... | |
Static Public Member Functions | |
static SymmetricKey | deriveFromSeed (const std::string &seedString, const std::string &recipe) |
Construct a new SymmetricKey by (re)deriving it from a seed string and a set of recipe in JSON Format for Recipes. More... | |
static const SodiumBuffer | unseal (const PackagedSealedMessage &packagedSealedMessage, const std::string &seedString) |
Unseal a message by re-deriving the SymmetricKey from a seed. More... | |
static SymmetricKey | fromSerializedBinaryForm (const SodiumBuffer &serializedBinaryForm) |
Deserialize from a byte array stored as a list of: (keyBytes, recipe) More... | |
static SymmetricKey | fromJson (const std::string &symmetricKeyAsJson) |
Internal implementation of JSON parser for the JSON contructor. | |
Public Attributes | |
const SodiumBuffer | keyBytes |
The binary representation of the symmetric key. More... | |
const std::string | recipe |
A JSON Format for Recipes string used to specify how this key is derived. | |
Protected Member Functions | |
const SodiumBuffer | unsealMessageContents (const unsigned char *ciphertext, const size_t ciphertextLength, const std::string &unsealingInstructions={}) const |
Internal implementation of unseal. | |
A SymmetricKey can be used to seal and unseal messages. This SymmetricKey class can be (re) derived from a seed using set of recipe specified in JSON Format for Recipes. So, you can use this symmetric-key to seal a message, throw the key away, and re-generate the key when you need to unseal the message so long as you still have the original seed and recipe.
Sealing a message (plaintext) creates a _ciphertext which contains the message but from which observers who do not have the UnsealingKey cannot discern the contents of the message. Sealing also provides integrity-protection, which will preven the message from being unsealed if it is modified. We use the verbs seal and unseal, rather than encrypt and decrypt, because the encrypting alone does not confer that the message includes an integrity (message authentication) code to prove that the ciphertext has not been tampered with.
The seal operation is built on LibSodium's crypto_secretbox_easy function, but despite it's name the construct isn't as easy as it should be. The caller must store both the ciphertext AND a 24-byte nonce (crypto_secretbox_NONCEBYTES = 24). Hence, the SymmetricKey seal operation outputs a composite ciphertext containing the nonce followed by the "secret box" ciphertext generated by LibSodium. Since the "secret box" is 16 bytes longer than the message size (crypto_secretbox_MACBYTES = 16), the composite ciphertext is is 40 bytes longer than the message length (24 for then nonce, plus the 16 added to create the secret box)
SymmetricKey::SymmetricKey | ( | const std::string & | seedString, |
const std::string & | recipe | ||
) |
Construct a new SymmetricKey by (re)deriving it from a seed string and a set of recipe in JSON Format for Recipes.
seedString | The private seed which is used to generate the key. Anyone who knows (or can guess) this seed can re-generate the key by passing it along with the recipe. |
recipe | The recipe in JSON Format for Recipes. |
|
static |
Construct a new SymmetricKey by (re)deriving it from a seed string and a set of recipe in JSON Format for Recipes.
seedString | The private seed which is used to generate the key. Anyone who knows (or can guess) this seed can re-generate the key by passing it along with the recipe. |
recipe | The recipe in JSON Format for Recipes. |
|
static |
Deserialize from a byte array stored as a list of: (keyBytes, recipe)
Stored in SodiumBuffer's fixed-length list format. Strings are stored as UTF8 byte arrays.
const PackagedSealedMessage SymmetricKey::seal | ( | const SodiumBuffer & | message, |
const std::string & | unsealingInstructions = {} |
||
) | const |
Seal a plaintext message.
message | The plaintxt message to seal |
unsealingInstructions | If this optional string is passed, the same string must be passed to unseal the message. It can be used to pair a sealed message with public instructions about what should happen after the message is unsealed. |
const PackagedSealedMessage SymmetricKey::seal | ( | const std::string & | message, |
const std::string & | unsealingInstructions = {} |
||
) | const |
Seal a plaintext message.
message | The plaintxt message to seal |
unsealingInstructions | If this optional string is passed, the same string must be passed to unseal the message. It can be used to pair a sealed message with public instructions about what should happen after the message is unsealed. |
const PackagedSealedMessage SymmetricKey::seal | ( | const std::vector< unsigned char > & | message, |
const std::string & | unsealingInstructions = {} |
||
) | const |
Seal a plaintext message.
message | The plaintxt message to seal |
unsealingInstructions | If this optional string is passed, the same string must be passed to unseal the message. It can be used to pair a sealed message with public instructions about what should happen after the message is unsealed. |
const PackagedSealedMessage SymmetricKey::seal | ( | const unsigned char * | message, |
const size_t | messageLength, | ||
const std::string & | unsealingInstructions = {} |
||
) | const |
Seal a plaintext message.
message | The plaintxt message to seal |
messageLength | The length of the plaintext message to seal |
unsealingInstructions | If this optional string is passed, the same string must be passed to unseal the message. It can be used to pair a sealed message with public instructions about what should happen after the message is unsealed. |
const std::vector< unsigned char > SymmetricKey::sealToCiphertextOnly | ( | const SodiumBuffer & | message, |
const std::string & | unsealingInstructions = {} |
||
) | const |
Seal a plaintext message.
message | The plaintxt message to seal |
unsealingInstructions | If this optional string is passed, the same string must be passed to unseal the message. It can be used to pair a sealed message with public instructions about what should happen after the message is unsealed. |
const std::vector< unsigned char > SymmetricKey::sealToCiphertextOnly | ( | const unsigned char * | message, |
const size_t | messageLength, | ||
const std::string & | unsealingInstructions = {} |
||
) | const |
Seal a plaintext message.
message | The plaintxt message to seal |
messageLength | The length of the plaintext message in bytes |
unsealingInstructions | If this optional string is passed, the same string must be passed to unseal the message. |
const std::string SymmetricKey::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.
indent | The number of characters to indent the JSON (optional) |
indent_char | The character with which to indent the JSON (optional) |
const SodiumBuffer SymmetricKey::toSerializedBinaryForm | ( | ) | const |
Serialize to byte array as a list of: (keyBytes, recipe)
Stored in SodiumBuffer's fixed-length list format. Strings are stored as UTF8 byte arrays.
const SodiumBuffer SymmetricKey::unseal | ( | const PackagedSealedMessage & | packagedSealedMessage | ) | const |
Unseal a message by re-deriving the SymmetricKey from a seed.
packagedSealedMessage | The message to be unsealed |
|
static |
Unseal a message by re-deriving the SymmetricKey from a seed.
seedString | The seed string used to generate the SymmetricKey that sealed this message |
packagedSealedMessage | The message to be unsealed |
const SodiumBuffer SymmetricKey::unseal | ( | const std::vector< unsigned char > & | ciphertext, |
const std::string & | unsealingInstructions = {} |
||
) | const |
Unseal a message.
ciphertext | The sealed message to be unsealed |
unsealingInstructions | If this optional value was set during the SymmetricKey::seal operation, the same value must be provided to unseal the message or the operation will fail. |
CryptographicVerificationFailureException | Thrown if the ciphertext is not valid and cannot be unsealed. |
const SodiumBuffer SymmetricKey::unseal | ( | const unsigned char * | ciphertext, |
const size_t | ciphertextLength, | ||
const std::string & | unsealingInstructions = {} |
||
) | const |
Unseal a message.
ciphertext | The sealed message to be unsealed |
ciphertextLength | The length of the sealed message |
unsealingInstructions | If this optional value was set during the SymmetricKey::seal operation, the same value must be provided to unseal the message or the operation will fail. It can be used to pair a secret (sealed) message with public instructions about what should happen after the message is unsealed. |
CryptographicVerificationFailureException | Thrown if the ciphertext is not valid and cannot be unsealed. |
const SodiumBuffer SymmetricKey::keyBytes |
The binary representation of the symmetric key.