blob: b45e984c4dcc2190e62d026b4df2e4b214f6d6d3 [file] [log] [blame]
David Brownfecda2d2017-09-07 10:20:34 -06001/* test_hmac_prng.c - TinyCrypt implementation of some HMAC-PRNG tests */
2
3/*
Fabio Utzig3efe6b62017-09-22 16:03:24 -03004 * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
David Brownfecda2d2017-09-07 10:20:34 -06005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of Intel Corporation nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 DESCRIPTION
35 This module tests the following PRNG routines:
36
37 Scenarios tested include:
38 - HMAC-PRNG init
39 - HMAC-PRNG reseed
40 - HMAC-PRNG generate)
41*/
42
43#include <tinycrypt/hmac_prng.h>
44#include <tinycrypt/constants.h>
45#include <test_utils.h>
46
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50
Fabio Utzig3efe6b62017-09-22 16:03:24 -030051#define TC_DEBUG_MODE 0
52
53#ifdef TC_DEBUG_MODE
54void show(const char *label, const uint8_t *s, size_t len)
55{
56 unsigned int i;
57 printf ("%s = ", label);
58 for (i = 0; i < (unsigned int) len; ++i) {
59 printf ("%02x", s[i]);
60 }
61 printf ("\n");
62}
63
64void printBinaryFile(const uint8_t *s, unsigned int slen)
65{
66 FILE *write_ptr;
67 write_ptr = fopen("pseudo-random-data.bin","wb");
68 fwrite(s, slen, 1, write_ptr);
69}
70#endif
71
David Brownfecda2d2017-09-07 10:20:34 -060072/*
73 * Main task to test AES
74 */
David Brownfecda2d2017-09-07 10:20:34 -060075int main(void)
76{
77 uint8_t seed[128];
78 struct tc_hmac_prng_struct h;
Fabio Utzig3efe6b62017-09-22 16:03:24 -030079 unsigned int size = (1 << 19);
David Brownfecda2d2017-09-07 10:20:34 -060080 uint8_t random[size];
Fabio Utzig3efe6b62017-09-22 16:03:24 -030081 unsigned int i;
82 unsigned int result = TC_PASS;
David Brownfecda2d2017-09-07 10:20:34 -060083
84 TC_START("Performing HMAC-PRNG tests:");
85 TC_PRINT("HMAC-PRNG test#1 (init, reseed, generate):\n");
86
87 /* Fake seed (replace by a a truly random seed): */
Fabio Utzig3efe6b62017-09-22 16:03:24 -030088 for (i = 0; i < (unsigned int) sizeof(seed); ++i) {
David Brownfecda2d2017-09-07 10:20:34 -060089 seed[i] = i;
90 }
91
92 /* Fake personalization and additional_input (replace by appropriate
93 * values): *
94 * e.g.: hostname+timestamp */
95 uint8_t *personalization = (uint8_t *) "HOSTNAME";
96 uint8_t *additional_input = (uint8_t *) "additional input";
97
98 TC_PRINT("HMAC-PRNG test#1 (init):\n");
99 if (tc_hmac_prng_init(&h, personalization,
100 sizeof(personalization)) == 0) {
101 TC_ERROR("HMAC-PRNG initialization failed.\n");
102 result = TC_FAIL;
103 goto exitTest;
104 }
105 TC_END_RESULT(result);
106
107 TC_PRINT("HMAC-PRNG test#1 (reseed):\n");
108 if (tc_hmac_prng_reseed(&h, seed, sizeof(seed), additional_input,
109 sizeof(additional_input)) == 0) {
110 TC_ERROR("HMAC-PRNG reseed failed.\n");
111 result = TC_FAIL;
112 goto exitTest;
113 }
114
115 TC_END_RESULT(result);
116
117 TC_PRINT("HMAC-PRNG test#1 (generate):\n");
118 if (tc_hmac_prng_generate(random, size, &h) < 1) {
119 TC_ERROR("HMAC-PRNG generate failed.\n");
120 result = TC_FAIL;
121 goto exitTest;
122 }
123 TC_END_RESULT(result);
124
Fabio Utzig3efe6b62017-09-22 16:03:24 -0300125#ifdef TC_DEBUG_MODE
126 printBinaryFile(random, size);
127 show ("Pseudo-random data", random, size);
128#endif
129
David Brownfecda2d2017-09-07 10:20:34 -0600130 TC_PRINT("All HMAC tests succeeded!\n");
131
132 exitTest:
133 TC_END_RESULT(result);
134 TC_END_REPORT(result);
135}