Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(269)

Side by Side Diff: omaha_hash_calculator.cc

Issue 3132033: AU: Sign delta payloads (Closed) Base URL: ssh://git@chromiumos-git/update_engine.git
Patch Set: fixes for review Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « omaha_hash_calculator.h ('k') | payload_signer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. 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 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 "update_engine/omaha_hash_calculator.h"
6
5 #include <openssl/bio.h> 7 #include <openssl/bio.h>
6 #include <openssl/buffer.h> 8 #include <openssl/buffer.h>
7 #include <openssl/evp.h> 9 #include <openssl/evp.h>
8 #include "base/logging.h" 10 #include "base/logging.h"
9 #include "update_engine/omaha_hash_calculator.h" 11 #include "update_engine/utils.h"
12
13 using std::string;
14 using std::vector;
10 15
11 namespace chromeos_update_engine { 16 namespace chromeos_update_engine {
12 17
13 OmahaHashCalculator::OmahaHashCalculator() { 18 OmahaHashCalculator::OmahaHashCalculator() : valid_(false) {
14 CHECK_EQ(SHA1_Init(&ctx_), 1); 19 valid_ = (SHA1_Init(&ctx_) == 1);
20 LOG_IF(ERROR, !valid_) << "SHA1_Init failed";
15 } 21 }
16 22
17 // Update is called with all of the data that should be hashed in order. 23 // Update is called with all of the data that should be hashed in order.
18 // Mostly just passes the data through to OpenSSL's SHA1_Update() 24 // Mostly just passes the data through to OpenSSL's SHA1_Update()
19 void OmahaHashCalculator::Update(const char* data, size_t length) { 25 bool OmahaHashCalculator::Update(const char* data, size_t length) {
20 CHECK(hash_.empty()) << "Can't Update after hash is finalized"; 26 TEST_AND_RETURN_FALSE(valid_);
27 TEST_AND_RETURN_FALSE(hash_.empty());
21 COMPILE_ASSERT(sizeof(size_t) <= sizeof(unsigned long), 28 COMPILE_ASSERT(sizeof(size_t) <= sizeof(unsigned long),
22 length_param_may_be_truncated_in_SHA1_Update); 29 length_param_may_be_truncated_in_SHA1_Update);
23 CHECK_EQ(SHA1_Update(&ctx_, data, length), 1); 30 TEST_AND_RETURN_FALSE(SHA1_Update(&ctx_, data, length) == 1);
31 return true;
24 } 32 }
25 33
26 // Call Finalize() when all data has been passed in. This mostly just 34 // Call Finalize() when all data has been passed in. This mostly just
27 // calls OpenSSL's SHA1_Final() and then base64 encodes the hash. 35 // calls OpenSSL's SHA1_Final() and then base64 encodes the hash.
28 void OmahaHashCalculator::Finalize() { 36 bool OmahaHashCalculator::Finalize() {
29 CHECK(hash_.empty()) << "Don't call Finalize() twice"; 37 bool success = true;
38 TEST_AND_RETURN_FALSE(hash_.empty());
30 unsigned char md[SHA_DIGEST_LENGTH]; 39 unsigned char md[SHA_DIGEST_LENGTH];
31 CHECK_EQ(SHA1_Final(md, &ctx_), 1); 40 TEST_AND_RETURN_FALSE(SHA1_Final(md, &ctx_) == 1);
32 41
33 // Convert md to base64 encoding and store it in hash_ 42 // Convert md to base64 encoding and store it in hash_
34 BIO *b64 = BIO_new(BIO_f_base64()); 43 BIO *b64 = BIO_new(BIO_f_base64());
35 CHECK(b64); 44 if (!b64)
45 LOG(INFO) << "BIO_new(BIO_f_base64()) failed";
36 BIO *bmem = BIO_new(BIO_s_mem()); 46 BIO *bmem = BIO_new(BIO_s_mem());
37 CHECK(bmem); 47 if (!bmem)
38 b64 = BIO_push(b64, bmem); 48 LOG(INFO) << "BIO_new(BIO_s_mem()) failed";
39 CHECK_EQ(BIO_write(b64, md, sizeof(md)), sizeof(md)); 49 if (b64 && bmem) {
40 CHECK_EQ(BIO_flush(b64), 1); 50 b64 = BIO_push(b64, bmem);
51 success = (BIO_write(b64, md, sizeof(md)) == sizeof(md));
52 if (success)
53 success = (BIO_flush(b64) == 1);
41 54
42 BUF_MEM *bptr = NULL; 55 BUF_MEM *bptr = NULL;
43 BIO_get_mem_ptr(b64, &bptr); 56 BIO_get_mem_ptr(b64, &bptr);
44 hash_.assign(bptr->data, bptr->length - 1); 57 hash_.assign(bptr->data, bptr->length - 1);
45 58 }
46 BIO_free_all(b64); 59 if (b64) {
60 BIO_free_all(b64);
61 b64 = NULL;
62 }
63 return success;
47 } 64 }
48 65
49 std::string OmahaHashCalculator::OmahaHashOfBytes( 66 bool OmahaHashCalculator::RawHashOfData(const vector<char>& data,
67 vector<char>* out_hash) {
68 OmahaHashCalculator calc;
69 calc.Update(&data[0], data.size());
70
71 out_hash->resize(out_hash->size() + SHA_DIGEST_LENGTH);
72 TEST_AND_RETURN_FALSE(
73 SHA1_Final(reinterpret_cast<unsigned char*>(&(*(out_hash->end() -
74 SHA_DIGEST_LENGTH))),
75 &calc.ctx_) == 1);
76 return true;
77 }
78
79 string OmahaHashCalculator::OmahaHashOfBytes(
50 const void* data, size_t length) { 80 const void* data, size_t length) {
51 OmahaHashCalculator calc; 81 OmahaHashCalculator calc;
52 calc.Update(reinterpret_cast<const char*>(data), length); 82 calc.Update(reinterpret_cast<const char*>(data), length);
53 calc.Finalize(); 83 calc.Finalize();
54 return calc.hash(); 84 return calc.hash();
55 } 85 }
56 86
57 std::string OmahaHashCalculator::OmahaHashOfString( 87 string OmahaHashCalculator::OmahaHashOfString(const string& str) {
58 const std::string& str) {
59 return OmahaHashOfBytes(str.data(), str.size()); 88 return OmahaHashOfBytes(str.data(), str.size());
60 } 89 }
61 90
62 std::string OmahaHashCalculator::OmahaHashOfData( 91 string OmahaHashCalculator::OmahaHashOfData(const vector<char>& data) {
63 const std::vector<char>& data) {
64 return OmahaHashOfBytes(&data[0], data.size()); 92 return OmahaHashOfBytes(&data[0], data.size());
65 } 93 }
66 94
67 } // namespace chromeos_update_engine 95 } // namespace chromeos_update_engine
OLDNEW
« no previous file with comments | « omaha_hash_calculator.h ('k') | payload_signer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698