Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/des/des_test.cc

raw
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0 OR ISC
3
4 #include <openssl/des.h>
5
6 #include <gtest/gtest.h>
7
8 TEST(DESTest, WeakKeys) {
9 // The all 2 key is not weak and has odd parity
10 DES_cblock validKey = {{2, 2, 2, 2, 2, 2, 2, 2}};
11 EXPECT_FALSE(DES_is_weak_key(&validKey));
12 DES_key_schedule des;
13 EXPECT_EQ(0, DES_set_key(&validKey, &des));
14
15 // Weak key example from SP 800-67r2 section 3.3.2
16 static const DES_cblock weakKey = {{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}};
17 EXPECT_TRUE(DES_is_weak_key(&weakKey));
18 EXPECT_EQ(-2, DES_set_key(&weakKey, &des));
19 }
20
21 // If it wasn't for MSVC this could be __builtin_popcount, if this was C++20
22 // it could be std::popcount
23 static int countSetBits(uint8_t n) {
24 int count = 0;
25 while (n) {
26 count += n & 1;
27 n >>= 1;
28 }
29 return count;
30 }
31
32 TEST(DESTest, Parity) {
33 // The all 2 key is not weak and has odd parity for each byte
34 DES_cblock key = {{2, 2, 2, 2, 2, 2, 2, 2}};
35 DES_key_schedule des;
36 int result = DES_set_key(&key, &des);
37 EXPECT_EQ(result, 0);
38
39 for (uint8_t i = 0; i < 255; i++) {
40 key.bytes[0] = i;
41 result = DES_set_key(&key, &des);
42 int bitsSet = countSetBits(i);
43 int oddParity = bitsSet % 2 == 1;
44 if (oddParity) {
45 EXPECT_EQ(result, 0);
46 } else {
47 EXPECT_EQ(result, -1);
48 }
49 }
50 }