gcm_crypto_test_helpers.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_crypto_test_helpers.h"
  5. #include <stddef.h>
  6. #include <sstream>
  7. #include <string>
  8. #include "base/base64url.h"
  9. #include "base/strings/string_util.h"
  10. #include "components/gcm_driver/common/gcm_message.h"
  11. #include "components/gcm_driver/crypto/gcm_message_cryptographer.h"
  12. #include "components/gcm_driver/crypto/p256_key_util.h"
  13. #include "crypto/ec_private_key.h"
  14. #include "crypto/random.h"
  15. namespace gcm {
  16. bool CreateEncryptedPayloadForTesting(const base::StringPiece& payload,
  17. const base::StringPiece& peer_public_key,
  18. const base::StringPiece& auth_secret,
  19. IncomingMessage* message) {
  20. DCHECK(message);
  21. // Create an ephemeral key for the sender.
  22. std::unique_ptr<crypto::ECPrivateKey> key = crypto::ECPrivateKey::Create();
  23. if (!key)
  24. return false;
  25. std::string shared_secret;
  26. // Calculate the shared secret between the sender and its peer.
  27. if (!ComputeSharedP256Secret(*key, peer_public_key, &shared_secret)) {
  28. return false;
  29. }
  30. std::string salt;
  31. // Generate a cryptographically secure random salt for the message.
  32. const size_t salt_size = GCMMessageCryptographer::kSaltSize;
  33. crypto::RandBytes(base::WriteInto(&salt, salt_size + 1), salt_size);
  34. GCMMessageCryptographer cryptographer(
  35. GCMMessageCryptographer::Version::DRAFT_03);
  36. size_t record_size;
  37. std::string ciphertext;
  38. std::string public_key;
  39. if (!GetRawPublicKey(*key, &public_key))
  40. return false;
  41. if (!cryptographer.Encrypt(peer_public_key, public_key, shared_secret,
  42. auth_secret, salt, payload, &record_size,
  43. &ciphertext)) {
  44. return false;
  45. }
  46. std::string encoded_salt, encoded_public_key;
  47. // Create base64url encoded representations of the salt and local public key.
  48. base::Base64UrlEncode(salt, base::Base64UrlEncodePolicy::OMIT_PADDING,
  49. &encoded_salt);
  50. base::Base64UrlEncode(public_key, base::Base64UrlEncodePolicy::OMIT_PADDING,
  51. &encoded_public_key);
  52. // Write the Encryption header value to |*message|.
  53. std::stringstream encryption_header;
  54. encryption_header << "salt=" << encoded_salt << ";rs=" << record_size;
  55. message->data["encryption"] = encryption_header.str();
  56. // Write the Crypto-Key value to |*message|.
  57. std::stringstream crypto_key_header;
  58. crypto_key_header << "dh=" << encoded_public_key;
  59. message->data["crypto-key"] = crypto_key_header.str();
  60. message->raw_data.swap(ciphertext);
  61. return true;
  62. }
  63. } // namespace gcm