NSSCipherStrategy.h (1822B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_quota_NSSCipherStrategy_h 8 #define mozilla_dom_quota_NSSCipherStrategy_h 9 10 #include <array> 11 #include <cstddef> 12 #include <cstdint> 13 14 #include "CipherStrategy.h" 15 #include "ErrorList.h" 16 #include "ScopedNSSTypes.h" 17 #include "mozilla/InitializedOnce.h" 18 #include "mozilla/Result.h" 19 #include "mozilla/Span.h" 20 #include "nsTArray.h" 21 22 namespace mozilla::dom::quota { 23 24 struct NSSCipherStrategy { 25 // Use numeric literals here to avoid having to include NSS headers here. 26 // static_assert's in the cpp file check their consistency. 27 using KeyType = std::array<uint8_t, 32>; 28 static constexpr size_t BlockPrefixLength = 32; 29 static constexpr size_t BasicBlockSize = 16; 30 31 static Result<KeyType, nsresult> GenerateKey(); 32 33 nsresult Init(CipherMode aCipherMode, Span<const uint8_t> aKey, 34 Span<const uint8_t> aInitialIv = Span<const uint8_t>{}); 35 36 nsresult Cipher(Span<uint8_t> aIv, Span<const uint8_t> aIn, 37 Span<uint8_t> aOut); 38 39 static std::array<uint8_t, BlockPrefixLength> MakeBlockPrefix(); 40 41 static Span<const uint8_t> SerializeKey(const KeyType& aKey); 42 43 static Maybe<KeyType> DeserializeKey( 44 const Span<const uint8_t>& aSerializedKey); 45 46 private: 47 // XXX Remove EarlyDestructible, remove moving of the CipherStrategy. 48 LazyInitializedOnceEarlyDestructible<const CipherMode> mMode; 49 LazyInitializedOnceEarlyDestructible<const UniquePK11Context> mPK11Context; 50 nsTArray<uint8_t> mIv; 51 }; 52 53 } // namespace mozilla::dom::quota 54 55 #endif