developer | 02e6591 | 2023-08-17 16:33:10 +0800 | [diff] [blame] | 1 | /* adapter_global drbg_init.c |
| 2 | * |
| 3 | * Initialize Global DRBG Control functionality. |
| 4 | */ |
| 5 | |
| 6 | /***************************************************************************** |
| 7 | * Copyright (c) 2017-2020 by Rambus, Inc. and/or its subsidiaries. |
| 8 | * |
| 9 | * This program is free software: you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation, either version 2 of the License, or |
| 12 | * any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | *****************************************************************************/ |
| 22 | |
| 23 | /*---------------------------------------------------------------------------- |
| 24 | * This module implements (provides) the following interface(s): |
| 25 | */ |
| 26 | |
| 27 | #include "adapter_global_drbg_init.h" |
| 28 | |
| 29 | |
| 30 | /*---------------------------------------------------------------------------- |
| 31 | * This module uses (requires) the following interface(s): |
| 32 | */ |
| 33 | |
| 34 | // Default configuration |
| 35 | #include "c_adapter_global.h" |
| 36 | |
| 37 | // Global Control DRBG API |
| 38 | #include "api_global_eip74.h" |
| 39 | |
| 40 | // Driver Framework Basic Definitions API |
| 41 | #include "basic_defs.h" // uint8_t, uint32_t, bool |
| 42 | |
| 43 | // Driver Framework C Library API |
| 44 | #include "clib.h" // memcpy, ZEROINIT |
| 45 | |
| 46 | // Log API |
| 47 | #include "log.h" |
| 48 | |
| 49 | /*---------------------------------------------------------------------------- |
| 50 | * Definitions and macros |
| 51 | */ |
| 52 | |
| 53 | |
| 54 | /*---------------------------------------------------------------------------- |
| 55 | * Local variables |
| 56 | */ |
| 57 | |
| 58 | static bool fDRBGPresent; |
| 59 | |
| 60 | #ifdef MODULE |
| 61 | #include <linux/random.h> |
| 62 | #define Global_DRBG_Entropy_Get(p) get_random_bytes(p, 48); |
| 63 | #else |
| 64 | #include <stdio.h> |
| 65 | /*---------------------------------------------------------------------------- |
| 66 | * Global_DRBG_Entropy_Get |
| 67 | * |
| 68 | * Get 48 bytes of entropy to initialize/reseed DRBG. |
| 69 | */ |
| 70 | static void |
| 71 | Global_DRBG_Entropy_Get( |
| 72 | uint8_t * Key_p) |
| 73 | { |
| 74 | FILE *rng = fopen("/dev/urandom","rb"); |
| 75 | if (rng==NULL) |
| 76 | { |
| 77 | LOG_CRIT("/dev/urandom not available\n"); |
| 78 | return; |
| 79 | } |
| 80 | if (fread(Key_p, 1, 48, rng) < 48) |
| 81 | { |
| 82 | LOG_CRIT("random data not read\n"); |
| 83 | return; |
| 84 | } |
| 85 | Log_HexDump("Entropy",0,Key_p,48); |
| 86 | |
| 87 | fclose(rng); |
| 88 | } |
| 89 | |
| 90 | #endif |
| 91 | |
| 92 | /*---------------------------------------------------------------------------- |
| 93 | * BoolToString() |
| 94 | * |
| 95 | * Convert boolean value to string. |
| 96 | */ |
| 97 | static const char * |
| 98 | BoolToString( |
| 99 | const bool b) |
| 100 | { |
| 101 | if (b) |
| 102 | return "true"; |
| 103 | else |
| 104 | return "false"; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /*---------------------------------------------------------------------------- |
| 109 | * Adapter_Global_DRBG_StatusReport() |
| 110 | * |
| 111 | * Obtain all available global status information from the Global DRBG |
| 112 | * hardware and report it. |
| 113 | */ |
| 114 | void |
| 115 | Adapter_Global_DRBG_StatusReport(void) |
| 116 | { |
| 117 | GlobalControl74_Error_t Rc; |
| 118 | GlobalControl74_Status_t Status; |
| 119 | |
| 120 | LOG_INFO("DA_GC: Global_DRBG_StatusReport \n"); |
| 121 | |
| 122 | LOG_CRIT("DA_GC: Global DRBG Status\n"); |
| 123 | Rc = GlobalControl74_Status_Get(&Status); |
| 124 | if (Rc != GLOBAL_CONTROL_EIP74_NO_ERROR) |
| 125 | { |
| 126 | LOG_CRIT("EIP74 status get error\n"); |
| 127 | return; |
| 128 | } |
| 129 | Log_FormattedMessage( |
| 130 | "EIP 74 status: GenBlockCount=%u StuckOut=%s\n" |
| 131 | "\t\tNotInitialized=%s ReseedErr=%s ReseedWarn=%s\n" |
| 132 | "\t\tInstantiated=%s AvailableCount=%u\n", |
| 133 | Status.GenerateBlockCount, |
| 134 | BoolToString(Status.fStuckOut), |
| 135 | BoolToString(Status.fNotInitialized), |
| 136 | BoolToString(Status.fReseedError), |
| 137 | BoolToString(Status.fReseedWarning), |
| 138 | BoolToString(Status.fInstantiated), |
| 139 | Status.AvailableCount); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /*---------------------------------------------------------------------------- |
| 144 | * Adapter_Global_DRBG_Init() |
| 145 | * |
| 146 | */ |
| 147 | bool |
| 148 | Adapter_Global_DRBG_Init(void) |
| 149 | { |
| 150 | GlobalControl74_Error_t rc; |
| 151 | GlobalControl74_Capabilities_t Capabilities; |
| 152 | GlobalControl74_Configuration_t Configuration; |
| 153 | uint8_t Entropy[48]; |
| 154 | |
| 155 | LOG_INFO("DA_GC: Global_DRBG_Init \n"); |
| 156 | |
| 157 | ZEROINIT(Configuration); |
| 158 | Configuration.fStuckOut = true; |
| 159 | |
| 160 | Global_DRBG_Entropy_Get(Entropy); |
| 161 | |
| 162 | rc = GlobalControl74_Init(&Configuration, Entropy); |
| 163 | if (rc == GLOBAL_CONTROL_EIP74_ERROR_NOT_IMPLEMENTED) |
| 164 | { |
| 165 | LOG_CRIT("EIP74 not present\n"); |
| 166 | return true; |
| 167 | } |
| 168 | if (rc == GLOBAL_CONTROL_EIP74_NO_ERROR) |
| 169 | { |
| 170 | fDRBGPresent = true; |
| 171 | Log_FormattedMessage("EIP74 initialized OK\n"); |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | LOG_CRIT("EIP74 initialization error\n"); |
| 176 | } |
| 177 | |
| 178 | Capabilities.szTextDescription[0] = 0; |
| 179 | |
| 180 | GlobalControl74_Capabilities_Get(&Capabilities); |
| 181 | |
| 182 | LOG_CRIT("DA_GC: Global DRBG capabilities: %s\n", |
| 183 | Capabilities.szTextDescription); |
| 184 | |
| 185 | Adapter_Global_DRBG_StatusReport(); |
| 186 | |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /*---------------------------------------------------------------------------- |
| 192 | * Adapter_Global_DRBG_UnInit() |
| 193 | * |
| 194 | */ |
| 195 | void |
| 196 | Adapter_Global_DRBG_UnInit(void) |
| 197 | { |
| 198 | LOG_INFO("\n\t\t Adapter_Global_DRBG_UnInit \n"); |
| 199 | |
| 200 | if (fDRBGPresent) |
| 201 | { |
| 202 | Adapter_Global_DRBG_StatusReport(); |
| 203 | |
| 204 | GlobalControl74_UnInit(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | |
| 209 | /* end of file adapter_global_drbg_init.c */ |