uint32_t SecretKey::retain() { uint32_t cRefs = ASMAtomicIncU32(&m_cRefs); if (cRefs == 1) { int rc = RTMemSaferUnscramble(m_pbKey, m_cbKey); AssertRC(rc); } return cRefs; }
static void doMemSaferScramble(RTTEST hTest, void *pvBuf, size_t cbAlloc) { /* * Fill it with random bytes and make a reference copy of these. */ RTRandBytes(pvBuf, cbAlloc); void *pvRef = RTMemDup(pvBuf, cbAlloc); RTTESTI_CHECK_RETV(pvRef); /* * Scramble the allocation and check that it no longer matches the refernece bytes. */ int rc = RTMemSaferScramble(pvBuf, cbAlloc); if (RT_SUCCESS(rc)) { if (!memcmp(pvRef, pvBuf, cbAlloc)) RTTestIFailed("Memory blocks must differ (%z bytes, 0x%p vs. 0x%p)!\n", cbAlloc, pvRef, pvBuf); else { /* * Check that unscrambling returns the original content. */ rc = RTMemSaferUnscramble(pvBuf, cbAlloc); if (RT_SUCCESS(rc)) { if (memcmp(pvRef, pvBuf, cbAlloc)) RTTestIFailed("Memory blocks must not differ (%z bytes, 0x%p vs. 0x%p)!\n", cbAlloc, pvRef, pvBuf); } else RTTestIFailed("Unscrambling %z bytes failed with %Rrc!\n", cbAlloc, rc); } } else RTTestIFailed("Scrambling %z bytes failed with %Rrc!\n", cbAlloc, rc); RTMemFree(pvRef); }