/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ #ifndef DOM_QUOTA_ENCRYPTEDRANDOMACCESSBLOCKVIEWV1_H_ #define DOM_QUOTA_ENCRYPTEDRANDOMACCESSBLOCKVIEWV1_H_ #include #include #include "EncryptedRandomAccessBlock.h" #include "mozilla/Span.h" namespace mozilla::dom::quota { /** * Version 1 interpretation of CipherMetadata of EncryptedRandomAccessBlock * (byte offsets relative to the start of CipherMetadata, i.e., byte 32 in the * full block): * * --------+-----------------------------------------------+ * offset | field size | * --------+-----------------------------------------------+ * 0 | Nonce / IV 12 bytes | * 12 | Authentication tag 16 bytes | * 28 | Reserved (unused) 4 bytes | * --------+-----------------------------------------------+ * 32 */ class EncryptedRandomAccessBlockViewV1 { public: static constexpr size_t NonceSize = 12; static constexpr size_t AuthenticationTagSize = 16; template using ConstSpan = Span; template using MutableSpan = Span; private: static constexpr size_t ReservedBytesSize = 4; static_assert(NonceSize + AuthenticationTagSize + ReservedBytesSize == EncryptedRandomAccessBlock::CipherMetadataSize); public: explicit EncryptedRandomAccessBlockViewV1( MutableSpan aBlock) : mCipherMetadata(aBlock) {} /** The AEAD nonce (initialization vector) for this block. */ ConstSpan Nonce() const { return mCipherMetadata.First(); } void SetNonce(ConstSpan aNonce) { memcpy(mCipherMetadata.data(), aNonce.data(), NonceSize); } /** The AEAD authentication tag for this block. */ ConstSpan AuthenticationTag() const { return mCipherMetadata.Subspan(); } void SetAuthenticationTag( ConstSpan aAuthenticationTag) { memcpy(mCipherMetadata.data() + NonceSize, aAuthenticationTag.data(), AuthenticationTagSize); } private: MutableSpan mCipherMetadata; }; } // namespace mozilla::dom::quota #endif // DOM_QUOTA_ENCRYPTEDRANDOMACCESSBLOCKVIEWV1_H_