OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "net/quic/core/crypto/channel_id.h" | 5 #include "net/quic/core/crypto/channel_id.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "net/quic/test_tools/crypto_test_utils.h" | 9 #include "net/quic/test_tools/crypto_test_utils.h" |
10 #include "net/quic/test_tools/quic_test_utils.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
12 | 11 |
13 using base::StringPiece; | 12 using base::StringPiece; |
14 using std::string; | 13 using std::string; |
15 | 14 |
16 namespace net { | 15 namespace net { |
17 namespace test { | 16 namespace test { |
18 | 17 |
19 namespace { | 18 namespace { |
20 | 19 |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 "d1c211fbc2e6d884cddd7cb9d90d5bf4a7311b83f352508033812c776a0e00c0" | 194 "d1c211fbc2e6d884cddd7cb9d90d5bf4a7311b83f352508033812c776a0e00c0" |
196 "03c7e0d628e50736c7512df0acfa9f2320bd102229f46495ae6d0857cc452a84", | 195 "03c7e0d628e50736c7512df0acfa9f2320bd102229f46495ae6d0857cc452a84", |
197 "2d98ea01f754d34bbc3003df5050200abf445ec728556d7ed7d5c54c55552b6d", | 196 "2d98ea01f754d34bbc3003df5050200abf445ec728556d7ed7d5c54c55552b6d", |
198 "9b52672742d637a32add056dfd6d8792f2a33c2e69dafabea09b960bc61e230a", | 197 "9b52672742d637a32add056dfd6d8792f2a33c2e69dafabea09b960bc61e230a", |
199 "06108e525f845d0155bf60193222b3219c98e3d49424c2fb2a0987f825c17959", | 198 "06108e525f845d0155bf60193222b3219c98e3d49424c2fb2a0987f825c17959", |
200 "62b5cdd591e5b507e560167ba8f6f7cda74673eb315680cb89ccbc4eec477dce", | 199 "62b5cdd591e5b507e560167ba8f6f7cda74673eb315680cb89ccbc4eec477dce", |
201 true // P (0 ) | 200 true // P (0 ) |
202 }, | 201 }, |
203 {nullptr}}; | 202 {nullptr}}; |
204 | 203 |
| 204 // Returns true if |ch| is a lowercase hexadecimal digit. |
| 205 bool IsHexDigit(char ch) { |
| 206 return ('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'f'); |
| 207 } |
| 208 |
| 209 // Converts a lowercase hexadecimal digit to its integer value. |
| 210 int HexDigitToInt(char ch) { |
| 211 if ('0' <= ch && ch <= '9') { |
| 212 return ch - '0'; |
| 213 } |
| 214 return ch - 'a' + 10; |
| 215 } |
| 216 |
| 217 // |in| is a string consisting of lowercase hexadecimal digits, where |
| 218 // every two digits represent one byte. |out| is a buffer of size |max_len|. |
| 219 // Converts |in| to bytes and stores the bytes in the |out| buffer. The |
| 220 // number of bytes converted is returned in |*out_len|. Returns true on |
| 221 // success, false on failure. |
| 222 bool DecodeHexString(const char* in, |
| 223 char* out, |
| 224 size_t* out_len, |
| 225 size_t max_len) { |
| 226 if (!in) { |
| 227 *out_len = static_cast<size_t>(-1); |
| 228 return true; |
| 229 } |
| 230 *out_len = 0; |
| 231 while (*in != '\0') { |
| 232 if (!IsHexDigit(*in) || !IsHexDigit(*(in + 1))) { |
| 233 return false; |
| 234 } |
| 235 if (*out_len >= max_len) { |
| 236 return false; |
| 237 } |
| 238 out[*out_len] = HexDigitToInt(*in) * 16 + HexDigitToInt(*(in + 1)); |
| 239 (*out_len)++; |
| 240 in += 2; |
| 241 } |
| 242 return true; |
| 243 } |
| 244 |
205 } // namespace | 245 } // namespace |
206 | 246 |
207 // A known answer test for ChannelIDVerifier. | 247 // A known answer test for ChannelIDVerifier. |
208 TEST(ChannelIDTest, VerifyKnownAnswerTest) { | 248 TEST(ChannelIDTest, VerifyKnownAnswerTest) { |
209 string msg; | 249 char msg[1024]; |
210 string qx; | 250 size_t msg_len; |
211 string qy; | 251 char key[64]; |
212 string r; | 252 size_t qx_len; |
213 string s; | 253 size_t qy_len; |
| 254 char signature[64]; |
| 255 size_t r_len; |
| 256 size_t s_len; |
214 | 257 |
215 for (size_t i = 0; test_vector[i].msg != nullptr; i++) { | 258 for (size_t i = 0; test_vector[i].msg != nullptr; i++) { |
216 SCOPED_TRACE(i); | 259 SCOPED_TRACE(i); |
217 // Decode the test vector. | 260 // Decode the test vector. |
218 ASSERT_TRUE(DecodeHexString(test_vector[i].msg, &msg)); | 261 ASSERT_TRUE( |
219 ASSERT_TRUE(DecodeHexString(test_vector[i].qx, &qx)); | 262 DecodeHexString(test_vector[i].msg, msg, &msg_len, sizeof(msg))); |
220 ASSERT_TRUE(DecodeHexString(test_vector[i].qy, &qy)); | 263 ASSERT_TRUE(DecodeHexString(test_vector[i].qx, key, &qx_len, sizeof(key))); |
221 ASSERT_TRUE(DecodeHexString(test_vector[i].r, &r)); | 264 ASSERT_TRUE(DecodeHexString(test_vector[i].qy, key + qx_len, &qy_len, |
222 ASSERT_TRUE(DecodeHexString(test_vector[i].s, &s)); | 265 sizeof(key) - qx_len)); |
223 | 266 ASSERT_TRUE(DecodeHexString(test_vector[i].r, signature, &r_len, |
224 string key = qx + qy; | 267 sizeof(signature))); |
225 string signature = r + s; | 268 ASSERT_TRUE(DecodeHexString(test_vector[i].s, signature + r_len, &s_len, |
| 269 sizeof(signature) - r_len)); |
226 | 270 |
227 // The test vector's lengths should look sane. | 271 // The test vector's lengths should look sane. |
228 EXPECT_EQ(32u, qx.size()); | 272 EXPECT_EQ(sizeof(key) / 2, qx_len); |
229 EXPECT_EQ(32u, qy.size()); | 273 EXPECT_EQ(sizeof(key) / 2, qy_len); |
230 EXPECT_EQ(32u, r.size()); | 274 EXPECT_EQ(sizeof(signature) / 2, r_len); |
231 EXPECT_EQ(32u, s.size()); | 275 EXPECT_EQ(sizeof(signature) / 2, s_len); |
232 | 276 |
233 EXPECT_EQ(test_vector[i].result, | 277 EXPECT_EQ(test_vector[i].result, |
234 ChannelIDVerifier::VerifyRaw(key, msg, signature, false)); | 278 ChannelIDVerifier::VerifyRaw( |
| 279 StringPiece(key, sizeof(key)), StringPiece(msg, msg_len), |
| 280 StringPiece(signature, sizeof(signature)), false)); |
235 } | 281 } |
236 } | 282 } |
237 | 283 |
238 TEST(ChannelIDTest, SignAndVerify) { | 284 TEST(ChannelIDTest, SignAndVerify) { |
239 std::unique_ptr<ChannelIDSource> source( | 285 std::unique_ptr<ChannelIDSource> source( |
240 CryptoTestUtils::ChannelIDSourceForTesting()); | 286 CryptoTestUtils::ChannelIDSourceForTesting()); |
241 | 287 |
242 const string signed_data = "signed data"; | 288 const string signed_data = "signed data"; |
243 const string hostname = "foo.example.com"; | 289 const string hostname = "foo.example.com"; |
244 std::unique_ptr<ChannelIDKey> channel_id_key; | 290 std::unique_ptr<ChannelIDKey> channel_id_key; |
(...skipping 20 matching lines...) Expand all Loading... |
265 memcpy(bad_signature.get(), signature.data(), signature.size()); | 311 memcpy(bad_signature.get(), signature.data(), signature.size()); |
266 bad_signature[1] ^= 0x80; | 312 bad_signature[1] ^= 0x80; |
267 EXPECT_FALSE(ChannelIDVerifier::Verify( | 313 EXPECT_FALSE(ChannelIDVerifier::Verify( |
268 key, signed_data, string(bad_signature.get(), signature.size()))); | 314 key, signed_data, string(bad_signature.get(), signature.size()))); |
269 | 315 |
270 EXPECT_FALSE(ChannelIDVerifier::Verify(key, "wrong signed data", signature)); | 316 EXPECT_FALSE(ChannelIDVerifier::Verify(key, "wrong signed data", signature)); |
271 } | 317 } |
272 | 318 |
273 } // namespace test | 319 } // namespace test |
274 } // namespace net | 320 } // namespace net |
OLD | NEW |