gcm_message_cryptographer_unittest.cc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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
  3. // found in the LICENSE file.
  4. #include "components/gcm_driver/crypto/gcm_message_cryptographer.h"
  5. #include <memory>
  6. #include "base/base64url.h"
  7. #include "base/big_endian.h"
  8. #include "base/logging.h"
  9. #include "base/strings/string_piece.h"
  10. #include "base/strings/string_util.h"
  11. #include "components/gcm_driver/crypto/message_payload_parser.h"
  12. #include "components/gcm_driver/crypto/p256_key_util.h"
  13. #include "crypto/ec_private_key.h"
  14. #include "crypto/random.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. namespace gcm {
  17. namespace {
  18. // Example plaintext data to use in the tests.
  19. const char kExamplePlaintext[] = "Example plaintext";
  20. // Expected sizes of the different input given to the cryptographer.
  21. constexpr size_t kEcdhSharedSecretSize = 32;
  22. constexpr size_t kAuthSecretSize = 16;
  23. constexpr size_t kSaltSize = 16;
  24. // Keying material for both parties as P-256 EC points. Used to make sure that
  25. // the test vectors are reproducible.
  26. const unsigned char kCommonSenderPublicKey[] = {
  27. 0x04, 0x05, 0x3C, 0xA1, 0xB9, 0xA5, 0xAB, 0xB8, 0x2D, 0x88, 0x48,
  28. 0x82, 0xC9, 0x49, 0x19, 0x91, 0xD5, 0xFD, 0xD1, 0x92, 0xDB, 0xA7,
  29. 0x7E, 0x70, 0x48, 0x37, 0x41, 0xCD, 0x90, 0x05, 0x80, 0xDF, 0x65,
  30. 0x9A, 0xA1, 0x1A, 0x04, 0xF1, 0x98, 0x25, 0xF2, 0xC2, 0x13, 0x5D,
  31. 0xD9, 0x72, 0x35, 0x75, 0x24, 0xF9, 0xFF, 0x25, 0xD1, 0xBC, 0x84,
  32. 0x46, 0x4E, 0x88, 0x08, 0x55, 0x70, 0x9F, 0xA7, 0x07, 0xD9};
  33. static_assert(std::size(kCommonSenderPublicKey) == 65,
  34. "Raw P-256 public keys must be 65 bytes in size.");
  35. const unsigned char kCommonRecipientPublicKey[] = {
  36. 0x04, 0x35, 0x02, 0x67, 0xB9, 0x10, 0x8F, 0x9B, 0xF1, 0x85, 0xF5,
  37. 0x1B, 0xD7, 0xA4, 0xEF, 0xBD, 0x28, 0xB3, 0x11, 0x40, 0xBA, 0xD0,
  38. 0xEE, 0xB2, 0x97, 0xDA, 0x6A, 0x93, 0x2D, 0x26, 0x45, 0xBD, 0xB2,
  39. 0x9A, 0x9F, 0xB8, 0x19, 0xD8, 0x21, 0x6F, 0x66, 0xE3, 0xF6, 0x0B,
  40. 0x74, 0xB2, 0x28, 0x38, 0xDC, 0xA7, 0x8A, 0x58, 0x0D, 0x56, 0x47,
  41. 0x3E, 0xD0, 0x5B, 0x5C, 0x93, 0x4E, 0xB3, 0x89, 0x87, 0x64};
  42. static_assert(std::size(kCommonRecipientPublicKey) == 65,
  43. "Raw P-256 public keys must be 65 bytes in size.");
  44. const unsigned char kCommonRecipientPrivateKey[] = {
  45. 0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86,
  46. 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  47. 0x03, 0x01, 0x07, 0x04, 0x6D, 0x30, 0x6B, 0x02, 0x01, 0x01, 0x04, 0x20,
  48. 0x16, 0xCC, 0xB4, 0x37, 0xA3, 0x04, 0x0C, 0x28, 0xDE, 0x56, 0x77, 0x27,
  49. 0x0B, 0xD8, 0x1E, 0x82, 0xD7, 0x7F, 0x07, 0xA6, 0x43, 0x6E, 0x70, 0xDD,
  50. 0x9C, 0x3C, 0xF1, 0x2C, 0x93, 0xE3, 0x37, 0xD1, 0xA1, 0x44, 0x03, 0x42,
  51. 0x00, 0x04, 0x35, 0x02, 0x67, 0xB9, 0x10, 0x8F, 0x9B, 0xF1, 0x85, 0xF5,
  52. 0x1B, 0xD7, 0xA4, 0xEF, 0xBD, 0x28, 0xB3, 0x11, 0x40, 0xBA, 0xD0, 0xEE,
  53. 0xB2, 0x97, 0xDA, 0x6A, 0x93, 0x2D, 0x26, 0x45, 0xBD, 0xB2, 0x9A, 0x9F,
  54. 0xB8, 0x19, 0xD8, 0x21, 0x6F, 0x66, 0xE3, 0xF6, 0x0B, 0x74, 0xB2, 0x28,
  55. 0x38, 0xDC, 0xA7, 0x8A, 0x58, 0x0D, 0x56, 0x47, 0x3E, 0xD0, 0x5B, 0x5C,
  56. 0x93, 0x4E, 0xB3, 0x89, 0x87, 0x64};
  57. const unsigned char kCommonAuthSecret[] = {0x25, 0xF2, 0xC2, 0xB8, 0x19, 0xD8,
  58. 0xFD, 0x35, 0x97, 0xDF, 0xFB, 0x5E,
  59. 0xF6, 0x0B, 0xD7, 0xA4};
  60. static_assert(std::size(kCommonAuthSecret) == 16,
  61. "Auth secrets must be 16 bytes in size.");
  62. // Test vectors containing reference input for draft-ietf-webpush-encryption
  63. // that was created using an separate JavaScript implementation of the draft.
  64. struct TestVector {
  65. const char* const input;
  66. const unsigned char ecdh_shared_secret[kEcdhSharedSecretSize];
  67. const unsigned char auth_secret[kAuthSecretSize];
  68. const unsigned char salt[kSaltSize];
  69. size_t record_size;
  70. const char* const output;
  71. };
  72. const TestVector kEncryptionTestVectorsDraft03[] = {
  73. // Simple message.
  74. {"Hello, world!",
  75. {0x0B, 0x32, 0xE2, 0xD1, 0x6A, 0xBF, 0x4F, 0x2C, 0x49, 0xEA, 0xF7,
  76. 0x5D, 0x71, 0x7D, 0x89, 0xA9, 0xA7, 0x5E, 0x21, 0xB2, 0xB5, 0x51,
  77. 0xE6, 0x4C, 0x08, 0x68, 0xD3, 0x6F, 0x8F, 0x72, 0x7E, 0x14},
  78. {0xD3, 0xF2, 0x78, 0xBD, 0x8D, 0xDD, 0x84, 0x99, 0x66, 0x08, 0xD7, 0x0F,
  79. 0xBA, 0x9B, 0x60, 0xFC},
  80. {0x15, 0x4A, 0xD7, 0x73, 0x92, 0xBD, 0x3B, 0xCF, 0x6F, 0x98, 0xDC, 0x9B,
  81. 0x8B, 0x56, 0xFB, 0xBD},
  82. 4096,
  83. "T4SXCyj84drA6wRaBNLGDMzeyOEBWjsIEkS2ros6Aw"},
  84. // Empty message.
  85. {"",
  86. {0x3F, 0xD8, 0x95, 0x2C, 0xA2, 0x11, 0xBD, 0x7B, 0x57, 0xB2, 0x00,
  87. 0xBD, 0x57, 0x68, 0x3F, 0xF0, 0x14, 0x57, 0x5F, 0xB1, 0x9F, 0x15,
  88. 0x4F, 0x11, 0xF0, 0x4D, 0xA2, 0xE8, 0x4C, 0xEA, 0x74, 0x3B},
  89. {0xB1, 0xE1, 0xC7, 0x32, 0x4C, 0xAA, 0x56, 0x32, 0x68, 0x20, 0x0F, 0x26,
  90. 0x3F, 0x48, 0x4D, 0x99},
  91. {0xE9, 0x39, 0x45, 0xBC, 0x96, 0x96, 0x88, 0x76, 0xFC, 0xA1, 0xAD, 0xE4,
  92. 0x9D, 0x28, 0xF3, 0x73},
  93. 4096,
  94. "8s-Tzq8Cn_eobL6uEcNDXL7K"}};
  95. const TestVector kEncryptionTestVectorsDraft08[] = {
  96. // Simple message.
  97. {"Hello, world!",
  98. {0x0B, 0x32, 0xE2, 0xD1, 0x6A, 0xBF, 0x4F, 0x2C, 0x49, 0xEA, 0xF7,
  99. 0x5D, 0x71, 0x7D, 0x89, 0xA9, 0xA7, 0x5E, 0x21, 0xB2, 0xB5, 0x51,
  100. 0xE6, 0x4C, 0x08, 0x68, 0xD3, 0x6F, 0x8F, 0x72, 0x7E, 0x14},
  101. {0xD3, 0xF2, 0x78, 0xBD, 0x8D, 0xDD, 0x84, 0x99, 0x66, 0x08, 0xD7, 0x0F,
  102. 0xBA, 0x9B, 0x60, 0xFC},
  103. {0x15, 0x4A, 0xD7, 0x73, 0x92, 0xBD, 0x3B, 0xCF, 0x6F, 0x98, 0xDC, 0x9B,
  104. 0x8B, 0x56, 0xFB, 0xBD},
  105. 4096,
  106. "3biYN3Aa30D30bKJMdGlEyYPrz7Wg293NYc31rb6"},
  107. // Empty message.
  108. {"",
  109. {0x3F, 0xD8, 0x95, 0x2C, 0xA2, 0x11, 0xBD, 0x7B, 0x57, 0xB2, 0x00,
  110. 0xBD, 0x57, 0x68, 0x3F, 0xF0, 0x14, 0x57, 0x5F, 0xB1, 0x9F, 0x15,
  111. 0x4F, 0x11, 0xF0, 0x4D, 0xA2, 0xE8, 0x4C, 0xEA, 0x74, 0x3B},
  112. {0xB1, 0xE1, 0xC7, 0x32, 0x4C, 0xAA, 0x56, 0x32, 0x68, 0x20, 0x0F, 0x26,
  113. 0x3F, 0x48, 0x4D, 0x99},
  114. {0xE9, 0x39, 0x45, 0xBC, 0x96, 0x96, 0x88, 0x76, 0xFC, 0xA1, 0xAD, 0xE4,
  115. 0x9D, 0x28, 0xF3, 0x73},
  116. 4096,
  117. "5OXY345WYPyIvsF7hx4swuA"}};
  118. const TestVector kDecryptionTestVectorsDraft03[] = {
  119. // Simple message.
  120. {"lsemWwzlFoJzoidHCnVuxRiJpotTcYokJHKzmQ2FsA",
  121. {0x4D, 0x3A, 0x6C, 0xBA, 0xD8, 0x1D, 0x8E, 0x68, 0x8B, 0xE6, 0x76,
  122. 0xA7, 0xFF, 0x60, 0xC7, 0xFE, 0x77, 0xE2, 0x6D, 0x37, 0xF6, 0x12,
  123. 0x44, 0xE2, 0x25, 0xFE, 0xE1, 0xD8, 0xCF, 0x8A, 0xA8, 0x33},
  124. {0x62, 0x36, 0xAC, 0xCA, 0x74, 0xD4, 0x49, 0x49, 0x6B, 0x27, 0xB4, 0xF7,
  125. 0xC1, 0xE5, 0x30, 0x9A},
  126. {0x1C, 0xA7, 0xFD, 0x98, 0x1A, 0xE4, 0xA7, 0x92, 0xE1, 0xB6, 0xA1, 0xE3,
  127. 0x41, 0x63, 0x87, 0x76},
  128. 4096,
  129. "Hello, world!"},
  130. // Simple message with 16 bytes of padding.
  131. {"VQB6Ds-q9xRqyM1tj_gksSgc78vCWEhphZ-NF1E7_yMfPuRRZlC_Xt9_2NsX3SU",
  132. {0x8B, 0x38, 0x8E, 0x22, 0xD5, 0xC4, 0xFD, 0x65, 0x8A, 0xBB, 0xD9,
  133. 0x58, 0xBD, 0xF5, 0xFF, 0x79, 0xCF, 0x9D, 0xBD, 0x87, 0x16, 0x7E,
  134. 0x93, 0x84, 0x20, 0x8E, 0x8D, 0x49, 0x41, 0x7D, 0x8E, 0x8F},
  135. {0x3E, 0x65, 0xC7, 0x1F, 0x75, 0x7A, 0x43, 0xC4, 0x78, 0x6C, 0x64, 0x99,
  136. 0x49, 0xA0, 0xC4, 0xB2},
  137. {0x43, 0x4D, 0x30, 0x8E, 0xE4, 0x76, 0xB5, 0xD0, 0x87, 0xFC, 0x04, 0xD1,
  138. 0x2E, 0x35, 0x75, 0x63},
  139. 4096,
  140. "Hello, world!"},
  141. // Empty message.
  142. {"xU8a499UHB_-YSV4VOm-JZnT",
  143. {0x68, 0x72, 0x3D, 0x13, 0xE7, 0x50, 0xFA, 0x3E, 0xA0, 0x59, 0x33,
  144. 0xF1, 0x73, 0xA8, 0xE8, 0xCD, 0x8D, 0xD4, 0x3C, 0xDC, 0xDE, 0x06,
  145. 0x35, 0x5F, 0x51, 0xBB, 0xB2, 0x57, 0x97, 0x72, 0x9D, 0xFB},
  146. {0x84, 0xB2, 0x2A, 0xE7, 0xC6, 0xC0, 0xCE, 0x5F, 0xAD, 0x37, 0x06, 0x7F,
  147. 0xD1, 0xFD, 0x10, 0x87},
  148. {0x9B, 0xC5, 0x8D, 0x5F, 0xD6, 0xD2, 0xA6, 0xBD, 0xAF, 0x4B, 0xD9, 0x60,
  149. 0xC6, 0xB4, 0x50, 0x0F},
  150. 4096,
  151. ""},
  152. // Message with an invalid record size.
  153. {"gfB-_edj7qEVokyVHpkDJN6FVKHnlWs1RCDw5bmrwQ",
  154. {0x5F, 0xE1, 0x7C, 0x4B, 0xFF, 0x04, 0xBF, 0x2C, 0x70, 0x67, 0xFA,
  155. 0xF8, 0xB0, 0x07, 0x4F, 0xF6, 0x3C, 0x03, 0x6F, 0xBE, 0xA1, 0x1F,
  156. 0x4B, 0x99, 0x25, 0x4F, 0xB9, 0x5F, 0xC4, 0x78, 0x76, 0xDE},
  157. {0x59, 0xAB, 0x45, 0xFC, 0x6A, 0xF5, 0xB3, 0xE0, 0xF5, 0x40, 0xD7, 0x98,
  158. 0x0F, 0xF0, 0xA4, 0xCB},
  159. {0xDB, 0xA0, 0xF2, 0x91, 0x8D, 0x50, 0x42, 0xE0, 0x17, 0x68, 0x5B, 0x9B,
  160. 0xF2, 0xA2, 0xC3, 0xF9},
  161. 7,
  162. nullptr},
  163. // Message with four bytes of invalid, non-zero padding.
  164. {"2FJmrF95yVU8Q8cYQy9OoOwCb59ZoRlxazPE0T-MNOSMbr0",
  165. {0x6B, 0x82, 0x92, 0xD3, 0x71, 0x9A, 0x97, 0x76, 0x45, 0x11, 0x99,
  166. 0x6D, 0xBF, 0x56, 0xCC, 0x81, 0x98, 0x56, 0x80, 0xF5, 0x78, 0x36,
  167. 0xD6, 0x43, 0x95, 0x68, 0xDB, 0x0F, 0x23, 0x39, 0xF3, 0x6E},
  168. {0x02, 0x16, 0xDC, 0xC3, 0xDE, 0x2C, 0xB5, 0x08, 0x89, 0xDB, 0xD8, 0x18,
  169. 0x68, 0x83, 0x1C, 0xDB},
  170. {0xB7, 0x85, 0x5D, 0x8E, 0x84, 0xC3, 0x2D, 0x61, 0x9B, 0x78, 0x3B, 0x60,
  171. 0x0E, 0x70, 0x84, 0xF3},
  172. 4096,
  173. nullptr},
  174. // Message with multiple (2) records.
  175. {"reI6sW6y67FI8Kxk-x9GNwiu77His_f5GioDBiKS7IzjDQ",
  176. {0xC6, 0x16, 0x6F, 0xAF, 0xE1, 0xB6, 0x8F, 0x2B, 0x0F, 0x67, 0x5A,
  177. 0xC7, 0xAC, 0x7E, 0xF6, 0x7C, 0x33, 0xA2, 0xA1, 0x11, 0xB0, 0xB0,
  178. 0xAB, 0xAC, 0x37, 0x61, 0xF4, 0xCB, 0x98, 0xFF, 0x00, 0x51},
  179. {0xAE, 0xDA, 0x86, 0xDF, 0x6B, 0x03, 0x88, 0xDE, 0x90, 0xBB, 0xB7, 0xA0,
  180. 0x78, 0x91, 0x3A, 0x36},
  181. {0x4C, 0x4E, 0x2A, 0x8D, 0x88, 0x82, 0xCF, 0xC2, 0xF9, 0x8A, 0xFD, 0x31,
  182. 0xF8, 0xD1, 0xF6, 0xB5},
  183. 8,
  184. nullptr}};
  185. const TestVector kDecryptionTestVectorsDraft08[] = {
  186. // Simple message.
  187. {"baIDPDv-Do_x1RVtlFDex2uCvd3Ugrv-gJG3sWeg",
  188. {0x4D, 0x3A, 0x6C, 0xBA, 0xD8, 0x1D, 0x8E, 0x68, 0x8B, 0xE6, 0x76,
  189. 0xA7, 0xFF, 0x60, 0xC7, 0xFE, 0x77, 0xE2, 0x6D, 0x37, 0xF6, 0x12,
  190. 0x44, 0xE2, 0x25, 0xFE, 0xE1, 0xD8, 0xCF, 0x8A, 0xA8, 0x33},
  191. {0x62, 0x36, 0xAC, 0xCA, 0x74, 0xD4, 0x49, 0x49, 0x6B, 0x27, 0xB4, 0xF7,
  192. 0xC1, 0xE5, 0x30, 0x9A},
  193. {0x1C, 0xA7, 0xFD, 0x98, 0x1A, 0xE4, 0xA7, 0x92, 0xE1, 0xB6, 0xA1, 0xE3,
  194. 0x41, 0x63, 0x87, 0x76},
  195. 4096,
  196. "Hello, world!"},
  197. // Simple message with 16 bytes of padding.
  198. {"6Zq7GKQ7zRxeOWoYR71Nx7xJzCZUUNhz6bhV1-ZIg6dVra0x1uWXms5gHp6F6A",
  199. {0x8B, 0x38, 0x8E, 0x22, 0xD5, 0xC4, 0xFD, 0x65, 0x8A, 0xBB, 0xD9,
  200. 0x58, 0xBD, 0xF5, 0xFF, 0x79, 0xCF, 0x9D, 0xBD, 0x87, 0x16, 0x7E,
  201. 0x93, 0x84, 0x20, 0x8E, 0x8D, 0x49, 0x41, 0x7D, 0x8E, 0x8F},
  202. {0x3E, 0x65, 0xC7, 0x1F, 0x75, 0x7A, 0x43, 0xC4, 0x78, 0x6C, 0x64, 0x99,
  203. 0x49, 0xA0, 0xC4, 0xB2},
  204. {0x43, 0x4D, 0x30, 0x8E, 0xE4, 0x76, 0xB5, 0xD0, 0x87, 0xFC, 0x04, 0xD1,
  205. 0x2E, 0x35, 0x75, 0x63},
  206. 4096,
  207. "Hello, world!"},
  208. // Empty message.
  209. {"bHU7ponA7WAGB0onUybG9nQ",
  210. {0x68, 0x72, 0x3D, 0x13, 0xE7, 0x50, 0xFA, 0x3E, 0xA0, 0x59, 0x33,
  211. 0xF1, 0x73, 0xA8, 0xE8, 0xCD, 0x8D, 0xD4, 0x3C, 0xDC, 0xDE, 0x06,
  212. 0x35, 0x5F, 0x51, 0xBB, 0xB2, 0x57, 0x97, 0x72, 0x9D, 0xFB},
  213. {0x84, 0xB2, 0x2A, 0xE7, 0xC6, 0xC0, 0xCE, 0x5F, 0xAD, 0x37, 0x06, 0x7F,
  214. 0xD1, 0xFD, 0x10, 0x87},
  215. {0x9B, 0xC5, 0x8D, 0x5F, 0xD6, 0xD2, 0xA6, 0xBD, 0xAF, 0x4B, 0xD9, 0x60,
  216. 0xC6, 0xB4, 0x50, 0x0F},
  217. 4096,
  218. ""}};
  219. // Computes the shared secret between the sender and the receiver. The sender
  220. // must have a ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo block, whereas
  221. // the receiver must have a public key in uncompressed EC point format.
  222. bool ComputeSharedP256SecretFromPrivateKeyStr(
  223. const base::StringPiece& private_key,
  224. const base::StringPiece& peer_public_key,
  225. std::string* out_shared_secret) {
  226. DCHECK(out_shared_secret);
  227. std::unique_ptr<crypto::ECPrivateKey> local_key(
  228. crypto::ECPrivateKey::CreateFromPrivateKeyInfo(std::vector<uint8_t>(
  229. private_key.data(), private_key.data() + private_key.size())));
  230. if (!local_key) {
  231. DLOG(ERROR) << "Unable to create the local key";
  232. return false;
  233. }
  234. return ComputeSharedP256Secret(*local_key, peer_public_key,
  235. out_shared_secret);
  236. }
  237. void ComputeSharedSecret(
  238. const base::StringPiece& encoded_sender_private_key,
  239. const base::StringPiece& encoded_receiver_public_key,
  240. std::string* shared_secret) {
  241. std::string sender_private_key, receiver_public_key;
  242. ASSERT_TRUE(base::Base64UrlDecode(
  243. encoded_sender_private_key,
  244. base::Base64UrlDecodePolicy::IGNORE_PADDING, &sender_private_key));
  245. ASSERT_TRUE(base::Base64UrlDecode(
  246. encoded_receiver_public_key,
  247. base::Base64UrlDecodePolicy::IGNORE_PADDING, &receiver_public_key));
  248. ASSERT_TRUE(ComputeSharedP256SecretFromPrivateKeyStr(
  249. sender_private_key, receiver_public_key,
  250. shared_secret));
  251. }
  252. } // namespace
  253. class GCMMessageCryptographerTestBase : public ::testing::Test {
  254. public:
  255. void SetUp() override {
  256. recipient_public_key_.assign(
  257. kCommonRecipientPublicKey,
  258. kCommonRecipientPublicKey + std::size(kCommonRecipientPublicKey));
  259. sender_public_key_.assign(
  260. kCommonSenderPublicKey,
  261. kCommonSenderPublicKey + std::size(kCommonSenderPublicKey));
  262. std::string recipient_private_key(
  263. kCommonRecipientPrivateKey,
  264. kCommonRecipientPrivateKey + std::size(kCommonRecipientPrivateKey));
  265. std::vector<uint8_t> recipient_private_key_vec(
  266. recipient_private_key.begin(), recipient_private_key.end());
  267. std::unique_ptr<crypto::ECPrivateKey> recipient_key =
  268. crypto::ECPrivateKey::CreateFromPrivateKeyInfo(recipient_private_key_vec);
  269. ASSERT_TRUE(recipient_key);
  270. ASSERT_TRUE(ComputeSharedP256Secret(
  271. *recipient_key, sender_public_key_, &ecdh_shared_secret_));
  272. auth_secret_.assign(kCommonAuthSecret,
  273. kCommonAuthSecret + std::size(kCommonAuthSecret));
  274. }
  275. protected:
  276. // Public keys of the recipient and sender as uncompressed P-256 EC points.
  277. std::string recipient_public_key_;
  278. std::string sender_public_key_;
  279. // Shared secret to use in transformations. Associated with the keys above.
  280. std::string ecdh_shared_secret_;
  281. // Authentication secret to use in tests where no specific value is expected.
  282. std::string auth_secret_;
  283. };
  284. class GCMMessageCryptographerTest
  285. : public GCMMessageCryptographerTestBase,
  286. public testing::WithParamInterface<GCMMessageCryptographer::Version> {
  287. public:
  288. void SetUp() override {
  289. GCMMessageCryptographerTestBase::SetUp();
  290. cryptographer_ = std::make_unique<GCMMessageCryptographer>(GetParam());
  291. }
  292. protected:
  293. // Generates a cryptographically secure random salt of 16-octets in size, the
  294. // required length as expected by the HKDF.
  295. std::string GenerateRandomSalt() {
  296. std::string salt;
  297. crypto::RandBytes(base::WriteInto(&salt, kSaltSize + 1), kSaltSize);
  298. return salt;
  299. }
  300. // The GCMMessageCryptographer instance to use for the tests.
  301. std::unique_ptr<GCMMessageCryptographer> cryptographer_;
  302. };
  303. TEST_P(GCMMessageCryptographerTest, RoundTrip) {
  304. const std::string salt = GenerateRandomSalt();
  305. size_t record_size = 0;
  306. std::string ciphertext, plaintext;
  307. ASSERT_TRUE(cryptographer_->Encrypt(
  308. recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
  309. auth_secret_, salt, kExamplePlaintext, &record_size, &ciphertext));
  310. EXPECT_GT(record_size, ciphertext.size() - 16);
  311. EXPECT_GT(ciphertext.size(), 0u);
  312. ASSERT_TRUE(cryptographer_->Decrypt(recipient_public_key_, sender_public_key_,
  313. ecdh_shared_secret_, auth_secret_, salt,
  314. ciphertext, record_size, &plaintext));
  315. EXPECT_EQ(kExamplePlaintext, plaintext);
  316. }
  317. TEST_P(GCMMessageCryptographerTest, RoundTripEmptyMessage) {
  318. const std::string salt = GenerateRandomSalt();
  319. const std::string message;
  320. size_t record_size = 0;
  321. std::string ciphertext, plaintext;
  322. ASSERT_TRUE(cryptographer_->Encrypt(recipient_public_key_, sender_public_key_,
  323. ecdh_shared_secret_, auth_secret_, salt,
  324. message, &record_size, &ciphertext));
  325. EXPECT_GT(record_size, ciphertext.size() - 16);
  326. EXPECT_GT(ciphertext.size(), 0u);
  327. ASSERT_TRUE(cryptographer_->Decrypt(recipient_public_key_, sender_public_key_,
  328. ecdh_shared_secret_, auth_secret_, salt,
  329. ciphertext, record_size, &plaintext));
  330. EXPECT_EQ(message, plaintext);
  331. }
  332. TEST_P(GCMMessageCryptographerTest, InvalidRecordSize) {
  333. const std::string salt = GenerateRandomSalt();
  334. size_t record_size = 0;
  335. std::string ciphertext, plaintext;
  336. ASSERT_TRUE(cryptographer_->Encrypt(
  337. recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
  338. auth_secret_, salt, kExamplePlaintext, &record_size, &ciphertext));
  339. EXPECT_GT(record_size, ciphertext.size() - 16);
  340. EXPECT_FALSE(cryptographer_->Decrypt(
  341. recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
  342. auth_secret_, salt, ciphertext, 0 /* record_size */, &plaintext));
  343. EXPECT_FALSE(cryptographer_->Decrypt(
  344. recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
  345. auth_secret_, salt, ciphertext, ciphertext.size() - 17, &plaintext));
  346. EXPECT_TRUE(cryptographer_->Decrypt(
  347. recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
  348. auth_secret_, salt, ciphertext, ciphertext.size() - 16, &plaintext));
  349. }
  350. TEST_P(GCMMessageCryptographerTest, InvalidRecordPadding) {
  351. std::string message;
  352. switch (GetParam()) {
  353. case GCMMessageCryptographer::Version::DRAFT_03:
  354. message.append(sizeof(uint8_t), '\00'); // padding length octets
  355. message.append(sizeof(uint8_t), '\01');
  356. message.append(sizeof(uint8_t), '\00'); // padding octet
  357. message.append(kExamplePlaintext);
  358. break;
  359. case GCMMessageCryptographer::Version::DRAFT_08:
  360. message.append(kExamplePlaintext);
  361. message.append(sizeof(uint8_t), '\x02'); // padding delimiter octet
  362. message.append(sizeof(uint8_t), '\x00'); // padding octet
  363. break;
  364. }
  365. const std::string salt = GenerateRandomSalt();
  366. const std::string prk =
  367. cryptographer_->encryption_scheme_->DerivePseudoRandomKey(
  368. recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
  369. auth_secret_);
  370. const std::string content_encryption_key =
  371. cryptographer_->DeriveContentEncryptionKey(recipient_public_key_,
  372. sender_public_key_, prk, salt);
  373. const std::string nonce = cryptographer_->DeriveNonce(
  374. recipient_public_key_, sender_public_key_, prk, salt);
  375. ASSERT_GT(message.size(), 2u);
  376. const size_t record_size = message.size() + 1;
  377. std::string ciphertext, plaintext;
  378. ASSERT_TRUE(cryptographer_->TransformRecord(
  379. GCMMessageCryptographer::Direction::ENCRYPT, message,
  380. content_encryption_key, nonce, &ciphertext));
  381. ASSERT_TRUE(cryptographer_->Decrypt(recipient_public_key_, sender_public_key_,
  382. ecdh_shared_secret_, auth_secret_, salt,
  383. ciphertext, record_size, &plaintext));
  384. // Note that GCMMessageCryptographer::Decrypt removes the padding.
  385. EXPECT_EQ(kExamplePlaintext, plaintext);
  386. // Now run the same steps again, but have invalid padding length indicators.
  387. // (Only applicable to draft-ietf-webpush-encryption-03.)
  388. if (GetParam() == GCMMessageCryptographer::Version::DRAFT_03) {
  389. // Padding that will spill over in the payload.
  390. {
  391. message[1] = 4;
  392. ASSERT_TRUE(cryptographer_->TransformRecord(
  393. GCMMessageCryptographer::Direction::ENCRYPT, message,
  394. content_encryption_key, nonce, &ciphertext));
  395. ASSERT_FALSE(cryptographer_->Decrypt(
  396. recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
  397. auth_secret_, salt, ciphertext, record_size, &plaintext));
  398. }
  399. // More padding octets than the length of the message.
  400. {
  401. message[1] = 64;
  402. ASSERT_TRUE(cryptographer_->TransformRecord(
  403. GCMMessageCryptographer::Direction::ENCRYPT, message,
  404. content_encryption_key, nonce, &ciphertext));
  405. ASSERT_FALSE(cryptographer_->Decrypt(
  406. recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
  407. auth_secret_, salt, ciphertext, record_size, &plaintext));
  408. }
  409. // Correct the |message| to be valid again. (A single byte of padding.)
  410. message[1] = 1;
  411. }
  412. // Run tests for a missing delimiter in the record.
  413. // (Only applicable to draft-ietf-webpush-encryption-03.)
  414. if (GetParam() == GCMMessageCryptographer::Version::DRAFT_08) {
  415. message[message.size() - 2] = 0x00;
  416. ASSERT_TRUE(cryptographer_->TransformRecord(
  417. GCMMessageCryptographer::Direction::ENCRYPT, message,
  418. content_encryption_key, nonce, &ciphertext));
  419. ASSERT_FALSE(cryptographer_->Decrypt(
  420. recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
  421. auth_secret_, salt, ciphertext, record_size, &plaintext));
  422. // Correct the |message| to be valid again. (Proper padding delimiter.)
  423. message[message.size() - 2] = 0x02;
  424. }
  425. // Finally run a test to make sure that we validate that all padding bytes are
  426. // set to zeros. The position of the padding byte depends on the version.
  427. switch (GetParam()) {
  428. case GCMMessageCryptographer::Version::DRAFT_03:
  429. message[2] = 0x13;
  430. break;
  431. case GCMMessageCryptographer::Version::DRAFT_08:
  432. message[message.size() - 1] = 0x13;
  433. break;
  434. }
  435. ASSERT_TRUE(cryptographer_->TransformRecord(
  436. GCMMessageCryptographer::Direction::ENCRYPT, message,
  437. content_encryption_key, nonce, &ciphertext));
  438. ASSERT_FALSE(cryptographer_->Decrypt(
  439. recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
  440. auth_secret_, salt, ciphertext, record_size, &plaintext));
  441. }
  442. TEST_P(GCMMessageCryptographerTest, AuthSecretAffectsPRK) {
  443. std::string first_auth_secret, second_auth_secret;
  444. crypto::RandBytes(base::WriteInto(&first_auth_secret, kAuthSecretSize + 1),
  445. kAuthSecretSize);
  446. crypto::RandBytes(base::WriteInto(&second_auth_secret, kAuthSecretSize + 1),
  447. kAuthSecretSize);
  448. ASSERT_NE(cryptographer_->encryption_scheme_->DerivePseudoRandomKey(
  449. recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
  450. first_auth_secret),
  451. cryptographer_->encryption_scheme_->DerivePseudoRandomKey(
  452. recipient_public_key_, sender_public_key_, ecdh_shared_secret_,
  453. second_auth_secret));
  454. std::string salt = GenerateRandomSalt();
  455. // Verify that the IKM actually gets used by the transformations.
  456. size_t first_record_size, second_record_size;
  457. std::string first_ciphertext, second_ciphertext;
  458. ASSERT_TRUE(cryptographer_->Encrypt(recipient_public_key_, sender_public_key_,
  459. ecdh_shared_secret_, first_auth_secret,
  460. salt, kExamplePlaintext,
  461. &first_record_size, &first_ciphertext));
  462. ASSERT_TRUE(cryptographer_->Encrypt(recipient_public_key_, sender_public_key_,
  463. ecdh_shared_secret_, second_auth_secret,
  464. salt, kExamplePlaintext,
  465. &second_record_size, &second_ciphertext));
  466. // If the ciphertexts differ despite the same key and salt, it got used.
  467. ASSERT_NE(first_ciphertext, second_ciphertext);
  468. EXPECT_EQ(first_record_size, second_record_size);
  469. // Verify that the different ciphertexts can also be translated back to the
  470. // plaintext content. This will fail if the auth secret isn't considered.
  471. std::string first_plaintext, second_plaintext;
  472. ASSERT_TRUE(cryptographer_->Decrypt(recipient_public_key_, sender_public_key_,
  473. ecdh_shared_secret_, first_auth_secret,
  474. salt, first_ciphertext, first_record_size,
  475. &first_plaintext));
  476. ASSERT_TRUE(cryptographer_->Decrypt(recipient_public_key_, sender_public_key_,
  477. ecdh_shared_secret_, second_auth_secret,
  478. salt, second_ciphertext,
  479. second_record_size, &second_plaintext));
  480. EXPECT_EQ(kExamplePlaintext, first_plaintext);
  481. EXPECT_EQ(kExamplePlaintext, second_plaintext);
  482. }
  483. INSTANTIATE_TEST_SUITE_P(
  484. GCMMessageCryptographerTestBase,
  485. GCMMessageCryptographerTest,
  486. ::testing::Values(GCMMessageCryptographer::Version::DRAFT_03,
  487. GCMMessageCryptographer::Version::DRAFT_08));
  488. class GCMMessageCryptographerTestVectorTest
  489. : public GCMMessageCryptographerTestBase {};
  490. TEST_F(GCMMessageCryptographerTestVectorTest, EncryptionVectorsDraft03) {
  491. GCMMessageCryptographer cryptographer(
  492. GCMMessageCryptographer::Version::DRAFT_03);
  493. std::string ecdh_shared_secret, auth_secret, salt, ciphertext, output;
  494. size_t record_size = 0;
  495. for (size_t i = 0; i < std::size(kEncryptionTestVectorsDraft03); ++i) {
  496. SCOPED_TRACE(i);
  497. ecdh_shared_secret.assign(
  498. kEncryptionTestVectorsDraft03[i].ecdh_shared_secret,
  499. kEncryptionTestVectorsDraft03[i].ecdh_shared_secret +
  500. kEcdhSharedSecretSize);
  501. auth_secret.assign(
  502. kEncryptionTestVectorsDraft03[i].auth_secret,
  503. kEncryptionTestVectorsDraft03[i].auth_secret + kAuthSecretSize);
  504. salt.assign(kEncryptionTestVectorsDraft03[i].salt,
  505. kEncryptionTestVectorsDraft03[i].salt + kSaltSize);
  506. ASSERT_TRUE(cryptographer.Encrypt(recipient_public_key_, sender_public_key_,
  507. ecdh_shared_secret, auth_secret, salt,
  508. kEncryptionTestVectorsDraft03[i].input,
  509. &record_size, &ciphertext));
  510. base::Base64UrlEncode(ciphertext, base::Base64UrlEncodePolicy::OMIT_PADDING,
  511. &output);
  512. EXPECT_EQ(kEncryptionTestVectorsDraft03[i].record_size, record_size);
  513. EXPECT_EQ(kEncryptionTestVectorsDraft03[i].output, output);
  514. }
  515. }
  516. TEST_F(GCMMessageCryptographerTestVectorTest, DecryptionVectorsDraft03) {
  517. GCMMessageCryptographer cryptographer(
  518. GCMMessageCryptographer::Version::DRAFT_03);
  519. std::string input, ecdh_shared_secret, auth_secret, salt, plaintext;
  520. for (size_t i = 0; i < std::size(kDecryptionTestVectorsDraft03); ++i) {
  521. SCOPED_TRACE(i);
  522. ASSERT_TRUE(base::Base64UrlDecode(
  523. kDecryptionTestVectorsDraft03[i].input,
  524. base::Base64UrlDecodePolicy::IGNORE_PADDING, &input));
  525. ecdh_shared_secret.assign(
  526. kDecryptionTestVectorsDraft03[i].ecdh_shared_secret,
  527. kDecryptionTestVectorsDraft03[i].ecdh_shared_secret +
  528. kEcdhSharedSecretSize);
  529. auth_secret.assign(
  530. kDecryptionTestVectorsDraft03[i].auth_secret,
  531. kDecryptionTestVectorsDraft03[i].auth_secret + kAuthSecretSize);
  532. salt.assign(kDecryptionTestVectorsDraft03[i].salt,
  533. kDecryptionTestVectorsDraft03[i].salt + kSaltSize);
  534. const bool has_output = kDecryptionTestVectorsDraft03[i].output;
  535. const bool result = cryptographer.Decrypt(
  536. recipient_public_key_, sender_public_key_, ecdh_shared_secret,
  537. auth_secret, salt, input, kDecryptionTestVectorsDraft03[i].record_size,
  538. &plaintext);
  539. if (!has_output) {
  540. EXPECT_FALSE(result);
  541. continue;
  542. }
  543. EXPECT_TRUE(result);
  544. EXPECT_EQ(kDecryptionTestVectorsDraft03[i].output, plaintext);
  545. }
  546. }
  547. TEST_F(GCMMessageCryptographerTestVectorTest, EncryptionVectorsDraft08) {
  548. GCMMessageCryptographer cryptographer(
  549. GCMMessageCryptographer::Version::DRAFT_08);
  550. std::string ecdh_shared_secret, auth_secret, salt, ciphertext, output;
  551. size_t record_size = 0;
  552. for (size_t i = 0; i < std::size(kEncryptionTestVectorsDraft08); ++i) {
  553. SCOPED_TRACE(i);
  554. ecdh_shared_secret.assign(
  555. kEncryptionTestVectorsDraft08[i].ecdh_shared_secret,
  556. kEncryptionTestVectorsDraft08[i].ecdh_shared_secret +
  557. kEcdhSharedSecretSize);
  558. auth_secret.assign(
  559. kEncryptionTestVectorsDraft08[i].auth_secret,
  560. kEncryptionTestVectorsDraft08[i].auth_secret + kAuthSecretSize);
  561. salt.assign(kEncryptionTestVectorsDraft08[i].salt,
  562. kEncryptionTestVectorsDraft08[i].salt + kSaltSize);
  563. ASSERT_TRUE(cryptographer.Encrypt(recipient_public_key_, sender_public_key_,
  564. ecdh_shared_secret, auth_secret, salt,
  565. kEncryptionTestVectorsDraft08[i].input,
  566. &record_size, &ciphertext));
  567. base::Base64UrlEncode(ciphertext, base::Base64UrlEncodePolicy::OMIT_PADDING,
  568. &output);
  569. EXPECT_EQ(kEncryptionTestVectorsDraft08[i].record_size, record_size);
  570. EXPECT_EQ(kEncryptionTestVectorsDraft08[i].output, output);
  571. }
  572. }
  573. TEST_F(GCMMessageCryptographerTestVectorTest, DecryptionVectorsDraft08) {
  574. GCMMessageCryptographer cryptographer(
  575. GCMMessageCryptographer::Version::DRAFT_08);
  576. std::string input, ecdh_shared_secret, auth_secret, salt, plaintext;
  577. for (size_t i = 0; i < std::size(kDecryptionTestVectorsDraft08); ++i) {
  578. SCOPED_TRACE(i);
  579. ASSERT_TRUE(base::Base64UrlDecode(
  580. kDecryptionTestVectorsDraft08[i].input,
  581. base::Base64UrlDecodePolicy::IGNORE_PADDING, &input));
  582. ecdh_shared_secret.assign(
  583. kDecryptionTestVectorsDraft08[i].ecdh_shared_secret,
  584. kDecryptionTestVectorsDraft08[i].ecdh_shared_secret +
  585. kEcdhSharedSecretSize);
  586. auth_secret.assign(
  587. kDecryptionTestVectorsDraft08[i].auth_secret,
  588. kDecryptionTestVectorsDraft08[i].auth_secret + kAuthSecretSize);
  589. salt.assign(kDecryptionTestVectorsDraft08[i].salt,
  590. kDecryptionTestVectorsDraft08[i].salt + kSaltSize);
  591. const bool has_output = kDecryptionTestVectorsDraft08[i].output;
  592. const bool result = cryptographer.Decrypt(
  593. recipient_public_key_, sender_public_key_, ecdh_shared_secret,
  594. auth_secret, salt, input, kDecryptionTestVectorsDraft08[i].record_size,
  595. &plaintext);
  596. if (!has_output) {
  597. EXPECT_FALSE(result);
  598. continue;
  599. }
  600. EXPECT_TRUE(result);
  601. EXPECT_EQ(kDecryptionTestVectorsDraft08[i].output, plaintext);
  602. }
  603. }
  604. class GCMMessageCryptographerReferenceTest : public ::testing::Test {};
  605. // Reference test included for the Version::DRAFT_03 implementation.
  606. // https://tools.ietf.org/html/draft-ietf-webpush-encryption-03
  607. // https://tools.ietf.org/html/draft-ietf-httpbis-encryption-encoding-02
  608. TEST_F(GCMMessageCryptographerReferenceTest, ReferenceDraft03) {
  609. // The 16-byte salt unique to the message.
  610. const char kSalt[] = "lngarbyKfMoi9Z75xYXmkg";
  611. // The 16-byte prearranged secret between the sender and receiver.
  612. const char kAuthSecret[] = "R29vIGdvbyBnJyBqb29iIQ";
  613. // The keying material used by the sender to encrypt the |kCiphertext|.
  614. const char kSenderPrivate[] =
  615. "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgnCScek-QpEjmOOlT-rQ38nZz"
  616. "vdPlqa00Zy0i6m2OJvahRANCAATaEQ22_OCRpvIOWeQhcbq0qrF1iddSLX1xFmFSxPOWOwmJ"
  617. "A417CBHOGqsWGkNRvAapFwiegz6Q61rXVo_5roB1";
  618. const char kSenderPublicKeyUncompressed[] =
  619. "BNoRDbb84JGm8g5Z5CFxurSqsXWJ11ItfXEWYVLE85Y7CYkDjXsIEc4aqxYaQ1G8BqkXCJ6D"
  620. "PpDrWtdWj_mugHU";
  621. // The keying material used by the recipient to decrypt the |kCiphertext|.
  622. const char kRecipientPrivate[] =
  623. "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg9FWl15_QUQAWDaD3k3l50ZBZ"
  624. "QJ4au27F1V4F0uLSD_OhRANCAAQhJAY8y_GdwvqItkO6BObdjafqe6LIxi4Pd6lD9ML6kU9t"
  625. "RBFsn9HEA0HGpEDKs-IUCmDkN4pdpzWXLeB4AFEF";
  626. const char kRecipientPublicKeyUncompressed[] =
  627. "BCEkBjzL8Z3C-oi2Q7oE5t2Np-p7osjGLg93qUP0wvqRT21EEWyf0cQDQcakQMqz4hQKYOQ3"
  628. "il2nNZct4HgAUQU";
  629. // The ciphertext and associated plaintext of the message.
  630. const char kCiphertext[] = "6nqAQUME8hNqw5J3kl8cpVVJylXKYqZOeseZG8UueKpA";
  631. const char kPlaintext[] = "I am the walrus";
  632. std::string sender_shared_secret, receiver_shared_secret;
  633. // Compute the shared secrets between the sender and receiver's keys.
  634. ASSERT_NO_FATAL_FAILURE(ComputeSharedSecret(
  635. kSenderPrivate, kRecipientPublicKeyUncompressed, &sender_shared_secret));
  636. ASSERT_NO_FATAL_FAILURE(ComputeSharedSecret(kRecipientPrivate,
  637. kSenderPublicKeyUncompressed,
  638. &receiver_shared_secret));
  639. ASSERT_GT(sender_shared_secret.size(), 0u);
  640. ASSERT_EQ(sender_shared_secret, receiver_shared_secret);
  641. // Decode the public keys of both parties, the auth secret and the salt.
  642. std::string recipient_public_key, sender_public_key, auth_secret, salt;
  643. ASSERT_TRUE(base::Base64UrlDecode(kRecipientPublicKeyUncompressed,
  644. base::Base64UrlDecodePolicy::IGNORE_PADDING,
  645. &recipient_public_key));
  646. ASSERT_TRUE(base::Base64UrlDecode(kSenderPublicKeyUncompressed,
  647. base::Base64UrlDecodePolicy::IGNORE_PADDING,
  648. &sender_public_key));
  649. ASSERT_TRUE(base::Base64UrlDecode(
  650. kAuthSecret, base::Base64UrlDecodePolicy::IGNORE_PADDING, &auth_secret));
  651. ASSERT_TRUE(base::Base64UrlDecode(
  652. kSalt, base::Base64UrlDecodePolicy::IGNORE_PADDING, &salt));
  653. std::string encoded_ciphertext, ciphertext, plaintext;
  654. size_t record_size = 0;
  655. // Now verify that encrypting a message with the given information yields the
  656. // expected ciphertext given the defined input.
  657. GCMMessageCryptographer cryptographer(
  658. GCMMessageCryptographer::Version::DRAFT_03);
  659. ASSERT_TRUE(cryptographer.Encrypt(recipient_public_key, sender_public_key,
  660. sender_shared_secret, auth_secret, salt,
  661. kPlaintext, &record_size, &ciphertext));
  662. base::Base64UrlEncode(ciphertext, base::Base64UrlEncodePolicy::OMIT_PADDING,
  663. &encoded_ciphertext);
  664. ASSERT_EQ(kCiphertext, encoded_ciphertext);
  665. // And verify that decrypting the message yields the plaintext again.
  666. ASSERT_TRUE(cryptographer.Decrypt(recipient_public_key, sender_public_key,
  667. sender_shared_secret, auth_secret, salt,
  668. ciphertext, record_size, &plaintext));
  669. ASSERT_EQ(kPlaintext, plaintext);
  670. }
  671. // Reference test included for the Version::DRAFT_08 implementation.
  672. // https://tools.ietf.org/html/draft-ietf-webpush-encryption-08
  673. // https://tools.ietf.org/html/draft-ietf-httpbis-encryption-encoding-07
  674. TEST_F(GCMMessageCryptographerReferenceTest, ReferenceDraft08) {
  675. // The 16-byte prearranged secret between the sender and receiver.
  676. const char kAuthSecret[] = "BTBZMqHH6r4Tts7J_aSIgg";
  677. // The keying material used by the sender to encrypt the |kCiphertext|.
  678. const char kSenderPrivate[] =
  679. "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgyfWPiYE-n46HLnH0KqZOF1fJ"
  680. "JU3MYrct3AELtAQ-oRyhRANCAAT-M_SrDepxkU21WCP3O1SUj0EwbZIHMtu5pZpTKGSCIA5Z"
  681. "ent7wmC6HCJ5mFgJkuk5cwAvMBKiiujwa7t45ewP";
  682. // The keying material used by the recipient to decrypt the |kCiphertext|.
  683. const char kRecipientPrivate[] =
  684. "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgq1dXpw3UpT5VOmu_cf_v6ih0"
  685. "7Aems3njxI-JWgLcM96hRANCAAQlcbK-zf3jYFUarx7Q9M02bBHOvlVfiby3sYalMzkXMWjs"
  686. "4uvgGFl70wR5uG48j47O1XfKWRh-kkaZDbaCAIsO";
  687. const char kRecipientPublicKeyUncompressed[] =
  688. "BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZ"
  689. "GH6SRpkNtoIAiw4";
  690. // The plain text of the message, as well as the encrypted reference message.
  691. const char kPlaintext[] = "When I grow up, I want to be a watermelon";
  692. const char kReferenceMessage[] =
  693. "DGv6ra1nlYgDCS1FRnbzlwAAEABBBP4z9KsN6nGRTbVYI_"
  694. "c7VJSPQTBtkgcy27mlmlMoZIIgDll6e3vCYLocInmYWAmS6TlzAC8wEqKK6PBru3jl7A_"
  695. "yl95bQpu6cVPTpK4Mqgkf1CXztLVBSt2Ks3oZwbuwXPXLWyouBWLVWGNWQexSgSxsj_"
  696. "Qulcy4a-fN";
  697. std::string message;
  698. ASSERT_TRUE(base::Base64UrlDecode(kReferenceMessage,
  699. base::Base64UrlDecodePolicy::IGNORE_PADDING,
  700. &message));
  701. MessagePayloadParser message_parser(message);
  702. ASSERT_TRUE(message_parser.IsValid());
  703. base::StringPiece salt = message_parser.salt();
  704. uint32_t record_size = message_parser.record_size();
  705. base::StringPiece sender_public_key = message_parser.public_key();
  706. base::StringPiece ciphertext = message_parser.ciphertext();
  707. std::string sender_shared_secret, receiver_shared_secret;
  708. // Compute the shared secrets between the sender and receiver's keys.
  709. ASSERT_NO_FATAL_FAILURE(ComputeSharedSecret(
  710. kSenderPrivate, kRecipientPublicKeyUncompressed, &sender_shared_secret));
  711. // Compute the shared secret based on the sender's public key, which isn't a
  712. // constant but instead is included in the message's binary header.
  713. std::string recipient_private_key;
  714. ASSERT_TRUE(base::Base64UrlDecode(kRecipientPrivate,
  715. base::Base64UrlDecodePolicy::IGNORE_PADDING,
  716. &recipient_private_key));
  717. ASSERT_NO_FATAL_FAILURE(ComputeSharedP256SecretFromPrivateKeyStr(
  718. recipient_private_key, sender_public_key,
  719. &receiver_shared_secret));
  720. ASSERT_GT(sender_shared_secret.size(), 0u);
  721. ASSERT_EQ(sender_shared_secret, receiver_shared_secret);
  722. // Decode the public keys of both parties and the auth secret.
  723. std::string recipient_public_key, auth_secret;
  724. ASSERT_TRUE(base::Base64UrlDecode(kRecipientPublicKeyUncompressed,
  725. base::Base64UrlDecodePolicy::IGNORE_PADDING,
  726. &recipient_public_key));
  727. ASSERT_TRUE(base::Base64UrlDecode(
  728. kAuthSecret, base::Base64UrlDecodePolicy::IGNORE_PADDING, &auth_secret));
  729. // Attempt to decrypt the message using a GCMMessageCryptographer for this
  730. // version of the draft, and then re-encrypt it agian to make sure it matches.
  731. GCMMessageCryptographer cryptographer(
  732. GCMMessageCryptographer::Version::DRAFT_08);
  733. std::string plaintext;
  734. ASSERT_TRUE(cryptographer.Decrypt(recipient_public_key, sender_public_key,
  735. sender_shared_secret, auth_secret, salt,
  736. ciphertext, record_size, &plaintext));
  737. ASSERT_EQ(kPlaintext, plaintext);
  738. size_t record_size2;
  739. std::string ciphertext2;
  740. ASSERT_TRUE(cryptographer.Encrypt(recipient_public_key, sender_public_key,
  741. sender_shared_secret, auth_secret, salt,
  742. kPlaintext, &record_size2, &ciphertext2));
  743. EXPECT_GE(record_size2, record_size);
  744. EXPECT_EQ(ciphertext2, ciphertext);
  745. }
  746. } // namespace gcm