Electroneum
Loading...
Searching...
No Matches
crypto.cpp
Go to the documentation of this file.
1// Copyright (c) 2017-Present, Electroneum
2// Copyright (c) 2017-2019, The Monero Project
3//
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without modification, are
7// permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this list of
10// conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice, this list
13// of conditions and the following disclaimer in the documentation and/or other
14// materials provided with the distribution.
15//
16// 3. Neither the name of the copyright holder nor the names of its contributors may be
17// used to endorse or promote products derived from this software without specific
18// prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#include <cstdint>
31#include <gtest/gtest.h>
32#include <memory>
33#include <sstream>
34#include <string>
35
37
38namespace
39{
40 static constexpr const std::uint8_t source[] = {
41 0x8b, 0x65, 0x59, 0x70, 0x15, 0x37, 0x99, 0xaf, 0x2a, 0xea, 0xdc, 0x9f, 0xf1, 0xad, 0xd0, 0xea,
42 0x6c, 0x72, 0x51, 0xd5, 0x41, 0x54, 0xcf, 0xa9, 0x2c, 0x17, 0x3a, 0x0d, 0xd3, 0x9c, 0x1f, 0x94,
43 0x6c, 0x72, 0x51, 0xd5, 0x41, 0x54, 0xcf, 0xa9, 0x2c, 0x17, 0x3a, 0x0d, 0xd3, 0x9c, 0x1f, 0x94,
44 0x8b, 0x65, 0x59, 0x70, 0x15, 0x37, 0x99, 0xaf, 0x2a, 0xea, 0xdc, 0x9f, 0xf1, 0xad, 0xd0, 0xea
45 };
46
47 static constexpr const char expected[] =
48 "8b655970153799af2aeadc9ff1add0ea6c7251d54154cfa92c173a0dd39c1f94"
49 "6c7251d54154cfa92c173a0dd39c1f948b655970153799af2aeadc9ff1add0ea";
50
51 template<typename T> void *addressof(T &t) { return &t; }
52 template<> void *addressof(crypto::secret_key &k) { return addressof(unwrap(unwrap(k))); }
53
54 template<typename T>
55 bool is_formatted()
56 {
57 T value{};
58
59 static_assert(alignof(T) == 1, "T must have 1 byte alignment");
60 static_assert(sizeof(T) <= sizeof(source), "T is too large for source");
61 static_assert(sizeof(T) * 2 <= sizeof(expected), "T is too large for destination");
62 std::memcpy(addressof(value), source, sizeof(T));
63
64 std::stringstream out;
65 out << "BEGIN" << value << "END";
66 return out.str() == "BEGIN<" + std::string{expected, sizeof(T) * 2} + ">END";
67 }
68}
69
70TEST(Crypto, Ostream)
71{
72 EXPECT_TRUE(is_formatted<crypto::hash8>());
73 EXPECT_TRUE(is_formatted<crypto::hash>());
74 EXPECT_TRUE(is_formatted<crypto::public_key>());
75 EXPECT_TRUE(is_formatted<crypto::secret_key>());
76 EXPECT_TRUE(is_formatted<crypto::signature>());
77 EXPECT_TRUE(is_formatted<crypto::key_derivation>());
78 EXPECT_TRUE(is_formatted<crypto::key_image>());
79}
80
81TEST(Crypto, null_keys)
82{
83 char zero[32];
84 memset(zero, 0, 32);
85 ASSERT_EQ(memcmp(crypto::null_skey.data, zero, 32), 0);
86 ASSERT_EQ(memcmp(crypto::null_pkey.data, zero, 32), 0);
87}
88
89TEST(Crypto, verify_32)
90{
91 // all bytes are treated the same, so we can brute force just one byte
92 unsigned char k0[32] = {0}, k1[32] = {0};
93 for (unsigned int i0 = 0; i0 < 256; ++i0)
94 {
95 k0[0] = i0;
96 for (unsigned int i1 = 0; i1 < 256; ++i1)
97 {
98 k1[0] = i1;
99 ASSERT_EQ(!crypto_verify_32(k0, k1), i0 == i1);
100 }
101 }
102}
int crypto_verify_32(const unsigned char *, const unsigned char *)
#define ASSERT_EQ(val1, val2)
Definition gtest.h:1956
#define EXPECT_TRUE(condition)
Definition gtest.h:1859
#define TEST(test_case_name, test_name)
Definition gtest.h:2187
const crypto::public_key null_pkey
Definition crypto.cpp:72
const crypto::secret_key null_skey
Definition crypto.cpp:73
epee::mlocked< tools::scrubbed< ec_scalar > > secret_key
Definition crypto.h:82
T & unwrap(mlocked< T > &src)
Definition mlocker.h:80
const GenericPointer< typename T::ValueType > T2 value
Definition pointer.h:1225
const CharType(& source)[N]
Definition pointer.h:1147
#define T(x)