Re: random numbers for efiboot

2018-04-08 Thread Mark Kettenis
> From: "Theo de Raadt" 
> Date: Sat, 07 Apr 2018 15:26:30 -0600
> 
> Mark Kettenis  wrote:
> 
> > The UEFI firmware for the MACCHIATObin implements the EFI Random
> > Number Generator Protocol.  That makes it possible to implement the
> > mdrandom() function for arm64's EFIBOOT.  The random data is XORed
> > onto the buffer so bad random data can't hurt us.
> > 
> > The code is written such that it can be easily dropped into our other
> > EFI bootloaders.  That raises a question though.  On amd64 we already
> > have an mdrandom() implementation.  Should I call the function
> > fwrandom() instead and and an
> > 
> > #ifdef FWRANDOM
> >fwrandom(rnddata, sizeof(rnddata));
> > #endif
> > 
> > next to the mdrandom() call in boot.c?
> 
> Yes, it should call every thing like that.

Here is a diff that implements that aproach.

ok?

Index: stand/boot/boot.c
===
RCS file: /cvs/src/sys/stand/boot/boot.c,v
retrieving revision 1.44
diff -u -p -r1.44 boot.c
--- stand/boot/boot.c   19 Jun 2017 22:50:50 -  1.44
+++ stand/boot/boot.c   8 Apr 2018 11:18:26 -
@@ -103,6 +103,9 @@ boot(dev_t bootdev)
 #ifdef MDRANDOM
mdrandom(rnddata, sizeof(rnddata));
 #endif
+#ifdef FWRANDOM
+   fwrandom(rnddata, sizeof(rnddata));
+#endif
 
st = 0;
bootprompt = 1; /* allow reselect should we fail */
Index: stand/boot/bootarg.h
===
RCS file: /cvs/src/sys/stand/boot/bootarg.h,v
retrieving revision 1.14
diff -u -p -r1.14 bootarg.h
--- stand/boot/bootarg.h2 Sep 2015 01:52:26 -   1.14
+++ stand/boot/bootarg.h8 Apr 2018 11:18:26 -
@@ -50,7 +50,8 @@ extern bootarg_t *bootargp;
 #endif
 
 void   loadrandom(char *name, char *buf, size_t buflen);
-intmdrandom(char *buf, size_t buflen);
+void   mdrandom(char *buf, size_t buflen);
+void   fwrandom(char *buf, size_t buflen);
 
 #ifdef _STANDALONE
 void addbootarg(int, size_t, void *);
Index: arch/arm64/stand/efiboot/Makefile
===
RCS file: /cvs/src/sys/arch/arm64/stand/efiboot/Makefile,v
retrieving revision 1.4
diff -u -p -r1.4 Makefile
--- arch/arm64/stand/efiboot/Makefile   31 Mar 2018 17:43:53 -  1.4
+++ arch/arm64/stand/efiboot/Makefile   8 Apr 2018 11:18:26 -
@@ -8,8 +8,8 @@ PROG=   BOOTAA64.EFI
 OBJFMT=binary
 INSTALL_STRIP=
 BINDIR=/usr/mdec
-SRCS=  start.S self_reloc.c efiboot.c conf.c exec.c efidev.c efipxe.c
-SRCS+= fdt.c
+SRCS=  start.S self_reloc.c efiboot.c conf.c exec.c efidev.c
+SRCS+= efipxe.c efirng.c fdt.c
 
 S= ${.CURDIR}/../../../..
 EFIDIR=${S}/stand/efi
@@ -43,7 +43,7 @@ CPPFLAGS+=-I${S} -I. -I${.CURDIR}
 CPPFLAGS+= -I${EFIDIR}/include -I${EFIDIR}/include/arm64
 CPPFLAGS+= -D_STANDALONE
 CPPFLAGS+= -DSMALL -DSLOW -DNOBYFOUR -D__INTERNAL_LIBSA_CREAD
