pg.ddx.io  pgbackrest/pgbackrest GitHub issues and pull requests  
help / color / mirror / Atom feed
From: dwsteele (@dwsteele) <noreply+dwsteele@github.com>
To: pgbackrest/pgbackrest <noreply+pgbackrest-pgbackrest@github.com>
Subject: [pgbackrest/pgbackrest] PR #2883: RFC: Derive the info file key with PBKDF2 in repository format 6.
Date: Sat, 17 Oct 2026 17:43:12 +0000
Message-ID: <gh-pgbackrest-pgbackrest-2883@github.com> (raw)

NOT READY FOR REVIEW -- REQUESTING COMMENTS ONLY.

I'm really conflicted about this PR.

The point of additional rounds is to make up for a weak passphrase selected by the user. PR #2880 already requires a 256-bit key for repository format 6. We could add the weak passphrase detection from this commit (see src/command/stanza/common.c) to #2880 and skip this PR entirely or at least default it to 1 if we really feel like we need it. This patch should prevent good passphrases from being penalized with 600k rounds of hashing but it also adds quite a bit of complexity

---

At repository format 5 the key that encrypts archive.info and backup.info is derived from repo-cipher-pass with a single round of EVP_BytesToKey, so anyone with a copy of either file can test guesses at the passphrase cheaply. At format 6 these files now derive the key with PBKDF2 using the digest of the format.

The rounds are stored in the format header after a new P marker as four bytes in big-endian order. A reader takes the rounds from the header and rejects zero rounds or more than 600000. The key and IV are split from the PBKDF2 output the way openssl enc -pbkdf2 does, so a file can still be decrypted with the openssl command-line tool. Subkeys are random and continue to derive with one round of EVP_BytesToKey.

The rounds belong to the stanza. stanza-create sets them when it creates an encrypted stanza at format 6, and stanza-upgrade sets them when it upgrades a stanza to format 6 or when the internal repo-cipher-round option is set. Other commands keep the rounds stored in the info file they rewrite, so archive-push, archive-get, and the rest need no setting. stanza-create and stanza-upgrade warn when repo-cipher-round is set for a stanza below format 6.

When repo-cipher-round is not set, a passphrase that looks generated uses one round and any other passphrase uses 600000. More rounds only make guessing a weak passphrase more expensive, while every command that loads an info file pays for them, including archive-get once per WAL segment during recovery. A passphrase looks generated when it is base64 that decodes to at least 256 bits, the decoded bytes are not all text, and there are at least 16 distinct byte values and 16 distinct differences between adjacent bytes. A weak secret that has been hashed or encoded some other way can still pass.

NOTE: The dependency on repo format 6 is manually coded but the config system should be updated to allow for this if we go forward with this PR.

