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

Side by Side Diff: crypto/hkdf.cc

Issue 12330157: CRYPTO - Resolved comments from wtc. Used scoped_ptr<char[]> and (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « crypto/hkdf.h ('k') | no next file » | 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) 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 "base/logging.h" 7 #include "base/logging.h"
8 #include "crypto/hmac.h" 8 #include "crypto/hmac.h"
9 9
10 namespace crypto { 10 namespace crypto {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 // and info into the output keying material. 42 // and info into the output keying material.
43 const size_t material_length = 43 const size_t material_length =
44 2*key_bytes_to_generate + 2*iv_bytes_to_generate; 44 2*key_bytes_to_generate + 2*iv_bytes_to_generate;
45 const size_t n = (material_length + kSHA256HashLength-1) / 45 const size_t n = (material_length + kSHA256HashLength-1) /
46 kSHA256HashLength; 46 kSHA256HashLength;
47 DCHECK_LT(n, 256u); 47 DCHECK_LT(n, 256u);
48 48
49 output_.resize(n * kSHA256HashLength); 49 output_.resize(n * kSHA256HashLength);
50 base::StringPiece previous; 50 base::StringPiece previous;
51 51
52 char* buf = new char[kSHA256HashLength + info.size() + 1]; 52 scoped_ptr<char[]> buf(new char[kSHA256HashLength + info.size() + 1]);
53 uint8 digest[kSHA256HashLength]; 53 uint8 digest[kSHA256HashLength];
54 54
55 HMAC hmac(HMAC::SHA256); 55 HMAC hmac(HMAC::SHA256);
56 result = hmac.Init(prk, sizeof(prk)); 56 result = hmac.Init(prk, sizeof(prk));
57 DCHECK(result); 57 DCHECK(result);
58 58
59 for (size_t i = 0; i < n; i++) { 59 for (size_t i = 0; i < n; i++) {
60 memcpy(buf, previous.data(), previous.size()); 60 memcpy(buf.get(), previous.data(), previous.size());
61 size_t j = previous.size(); 61 size_t j = previous.size();
62 memcpy(buf + j, info.data(), info.size()); 62 memcpy(buf.get() + j, info.data(), info.size());
63 j += info.size(); 63 j += info.size();
64 buf[j++] = static_cast<char>((i + 1) & 0xFF); 64 buf[j++] = static_cast<char>(i + 1);
65 65
66 result = hmac.Sign(base::StringPiece(buf, j), digest, sizeof(digest)); 66 result = hmac.Sign(base::StringPiece(buf.get(), j), digest, sizeof(digest));
67 DCHECK(result); 67 DCHECK(result);
68 68
69 memcpy(&output_[i*sizeof(digest)], digest, sizeof(digest)); 69 memcpy(&output_[i*sizeof(digest)], digest, sizeof(digest));
70 previous = base::StringPiece(reinterpret_cast<char*>(digest), 70 previous = base::StringPiece(reinterpret_cast<char*>(digest),
71 sizeof(digest)); 71 sizeof(digest));
72 } 72 }
73 73
74 size_t j = 0; 74 size_t j = 0;
75 client_write_key_ = base::StringPiece(reinterpret_cast<char*>(&output_[j]), 75 client_write_key_ = base::StringPiece(reinterpret_cast<char*>(&output_[j]),
76 key_bytes_to_generate); 76 key_bytes_to_generate);
77 j += key_bytes_to_generate; 77 j += key_bytes_to_generate;
78 server_write_key_ = base::StringPiece(reinterpret_cast<char*>(&output_[j]), 78 server_write_key_ = base::StringPiece(reinterpret_cast<char*>(&output_[j]),
79 key_bytes_to_generate); 79 key_bytes_to_generate);
80 j += key_bytes_to_generate; 80 j += key_bytes_to_generate;
81 client_write_iv_ = base::StringPiece(reinterpret_cast<char*>(&output_[j]), 81 client_write_iv_ = base::StringPiece(reinterpret_cast<char*>(&output_[j]),
82 iv_bytes_to_generate); 82 iv_bytes_to_generate);
83 j += iv_bytes_to_generate; 83 j += iv_bytes_to_generate;
84 server_write_iv_ = base::StringPiece(reinterpret_cast<char*>(&output_[j]), 84 server_write_iv_ = base::StringPiece(reinterpret_cast<char*>(&output_[j]),
85 iv_bytes_to_generate); 85 iv_bytes_to_generate);
86 delete[] buf;
87 } 86 }
88 87
89 HKDF::~HKDF() { 88 HKDF::~HKDF() {
90 } 89 }
91 90
92 } // namespace crypto 91 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/hkdf.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698