My Project: dht::crypto Namespace Reference

My Project 3.7.7 C++ Distributed Hash Table

Loading...
Searching...
No Matches
Classes | Typedefs | Enumerations | Functions
dht::crypto Namespace Reference

## Classes
class | CryptoException
class | DecryptError
struct | PublicKey
struct | PrivateKey
class | RevocationList
class | CertificateRequest
class | OcspRequest
class | OcspResponse
struct | Certificate
struct | TrustList
class | secure_vector

## Typedefs
using | Identity = std::pair<std::shared_ptr<PrivateKey>, std::shared_ptr<Certificate>>
using | SecureBlob = secure_vector<uint8_t>

## Enumerations
enum class | NameType { UNKNOWN = 0 , RFC822 , DNS , URI , IP }

## Functions
OPENDHT_PUBLIC Identity | generateIdentity (const std::string &name, const Identity &ca, unsigned key_length, bool is_ca)
OPENDHT_PUBLIC Identity | generateIdentity (const std::string &name="dhtnode", const Identity &ca={}, unsigned key_length=4096)
OPENDHT_PUBLIC Identity | generateEcIdentity (const std::string &name, const Identity &ca, bool is_ca)
OPENDHT_PUBLIC Identity | generateEcIdentity (const std::string &name="dhtnode", const Identity &ca={})
OPENDHT_PUBLIC void | saveIdentity (const Identity &id, const std::string &path, const std::string &privkey_password={})
OPENDHT_PUBLIC Identity | loadIdentity (const std::string &path, const std::string &privkey_password={})
OPENDHT_PUBLIC Blob | hash (const Blob &data, size_t hash_length=512/8)
OPENDHT_PUBLIC void | hash (const uint8_t *data, size_t data_length, uint8_t *hash, size_t hash_length)
OPENDHT_PUBLIC Blob | stretchKey (std::string_view password, Blob &salt, size_t key_length=512/8)
OPENDHT_PUBLIC Blob | aesEncrypt (const uint8_t *data, size_t data_length, const Blob &key)
OPENDHT_PUBLIC Blob | aesEncrypt (const Blob &data, const Blob &key)
OPENDHT_PUBLIC Blob | aesEncrypt (const Blob &data, std::string_view password, const Blob &salt={})
OPENDHT_PUBLIC Blob | aesDecrypt (const uint8_t *data, size_t data_length, const Blob &key)
OPENDHT_PUBLIC Blob | aesDecrypt (const Blob &data, const Blob &key)
OPENDHT_PUBLIC Blob | aesDecrypt (std::string_view data, const Blob &key)
OPENDHT_PUBLIC Blob | aesDecrypt (const uint8_t *data, size_t data_length, std::string_view password)
OPENDHT_PUBLIC Blob | aesDecrypt (const Blob &data, std::string_view password)
OPENDHT_PUBLIC Blob | aesDecrypt (std::string_view data, std::string_view password)
OPENDHT_PUBLIC Blob | aesGetKey (const uint8_t *data, size_t data_length, std::string_view password)
OPENDHT_PUBLIC Blob | aesGetKey (const Blob &data, std::string_view password)
OPENDHT_PUBLIC Blob | aesGetSalt (const uint8_t *data, size_t data_length)
OPENDHT_PUBLIC Blob | aesGetSalt (const Blob &data)
OPENDHT_PUBLIC std::string_view | aesGetEncrypted (const uint8_t *data, size_t data_length)
OPENDHT_PUBLIC std::string_view | aesGetEncrypted (const Blob &data)
OPENDHT_PUBLIC Blob | aesBuildEncrypted (const uint8_t *encryptedData, size_t data_length, const Blob &salt)
OPENDHT_PUBLIC Blob | aesBuildEncrypted (const Blob &encryptedData, const Blob &salt)
OPENDHT_PUBLIC Blob | aesBuildEncrypted (std::string_view encryptedData, const Blob &salt)
OPENDHT_PUBLIC Blob | webPushEncrypt (const Blob &p256dh, const Blob &auth, const uint8_t *payload, size_t payload_length, const PrivateKey &privKey={}, const Blob &salt={})
template<class T = std::mt19937, std::size_t N = T::state_size + 1>
auto | getSeededRandomEngine () -> typename std::enable_if<!!N, T >::type
template<class T = std::mt19937, std::size_t N = T::state_size + 1>
auto | getDerivedRandomEngine (T &source) -> typename std::enable_if<!!N, T >::type

