libcdoc 0.1.8
libcdoc::CryptoBackend Struct Reference

An authentication provider. More...

#include <CryptoBackend.h>

Inherited by libcdoc::PKCS11Backend, and libcdoc::WinBackend.

Public Types

enum  HashAlgorithm : uint32_t { SHA_224 , SHA_256 , SHA_384 , SHA_512 }
 

Public Member Functions

 CryptoBackend ()=default
 
virtual ~CryptoBackend () noexcept=default
 
 CryptoBackend (const CryptoBackend &)=delete
 
CryptoBackendoperator= (const CryptoBackend &)=delete
 
 CDOC_DISABLE_MOVE (CryptoBackend)
 
virtual std::string getLastErrorStr (result_t code) const
 
virtual result_t random (std::vector< uint8_t > &dst, unsigned int size)
 Fill vector with random bytes.
 
virtual result_t deriveECDH1 (std::vector< uint8_t > &dst, const std::vector< uint8_t > &public_key, unsigned int idx)
 Derive shared secret.
 
virtual result_t decryptRSA (std::vector< uint8_t > &dst, const std::vector< uint8_t > &data, bool oaep, unsigned int idx)
 decryptRSA
 
virtual result_t deriveConcatKDF (std::vector< uint8_t > &dst, const std::vector< uint8_t > &public_key, const std::string &digest, const std::vector< uint8_t > &algorithm_id, const std::vector< uint8_t > &party_uinfo, const std::vector< uint8_t > &party_vinfo, unsigned int idx)
 Derive key by ConcatKDF algorithm.
 
virtual result_t deriveHMACExtract (std::vector< uint8_t > &dst, const std::vector< uint8_t > &public_key, const std::vector< uint8_t > &salt, unsigned int idx)
 Get CDoc2 KEK pre-master from ECC key.
 
virtual result_t getSecret (std::vector< uint8_t > &dst, unsigned int idx)
 Get secret value (either password or symmetric key) for a lock.
 
virtual result_t getKeyMaterial (std::vector< uint8_t > &dst, const std::vector< uint8_t > &pw_salt, int32_t kdf_iter, unsigned int idx)
 Get CDoc2 key material for HKDF expansion.
 
virtual result_t extractHKDF (std::vector< uint8_t > &dst, const std::vector< uint8_t > &salt, const std::vector< uint8_t > &pw_salt, int32_t kdf_iter, unsigned int idx)
 Get CDoc2 KEK pre-master from symmetric key.
 
virtual result_t sign (std::vector< uint8_t > &dst, HashAlgorithm algorithm, const std::vector< uint8_t > &digest, unsigned int idx)
 sign Sign message with given algorithm
 
virtual int test (libcdoc::Lock &lock)
 

Static Public Attributes

static constexpr int INVALID_PARAMS = -201
 
static constexpr int OPENSSL_ERROR = -202
 
static constexpr int ECC_KEY_LEN = 32
 

Detailed Description

An authentication provider.

Implements cryptographic methods that may need either user action (supplying password) or external communication (PKCS11). At minimum one should implement:

  • deriveECDH1 for ECC keys
  • decryptRSA for RSA keys
  • getSecret for symmetric keys.

ECC and symmetric keys have also frontend methods; implementing these allows the program to perform certain cryptographic procedures in controlled environment and (in case of symmetric keys) avoid exposing secret keys/passwords.

Member Enumeration Documentation

◆ HashAlgorithm

Enumerator
SHA_224 
SHA_256 
SHA_384 
SHA_512 

Constructor & Destructor Documentation

◆ CryptoBackend() [1/2]

libcdoc::CryptoBackend::CryptoBackend ( )
default

◆ ~CryptoBackend()

virtual libcdoc::CryptoBackend::~CryptoBackend ( )
virtualdefaultnoexcept

◆ CryptoBackend() [2/2]

libcdoc::CryptoBackend::CryptoBackend ( const CryptoBackend )
delete

Member Function Documentation

◆ CDOC_DISABLE_MOVE()

libcdoc::CryptoBackend::CDOC_DISABLE_MOVE ( CryptoBackend  )

◆ decryptRSA()

virtual result_t libcdoc::CryptoBackend::decryptRSA ( std::vector< uint8_t > &  dst,
const std::vector< uint8_t > &  data,
bool  oaep,
unsigned int  idx 
)
inlinevirtual

decryptRSA

Parameters
dstthe destination container for decrypted data
dataencrypted data
oaepuse OAEP padding
idxlock index (0-based) in container
Returns
error code or OK

Reimplemented in libcdoc::WinBackend, and libcdoc::PKCS11Backend.

References libcdoc::NOT_IMPLEMENTED.

◆ deriveConcatKDF()

virtual result_t libcdoc::CryptoBackend::deriveConcatKDF ( std::vector< uint8_t > &  dst,
const std::vector< uint8_t > &  public_key,
const std::string &  digest,
const std::vector< uint8_t > &  algorithm_id,
const std::vector< uint8_t > &  party_uinfo,
const std::vector< uint8_t > &  party_vinfo,
unsigned int  idx 
)
virtual

Derive key by ConcatKDF algorithm.

The ConcatKDF key derivation algorithm is defined in Section 5.8.1 of NIST SP 800-56A. The default implementation calls deriveECDH1 and performs local concatKDF

