1*1a96fba6SXin Li // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. 2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be 3*1a96fba6SXin Li // found in the LICENSE file. 4*1a96fba6SXin Li 5*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_SECURE_BLOB_H_ 6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_SECURE_BLOB_H_ 7*1a96fba6SXin Li 8*1a96fba6SXin Li #include <initializer_list> 9*1a96fba6SXin Li #include <string> 10*1a96fba6SXin Li #include <vector> 11*1a96fba6SXin Li 12*1a96fba6SXin Li #include <brillo/asan.h> 13*1a96fba6SXin Li #include <brillo/brillo_export.h> 14*1a96fba6SXin Li 15*1a96fba6SXin Li namespace brillo { 16*1a96fba6SXin Li 17*1a96fba6SXin Li // TODO(sarthakkukreti): remove temp. SecureVector once we break SecureBlob's 18*1a96fba6SXin Li // dependence on std::vector<uint8_t> 19*1a96fba6SXin Li using Blob = std::vector<uint8_t>; 20*1a96fba6SXin Li using SecureVector = std::vector<uint8_t>; 21*1a96fba6SXin Li 22*1a96fba6SXin Li // Conversion of Blob to/from std::string, where the string holds raw byte 23*1a96fba6SXin Li // contents. 24*1a96fba6SXin Li BRILLO_EXPORT std::string BlobToString(const Blob& blob); 25*1a96fba6SXin Li BRILLO_EXPORT Blob BlobFromString(const std::string& bytes); 26*1a96fba6SXin Li 27*1a96fba6SXin Li // Returns a concatenation of given Blobs. 28*1a96fba6SXin Li BRILLO_EXPORT Blob CombineBlobs(const std::initializer_list<Blob>& blobs); 29*1a96fba6SXin Li 30*1a96fba6SXin Li // SecureBlob erases the contents on destruction. It does not guarantee erasure 31*1a96fba6SXin Li // on resize, assign, etc. 32*1a96fba6SXin Li class BRILLO_EXPORT SecureBlob : public Blob { 33*1a96fba6SXin Li public: 34*1a96fba6SXin Li SecureBlob() = default; 35*1a96fba6SXin Li using Blob::vector; // Inherit standard constructors from vector. 36*1a96fba6SXin Li explicit SecureBlob(const Blob& blob); 37*1a96fba6SXin Li explicit SecureBlob(const std::string& data); 38*1a96fba6SXin Li ~SecureBlob(); 39*1a96fba6SXin Li 40*1a96fba6SXin Li void resize(size_type count); 41*1a96fba6SXin Li void resize(size_type count, const value_type& value); 42*1a96fba6SXin Li void clear(); 43*1a96fba6SXin Li 44*1a96fba6SXin Li std::string to_string() const; char_data()45*1a96fba6SXin Li char* char_data() { return reinterpret_cast<char*>(data()); } char_data()46*1a96fba6SXin Li const char* char_data() const { 47*1a96fba6SXin Li return reinterpret_cast<const char*>(data()); 48*1a96fba6SXin Li } 49*1a96fba6SXin Li static SecureBlob Combine(const SecureBlob& blob1, const SecureBlob& blob2); 50*1a96fba6SXin Li static bool HexStringToSecureBlob(const std::string& input, 51*1a96fba6SXin Li SecureBlob* output); 52*1a96fba6SXin Li }; 53*1a96fba6SXin Li 54*1a96fba6SXin Li // Secure memset(). This function is guaranteed to fill in the whole buffer 55*1a96fba6SXin Li // and is not subject to compiler optimization as allowed by Sub-clause 5.1.2.3 56*1a96fba6SXin Li // of C Standard [ISO/IEC 9899:2011] which states: 57*1a96fba6SXin Li // In the abstract machine, all expressions are evaluated as specified by the 58*1a96fba6SXin Li // semantics. An actual implementation need not evaluate part of an expression 59*1a96fba6SXin Li // if it can deduce that its value is not used and that no needed side effects 60*1a96fba6SXin Li // are produced (including any caused by calling a function or accessing 61*1a96fba6SXin Li // a volatile object). 62*1a96fba6SXin Li // While memset() can be optimized out in certain situations (since most 63*1a96fba6SXin Li // compilers implement this function as intrinsic and know of its side effects), 64*1a96fba6SXin Li // this function will not be optimized out. 65*1a96fba6SXin Li // 66*1a96fba6SXin Li // SecureMemset is used to write beyond the size() in several functions. 67*1a96fba6SXin Li // Since this is intentional, disable address sanitizer from analying it. 68*1a96fba6SXin Li BRILLO_EXPORT BRILLO_DISABLE_ASAN void* SecureMemset(void* v, int c, size_t n); 69*1a96fba6SXin Li 70*1a96fba6SXin Li // Compare [n] bytes starting at [s1] with [s2] and return 0 if they match, 71*1a96fba6SXin Li // 1 if they don't. Time taken to perform the comparison is only dependent on 72*1a96fba6SXin Li // [n] and not on the relationship of the match between [s1] and [s2]. 73*1a96fba6SXin Li BRILLO_EXPORT int SecureMemcmp(const void* s1, const void* s2, size_t n); 74*1a96fba6SXin Li 75*1a96fba6SXin Li // Conversion of SecureBlob data to/from SecureBlob hex. This is useful 76*1a96fba6SXin Li // for sensitive data like encryption keys, that should, in the ideal case never 77*1a96fba6SXin Li // be exposed as strings in the first place. In case the existing data or hex 78*1a96fba6SXin Li // string is already exposed as a std::string, it is preferable to use the 79*1a96fba6SXin Li // BlobToString variant. 80*1a96fba6SXin Li BRILLO_EXPORT SecureBlob SecureBlobToSecureHex(const SecureBlob& blob); 81*1a96fba6SXin Li BRILLO_EXPORT SecureBlob SecureHexToSecureBlob(const SecureBlob& hex); 82*1a96fba6SXin Li 83*1a96fba6SXin Li } // namespace brillo 84*1a96fba6SXin Li 85*1a96fba6SXin Li #endif // LIBBRILLO_BRILLO_SECURE_BLOB_H_ 86