1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/nacl_host/nacl_validation_cache.h"
7 #include "base/pickle.h"
8 #include "base/rand_util.h"
12 // For the moment, choose an arbitrary cache size.
13 const size_t kValidationCacheCacheSize
= 200;
14 // Key size is equal to the block size (not the digest size) of SHA256.
15 const size_t kValidationCacheKeySize
= 64;
16 // Entry size is equal to the digest size of SHA256.
17 const size_t kValidationCacheEntrySize
= 32;
19 const char kValidationCacheBeginMagic
[] = "NaCl";
20 const char kValidationCacheEndMagic
[] = "Done";
24 NaClValidationCache::NaClValidationCache()
25 : validation_cache_(kValidationCacheCacheSize
) {
26 // Make sure the cache key is unpredictable, even if the cache has not
31 NaClValidationCache::~NaClValidationCache() {
32 // Make clang's style checking happy by adding a destructor.
35 bool NaClValidationCache::QueryKnownToValidate(const std::string
& signature
,
37 if (signature
.length() == kValidationCacheEntrySize
) {
38 ValidationCacheType::iterator iter
;
40 iter
= validation_cache_
.Get(signature
);
42 iter
= validation_cache_
.Peek(signature
);
44 if (iter
!= validation_cache_
.end()) {
51 void NaClValidationCache::SetKnownToValidate(const std::string
& signature
) {
52 if (signature
.length() == kValidationCacheEntrySize
) {
53 validation_cache_
.Put(signature
, true);
57 void NaClValidationCache::Serialize(Pickle
* pickle
) const {
58 // Mark the beginning of the data stream.
59 pickle
->WriteString(kValidationCacheBeginMagic
);
60 pickle
->WriteString(validation_cache_key_
);
61 pickle
->WriteInt(validation_cache_
.size());
63 // Serialize the cache in reverse order so that deserializing it can easily
64 // preserve the MRU order. (Last item deserialized => most recently used.)
65 ValidationCacheType::const_reverse_iterator iter
;
66 for (iter
= validation_cache_
.rbegin();
67 iter
!= validation_cache_
.rend();
69 pickle
->WriteString(iter
->first
);
72 // Mark the end of the data stream.
73 pickle
->WriteString(kValidationCacheEndMagic
);
76 void NaClValidationCache::Reset() {
77 validation_cache_key_
= base::RandBytesAsString(kValidationCacheKeySize
);
78 validation_cache_
.Clear();
81 bool NaClValidationCache::Deserialize(const Pickle
* pickle
) {
82 bool success
= DeserializeImpl(pickle
);
89 bool NaClValidationCache::DeserializeImpl(const Pickle
* pickle
) {
90 PickleIterator
iter(*pickle
);
95 if (!iter
.ReadString(&buffer
))
97 if (0 != buffer
.compare(kValidationCacheBeginMagic
))
101 if (!iter
.ReadString(&buffer
))
103 if (buffer
.size() != kValidationCacheKeySize
)
106 validation_cache_key_
= buffer
;
107 validation_cache_
.Clear();
110 if (!iter
.ReadInt(&count
))
112 for (int i
= 0; i
< count
; ++i
) {
113 if (!iter
.ReadString(&buffer
))
115 if (buffer
.size() != kValidationCacheEntrySize
)
117 validation_cache_
.Put(buffer
, true);
121 if (!iter
.ReadString(&buffer
))
123 if (0 != buffer
.compare(kValidationCacheEndMagic
))