| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/hkdf.h" | 5 #include "crypto/hkdf.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> |
| 11 |
| 10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "crypto/hmac.h" | 13 #include "crypto/hmac.h" |
| 13 | 14 |
| 14 namespace crypto { | 15 namespace crypto { |
| 15 | 16 |
| 16 const size_t kSHA256HashLength = 32; | 17 const size_t kSHA256HashLength = 32; |
| 17 | 18 |
| 18 HKDF::HKDF(const base::StringPiece& secret, | 19 HKDF::HKDF(const base::StringPiece& secret, |
| 19 const base::StringPiece& salt, | 20 const base::StringPiece& salt, |
| 20 const base::StringPiece& info, | 21 const base::StringPiece& info, |
| 21 size_t key_bytes_to_generate, | 22 size_t key_bytes_to_generate, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 48 const size_t material_length = 2 * key_bytes_to_generate + | 49 const size_t material_length = 2 * key_bytes_to_generate + |
| 49 2 * iv_bytes_to_generate + | 50 2 * iv_bytes_to_generate + |
| 50 subkey_secret_bytes_to_generate; | 51 subkey_secret_bytes_to_generate; |
| 51 const size_t n = (material_length + kSHA256HashLength-1) / | 52 const size_t n = (material_length + kSHA256HashLength-1) / |
| 52 kSHA256HashLength; | 53 kSHA256HashLength; |
| 53 DCHECK_LT(n, 256u); | 54 DCHECK_LT(n, 256u); |
| 54 | 55 |
| 55 output_.resize(n * kSHA256HashLength); | 56 output_.resize(n * kSHA256HashLength); |
| 56 base::StringPiece previous; | 57 base::StringPiece previous; |
| 57 | 58 |
| 58 scoped_ptr<char[]> buf(new char[kSHA256HashLength + info.size() + 1]); | 59 std::unique_ptr<char[]> buf(new char[kSHA256HashLength + info.size() + 1]); |
| 59 uint8_t digest[kSHA256HashLength]; | 60 uint8_t digest[kSHA256HashLength]; |
| 60 | 61 |
| 61 HMAC hmac(HMAC::SHA256); | 62 HMAC hmac(HMAC::SHA256); |
| 62 result = hmac.Init(prk, sizeof(prk)); | 63 result = hmac.Init(prk, sizeof(prk)); |
| 63 DCHECK(result); | 64 DCHECK(result); |
| 64 | 65 |
| 65 for (size_t i = 0; i < n; i++) { | 66 for (size_t i = 0; i < n; i++) { |
| 66 memcpy(buf.get(), previous.data(), previous.size()); | 67 memcpy(buf.get(), previous.data(), previous.size()); |
| 67 size_t j = previous.size(); | 68 size_t j = previous.size(); |
| 68 memcpy(buf.get() + j, info.data(), info.size()); | 69 memcpy(buf.get() + j, info.data(), info.size()); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 if (subkey_secret_bytes_to_generate) { | 102 if (subkey_secret_bytes_to_generate) { |
| 102 subkey_secret_ = base::StringPiece(reinterpret_cast<char*>(&output_[j]), | 103 subkey_secret_ = base::StringPiece(reinterpret_cast<char*>(&output_[j]), |
| 103 subkey_secret_bytes_to_generate); | 104 subkey_secret_bytes_to_generate); |
| 104 } | 105 } |
| 105 } | 106 } |
| 106 | 107 |
| 107 HKDF::~HKDF() { | 108 HKDF::~HKDF() { |
| 108 } | 109 } |
| 109 | 110 |
| 110 } // namespace crypto | 111 } // namespace crypto |
| OLD | NEW |