tests-tweetnacl.c
2.23 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup unittests
* @{
*
* @file
* @brief tweetnacl NaCl crypto library tests
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author Martin Landsmann <Martin.Landsmann@HAW-Hamburg.de>
*
* @}
*/
#include <stdio.h>
#include <string.h>
#include <tweetnacl.h>
#include "embUnit.h"
#include "tests-tweetnacl.h"
static const char message[] = "0123456789abcdef";
static char r[sizeof(message)];
#define MLEN (sizeof(message) + crypto_box_ZEROBYTES)
static unsigned char alice_pk[crypto_box_PUBLICKEYBYTES];
static unsigned char alice_sk[crypto_box_SECRETKEYBYTES];
static unsigned char bob_pk[crypto_box_PUBLICKEYBYTES];
static unsigned char bob_sk[crypto_box_SECRETKEYBYTES];
static unsigned char m[MLEN];
static unsigned char c[MLEN];
static const unsigned char n[crypto_box_NONCEBYTES];
static unsigned char result[MLEN];
static void setUp(void)
{
/* Initialize */
random_init(0);
}
static void tearDown(void)
{
/* Finalize */
}
static void test_tweetnacl_01(void)
{
int res;
/* Creating keypair ALICE... */
crypto_box_keypair(alice_pk, alice_sk);
/* Creating keypair BOB... */
crypto_box_keypair(bob_pk, bob_sk);
memset(m, 0, crypto_box_ZEROBYTES);
memcpy(m + crypto_box_ZEROBYTES, message, MLEN - crypto_box_ZEROBYTES);
/* Encrypting using pk_bob... */
crypto_box(c, m, MLEN, n, bob_pk, alice_sk);
memset(result, '\0', sizeof(result));
/* Decrypting... */
res = crypto_box_open(result, c, MLEN, n, alice_pk, bob_sk);
TEST_ASSERT_EQUAL_INT(0, res);
memset(r, 0, sizeof(r));
memcpy(r, result + crypto_box_ZEROBYTES, MLEN - crypto_box_ZEROBYTES);
TEST_ASSERT_EQUAL_STRING("0123456789abcdef", (const char*)r);
}
Test *tests_tweetnacl_all(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_tweetnacl_01)
};
EMB_UNIT_TESTCALLER(tweetnacl_tests, setUp, tearDown, fixtures);
return (Test*)&tweetnacl_tests;
}
void tests_tweetnacl(void)
{
TESTS_RUN(tests_tweetnacl_all());
}