From f5cabbfc3085a7a7a3451452c1a7ebe1f19c1223 Mon Sep 17 00:00:00 2001 From: tsepez Date: Mon, 21 Nov 2016 12:08:22 -0800 Subject: Add unit test for fdrm's MD5 Review-Url: https://codereview.chromium.org/2517153003 --- core/fdrm/crypto/fx_crypt.cpp | 15 +-- core/fdrm/crypto/fx_crypt.h | 6 + core/fdrm/crypto/fx_crypt_unittest.cpp | 200 +++++++++++++++++++++++++++++++++ 3 files changed, 211 insertions(+), 10 deletions(-) create mode 100644 core/fdrm/crypto/fx_crypt_unittest.cpp (limited to 'core/fdrm/crypto') diff --git a/core/fdrm/crypto/fx_crypt.cpp b/core/fdrm/crypto/fx_crypt.cpp index 12b955d93d..326ec8c83e 100644 --- a/core/fdrm/crypto/fx_crypt.cpp +++ b/core/fdrm/crypto/fx_crypt.cpp @@ -57,11 +57,6 @@ void CRYPT_ArcFourCryptBlock(uint8_t* pData, CRYPT_ArcFourSetup(&s, key, keylen); CRYPT_ArcFourCrypt(&s, pData, size); } -struct md5_context { - uint32_t total[2]; - uint32_t state[4]; - uint8_t buffer[64]; -}; #define GET_UINT32(n, b, i) \ { \ (n) = (uint32_t)((uint8_t*)b)[(i)] | \ @@ -76,7 +71,7 @@ struct md5_context { (((uint8_t*)b)[(i) + 2]) = (uint8_t)(((n) >> 16) & 0xFF); \ (((uint8_t*)b)[(i) + 3]) = (uint8_t)(((n) >> 24) & 0xFF); \ } -void md5_process(struct md5_context* ctx, const uint8_t data[64]) { +void md5_process(struct CRYPT_md5_context* ctx, const uint8_t data[64]) { uint32_t A, B, C, D, X[16]; GET_UINT32(X[0], data, 0); GET_UINT32(X[1], data, 4); @@ -182,7 +177,7 @@ void md5_process(struct md5_context* ctx, const uint8_t data[64]) { ctx->state[3] += D; } void CRYPT_MD5Start(void* context) { - struct md5_context* ctx = (struct md5_context*)context; + struct CRYPT_md5_context* ctx = (struct CRYPT_md5_context*)context; ctx->total[0] = 0; ctx->total[1] = 0; ctx->state[0] = 0x67452301; @@ -191,7 +186,7 @@ void CRYPT_MD5Start(void* context) { ctx->state[3] = 0x10325476; } void CRYPT_MD5Update(void* pctx, const uint8_t* input, uint32_t length) { - struct md5_context* ctx = (struct md5_context*)pctx; + struct CRYPT_md5_context* ctx = (struct CRYPT_md5_context*)pctx; uint32_t left, fill; if (!length) { return; @@ -223,7 +218,7 @@ const uint8_t md5_padding[64] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; void CRYPT_MD5Finish(void* pctx, uint8_t digest[16]) { - struct md5_context* ctx = (struct md5_context*)pctx; + struct CRYPT_md5_context* ctx = (struct CRYPT_md5_context*)pctx; uint32_t last, padn; uint8_t msglen[8]; PUT_UINT32(ctx->total[0], msglen, 0); @@ -240,7 +235,7 @@ void CRYPT_MD5Finish(void* pctx, uint8_t digest[16]) { void CRYPT_MD5Generate(const uint8_t* input, uint32_t length, uint8_t digest[16]) { - md5_context ctx; + CRYPT_md5_context ctx; CRYPT_MD5Start(&ctx); CRYPT_MD5Update(&ctx, input, length); CRYPT_MD5Finish(&ctx, digest); diff --git a/core/fdrm/crypto/fx_crypt.h b/core/fdrm/crypto/fx_crypt.h index 24650bec9a..3ee2e4bdbe 100644 --- a/core/fdrm/crypto/fx_crypt.h +++ b/core/fdrm/crypto/fx_crypt.h @@ -13,6 +13,12 @@ extern "C" { #endif +struct CRYPT_md5_context { + uint32_t total[2]; + uint32_t state[4]; + uint8_t buffer[64]; +}; + void CRYPT_ArcFourCryptBlock(uint8_t* data, uint32_t size, const uint8_t* key, diff --git a/core/fdrm/crypto/fx_crypt_unittest.cpp b/core/fdrm/crypto/fx_crypt_unittest.cpp new file mode 100644 index 0000000000..6db43fb54a --- /dev/null +++ b/core/fdrm/crypto/fx_crypt_unittest.cpp @@ -0,0 +1,200 @@ +// Copyright (c) 2016 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. + +// Originally from chromium's /src/base/md5_unittest.cc. + +#include "core/fdrm/crypto/fx_crypt.h" + +#include +#include + +#include "core/fxcrt/fx_basic.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +std::string CRYPT_ToBase16(const uint8_t* digest) { + static char const zEncode[] = "0123456789abcdef"; + std::string ret; + ret.resize(32); + for (int i = 0, j = 0; i < 16; i++, j += 2) { + uint8_t a = digest[i]; + ret[j] = zEncode[(a >> 4) & 0xf]; + ret[j + 1] = zEncode[a & 0xf]; + } + return ret; +} + +std::string CRYPT_MD5String(const char* str) { + uint8_t digest[16]; + CRYPT_MD5Generate(reinterpret_cast(str), strlen(str), digest); + return CRYPT_ToBase16(digest); +} + +} // namespace + +TEST(FXCRYPT, CRYPT_ToBase16) { + uint8_t data[] = {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, + 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e}; + + std::string actual = CRYPT_ToBase16(data); + std::string expected = "d41d8cd98f00b204e9800998ecf8427e"; + + EXPECT_EQ(expected, actual); +} + +TEST(FXCRYPT, MD5GenerateEmtpyData) { + uint8_t digest[16]; + const char data[] = ""; + uint32_t length = static_cast(strlen(data)); + + CRYPT_MD5Generate(reinterpret_cast(data), length, digest); + + uint8_t expected[] = {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, + 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e}; + + for (int i = 0; i < 16; ++i) + EXPECT_EQ(expected[i], digest[i]); +} + +TEST(FXCRYPT, MD5GenerateOneByteData) { + uint8_t digest[16]; + const char data[] = "a"; + uint32_t length = static_cast(strlen(data)); + + CRYPT_MD5Generate(reinterpret_cast(data), length, digest); + + uint8_t expected[] = {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8, + 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61}; + + for (int i = 0; i < 16; ++i) + EXPECT_EQ(expected[i], digest[i]); +} + +TEST(FXCRYPT, MD5GenerateLongData) { + const uint32_t length = 10 * 1024 * 1024 + 1; + std::unique_ptr data(new char[length]); + + for (uint32_t i = 0; i < length; ++i) + data[i] = i & 0xFF; + + uint8_t digest[16]; + CRYPT_MD5Generate(reinterpret_cast(data.get()), length, + digest); + + uint8_t expected[] = {0x90, 0xbd, 0x6a, 0xd9, 0x0a, 0xce, 0xf5, 0xad, + 0xaa, 0x92, 0x20, 0x3e, 0x21, 0xc7, 0xa1, 0x3e}; + + for (int i = 0; i < 16; ++i) + EXPECT_EQ(expected[i], digest[i]); +} + +TEST(FXCRYPT, ContextWithEmptyData) { + CRYPT_md5_context ctx; + CRYPT_MD5Start(&ctx); + + uint8_t digest[16]; + CRYPT_MD5Finish(&ctx, digest); + + uint8_t expected[] = {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, + 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e}; + + for (int i = 0; i < 16; ++i) + EXPECT_EQ(expected[i], digest[i]); +} + +TEST(FXCRYPT, ContextWithLongData) { + CRYPT_md5_context ctx; + CRYPT_MD5Start(&ctx); + + const uint32_t length = 10 * 1024 * 1024 + 1; + std::unique_ptr data(new uint8_t[length]); + + for (uint32_t i = 0; i < length; ++i) + data[i] = i & 0xFF; + + uint32_t total = 0; + while (total < length) { + uint32_t len = 4097; // intentionally not 2^k. + if (len > length - total) + len = length - total; + + CRYPT_MD5Update(&ctx, data.get() + total, len); + total += len; + } + + EXPECT_EQ(length, total); + + uint8_t digest[16]; + CRYPT_MD5Finish(&ctx, digest); + + uint8_t expected[] = {0x90, 0xbd, 0x6a, 0xd9, 0x0a, 0xce, 0xf5, 0xad, + 0xaa, 0x92, 0x20, 0x3e, 0x21, 0xc7, 0xa1, 0x3e}; + + for (int i = 0; i < 16; ++i) + EXPECT_EQ(expected[i], digest[i]); +} + +// Example data from http://www.ietf.org/rfc/rfc1321.txt A.5 Test Suite +TEST(FXCRYPT, MD5StringTestSuite1) { + std::string actual = CRYPT_MD5String(""); + std::string expected = "d41d8cd98f00b204e9800998ecf8427e"; + EXPECT_EQ(expected, actual); +} + +TEST(FXCRYPT, MD5StringTestSuite2) { + std::string actual = CRYPT_MD5String("a"); + std::string expected = "0cc175b9c0f1b6a831c399e269772661"; + EXPECT_EQ(expected, actual); +} + +TEST(FXCRYPT, MD5StringTestSuite3) { + std::string actual = CRYPT_MD5String("abc"); + std::string expected = "900150983cd24fb0d6963f7d28e17f72"; + EXPECT_EQ(expected, actual); +} + +TEST(FXCRYPT, MD5StringTestSuite4) { + std::string actual = CRYPT_MD5String("message digest"); + std::string expected = "f96b697d7cb7938d525a2f31aaf161d0"; + EXPECT_EQ(expected, actual); +} + +TEST(FXCRYPT, MD5StringTestSuite5) { + std::string actual = CRYPT_MD5String("abcdefghijklmnopqrstuvwxyz"); + std::string expected = "c3fcd3d76192e4007dfb496cca67e13b"; + EXPECT_EQ(expected, actual); +} + +TEST(FXCRYPT, MD5StringTestSuite6) { + std::string actual = CRYPT_MD5String( + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789"); + std::string expected = "d174ab98d277d9f5a5611c2c9f419d9f"; + EXPECT_EQ(expected, actual); +} + +TEST(FXCRYPT, MD5StringTestSuite7) { + std::string actual = CRYPT_MD5String( + "12345678901234567890" + "12345678901234567890" + "12345678901234567890" + "12345678901234567890"); + std::string expected = "57edf4a22be3c955ac49da2e2107b67a"; + EXPECT_EQ(expected, actual); +} + +TEST(FXCRYPT, ContextWithStringData) { + CRYPT_md5_context ctx; + CRYPT_MD5Start(&ctx); + CRYPT_MD5Update(&ctx, reinterpret_cast("abc"), 3); + + uint8_t digest[16]; + CRYPT_MD5Finish(&ctx, digest); + + std::string actual = CRYPT_ToBase16(digest); + std::string expected = "900150983cd24fb0d6963f7d28e17f72"; + EXPECT_EQ(expected, actual); +} -- cgit v1.2.3