Skip to content

Commit ed71271

Browse files
test(unit): slhdsa keypair tests added (#5)
key gen, serialization, deserialization, pkcs8, sign and verify
1 parent 429357f commit ed71271

3 files changed

Lines changed: 303 additions & 0 deletions

File tree

src/lib/crypto/test/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ cryptotest_SOURCES = cryptotest.cpp \
2020
DSATests.cpp \
2121
ECDHTests.cpp \
2222
ECDSATests.cpp \
23+
SLHDSATests.cpp \
2324
EDDSATests.cpp \
2425
GOSTTests.cpp \
2526
HashTests.cpp \
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
/*
2+
* Copyright (c) 2010 SURFnet bv
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20+
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22+
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23+
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24+
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
27+
/*****************************************************************************
28+
SLHDSATests.cpp
29+
30+
Contains test cases to test the SLHDSA class
31+
*****************************************************************************/
32+
33+
#include "SLHDSATests.h"
34+
#include "AsymmetricAlgorithm.h"
35+
#include "AsymmetricKeyPair.h"
36+
#include "CryptoFactory.h"
37+
#include "RNG.h"
38+
#ifdef WITH_SLHDSA
39+
#include "SLHParameters.h"
40+
#include "SLHPrivateKey.h"
41+
#include "SLHPublicKey.h"
42+
#include <cppunit/extensions/HelperMacros.h>
43+
#include <stdlib.h>
44+
#include <utility>
45+
#include <vector>
46+
47+
CPPUNIT_TEST_SUITE_REGISTRATION(SLHDSATests);
48+
49+
void SLHDSATests::setUp() {
50+
slhdsa = NULL;
51+
52+
slhdsa = CryptoFactory::i()->getAsymmetricAlgorithm(AsymAlgo::SLHDSA);
53+
54+
// Check the SLHDSA object
55+
CPPUNIT_ASSERT(slhdsa != NULL);
56+
}
57+
58+
void SLHDSATests::tearDown() {
59+
if (slhdsa != NULL) {
60+
CryptoFactory::i()->recycleAsymmetricAlgorithm(slhdsa);
61+
}
62+
63+
fflush(stdout);
64+
}
65+
66+
void SLHDSATests::testKeyGeneration() {
67+
// slhdsa_names to test
68+
std::vector<ByteString> slhdsa_names;
69+
slhdsa_names.push_back(
70+
ByteString((const unsigned char *)"SLH-DSA-SHA2-128s", 18));
71+
slhdsa_names.push_back(
72+
ByteString((const unsigned char *)"SLH-DSA-SHAKE-128s", 20));
73+
slhdsa_names.push_back(
74+
ByteString((const unsigned char *)"SLH-DSA-SHA2-128f", 18));
75+
slhdsa_names.push_back(
76+
ByteString((const unsigned char *)"SLH-DSA-SHAKE-128f", 20));
77+
78+
slhdsa_names.push_back(
79+
ByteString((const unsigned char *)"SLH-DSA-SHA2-192s", 18));
80+
slhdsa_names.push_back(
81+
ByteString((const unsigned char *)"SLH-DSA-SHAKE-192s", 20));
82+
slhdsa_names.push_back(
83+
ByteString((const unsigned char *)"SLH-DSA-SHA2-192f", 18));
84+
slhdsa_names.push_back(
85+
ByteString((const unsigned char *)"SLH-DSA-SHAKE-192f", 20));
86+
87+
slhdsa_names.push_back(
88+
ByteString((const unsigned char *)"SLH-DSA-SHA2-256s", 18));
89+
slhdsa_names.push_back(
90+
ByteString((const unsigned char *)"SLH-DSA-SHAKE-256s", 20));
91+
slhdsa_names.push_back(
92+
ByteString((const unsigned char *)"SLH-DSA-SHA2-256f", 18));
93+
slhdsa_names.push_back(
94+
ByteString((const unsigned char *)"SLH-DSA-SHAKE-256f", 20));
95+
96+
for (auto c = slhdsa_names.begin(); c != slhdsa_names.end(); c++) {
97+
AsymmetricKeyPair *kp;
98+
SLHParameters *p = new SLHParameters();
99+
p->setName(*c);
100+
101+
// Generate key-pair
102+
CPPUNIT_ASSERT(slhdsa->generateKeyPair(&kp, p));
103+
104+
SLHPublicKey *pub = (SLHPublicKey *)kp->getPublicKey();
105+
SLHPrivateKey *priv = (SLHPrivateKey *)kp->getPrivateKey();
106+
107+
CPPUNIT_ASSERT(pub->getDerPublicKey() != ByteString(""));
108+
CPPUNIT_ASSERT(priv->getDerPrivateKey() != ByteString(""));
109+
110+
slhdsa->recycleParameters(p);
111+
slhdsa->recycleKeyPair(kp);
112+
}
113+
}
114+
115+
void SLHDSATests::testSerialisation()
116+
{
117+
SLHParameters* p = new SLHParameters;
118+
p->setName(ByteString((const unsigned char *) "SLH-DSA-SHA2-192f", 18));
119+
120+
// Serialise the parameters
121+
ByteString serialisedParams = p->serialise();
122+
123+
// Deserialise the parameters
124+
AsymmetricParameters* dName;
125+
126+
CPPUNIT_ASSERT(slhdsa->reconstructParameters(&dName, serialisedParams));
127+
128+
CPPUNIT_ASSERT(dName->areOfType(SLHParameters::type));
129+
130+
SLHParameters* ddName = (SLHParameters*) dName;
131+
132+
CPPUNIT_ASSERT(p->getName() == ddName->getName());
133+
134+
// Generate a key-pair
135+
AsymmetricKeyPair* kp;
136+
137+
CPPUNIT_ASSERT(slhdsa->generateKeyPair(&kp, dName));
138+
139+
// Serialise the key-pair
140+
ByteString serialisedKP = kp->serialise();
141+
142+
// Deserialise the key-pair
143+
AsymmetricKeyPair* dKP;
144+
145+
CPPUNIT_ASSERT(slhdsa->reconstructKeyPair(&dKP, serialisedKP));
146+
147+
// Check the deserialised key-pair
148+
SLHPrivateKey* privKey = (SLHPrivateKey*) kp->getPrivateKey();
149+
SLHPublicKey* pubKey = (SLHPublicKey*) kp->getPublicKey();
150+
151+
SLHPrivateKey* dPrivKey = (SLHPrivateKey*) dKP->getPrivateKey();
152+
SLHPublicKey* dPubKey = (SLHPublicKey*) dKP->getPublicKey();
153+
154+
CPPUNIT_ASSERT(privKey->getDerPrivateKey() == dPrivKey->getDerPrivateKey());
155+
156+
CPPUNIT_ASSERT(pubKey->getDerPublicKey() == dPubKey->getDerPublicKey());
157+
158+
slhdsa->recycleParameters(p);
159+
slhdsa->recycleParameters(dName);
160+
slhdsa->recycleKeyPair(kp);
161+
slhdsa->recycleKeyPair(dKP);
162+
}
163+
164+
void SLHDSATests::testPKCS8()
165+
{
166+
SLHParameters* p = new SLHParameters;
167+
p->setName(ByteString((const unsigned char *) "SLH-DSA-SHA2-192f", 18));
168+
169+
// Generate a key-pair
170+
AsymmetricKeyPair* kp;
171+
172+
CPPUNIT_ASSERT(slhdsa->generateKeyPair(&kp, p));
173+
CPPUNIT_ASSERT(kp != NULL);
174+
175+
SLHPrivateKey* priv = (SLHPrivateKey*) kp->getPrivateKey();
176+
CPPUNIT_ASSERT(priv != NULL);
177+
178+
// Encode and decode the private key
179+
ByteString pkcs8 = priv->PKCS8Encode();
180+
CPPUNIT_ASSERT(pkcs8.size() != 0);
181+
182+
SLHPrivateKey* dPriv = (SLHPrivateKey*) slhdsa->newPrivateKey();
183+
CPPUNIT_ASSERT(dPriv != NULL);
184+
185+
CPPUNIT_ASSERT(dPriv->PKCS8Decode(pkcs8));
186+
187+
CPPUNIT_ASSERT(priv->getDerPrivateKey() == dPriv->getDerPrivateKey());
188+
189+
slhdsa->recycleParameters(p);
190+
slhdsa->recycleKeyPair(kp);
191+
slhdsa->recyclePrivateKey(dPriv);
192+
}
193+
194+
void SLHDSATests::testSigningVerifying()
195+
{
196+
// slhdsa_names to test
197+
std::vector<ByteString> slhdsa_names;
198+
slhdsa_names.push_back(ByteString((const unsigned char *)"SLH-DSA-SHA2-128s", 18));
199+
slhdsa_names.push_back(ByteString((const unsigned char *)"SLH-DSA-SHAKE-128s", 20));
200+
slhdsa_names.push_back(ByteString((const unsigned char *)"SLH-DSA-SHA2-128f", 18));
201+
slhdsa_names.push_back(ByteString((const unsigned char *)"SLH-DSA-SHAKE-128f", 20));
202+
203+
slhdsa_names.push_back(ByteString((const unsigned char *)"SLH-DSA-SHA2-192s", 18));
204+
slhdsa_names.push_back(ByteString((const unsigned char *)"SLH-DSA-SHAKE-192s", 20));
205+
slhdsa_names.push_back(ByteString((const unsigned char *)"SLH-DSA-SHA2-192f", 18));
206+
slhdsa_names.push_back(ByteString((const unsigned char *)"SLH-DSA-SHAKE-192f", 20));
207+
208+
slhdsa_names.push_back(ByteString((const unsigned char *)"SLH-DSA-SHA2-256s", 18));
209+
slhdsa_names.push_back(ByteString((const unsigned char *)"SLH-DSA-SHAKE-256s", 20));
210+
slhdsa_names.push_back(ByteString((const unsigned char *)"SLH-DSA-SHA2-256f", 18));
211+
slhdsa_names.push_back(ByteString((const unsigned char *)"SLH-DSA-SHAKE-256f", 20));
212+
213+
for (auto c = slhdsa_names.begin(); c != slhdsa_names.end(); c++) {
214+
AsymmetricKeyPair *kp;
215+
SLHParameters *p = new SLHParameters();
216+
p->setName(*c);
217+
218+
// Generate key-pair
219+
CPPUNIT_ASSERT(slhdsa->generateKeyPair(&kp, p));
220+
CPPUNIT_ASSERT(p != NULL);
221+
222+
// Generate some data to sign
223+
ByteString dataToSign;
224+
225+
RNG* rng = CryptoFactory::i()->getRNG();
226+
CPPUNIT_ASSERT(rng != NULL);
227+
228+
CPPUNIT_ASSERT(rng->generateRandom(dataToSign, 567));
229+
230+
// Sign the data
231+
ByteString sig;
232+
CPPUNIT_ASSERT(slhdsa->sign(kp->getPrivateKey(), dataToSign, sig, AsymMech::SLHDSA));
233+
234+
// And verify it
235+
CPPUNIT_ASSERT(slhdsa->verify(kp->getPublicKey(), dataToSign, sig, AsymMech::SLHDSA));
236+
237+
slhdsa->recycleKeyPair(kp);
238+
slhdsa->recycleParameters(p);
239+
}
240+
}
241+
#endif

src/lib/crypto/test/SLHDSATests.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2010 SURFnet bv
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20+
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22+
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23+
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24+
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
27+
/*****************************************************************************
28+
SLHDSATests.h
29+
30+
Contains test cases to test the SLHDSA class
31+
*****************************************************************************/
32+
33+
#ifndef _SOFTHSM_V2_SLHDSATESTS_H
34+
#define _SOFTHSM_V2_SLHDSATESTS_H
35+
36+
#include "AsymmetricAlgorithm.h"
37+
#include <cppunit/extensions/HelperMacros.h>
38+
39+
class SLHDSATests : public CppUnit::TestFixture {
40+
CPPUNIT_TEST_SUITE(SLHDSATests);
41+
CPPUNIT_TEST(testKeyGeneration);
42+
CPPUNIT_TEST(testSerialisation);
43+
CPPUNIT_TEST(testPKCS8);
44+
CPPUNIT_TEST(testSigningVerifying);
45+
CPPUNIT_TEST_SUITE_END();
46+
47+
public:
48+
void testKeyGeneration();
49+
void testSerialisation();
50+
void testPKCS8();
51+
void testSigningVerifying();
52+
53+
void setUp();
54+
void tearDown();
55+
56+
private:
57+
// SLHDSA instance
58+
AsymmetricAlgorithm *slhdsa;
59+
};
60+
61+
#endif // !_SOFTHSM_V2_SLHDSATESTS_H

0 commit comments

Comments
 (0)