| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "crypto/secure_hash.h" | 5 #include "crypto/secure_hash.h" |
| 6 | 6 |
| 7 #include <openssl/mem.h> | 7 #include <openssl/mem.h> |
| 8 #include <openssl/sha.h> | 8 #include <openssl/sha.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/pickle.h" | 12 #include "base/pickle.h" |
| 13 #include "crypto/openssl_util.h" | 13 #include "crypto/openssl_util.h" |
| 14 | 14 |
| 15 namespace crypto { | 15 namespace crypto { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 class SecureHashSHA1 : public SecureHash { |
| 20 public: |
| 21 SecureHashSHA1() { SHA1_Init(&ctx_); } |
| 22 |
| 23 SecureHashSHA1(const SecureHashSHA1& other) { |
| 24 memcpy(&ctx_, &other.ctx_, sizeof(ctx_)); |
| 25 } |
| 26 |
| 27 ~SecureHashSHA1() override { OPENSSL_cleanse(&ctx_, sizeof(ctx_)); } |
| 28 |
| 29 void Update(const void* input, size_t len) override { |
| 30 SHA1_Update(&ctx_, static_cast<const unsigned char*>(input), len); |
| 31 } |
| 32 |
| 33 void Finish(void* output, size_t len) override { |
| 34 ScopedOpenSSLSafeSizeBuffer<SHA_DIGEST_LENGTH> result( |
| 35 static_cast<unsigned char*>(output), len); |
| 36 SHA1_Final(result.safe_buffer(), &ctx_); |
| 37 } |
| 38 |
| 39 SecureHash* Clone() const override { return new SecureHashSHA1(*this); } |
| 40 |
| 41 size_t GetHashLength() const override { return SHA_DIGEST_LENGTH; } |
| 42 |
| 43 private: |
| 44 SHA_CTX ctx_; |
| 45 }; |
| 46 |
| 19 class SecureHashSHA256 : public SecureHash { | 47 class SecureHashSHA256 : public SecureHash { |
| 20 public: | 48 public: |
| 21 SecureHashSHA256() { | 49 SecureHashSHA256() { |
| 22 SHA256_Init(&ctx_); | 50 SHA256_Init(&ctx_); |
| 23 } | 51 } |
| 24 | 52 |
| 25 SecureHashSHA256(const SecureHashSHA256& other) { | 53 SecureHashSHA256(const SecureHashSHA256& other) { |
| 26 memcpy(&ctx_, &other.ctx_, sizeof(ctx_)); | 54 memcpy(&ctx_, &other.ctx_, sizeof(ctx_)); |
| 27 } | 55 } |
| 28 | 56 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 49 private: | 77 private: |
| 50 SHA256_CTX ctx_; | 78 SHA256_CTX ctx_; |
| 51 }; | 79 }; |
| 52 | 80 |
| 53 } // namespace | 81 } // namespace |
| 54 | 82 |
| 55 SecureHash* SecureHash::Create(Algorithm algorithm) { | 83 SecureHash* SecureHash::Create(Algorithm algorithm) { |
| 56 switch (algorithm) { | 84 switch (algorithm) { |
| 57 case SHA256: | 85 case SHA256: |
| 58 return new SecureHashSHA256(); | 86 return new SecureHashSHA256(); |
| 87 case SHA1: |
| 88 return new SecureHashSHA1(); |
| 59 default: | 89 default: |
| 60 NOTIMPLEMENTED(); | 90 NOTIMPLEMENTED(); |
| 61 return NULL; | 91 return NULL; |
| 62 } | 92 } |
| 63 } | 93 } |
| 64 | 94 |
| 65 } // namespace crypto | 95 } // namespace crypto |
| OLD | NEW |