1 1.4 jmcneill /* $NetBSD: efirng.c,v 1.4 2022/08/14 11:26:41 jmcneill Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /*- 4 1.1 riastrad * Copyright (c) 2020 The NetBSD Foundation, Inc. 5 1.1 riastrad * All rights reserved. 6 1.1 riastrad * 7 1.1 riastrad * Redistribution and use in source and binary forms, with or without 8 1.1 riastrad * modification, are permitted provided that the following conditions 9 1.1 riastrad * are met: 10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright 11 1.1 riastrad * notice, this list of conditions and the following disclaimer. 12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the 14 1.1 riastrad * documentation and/or other materials provided with the distribution. 15 1.1 riastrad * 16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE. 27 1.1 riastrad */ 28 1.1 riastrad 29 1.1 riastrad /* 30 1.1 riastrad * UEFI Forum, Inc.: UEFI Specification, Version 2.8 Errata A, February 31 1.1 riastrad * 2020, Sec. 37.5 EFI Random Number Generator Protocol, pp. 2158--2162 32 1.1 riastrad * https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_A_Feb14.pdf 33 1.1 riastrad */ 34 1.1 riastrad 35 1.1 riastrad #include "efirng.h" 36 1.1 riastrad 37 1.1 riastrad #include "efiboot.h" 38 1.1 riastrad 39 1.1 riastrad static EFI_GUID RngProtocolGuid = EFI_RNG_PROTOCOL_GUID; 40 1.1 riastrad static EFI_GUID RngAlgorithmRawGuid = EFI_RNG_ALGORITHM_RAW; 41 1.1 riastrad static EFI_RNG_PROTOCOL *rng; 42 1.1 riastrad 43 1.1 riastrad #ifndef EFIBOOT_DEBUG 44 1.1 riastrad #define DPRINT(...) __nothing 45 1.1 riastrad #else 46 1.1 riastrad #define DPRINT Print 47 1.1 riastrad #endif 48 1.1 riastrad 49 1.1 riastrad static const struct { 50 1.1 riastrad EFI_GUID guid; 51 1.1 riastrad const CHAR16 *name; 52 1.1 riastrad } algname[] = { 53 1.1 riastrad {EFI_RNG_ALGORITHM_SP800_90_HASH_256_GUID, 54 1.1 riastrad L"NIST SP800-90 Hash_DRBG SHA-256"}, 55 1.1 riastrad {EFI_RNG_ALGORITHM_SP800_90_HMAC_256_GUID, 56 1.1 riastrad L"NIST SP800-90 HMAC_DRBG SHA-256"}, 57 1.1 riastrad {EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID, 58 1.1 riastrad L"NIST SP800-90 CTR_DRBG AES-256"}, 59 1.1 riastrad {EFI_RNG_ALGORITHM_X9_31_3DES_GUID, L"ANSI X9.31 3DES"}, 60 1.1 riastrad {EFI_RNG_ALGORITHM_X9_31_AES_GUID, L"ANSI X9.31 AES"}, 61 1.1 riastrad {EFI_RNG_ALGORITHM_RAW, L"raw"}, 62 1.1 riastrad }; 63 1.1 riastrad 64 1.1 riastrad void 65 1.1 riastrad efi_rng_probe(void) 66 1.1 riastrad { 67 1.1 riastrad EFI_STATUS status; 68 1.1 riastrad 69 1.1 riastrad /* Get the RNG protocol. */ 70 1.1 riastrad status = LibLocateProtocol(&RngProtocolGuid, (void **)&rng); 71 1.1 riastrad if (EFI_ERROR(status)) { 72 1.1 riastrad DPRINT(L"efirng: protocol: %r\n", status); 73 1.1 riastrad rng = NULL; 74 1.1 riastrad return; 75 1.1 riastrad } 76 1.1 riastrad } 77 1.1 riastrad 78 1.1 riastrad void 79 1.1 riastrad efi_rng_show(void) 80 1.1 riastrad { 81 1.1 riastrad EFI_RNG_ALGORITHM alglist[10]; 82 1.2 jmcneill UINTN i, j, alglistsz = sizeof(alglist); 83 1.1 riastrad EFI_STATUS status; 84 1.1 riastrad 85 1.2 jmcneill if (!efi_rng_available()) 86 1.2 jmcneill return; 87 1.2 jmcneill 88 1.4 jmcneill command_printtab("RNG", ""); 89 1.4 jmcneill 90 1.1 riastrad /* Query the list of supported algorithms. */ 91 1.1 riastrad status = uefi_call_wrapper(rng->GetInfo, 3, rng, &alglistsz, alglist); 92 1.1 riastrad if (EFI_ERROR(status)) { 93 1.4 jmcneill Print(L"GetInfo: %r\n", status); 94 1.1 riastrad return; 95 1.1 riastrad } 96 1.1 riastrad 97 1.1 riastrad /* Print the list of supported algorithms. */ 98 1.1 riastrad for (i = 0; i < alglistsz/sizeof(alglist[0]); i++) { 99 1.1 riastrad const CHAR16 *name = L"[unknown]"; 100 1.1 riastrad for (j = 0; j < __arraycount(algname); j++) { 101 1.1 riastrad if (memcmp(&alglist[i], &algname[j].guid, 102 1.1 riastrad sizeof(EFI_GUID)) == 0) { 103 1.1 riastrad name = algname[j].name; 104 1.1 riastrad break; 105 1.1 riastrad } 106 1.1 riastrad } 107 1.4 jmcneill Print(L"%s (%g)\n", name, &alglist[i]); 108 1.1 riastrad } 109 1.1 riastrad } 110 1.1 riastrad 111 1.1 riastrad int 112 1.1 riastrad efi_rng_available(void) 113 1.1 riastrad { 114 1.1 riastrad 115 1.1 riastrad return rng != NULL; 116 1.1 riastrad } 117 1.1 riastrad 118 1.1 riastrad int 119 1.1 riastrad efi_rng(void *buf, UINTN len) 120 1.1 riastrad { 121 1.1 riastrad EFI_STATUS status; 122 1.1 riastrad 123 1.2 jmcneill if (!efi_rng_available()) 124 1.1 riastrad return EIO; 125 1.1 riastrad 126 1.3 jmcneill status = uefi_call_wrapper(rng->GetRNG, 4, rng, &RngAlgorithmRawGuid, 127 1.1 riastrad len, buf); 128 1.1 riastrad if (status == EFI_UNSUPPORTED) { 129 1.1 riastrad /* 130 1.1 riastrad * Fall back to any supported RNG `algorithm' even 131 1.1 riastrad * though we would prefer raw samples. 132 1.1 riastrad */ 133 1.3 jmcneill status = uefi_call_wrapper(rng->GetRNG, 4, rng, NULL, len, buf); 134 1.1 riastrad } 135 1.1 riastrad if (EFI_ERROR(status)) { 136 1.1 riastrad DPRINT(L"efirng: GetRNG: %r\n", status); 137 1.1 riastrad return EIO; 138 1.1 riastrad } 139 1.1 riastrad 140 1.1 riastrad return 0; 141 1.1 riastrad } 142