summaryrefslogtreecommitdiffstats
path: root/encryption/utils.cpp
diff options
context:
space:
mode:
authorEric Biggers <ebiggers@google.com>2020-04-08 11:00:11 -0700
committerEric Biggers <ebiggers@google.com>2020-04-08 13:12:16 -0700
commita523b25ace66a0fa3f026ea03c4b6db87bc52ff8 (patch)
treefd8f7f32b039e9c122a4b2f95053f23de54f0b0b /encryption/utils.cpp
parent90c5119f3ade00e914c08f871684df9a6449a6f6 (diff)
downloadplatform_test_vts-testcase_kernel-a523b25ace66a0fa3f026ea03c4b6db87bc52ff8.tar.gz
platform_test_vts-testcase_kernel-a523b25ace66a0fa3f026ea03c4b6db87bc52ff8.tar.bz2
platform_test_vts-testcase_kernel-a523b25ace66a0fa3f026ea03c4b6db87bc52ff8.zip
VtsKernelEncryptionTest: add VerifyDataRandomness()
In preparation for adding randomness tests, add a function which checks whether a data buffer seems to be random. Test: 'atest vts_kernel_encryption_test' on cuttlefish and coral Bug: 151100202 Change-Id: I300ed26e845806f85098afd7322f0a95b64afa3c
Diffstat (limited to 'encryption/utils.cpp')
-rw-r--r--encryption/utils.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/encryption/utils.cpp b/encryption/utils.cpp
index f43bc49c..743a8fd2 100644
--- a/encryption/utils.cpp
+++ b/encryption/utils.cpp
@@ -16,6 +16,7 @@
// Utility functions for VtsKernelEncryptionTest.
+#include <LzmaLib.h>
#include <android-base/properties.h>
#include <errno.h>
#include <fstab/fstab.h>
@@ -81,5 +82,42 @@ bool FindRawPartition(const std::string &mountpoint,
return true;
}
+// Returns true if the given data seems to be random.
+//
+// Check compressibility rather than byte frequencies. Compressibility is a
+// stronger test since it also detects repetitions.
+//
+// To check compressibility, use LZMA rather than DEFLATE/zlib/gzip because LZMA
+// compression is stronger and supports a much larger dictionary. DEFLATE is
+// limited to a 32 KiB dictionary. So, data repeating after 32 KiB (or more)
+// would not be detected with DEFLATE. But LZMA can detect it.
+bool VerifyDataRandomness(const std::vector<uint8_t> &bytes) {
+ // To avoid flakiness, allow the data to be compressed a tiny bit by chance.
+ // There is at most a 2^-32 chance that random data can be compressed to be 4
+ // bytes shorter. In practice it's even lower due to compression overhead.
+ size_t destLen = bytes.size() - std::min<size_t>(4, bytes.size());
+ std::vector<uint8_t> dest(destLen);
+ uint8_t outProps[LZMA_PROPS_SIZE];
+ size_t outPropsSize = LZMA_PROPS_SIZE;
+ int ret;
+
+ ret = LzmaCompress(dest.data(), &destLen, bytes.data(), bytes.size(),
+ outProps, &outPropsSize,
+ 6, // compression level (0 <= level <= 9)
+ bytes.size(), // dictionary size
+ -1, -1, -1, -1, // lc, lp, bp, fb (-1 selects the default)
+ 1); // number of threads
+
+ if (ret == SZ_ERROR_OUTPUT_EOF) return true; // incompressible
+
+ if (ret == SZ_OK) {
+ ADD_FAILURE() << "Data is not random! Compressed " << bytes.size()
+ << " to " << destLen << " bytes";
+ } else {
+ ADD_FAILURE() << "LZMA compression error: ret=" << ret;
+ }
+ return false;
+}
+
} // namespace kernel
} // namespace android