| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/client_update_protocol/ecdsa.h" | 5 #include "components/client_update_protocol/ecdsa.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 Ecdsa::~Ecdsa() {} | 91 Ecdsa::~Ecdsa() {} |
| 92 | 92 |
| 93 std::unique_ptr<Ecdsa> Ecdsa::Create(int key_version, | 93 std::unique_ptr<Ecdsa> Ecdsa::Create(int key_version, |
| 94 const base::StringPiece& public_key) { | 94 const base::StringPiece& public_key) { |
| 95 DCHECK_GT(key_version, 0); | 95 DCHECK_GT(key_version, 0); |
| 96 DCHECK(!public_key.empty()); | 96 DCHECK(!public_key.empty()); |
| 97 | 97 |
| 98 return base::WrapUnique(new Ecdsa(key_version, public_key)); | 98 return base::WrapUnique(new Ecdsa(key_version, public_key)); |
| 99 } | 99 } |
| 100 | 100 |
| 101 void Ecdsa::OverrideNonceForTesting(int key_version, uint32_t nonce) { | |
| 102 DCHECK(!request_query_cup2key_.empty()); | |
| 103 request_query_cup2key_ = base::StringPrintf("%d:%u", pub_key_version_, nonce); | |
| 104 } | |
| 105 | |
| 106 void Ecdsa::SignRequest(const base::StringPiece& request_body, | 101 void Ecdsa::SignRequest(const base::StringPiece& request_body, |
| 107 std::string* query_params) { | 102 std::string* query_params) { |
| 103 DCHECK(!request_body.empty()); |
| 108 DCHECK(query_params); | 104 DCHECK(query_params); |
| 109 | 105 |
| 110 // Generate a random nonce to use for freshness, build the cup2key query | 106 // Generate a random nonce to use for freshness, build the cup2key query |
| 111 // string, and compute the SHA-256 hash of the request body. Set these | 107 // string, and compute the SHA-256 hash of the request body. Set these |
| 112 // two pieces of data aside to use during ValidateResponse(). | 108 // two pieces of data aside to use during ValidateResponse(). |
| 113 uint32_t nonce = 0; | 109 uint32_t nonce = 0; |
| 114 crypto::RandBytes(&nonce, sizeof(nonce)); | 110 crypto::RandBytes(&nonce, sizeof(nonce)); |
| 115 request_query_cup2key_ = base::StringPrintf("%d:%u", pub_key_version_, nonce); | 111 request_query_cup2key_ = base::StringPrintf("%d:%u", pub_key_version_, nonce); |
| 116 request_hash_ = SHA256HashStr(request_body); | 112 request_hash_ = SHA256HashStr(request_body); |
| 117 | 113 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 // * The signature was modified | 178 // * The signature was modified |
| 183 // * The buffer that the server signed does not match the buffer that the | 179 // * The buffer that the server signed does not match the buffer that the |
| 184 // client assembled -- implying that either request body or response body | 180 // client assembled -- implying that either request body or response body |
| 185 // was modified, or a different nonce value was used. | 181 // was modified, or a different nonce value was used. |
| 186 verifier.VerifyUpdate(&signed_message_hash.front(), | 182 verifier.VerifyUpdate(&signed_message_hash.front(), |
| 187 static_cast<int>(signed_message_hash.size())); | 183 static_cast<int>(signed_message_hash.size())); |
| 188 return verifier.VerifyFinal(); | 184 return verifier.VerifyFinal(); |
| 189 } | 185 } |
| 190 | 186 |
| 191 } // namespace client_update_protocol | 187 } // namespace client_update_protocol |
| OLD | NEW |