tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

shake_unittest.cc (2932B)


      1 /*
      2 * shake_unittest.cc - unittests for SHAKE-128 and SHAKE-256 XOFs
      3 *
      4 * This Source Code Form is subject to the terms of the Mozilla Public
      5 * License, v. 2.0. If a copy of the MPL was not distributed with this
      6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      7 
      8 #include "blapi.h"
      9 #include "nspr.h"
     10 #include "nss.h"
     11 #include "secerr.h"
     12 
     13 #include <cstdlib>
     14 #include <iostream>
     15 #include <memory>
     16 
     17 #define GTEST_HAS_RTTI 0
     18 #include "gtest/gtest.h"
     19 
     20 template <class T>
     21 struct ScopedDelete128 {
     22  void operator()(T* ptr) {
     23    if (ptr) {
     24      SHAKE_128_DestroyContext(ptr, PR_TRUE);
     25    }
     26  }
     27 };
     28 
     29 template <class T>
     30 struct ScopedDelete256 {
     31  void operator()(T* ptr) {
     32    if (ptr) {
     33      SHAKE_256_DestroyContext(ptr, PR_TRUE);
     34    }
     35  }
     36 };
     37 
     38 typedef std::unique_ptr<SHAKE_128Context, ScopedDelete128<SHAKE_128Context>>
     39    ScopedSHAKE_128Context;
     40 
     41 typedef std::unique_ptr<SHAKE_256Context, ScopedDelete256<SHAKE_256Context>>
     42    ScopedSHAKE_256Context;
     43 
     44 class SHAKE_128Tests : public ::testing::Test {};
     45 class SHAKE_256Tests : public ::testing::Test {};
     46 
     47 TEST_F(SHAKE_128Tests, TestVector1) {
     48  ScopedSHAKE_128Context ctx(SHAKE_128_NewContext());
     49  ASSERT_TRUE(ctx) << "SHAKE_128_NewContext failed!";
     50 
     51  std::vector<uint8_t> digest(16);
     52  SECStatus rv = SHAKE_128_Hash(digest.data(), 16, "abc");
     53  std::vector<uint8_t> expected = {0x58, 0x81, 0x09, 0x2d, 0xd8, 0x18,
     54                                   0xbf, 0x5c, 0xf8, 0xa3, 0xdd, 0xb7,
     55                                   0x93, 0xfb, 0xcb, 0xa7};
     56  ASSERT_EQ(SECSuccess, rv);
     57  EXPECT_EQ(expected, digest);
     58 }
     59 
     60 TEST_F(SHAKE_128Tests, TestVector2) {
     61  ScopedSHAKE_128Context ctx(SHAKE_128_NewContext());
     62  ASSERT_TRUE(ctx) << "SHAKE_128_NewContext failed!";
     63 
     64  std::vector<uint8_t> digest(8);
     65  SECStatus rv = SHAKE_128_Hash(digest.data(), 8, "hello123");
     66  std::vector<uint8_t> expected = {0x1b, 0x85, 0x86, 0x15,
     67                                   0x10, 0xbc, 0x4d, 0x8e};
     68  ASSERT_EQ(SECSuccess, rv);
     69  EXPECT_EQ(expected, digest);
     70 }
     71 
     72 TEST_F(SHAKE_256Tests, TestVector1) {
     73  ScopedSHAKE_256Context ctx(SHAKE_256_NewContext());
     74  ASSERT_TRUE(ctx) << "SHAKE_256_NewContext failed!";
     75 
     76  std::vector<uint8_t> digest(16);
     77  SECStatus rv = SHAKE_256_Hash(digest.data(), 16, "abc");
     78  std::vector<uint8_t> expected = {0x48, 0x33, 0x66, 0x60, 0x13, 0x60,
     79                                   0xa8, 0x77, 0x1c, 0x68, 0x63, 0x08,
     80                                   0x0c, 0xc4, 0x11, 0x4d};
     81  ASSERT_EQ(SECSuccess, rv);
     82  EXPECT_EQ(expected, digest);
     83 }
     84 
     85 TEST_F(SHAKE_256Tests, TestVector2) {
     86  ScopedSHAKE_256Context ctx(SHAKE_256_NewContext());
     87  ASSERT_TRUE(ctx) << "SHAKE_256_NewContext failed!";
     88 
     89  std::vector<uint8_t> digest(8);
     90  SECStatus rv = SHAKE_256_Hash(digest.data(), 8, "hello123");
     91  std::vector<uint8_t> expected = {0xad, 0xe6, 0x12, 0xba,
     92                                   0x26, 0x5f, 0x92, 0xde};
     93  ASSERT_EQ(SECSuccess, rv);
     94  EXPECT_EQ(expected, digest);
     95 }