## Detailed Description

Contains all crypto primitives

## Typedef Documentation

## ◆ Identity

using dht::crypto::Identity = std::pair<std::shared_ptr<PrivateKey>, std::shared_ptr<Certificate>>

Definition at line 62 of file crypto.h.

## ◆ SecureBlob

using dht::crypto::SecureBlob = secure_vector<uint8_t>

Definition at line 118 of file secure_vector.h.

## Enumeration Type Documentation

## ◆ NameType

enum class dht::crypto::NameType
| strong

Definition at line 309 of file crypto.h.

## Function Documentation

## ◆ aesBuildEncrypted() [1/3]

OPENDHT_PUBLIC Blob dht::crypto::aesBuildEncrypted | ( | const Blob & | encryptedData,
|  | const Blob & | salt )
| inline

Definition at line 838 of file crypto.h.

## ◆ aesBuildEncrypted() [2/3]

OPENDHT_PUBLIC Blob dht::crypto::aesBuildEncrypted | ( | const uint8_t * | encryptedData,
|  | size_t | data_length,
|  | const Blob & | salt )

Build an encrypted bundle that can be decrypted with aesDecrypt(data, password).

Parameters

  encryptedData | result of aesEncrypt(data, key) or aesGetEncrypted
  salt | should match the encryption key and password so that stretchKey(password, salk) == key. Can be obtained from an existing bundle with aesGetSalt.

## ◆ aesBuildEncrypted() [3/3]

OPENDHT_PUBLIC Blob dht::crypto::aesBuildEncrypted | ( | std::string_view | encryptedData,
|  | const Blob & | salt )
| inline

Definition at line 842 of file crypto.h.

## ◆ aesDecrypt() [1/5]

OPENDHT_PUBLIC Blob dht::crypto::aesDecrypt | ( | const Blob & | data,
|  | const Blob & | key )
| inline

Definition at line 789 of file crypto.h.

## ◆ aesDecrypt() [2/5]

OPENDHT_PUBLIC Blob dht::crypto::aesDecrypt | ( | const Blob & | data,
|  | std::string_view | password )
| inline

Definition at line 801 of file crypto.h.

## ◆ aesDecrypt() [3/5]

OPENDHT_PUBLIC Blob dht::crypto::aesDecrypt | ( | const uint8_t * | data,
|  | size_t | data_length,
|  | const Blob & | key )

AES-GCM decryption.

## ◆ aesDecrypt() [4/5]

OPENDHT_PUBLIC Blob dht::crypto::aesDecrypt | ( | std::string_view | data,
|  | const Blob & | key )
| inline

Definition at line 794 of file crypto.h.

## ◆ aesDecrypt() [5/5]

OPENDHT_PUBLIC Blob dht::crypto::aesDecrypt | ( | std::string_view | data,
|  | std::string_view | password )
| inline

Definition at line 806 of file crypto.h.

## ◆ aesEncrypt() [1/3]

OPENDHT_PUBLIC Blob dht::crypto::aesEncrypt | ( | const Blob & | data,
|  | const Blob & | key )
| inline

Definition at line 768 of file crypto.h.

## ◆ aesEncrypt() [2/3]

OPENDHT_PUBLIC Blob dht::crypto::aesEncrypt | ( | const Blob & | data,
|  | std::string_view | password,
|  | const Blob & | salt = {} )

AES-GCM encryption with argon2 key derivation. This function uses stretchKey to generate an AES key from the password and a random salt. The result is a bundle including the salt that can be decrypted with aesDecrypt(data, password). If needed, the salt or encrypted data can be individually extracted from the bundle with aesGetSalt and aesGetEncrypted.

Parameters

  data | data to encrypt
  password | password to encrypt the data with
  salt | optional salt to use for key derivation. If not provided, a random salt will be generated.

## ◆ aesEncrypt() [3/3]

OPENDHT_PUBLIC Blob dht::crypto::aesEncrypt | ( | const uint8_t * | data,
|  | size_t | data_length,
|  | const Blob & | key )

AES-GCM encryption. Key must be 128, 192 or 256 bits long (16, 24 or 32 bytes).

