| Index: components/gcm_driver/crypto/gcm_message_cryptographer_unittest.cc
|
| diff --git a/components/gcm_driver/crypto/gcm_message_cryptographer_unittest.cc b/components/gcm_driver/crypto/gcm_message_cryptographer_unittest.cc
|
| index 1a52c06dc33a2efc78985f3b5a17e03746f8bd8d..d445cbd6839e682e7a5ab0b13cc3164e8e49a9fc 100644
|
| --- a/components/gcm_driver/crypto/gcm_message_cryptographer_unittest.cc
|
| +++ b/components/gcm_driver/crypto/gcm_message_cryptographer_unittest.cc
|
| @@ -11,7 +11,6 @@
|
| #include "base/strings/string_util.h"
|
| #include "components/gcm_driver/crypto/p256_key_util.h"
|
| #include "crypto/random.h"
|
| -#include "crypto/symmetric_key.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| namespace gcm {
|
| @@ -21,79 +20,168 @@ namespace {
|
| // Example plaintext data to use in the tests.
|
| const char kExamplePlaintext[] = "Example plaintext";
|
|
|
| -// The number of bits of the key in AEAD_AES_128_GCM.
|
| -const size_t kKeySizeBits = 128;
|
| -
|
| -// Public keys of both the sender and the recipient as base64url-encoded
|
| -// uncompressed P-256 EC points, as well as an authentication secret used for
|
| -// the reference vectors. All used to create stable output.
|
| -const char kCommonRecipientPublicKey[] =
|
| - "BIXzEKOFquzVlr_1tS1bhmobZU3IJq2bswDflMJsizixqd_HFSvCJaCAotNjBw6A-iKQk7FshA"
|
| - "jdAA-T9Rh1a7U";
|
| -const char kCommonSenderPublicKey[] =
|
| - "BAuzSrdIyKZsHnuOhqklkIKi6fl65V9OdPy6nFwI2SywL5-6I5SkkDtfIL9y7NkoEE345jv2Eo"
|
| - "5n4NIbLJIBjTM";
|
| -const char kCommonAuthSecret[] = "MyAuthenticationSecret";
|
| +// Expected sizes of the different input given to the cryptographer.
|
| +constexpr size_t kEcdhSharedSecretSize = 32;
|
| +constexpr size_t kAuthSecretSize = 16;
|
| +constexpr size_t kSaltSize = 16;
|
| +
|
| +// Keying material for both parties as P-256 EC points. Used to make sure that
|
| +// the test vectors are reproducible.
|
| +const unsigned char kCommonSenderPublicKey[] = {
|
| + 0x04, 0x05, 0x3C, 0xA1, 0xB9, 0xA5, 0xAB, 0xB8, 0x2D, 0x88, 0x48,
|
| + 0x82, 0xC9, 0x49, 0x19, 0x91, 0xD5, 0xFD, 0xD1, 0x92, 0xDB, 0xA7,
|
| + 0x7E, 0x70, 0x48, 0x37, 0x41, 0xCD, 0x90, 0x05, 0x80, 0xDF, 0x65,
|
| + 0x9A, 0xA1, 0x1A, 0x04, 0xF1, 0x98, 0x25, 0xF2, 0xC2, 0x13, 0x5D,
|
| + 0xD9, 0x72, 0x35, 0x75, 0x24, 0xF9, 0xFF, 0x25, 0xD1, 0xBC, 0x84,
|
| + 0x46, 0x4E, 0x88, 0x08, 0x55, 0x70, 0x9F, 0xA7, 0x07, 0xD9};
|
| +static_assert(arraysize(kCommonSenderPublicKey) == 65,
|
| + "Raw P-256 public keys must be 65 bytes in size.");
|
| +
|
| +const unsigned char kCommonRecipientPublicKey[] = {
|
| + 0x04, 0x35, 0x02, 0x67, 0xB9, 0x10, 0x8F, 0x9B, 0xF1, 0x85, 0xF5,
|
| + 0x1B, 0xD7, 0xA4, 0xEF, 0xBD, 0x28, 0xB3, 0x11, 0x40, 0xBA, 0xD0,
|
| + 0xEE, 0xB2, 0x97, 0xDA, 0x6A, 0x93, 0x2D, 0x26, 0x45, 0xBD, 0xB2,
|
| + 0x9A, 0x9F, 0xB8, 0x19, 0xD8, 0x21, 0x6F, 0x66, 0xE3, 0xF6, 0x0B,
|
| + 0x74, 0xB2, 0x28, 0x38, 0xDC, 0xA7, 0x8A, 0x58, 0x0D, 0x56, 0x47,
|
| + 0x3E, 0xD0, 0x5B, 0x5C, 0x93, 0x4E, 0xB3, 0x89, 0x87, 0x64};
|
| +static_assert(arraysize(kCommonRecipientPublicKey) == 65,
|
| + "Raw P-256 public keys must be 65 bytes in size.");
|
| +
|
| +const unsigned char kCommonRecipientPublicKeyX509[] = {
|
| + 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02,
|
| + 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03,
|
| + 0x42, 0x00, 0x04, 0x35, 0x02, 0x67, 0xB9, 0x10, 0x8F, 0x9B, 0xF1, 0x85,
|
| + 0xF5, 0x1B, 0xD7, 0xA4, 0xEF, 0xBD, 0x28, 0xB3, 0x11, 0x40, 0xBA, 0xD0,
|
| + 0xEE, 0xB2, 0x97, 0xDA, 0x6A, 0x93, 0x2D, 0x26, 0x45, 0xBD, 0xB2, 0x9A,
|
| + 0x9F, 0xB8, 0x19, 0xD8, 0x21, 0x6F, 0x66, 0xE3, 0xF6, 0x0B, 0x74, 0xB2,
|
| + 0x28, 0x38, 0xDC, 0xA7, 0x8A, 0x58, 0x0D, 0x56, 0x47, 0x3E, 0xD0, 0x5B,
|
| + 0x5C, 0x93, 0x4E, 0xB3, 0x89, 0x87, 0x64};
|
| +
|
| +const unsigned char kCommonRecipientPrivateKey[] = {
|
| + 0x30, 0x81, 0xB0, 0x30, 0x1B, 0x06, 0x0A, 0x2A, 0x86, 0x48, 0x86, 0xF7,
|
| + 0x0D, 0x01, 0x0C, 0x01, 0x03, 0x30, 0x0D, 0x04, 0x08, 0xF3, 0xD6, 0x39,
|
| + 0xB0, 0xBF, 0xC2, 0xF7, 0xEF, 0x02, 0x01, 0x01, 0x04, 0x81, 0x90, 0x0C,
|
| + 0xBC, 0xC9, 0x5F, 0x95, 0x6C, 0x4C, 0x6A, 0x57, 0xC4, 0x04, 0x47, 0x8F,
|
| + 0x59, 0xFD, 0x35, 0x97, 0x3B, 0xF3, 0x66, 0xA7, 0xEB, 0x8D, 0x44, 0x1E,
|
| + 0xCB, 0x4D, 0xFC, 0xD8, 0x8A, 0x38, 0xCA, 0xA8, 0xD7, 0x93, 0x45, 0x61,
|
| + 0xCC, 0xC7, 0xD8, 0x24, 0x16, 0xBF, 0xFC, 0xF4, 0x20, 0x54, 0x53, 0xB1,
|
| + 0x20, 0x3E, 0x19, 0x62, 0x08, 0xBE, 0x3D, 0x5B, 0x56, 0x60, 0x2D, 0x0C,
|
| + 0xDB, 0x29, 0x4A, 0x0B, 0xC8, 0xE0, 0x58, 0xAA, 0xE0, 0xAE, 0xE5, 0x91,
|
| + 0xEA, 0x6E, 0x40, 0x39, 0xD7, 0x3A, 0x1D, 0x75, 0x1E, 0xFF, 0xE3, 0xA7,
|
| + 0x64, 0x53, 0x50, 0x4A, 0xC1, 0xE9, 0x31, 0x9F, 0x0E, 0xB5, 0x12, 0x3E,
|
| + 0x57, 0xD7, 0x96, 0x5D, 0x69, 0xD5, 0x5A, 0xF8, 0x7C, 0x21, 0xF2, 0x43,
|
| + 0x2C, 0x34, 0x11, 0x8C, 0xB4, 0x37, 0x85, 0xB8, 0xDF, 0xFB, 0x5E, 0x68,
|
| + 0xD5, 0xAA, 0xBC, 0x29, 0x2B, 0xE1, 0x8C, 0xA2, 0x76, 0xAA, 0x59, 0x2D,
|
| + 0x8D, 0xB9, 0xC0, 0x11, 0xC8, 0xA9, 0x01, 0xD6, 0xB3, 0xBD, 0xEE};
|
| +
|
| +const unsigned char kCommonAuthSecret[] = {0x25, 0xF2, 0xC2, 0xB8, 0x19, 0xD8,
|
| + 0xFD, 0x35, 0x97, 0xDF, 0xFB, 0x5E,
|
| + 0xF6, 0x0B, 0xD7, 0xA4};
|
| +static_assert(arraysize(kCommonAuthSecret) == 16,
|
| + "Auth secrets must be 16 bytes in size.");
|
|
|
| // Test vectors containing reference input for draft-ietf-webpush-encryption-03
|
| // that was created using an separate JavaScript implementation of the draft.
|
| struct TestVector {
|
| const char* const input;
|
| - const char* const ecdh_shared_secret;
|
| - const char* const salt;
|
| + const unsigned char ecdh_shared_secret[kEcdhSharedSecretSize];
|
| + const unsigned char auth_secret[kAuthSecretSize];
|
| + const unsigned char salt[kSaltSize];
|
| size_t record_size;
|
| const char* const output;
|
| };
|
|
|
| const TestVector kEncryptionTestVectorsDraft03[] = {
|
| // Simple message.
|
| - {"Hello, world!", "AhA6n2oFYPWIh-cXwyv1m2C0JvmjHB4ZkXj8QylESXU",
|
| - "tsJYqAGvFDk6lDEv7daecw", 4096,
|
| - "FgWrrnZq79oI_N4ORkVLHx1jfVmjeiIk-xFX8PzVuA"},
|
| + {"Hello, world!",
|
| + {0x0B, 0x32, 0xE2, 0xD1, 0x6A, 0xBF, 0x4F, 0x2C, 0x49, 0xEA, 0xF7,
|
| + 0x5D, 0x71, 0x7D, 0x89, 0xA9, 0xA7, 0x5E, 0x21, 0xB2, 0xB5, 0x51,
|
| + 0xE6, 0x4C, 0x08, 0x68, 0xD3, 0x6F, 0x8F, 0x72, 0x7E, 0x14},
|
| + {0xD3, 0xF2, 0x78, 0xBD, 0x8D, 0xDD, 0x84, 0x99, 0x66, 0x08, 0xD7, 0x0F,
|
| + 0xBA, 0x9B, 0x60, 0xFC},
|
| + {0x15, 0x4A, 0xD7, 0x73, 0x92, 0xBD, 0x3B, 0xCF, 0x6F, 0x98, 0xDC, 0x9B,
|
| + 0x8B, 0x56, 0xFB, 0xBD},
|
| + 4096,
|
| + "T4SXCyj84drA6wRaBNLGDMzeyOEBWjsIEkS2ros6Aw"},
|
| // Empty message.
|
| - {"", "lMyvTong4VR053jfCpWmMDGW5dEDAqiTZUIU-inhTjU",
|
| - "wH3uvZqcN6oey9whiGpn1A", 4096, "MTI9zZ8CJTUzbZ4qNDoQZs0k"},
|
| - // Message with an invalid salt size.
|
| - {
|
| - "Hello, world!", "CcdxzkR6z1EY9vSrM7_IxYVxDxu46hV638EZQTPd7XI",
|
| - "aRr1fI1YSGVi5XU", 4096,
|
| - nullptr // expected to fail
|
| - }};
|
| + {"",
|
| + {0x3F, 0xD8, 0x95, 0x2C, 0xA2, 0x11, 0xBD, 0x7B, 0x57, 0xB2, 0x00,
|
| + 0xBD, 0x57, 0x68, 0x3F, 0xF0, 0x14, 0x57, 0x5F, 0xB1, 0x9F, 0x15,
|
| + 0x4F, 0x11, 0xF0, 0x4D, 0xA2, 0xE8, 0x4C, 0xEA, 0x74, 0x3B},
|
| + {0xB1, 0xE1, 0xC7, 0x32, 0x4C, 0xAA, 0x56, 0x32, 0x68, 0x20, 0x0F, 0x26,
|
| + 0x3F, 0x48, 0x4D, 0x99},
|
| + {0xE9, 0x39, 0x45, 0xBC, 0x96, 0x96, 0x88, 0x76, 0xFC, 0xA1, 0xAD, 0xE4,
|
| + 0x9D, 0x28, 0xF3, 0x73},
|
| + 4096,
|
| + "8s-Tzq8Cn_eobL6uEcNDXL7K"}};
|
|
|
| const TestVector kDecryptionTestVectorsDraft03[] = {
|
| // Simple message.
|
| - {"ceiEu_YpmqLoakD4smdzvy2XKRQrJ9vBzB2aqYEfzw",
|
| - "47ZytAw9qHlm-Q8g-7rH81rUPzaCgGcoFvlS1qxQtQk", "EuR7EVetcaWpndXd_dKeyA",
|
| - 4096, "Hello, world!"},
|
| + {"lsemWwzlFoJzoidHCnVuxRiJpotTcYokJHKzmQ2FsA",
|
| + {0x4D, 0x3A, 0x6C, 0xBA, 0xD8, 0x1D, 0x8E, 0x68, 0x8B, 0xE6, 0x76,
|
| + 0xA7, 0xFF, 0x60, 0xC7, 0xFE, 0x77, 0xE2, 0x6D, 0x37, 0xF6, 0x12,
|
| + 0x44, 0xE2, 0x25, 0xFE, 0xE1, 0xD8, 0xCF, 0x8A, 0xA8, 0x33},
|
| + {0x62, 0x36, 0xAC, 0xCA, 0x74, 0xD4, 0x49, 0x49, 0x6B, 0x27, 0xB4, 0xF7,
|
| + 0xC1, 0xE5, 0x30, 0x9A},
|
| + {0x1C, 0xA7, 0xFD, 0x98, 0x1A, 0xE4, 0xA7, 0x92, 0xE1, 0xB6, 0xA1, 0xE3,
|
| + 0x41, 0x63, 0x87, 0x76},
|
| + 4096,
|
| + "Hello, world!"},
|
| // Simple message with 16 bytes of padding.
|
| - {"WSf6fz1O0aIJyaPTCnvk83OqIQxsRKeFOvblPLsPpFB_1AV9ROk09TE1cGrB6zQ",
|
| - "MYSsNybwrTzRIzQYUq_yFPc6ugcTrJdEZJDM4NswvUg", "8sEAMQYnufo2UkKl80cUGQ",
|
| - 4096, "Hello, world!"},
|
| + {"VQB6Ds-q9xRqyM1tj_gksSgc78vCWEhphZ-NF1E7_yMfPuRRZlC_Xt9_2NsX3SU",
|
| + {0x8B, 0x38, 0x8E, 0x22, 0xD5, 0xC4, 0xFD, 0x65, 0x8A, 0xBB, 0xD9,
|
| + 0x58, 0xBD, 0xF5, 0xFF, 0x79, 0xCF, 0x9D, 0xBD, 0x87, 0x16, 0x7E,
|
| + 0x93, 0x84, 0x20, 0x8E, 0x8D, 0x49, 0x41, 0x7D, 0x8E, 0x8F},
|
| + {0x3E, 0x65, 0xC7, 0x1F, 0x75, 0x7A, 0x43, 0xC4, 0x78, 0x6C, 0x64, 0x99,
|
| + 0x49, 0xA0, 0xC4, 0xB2},
|
| + {0x43, 0x4D, 0x30, 0x8E, 0xE4, 0x76, 0xB5, 0xD0, 0x87, 0xFC, 0x04, 0xD1,
|
| + 0x2E, 0x35, 0x75, 0x63},
|
| + 4096,
|
| + "Hello, world!"},
|
| // Empty message.
|
| - {"Ur3vHedGDO5IPYDvbhHYjbjG", "S3-Ki_-XtzR66gUp_zR75CC5JXO62pyr5fWfneTYwFE",
|
| - "4RM6s19jJHdmqiVEJDp9jg", 4096, ""},
|
| - // Message with an invalid salt size.
|
| - {
|
| - "iGrOpmJC5XTTf7wtgdhZ_qT",
|
| - "wW3Iy5ma803lLd-ysPdHUe2NB3HqXbY0XhCCdG5Y1Gw", "N7oMH_xohAhMhOY", 4096,
|
| - nullptr // expected to fail
|
| - },
|
| + {"xU8a499UHB_-YSV4VOm-JZnT",
|
| + {0x68, 0x72, 0x3D, 0x13, 0xE7, 0x50, 0xFA, 0x3E, 0xA0, 0x59, 0x33,
|
| + 0xF1, 0x73, 0xA8, 0xE8, 0xCD, 0x8D, 0xD4, 0x3C, 0xDC, 0xDE, 0x06,
|
| + 0x35, 0x5F, 0x51, 0xBB, 0xB2, 0x57, 0x97, 0x72, 0x9D, 0xFB},
|
| + {0x84, 0xB2, 0x2A, 0xE7, 0xC6, 0xC0, 0xCE, 0x5F, 0xAD, 0x37, 0x06, 0x7F,
|
| + 0xD1, 0xFD, 0x10, 0x87},
|
| + {0x9B, 0xC5, 0x8D, 0x5F, 0xD6, 0xD2, 0xA6, 0xBD, 0xAF, 0x4B, 0xD9, 0x60,
|
| + 0xC6, 0xB4, 0x50, 0x0F},
|
| + 4096,
|
| + ""},
|
| // Message with an invalid record size.
|
| - {
|
| - "iGrOpmJC5XTTf7wtgdhZ_qT",
|
| - "kR5BMfqMKOD1yrLKE2giObXHI7merrMtnoO2oqneqXA", "SQeJSPrqHvTdSfAMF8bBzQ",
|
| - 8,
|
| - nullptr // expected to fail
|
| - },
|
| + {"gfB-_edj7qEVokyVHpkDJN6FVKHnlWs1RCDw5bmrwQ",
|
| + {0x5F, 0xE1, 0x7C, 0x4B, 0xFF, 0x04, 0xBF, 0x2C, 0x70, 0x67, 0xFA,
|
| + 0xF8, 0xB0, 0x07, 0x4F, 0xF6, 0x3C, 0x03, 0x6F, 0xBE, 0xA1, 0x1F,
|
| + 0x4B, 0x99, 0x25, 0x4F, 0xB9, 0x5F, 0xC4, 0x78, 0x76, 0xDE},
|
| + {0x59, 0xAB, 0x45, 0xFC, 0x6A, 0xF5, 0xB3, 0xE0, 0xF5, 0x40, 0xD7, 0x98,
|
| + 0x0F, 0xF0, 0xA4, 0xCB},
|
| + {0xDB, 0xA0, 0xF2, 0x91, 0x8D, 0x50, 0x42, 0xE0, 0x17, 0x68, 0x5B, 0x9B,
|
| + 0xF2, 0xA2, 0xC3, 0xF9},
|
| + 7,
|
| + nullptr},
|
| + // Message with four bytes of invalid, non-zero padding.
|
| + {"2FJmrF95yVU8Q8cYQy9OoOwCb59ZoRlxazPE0T-MNOSMbr0",
|
| + {0x6B, 0x82, 0x92, 0xD3, 0x71, 0x9A, 0x97, 0x76, 0x45, 0x11, 0x99,
|
| + 0x6D, 0xBF, 0x56, 0xCC, 0x81, 0x98, 0x56, 0x80, 0xF5, 0x78, 0x36,
|
| + 0xD6, 0x43, 0x95, 0x68, 0xDB, 0x0F, 0x23, 0x39, 0xF3, 0x6E},
|
| + {0x02, 0x16, 0xDC, 0xC3, 0xDE, 0x2C, 0xB5, 0x08, 0x89, 0xDB, 0xD8, 0x18,
|
| + 0x68, 0x83, 0x1C, 0xDB},
|
| + {0xB7, 0x85, 0x5D, 0x8E, 0x84, 0xC3, 0x2D, 0x61, 0x9B, 0x78, 0x3B, 0x60,
|
| + 0x0E, 0x70, 0x84, 0xF3},
|
| + 4096,
|
| + nullptr},
|
| // Message with multiple (2) records.
|
| - {
|
| - "RqQVHRXlfYjzW9xhzh3V_KijLKjZiKzGXosqN_"
|
| - "IaMzi0zI0tXXhC1urtrk3iWRoqttNXpkD2r"
|
| - "UCgLy8A1FnTjw",
|
| - "W3W4gx7sqcfmBnvNNdO9d4MBCC1bvJkvsNjZOGD-CCg", "xG0TPGi9aIcxjpXKmaYBBQ",
|
| - 7,
|
| - nullptr // expected to fail
|
| - }};
|
| + {"reI6sW6y67FI8Kxk-x9GNwiu77His_f5GioDBiKS7IzjDQ",
|
| + {0xC6, 0x16, 0x6F, 0xAF, 0xE1, 0xB6, 0x8F, 0x2B, 0x0F, 0x67, 0x5A,
|
| + 0xC7, 0xAC, 0x7E, 0xF6, 0x7C, 0x33, 0xA2, 0xA1, 0x11, 0xB0, 0xB0,
|
| + 0xAB, 0xAC, 0x37, 0x61, 0xF4, 0xCB, 0x98, 0xFF, 0x00, 0x51},
|
| + {0xAE, 0xDA, 0x86, 0xDF, 0x6B, 0x03, 0x88, 0xDE, 0x90, 0xBB, 0xB7, 0xA0,
|
| + 0x78, 0x91, 0x3A, 0x36},
|
| + {0x4C, 0x4E, 0x2A, 0x8D, 0x88, 0x82, 0xCF, 0xC2, 0xF9, 0x8A, 0xFD, 0x31,
|
| + 0xF8, 0xD1, 0xF6, 0xB5},
|
| + 8,
|
| + nullptr}};
|
|
|
| } // namespace
|
|
|
| @@ -103,26 +191,36 @@ class GCMMessageCryptographerTest : public ::testing::Test {
|
| cryptographer_ = base::MakeUnique<GCMMessageCryptographer>(
|
| GCMMessageCryptographer::Version::DRAFT_03);
|
|
|
| - ASSERT_TRUE(base::Base64UrlDecode(
|
| - kCommonRecipientPublicKey, base::Base64UrlDecodePolicy::IGNORE_PADDING,
|
| - &recipient_public_key_));
|
| - ASSERT_TRUE(base::Base64UrlDecode(
|
| - kCommonSenderPublicKey, base::Base64UrlDecodePolicy::IGNORE_PADDING,
|
| - &sender_public_key_));
|
| + recipient_public_key_.assign(
|
| + kCommonRecipientPublicKey,
|
| + kCommonRecipientPublicKey + arraysize(kCommonRecipientPublicKey));
|
| + sender_public_key_.assign(
|
| + kCommonSenderPublicKey,
|
| + kCommonSenderPublicKey + arraysize(kCommonSenderPublicKey));
|
|
|
| - std::unique_ptr<crypto::SymmetricKey> random_key(
|
| - crypto::SymmetricKey::GenerateRandomKey(crypto::SymmetricKey::AES,
|
| - kKeySizeBits));
|
| + std::string recipient_private_key(
|
| + kCommonRecipientPrivateKey,
|
| + kCommonRecipientPrivateKey + arraysize(kCommonRecipientPrivateKey));
|
|
|
| - ASSERT_TRUE(random_key->GetRawKey(&ecdh_shared_secret_));
|
| + // TODO(peter): Remove the dependency on the x509 keying material when
|
| + // crbug.com/618025 has been resolved.
|
| + std::string recipient_public_key_x509(
|
| + kCommonRecipientPublicKeyX509,
|
| + kCommonRecipientPublicKeyX509 +
|
| + arraysize(kCommonRecipientPublicKeyX509));
|
| +
|
| + ASSERT_TRUE(ComputeSharedP256Secret(
|
| + recipient_private_key, recipient_public_key_x509, sender_public_key_,
|
| + &ecdh_shared_secret_));
|
| +
|
| + auth_secret_.assign(kCommonAuthSecret,
|
| + kCommonAuthSecret + arraysize(kCommonAuthSecret));
|
| }
|
|
|
| protected:
|
| // Generates a cryptographically secure random salt of 16-octets in size, the
|
| // required length as expected by the HKDF.
|
| std::string GenerateRandomSalt() {
|
| - const size_t kSaltSize = 16;
|
| -
|
| std::string salt;
|
|
|
| crypto::RandBytes(base::WriteInto(&salt, kSaltSize + 1), kSaltSize);
|
| @@ -133,9 +231,12 @@ class GCMMessageCryptographerTest : public ::testing::Test {
|
| std::string recipient_public_key_;
|
| std::string sender_public_key_;
|
|
|
| - // Shared secret to use in transformations. Unrelated to the keys above.
|
| + // Shared secret to use in transformations. Associated with the keys above.
|
| std::string ecdh_shared_secret_;
|
|
|
| + // Authentication secret to use in tests where no specific value is expected.
|
| + std::string auth_secret_;
|
| +
|
| // The GCMMessageCryptographer instance to use for the tests.
|
| std::unique_ptr<GCMMessageCryptographer> cryptographer_;
|
| };
|
| @@ -148,14 +249,14 @@ TEST_F(GCMMessageCryptographerTest, RoundTrip) {
|
| std::string ciphertext, plaintext;
|
| ASSERT_TRUE(cryptographer_->Encrypt(
|
| recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
|
| - kCommonAuthSecret, salt, kExamplePlaintext, &record_size, &ciphertext));
|
| + auth_secret_, salt, kExamplePlaintext, &record_size, &ciphertext));
|
|
|
| EXPECT_GT(record_size, ciphertext.size() - 16);
|
| EXPECT_GT(ciphertext.size(), 0u);
|
|
|
| - ASSERT_TRUE(cryptographer_->Decrypt(
|
| - recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
|
| - kCommonAuthSecret, salt, ciphertext, record_size, &plaintext));
|
| + ASSERT_TRUE(cryptographer_->Decrypt(recipient_public_key_, sender_public_key_,
|
| + ecdh_shared_secret_, auth_secret_, salt,
|
| + ciphertext, record_size, &plaintext));
|
|
|
| EXPECT_EQ(kExamplePlaintext, plaintext);
|
| }
|
| @@ -167,16 +268,16 @@ TEST_F(GCMMessageCryptographerTest, RoundTripEmptyMessage) {
|
| size_t record_size = 0;
|
|
|
| std::string ciphertext, plaintext;
|
| - ASSERT_TRUE(cryptographer_->Encrypt(
|
| - recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
|
| - kCommonAuthSecret, salt, message, &record_size, &ciphertext));
|
| + ASSERT_TRUE(cryptographer_->Encrypt(recipient_public_key_, sender_public_key_,
|
| + ecdh_shared_secret_, auth_secret_, salt,
|
| + message, &record_size, &ciphertext));
|
|
|
| EXPECT_GT(record_size, ciphertext.size() - 16);
|
| EXPECT_GT(ciphertext.size(), 0u);
|
|
|
| - ASSERT_TRUE(cryptographer_->Decrypt(
|
| - recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
|
| - kCommonAuthSecret, salt, ciphertext, record_size, &plaintext));
|
| + ASSERT_TRUE(cryptographer_->Decrypt(recipient_public_key_, sender_public_key_,
|
| + ecdh_shared_secret_, auth_secret_, salt,
|
| + ciphertext, record_size, &plaintext));
|
|
|
| EXPECT_EQ(message, plaintext);
|
| }
|
| @@ -189,21 +290,21 @@ TEST_F(GCMMessageCryptographerTest, InvalidRecordSize) {
|
| std::string ciphertext, plaintext;
|
| ASSERT_TRUE(cryptographer_->Encrypt(
|
| recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
|
| - kCommonAuthSecret, salt, kExamplePlaintext, &record_size, &ciphertext));
|
| + auth_secret_, salt, kExamplePlaintext, &record_size, &ciphertext));
|
|
|
| EXPECT_GT(record_size, ciphertext.size() - 16);
|
|
|
| EXPECT_FALSE(cryptographer_->Decrypt(
|
| recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
|
| - kCommonAuthSecret, salt, ciphertext, 0 /* record_size */, &plaintext));
|
| + auth_secret_, salt, ciphertext, 0 /* record_size */, &plaintext));
|
|
|
| EXPECT_FALSE(cryptographer_->Decrypt(
|
| recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
|
| - kCommonAuthSecret, salt, ciphertext, ciphertext.size() - 17, &plaintext));
|
| + auth_secret_, salt, ciphertext, ciphertext.size() - 17, &plaintext));
|
|
|
| EXPECT_TRUE(cryptographer_->Decrypt(
|
| recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
|
| - kCommonAuthSecret, salt, ciphertext, ciphertext.size() - 16, &plaintext));
|
| + auth_secret_, salt, ciphertext, ciphertext.size() - 16, &plaintext));
|
| }
|
|
|
| TEST_F(GCMMessageCryptographerTest, InvalidRecordPadding) {
|
| @@ -213,7 +314,7 @@ TEST_F(GCMMessageCryptographerTest, InvalidRecordPadding) {
|
|
|
| const std::string prk =
|
| cryptographer_->encryption_scheme_->DerivePseudoRandomKey(
|
| - ecdh_shared_secret_, kCommonAuthSecret);
|
| + ecdh_shared_secret_, auth_secret_);
|
| const std::string content_encryption_key =
|
| cryptographer_->DeriveContentEncryptionKey(recipient_public_key_,
|
| sender_public_key_, prk, salt);
|
| @@ -228,9 +329,9 @@ TEST_F(GCMMessageCryptographerTest, InvalidRecordPadding) {
|
| GCMMessageCryptographer::Direction::ENCRYPT, message,
|
| content_encryption_key, nonce, &ciphertext));
|
|
|
| - ASSERT_TRUE(cryptographer_->Decrypt(
|
| - recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
|
| - kCommonAuthSecret, salt, ciphertext, record_size, &plaintext));
|
| + ASSERT_TRUE(cryptographer_->Decrypt(recipient_public_key_, sender_public_key_,
|
| + ecdh_shared_secret_, auth_secret_, salt,
|
| + ciphertext, record_size, &plaintext));
|
|
|
| // Note that GCMMessageCryptographer::Decrypt removes the padding.
|
| EXPECT_EQ(kExamplePlaintext, plaintext);
|
| @@ -245,7 +346,7 @@ TEST_F(GCMMessageCryptographerTest, InvalidRecordPadding) {
|
|
|
| ASSERT_FALSE(cryptographer_->Decrypt(
|
| recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
|
| - kCommonAuthSecret, salt, ciphertext, record_size, &plaintext));
|
| + auth_secret_, salt, ciphertext, record_size, &plaintext));
|
|
|
| // Do the same but changing the second octet indicating padding size, leaving
|
| // the first octet at zero.
|
| @@ -258,7 +359,7 @@ TEST_F(GCMMessageCryptographerTest, InvalidRecordPadding) {
|
|
|
| ASSERT_FALSE(cryptographer_->Decrypt(
|
| recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
|
| - kCommonAuthSecret, salt, ciphertext, record_size, &plaintext));
|
| + auth_secret_, salt, ciphertext, record_size, &plaintext));
|
|
|
| // Run the same steps again, but say that there are more padding octets than
|
| // the length of the message.
|
| @@ -271,107 +372,121 @@ TEST_F(GCMMessageCryptographerTest, InvalidRecordPadding) {
|
|
|
| ASSERT_FALSE(cryptographer_->Decrypt(
|
| recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
|
| - kCommonAuthSecret, salt, ciphertext, record_size, &plaintext));
|
| + auth_secret_, salt, ciphertext, record_size, &plaintext));
|
| }
|
|
|
| TEST_F(GCMMessageCryptographerTest, AuthSecretAffectsPRK) {
|
| + std::string first_auth_secret, second_auth_secret;
|
| +
|
| + crypto::RandBytes(base::WriteInto(&first_auth_secret, kAuthSecretSize + 1),
|
| + kAuthSecretSize);
|
| + crypto::RandBytes(base::WriteInto(&second_auth_secret, kAuthSecretSize + 1),
|
| + kAuthSecretSize);
|
| +
|
| ASSERT_NE(cryptographer_->encryption_scheme_->DerivePseudoRandomKey(
|
| - ecdh_shared_secret_, "Hello"),
|
| + ecdh_shared_secret_, first_auth_secret),
|
| cryptographer_->encryption_scheme_->DerivePseudoRandomKey(
|
| - ecdh_shared_secret_, "World"));
|
| + ecdh_shared_secret_, second_auth_secret));
|
|
|
| std::string salt = GenerateRandomSalt();
|
|
|
| // Verify that the IKM actually gets used by the transformations.
|
| - size_t hello_record_size, world_record_size;
|
| - std::string hello_ciphertext, world_ciphertext;
|
| + size_t first_record_size, second_record_size;
|
| + std::string first_ciphertext, second_ciphertext;
|
|
|
| - ASSERT_TRUE(cryptographer_->Encrypt(
|
| - recipient_public_key_, sender_public_key_, ecdh_shared_secret_, "Hello",
|
| - salt, kExamplePlaintext, &hello_record_size, &hello_ciphertext));
|
| + ASSERT_TRUE(cryptographer_->Encrypt(recipient_public_key_, sender_public_key_,
|
| + ecdh_shared_secret_, first_auth_secret,
|
| + salt, kExamplePlaintext,
|
| + &first_record_size, &first_ciphertext));
|
|
|
| - ASSERT_TRUE(cryptographer_->Encrypt(
|
| - recipient_public_key_, sender_public_key_, ecdh_shared_secret_, "World",
|
| - salt, kExamplePlaintext, &world_record_size, &world_ciphertext));
|
| + ASSERT_TRUE(cryptographer_->Encrypt(recipient_public_key_, sender_public_key_,
|
| + ecdh_shared_secret_, second_auth_secret,
|
| + salt, kExamplePlaintext,
|
| + &second_record_size, &second_ciphertext));
|
|
|
| // If the ciphertexts differ despite the same key and salt, it got used.
|
| - ASSERT_NE(hello_ciphertext, world_ciphertext);
|
| - EXPECT_EQ(hello_record_size, world_record_size);
|
| + ASSERT_NE(first_ciphertext, second_ciphertext);
|
| + EXPECT_EQ(first_record_size, second_record_size);
|
|
|
| // Verify that the different ciphertexts can also be translated back to the
|
| // plaintext content. This will fail if the auth secret isn't considered.
|
| - std::string hello_plaintext, world_plaintext;
|
| + std::string first_plaintext, second_plaintext;
|
|
|
| - ASSERT_TRUE(cryptographer_->Decrypt(
|
| - recipient_public_key_, sender_public_key_, ecdh_shared_secret_, "Hello",
|
| - salt, hello_ciphertext, hello_record_size, &hello_plaintext));
|
| + ASSERT_TRUE(cryptographer_->Decrypt(recipient_public_key_, sender_public_key_,
|
| + ecdh_shared_secret_, first_auth_secret,
|
| + salt, first_ciphertext, first_record_size,
|
| + &first_plaintext));
|
|
|
| - ASSERT_TRUE(cryptographer_->Decrypt(
|
| - recipient_public_key_, sender_public_key_, ecdh_shared_secret_, "World",
|
| - salt, world_ciphertext, world_record_size, &world_plaintext));
|
| + ASSERT_TRUE(cryptographer_->Decrypt(recipient_public_key_, sender_public_key_,
|
| + ecdh_shared_secret_, second_auth_secret,
|
| + salt, second_ciphertext,
|
| + second_record_size, &second_plaintext));
|
|
|
| - EXPECT_EQ(kExamplePlaintext, hello_plaintext);
|
| - EXPECT_EQ(kExamplePlaintext, world_plaintext);
|
| + EXPECT_EQ(kExamplePlaintext, first_plaintext);
|
| + EXPECT_EQ(kExamplePlaintext, second_plaintext);
|
| }
|
|
|
| class GCMMessageCryptographerTestVectorTest
|
| : public GCMMessageCryptographerTest {};
|
|
|
| TEST_F(GCMMessageCryptographerTestVectorTest, EncryptionVectorsDraft03) {
|
| - std::string ecdh_shared_secret, salt, output, ciphertext;
|
| + std::string ecdh_shared_secret, auth_secret, salt, ciphertext, output;
|
| size_t record_size = 0;
|
|
|
| for (size_t i = 0; i < arraysize(kEncryptionTestVectorsDraft03); ++i) {
|
| SCOPED_TRACE(i);
|
|
|
| - ASSERT_TRUE(base::Base64UrlDecode(
|
| + ecdh_shared_secret.assign(
|
| kEncryptionTestVectorsDraft03[i].ecdh_shared_secret,
|
| - base::Base64UrlDecodePolicy::IGNORE_PADDING, &ecdh_shared_secret));
|
| - ASSERT_TRUE(base::Base64UrlDecode(
|
| - kEncryptionTestVectorsDraft03[i].salt,
|
| - base::Base64UrlDecodePolicy::IGNORE_PADDING, &salt));
|
| + kEncryptionTestVectorsDraft03[i].ecdh_shared_secret +
|
| + kEcdhSharedSecretSize);
|
|
|
| - const bool has_output = kEncryptionTestVectorsDraft03[i].output;
|
| - const bool result = cryptographer_->Encrypt(
|
| - recipient_public_key_, sender_public_key_, ecdh_shared_secret,
|
| - kCommonAuthSecret, salt, kEncryptionTestVectorsDraft03[i].input,
|
| - &record_size, &ciphertext);
|
| + auth_secret.assign(
|
| + kEncryptionTestVectorsDraft03[i].auth_secret,
|
| + kEncryptionTestVectorsDraft03[i].auth_secret + kAuthSecretSize);
|
|
|
| - if (!has_output) {
|
| - EXPECT_FALSE(result);
|
| - continue;
|
| - }
|
| + salt.assign(kEncryptionTestVectorsDraft03[i].salt,
|
| + kEncryptionTestVectorsDraft03[i].salt + kSaltSize);
|
|
|
| - EXPECT_TRUE(result);
|
| - ASSERT_TRUE(base::Base64UrlDecode(
|
| - kEncryptionTestVectorsDraft03[i].output,
|
| - base::Base64UrlDecodePolicy::IGNORE_PADDING, &output));
|
| + ASSERT_TRUE(cryptographer_->Encrypt(
|
| + recipient_public_key_, sender_public_key_, ecdh_shared_secret,
|
| + auth_secret, salt, kEncryptionTestVectorsDraft03[i].input, &record_size,
|
| + &ciphertext));
|
| +
|
| + base::Base64UrlEncode(ciphertext, base::Base64UrlEncodePolicy::OMIT_PADDING,
|
| + &output);
|
|
|
| EXPECT_EQ(kEncryptionTestVectorsDraft03[i].record_size, record_size);
|
| - EXPECT_EQ(output, ciphertext);
|
| + EXPECT_EQ(kEncryptionTestVectorsDraft03[i].output, output);
|
| }
|
| }
|
|
|
| TEST_F(GCMMessageCryptographerTestVectorTest, DecryptionVectorsDraft03) {
|
| - std::string input, ecdh_shared_secret, salt, plaintext;
|
| + std::string input, ecdh_shared_secret, auth_secret, salt, plaintext;
|
| for (size_t i = 0; i < arraysize(kDecryptionTestVectorsDraft03); ++i) {
|
| SCOPED_TRACE(i);
|
|
|
| ASSERT_TRUE(base::Base64UrlDecode(
|
| kDecryptionTestVectorsDraft03[i].input,
|
| base::Base64UrlDecodePolicy::IGNORE_PADDING, &input));
|
| - ASSERT_TRUE(base::Base64UrlDecode(
|
| +
|
| + ecdh_shared_secret.assign(
|
| kDecryptionTestVectorsDraft03[i].ecdh_shared_secret,
|
| - base::Base64UrlDecodePolicy::IGNORE_PADDING, &ecdh_shared_secret));
|
| - ASSERT_TRUE(base::Base64UrlDecode(
|
| - kDecryptionTestVectorsDraft03[i].salt,
|
| - base::Base64UrlDecodePolicy::IGNORE_PADDING, &salt));
|
| + kDecryptionTestVectorsDraft03[i].ecdh_shared_secret +
|
| + kEcdhSharedSecretSize);
|
| +
|
| + auth_secret.assign(
|
| + kDecryptionTestVectorsDraft03[i].auth_secret,
|
| + kDecryptionTestVectorsDraft03[i].auth_secret + kAuthSecretSize);
|
| +
|
| + salt.assign(kDecryptionTestVectorsDraft03[i].salt,
|
| + kDecryptionTestVectorsDraft03[i].salt + kSaltSize);
|
|
|
| const bool has_output = kDecryptionTestVectorsDraft03[i].output;
|
| const bool result = cryptographer_->Decrypt(
|
| recipient_public_key_, sender_public_key_, ecdh_shared_secret,
|
| - kCommonAuthSecret, salt, input,
|
| - kDecryptionTestVectorsDraft03[i].record_size, &plaintext);
|
| + auth_secret, salt, input, kDecryptionTestVectorsDraft03[i].record_size,
|
| + &plaintext);
|
|
|
| if (!has_output) {
|
| EXPECT_FALSE(result);
|
|
|