From cadb4711bdc9cb99113aad2acdbc47711f065626 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Tue, 2 May 2017 18:24:51 -0700 Subject: Add a unit test for CRYPT_ArcFourSetup(). BUG=chromium:686128 Change-Id: Iad3ebbfd304dc145af2e694818864c4d25ccf99a Reviewed-on: https://pdfium-review.googlesource.com/4793 Commit-Queue: Lei Zhang Reviewed-by: Tom Sepez --- core/fdrm/crypto/fx_crypt.cpp | 4 +-- core/fdrm/crypto/fx_crypt.h | 23 ++++++------- core/fdrm/crypto/fx_crypt_unittest.cpp | 60 ++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/core/fdrm/crypto/fx_crypt.cpp b/core/fdrm/crypto/fx_crypt.cpp index f4ead91d33..f5ebdf09d2 100644 --- a/core/fdrm/crypto/fx_crypt.cpp +++ b/core/fdrm/crypto/fx_crypt.cpp @@ -143,11 +143,11 @@ void CRYPT_ArcFourSetup(CRYPT_rc4_context* s, uint32_t length) { s->x = 0; s->y = 0; - for (int i = 0; i < 256; ++i) + for (int i = 0; i < kRC4ContextPermutationLength; ++i) s->m[i] = i; int j = 0; - for (int i = 0; i < 256; ++i) { + for (int i = 0; i < kRC4ContextPermutationLength; ++i) { j = (j + s->m[i] + (length ? key[i % length] : 0)) & 0xFF; std::swap(s->m[i], s->m[j]); } diff --git a/core/fdrm/crypto/fx_crypt.h b/core/fdrm/crypto/fx_crypt.h index cfbe119ddb..2b127948f0 100644 --- a/core/fdrm/crypto/fx_crypt.h +++ b/core/fdrm/crypto/fx_crypt.h @@ -9,37 +9,38 @@ #include "core/fxcrt/fx_system.h" -typedef struct { +constexpr int32_t kRC4ContextPermutationLength = 256; +struct CRYPT_rc4_context { int32_t x; int32_t y; - int32_t m[256]; -} CRYPT_rc4_context; + int32_t m[kRC4ContextPermutationLength]; +}; -typedef struct { +struct CRYPT_md5_context { uint32_t total[2]; uint32_t state[4]; uint8_t buffer[64]; -} CRYPT_md5_context; +}; -typedef struct { +struct CRYPT_sha1_context { unsigned int h[5]; unsigned char block[64]; int blkused; unsigned int lenhi; unsigned int lenlo; -} CRYPT_sha1_context; +}; -typedef struct { +struct CRYPT_sha256_context { uint32_t total[2]; uint32_t state[8]; uint8_t buffer[64]; -} CRYPT_sha256_context; +}; -typedef struct { +struct CRYPT_sha384_context { uint64_t total[2]; uint64_t state[8]; uint8_t buffer[128]; -} CRYPT_sha384_context; +}; void CRYPT_ArcFourCryptBlock(uint8_t* data, uint32_t size, diff --git a/core/fdrm/crypto/fx_crypt_unittest.cpp b/core/fdrm/crypto/fx_crypt_unittest.cpp index f5fb457d3d..247da07233 100644 --- a/core/fdrm/crypto/fx_crypt_unittest.cpp +++ b/core/fdrm/crypto/fx_crypt_unittest.cpp @@ -214,3 +214,63 @@ TEST(FXCRYPT, Sha256TestB2) { for (size_t i = 0; i < 32; ++i) EXPECT_EQ(expected[i], actual[i]) << " at byte " << i; } + +TEST(FXCRYPT, CRYPT_ArcFourSetup) { + { + const uint8_t kNullPermutation[kRC4ContextPermutationLength] = { + 0, 35, 3, 43, 9, 11, 65, 229, 32, 36, 134, 98, 59, 34, + 173, 153, 214, 200, 64, 161, 191, 62, 6, 25, 56, 234, 49, 246, + 69, 133, 203, 194, 10, 42, 228, 198, 195, 245, 236, 91, 206, 23, + 235, 27, 138, 18, 143, 250, 244, 76, 123, 217, 132, 249, 72, 127, + 94, 151, 33, 60, 248, 85, 177, 210, 142, 83, 110, 140, 41, 135, + 196, 238, 156, 242, 141, 67, 5, 185, 131, 63, 137, 37, 172, 121, + 70, 144, 237, 130, 17, 44, 253, 166, 78, 201, 12, 119, 215, 7, + 126, 114, 97, 192, 53, 4, 254, 45, 102, 122, 230, 88, 193, 129, + 160, 124, 84, 108, 239, 189, 152, 120, 115, 207, 50, 176, 86, 157, + 164, 187, 71, 1, 15, 58, 29, 21, 46, 145, 247, 162, 95, 183, + 13, 226, 159, 175, 221, 100, 96, 202, 101, 178, 154, 47, 205, 106, + 148, 104, 93, 112, 26, 165, 128, 186, 146, 218, 66, 211, 171, 90, + 252, 19, 40, 99, 223, 174, 255, 51, 77, 227, 48, 220, 168, 118, + 224, 103, 75, 105, 125, 199, 73, 82, 57, 181, 81, 149, 68, 52, + 232, 22, 2, 216, 113, 30, 109, 163, 92, 61, 14, 8, 38, 225, + 79, 231, 170, 240, 20, 219, 204, 150, 180, 188, 116, 190, 241, 197, + 179, 87, 74, 147, 80, 54, 212, 16, 167, 222, 136, 213, 55, 182, + 139, 24, 209, 251, 208, 28, 111, 89, 158, 155, 243, 107, 233, 169, + 117, 184, 31, 39}; + CRYPT_rc4_context s; + CRYPT_ArcFourSetup(&s, nullptr, 0); + EXPECT_EQ(0, s.x); + EXPECT_EQ(0, s.y); + for (int32_t i = 0; i < kRC4ContextPermutationLength; ++i) + EXPECT_EQ(kNullPermutation[i], s.m[i]) << i; + } + { + const uint8_t kFoobarPermutation[kRC4ContextPermutationLength] = { + 102, 214, 39, 49, 17, 132, 244, 106, 114, 76, 183, 212, 116, 73, + 42, 103, 128, 246, 139, 199, 31, 234, 25, 109, 48, 19, 121, 4, + 20, 54, 134, 77, 163, 38, 61, 101, 145, 78, 215, 96, 92, 80, + 224, 168, 243, 210, 82, 252, 113, 56, 217, 62, 218, 129, 125, 33, + 99, 9, 153, 59, 43, 13, 206, 124, 131, 18, 213, 118, 173, 122, + 193, 172, 177, 105, 148, 207, 186, 5, 85, 32, 68, 220, 79, 84, + 169, 209, 150, 7, 133, 63, 147, 93, 26, 130, 60, 117, 250, 57, + 24, 247, 200, 127, 136, 66, 112, 107, 140, 154, 70, 170, 185, 138, + 248, 236, 88, 86, 44, 216, 241, 35, 100, 151, 156, 74, 119, 55, + 245, 46, 227, 208, 229, 16, 249, 149, 53, 157, 201, 75, 58, 28, + 142, 238, 182, 180, 179, 144, 12, 6, 176, 10, 90, 239, 104, 40, + 181, 194, 137, 69, 221, 205, 165, 188, 191, 87, 1, 91, 2, 171, + 232, 34, 162, 166, 160, 126, 225, 167, 123, 197, 223, 195, 22, 203, + 189, 237, 37, 27, 222, 175, 23, 143, 152, 192, 21, 231, 228, 141, + 30, 204, 158, 240, 120, 98, 89, 83, 135, 251, 81, 196, 161, 3, + 8, 230, 52, 219, 41, 242, 36, 97, 15, 155, 65, 187, 254, 64, + 159, 67, 211, 108, 178, 146, 202, 11, 164, 226, 184, 50, 190, 174, + 71, 233, 235, 198, 95, 51, 110, 255, 253, 72, 115, 0, 47, 94, + 29, 45, 14, 111}; + CRYPT_rc4_context s; + const uint8_t kFooBar[] = "foobar"; + CRYPT_ArcFourSetup(&s, kFooBar, 6); + EXPECT_EQ(0, s.x); + EXPECT_EQ(0, s.y); + for (int32_t i = 0; i < kRC4ContextPermutationLength; ++i) + EXPECT_EQ(kFoobarPermutation[i], s.m[i]) << i; + } +} -- cgit v1.2.3