Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src-draft/trunk]: src/sys/stand/efiboot Draft logic to get at EFI RNG.



details:   https://anonhg.NetBSD.org/src-all/rev/ed9b769cdf86
branches:  trunk
changeset: 932542:ed9b769cdf86
user:      Taylor R Campbell <riastradh%NetBSD.org@localhost>
date:      Mon May 11 02:10:19 2020 +0000

description:
Draft logic to get at EFI RNG.

diffstat:

 sys/stand/efiboot/Makefile.efiboot |    2 +-
 sys/stand/efiboot/efirng.c         |  121 +++++++++++++++++++++++++++++++++++++
 sys/stand/efiboot/efirng.h         |   37 +++++++++++
 3 files changed, 159 insertions(+), 1 deletions(-)

diffs (178 lines):

diff -r 5db97962c4a9 -r ed9b769cdf86 sys/stand/efiboot/Makefile.efiboot
--- a/sys/stand/efiboot/Makefile.efiboot        Sun May 10 17:58:16 2020 +0000
+++ b/sys/stand/efiboot/Makefile.efiboot        Mon May 11 02:10:19 2020 +0000
@@ -22,7 +22,7 @@
 .PATH: ${EFIDIR}/gnuefi
 SOURCES=       crt0-efi-${GNUEFIARCH}.S reloc_${GNUEFIARCH}.c
 SOURCES+=      boot.c conf.c console.c dev_net.c devopen.c exec.c panic.c prompt.c
-SOURCES+=      efiboot.c efichar.c efidev.c efienv.c efigetsecs.c efifdt.c efifile.c efiblock.c efinet.c efipxe.c efiacpi.c smbios.c
+SOURCES+=      efiboot.c efichar.c efidev.c efienv.c efigetsecs.c efifdt.c efifile.c efiblock.c efinet.c efipxe.c efiacpi.c efirng.c smbios.c
 
 .PATH: ${S}/external/bsd/libfdt/dist
 CPPFLAGS+=     -I${S}/external/bsd/libfdt/dist
diff -r 5db97962c4a9 -r ed9b769cdf86 sys/stand/efiboot/efirng.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/stand/efiboot/efirng.c        Mon May 11 02:10:19 2020 +0000
@@ -0,0 +1,121 @@
+/*     $NetBSD$        */
+
+/*-
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * UEFI Forum, Inc.: UEFI Specification, Version 2.8 Errata A, February
+ * 2020, Sec. 37.5 EFI Random Number Generator Protocol, pp. 2158--2162
+ * https://uefi.org/sites/default/files/resources/UEFI_Spec_2_8_A_Feb14.pdf
+ */
+
+#include "efirng.h"
+
+#include "efiboot.h"
+
+static EFI_GUID RngProtocolGuid = EFI_RNG_PROTOCOL_GUID;
+static EFI_GUID RngAlgorithmRawGuid = EFI_RNG_ALGORITHM_RAW;
+static EFI_RNG_PROTOCOL *rng;
+static bool rng_probed;
+
+static const struct {
+       EFI_GUID guid;
+       const char *name;
+} algname[] = {
+       {EFI_RNG_ALGORITHM_SP800_90_HASH_256_GUID,
+        "NIST SP800-90 Hash_DRBG SHA-256"},
+       {EFI_RNG_ALGORITHM_SP800_90_HMAC_256_GUID,
+        "NIST SP800-90 HMAC_DRBG SHA-256"},
+       {EFI_RNG_ALGORITHM_SP800_90_CTR_256_GUID,
+        "NIST SP800-90 CTR_DRBG AES-256"},
+       {EFI_RNG_ALGORITHM_X9_31_3DES_GUID, "ANSI X9.31 3DES"},
+       {EFI_RNG_ALGORITHM_X9_31_AES_GUID, "ANSI X9.31 AES"},
+       {EFI_RNG_ALGORITHM_RAW, "raw"},
+};
+
+void
+efi_rng_probe(void)
+{
+       EFI_RNG_ALGORITHM alglist[10];
+       UINTN i, j, alglistsz = sizeof alglist;
+       EFI_STATUS status;
+
+       /* Get the RNG protocol.  */
+       status = LibLocateProtocol(&RngProtocolGuid, (void **)&rng);
+       if (EFI_ERROR(status)) {
+               printf("%s: locate rng protocol: status=%ld\n", __func__,
+                   status);
+               goto fail;
+       }
+
+       /* Query the list of supported algorithms.  */
+       status = uefi_call_wrapper(rng->GetInfo, 3, rng, &alglistsz, alglist);
+       if (EFI_ERROR(status)) {
+               printf("%s: GetInfo: status=%ld\n", __func__, status);
+               goto fail;
+       }
+
+       /* Print the list of supported algorithms.  */
+       for (i = 0; i < alglistsz/sizeof(alglist[0]); i++) {
+               const char *name = "[unknown]";
+               for (j = 0; j < __arraycount(algname); j++) {
+                       if (memcmp(&alglist[i], &algname[j].guid,
+                               sizeof(EFI_GUID)) == 0) {
+                               name = algname[j].name;
+                               break;
+                       }
+               }
+               Print(L"%s: alg %s (%g)\n", __func__, name, alglist[i]);
+       }
+
+       /* Success!  */
+       printf("%s: success\n", __func__);
+       rng_probed = true;
+       return;
+
+fail:  rng = NULL;
+}
+
+int
+efi_rng(void *buf, UINTN len)
+{
+       EFI_STATUS status;
+
+       if (!rng_probed) {
+               printf("%s: no rng\n", __func__);
+               return EIO;
+       }
+
+       status = uefi_call_wrapper(rng->GetRNG, 3, rng, &RngAlgorithmRawGuid,
+           len, buf);
+       if (EFI_ERROR(status)) {
+               printf("%s: rng failed: %ld\n", __func__, status);
+               return EIO;
+       }
+
+       /* Success!  */
+       return 0;
+}
diff -r 5db97962c4a9 -r ed9b769cdf86 sys/stand/efiboot/efirng.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/stand/efiboot/efirng.h        Mon May 11 02:10:19 2020 +0000
@@ -0,0 +1,37 @@
+/*     $NetBSD$        */
+
+/*-
+ * Copyright (c) 2020 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef        EFIBOOT_EFIRNG_H
+#define        EFIBOOT_EFIRNG_H
+
+#include <efi.h>
+
+void   efi_rng_probe(void);
+int    efi_rng(void *, UINTN);
+
+#endif /* EFIBOOT_EFIRNG_H */



Home | Main Index | Thread Index | Old Index