/*********************************************************************************************************************************** Cipher Spec ***********************************************************************************************************************************/ #include #include "common/crypto/spec.h" #include "common/debug.h" #include "common/log.h" /*********************************************************************************************************************************** Object type ***********************************************************************************************************************************/ struct CipherSpec { CipherSpecPub pub; // Publicly accessible variables }; /**********************************************************************************************************************************/ FN_EXTERN CipherSpec * cipherSpecNew(const CipherType type, const Buffer *const pass, const CipherSpecNewParam param) { FUNCTION_TEST_BEGIN(); FUNCTION_TEST_PARAM(STRING_ID, type); FUNCTION_TEST_PARAM(BUFFER, pass); FUNCTION_TEST_PARAM(STRING_ID, param.digest); FUNCTION_TEST_PARAM(CIPHER_SPEC, param.prior); FUNCTION_TEST_END(); ASSERT((type == cipherTypeNone) == (pass == NULL)); ASSERT(pass == NULL || !bufEmpty(pass)); ASSERT(param.prior == NULL || (type != cipherTypeNone && cipherSpecType(param.prior) == type)); OBJ_NEW_BEGIN(CipherSpec, .childQty = MEM_CONTEXT_QTY_MAX) { *this = (CipherSpec){.pub = {.type = type}}; if (this->pub.type != cipherTypeNone) { this->pub.digest = param.digest; this->pub.pass = bufDup(pass); if (param.prior != NULL) this->pub.prior = cipherSpecDupP(param.prior); } } OBJ_NEW_END(); FUNCTION_TEST_RETURN(CIPHER_SPEC, this); } /**********************************************************************************************************************************/ FN_EXTERN CipherSpec * cipherSpecNewPack(PackRead *const packRead) { FUNCTION_TEST_BEGIN(); FUNCTION_TEST_PARAM(PACK_READ, packRead); FUNCTION_TEST_END(); ASSERT(packRead != NULL); OBJ_NEW_BEGIN(CipherSpec, .childQty = MEM_CONTEXT_QTY_MAX) { *this = (CipherSpec){.pub = {.type = (CipherType)pckReadStrIdP(packRead)}}; // Nothing else was written when there is no cipher. The pass is read here rather than copied in since this context is the // one it belongs in. if (this->pub.type != cipherTypeNone) { this->pub.digest = (HashType)pckReadStrIdP(packRead); this->pub.pass = pckReadBinP(packRead); } } OBJ_NEW_END(); FUNCTION_TEST_RETURN(CIPHER_SPEC, this); } /**********************************************************************************************************************************/ FN_EXTERN CipherSpec * cipherSpecDup(const CipherSpec *const this, const CipherSpecDupParam param) { FUNCTION_TEST_BEGIN(); FUNCTION_TEST_PARAM(CIPHER_SPEC, this); FUNCTION_TEST_PARAM(STRING_ID, param.digestDefault); FUNCTION_TEST_END(); ASSERT(this != NULL); FUNCTION_TEST_RETURN( CIPHER_SPEC, cipherSpecNewP( cipherSpecType(this), cipherSpecPass(this), .digest = cipherSpecDigest(this) == 0 ? param.digestDefault : cipherSpecDigest(this), .prior = cipherSpecPrior(this))); } /**********************************************************************************************************************************/ FN_EXTERN void cipherSpecPack(PackWrite *const packWrite, const CipherSpec *const this) { FUNCTION_TEST_BEGIN(); FUNCTION_TEST_PARAM(PACK_WRITE, packWrite); FUNCTION_TEST_PARAM(CIPHER_SPEC, this); FUNCTION_TEST_END(); ASSERT(packWrite != NULL); ASSERT(this != NULL); pckWriteStrIdP(packWrite, cipherSpecType(this)); // Nothing else is needed when there is no cipher if (cipherSpecType(this) != cipherTypeNone) { pckWriteStrIdP(packWrite, cipherSpecDigest(this)); pckWriteBinP(packWrite, cipherSpecPass(this)); } FUNCTION_TEST_RETURN_VOID(); } /**********************************************************************************************************************************/ FN_EXTERN void cipherSpecToLog(const CipherSpec *const this, StringStatic *const debugLog) { char typeZ[STRID_MAX + 1]; strIdToZ(cipherSpecType(this), typeZ); strStcFmt(debugLog, "{type: %s", typeZ); // There is no digest when there is no cipher or when the format decides it. The pass is never logged. if (cipherSpecDigest(this) != 0) { char digestZ[STRID_MAX + 1]; strIdToZ(cipherSpecDigest(this), digestZ); strStcFmt(debugLog, ", digest: %s", digestZ); } strStcCatChr(debugLog, '}'); }