1 /*  test_hmac_prng.c - TinyCrypt implementation of some HMAC-PRNG tests */
2 
3 /*
4  *  Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
5  *
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 
51 #define TC_DEBUG_MODE 0
52 
53 #ifdef TC_DEBUG_MODE
show(const char * label,const uint8_t * s,size_t len)54 void 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 
printBinaryFile(const uint8_t * s,unsigned int slen)64 void 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 
72 /*
73  * Main task to test AES
74  */
main(void)75 int main(void)
76 {
77         uint8_t seed[128];
78         struct tc_hmac_prng_struct h;
79         unsigned int size = (1 << 19);
80         uint8_t random[size];
81         unsigned int i;
82         unsigned int result = TC_PASS;
83 
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): */
88         for (i = 0; i < (unsigned int) sizeof(seed); ++i) {
89                 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 
125 #ifdef TC_DEBUG_MODE
126 	printBinaryFile(random, size);
127 	show ("Pseudo-random data", random, size);
128 #endif
129 
130         TC_PRINT("All HMAC tests succeeded!\n");
131 
132  exitTest:
133         TC_END_RESULT(result);
134         TC_END_REPORT(result);
135 }
136