rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS 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 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 5 | #include "update_engine/omaha_hash_calculator.h" |
| 6 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | #include <openssl/bio.h> |
| 8 | #include <openssl/buffer.h> |
| 9 | #include <openssl/evp.h> |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 10 | #include "base/logging.h" |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 11 | #include "update_engine/utils.h" |
| 12 | |
| 13 | using std::string; |
| 14 | using std::vector; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 15 | |
| 16 | namespace chromeos_update_engine { |
| 17 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 18 | OmahaHashCalculator::OmahaHashCalculator() : valid_(false) { |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 19 | valid_ = (SHA256_Init(&ctx_) == 1); |
| 20 | LOG_IF(ERROR, !valid_) << "SHA256_Init failed"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | // Update is called with all of the data that should be hashed in order. |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 24 | // Mostly just passes the data through to OpenSSL's SHA256_Update() |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 25 | bool OmahaHashCalculator::Update(const char* data, size_t length) { |
| 26 | TEST_AND_RETURN_FALSE(valid_); |
| 27 | TEST_AND_RETURN_FALSE(hash_.empty()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 28 | COMPILE_ASSERT(sizeof(size_t) <= sizeof(unsigned long), |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 29 | length_param_may_be_truncated_in_SHA256_Update); |
| 30 | TEST_AND_RETURN_FALSE(SHA256_Update(&ctx_, data, length) == 1); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 31 | return true; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // Call Finalize() when all data has been passed in. This mostly just |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 35 | // calls OpenSSL's SHA256_Final() and then base64 encodes the hash. |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 36 | bool OmahaHashCalculator::Finalize() { |
| 37 | bool success = true; |
| 38 | TEST_AND_RETURN_FALSE(hash_.empty()); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 39 | TEST_AND_RETURN_FALSE(raw_hash_.empty()); |
| 40 | raw_hash_.resize(SHA256_DIGEST_LENGTH); |
| 41 | TEST_AND_RETURN_FALSE( |
| 42 | SHA256_Final(reinterpret_cast<unsigned char*>(&raw_hash_[0]), |
| 43 | &ctx_) == 1); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 44 | |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 45 | // Convert raw_hash_ to base64 encoding and store it in hash_. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 46 | BIO *b64 = BIO_new(BIO_f_base64()); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 47 | if (!b64) |
| 48 | LOG(INFO) << "BIO_new(BIO_f_base64()) failed"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 49 | BIO *bmem = BIO_new(BIO_s_mem()); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 50 | if (!bmem) |
| 51 | LOG(INFO) << "BIO_new(BIO_s_mem()) failed"; |
| 52 | if (b64 && bmem) { |
| 53 | b64 = BIO_push(b64, bmem); |
Darin Petkov | d7061ab | 2010-10-06 14:37:09 -0700 | [diff] [blame] | 54 | success = |
| 55 | (BIO_write(b64, &raw_hash_[0], raw_hash_.size()) == |
| 56 | static_cast<int>(raw_hash_.size())); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 57 | if (success) |
| 58 | success = (BIO_flush(b64) == 1); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 59 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 60 | BUF_MEM *bptr = NULL; |
| 61 | BIO_get_mem_ptr(b64, &bptr); |
| 62 | hash_.assign(bptr->data, bptr->length - 1); |
| 63 | } |
| 64 | if (b64) { |
| 65 | BIO_free_all(b64); |
| 66 | b64 = NULL; |
| 67 | } |
| 68 | return success; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 71 | bool OmahaHashCalculator::RawHashOfData(const vector<char>& data, |
| 72 | vector<char>* out_hash) { |
| 73 | OmahaHashCalculator calc; |
| 74 | calc.Update(&data[0], data.size()); |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 75 | |
| 76 | out_hash->resize(out_hash->size() + SHA256_DIGEST_LENGTH); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 77 | TEST_AND_RETURN_FALSE( |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 78 | SHA256_Final(reinterpret_cast<unsigned char*>(&(*(out_hash->end() - |
| 79 | SHA256_DIGEST_LENGTH))), |
| 80 | &calc.ctx_) == 1); |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 81 | return true; |
| 82 | } |
| 83 | |
| 84 | string OmahaHashCalculator::OmahaHashOfBytes( |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 85 | const void* data, size_t length) { |
| 86 | OmahaHashCalculator calc; |
| 87 | calc.Update(reinterpret_cast<const char*>(data), length); |
| 88 | calc.Finalize(); |
| 89 | return calc.hash(); |
| 90 | } |
| 91 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 92 | string OmahaHashCalculator::OmahaHashOfString(const string& str) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 93 | return OmahaHashOfBytes(str.data(), str.size()); |
| 94 | } |
| 95 | |
Andrew de los Reyes | 932bc4c | 2010-08-23 18:14:09 -0700 | [diff] [blame] | 96 | string OmahaHashCalculator::OmahaHashOfData(const vector<char>& data) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 97 | return OmahaHashOfBytes(&data[0], data.size()); |
| 98 | } |
| 99 | |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame^] | 100 | string OmahaHashCalculator::GetContext() const { |
| 101 | return string(reinterpret_cast<const char*>(&ctx_), sizeof(ctx_)); |
| 102 | } |
| 103 | |
| 104 | bool OmahaHashCalculator::SetContext(const std::string& context) { |
| 105 | TEST_AND_RETURN_FALSE(context.size() == sizeof(ctx_)); |
| 106 | memcpy(&ctx_, context.data(), sizeof(ctx_)); |
| 107 | return true; |
| 108 | } |
| 109 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 110 | } // namespace chromeos_update_engine |