| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <gtest/gtest.h> |
| 16 |
| 17 | #include <openssl/des.h> |
| 18 | #include <openssl/span.h> |
| 19 |
| 20 | #include "../../crypto/test/test_util.h" |
| 21 |
| 22 |
| 23 | // DES-CFB tests from OpenSSL. OpenSSL has no test vectors for 3DES-CFB at all. |
| 24 | // Instead, we repurpose those tests to cover 3DES-CFB by running the inputs |
| 25 | // through three times. |
| 26 | static const DES_cblock cfb_key = { |
| 27 | {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}}; |
| 28 | static const DES_cblock cfb_iv = { |
| 29 | {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}}; |
| 30 | static const uint8_t plain[24] = { |
| 31 | 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, |
| 32 | 0x69, 0x6d, 0x65, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20}; |
| 33 | static const uint8_t cfb_cipher8[24] = { |
| 34 | 0xf3, 0x1f, 0xda, 0x07, 0x01, 0x14, 0x62, 0xee, 0x18, 0x7f, 0x43, 0xd8, |
| 35 | 0x0a, 0x7c, 0xd9, 0xb5, 0xb0, 0xd2, 0x90, 0xda, 0x6e, 0x5b, 0x9a, 0x87}; |
| 36 | static const uint8_t cfb_cipher16[24] = { |
| 37 | 0xf3, 0x09, 0x87, 0x87, 0x7f, 0x57, 0xf7, 0x3c, 0x36, 0xb6, 0xdb, 0x70, |
| 38 | 0xd8, 0xd5, 0x34, 0x19, 0xd3, 0x86, 0xb2, 0x23, 0xb7, 0xb2, 0xad, 0x1b}; |
| 39 | static const uint8_t cfb_cipher32[24] = { |
| 40 | 0xf3, 0x09, 0x62, 0x49, 0xa4, 0xdf, 0xa4, 0x9f, 0x33, 0xdc, 0x7b, 0xad, |
| 41 | 0x4c, 0xc8, 0x9f, 0x64, 0xe4, 0x53, 0xe5, 0xec, 0x67, 0x20, 0xda, 0xb6}; |
| 42 | static const uint8_t cfb_cipher48[24] = { |
| 43 | 0xf3, 0x09, 0x62, 0x49, 0xc7, 0xf4, 0x30, 0xb5, 0x15, 0xec, 0xbb, 0x85, |
| 44 | 0x97, 0x5a, 0x13, 0x8c, 0x68, 0x60, 0xe2, 0x38, 0x34, 0x3c, 0xdc, 0x1f}; |
| 45 | static const uint8_t cfb_cipher64[24] = { |
| 46 | 0xf3, 0x09, 0x62, 0x49, 0xc7, 0xf4, 0x6e, 0x51, 0xa6, 0x9e, 0x83, 0x9b, |
| 47 | 0x1a, 0x92, 0xf7, 0x84, 0x03, 0x46, 0x71, 0x33, 0x89, 0x8e, 0xa6, 0x22}; |
| 48 |
| 49 | // Unlike the above test vectors, this test vector was computed by running the |
| 50 | // existing implementation and saving the output. OpenSSL lacks tests for this |
| 51 | // function, but also implements an incorrect construction in its low-level |
| 52 | // APIs. As a result, importing a standard test vector would only test 1/8 of |
| 53 | // the output. See discussion in the test. |
| 54 | static const uint8_t cfb_cipher1[24] = { |
| 55 | 0xf3, 0x27, 0xff, 0x2d, 0x80, 0xee, 0x12, 0xbe, 0xb6, 0x74, 0xa3, 0xb4, |
| 56 | 0xd6, 0xfb, 0x5d, 0x0d, 0x49, 0x18, 0x84, 0xed, 0xfe, 0xca, 0x17, 0x5f}; |
| 57 |
| 58 | TEST(DESTest, CFB) { |
| 59 | DES_key_schedule ks; |
| 60 | DES_set_key(&cfb_key, &ks); |
| 61 |
| 62 | struct { |
| 63 | int numbits; |
| 64 | const uint8_t (&ciphertext)[24]; |
| 65 | } kTests[] = { |
| 66 | {1, cfb_cipher1}, {8, cfb_cipher8}, {16, cfb_cipher16}, |
| 67 | {32, cfb_cipher32}, {48, cfb_cipher48}, {64, cfb_cipher64}, |
| 68 | }; |
| 69 | for (const auto &t : kTests) { |
| 70 | SCOPED_TRACE(t.numbits); |
| 71 |
| 72 | // |DES_ede3_cfb_encrypt| only supports streaming at segment boundaries. |
| 73 | // Segments, however, are measured in bits, not bytes. When the segment is |
| 74 | // not a whole number of bytes, OpenSSL's low-level functions do not |
| 75 | // implement CFB correctly. CFB-n ultimately computes a sequence of E(I_i) |
| 76 | // blocks, extracts n bits from each block to XOR into the next n bits of |
| 77 | // plaintext. OpenSSL computes the correct sequence of blocks, but then |
| 78 | // rounds n up to a byte boundary when consuming input. |
| 79 | // |
| 80 | // It essentially interprets CFB-1 as a funny CFB-8, with the wrong amount |
| 81 | // of cipher feedback. To get the real CFB-1 out of OpenSSL's CFB-1, you put |
| 82 | // each plaintext bit as into its byte, with bit at the MSB, then mask off |
| 83 | // all but the MSB of each ciphertext byte. OpenSSL's |EVP_des_ede3_cfb1| |
| 84 | // does this transformation internally, to work around this bug. |
| 85 | // |
| 86 | // In case anyone is relying on the remaining bits, we test all the output |
| 87 | // bits of the OpenSSL version. However, for such callers, it is unclear if |
| 88 | // this version has been sufficiently analyzed. |
| 89 | size_t offset = (t.numbits + 7) / 8; |
| 90 | for (size_t split = 0; split < sizeof(plain); split += offset) { |
| 91 | SCOPED_TRACE(split); |
| 92 | uint8_t out[sizeof(plain)]; |
| 93 | DES_cblock iv = cfb_iv; |
| 94 | DES_ede3_cfb_encrypt(plain, out, t.numbits, split, &ks, &ks, &ks, &iv, |
| 95 | DES_ENCRYPT); |
| 96 | DES_ede3_cfb_encrypt(plain + split, out + split, t.numbits, |
| 97 | sizeof(plain) - split, &ks, &ks, &ks, &iv, |
| 98 | DES_ENCRYPT); |
| 99 | EXPECT_EQ(Bytes(out), Bytes(t.ciphertext)); |
| 100 |
| 101 | iv = cfb_iv; |
| 102 | DES_ede3_cfb_encrypt(t.ciphertext, out, t.numbits, split, &ks, &ks, &ks, |
| 103 | &iv, DES_DECRYPT); |
| 104 | DES_ede3_cfb_encrypt(t.ciphertext + split, out + split, t.numbits, |
| 105 | sizeof(plain) - split, &ks, &ks, &ks, &iv, |
| 106 | DES_DECRYPT); |
| 107 | EXPECT_EQ(Bytes(out), Bytes(plain)); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 |
| 112 | TEST(DESTest, CFB64) { |
| 113 | DES_key_schedule ks; |
| 114 | DES_set_key(&cfb_key, &ks); |
| 115 |
| 116 | // Unlike the generic CFB API, the CFB64 API can be split within a block |
| 117 | // boundary. |
| 118 | for (size_t split = 0; split <= sizeof(plain); split++) { |
| 119 | SCOPED_TRACE(split); |
| 120 | uint8_t out[sizeof(plain)]; |
| 121 | DES_cblock iv = cfb_iv; |
| 122 | int n = 0; |
| 123 | DES_ede3_cfb64_encrypt(plain, out, split, &ks, &ks, &ks, &iv, &n, |
| 124 | DES_ENCRYPT); |
| 125 | DES_ede3_cfb64_encrypt(plain + split, out + split, sizeof(plain) - split, |
| 126 | &ks, &ks, &ks, &iv, &n, DES_ENCRYPT); |
| 127 | EXPECT_EQ(Bytes(out), Bytes(cfb_cipher64)); |
| 128 |
| 129 | n = 0; |
| 130 | iv = cfb_iv; |
| 131 | DES_ede3_cfb64_encrypt(cfb_cipher64, out, split, &ks, &ks, &ks, &iv, &n, |
| 132 | DES_DECRYPT); |
| 133 | DES_ede3_cfb64_encrypt(cfb_cipher64 + split, out + split, |
| 134 | sizeof(cfb_cipher64) - split, &ks, &ks, &ks, &iv, &n, |
| 135 | DES_DECRYPT); |
| 136 | EXPECT_EQ(Bytes(out), Bytes(plain)); |
| 137 | } |
| 138 | } |
| 139 |