• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "crypto/aes_ctr.h"
6 
7 #include "testing/gtest/include/gtest/gtest.h"
8 
TEST(AesCtrTests,KnownAnswers)9 TEST(AesCtrTests, KnownAnswers) {
10   // To reproduce this test case, because openssl doesn't have a builtin
11   // aes-128-ctr mode:
12   //   echo [counter hex] | xxd -r -p | openssl aes-128-ecb -K [key hex]
13   // That emits a block of key stream, which you can xor with plaintext to
14   // produce a sample ciphertext.
15   // clang-format off
16   constexpr auto kKey = std::to_array<uint8_t>({
17     0xff, 0x00, 0xee, 0x11, 0xdd, 0x22, 0xcc, 0x33,
18     0xbb, 0x44, 0xaa, 0x55, 0x99, 0x66, 0x88, 0x77,
19   });
20   constexpr auto kCounter = std::to_array<uint8_t>({
21     0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
22     0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
23   });
24   constexpr auto kPlaintext = std::to_array<uint8_t>({
25     0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
26   });
27   constexpr auto kExpectedCiphertext = std::to_array<uint8_t>({
28     0xd7, 0x07, 0x48, 0x4f, 0x58,
29   });
30   // clang-format on
31   std::array<uint8_t, std::size(kPlaintext)> ciphertext;
32   std::array<uint8_t, std::size(kPlaintext)> plaintext;
33 
34   crypto::aes_ctr::Encrypt(kKey, kCounter, kPlaintext, ciphertext);
35   crypto::aes_ctr::Decrypt(kKey, kCounter, ciphertext, plaintext);
36 
37   EXPECT_EQ(kPlaintext, plaintext);
38   EXPECT_EQ(kExpectedCiphertext, ciphertext);
39 }
40