| 150 | } |
| 151 | |
| 152 | Reference<BlobCipherKey> BlobCipherKeyIdCache::insertBaseCipherKey(const EncryptCipherBaseKeyId& baseCipherId, |
| 153 | const uint8_t* baseCipher, |
| 154 | int baseCipherLen) { |
| 155 | ASSERT_GT(baseCipherId, ENCRYPT_INVALID_CIPHER_KEY_ID); |
| 156 | |
| 157 | // BaseCipherKeys are immutable, given the routine invocation updates 'latestCipher', |
| 158 | // ensure no key-tampering is done |
| 159 | Reference<BlobCipherKey> latestCipherKey = getLatestCipherKey(); |
| 160 | if (latestCipherKey.isValid() && latestCipherKey->getBaseCipherId() == baseCipherId) { |
| 161 | if (memcmp(latestCipherKey->rawBaseCipher(), baseCipher, baseCipherLen) == 0) { |
| 162 | TraceEvent("InsertBaseCipherKey_AlreadyPresent") |
| 163 | .detail("BaseCipherKeyId", baseCipherId) |
| 164 | .detail("DomainId", domainId); |
| 165 | // Key is already present; nothing more to do. |
| 166 | return latestCipherKey; |
| 167 | } else { |
| 168 | TraceEvent("InsertBaseCipherKey_UpdateCipher") |
| 169 | .detail("BaseCipherKeyId", baseCipherId) |
| 170 | .detail("DomainId", domainId); |
| 171 | throw encrypt_update_cipher(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | Reference<BlobCipherKey> cipherKey = |
| 176 | makeReference<BlobCipherKey>(domainId, baseCipherId, baseCipher, baseCipherLen); |
| 177 | BlobCipherKeyIdCacheKey cacheKey = getCacheKey(cipherKey->getBaseCipherId(), cipherKey->getSalt()); |
| 178 | keyIdCache.emplace(cacheKey, cipherKey); |
| 179 | |
| 180 | // Update the latest BaseCipherKeyId for the given encryption domain |
| 181 | latestBaseCipherKeyId = baseCipherId; |
| 182 | latestRandomSalt = cipherKey->getSalt(); |
| 183 | |
| 184 | return cipherKey; |
| 185 | } |
| 186 | |
| 187 | Reference<BlobCipherKey> BlobCipherKeyIdCache::insertBaseCipherKey(const EncryptCipherBaseKeyId& baseCipherId, |
| 188 | const uint8_t* baseCipher, |
no test coverage detected