xref: /freebsd/sys/contrib/libsodium/test/default/scalarmult.c (revision 3611ec604864a7d4dcc9a3ea898c80eb35eef8a0)
1*0ac341f1SConrad Meyer 
2*0ac341f1SConrad Meyer #define TEST_NAME "scalarmult"
3*0ac341f1SConrad Meyer #include "cmptest.h"
4*0ac341f1SConrad Meyer 
5*0ac341f1SConrad Meyer static const unsigned char alicesk[crypto_scalarmult_BYTES] = {
6*0ac341f1SConrad Meyer     0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d, 0x3c, 0x16, 0xc1,
7*0ac341f1SConrad Meyer     0x72, 0x51, 0xb2, 0x66, 0x45, 0xdf, 0x4c, 0x2f, 0x87, 0xeb, 0xc0,
8*0ac341f1SConrad Meyer     0x99, 0x2a, 0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9, 0x2c, 0x2a
9*0ac341f1SConrad Meyer };
10*0ac341f1SConrad Meyer 
11*0ac341f1SConrad Meyer static const unsigned char bobsk[crypto_scalarmult_BYTES] = {
12*0ac341f1SConrad Meyer     0x5d, 0xab, 0x08, 0x7e, 0x62, 0x4a, 0x8a, 0x4b, 0x79, 0xe1, 0x7f,
13*0ac341f1SConrad Meyer     0x8b, 0x83, 0x80, 0x0e, 0xe6, 0x6f, 0x3b, 0xb1, 0x29, 0x26, 0x18,
14*0ac341f1SConrad Meyer     0xb6, 0xfd, 0x1c, 0x2f, 0x8b, 0x27, 0xff, 0x88, 0xe0, 0xeb
15*0ac341f1SConrad Meyer };
16*0ac341f1SConrad Meyer 
17*0ac341f1SConrad Meyer static const unsigned char small_order_p[crypto_scalarmult_BYTES] = {
18*0ac341f1SConrad Meyer     0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae, 0x16, 0x56, 0xe3,
19*0ac341f1SConrad Meyer     0xfa, 0xf1, 0x9f, 0xc4, 0x6a, 0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32,
20*0ac341f1SConrad Meyer     0xb1, 0xfd, 0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00
21*0ac341f1SConrad Meyer };
22*0ac341f1SConrad Meyer 
23*0ac341f1SConrad Meyer static char hex[crypto_scalarmult_BYTES * 2 + 1];
24*0ac341f1SConrad Meyer 
25*0ac341f1SConrad Meyer int
main(void)26*0ac341f1SConrad Meyer main(void)
27*0ac341f1SConrad Meyer {
28*0ac341f1SConrad Meyer     unsigned char *alicepk =
29*0ac341f1SConrad Meyer         (unsigned char *) sodium_malloc(crypto_scalarmult_BYTES);
30*0ac341f1SConrad Meyer     unsigned char *bobpk =
31*0ac341f1SConrad Meyer         (unsigned char *) sodium_malloc(crypto_scalarmult_BYTES);
32*0ac341f1SConrad Meyer     unsigned char *k = (unsigned char *) sodium_malloc(crypto_scalarmult_BYTES);
33*0ac341f1SConrad Meyer     int            ret;
34*0ac341f1SConrad Meyer 
35*0ac341f1SConrad Meyer     assert(alicepk != NULL && bobpk != NULL && k != NULL);
36*0ac341f1SConrad Meyer 
37*0ac341f1SConrad Meyer     crypto_scalarmult_base(alicepk, alicesk);
38*0ac341f1SConrad Meyer     sodium_bin2hex(hex, sizeof hex, alicepk, crypto_scalarmult_BYTES);
39*0ac341f1SConrad Meyer     printf("%s\n", hex);
40*0ac341f1SConrad Meyer 
41*0ac341f1SConrad Meyer     crypto_scalarmult_base(bobpk, bobsk);
42*0ac341f1SConrad Meyer     sodium_bin2hex(hex, sizeof hex, bobpk, crypto_scalarmult_BYTES);
43*0ac341f1SConrad Meyer     printf("%s\n", hex);
44*0ac341f1SConrad Meyer 
45*0ac341f1SConrad Meyer     ret = crypto_scalarmult(k, alicesk, bobpk);
46*0ac341f1SConrad Meyer     assert(ret == 0);
47*0ac341f1SConrad Meyer     sodium_bin2hex(hex, sizeof hex, k, crypto_scalarmult_BYTES);
48*0ac341f1SConrad Meyer     printf("%s\n", hex);
49*0ac341f1SConrad Meyer 
50*0ac341f1SConrad Meyer     ret = crypto_scalarmult(k, bobsk, alicepk);
51*0ac341f1SConrad Meyer     assert(ret == 0);
52*0ac341f1SConrad Meyer     sodium_bin2hex(hex, sizeof hex, k, crypto_scalarmult_BYTES);
53*0ac341f1SConrad Meyer     printf("%s\n", hex);
54*0ac341f1SConrad Meyer 
55*0ac341f1SConrad Meyer     ret = crypto_scalarmult(k, bobsk, small_order_p);
56*0ac341f1SConrad Meyer     assert(ret == -1);
57*0ac341f1SConrad Meyer 
58*0ac341f1SConrad Meyer     sodium_free(bobpk);
59*0ac341f1SConrad Meyer     sodium_free(alicepk);
60*0ac341f1SConrad Meyer     sodium_free(k);
61*0ac341f1SConrad Meyer 
62*0ac341f1SConrad Meyer     assert(crypto_scalarmult_bytes() > 0U);
63*0ac341f1SConrad Meyer     assert(crypto_scalarmult_scalarbytes() > 0U);
64*0ac341f1SConrad Meyer     assert(strcmp(crypto_scalarmult_primitive(), "curve25519") == 0);
65*0ac341f1SConrad Meyer     assert(crypto_scalarmult_bytes() == crypto_scalarmult_curve25519_bytes());
66*0ac341f1SConrad Meyer     assert(crypto_scalarmult_scalarbytes() ==
67*0ac341f1SConrad Meyer            crypto_scalarmult_curve25519_scalarbytes());
68*0ac341f1SConrad Meyer     assert(crypto_scalarmult_bytes() == crypto_scalarmult_scalarbytes());
69*0ac341f1SConrad Meyer 
70*0ac341f1SConrad Meyer     return 0;
71*0ac341f1SConrad Meyer }
72