OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/aes_128_gcm_helpers_nss.h" | 5 #include "crypto/aes_128_gcm_helpers_nss.h" |
6 | 6 |
7 #include <pk11pub.h> | 7 #include <pk11pub.h> |
8 #include <secerr.h> | 8 #include <secerr.h> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 param.len = sizeof(CK_GCM_PARAMS); | 418 param.len = sizeof(CK_GCM_PARAMS); |
419 | 419 |
420 size_t maximum_output_length = input.size(); | 420 size_t maximum_output_length = input.size(); |
421 if (mode == ENCRYPT) | 421 if (mode == ENCRYPT) |
422 maximum_output_length += auth_tag_size; | 422 maximum_output_length += auth_tag_size; |
423 | 423 |
424 unsigned int output_length = 0; | 424 unsigned int output_length = 0; |
425 unsigned char* raw_input = const_cast<unsigned char*>( | 425 unsigned char* raw_input = const_cast<unsigned char*>( |
426 reinterpret_cast<const unsigned char*>(input.data())); | 426 reinterpret_cast<const unsigned char*>(input.data())); |
427 unsigned char* raw_output = reinterpret_cast<unsigned char*>( | 427 unsigned char* raw_output = reinterpret_cast<unsigned char*>( |
428 WriteInto(output, maximum_output_length + 1 /* null */)); | 428 base::WriteInto(output, maximum_output_length + 1 /* null */)); |
429 | 429 |
430 PK11Helper_TransformFunction* transform_function = | 430 PK11Helper_TransformFunction* transform_function = |
431 mode == DECRYPT ? PK11DecryptHelper : PK11EncryptHelper; | 431 mode == DECRYPT ? PK11DecryptHelper : PK11EncryptHelper; |
432 | 432 |
433 const SECStatus result = transform_function( | 433 const SECStatus result = transform_function( |
434 aead_key.get(), CKM_AES_GCM, ¶m, raw_output, &output_length, | 434 aead_key.get(), CKM_AES_GCM, ¶m, raw_output, &output_length, |
435 maximum_output_length, raw_input, input.size()); | 435 maximum_output_length, raw_input, input.size()); |
436 | 436 |
437 if (result != SECSuccess) | 437 if (result != SECSuccess) |
438 return false; | 438 return false; |
(...skipping 22 matching lines...) Expand all Loading... |
461 | 461 |
462 } // namespace | 462 } // namespace |
463 | 463 |
464 TEST_F(Aes128GcmHelpersTest, RoundTrip) { | 464 TEST_F(Aes128GcmHelpersTest, RoundTrip) { |
465 const std::string message = "Hello, world!"; | 465 const std::string message = "Hello, world!"; |
466 | 466 |
467 const size_t kKeySize = 16; | 467 const size_t kKeySize = 16; |
468 const size_t kNonceSize = 16; | 468 const size_t kNonceSize = 16; |
469 | 469 |
470 std::string key, nonce; | 470 std::string key, nonce; |
471 RandBytes(WriteInto(&key, kKeySize + 1), kKeySize); | 471 RandBytes(base::WriteInto(&key, kKeySize + 1), kKeySize); |
472 RandBytes(WriteInto(&nonce, kNonceSize + 1), kNonceSize); | 472 RandBytes(base::WriteInto(&nonce, kNonceSize + 1), kNonceSize); |
473 | 473 |
474 // AEAD_AES_128_GCM is defined with a default authentication tag size of 16, | 474 // AEAD_AES_128_GCM is defined with a default authentication tag size of 16, |
475 // but RFC 5282 extends this to authentication tag sizes of 8 and 12 as well. | 475 // but RFC 5282 extends this to authentication tag sizes of 8 and 12 as well. |
476 size_t auth_tag_size = base::RandInt(2, 4) * 4; | 476 size_t auth_tag_size = base::RandInt(2, 4) * 4; |
477 | 477 |
478 std::string encrypted; | 478 std::string encrypted; |
479 ASSERT_TRUE(DecryptOrEncrypt(ENCRYPT, message, key, nonce, | 479 ASSERT_TRUE(DecryptOrEncrypt(ENCRYPT, message, key, nonce, |
480 base::StringPiece(), auth_tag_size, &encrypted)); | 480 base::StringPiece(), auth_tag_size, &encrypted)); |
481 | 481 |
482 std::string decrypted; | 482 std::string decrypted; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 | 571 |
572 const std::string expected_output_with_tag = expected_output + tag; | 572 const std::string expected_output_with_tag = expected_output + tag; |
573 | 573 |
574 EXPECT_TRUE(has_output); | 574 EXPECT_TRUE(has_output); |
575 EXPECT_EQ(expected_output_with_tag, output); | 575 EXPECT_EQ(expected_output_with_tag, output); |
576 } | 576 } |
577 } | 577 } |
578 } | 578 } |
579 | 579 |
580 } // namespace crypto | 580 } // namespace crypto |
OLD | NEW |