inline patch 1: build/config.yaml, doc/xml/reference.xml, src/command/stanza/common.c and 25 more (+640/-70 lines) download inline-1.patchdiff --git a/build/config.yaml b/build/config.yaml index e20b77be03..805bf6056f 100644 --- a/build/config.yaml +++ b/build/config.yaml @@ -1761,6 +1761,19 @@ option: list: - aes-256-cbc + repo-cipher-round: + section: global + group: repo + type: integer + internal: true + default: 600000 + allow-range: [1, 600000] + command: + stanza-create: {} + stanza-upgrade: {} + command-role: + main: {} + repo-hardlink: section: global group: repo diff --git a/doc/xml/reference.xml b/doc/xml/reference.xml index 5659cb7b3c..a9427d4f16 100644 --- a/doc/xml/reference.xml +++ b/doc/xml/reference.xml @@ -1473,6 +1473,20 @@ <example>30d</example> </config-key> + + <config-key id="repo-cipher-round" name="Repository Cipher Rounds"> + <summary>Passphrase key derivation rounds.</summary> + + <text> + <p>Rounds of PBKDF2 used to derive the key that encrypts <file>archive.info</file> and <file>backup.info</file> from <br-option>repo-cipher-pass</br-option>. More rounds make each guess at the passphrase more expensive for an attacker, but every command that reads or writes these files pays for the rounds as well. For example, <cmd>archive-push</cmd> and <cmd>archive-get</cmd> read <file>archive.info</file> each time they run.</p> + + <p>Rounds only help when the passphrase can be guessed. A passphrase generated from at least 256 random bits, e.g. with <code>openssl rand -base64 32</code>, cannot be guessed no matter how few rounds are used. When this option is not set, a passphrase that looks generated uses one round and any other passphrase uses the default. A passphrase looks generated when it is base64 that decodes to at least 256 bits, the decoded bytes are not all text, and they contain at least 16 distinct values and at least 16 distinct differences between adjacent bytes. The last two rule out repeated values, sequences, and short repeating patterns. Do not reduce the rounds for a passphrase that a person chose, even if it has been encoded to look generated.</p> + + <p>The rounds are set by <cmd>stanza-create</cmd> when it creates a stanza at <br-option>repo-format</br-option> 6 and by <cmd>stanza-upgrade</cmd> when it upgrades a stanza to <br-option>repo-format</br-option> 6. They are stored in the info files and every other command uses the rounds stored there. To change the rounds of a stanza already at <br-option>repo-format</br-option> 6, run <cmd>stanza-upgrade</cmd> with this option.</p> + </text> + + <example>1</example> + </config-key> </config-key-list> </config-section> diff --git a/src/command/stanza/common.c b/src/command/stanza/common.c index 30b2939f30..87483c1cea 100644 --- a/src/command/stanza/common.c +++ b/src/command/stanza/common.c @@ -6,6 +6,7 @@ Stanza Commands Handler #include "command/check/common.h" #include "command/stanza/common.h" #include "common/debug.h" +#include "common/encode.h" #include "common/format/format.h" #include "common/log.h" #include "config/config.h" @@ -49,6 +50,128 @@ cipherSpecGen(const CipherType cipherType, const unsigned int format) FUNCTION_TEST_RETURN(CIPHER_SPEC, result); } +/**********************************************************************************************************************************/ +FN_EXTERN void +cipherRoundCheck(const unsigned int repoIdx, const unsigned int format) +{ + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM(UINT, repoIdx); + FUNCTION_TEST_PARAM(UINT, format); + FUNCTION_TEST_END(); + + if (format < REPOSITORY_FORMAT_6 && cfgOptionIdxSource(cfgOptRepoCipherRound, repoIdx) != cfgSourceDefault) + { + LOG_WARN_FMT( + "option '%s' requires repository format %d or higher", cfgOptionIdxName(cfgOptRepoCipherRound, repoIdx), + REPOSITORY_FORMAT_6); + } + + FUNCTION_TEST_RETURN_VOID(); +} + +/*********************************************************************************************************************************** +Does the passphrase look generated? It must be base64 that decodes to at least 256 bits, the decoded bytes must not all be text, and +there must be enough distinct byte values and enough distinct differences between adjacent bytes. Too few distinct differences +means repeated values, a sequence, or a short repeating pattern. 32 random bytes are all text with a probability of about 10^-14 +and have fewer than 16 distinct values or differences with a probability of about 10^-15. The passphrase could still be a weak +secret that was hashed or encoded some other way, which we can't detect. +***********************************************************************************************************************************/ +// Bytes the key must decode to, i.e. 256 bits +#define CIPHER_PASS_GENERATED_SIZE_MIN 32 + +// Distinct byte values and distinct differences between adjacent bytes the key must contain +#define CIPHER_PASS_GENERATED_DISTINCT_MIN 16 + +static bool +cipherPassGenerated(const CipherSpec *const cipherSpec) +{ + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM(CIPHER_SPEC, cipherSpec); + FUNCTION_TEST_END(); + + ASSERT(cipherSpec != NULL); + ASSERT(cipherSpecType(cipherSpec) != cipherTypeNone); + + bool result = false; + + MEM_CONTEXT_TEMP_BEGIN() + { + TRY_BEGIN() + { + const Buffer *const key = bufNewDecode(encodingBase64, strNewBuf(cipherSpecPass(cipherSpec))); + + if (bufUsed(key) >= CIPHER_PASS_GENERATED_SIZE_MIN) + { + bool text = true; + bool byteFound[256] = {false}; + unsigned int byteDistinct = 0; + bool deltaFound[256] = {false}; + unsigned int deltaDistinct = 0; + + for (size_t keyIdx = 0; keyIdx < bufUsed(key); keyIdx++) + { + const uint8_t byte = bufPtrConst(key)[keyIdx]; + + // A byte that is neither printable ASCII nor a tab, LF, or CR means the key is not text + if ((byte < ' ' || byte > '~') && byte != '\t' && byte != '\n' && byte != '\r') + text = false; + + // Count each byte value the first time it is seen + if (!byteFound[byte]) + { + byteFound[byte] = true; + byteDistinct++; + } + + // Count each difference from the prior byte the first time it is seen + if (keyIdx > 0) + { + const uint8_t delta = (uint8_t)(byte - bufPtrConst(key)[keyIdx - 1]); + + if (!deltaFound[delta]) + { + deltaFound[delta] = true; + deltaDistinct++; + } + } + } + + result = + !text && byteDistinct >= CIPHER_PASS_GENERATED_DISTINCT_MIN && + deltaDistinct >= CIPHER_PASS_GENERATED_DISTINCT_MIN; + } + } + // A passphrase that is not base64 does not look generated + CATCH(FormatError) + { + } + TRY_END(); + } + MEM_CONTEXT_TEMP_END(); + + FUNCTION_TEST_RETURN(BOOL, result); +} + +/**********************************************************************************************************************************/ +FN_EXTERN unsigned int +cipherRoundGet(const unsigned int repoIdx) +{ + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM(UINT, repoIdx); + FUNCTION_TEST_END(); + + unsigned int result = cfgOptionIdxUInt(cfgOptRepoCipherRound, repoIdx); + + // One round when the rounds are default and the passphrase looks generated + if (cfgOptionIdxSource(cfgOptRepoCipherRound, repoIdx) == cfgSourceDefault && + cipherPassGenerated(cfgCipherSpecMainIdx(repoIdx))) + { + result = 1; + } + + FUNCTION_TEST_RETURN(UINT, result); +} + /**********************************************************************************************************************************/ FN_EXTERN PgControl pgValidate(void) diff --git a/src/command/stanza/common.h b/src/command/stanza/common.h index 14886f3273..e8cb46737e 100644 --- a/src/command/stanza/common.h +++ b/src/command/stanza/common.h @@ -13,6 +13,13 @@ Functions // Generate a sub pass for a file at this format, which sets the digest since that is what will derive it when it is read back FN_EXTERN CipherSpec *cipherSpecGen(CipherType cipherType, unsigned int format); +// Warn when PBKDF2 rounds are set for a stanza at a format that does not use them +FN_EXTERN void cipherRoundCheck(unsigned int repoIdx, unsigned int format); + +// PBKDF2 rounds for the info files of an encrypted repo at format >= 6. The rounds requested, else one round for a passphrase that +// looks generated, else the default. +FN_EXTERN unsigned int cipherRoundGet(unsigned int repoIdx); + // Validate and return database information FN_EXTERN PgControl pgValidate(void); diff --git a/src/command/stanza/create.c b/src/command/stanza/create.c index 2883a7cdb0..b889c7236a 100644 --- a/src/command/stanza/create.c +++ b/src/command/stanza/create.c @@ -78,6 +78,7 @@ cmdStanzaCreate(void) // Format for the new stanza const unsigned int format = cfgOptionIdxUInt(cfgOptRepoFormat, repoIdx); + cipherRoundCheck(repoIdx, format); // If the repo is encrypted, generate a cipher passphrase for encrypting archive files. Format >= 6 adds it by // rotating to the first key id and earlier formats store it as the default key with no id. @@ -90,8 +91,12 @@ cmdStanzaCreate(void) // Create and save archive info infoArchive = infoArchiveNew(pgControl.version, pgControl.systemId, format, cipherSpecMapArchive); + // Format >= 6 also encrypts the info files with PBKDF2 rounds if (cipherSpecType(cipherSpecArchive) != cipherTypeNone && format >= REPOSITORY_FORMAT_6) + { infoArchiveCipherRotate(infoArchive, cipherSpecArchive, time(NULL)); + infoArchiveCipherRoundSet(infoArchive, cipherRoundGet(repoIdx)); + } infoArchiveSaveFile(infoArchive, storageRepoWriteStanza, INFO_ARCHIVE_PATH_FILE_STR, cfgCipherSpecMainIdx(repoIdx)); @@ -102,6 +107,7 @@ cmdStanzaCreate(void) // Create and save backup info infoBackup = infoBackupNew( pgControl.version, pgControl.systemId, pgControl.catalogVersion, format, cipherSpecManifest); + infoBackupCipherRoundSet(infoBackup, infoArchiveCipherRound(infoArchive)); infoBackupSaveFile(infoBackup, storageRepoWriteStanza, INFO_BACKUP_PATH_FILE_STR, cfgCipherSpecMainIdx(repoIdx)); } diff --git a/src/command/stanza/upgrade.c b/src/command/stanza/upgrade.c index 90631bf876..782175ec81 100644 --- a/src/command/stanza/upgrade.c +++ b/src/command/stanza/upgrade.c @@ -87,6 +87,8 @@ cmdStanzaUpgrade(void) format = formatRequest; } + cipherRoundCheck(repoIdx, format); + // Since the file save of archive.info and backup.info are not atomic, then check and update each separately. // Update archive if (pgControl.version != archiveInfo.version || pgControl.systemId != archiveInfo.systemId) @@ -103,7 +105,9 @@ cmdStanzaUpgrade(void) } // Update the format on both info files together so they never disagree - if (format != formatArchive || format != formatBackup) + const bool formatUpdate = format != formatArchive || format != formatBackup; + + if (formatUpdate) { // Report a repository that was found at two formats. It is repaired here but a prior upgrade did not finish, which // the user has not been told about since the run it happened on did not get far enough to report it. @@ -132,6 +136,26 @@ cmdStanzaUpgrade(void) infoBackupUpgrade = true; } + // Set PBKDF2 rounds on the info files of an encrypted repo at format >= 6 when the format is updated or rounds are + // requested + if (format >= REPOSITORY_FORMAT_6 && cfgOptionIdxStrId(cfgOptRepoCipherType, repoIdx) != cipherTypeNone && + (formatUpdate || cfgOptionIdxSource(cfgOptRepoCipherRound, repoIdx) != cfgSourceDefault)) + { + const unsigned int cipherRound = cipherRoundGet(repoIdx); + + if (infoArchiveCipherRound(infoArchive) != cipherRound) + { + infoArchiveCipherRoundSet(infoArchive, cipherRound); + infoArchiveUpgrade = true; + } + + if (infoBackupCipherRound(infoBackup) != cipherRound) + { + infoBackupCipherRoundSet(infoBackup, cipherRound); + infoBackupUpgrade = true; + } + } + // Throw an error if the info files do not match before saving (even if only one needed to be updated) checkStanzaInfo(repoIdx, infoArchivePg(infoArchive), infoBackupPg(infoBackup)); diff --git a/src/common/crypto/cipherBlock.c b/src/common/crypto/cipherBlock.c index 9a465ca876..3b79ec31af 100644 --- a/src/common/crypto/cipherBlock.c +++ b/src/common/crypto/cipherBlock.c @@ -27,6 +27,7 @@ typedef struct CipherBlock bool saltDone; // Has the salt been read/generated? bool processDone; // Has any data been processed? const Buffer *pass; // Passphrase used to generate encryption key + unsigned int round; // PBKDF2 rounds, zero for one round of EVP_BytesToKey size_t headerSize; // Size of header read during decrypt uint8_t header[CIPHER_BLOCK_MAGIC_SIZE + PKCS5_SALT_LEN]; // Buffer to hold partial header during decrypt const EVP_CIPHER *cipher; // Cipher object @@ -175,10 +176,30 @@ cipherBlockProcessBlock(CipherBlock *const this, const uint8_t *source, size_t s uint8_t key[EVP_MAX_KEY_LENGTH]; uint8_t initVector[EVP_MAX_IV_LENGTH]; - cryptoError( - EVP_BytesToKey( - this->cipher, this->digest, salt, bufPtrConst(this->pass), (int)bufSize(this->pass), 1, key, initVector) == 0, - "unable to generate key and initialization vector"); + if (this->round == 0) + { + cryptoError( + EVP_BytesToKey( + this->cipher, this->digest, salt, bufPtrConst(this->pass), (int)bufSize(this->pass), 1, key, + initVector) == 0, + "unable to generate key and initialization vector"); + } + // Else derive with PBKDF2, splitting the output into the key and initialization vector the way openssl enc -pbkdf2 does + else + { + const size_t keySize = (size_t)EVP_CIPHER_key_length(this->cipher); + const size_t initVectorSize = (size_t)EVP_CIPHER_iv_length(this->cipher); + uint8_t keyInitVector[EVP_MAX_KEY_LENGTH + EVP_MAX_IV_LENGTH]; + + cryptoError( + PKCS5_PBKDF2_HMAC( + (const char *)bufPtrConst(this->pass), (int)bufSize(this->pass), salt, PKCS5_SALT_LEN, (int)this->round, + this->digest, (int)(keySize + initVectorSize), keyInitVector) == 0, + "unable to generate key and initialization vector"); + + memcpy(key, keyInitVector, keySize); + memcpy(initVector, keyInitVector + keySize, initVectorSize); + } // Create context to track cipher cryptoError(!(this->cipherContext = EVP_CIPHER_CTX_new()), "unable to create context"); @@ -391,6 +412,9 @@ cipherBlockNew(const CipherMode mode, const CipherSpec *const cipherSpec, const ASSERT(cipherSpecDigest(cipherSpec) != 0); ASSERT(cipherSpecPass(cipherSpec) != NULL && !bufEmpty(cipherSpecPass(cipherSpec))); + // A file with the magic derives with one round of EVP_BytesToKey + ASSERT(cipherSpecRound(cipherSpec) == 0 || param.header == cipherBlockHeaderNone); + // Init crypto subsystem cryptoInit(); @@ -416,6 +440,7 @@ cipherBlockNew(const CipherMode mode, const CipherSpec *const cipherSpec, const .cipher = cipher, .digest = digest, .pass = bufDup(cipherSpecPass(cipherSpec)), + .round = cipherSpecRound(cipherSpec), }; } OBJ_NEW_END(); diff --git a/src/common/crypto/spec.c b/src/common/crypto/spec.c index 8013cd689c..304cf4d325 100644 --- a/src/common/crypto/spec.c +++ b/src/common/crypto/spec.c @@ -23,6 +23,7 @@ cipherSpecNew(const CipherType type, const Buffer *const pass, const CipherSpecN FUNCTION_TEST_PARAM(STRING_ID, type); FUNCTION_TEST_PARAM(BUFFER, pass); FUNCTION_TEST_PARAM(STRING_ID, param.digest); + FUNCTION_TEST_PARAM(UINT, param.round); FUNCTION_TEST_END(); ASSERT((type == cipherTypeNone) == (pass == NULL)); @@ -35,6 +36,7 @@ cipherSpecNew(const CipherType type, const Buffer *const pass, const CipherSpecN if (this->pub.type != cipherTypeNone) { this->pub.digest = param.digest; + this->pub.round = param.round; this->pub.pass = bufDup(pass); } } @@ -62,6 +64,7 @@ cipherSpecNewPack(PackRead *const packRead) if (this->pub.type != cipherTypeNone) { this->pub.digest = (HashType)pckReadStrIdP(packRead); + this->pub.round = pckReadU32P(packRead); this->pub.pass = pckReadBinP(packRead); } } @@ -85,7 +88,7 @@ cipherSpecDup(const CipherSpec *const this, const CipherSpecDupParam param) CIPHER_SPEC, cipherSpecNewP( cipherSpecType(this), cipherSpecPass(this), - .digest = cipherSpecDigest(this) == 0 ? param.digestDefault : cipherSpecDigest(this))); + .digest = cipherSpecDigest(this) == 0 ? param.digestDefault : cipherSpecDigest(this), .round = cipherSpecRound(this))); } /**********************************************************************************************************************************/ @@ -106,6 +109,7 @@ cipherSpecPack(PackWrite *const packWrite, const CipherSpec *const this) if (cipherSpecType(this) != cipherTypeNone) { pckWriteStrIdP(packWrite, cipherSpecDigest(this)); + pckWriteU32P(packWrite, cipherSpecRound(this)); pckWriteBinP(packWrite, cipherSpecPass(this)); } @@ -130,5 +134,8 @@ cipherSpecToLog(const CipherSpec *const this, StringStatic *const debugLog) strStcFmt(debugLog, ", digest: %s", digestZ); } + if (cipherSpecRound(this) != 0) + strStcFmt(debugLog, ", round: %u", cipherSpecRound(this)); + strStcCatChr(debugLog, '}'); } diff --git a/src/common/crypto/spec.h b/src/common/crypto/spec.h index 47bc041425..b3051338a1 100644 --- a/src/common/crypto/spec.h +++ b/src/common/crypto/spec.h @@ -12,6 +12,9 @@ copied into the object, so the caller can release whatever it read the pass from An unset digest means the repository format of the file being read or written specifies it. +A pass derives the key with one round of EVP_BytesToKey unless rounds are set, in which case it derives with PBKDF2 using that many +rounds. Rounds are for a passphrase that may be guessable. A generated key is random enough that more rounds add nothing. + There is no digest or pass when the type is none, and the pass is never logged. ***********************************************************************************************************************************/ #ifndef COMMON_CRYPTO_SPEC_H @@ -35,6 +38,7 @@ typedef struct CipherSpecNewParam { VAR_PARAM_HEADER; HashType digest; // Digest to derive the key with, unset when specified by format + unsigned int round; // PBKDF2 rounds, zero for one round of EVP_BytesToKey } CipherSpecNewParam; #define cipherSpecNewP(type, pass, ...) \ @@ -59,6 +63,7 @@ typedef struct CipherSpecPub { CipherType type; // Cipher type, none when not encrypted HashType digest; // Digest the pass derives with, unset when specified by format + unsigned int round; // PBKDF2 rounds, zero for one round of EVP_BytesToKey const Buffer *pass; // Passphrase text or key bytes } CipherSpecPub; @@ -76,6 +81,13 @@ cipherSpecDigest(const CipherSpec *const this) return THIS_PUB(CipherSpec)->digest; } +// PBKDF2 rounds, zero for one round of EVP_BytesToKey +FN_INLINE_ALWAYS unsigned int +cipherSpecRound(const CipherSpec *const this) +{ + return THIS_PUB(CipherSpec)->round; +} + // Passphrase text or key bytes FN_INLINE_ALWAYS const Buffer * cipherSpecPass(const CipherSpec *const this) diff --git a/src/common/format/cipherBlockFormat.c b/src/common/format/cipherBlockFormat.c index 00ffbfb612..6075d36f75 100644 --- a/src/common/format/cipherBlockFormat.c +++ b/src/common/format/cipherBlockFormat.c @@ -31,21 +31,30 @@ that tool expects. A file that begins with the magic rather than the header was written at format 5, the only format there was before the header, so that is not an error when a header was expected. -The first three bytes of the header magic are the format and the last is a marker indicating whether a key id follows. The format -comes first so it is always in the same place, which is how a version decides whether it can read the file. The marker is only -checked once the format turns out to be readable, and it must be valid for this version. +The first three bytes of the header magic are the format and the last is a marker indicating whether a key id or PBKDF2 rounds +follow. The format comes first so it is always in the same place, which is how a version decides whether it can read the file. The +marker is only checked once the format turns out to be readable, and it must be valid for this version. If a key id is indicated by the marker, there is first a byte to indicate the length of the key id and then the key id as a string. + +If PBKDF2 rounds are indicated by the marker, the rounds follow as four bytes in big-endian (network) byte order. The key and +initialization vector are derived the way openssl enc -pbkdf2 derives them, so the openssl command-line tool can still open the file +once the header and rounds are replaced with the magic. ***********************************************************************************************************************************/ #define CIPHER_BLOCK_FORMAT_MAGIC "PGBR" #define CIPHER_BLOCK_FORMAT_MAGIC_SIZE (sizeof(CIPHER_BLOCK_FORMAT_MAGIC) - 1) #define CIPHER_BLOCK_FORMAT_MARKER_NONE '_' #define CIPHER_BLOCK_FORMAT_MARKER_KEY 'K' +#define CIPHER_BLOCK_FORMAT_MARKER_ROUND 'P' #define CIPHER_BLOCK_FORMAT_SIZE 3 // The key id length is encoded as a single byte with a max of 255 characters #define CIPHER_BLOCK_FORMAT_KEY_SIZE_MAX 255 +// PBKDF2 rounds are encoded as four bytes. The max is the max of the repo-cipher-round option. +#define CIPHER_BLOCK_FORMAT_ROUND_SIZE 4 +#define CIPHER_BLOCK_FORMAT_ROUND_MAX 600000 + // Total header length: magic, format, and marker #define CIPHER_BLOCK_FORMAT_HEADER_SIZE (CIPHER_BLOCK_FORMAT_MAGIC_SIZE + CIPHER_BLOCK_FORMAT_SIZE + 1) @@ -70,6 +79,7 @@ typedef struct CipherBlockFormat const CipherSpecMap *cipherSpecMap; // Keys to choose from unsigned int formatExpected; // Format the caller expects, zero when any will do unsigned int format; // Format the header gave + unsigned int round; // PBKDF2 rounds the header gave, zero when none uint8_t header[CIPHER_BLOCK_FORMAT_HEADER_SIZE_MAX]; // Header bytes held until there are enough to read size_t headerSize; // Header bytes held so far @@ -123,7 +133,8 @@ cipherBlockFormatHeaderRead(const uint8_t *const header) // Check that the marker is valid for this version if (headerZ[CIPHER_BLOCK_FORMAT_HEADER_SIZE - 1] != CIPHER_BLOCK_FORMAT_MARKER_NONE && - headerZ[CIPHER_BLOCK_FORMAT_HEADER_SIZE - 1] != CIPHER_BLOCK_FORMAT_MARKER_KEY) + headerZ[CIPHER_BLOCK_FORMAT_HEADER_SIZE - 1] != CIPHER_BLOCK_FORMAT_MARKER_KEY && + headerZ[CIPHER_BLOCK_FORMAT_HEADER_SIZE - 1] != CIPHER_BLOCK_FORMAT_MARKER_ROUND) { THROW(FormatError, "invalid cipher header"); } @@ -135,9 +146,31 @@ cipherBlockFormatHeaderRead(const uint8_t *const header) FUNCTION_TEST_RETURN(UINT, result); } +/*********************************************************************************************************************************** +Cipher spec with the digest the format specifies when the spec has none and with the rounds the header records +***********************************************************************************************************************************/ +static CipherSpec * +cipherBlockFormatCipherSpec(const CipherSpec *const cipherSpec, const unsigned int format, const unsigned int round) +{ + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM(CIPHER_SPEC, cipherSpec); + FUNCTION_TEST_PARAM(UINT, format); + FUNCTION_TEST_PARAM(UINT, round); + FUNCTION_TEST_END(); + + ASSERT(cipherSpec != NULL); + + FUNCTION_TEST_RETURN( + CIPHER_SPEC, + cipherSpecNewP( + cipherSpecType(cipherSpec), cipherSpecPass(cipherSpec), + .digest = cipherSpecDigest(cipherSpec) == 0 ? repoFormatDigest(format) : cipherSpecDigest(cipherSpec), + .round = round)); +} + /*********************************************************************************************************************************** Build the block cipher that decrypts the content, which is only possible once the header has been read since the header defines the -format and the key id +format, the key id, and the rounds ***********************************************************************************************************************************/ static void cipherBlockFormatCipherNew(CipherBlockFormat *const this) @@ -160,7 +193,7 @@ cipherBlockFormatCipherNew(CipherBlockFormat *const this) { const String *keyId = CIPHER_SPEC_MAP_ID_DEFAULT_STR; - if (this->headerSize > CIPHER_BLOCK_FORMAT_HEADER_SIZE) + if (this->header[CIPHER_BLOCK_FORMAT_HEADER_SIZE - 1] == CIPHER_BLOCK_FORMAT_MARKER_KEY) { keyId = strNewZN( (const char *)this->header + CIPHER_BLOCK_FORMAT_HEADER_SIZE + 1, @@ -175,7 +208,7 @@ cipherBlockFormatCipherNew(CipherBlockFormat *const this) MEM_CONTEXT_OBJ_BEGIN(this) { this->cipherBlock = cipherBlockNewP( - cipherModeDecrypt, cipherSpecDupP(cipherSpec, .digestDefault = repoFormatDigest(this->format)), + cipherModeDecrypt, cipherBlockFormatCipherSpec(cipherSpec, this->format, this->round), .header = cipherBlockHeaderNone); } MEM_CONTEXT_OBJ_END(); @@ -229,6 +262,21 @@ cipherBlockFormatProcess(THIS_VOID, const Buffer *const source, Buffer *const de if (this->header[CIPHER_BLOCK_FORMAT_HEADER_SIZE - 1] == CIPHER_BLOCK_FORMAT_MARKER_KEY) this->headerSizeExpected++; + else if (this->header[CIPHER_BLOCK_FORMAT_HEADER_SIZE - 1] == CIPHER_BLOCK_FORMAT_MARKER_ROUND) + this->headerSizeExpected += CIPHER_BLOCK_FORMAT_ROUND_SIZE; + } + // Else the rounds are complete + else if (this->header[CIPHER_BLOCK_FORMAT_HEADER_SIZE - 1] == CIPHER_BLOCK_FORMAT_MARKER_ROUND) + { + const uint8_t *const round = this->header + CIPHER_BLOCK_FORMAT_HEADER_SIZE; + + this->round = + (unsigned int)round[0] << 24 | (unsigned int)round[1] << 16 | (unsigned int)round[2] << 8 | + (unsigned int)round[3]; + + // Error on rounds outside the range this version writes + if (this->round == 0 || this->round > CIPHER_BLOCK_FORMAT_ROUND_MAX) + THROW(FormatError, "invalid cipher header"); } // Else the length is complete so wait for the key id else if (this->headerSizeExpected == CIPHER_BLOCK_FORMAT_HEADER_SIZE + 1) @@ -307,7 +355,7 @@ cipherBlockFormatInputSame(const THIS_VOID) } /*********************************************************************************************************************************** -Report the format the header gave +Report the format and rounds the header gave ***********************************************************************************************************************************/ static Pack * cipherBlockFormatResultPack(THIS_VOID) @@ -327,6 +375,7 @@ cipherBlockFormatResultPack(THIS_VOID) PackWrite *const packWrite = pckWriteNewP(); pckWriteU32P(packWrite, this->format); + pckWriteU32P(packWrite, this->round); pckWriteEndP(packWrite); result = pckMove(pckWriteResult(packWrite), memContextPrior()); @@ -403,7 +452,7 @@ cipherBlockFormatNewPack(const Pack *const paramList) } /**********************************************************************************************************************************/ -FN_EXTERN unsigned int +FN_EXTERN CipherBlockFormatResult cipherBlockFormatResult(PackRead *const packRead) { FUNCTION_TEST_BEGIN(); @@ -412,7 +461,10 @@ cipherBlockFormatResult(PackRead *const packRead) ASSERT(packRead != NULL); - FUNCTION_TEST_RETURN(UINT, pckReadU32P(packRead)); + CipherBlockFormatResult result = {.format = pckReadU32P(packRead)}; + result.round = pckReadU32P(packRead); + + FUNCTION_TEST_RETURN_TYPE(CipherBlockFormatResult, result); } /*********************************************************************************************************************************** @@ -552,6 +604,7 @@ cipherBlockFormatFilterGroupWriteAdd( FUNCTION_LOG_PARAM(CIPHER_SPEC, cipherSpec); FUNCTION_LOG_PARAM(UINT, format); FUNCTION_LOG_PARAM(STRING, param.keyId); + FUNCTION_LOG_PARAM(UINT, param.round); FUNCTION_LOG_END(); FUNCTION_AUDIT_HELPER(); @@ -562,8 +615,11 @@ cipherBlockFormatFilterGroupWriteAdd( ASSERT(param.keyId == NULL || !strEmpty(param.keyId)); ASSERT(param.keyId == NULL || strSize(param.keyId) <= CIPHER_BLOCK_FORMAT_KEY_SIZE_MAX); - // Key id is only valid for format >= 6 + // Key id and rounds are only valid for format >= 6, and a file stores one or the other ASSERT(param.keyId == NULL || format >= REPOSITORY_FORMAT_6); + ASSERT(param.round == 0 || format >= REPOSITORY_FORMAT_6); + ASSERT(param.round == 0 || param.keyId == NULL); + ASSERT(param.round <= CIPHER_BLOCK_FORMAT_ROUND_MAX); if (cipherSpecType(cipherSpec) != cipherTypeNone) { @@ -573,7 +629,7 @@ cipherBlockFormatFilterGroupWriteAdd( ioFilterGroupAdd( filterGroup, cipherBlockNewP( - cipherModeEncrypt, cipherSpecDupP(cipherSpec, .digestDefault = repoFormatDigest(format)), + cipherModeEncrypt, cipherBlockFormatCipherSpec(cipherSpec, format, param.round), .header = header ? cipherBlockHeaderNone : cipherBlockHeaderMagic)); // The filter is added after the block cipher so the plaintext header is placed before the cipher output @@ -583,10 +639,12 @@ cipherBlockFormatFilterGroupWriteAdd( { char headerZ[CIPHER_BLOCK_FORMAT_HEADER_SIZE + 1]; - // Zero-pad the format and add the marker, depending on whether or not there is a key + // Zero-pad the format and add the marker, depending on whether there is a key id or rounds snprintf( headerZ, sizeof(headerZ), CIPHER_BLOCK_FORMAT_MAGIC "%03u%c", format % 1000, - param.keyId == NULL ? CIPHER_BLOCK_FORMAT_MARKER_NONE : CIPHER_BLOCK_FORMAT_MARKER_KEY); + param.keyId != NULL ? + CIPHER_BLOCK_FORMAT_MARKER_KEY : + param.round != 0 ? CIPHER_BLOCK_FORMAT_MARKER_ROUND : CIPHER_BLOCK_FORMAT_MARKER_NONE); Buffer *const headerBuffer = bufNew(CIPHER_BLOCK_FORMAT_HEADER_SIZE_MAX); bufCatC(headerBuffer, (const uint8_t *)headerZ, 0, CIPHER_BLOCK_FORMAT_HEADER_SIZE); @@ -599,6 +657,17 @@ cipherBlockFormatFilterGroupWriteAdd( bufCatC(headerBuffer, &keySize, 0, sizeof(keySize)); bufCatC(headerBuffer, (const uint8_t *)strZ(param.keyId), 0, strSize(param.keyId)); } + // Else write the rounds when present + else if (param.round != 0) + { + const uint8_t roundBytes[CIPHER_BLOCK_FORMAT_ROUND_SIZE] = + { + (uint8_t)(param.round >> 24), (uint8_t)(param.round >> 16), (uint8_t)(param.round >> 8), + (uint8_t)param.round, + }; + + bufCatC(headerBuffer, roundBytes, 0, sizeof(roundBytes)); + } ioFilterGroupAdd(filterGroup, cipherBlockFormatHeaderNew(headerBuffer)); } diff --git a/src/common/format/cipherBlockFormat.h b/src/common/format/cipherBlockFormat.h index a9bf5ae4a7..160ff77ab5 100644 --- a/src/common/format/cipherBlockFormat.h +++ b/src/common/format/cipherBlockFormat.h @@ -47,18 +47,25 @@ FN_EXTERN IoFilter *cipherBlockFormatHeaderNewPack(const Pack *paramList); /*********************************************************************************************************************************** Getters/Setters ***********************************************************************************************************************************/ -// The format the header gave, which is what the header was read for -FN_EXTERN unsigned int cipherBlockFormatResult(PackRead *packRead); +// What the header gave +typedef struct CipherBlockFormatResult +{ + unsigned int format; // Format the file was written at + unsigned int round; // PBKDF2 rounds the file is encrypted with, zero when none +} CipherBlockFormatResult; + +FN_EXTERN CipherBlockFormatResult cipherBlockFormatResult(PackRead *packRead); /*********************************************************************************************************************************** Helper functions ***********************************************************************************************************************************/ // Write the header the format requires into the buffer the content will be written into and add encryption to the filter group. A -// key id is stored in the header when one is specified. No header is added when the repository is not encrypted. +// key id or PBKDF2 rounds are stored in the header when specified. No header is added when the repository is not encrypted. typedef struct CipherBlockFormatFilterGroupWriteAddParam { VAR_PARAM_HEADER; const String *keyId; // Key id to store in the header, NULL to store no id + unsigned int round; // PBKDF2 rounds to derive with, zero for none } CipherBlockFormatFilterGroupWriteAddParam; #define cipherBlockFormatFilterGroupWriteAddP(filterGroup, cipherSpec, format, ...) \ diff --git a/src/config/config.auto.h b/src/config/config.auto.h index 07ea853524..746449aa4f 100644 --- a/src/config/config.auto.h +++ b/src/config/config.auto.h @@ -146,7 +146,7 @@ Option constants #define CFGOPT_VERBOSE "verbose" #define CFGOPT_VERSION "version" -#define CFG_OPTION_TOTAL 202 +#define CFG_OPTION_TOTAL 203 /*********************************************************************************************************************************** Option value constants @@ -591,6 +591,7 @@ typedef enum cfgOptRepoBundleSize, cfgOptRepoCipherPass, cfgOptRepoCipherRotate, + cfgOptRepoCipherRound, cfgOptRepoCipherType, cfgOptRepoFormat, cfgOptRepoGcsBucket, diff --git a/src/config/parse.auto.c.inc b/src/config/parse.auto.c.inc index b736d687ad..29209b59d9 100644 --- a/src/config/parse.auto.c.inc +++ b/src/config/parse.auto.c.inc @@ -65,6 +65,7 @@ static const StringPubConst parseRuleValueStr[] = PARSE_RULE_STRPUB("5432"), // val/str PARSE_RULE_STRPUB("5MiB"), // val/str PARSE_RULE_STRPUB("6"), // val/str + PARSE_RULE_STRPUB("600000"), // val/str PARSE_RULE_STRPUB("64KiB"), // val/str PARSE_RULE_STRPUB("65535"), // val/str PARSE_RULE_STRPUB("7d"), // val/str @@ -207,6 +208,7 @@ typedef enum parseRuleValStrQT_5432_QT, // val/str/enum parseRuleValStrQT_5MiB_QT, // val/str/enum parseRuleValStrQT_6_QT, // val/str/enum + parseRuleValStrQT_600000_QT, // val/str/enum parseRuleValStrQT_64KiB_QT, // val/str/enum parseRuleValStrQT_65535_QT, // val/str/enum parseRuleValStrQT_7d_QT, // val/str/enum @@ -326,6 +328,7 @@ static const int parseRuleValueInt[] = 5432, // val/int 8432, // val/int 65535, // val/int + 600000, // val/int 9999999, // val/int }; @@ -356,6 +359,7 @@ static const uint8_t parseRuleValueIntStrMap[] = parseRuleValStrQT_5432_QT, // val/int/strmap parseRuleValStrQT_8432_QT, // val/int/strmap parseRuleValStrQT_65535_QT, // val/int/strmap + parseRuleValStrQT_600000_QT, // val/int/strmap parseRuleValStrQT_9999999_QT, // val/int/strmap }; @@ -386,6 +390,7 @@ typedef enum parseRuleValInt5432, // val/int/enum parseRuleValInt8432, // val/int/enum parseRuleValInt65535, // val/int/enum + parseRuleValInt600000, // val/int/enum parseRuleValInt9999999, // val/int/enum } ParseRuleValueInt; @@ -6038,6 +6043,40 @@ static const ParseRuleOption parseRuleOption[CFG_OPTION_TOTAL] = ), // opt/repo-cipher-rotate ), // opt/repo-cipher-rotate // ----------------------------------------------------------------------------------------------------------------------------- + PARSE_RULE_OPTION // opt/repo-cipher-round + ( // opt/repo-cipher-round + PARSE_RULE_OPTION_NAME("repo-cipher-round"), // opt/repo-cipher-round + PARSE_RULE_OPTION_TYPE(Integer), // opt/repo-cipher-round + PARSE_RULE_OPTION_INTERNAL(true) // opt/repo-cipher-round + PARSE_RULE_OPTION_RESET(true), // opt/repo-cipher-round + PARSE_RULE_OPTION_REQUIRED(true), // opt/repo-cipher-round + PARSE_RULE_OPTION_SECTION(Global), // opt/repo-cipher-round + PARSE_RULE_OPTION_GROUP_ID(Repo), // opt/repo-cipher-round + // opt/repo-cipher-round + PARSE_RULE_OPTION_COMMAND_ROLE_MAIN_VALID_LIST // opt/repo-cipher-round + ( // opt/repo-cipher-round + PARSE_RULE_OPTION_COMMAND(StanzaCreate) // opt/repo-cipher-round + PARSE_RULE_OPTION_COMMAND(StanzaUpgrade) // opt/repo-cipher-round + ), // opt/repo-cipher-round + // opt/repo-cipher-round + PARSE_RULE_OPTIONAL // opt/repo-cipher-round + ( // opt/repo-cipher-round + PARSE_RULE_OPTIONAL_GROUP // opt/repo-cipher-round + ( // opt/repo-cipher-round + PARSE_RULE_OPTIONAL_ALLOW_RANGE // opt/repo-cipher-round + ( // opt/repo-cipher-round + PARSE_RULE_VAL_INT(1), // opt/repo-cipher-round + PARSE_RULE_VAL_INT(600000), // opt/repo-cipher-round + ), // opt/repo-cipher-round + // opt/repo-cipher-round + PARSE_RULE_OPTIONAL_DEFAULT // opt/repo-cipher-round + ( // opt/repo-cipher-round + PARSE_RULE_VAL_INT(600000), // opt/repo-cipher-round + ), // opt/repo-cipher-round + ), // opt/repo-cipher-round + ), // opt/repo-cipher-round + ), // opt/repo-cipher-round + // ----------------------------------------------------------------------------------------------------------------------------- PARSE_RULE_OPTION // opt/repo-cipher-type ( // opt/repo-cipher-type PARSE_RULE_OPTION_NAME("repo-cipher-type"), // opt/repo-cipher-type @@ -12212,6 +12251,7 @@ static const uint8_t optionResolveOrder[] = cfgOptRepoBundle, // opt-resolve-order cfgOptRepoBundleLimit, // opt-resolve-order cfgOptRepoBundleSize, // opt-resolve-order + cfgOptRepoCipherRound, // opt-resolve-order cfgOptRepoCipherType, // opt-resolve-order cfgOptRepoFormat, // opt-resolve-order cfgOptRepoGcsUserProject, // opt-resolve-order diff --git a/src/info/info.c b/src/info/info.c index 99d8c80247..c0aef5a187 100644 --- a/src/info/info.c +++ b/src/info/info.c @@ -321,13 +321,17 @@ infoNewLoad( if (cipherResult != NULL) { - const unsigned int formatHeader = cipherBlockFormatResult(cipherResult); + const CipherBlockFormatResult formatResult = cipherBlockFormatResult(cipherResult); - if (this->pub.format != formatHeader) + if (this->pub.format != formatResult.format) { THROW_FMT( - FormatError, "repository format %u does not match header format %u", this->pub.format, formatHeader); + FormatError, "repository format %u does not match header format %u", this->pub.format, + formatResult.format); } + + // Rounds the file is encrypted with + this->pub.cipherRound = formatResult.round; } } MEM_CONTEXT_TEMP_END(); @@ -642,6 +646,23 @@ infoCipherRotateTimeSet(Info *const this, const time_t cipherRotateTime) FUNCTION_TEST_RETURN_VOID(); } +/**********************************************************************************************************************************/ +FN_EXTERN void +infoCipherRoundSet(Info *const this, const unsigned int cipherRound) +{ + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM(INFO, this); + FUNCTION_TEST_PARAM(UINT, cipherRound); + FUNCTION_TEST_END(); + + ASSERT(this != NULL); + ASSERT(cipherRound == 0 || infoFormat(this) >= REPOSITORY_FORMAT_6); + + this->pub.cipherRound = cipherRound; + + FUNCTION_TEST_RETURN_VOID(); +} + /**********************************************************************************************************************************/ FN_EXTERN void infoCipherSpecSet(Info *const this, const CipherSpec *const cipherSpec) diff --git a/src/info/info.h b/src/info/info.h index 9a13db12a2..e259a5fc78 100644 --- a/src/info/info.h +++ b/src/info/info.h @@ -57,6 +57,7 @@ typedef struct InfoPub const String *backrestVersion; // pgBackRest version CipherSpecMap *cipherSpecMap; // Cipher keys for dependent files time_t cipherRotateTime; // Time of the last cipher key rotation + unsigned int cipherRound; // PBKDF2 rounds the file is encrypted with, zero when none } InfoPub; // Repository format @@ -96,6 +97,15 @@ infoCipherRotateTime(const Info *const this) FN_EXTERN void infoCipherRotateTimeSet(Info *this, time_t cipherRotateTime); +// PBKDF2 rounds the file is encrypted with, zero when none +FN_INLINE_ALWAYS unsigned int +infoCipherRound(const Info *const this) +{ + return THIS_PUB(Info)->cipherRound; +} + +FN_EXTERN void infoCipherRoundSet(Info *this, unsigned int cipherRound); + // pgBackRest version FN_INLINE_ALWAYS const String * infoBackrestVersion(const Info *const this) diff --git a/src/info/infoArchive.c b/src/info/infoArchive.c index 4396626fbb..dbac767e42 100644 --- a/src/info/infoArchive.c +++ b/src/info/infoArchive.c @@ -237,6 +237,22 @@ infoArchivePgSet(InfoArchive *const this, const unsigned int pgVersion, const ui FUNCTION_LOG_RETURN(INFO_ARCHIVE, this); } +/**********************************************************************************************************************************/ +FN_EXTERN void +infoArchiveCipherRoundSet(InfoArchive *const this, const unsigned int cipherRound) +{ + FUNCTION_LOG_BEGIN(logLevelDebug); + FUNCTION_LOG_PARAM(INFO_ARCHIVE, this); + FUNCTION_LOG_PARAM(UINT, cipherRound); + FUNCTION_LOG_END(); + + ASSERT(this != NULL); + + infoCipherRoundSet(infoPgInfo(infoArchivePg(this)), cipherRound); + + FUNCTION_LOG_RETURN_VOID(); +} + /**********************************************************************************************************************************/ FN_EXTERN void infoArchiveFormatSet(InfoArchive *const this, const unsigned int format) @@ -372,7 +388,8 @@ infoArchiveSaveFile( // Write output into a buffer since it needs to be saved to storage twice Buffer *const buffer = bufNew(ioBufferSize()); IoWrite *const write = ioBufferWriteNew(buffer); - cipherBlockFormatFilterGroupWriteAddP(ioWriteFilterGroup(write), cipherSpec, infoArchiveFormat(infoArchive)); + cipherBlockFormatFilterGroupWriteAddP( + ioWriteFilterGroup(write), cipherSpec, infoArchiveFormat(infoArchive), .round = infoArchiveCipherRound(infoArchive)); infoArchiveSave(infoArchive, write); // Save the file and make a copy diff --git a/src/info/infoArchive.h b/src/info/infoArchive.h index ac14e1a67c..27746010f5 100644 --- a/src/info/infoArchive.h +++ b/src/info/infoArchive.h @@ -81,6 +81,15 @@ infoArchiveCipherRotateTime(const InfoArchive *const this) return infoCipherRotateTime(infoPgInfo(infoArchivePg(this))); } +// PBKDF2 rounds the file is encrypted with, zero when none +FN_INLINE_ALWAYS unsigned int +infoArchiveCipherRound(const InfoArchive *const this) +{ + return infoCipherRound(infoPgInfo(infoArchivePg(this))); +} + +FN_EXTERN void infoArchiveCipherRoundSet(InfoArchive *this, unsigned int cipherRound); + // Repository format FN_INLINE_ALWAYS unsigned int infoArchiveFormat(const InfoArchive *const this) diff --git a/src/info/infoBackup.c b/src/info/infoBackup.c index 8594845c29..6353c54e6c 100644 --- a/src/info/infoBackup.c +++ b/src/info/infoBackup.c @@ -408,6 +408,23 @@ infoBackupFormatSet(InfoBackup *const this, const unsigned int format) FUNCTION_LOG_RETURN_VOID(); } +/**********************************************************************************************************************************/ +FN_EXTERN void +infoBackupCipherRoundSet(InfoBackup *const this, const unsigned int cipherRound) +{ + FUNCTION_LOG_BEGIN(logLevelDebug); + FUNCTION_LOG_PARAM(INFO_BACKUP, this); + FUNCTION_LOG_PARAM(UINT, cipherRound); + FUNCTION_LOG_END(); + + ASSERT(this != NULL); + + infoCipherRoundSet(infoPgInfo(infoBackupPg(this)), cipherRound); + this->pub.updated = true; + + FUNCTION_LOG_RETURN_VOID(); +} + /**********************************************************************************************************************************/ FN_EXTERN InfoBackupData infoBackupData(const InfoBackup *const this, const unsigned int backupDataIdx) @@ -938,7 +955,8 @@ infoBackupSaveFile( // Write output into a buffer since it needs to be saved to storage twice Buffer *const buffer = bufNew(ioBufferSize()); IoWrite *const write = ioBufferWriteNew(buffer); - cipherBlockFormatFilterGroupWriteAddP(ioWriteFilterGroup(write), cipherSpec, infoBackupFormat(infoBackup)); + cipherBlockFormatFilterGroupWriteAddP( + ioWriteFilterGroup(write), cipherSpec, infoBackupFormat(infoBackup), .round = infoBackupCipherRound(infoBackup)); infoBackupSave(infoBackup, write); // Save the file and make a copy diff --git a/src/info/infoBackup.h b/src/info/infoBackup.h index 78fd2e26ca..72d9cc656d 100644 --- a/src/info/infoBackup.h +++ b/src/info/infoBackup.h @@ -111,6 +111,15 @@ infoBackupCipherSpec(const InfoBackup *const this) return infoPgCipherSpec(infoBackupPg(this)); } +// PBKDF2 rounds the file is encrypted with, zero when none +FN_INLINE_ALWAYS unsigned int +infoBackupCipherRound(const InfoBackup *const this) +{ + return infoCipherRound(infoPgInfo(infoBackupPg(this))); +} + +FN_EXTERN void infoBackupCipherRoundSet(InfoBackup *this, unsigned int cipherRound); + // Repository format FN_INLINE_ALWAYS unsigned int infoBackupFormat(const InfoBackup *const this) diff --git a/test/src/harness/host.c b/test/src/harness/host.c index 6492c946d8..5cac8a8283 100644 --- a/test/src/harness/host.c +++ b/test/src/harness/host.c @@ -708,6 +708,7 @@ hrnHostConfig(HrnHost *const this) { strCatFmt(config, "repo1-cipher-type=%s\n", zNewStrId(cipherSpecType(hrnHostLocal.cipherSpecMain))); strCatFmt(config, "repo1-cipher-pass=%s\n", strZ(strNewBuf(cipherSpecPass(hrnHostLocal.cipherSpecMain)))); + strCatZ(config, "repo1-cipher-round=1000\n"); } if (hrnHostLocal.bundle) diff --git a/test/src/harness/info.c b/test/src/harness/info.c index a11f657565..cfd5cebc88 100644 --- a/test/src/harness/info.c +++ b/test/src/harness/info.c @@ -184,6 +184,7 @@ harnessInfoEncrypt(const Buffer *const content, const CipherSpec *const cipherSp FUNCTION_HARNESS_PARAM(BUFFER, content); FUNCTION_HARNESS_PARAM(CIPHER_SPEC, cipherSpec); FUNCTION_HARNESS_PARAM(UINT, param.format); + FUNCTION_HARNESS_PARAM(UINT, param.round); FUNCTION_HARNESS_END(); ASSERT(content != NULL); @@ -195,7 +196,7 @@ harnessInfoEncrypt(const Buffer *const content, const CipherSpec *const cipherSp if (param.format == 0) cipherBlockFilterGroupAdd(ioWriteFilterGroup(write), cipherModeEncrypt, cipherSpec); else - cipherBlockFormatFilterGroupWriteAddP(ioWriteFilterGroup(write), cipherSpec, param.format); + cipherBlockFormatFilterGroupWriteAddP(ioWriteFilterGroup(write), cipherSpec, param.format, .round = param.round); ioWriteOpen(write); ioWrite(write, content); diff --git a/test/src/harness/info.h b/test/src/harness/info.h index 733bb8144e..1ff722832b 100644 --- a/test/src/harness/info.h +++ b/test/src/harness/info.h @@ -48,6 +48,7 @@ typedef struct HarnessInfoEncryptParam { VAR_PARAM_HEADER; unsigned int format; // Repository format the file is stored at + unsigned int round; // PBKDF2 rounds stored in the header, zero for none } HarnessInfoEncryptParam; #define harnessInfoEncryptP(content, cipherSpec, ...) \ diff --git a/test/src/module/command/stanzaTest.c b/test/src/module/command/stanzaTest.c index 602f85d4cc..419fe716d3 100644 --- a/test/src/module/command/stanzaTest.c +++ b/test/src/module/command/stanzaTest.c @@ -12,6 +12,12 @@ Test Stanza Commands #include "harness/postgres.h" #include "harness/pq.h" +/*********************************************************************************************************************************** +Test data +***********************************************************************************************************************************/ +// Passphrase generated with openssl rand -base64 32 +#define TEST_CIPHER_PASS_GENERATED "SOb5SRNoVLdVRtr2K3lr/oDmocARh2c+SObK6H8CtFY=" + /*********************************************************************************************************************************** Test Run ***********************************************************************************************************************************/ @@ -119,6 +125,8 @@ testRun(void) hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 2, TEST_PATH "/repo2"); hrnCfgArgKeyRawStrId(argList, cfgOptRepoCipherType, 2, cipherTypeAes256Cbc); hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 2, "12345678"); + // Rounds are not used at format 5 + hrnCfgArgKeyRawZ(argList, cfgOptRepoCipherRound, 2, "1"); hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 3, TEST_PATH "/repo3"); hrnCfgArgKeyRawZ(argList, cfgOptRepoFormat, 3, "6"); // Request a format for repo1 as well, where the stanza already exists @@ -126,7 +134,7 @@ testRun(void) hrnCfgArgKeyRawZ(argList, cfgOptRepoPath, 4, TEST_PATH "/repo4"); hrnCfgArgKeyRawStrId(argList, cfgOptRepoCipherType, 4, cipherTypeAes256Cbc); hrnCfgArgKeyRawZ(argList, cfgOptRepoFormat, 4, "6"); - hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 4, "87654321"); + hrnCfgEnvKeyRawZ(cfgOptRepoCipherPass, 4, TEST_CIPHER_PASS_GENERATED); HRN_CFG_LOAD(cfgCmdStanzaCreate, argList); const time_t timeCreate = time(NULL); @@ -136,6 +144,7 @@ testRun(void) "P00 INFO: stanza-create for stanza 'db' on repo1\n" "P00 INFO: stanza 'db' already exists on repo1 and is valid\n" "P00 INFO: stanza-create for stanza 'db' on repo2\n" + "P00 WARN: option 'repo2-cipher-round' requires repository format 6 or higher\n" "P00 INFO: stanza-create for stanza 'db' on repo3\n" "P00 INFO: stanza-create for stanza 'db' on repo4"); @@ -191,7 +200,8 @@ testRun(void) TEST_ASSIGN( infoArchive, infoArchiveLoadFile( - storageRepoIdx(3), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF("87654321"))), + storageRepoIdx(3), INFO_ARCHIVE_PATH_FILE_STR, + cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF(TEST_CIPHER_PASS_GENERATED))), "load archive info from encrypted repo4"); const CipherSpecMap *const cipherSpecMapCreate = infoArchiveCipherSpecMap(infoArchive); @@ -212,10 +222,40 @@ testRun(void) TEST_ASSIGN( infoBackup, infoBackupLoadFile( - storageRepoIdx(3), INFO_BACKUP_PATH_FILE_STR, cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF("87654321"))), + storageRepoIdx(3), INFO_BACKUP_PATH_FILE_STR, + cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF(TEST_CIPHER_PASS_GENERATED))), "load backup info from encrypted repo4"); TEST_RESULT_UINT(cipherSpecType(infoBackupCipherSpec(infoBackup)), cipherTypeAes256Cbc, "cipher sub set"); + // A passphrase that looks generated uses one round when the rounds are not requested + TEST_RESULT_BOOL( + memcmp( + bufPtrConst(storageGetP(storageNewReadP(storageRepoIdx(3), INFO_BACKUP_PATH_FILE_STR))), "PGBR006P\x00\x00\x00\x01", + 12) == 0, + true, "header contains the rounds"); + + // ------------------------------------------------------------------------------------------------------------------------- + TEST_TITLE("passphrase that looks generated"); + + #define TEST_CIPHER_SPEC_GENERATED(pass) cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF(pass)) + + TEST_RESULT_BOOL(cipherPassGenerated(TEST_CIPHER_SPEC_GENERATED(TEST_CIPHER_PASS_GENERATED)), true, "generated"); + TEST_RESULT_BOOL(cipherPassGenerated(TEST_CIPHER_SPEC_GENERATED("not base64")), false, "not base64"); + TEST_RESULT_BOOL( + cipherPassGenerated(TEST_CIPHER_SPEC_GENERATED("SOb5SRNoVLdVRtr2K3lr/oDmocARh2c+SObK6H8CtA==")), false, + "less than 256 bits"); + TEST_RESULT_BOOL( + cipherPassGenerated(TEST_CIPHER_SPEC_GENERATED("Y29ycmVjdAlob3JzZQ0KYmF0dGVyeSBzdGFwbGUgYW5kIG1vcmUgdGV4dA==")), false, + "text"); + TEST_RESULT_BOOL( + cipherPassGenerated(TEST_CIPHER_SPEC_GENERATED("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")), false, + "too few distinct bytes"); + TEST_RESULT_BOOL( + cipherPassGenerated(TEST_CIPHER_SPEC_GENERATED("AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8=")), false, + "too few distinct differences"); + + #undef TEST_CIPHER_SPEC_GENERATED + // ------------------------------------------------------------------------------------------------------------------------- TEST_TITLE("cmdStanzaCreate missing files - multi-repo and encryption"); @@ -1248,6 +1288,8 @@ testRun(void) // Migrate the repository hrnCfgArgKeyRawZ(argList, cfgOptRepoFormat, 1, "6"); + StringList *const argListRound = strLstDup(argList); + hrnCfgArgKeyRawZ(argList, cfgOptRepoCipherRound, 1, "1"); HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argList); const time_t timeMigrate = time(NULL); @@ -1288,6 +1330,39 @@ testRun(void) TEST_RESULT_STR_Z( strNewBuf(storageGetP(walRead)), "WAL BEFORE MIGRATION", "wal from before the migration is still readable"); + // ------------------------------------------------------------------------------------------------------------------------- + TEST_TITLE("stanza-upgrade - rounds are changed only when requested"); + + TEST_RESULT_UINT(infoArchiveCipherRound(infoArchiveMigrate), 1, "archive info rounds set by the migration"); + + HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argListRound); + + TEST_RESULT_UINT(cipherRoundGet(0), 600000, "default rounds for a passphrase that does not look generated"); + TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - rounds not requested"); + TEST_RESULT_LOG( + "P00 INFO: stanza-upgrade for stanza 'db' on repo1\n" + "P00 INFO: stanza 'db' on repo1 is already up to date"); + + hrnCfgArgKeyRawZ(argListRound, cfgOptRepoCipherRound, 1, "2"); + HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argListRound); + + TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - rounds requested"); + TEST_RESULT_LOG("P00 INFO: stanza-upgrade for stanza 'db' on repo1"); + + TEST_RESULT_UINT( + infoArchiveCipherRound(infoArchiveLoadFile(storageRepoIdx(0), INFO_ARCHIVE_PATH_FILE_STR, cipherSpecMain)), 2, + "archive info rounds"); + TEST_RESULT_UINT( + infoBackupCipherRound(infoBackupLoadFile(storageRepoIdx(0), INFO_BACKUP_PATH_FILE_STR, cipherSpecMain)), 2, + "backup info rounds"); + + HRN_CFG_LOAD(cfgCmdStanzaUpgrade, argListRound); + + TEST_RESULT_VOID(cmdStanzaUpgrade(), "stanza upgrade - rounds requested are already set"); + TEST_RESULT_LOG( + "P00 INFO: stanza-upgrade for stanza 'db' on repo1\n" + "P00 INFO: stanza 'db' on repo1 is already up to date"); + hrnCfgEnvKeyRemoveRaw(cfgOptRepoCipherPass, 1); // ------------------------------------------------------------------------------------------------------------------------- diff --git a/test/src/module/common/cryptoTest.c b/test/src/module/common/cryptoTest.c index 35ed8217b2..2c1fc41d31 100644 --- a/test/src/module/common/cryptoTest.c +++ b/test/src/module/common/cryptoTest.c @@ -144,18 +144,26 @@ testRun(void) TEST_RESULT_UINT( cipherSpecType(cipherSpecNewPack(pckReadNew(pckWriteResult(packWrite)))), cipherTypeNone, "unpack none"); - // Else it contains the type, digest, and pass + // Else it contains the type, digest, rounds, and pass packWrite = pckWriteNewP(); - cipherSpecPack(packWrite, cipherSpecNewP(cipherTypeAes256Cbc, testPass, .digest = hashTypeSha1)); + cipherSpecPack( + packWrite, cipherSpecDupP(cipherSpecNewP(cipherTypeAes256Cbc, testPass, .digest = hashTypeSha1, .round = 2))); pckWriteEndP(packWrite); const CipherSpec *const cipherSpecUnpack = cipherSpecNewPack(pckReadNew(pckWriteResult(packWrite))); TEST_RESULT_UINT(cipherSpecType(cipherSpecUnpack), cipherTypeAes256Cbc, "unpack type"); TEST_RESULT_UINT(cipherSpecDigest(cipherSpecUnpack), hashTypeSha1, "unpack digest"); + TEST_RESULT_UINT(cipherSpecRound(cipherSpecUnpack), 2, "unpack round"); TEST_RESULT_STR_Z(strNewBuf(cipherSpecPass(cipherSpecUnpack)), TEST_PASS, "unpack pass"); + // The pass is never logged + char logBuf[STACK_TRACE_PARAM_MAX]; + + TEST_RESULT_VOID(FUNCTION_LOG_OBJECT_FORMAT(cipherSpecUnpack, cipherSpecToLog, logBuf, sizeof(logBuf)), "cipherSpecToLog"); + TEST_RESULT_Z(logBuf, "{type: aes-256-cbc, digest: sha1, round: 2}", "check log"); + CipherBlock *cipherBlock = (CipherBlock *)ioFilterDriver(cipherBlockNewP(cipherModeEncrypt, cipherSpec)); TEST_RESULT_UINT(cipherBlock->mode, cipherModeEncrypt, "mode is valid"); TEST_RESULT_UINT(bufSize(cipherBlock->pass), strlen(TEST_PASS), "passphrase size is valid"); @@ -316,6 +324,27 @@ testRun(void) ioFilterFree(blockDecryptFilter); + // ------------------------------------------------------------------------------------------------------------------------- + TEST_TITLE("decrypt with PBKDF2 rounds"); + + // Salt and output of: printf plaintext | openssl enc -aes-256-cbc -pbkdf2 -iter 2 -md sha256 + // -pass pass:areallybadpassphrase -S 0001020304050607 + blockDecryptFilter = cipherBlockNewPack( + ioFilterParamList( + cipherBlockNewP( + cipherModeDecrypt, cipherSpecNewP(cipherTypeAes256Cbc, testPass, .digest = hashTypeSha256, .round = 2), + .header = cipherBlockHeaderNone))); + + bufUsedZero(decryptBuffer); + + ioFilterProcessInOut( + blockDecryptFilter, bufNewDecode(encodingHex, STRDEF("0001020304050607e5a4b36e38b409aa730bf7087ee58e92")), + decryptBuffer); + ioFilterProcessInOut(blockDecryptFilter, NULL, decryptBuffer); + TEST_RESULT_STR_Z(strNewBuf(decryptBuffer), TEST_PLAINTEXT, "check decrypt"); + + ioFilterFree(blockDecryptFilter); + // Invalid cipher header // ------------------------------------------------------------------------------------------------------------------------- blockDecryptFilter = cipherBlockNewP(cipherModeDecrypt, TEST_CIPHER_SPEC()); diff --git a/test/src/module/common/formatTest.c b/test/src/module/common/formatTest.c index 6f02ca5fb8..bdd06b79c5 100644 --- a/test/src/module/common/formatTest.c +++ b/test/src/module/common/formatTest.c @@ -101,8 +101,11 @@ testRun(void) TEST_RESULT_STR_Z(strNewBuf(headerResult), TEST_PLAINTEXT, "content decrypted with the digest the header called for"); TEST_RESULT_UINT( - cipherBlockFormatResult(ioFilterGroupResultP(headerFilterGroup, CIPHER_BLOCK_FORMAT_FILTER_TYPE)), + cipherBlockFormatResult(ioFilterGroupResultP(headerFilterGroup, CIPHER_BLOCK_FORMAT_FILTER_TYPE)).format, REPOSITORY_FORMAT_6, "filter reports the format"); + TEST_RESULT_UINT( + cipherBlockFormatResult(ioFilterGroupResultP(headerFilterGroup, CIPHER_BLOCK_FORMAT_FILTER_TYPE)).round, 0, + "filter reports no rounds"); // ------------------------------------------------------------------------------------------------------------------------- TEST_TITLE("content is handed on in pieces when the destination cannot take it whole"); @@ -143,24 +146,44 @@ testRun(void) TEST_RESULT_BOOL(bufEq(piecesResult, piecesPlainText), true, "content decrypted in pieces"); // ------------------------------------------------------------------------------------------------------------------------- - TEST_TITLE("the header is read no matter how the input is split"); + TEST_TITLE("the header is read no matter how the input is split, here with rounds"); + + Buffer *const roundBuffer = bufNew(0); + IoWrite *const roundWrite = ioBufferWriteNew(roundBuffer); + cipherBlockFormatFilterGroupWriteAddP(ioWriteFilterGroup(roundWrite), cipherSpec, REPOSITORY_FORMAT_6, .round = 2); + ioWriteOpen(roundWrite); + ioWrite(roundWrite, testPlainText); + ioWriteClose(roundWrite); + + TEST_RESULT_BOOL( + memcmp( + bufPtrConst(roundBuffer), CIPHER_BLOCK_FORMAT_MAGIC "006P\x00\x00\x00\x02", + CIPHER_BLOCK_FORMAT_HEADER_SIZE + CIPHER_BLOCK_FORMAT_ROUND_SIZE) == 0, + true, "header contains the rounds"); + + // The rounds come from the header Buffer *const splitResult = bufNew(0); IoWrite *const splitWrite = ioBufferWriteNew(splitResult); cipherBlockFormatFilterGroupReadAdd(ioWriteFilterGroup(splitWrite), cipherSpec); - // Write in pieces smaller than the header so it arrives in more than one part, then a piece that completes it exactly, so - // that what follows arrives with the header already read + // Write in pieces smaller than the header so it arrives in more than one part, including the rounds, then a piece that + // completes it exactly, so that what follows arrives with the header already read ioBufferSizeSet(4); ioWriteOpen(splitWrite); - ioWrite(splitWrite, BUF(bufPtrConst(headerBuffer), 4)); - ioWrite(splitWrite, BUF(bufPtrConst(headerBuffer) + 4, 4)); - ioWrite(splitWrite, BUF(bufPtrConst(headerBuffer) + 8, bufUsed(headerBuffer) - 8)); + ioWrite(splitWrite, BUF(bufPtrConst(roundBuffer), 4)); + ioWrite(splitWrite, BUF(bufPtrConst(roundBuffer) + 4, 4)); + ioWrite(splitWrite, BUF(bufPtrConst(roundBuffer) + 8, 2)); + ioWrite(splitWrite, BUF(bufPtrConst(roundBuffer) + 10, 2)); + ioWrite(splitWrite, BUF(bufPtrConst(roundBuffer) + 12, bufUsed(roundBuffer) - 12)); ioWriteClose(splitWrite); ioBufferSizeSet(TEST_BUFFER_SIZE); TEST_RESULT_STR_Z(strNewBuf(splitResult), TEST_PLAINTEXT, "content decrypted from split input"); + TEST_RESULT_UINT( + cipherBlockFormatResult(ioFilterGroupResultP(ioWriteFilterGroup(splitWrite), CIPHER_BLOCK_FORMAT_FILTER_TYPE)).round, 2, + "filter reports the rounds"); // ------------------------------------------------------------------------------------------------------------------------- TEST_TITLE("a file that begins with the magic was written before there was a header"); @@ -188,7 +211,7 @@ testRun(void) TEST_RESULT_STR_Z(strNewBuf(headerResult), TEST_PLAINTEXT, "content decrypted"); TEST_RESULT_UINT( - cipherBlockFormatResult(ioFilterGroupResultP(headerFilterGroup, CIPHER_BLOCK_FORMAT_FILTER_TYPE)), + cipherBlockFormatResult(ioFilterGroupResultP(headerFilterGroup, CIPHER_BLOCK_FORMAT_FILTER_TYPE)).format, REPOSITORY_FORMAT_5, "filter reports the format before the header"); // ------------------------------------------------------------------------------------------------------------------------- @@ -232,11 +255,11 @@ testRun(void) // ------------------------------------------------------------------------------------------------------------------------- TEST_TITLE("damaged headers"); - // Decrypt the buffer above after damaging a byte of the header, which is the same buffer for each case - #define TEST_HEADER_DAMAGE(damageIdx, damageChar, errorType, errorMessage) \ + // Decrypt a buffer above after damaging a byte of the header + #define TEST_HEADER_DAMAGE(buffer, damageIdx, damageChar, errorType, errorMessage) \ do \ { \ - Buffer *const damaged = bufDup(headerBuffer); \ + Buffer *const damaged = bufDup(buffer); \ bufPtr(damaged)[damageIdx] = damageChar; \ \ IoWrite *const write = ioBufferWriteNew(bufNew(0)); \ @@ -248,25 +271,29 @@ testRun(void) while (0) // Where the format should be, so this version cannot tell what it is looking at - TEST_HEADER_DAMAGE(CIPHER_BLOCK_FORMAT_MAGIC_SIZE, 'X', FormatError, "invalid cipher header"); + TEST_HEADER_DAMAGE(headerBuffer, CIPHER_BLOCK_FORMAT_MAGIC_SIZE, 'X', FormatError, "invalid cipher header"); // The byte held back for later, which must be the one this version writes - TEST_HEADER_DAMAGE(CIPHER_BLOCK_FORMAT_HEADER_SIZE - 1, 'X', FormatError, "invalid cipher header"); + TEST_HEADER_DAMAGE(headerBuffer, CIPHER_BLOCK_FORMAT_HEADER_SIZE - 1, 'X', FormatError, "invalid cipher header"); + + // Rounds of zero and more rounds than this version writes + TEST_HEADER_DAMAGE(roundBuffer, CIPHER_BLOCK_FORMAT_HEADER_SIZE + 3, 0, FormatError, "invalid cipher header"); + TEST_HEADER_DAMAGE(roundBuffer, CIPHER_BLOCK_FORMAT_HEADER_SIZE, 'X', FormatError, "invalid cipher header"); // A format newer than this version can read, reported before anything is decrypted TEST_HEADER_DAMAGE( - CIPHER_BLOCK_FORMAT_HEADER_SIZE - 2, '7', FormatError, + headerBuffer, CIPHER_BLOCK_FORMAT_HEADER_SIZE - 2, '7', FormatError, "repository format 7 requires a newer version of " PROJECT_NAME "\n" "HINT: " PROJECT_NAME " " PROJECT_VERSION " supports repository format 5 to 6."); // A format older than this version can read TEST_HEADER_DAMAGE( - CIPHER_BLOCK_FORMAT_HEADER_SIZE - 2, '4', FormatError, + headerBuffer, CIPHER_BLOCK_FORMAT_HEADER_SIZE - 2, '4', FormatError, "repository format 4 is no longer supported by " PROJECT_NAME "\n" "HINT: " PROJECT_NAME " " PROJECT_VERSION " supports repository format 5 to 6."); // Neither a header nor the magic, which is what a file that was never encrypted looks like from here - TEST_HEADER_DAMAGE(0, 'X', CryptoError, "cipher header invalid"); + TEST_HEADER_DAMAGE(headerBuffer, 0, 'X', CryptoError, "cipher header invalid"); #undef TEST_HEADER_DAMAGE diff --git a/test/src/module/info/infoArchiveTest.c b/test/src/module/info/infoArchiveTest.c index e880dcbab9..88039b947a 100644 --- a/test/src/module/info/infoArchiveTest.c +++ b/test/src/module/info/infoArchiveTest.c @@ -198,20 +198,20 @@ testRun(void) // ------------------------------------------------------------------------------------------------------------------------- TEST_TITLE("save and load archive info file"); - InfoArchive *infoArchive = infoArchiveNew(PG_VERSION_10, 6569239123849665999, REPOSITORY_FORMAT_DEFAULT, NULL); + const CipherSpec *const cipherSpec = cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF("pass")); + + InfoArchive *infoArchive = infoArchiveNew(PG_VERSION_10, 6569239123849665999, REPOSITORY_FORMAT_6, NULL); + TEST_RESULT_VOID(infoArchiveCipherRoundSet(infoArchive, 2), "set rounds"); TEST_RESULT_VOID( - infoArchiveSaveFile(infoArchive, storageTest, STRDEF(INFO_ARCHIVE_FILE), cipherSpecNewNone()), - "save archive info"); + infoArchiveSaveFile(infoArchive, storageTest, STRDEF(INFO_ARCHIVE_FILE), cipherSpec), "save archive info"); - TEST_ASSIGN( - infoArchive, infoArchiveLoadFile(storageTest, STRDEF(INFO_ARCHIVE_FILE), cipherSpecNewNone()), - "load main"); + TEST_ASSIGN(infoArchive, infoArchiveLoadFile(storageTest, STRDEF(INFO_ARCHIVE_FILE), cipherSpec), "load main"); TEST_RESULT_UINT(infoPgDataCurrent(infoArchivePg(infoArchive)).systemId, 6569239123849665999, "check file loaded"); + TEST_RESULT_UINT(infoArchiveCipherRound(infoArchive), 2, "check rounds"); HRN_STORAGE_REMOVE(storageTest, INFO_ARCHIVE_FILE, .errorOnMissing = true, .comment = "remove main so only copy exists"); - TEST_ASSIGN( - infoArchive, infoArchiveLoadFile(storageTest, STRDEF(INFO_ARCHIVE_FILE), cipherSpecNewNone()), - "load copy"); + TEST_ASSIGN(infoArchive, infoArchiveLoadFile(storageTest, STRDEF(INFO_ARCHIVE_FILE), cipherSpec), "load copy"); TEST_RESULT_UINT(infoPgDataCurrent(infoArchivePg(infoArchive)).systemId, 6569239123849665999, "check file loaded"); + TEST_RESULT_UINT(infoArchiveCipherRound(infoArchive), 2, "check rounds"); } } diff --git a/test/src/module/info/infoBackupTest.c b/test/src/module/info/infoBackupTest.c index a905d3ee5b..9722d3c96f 100644 --- a/test/src/module/info/infoBackupTest.c +++ b/test/src/module/info/infoBackupTest.c @@ -918,22 +918,22 @@ testRun(void) // ------------------------------------------------------------------------------------------------------------------------- TEST_TITLE("save and load backup info file"); + const CipherSpec *const cipherSpec = cipherSpecNewP(cipherTypeAes256Cbc, BUFSTRDEF("pass")); + InfoBackup *infoBackup = infoBackupNew( - PG_VERSION_10, 6569239123849665999, hrnPgCatalogVersion(PG_VERSION_10), REPOSITORY_FORMAT_DEFAULT, NULL); + PG_VERSION_10, 6569239123849665999, hrnPgCatalogVersion(PG_VERSION_10), REPOSITORY_FORMAT_6, NULL); + TEST_RESULT_VOID(infoBackupCipherRoundSet(infoBackup, 2), "set rounds"); TEST_RESULT_VOID( - infoBackupSaveFile( - infoBackup, storageTest, STRDEF(INFO_BACKUP_FILE), cipherSpecNewNone()), "save backup info"); + infoBackupSaveFile(infoBackup, storageTest, STRDEF(INFO_BACKUP_FILE), cipherSpec), "save backup info"); - TEST_ASSIGN( - infoBackup, infoBackupLoadFile(storageTest, STRDEF(INFO_BACKUP_FILE), cipherSpecNewNone()), - "load main"); + TEST_ASSIGN(infoBackup, infoBackupLoadFile(storageTest, STRDEF(INFO_BACKUP_FILE), cipherSpec), "load main"); TEST_RESULT_UINT(infoPgDataCurrent(infoBackupPg(infoBackup)).systemId, 6569239123849665999, "check file loaded"); + TEST_RESULT_UINT(infoBackupCipherRound(infoBackup), 2, "check rounds"); HRN_STORAGE_REMOVE(storageTest, INFO_BACKUP_FILE, .errorOnMissing = true, .comment = "remove main so only copy exists"); - TEST_ASSIGN( - infoBackup, infoBackupLoadFile(storageTest, STRDEF(INFO_BACKUP_FILE), cipherSpecNewNone()), - "load copy"); + TEST_ASSIGN(infoBackup, infoBackupLoadFile(storageTest, STRDEF(INFO_BACKUP_FILE), cipherSpec), "load copy"); TEST_RESULT_UINT(infoPgDataCurrent(infoBackupPg(infoBackup)).systemId, 6569239123849665999, "check file loaded"); + TEST_RESULT_UINT(infoBackupCipherRound(infoBackup), 2, "check rounds"); } // ***************************************************************************************************************************** diff --git a/test/src/module/info/infoTest.c b/test/src/module/info/infoTest.c index f130ca39ed..5d2e8fb79f 100644 --- a/test/src/module/info/infoTest.c +++ b/test/src/module/info/infoTest.c @@ -316,11 +316,15 @@ testRun(void) TEST_ASSIGN( info, infoNewLoad( - testInfoReadHeader(harnessInfoEncryptP(contentSave, cipherSpec, .format = REPOSITORY_FORMAT_6), cipherSpec), + testInfoReadHeader( + harnessInfoEncryptP(contentSave, cipherSpec, .format = REPOSITORY_FORMAT_6, .round = 2), cipherSpec), cipherSpec, harnessInfoLoadNewCallback, strNew()), "load migrated info"); TEST_RESULT_UINT(infoFormat(info), REPOSITORY_FORMAT_6, " check format"); TEST_RESULT_UINT(cipherSpecDigest(testInfoCipherSpec(info)), hashTypeSha1, " check cipher sub digest unchanged"); + TEST_RESULT_UINT(infoCipherRound(info), 2, " check rounds from the header"); + TEST_RESULT_VOID(infoCipherRoundSet(info, 3), "set rounds"); + TEST_RESULT_UINT(infoCipherRound(info), 3, " check rounds"); // Header // -------------------------------------------------------------------------------------------------------------------------

view thread (10+ messages)  latest in thread

Message-ID: <gh-pgbackrest-pgbackrest-2883@github.com>
Permalink:  ../gh-pgbackrest-pgbackrest-2883@github.com/

 ·  · 


This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox