David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 1 | /* test_hmac_prng.c - TinyCrypt implementation of some HMAC-PRNG tests */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (C) 2015 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 | /* |
| 52 | * Main task to test AES |
| 53 | */ |
| 54 | |
| 55 | int main(void) |
| 56 | { |
| 57 | uint8_t seed[128]; |
| 58 | struct tc_hmac_prng_struct h; |
| 59 | uint32_t size = (1 << 15); |
| 60 | uint8_t random[size]; |
| 61 | uint32_t i; |
| 62 | uint32_t result = TC_PASS; |
| 63 | |
| 64 | TC_START("Performing HMAC-PRNG tests:"); |
| 65 | TC_PRINT("HMAC-PRNG test#1 (init, reseed, generate):\n"); |
| 66 | |
| 67 | /* Fake seed (replace by a a truly random seed): */ |
| 68 | for (i = 0; i < (uint32_t) sizeof(seed); ++i) { |
| 69 | seed[i] = i; |
| 70 | } |
| 71 | |
| 72 | /* Fake personalization and additional_input (replace by appropriate |
| 73 | * values): * |
| 74 | * e.g.: hostname+timestamp */ |
| 75 | uint8_t *personalization = (uint8_t *) "HOSTNAME"; |
| 76 | uint8_t *additional_input = (uint8_t *) "additional input"; |
| 77 | |
| 78 | TC_PRINT("HMAC-PRNG test#1 (init):\n"); |
| 79 | if (tc_hmac_prng_init(&h, personalization, |
| 80 | sizeof(personalization)) == 0) { |
| 81 | TC_ERROR("HMAC-PRNG initialization failed.\n"); |
| 82 | result = TC_FAIL; |
| 83 | goto exitTest; |
| 84 | } |
| 85 | TC_END_RESULT(result); |
| 86 | |
| 87 | TC_PRINT("HMAC-PRNG test#1 (reseed):\n"); |
| 88 | if (tc_hmac_prng_reseed(&h, seed, sizeof(seed), additional_input, |
| 89 | sizeof(additional_input)) == 0) { |
| 90 | TC_ERROR("HMAC-PRNG reseed failed.\n"); |
| 91 | result = TC_FAIL; |
| 92 | goto exitTest; |
| 93 | } |
| 94 | |
| 95 | TC_END_RESULT(result); |
| 96 | |
| 97 | TC_PRINT("HMAC-PRNG test#1 (generate):\n"); |
| 98 | if (tc_hmac_prng_generate(random, size, &h) < 1) { |
| 99 | TC_ERROR("HMAC-PRNG generate failed.\n"); |
| 100 | result = TC_FAIL; |
| 101 | goto exitTest; |
| 102 | } |
| 103 | TC_END_RESULT(result); |
| 104 | |
| 105 | TC_PRINT("All HMAC tests succeeded!\n"); |
| 106 | |
| 107 | exitTest: |
| 108 | TC_END_RESULT(result); |
| 109 | TC_END_REPORT(result); |
| 110 | } |