1 // Copyright 2017 The Fuchsia Authors 2 // 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file or at 5 // https://opensource.org/licenses/MIT 6 7 #include <lib/crypto/entropy/collector.h> 8 9 #include <assert.h> 10 11 namespace crypto { 12 13 namespace entropy { 14 Collector(const char * name,size_t entropy_per_1000_bytes)15Collector::Collector(const char* name, size_t entropy_per_1000_bytes) 16 : name_(name, strnlen(name, ZX_MAX_NAME_LEN)), 17 entropy_per_1000_bytes_(entropy_per_1000_bytes) { 18 DEBUG_ASSERT(entropy_per_1000_bytes_ > 0); 19 DEBUG_ASSERT(entropy_per_1000_bytes_ <= 8000); 20 } 21 ~Collector()22Collector::~Collector() { 23 } 24 BytesNeeded(size_t bits) const25size_t Collector::BytesNeeded(size_t bits) const { 26 // This avoids overflow, and (probably more likely) programming errors. 27 DEBUG_ASSERT(bits <= (1024u * 1024u)); 28 // Round up, to ensure that the result always contains at least the 29 // requested amount of entropy. 30 return (1000u * bits + entropy_per_1000_bytes_ - 1u) 31 / entropy_per_1000_bytes_; 32 } 33 34 } // namespace entropy 35 36 } // namespace crypto 37