uint32_t SecretKey::release()
{
    uint32_t cRefs = ASMAtomicDecU32(&m_cRefs);
    if (!cRefs)
    {
        int rc = RTMemSaferScramble(m_pbKey, m_cbKey);
        AssertRC(rc);
    }

    return cRefs;
}
SecretKey::SecretKey(const uint8_t *pbKey, size_t cbKey, bool fKeyBufNonPageable)
{
    m_cRefs            = 0;
    m_fRemoveOnSuspend = false;
    m_cUsers           = 0;
    m_cbKey            = cbKey;

    int rc = RTMemSaferAllocZEx((void **)&this->m_pbKey, cbKey,
                                fKeyBufNonPageable ? RTMEMSAFER_F_REQUIRE_NOT_PAGABLE : 0);
    if (RT_SUCCESS(rc))
    {
        memcpy(this->m_pbKey, pbKey, cbKey);

        /* Scramble content to make retrieving the key more difficult. */
        rc = RTMemSaferScramble(this->m_pbKey, cbKey);
    }
    else
        throw rc;
}
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);
}