Parameters
dstthe container for derived key
public_keyECDH public Key used to derive shared secret
digestDigest method to use for ConcatKDF algorithm
algorithm_idOtherInfo info parameters to input
party_uinfoOtherInfo info parameters to input
party_vinfoOtherInfo info parameters to input
idxlock index (0-based) in container
Returns
error code or OK

Reimplemented in libcdoc::WinBackend.

◆ deriveECDH1()

virtual result_t libcdoc::CryptoBackend::deriveECDH1 ( std::vector< uint8_t > &  dst,
const std::vector< uint8_t > &  public_key,
unsigned int  idx 
)
inlinevirtual

Derive shared secret.

Derive a shared secret from private key of given lock and public key using ECDH1 algorithm.

Parameters
dstthe container for shared secret
public_keyECDH public key used to derive shared secret
idxlock index (0-based) in container
Returns
error code or OK

Reimplemented in libcdoc::PKCS11Backend.

References libcdoc::NOT_IMPLEMENTED.

◆ deriveHMACExtract()

virtual result_t libcdoc::CryptoBackend::deriveHMACExtract ( std::vector< uint8_t > &  dst,
const std::vector< uint8_t > &  public_key,
const std::vector< uint8_t > &  salt,
unsigned int  idx 
)
virtual

Get CDoc2 KEK pre-master from ECC key.

Calculates KEK (Key Encryption Key) pre-master from an ECC public key. The default implementation calls deriveECDH1 and performs local HMAC extract

Parameters
dstthe container for derived key
public_keyECDH public Key used to derive shared secret
saltsalt for key derivation
idxlock index (0-based) in container
Returns
error code or OK

Reimplemented in libcdoc::WinBackend.

◆ extractHKDF()

virtual result_t libcdoc::CryptoBackend::extractHKDF ( std::vector< uint8_t > &  dst,
const std::vector< uint8_t > &  salt,
const std::vector< uint8_t > &  pw_salt,
int32_t  kdf_iter,
unsigned int  idx 
)
virtual

Get CDoc2 KEK pre-master from symmetric key.

Calculates KEK (Key Encryption Key) pre-master from a symmetric key (either password or key-based). The default implementation calls getKeyMaterial and performs local HKDF extract.

Parameters
dstthe destination container for KEK pre-master
saltthe salt value for HKDF extract
pw_saltthe salt value for PBKDF
kdf_iterthe number of KDF iterations. If kdf_iter is 0, the key is plain symmetric key instead of password.
idxlock or recipient index (0-based) in container
Returns
error code or OK

Reimplemented in libcdoc::PKCS11Backend.

◆ getKeyMaterial()

virtual result_t libcdoc::CryptoBackend::getKeyMaterial ( std::vector< uint8_t > &  dst,
const std::vector< uint8_t > &  pw_salt,
int32_t  kdf_iter,
unsigned int  idx 
)
virtual

Get CDoc2 key material for HKDF expansion.

Fetches key material for a symmetric key (either password or key-based). The default implementation calls getSecret and performs PBKDF2_SHA256 if key is password-based.

Parameters
dstthe destination container for key material
pw_saltthe salt value for PBKDF
kdf_iterkdf_iter the number of KDF iterations. If kdf_iter is 0, the key is plain symmetric key instead of password.
idxlock or recipient index (0-based) in container
Returns
error code or OK

◆ getLastErrorStr()

virtual std::string libcdoc::CryptoBackend::getLastErrorStr ( result_t  code) const
virtual

◆ getSecret()

virtual result_t libcdoc::CryptoBackend::getSecret ( std::vector< uint8_t > &  dst,
unsigned int  idx 
)
inlinevirtual

Get secret value (either password or symmetric key) for a lock.

Parameters
dstthe destination container for secret
idxlock or recipient index (0-based) in container
Returns
error code or OK

References libcdoc::NOT_IMPLEMENTED.

◆ operator=()

CryptoBackend & libcdoc::CryptoBackend::operator= ( const CryptoBackend )
delete

◆ random()

virtual result_t libcdoc::CryptoBackend::random ( std::vector< uint8_t > &  dst,
unsigned int  size 
)
virtual

Fill vector with random bytes.

Trim vector to requested size and fill it with random bytes. The default implementation uses OpenSSL randomness generator.

Parameters
dstthe destination container for randomness
sizethe requested amount of random data
Returns
error code or OK

◆ sign()

virtual result_t libcdoc::CryptoBackend::sign ( std::vector< uint8_t > &  dst,
HashAlgorithm  algorithm,
const std::vector< uint8_t > &  digest,
unsigned int  idx 
)
inlinevirtual

sign Sign message with given algorithm

Parameters
dstthe destination container for signed message
algorithmhashing algorithm
digesta message to sign
idxlock or recipient index (0-based) in container
Returns
error code or OK

Reimplemented in libcdoc::WinBackend, and libcdoc::PKCS11Backend.

References libcdoc::NOT_IMPLEMENTED.

◆ test()

virtual int libcdoc::CryptoBackend::test ( libcdoc::Lock lock)
inlinevirtual

Member Data Documentation

◆ ECC_KEY_LEN

constexpr int libcdoc::CryptoBackend::ECC_KEY_LEN = 32
staticconstexpr

◆ INVALID_PARAMS

constexpr int libcdoc::CryptoBackend::INVALID_PARAMS = -201
staticconstexpr

◆ OPENSSL_ERROR

constexpr int libcdoc::CryptoBackend::OPENSSL_ERROR = -202
staticconstexpr

The documentation for this struct was generated from the following file: