diff options
author | Jordan Bayles <jophba@chromium.org> | 2020-09-21 09:02:24 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-09-21 17:39:18 +0000 |
commit | 867358ee772e2c547c4d589e5594dc09a2c68beb (patch) | |
tree | c2ee5d1e0068fff390111e159763c6ae7579448d /util | |
parent | 7159d1a56e7d1f4aeee4be1ba85bbb766ac71c79 (diff) | |
download | openscreen-867358ee772e2c547c4d589e5594dc09a2c68beb.tar.gz |
[Cast Streaming] Implement sender session class
This patch adds a new SenderSession class along with unit testing for
this class.
Embedders can create an instance of this class in order to start a
streaming session with an already known receiver. The embedder is
expected to have a valid TLS connection already established with
the receiver.
Bug: b/162542753
Change-Id: Ia91df728d947e1fa20ed0b360f595f75d41bf11c
Reviewed-on: https://chromium-review.googlesource.com/c/openscreen/+/2378540
Reviewed-by: Jordan Bayles <jophba@chromium.org>
Reviewed-by: mark a. foltz <mfoltz@chromium.org>
Reviewed-by: Yuri Wiitala <miu@chromium.org>
Commit-Queue: Jordan Bayles <jophba@chromium.org>
Diffstat (limited to 'util')
-rw-r--r-- | util/BUILD.gn | 3 | ||||
-rw-r--r-- | util/crypto/random_bytes.cc | 25 | ||||
-rw-r--r-- | util/crypto/random_bytes.h | 19 | ||||
-rw-r--r-- | util/crypto/random_bytes_unittest.cc | 52 |
4 files changed, 99 insertions, 0 deletions
diff --git a/util/BUILD.gn b/util/BUILD.gn index 7eaf3d30..d2f756e7 100644 --- a/util/BUILD.gn +++ b/util/BUILD.gn @@ -30,6 +30,8 @@ source_set("util") { "crypto/digest_sign.h", "crypto/openssl_util.cc", "crypto/openssl_util.h", + "crypto/random_bytes.cc", + "crypto/random_bytes.h", "crypto/rsa_private_key.cc", "crypto/rsa_private_key.h", "crypto/secure_hash.cc", @@ -83,6 +85,7 @@ source_set("unittests") { sources = [ "alarm_unittest.cc", "big_endian_unittest.cc", + "crypto/random_bytes_unittest.cc", "crypto/rsa_private_key_unittest.cc", "crypto/secure_hash_unittest.cc", "crypto/sha2_unittest.cc", diff --git a/util/crypto/random_bytes.cc b/util/crypto/random_bytes.cc new file mode 100644 index 00000000..c090a762 --- /dev/null +++ b/util/crypto/random_bytes.cc @@ -0,0 +1,25 @@ +// Copyright 2020 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "util/crypto/random_bytes.h" + +#include "openssl/rand.h" +#include "util/osp_logging.h" + +namespace openscreen { +namespace crypto { + +std::array<uint8_t, 16> GenerateRandomBytes16() { + std::array<uint8_t, 16> result; + GenerateRandomBytes(result.begin(), result.size()); + return result; +} + +void GenerateRandomBytes(uint8_t* out, int len) { + // Working cryptography is mandatory for our library to run. + OSP_CHECK(RAND_bytes(out, len) == 1); +} + +} // namespace crypto +} // namespace openscreen diff --git a/util/crypto/random_bytes.h b/util/crypto/random_bytes.h new file mode 100644 index 00000000..be7381f0 --- /dev/null +++ b/util/crypto/random_bytes.h @@ -0,0 +1,19 @@ +// Copyright 2020 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef UTIL_CRYPTO_RANDOM_BYTES_H_ +#define UTIL_CRYPTO_RANDOM_BYTES_H_ + +#include <array> + +namespace openscreen { +namespace crypto { + +std::array<uint8_t, 16> GenerateRandomBytes16(); +void GenerateRandomBytes(uint8_t* out, int len); + +} // namespace crypto +} // namespace openscreen + +#endif // UTIL_CRYPTO_RANDOM_BYTES_H_ diff --git a/util/crypto/random_bytes_unittest.cc b/util/crypto/random_bytes_unittest.cc new file mode 100644 index 00000000..b42e3f08 --- /dev/null +++ b/util/crypto/random_bytes_unittest.cc @@ -0,0 +1,52 @@ +// Copyright 2020 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "util/crypto/random_bytes.h" + +#include <algorithm> +#include <utility> + +#include "gtest/gtest.h" + +namespace openscreen { +namespace crypto { +namespace { + +struct NonZero { + NonZero() = default; + bool operator()(int n) const { return n > 0; } +}; + +} // namespace + +TEST(RandomBytesTest, CanGenerateRandomBytes) { + std::array<uint8_t, 4> bytes; + GenerateRandomBytes(bytes.begin(), bytes.size()); + + NonZero pred; + ASSERT_TRUE(std::any_of(bytes.begin(), bytes.end(), pred)); +} + +TEST(RandomBytesTest, CanGenerate16RandomBytes) { + std::array<uint8_t, 16> bytes = GenerateRandomBytes16(); + + NonZero pred; + ASSERT_TRUE(std::any_of(bytes.begin(), bytes.end(), pred)); +} + +TEST(RandomBytesTest, KeysAreNotIdentical) { + constexpr int kNumKeys = 100; + constexpr int kKeyLength = 100; + std::array<std::array<uint8_t, kKeyLength>, kNumKeys> keys; + for (int i = 0; i < kNumKeys; ++i) { + GenerateRandomBytes(keys[i].begin(), kKeyLength); + } + + std::sort(std::begin(keys), std::end(keys)); + ASSERT_TRUE(std::adjacent_find(std::begin(keys), std::end(keys)) == + std::end(keys)); +} + +} // namespace crypto +} // namespace openscreen |