chromeos: bluetooth: add BluetoothNodeClient
[chromium-blink-merge.git] / crypto / secure_hash_openssl.cc
blob098bf271527f184b2b465384381a33ff6a271dae
1 // Copyright (c) 2011 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 "crypto/secure_hash.h"
7 #include <openssl/ssl.h>
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "base/pickle.h"
12 #include "crypto/openssl_util.h"
14 namespace crypto {
16 namespace {
18 const char kSHA256Descriptor[] = "OpenSSL";
20 class SecureHashSHA256OpenSSL : public SecureHash {
21 public:
22 static const int kSecureHashVersion = 1;
24 SecureHashSHA256OpenSSL() {
25 SHA256_Init(&ctx_);
28 virtual ~SecureHashSHA256OpenSSL() {
29 OPENSSL_cleanse(&ctx_, sizeof(ctx_));
32 virtual void Update(const void* input, size_t len) {
33 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len);
36 virtual void Finish(void* output, size_t len) {
37 ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result(
38 static_cast<unsigned char*>(output), len);
39 SHA256_Final(result.safe_buffer(), &ctx_);
42 virtual bool Serialize(Pickle* pickle);
43 virtual bool Deserialize(void** data_iterator, Pickle* pickle);
45 private:
46 SHA256_CTX ctx_;
49 bool SecureHashSHA256OpenSSL::Serialize(Pickle* pickle) {
50 if (!pickle)
51 return false;
53 if (!pickle->WriteInt(kSecureHashVersion) ||
54 !pickle->WriteString(kSHA256Descriptor) ||
55 !pickle->WriteBytes(&ctx_, sizeof(ctx_))) {
56 return false;
59 return true;
62 bool SecureHashSHA256OpenSSL::Deserialize(void** data_iterator,
63 Pickle* pickle) {
64 if (!pickle)
65 return false;
67 int version;
68 if (!pickle->ReadInt(data_iterator, &version))
69 return false;
71 if (version > kSecureHashVersion)
72 return false; // We don't know how to deal with this.
74 std::string type;
75 if (!pickle->ReadString(data_iterator, &type))
76 return false;
78 if (type != kSHA256Descriptor)
79 return false; // It's the wrong kind.
81 const char* data = NULL;
82 if (!pickle->ReadBytes(data_iterator, &data, sizeof(ctx_)))
83 return false;
85 memcpy(&ctx_, data, sizeof(ctx_));
87 return true;
90 } // namespace
92 SecureHash* SecureHash::Create(Algorithm algorithm) {
93 switch (algorithm) {
94 case SHA256:
95 return new SecureHashSHA256OpenSSL();
96 default:
97 NOTIMPLEMENTED();
98 return NULL;
102 } // namespace crypto