-CPPFLAGS+= -DNEEDS_HEAP_H
+CPPFLAGS+= -DNEEDS_HEAP_H -DFWRANDOM
 COPTS+=-Wno-attributes -Wno-format
 COPTS+=-ffreestanding -fno-stack-protector
 COPTS+=-fshort-wchar -fPIC -fno-builtin
Index: arch/arm64/stand/efiboot/conf.c
===
RCS file: /cvs/src/sys/arch/arm64/stand/efiboot/conf.c,v
retrieving revision 1.14
diff -u -p -r1.14 conf.c
--- arch/arm64/stand/efiboot/conf.c 31 Mar 2018 17:44:57 -  1.14
+++ arch/arm64/stand/efiboot/conf.c 8 Apr 2018 11:18:26 -
@@ -36,7 +36,7 @@
 #include "efidev.h"
 #include "efipxe.h"
 
-const char version[] = "0.12";
+const char version[] = "0.13";
 intdebug = 0;
 
 struct fs_ops file_system[] = {
Index: arch/arm64/stand/efiboot/efirng.c
===
RCS file: arch/arm64/stand/efiboot/efirng.c
diff -N arch/arm64/stand/efiboot/efirng.c
--- /dev/null   1 Jan 1970 00:00:00 -
+++ arch/arm64/stand/efiboot/efirng.c   8 Apr 2018 11:18:26 -
@@ -0,0 +1,87 @@
+/* $OpenBSD$   */
+
+/*
+ * Copyright (c) 2018 Mark Kettenis 
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE 

Re: random numbers for efiboot

2018-04-07 Thread Theo de Raadt
Mark Kettenis  wrote:

> The UEFI firmware for the MACCHIATObin implements the EFI Random
> Number Generator Protocol.  That makes it possible to implement the
> mdrandom() function for arm64's EFIBOOT.  The random data is XORed
> onto the buffer so bad random data can't hurt us.
> 
> The code is written such that it can be easily dropped into our other
> EFI bootloaders.  That raises a question though.  On amd64 we already
> have an mdrandom() implementation.  Should I call the function
> fwrandom() instead and and an
> 
> #ifdef FWRANDOM
>fwrandom(rnddata, sizeof(rnddata));
> #endif
> 
> next to the mdrandom() call in boot.c?

Yes, it should call every thing like that.



random numbers for efiboot

2018-04-07 Thread Mark Kettenis
The UEFI firmware for the MACCHIATObin implements the EFI Random
Number Generator Protocol.  That makes it possible to implement the
mdrandom() function for arm64's EFIBOOT.  The random data is XORed
onto the buffer so bad random data can't hurt us.

The code is written such that it can be easily dropped into our other
EFI bootloaders.  That raises a question though.  On amd64 we already
have an mdrandom() implementation.  Should I call the function
fwrandom() instead and and an

#ifdef FWRANDOM
   fwrandom(rnddata, sizeof(rnddata));
#endif

next to the mdrandom() call in boot.c?


Index: arch/arm64/stand/efiboot/Makefile
===
RCS file: /cvs/src/sys/arch/arm64/stand/efiboot/Makefile,v
retrieving revision 1.4
diff -u -p -r1.4 Makefile
--- arch/arm64/stand/efiboot/Makefile   31 Mar 2018 17:43:53 -  1.4
+++ arch/arm64/stand/efiboot/Makefile   7 Apr 2018 21:04:50 -
@@ -8,8 +8,8 @@ PROG=   BOOTAA64.EFI
 OBJFMT=binary
 INSTALL_STRIP=
 BINDIR=/usr/mdec
-SRCS=  start.S self_reloc.c efiboot.c conf.c exec.c efidev.c efipxe.c
-SRCS+= fdt.c
+SRCS=  start.S self_reloc.c efiboot.c conf.c exec.c efidev.c
+SRCS+= efipxe.c efirng.c fdt.c
 
 S= ${.CURDIR}/../../../..
 EFIDIR=${S}/stand/efi
@@ -43,7 +43,7 @@ CPPFLAGS+=-I${S} -I. -I${.CURDIR}
 CPPFLAGS+= -I${EFIDIR}/include -I${EFIDIR}/include/arm64
 CPPFLAGS+= -D_STANDALONE
 CPPFLAGS+= -DSMALL -DSLOW -DNOBYFOUR -D__INTERNAL_LIBSA_CREAD
-CPPFLAGS+= -DNEEDS_HEAP_H
+CPPFLAGS+= -DNEEDS_HEAP_H -DMDRANDOM
 COPTS+=-Wno-attributes -Wno-format
 COPTS+=-ffreestanding -fno-stack-protector
 COPTS+=-fshort-wchar -fPIC -fno-builtin
Index: arch/arm64/stand/efiboot/conf.c
===
RCS file: /cvs/src/sys/arch/arm64/stand/efiboot/conf.c,v
retrieving revision 1.14
diff -u -p -r1.14 conf.c
--- arch/arm64/stand/efiboot/conf.c 31 Mar 2018 17:44:57 -  1.14
+++ arch/arm64/stand/efiboot/conf.c 7 Apr 2018 21:04:50 -
@@ -36,7 +36,7 @@
 #include "efidev.h"
 #include "efipxe.h"
 
-const char version[] = "0.12";
+const char version[] = "0.13";
 intdebug = 0;
 
 struct fs_ops file_system[] = {
Index: arch/arm64/stand/efiboot/efirng.c
===
RCS file: arch/arm64/stand/efiboot/efirng.c
diff -N arch/arm64/stand/efiboot/efirng.c
--- /dev/null   1 Jan 1970 00:00:00 -
+++ arch/arm64/stand/efiboot/efirng.c   7 Apr 2018 21:04:50 -
@@ -0,0 +1,87 @@
+/* $OpenBSD$   */
+
+/*
+ * Copyright (c) 2018 Mark Kettenis 
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include 
+
+#include 
+#include 
+
+#include "eficall.h"
+#include "libsa.h"
+
+extern EFI_BOOT_SERVICES   *BS;
+
+/* Random Number Generator Protocol */
+
+#define EFI_RNG_PROTOCOL_GUID \
+{ 0x3152bca5, 0xeade, 0x433d, {0x86, 0x2e, 0xc0, 0x1c, 0xdc, 0x29, 0x1f, 
0x44} }
+
+INTERFACE_DECL(_EFI_RNG_PROTOCOL);
+
+typedef EFI_GUID EFI_RNG_ALGORITHM;
+
+typedef
+EFI_STATUS
+(EFIAPI *EFI_RNG_GET_INFO) (
+IN struct _EFI_RNG_PROTOCOL*This,
+IN  OUT UINTN  *RNGAlgorithmListSize,
+OUT EFI_RNG_ALGORITHM  *RNGAlgorithmList
+);
+
+typedef
+EFI_STATUS
+(EFIAPI *EFI_RNG_GET_RNG) (
+IN struct _EFI_RNG_PROTOCOL*This,
+IN EFI_RNG_ALGORITHM   *RNGAlgorithm, OPTIONAL
+IN UINTN   RNGValueLength,
+OUT UINT8  *RNGValue
+);
+
+typedef struct _EFI_RNG_PROTOCOL {
+   EFI_RNG_GET_INFOGetInfo;
+   EFI_RNG_GET_RNG GetRNG;
+} EFI_RNG_PROTOCOL;
+
+static EFI_GUIDrng_guid = EFI_RNG_PROTOCOL_GUID;
+
+void
+mdrandom(char *buf, size_t buflen)
+{
+   EFI_STATUS   status;
+   EFI_RNG_PROTOCOL*rng = NULL;
+   UINT8   *random;
+   size_t   i;
+
+   status = EFI_CALL(BS->LocateProtocol, _guid, NULL, (void **));
+   if (rng == NULL || EFI_ERROR(status))
+   return;
+
+   random =