OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "crypto/secure_hash.h" | |
6 | |
7 #include <openssl/mem.h> | |
8 #include <openssl/sha.h> | |
9 #include <stddef.h> | |
10 | |
11 #include "base/logging.h" | |
12 #include "base/pickle.h" | |
13 #include "crypto/openssl_util.h" | |
14 | |
15 namespace crypto { | |
16 | |
17 namespace { | |
18 | |
19 class SecureHashSHA256OpenSSL : public SecureHash { | |
20 public: | |
21 SecureHashSHA256OpenSSL() { | |
22 SHA256_Init(&ctx_); | |
23 } | |
24 | |
25 SecureHashSHA256OpenSSL(const SecureHashSHA256OpenSSL& other) { | |
26 memcpy(&ctx_, &other.ctx_, sizeof(ctx_)); | |
27 } | |
28 | |
29 ~SecureHashSHA256OpenSSL() override { | |
30 OPENSSL_cleanse(&ctx_, sizeof(ctx_)); | |
31 } | |
32 | |
33 void Update(const void* input, size_t len) override { | |
34 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len); | |
35 } | |
36 | |
37 void Finish(void* output, size_t len) override { | |
38 ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result( | |
39 static_cast<unsigned char*>(output), len); | |
40 SHA256_Final(result.safe_buffer(), &ctx_); | |
41 } | |
42 | |
43 SecureHash* Clone() const override { | |
44 return new SecureHashSHA256OpenSSL(*this); | |
45 } | |
46 | |
47 size_t GetHashLength() const override { return SHA256_DIGEST_LENGTH; } | |
48 | |
49 private: | |
50 SHA256_CTX ctx_; | |
51 }; | |
52 | |
53 } // namespace | |
54 | |
55 SecureHash* SecureHash::Create(Algorithm algorithm) { | |
56 switch (algorithm) { | |
57 case SHA256: | |
58 return new SecureHashSHA256OpenSSL(); | |
59 default: | |
60 NOTIMPLEMENTED(); | |
61 return NULL; | |
62 } | |
63 } | |
64 | |
65 } // namespace crypto | |
OLD | NEW |