1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#include <string>
#include "gtest/gtest.h"
#include "ScopedNSSTypes.h"
#include "mozilla/gtest/MozAssertions.h"
#include "mozilla/Span.h"
#include "nss.h"
#include "secoidt.h"
// From RFC 2202
const unsigned char kTestKey[] = "Jefe";
const unsigned char kTestInput[] = "what do ya want for nothing?";
struct HMACTestCase {
SECOidTag hashAlg;
std::basic_string<uint8_t> expectedOutput;
};
#define EXPECTED_RESULT(val) \
std::basic_string<uint8_t>(reinterpret_cast<const uint8_t*>(val), \
sizeof(val) - 1)
static const HMACTestCase HMACTestCases[] = {
{
SEC_OID_MD5,
EXPECTED_RESULT(
"\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38"),
},
{
SEC_OID_SHA256,
EXPECTED_RESULT(
"\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75\xc7"
"\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec\x38\x43"),
},
};
#undef EXPECTED_RESULT
class psm_HMAC : public ::testing::Test,
public ::testing::WithParamInterface<HMACTestCase> {
public:
void SetUp() override { NSS_NoDB_Init(nullptr); }
};
TEST_P(psm_HMAC, Test) {
mozilla::HMAC hmac;
const HMACTestCase& testCase(GetParam());
nsresult rv = hmac.Begin(testCase.hashAlg,
mozilla::Span(kTestKey, sizeof(kTestKey) - 1));
ASSERT_NS_SUCCEEDED(rv);
rv = hmac.Update(reinterpret_cast<const unsigned char*>(kTestInput),
sizeof(kTestInput) - 1);
ASSERT_NS_SUCCEEDED(rv);
nsTArray<uint8_t> output;
rv = hmac.End(output);
ASSERT_NS_SUCCEEDED(rv);
EXPECT_EQ(output.Length(), testCase.expectedOutput.length());
for (size_t i = 0; i < output.Length(); i++) {
EXPECT_EQ(output[i], testCase.expectedOutput[i]);
}
}
INSTANTIATE_TEST_SUITE_P(psm_HMAC, psm_HMAC,
::testing::ValuesIn(HMACTestCases));
|