## ◆ aesGetEncrypted() [1/2]

OPENDHT_PUBLIC std::string_view dht::crypto::aesGetEncrypted | ( | const Blob & | data | ) |
| inline

Definition at line 827 of file crypto.h.

## ◆ aesGetEncrypted() [2/2]

OPENDHT_PUBLIC std::string_view dht::crypto::aesGetEncrypted | ( | const uint8_t * | data,
|  | size_t | data_length )

Get the encrypted data (ciphertext) part of data password-encrypted with aesEncrypt(data, password)

## ◆ aesGetKey() [1/2]

OPENDHT_PUBLIC Blob dht::crypto::aesGetKey | ( | const Blob & | data,
|  | std::string_view | password )
| inline

Definition at line 815 of file crypto.h.

## ◆ aesGetKey() [2/2]

OPENDHT_PUBLIC Blob dht::crypto::aesGetKey | ( | const uint8_t * | data,
|  | size_t | data_length,
|  | std::string_view | password )

Get raw AES key from password and salt stored with the encrypted data.

## ◆ aesGetSalt() [1/2]

OPENDHT_PUBLIC Blob dht::crypto::aesGetSalt | ( | const Blob & | data | ) |
| inline

Definition at line 821 of file crypto.h.

## ◆ aesGetSalt() [2/2]

OPENDHT_PUBLIC Blob dht::crypto::aesGetSalt | ( | const uint8_t * | data,
|  | size_t | data_length )

Get the salt part of data password-encrypted with aesEncrypt(data, password)

## ◆ generateIdentity()

OPENDHT_PUBLIC Identity dht::crypto::generateIdentity | ( | const std::string & | name,
|  | const Identity & | ca,
|  | unsigned | key_length,
|  | bool | is_ca )

Generate an RSA key pair (4096 bits) and a certificate.

Parameters

  name | the name used in the generated certificate
  ca | if set, the certificate authority that will sign the generated certificate. If not set, the generated certificate will be a self-signed CA.
  key_length | stength of the generated private key (bits).

## ◆ getDerivedRandomEngine()

template<class T = std::mt19937, std::size_t N = T::state_size + 1>

auto dht::crypto::getDerivedRandomEngine | ( | T & | source | ) | -> typename std::enable_if<!!N, T >::type

Generate a random engine from another source.

Definition at line 52 of file rng.h.

## ◆ getSeededRandomEngine()

template<class T = std::mt19937, std::size_t N = T::state_size + 1>

auto dht::crypto::getSeededRandomEngine | ( |  | ) | -> typename std::enable_if<!!N, T >::type

Generate a seeded random engine.

Definition at line 19 of file rng.h.

## ◆ hash()

OPENDHT_PUBLIC Blob dht::crypto::hash | ( | const Blob & | data,
|  | size_t | hash_length = 512/8 )

Performs SHA512, SHA256 or SHA1, depending on hash_length. Attempts to choose an hash function with output size of at least hash_length bytes, Current implementation will use SHA1 for hash_length up to 20 bytes, will use SHA256 for hash_length up to 32 bytes, will use SHA512 for hash_length of 33 bytes and more.

## ◆ stretchKey()

OPENDHT_PUBLIC Blob dht::crypto::stretchKey | ( | std::string_view | password,
|  | Blob & | salt,
|  | size_t | key_length = 512/8 )

Generates an encryption key from a text password, making the key longer to bruteforce. The generated key also depends on a unique salt value of any size, that can be transmitted in clear, and will be generated if not provided (32 bytes).

## ◆ webPushEncrypt()

OPENDHT_PUBLIC Blob dht::crypto::webPushEncrypt | ( | const Blob & | p256dh,
|  | const Blob & | auth,
|  | const uint8_t * | payload,
|  | size_t | payload_length,
|  | const PrivateKey & | privKey = {},
|  | const Blob & | salt = {} )

Encrypts a payload for WebPush (RFC 8291).

Parameters

  p256dh | The user's P-256 public key (uncompressed, 65 bytes).
  auth | The user's authentication secret (16 bytes).
  payload | The data to encrypt.
  privKey | (Optional) The sender's private key (for testing).
  salt | (Optional) The salt to use (for testing).

Returns
  The encrypted body (including salt, record size, keyid).