• 【UEFI实战】BIOS中的openssl


    BIOS中的openssl

    openssl是一个密码库或者密码工具,在密码学基础_hex string is too short, padding with zero bytes t-CSDN博客介绍了基本的密码学概念已经openssl工具的使用,而这里将介绍BIOS下如何使用openssl。

    在开源的BIOS代码库EDK中包含一个CryptoPkg,其中包含了BIOS需要使用到的密码库接口。通过包含openssl密码库代码,就可以在BIOS下使用密码学中的各种算法和工具。需要注意,EDK代码并不会直接包含openssl代码,而是通过外部链接的方式来实现的,如下图所示:

    在这里插入图片描述

    点击红框会跳转到对应的代码库,其中@之后的数字是对应的版本,这是因为无论是EDK还是openssl代码都一直在更新,所以存在兼容性的问题,通过制定版本信息,可以保证EDK可以正常编译和使用openssl。

    这样当我们直接下载到EDK代码时,并不会包含openssl代码,需要额外的下载,这可以通过git的submodule子命令来下载。后续使用的测试代码https://gitee.com/jiangwei0512/edk2-beni.git已经提供了一键式的编译方式,在第一次编译的时候就会下载对应的openssl版本。

    代码处理

    当下载了openssl代码之后,EDK利用已有的库OpensslLib来包含代码并进行编译,这样的库有多个,差别在于包含的openssl功能,不如如果需要支持HTTPS中的TLS功能,在需要包含OpensslLib.inf,否则包含OpensslLibCrypto.inf即可:

    !if $(NETWORK_TLS_ENABLE) == TRUE
      OpensslLib|CryptoPkg/Library/OpensslLib/OpensslLib.inf
    !else
      OpensslLib|CryptoPkg/Library/OpensslLib/OpensslLibCrypto.inf
    !endif
    
    • 1
    • 2
    • 3
    • 4
    • 5

    其它的还有OpensslLibAccel.infOpensslLibFull.infOpensslLibFullAccel.inf等,本质没有差异,只是包含功能的多寡。

    需要注意的是,这个OpensslLib库并不会直接被其它的EDK代码使用,中间还有包装了一层EDK通用的库,这些库会对应到BIOS的不同阶段或功能:

    # SEC
    [Components]
      #
      # SEC Phase modules
      #
      OvmfPkg/Sec/SecMain.inf {
        <LibraryClasses>
          NULL|MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaCustomDecompressLib.inf
          NULL|OvmfPkg/IntelTdx/TdxHelperLib/SecTdxHelperLib.inf
          BaseCryptLib|CryptoPkg/Library/BaseCryptLib/SecCryptLib.inf
      }
    
    # 通用
    [LibraryClasses.common]
      BaseCryptLib|CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
    
    # Runtime
    [LibraryClasses.common.DXE_RUNTIME_DRIVER]
      BaseCryptLib|CryptoPkg/Library/BaseCryptLib/RuntimeCryptLib.inf
    
    # SMM
    [LibraryClasses.common.DXE_SMM_DRIVER]
      BaseCryptLib|CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf
      
    # TLS
    !if $(NETWORK_TLS_ENABLE) == TRUE
      TlsLib|CryptoPkg/Library/TlsLib/TlsLib.inf
    !endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    因此当需要在BIOS下使用openssl,需要包含的是BaseCryptLib等库,并在代码中引入BaseCryptLib.h头文件。

    此外,还需要注意几个点。

    首先是为了开源的openssl能够正常的在EDK源码中正常编译,需要对其底层的一些基本函数进行包含,比如openssl代码中非常常用的memset()函数,如果是在Linux下编译,直接包含C标准库即可,但是EDK源码中是没有C标准库的,所以为了支持该命令,就需要对这些基础函数进行包装,这又引入了另外的一个EDK下的CrtLib(C RunTime Library),这可以通过edk2\CryptoPkg\Library\Include\CrtLibSupport.h看出来:

    #ifndef __CRT_LIB_SUPPORT_H__
    #define __CRT_LIB_SUPPORT_H__
    
    #include 
    #include 
    #include 
    #include 
    
    #define OPENSSLDIR  ""
    #define ENGINESDIR  ""
    #define MODULESDIR  ""
    
    #define MAX_STRING_SIZE  0x1000
    
    // 中间略
    
    //
    // Macros that directly map functions to BaseLib, BaseMemoryLib, and DebugLib functions
    //
    #define memcpy(dest, source, count)         CopyMem(dest,source,(UINTN)(count))
    #define memset(dest, ch, count)             SetMem(dest,(UINTN)(count),(UINT8)(ch))
    #define memchr(buf, ch, count)              ScanMem8(buf,(UINTN)(count),(UINT8)ch)
    #define memcmp(buf1, buf2, count)           (int)(CompareMem(buf1,buf2,(UINTN)(count)))
    #define memmove(dest, source, count)        CopyMem(dest,source,(UINTN)(count))
    #define strlen(str)                         (size_t)(AsciiStrnLenS(str,MAX_STRING_SIZE))
    #define strncpy(strDest, strSource, count)  AsciiStrnCpyS(strDest,MAX_STRING_SIZE,strSource,(UINTN)count)
    #define strcat(strDest, strSource)          AsciiStrCatS(strDest,MAX_STRING_SIZE,strSource)
    #define strncmp(string1, string2, count)    (int)(AsciiStrnCmp(string1,string2,(UINTN)(count)))
    #define strcasecmp(str1, str2)              (int)AsciiStriCmp(str1,str2)
    #define strstr(s1, s2)                      AsciiStrStr(s1,s2)
    #define sprintf(buf, ...)                   AsciiSPrint(buf,MAX_STRING_SIZE,__VA_ARGS__)
    #define localtime(timer)                    NULL
    #define assert(expression)
    #define offsetof(type, member)  OFFSET_OF(type,member)
    #define atoi(nptr)              AsciiStrDecimalToUintn(nptr)
    #define gettimeofday(tvp, tz)   do { (tvp)->tv_sec = time(NULL); (tvp)->tv_usec = 0; } while (0)
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    通过EDK下的另外一些库(比如IntrinsicLib库)就可以实现C标准库中的函数。而在openssl中包含的stdlib.h等C标注库头函数,其内部的实现就是包含CrtLibSupport.h这个头文件即可:

    /** @file
      Include file to support building the third-party cryptographic library.
    
    Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
    SPDX-License-Identifier: BSD-2-Clause-Patent **/
    #include
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    这就构成了如下的样式:

    +================================+
    | EDK II Firmware Module/Library |
    +================================+
         ^                 ^ 
         |                 |
         v                 v
    +========+  +====================+
    | TlsLib |  |    BaseCryptLib    |
    +========+  +====================+
         ^                ^
         |                |
         v                v
    +================================+
    |     OpensslLib (Private)       |
    +================================+
                   ^
                   |
                   v
    +================================+
    |     IntrinsicLib (Private)     |
    +================================+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    其次,EDK包含openssl的方式是库的形式,但是这个库的实现却有不同的方式,可以是真正的库,也可以在库中包含Protocol或PPI。前者在速度上应该会快一点点,而后者可以降低openssl包含到EDK中的占用空间。

    +===================+    +===================+     +===================+
    |    EDK II PEI     |    |  EDK II DXE/UEFI  |     |     EDK II SMM    |
    |   Module/Library  |    |   Module/Library  |     |   Module/Library  |
    +===================+    +===================+     +===================+
      ^            ^           ^            ^            ^            ^
      |            |           |            |            |            |
      v            v           v            v            v            v
    +===================+    +===================+     +===================+
    |TlsLib|BaseCryptLib|    |TlsLib|BaseCryptLib|     |TlsLib|BaseCryptLib|
    +-------------------+    +-------------------+     +-------------------+
    |   BaseCryptLib    |    |   BaseCryptLib    |     |   BaseCryptLib    |
    |   OnPpiProtocol/  |    |   OnPpiProtocol/  |     |   OnPpiProtocol/  |
    |  PeiCryptLib.inf  |    |   DxeCryptLib.inf |     |  SmmCryptLib.inf  |
    +===================+    +===================+     +===================+
               ^                      ^                         ^
              ||| (Dynamic)          ||| (Dynamic)             ||| (Dynamic)
               v                      v                         v
    +===================+    +===================+    +=====================+
    |     Crypto PPI    |    |  Crypto Protocol  |    | Crypto SMM Protocol |
    +-------------------|    |-------------------|    |---------------------|
    |     CryptoPei     |    |     CryptoDxe     |    |      CryptoSmm      |
    +===================+    +===================+    +=====================+
         ^       ^                ^       ^                 ^       ^
         |       |                |       |                 |       |
         v       |                v       |                 v       |
    +========+   |           +========+   |            +========+   |
    | TlsLib |   |           | TlsLib |   |            | TlsLib |   |
    +========+   v           +========+   v            +========+   v
      ^  +==============+      ^  +==============+       ^  +==============+
      |  | BaseCryptLib |      |  | BaseCryptLib |       |  | BaseCryptLib |
      |  +==============+      |  +==============+       |  +==============+
      |          ^             |          ^              |          ^
      |          |             |          |              |          |
      v          v             v          v              v          v
    +===================+    +===================+     +===================+
    |    OpensslLib     |    |    OpensslLib     |     |    OpensslLib     |
    +===================+    +===================+     +===================+
              ^                        ^                         ^
              |                        |                         |
              v                        v                         v
    +===================+    +===================+     +===================+
    |    IntrinsicLib   |    |    IntrinsicLib   |     |    IntrinsicLib   |
    +===================+    +===================+     +===================+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    根据使用的BIOS二进制的大小,可以选择不同的方式。

    代码示例

    BIOS启动过程中会执行BootLoader来加载系统,最常用的就是GRUB,它可能是一个名为bootx64.efi的UEFI应用。BIOS启动的最后会将控制权交给这个bootx64.efi,并由后者来启动系统。但是这里存在一个问题,如何保证这个应用真的是我们需要的呢?如果该应用被修改甚至替换了,导致执行一些我们不希望其执行的代码,则是一个非常严重的安全漏洞。

    在看过密码学基础_hex string is too short, padding with zero bytes t-CSDN博客之后就可以知道,可以使用数字签名的方式来解决bootx64.efi被修改的问题,其原理如下图所示:

    在这里插入图片描述

    实际使用的流程是这样的:

    1. 使用私钥对bootx64.efi进行加密,私钥由bootx64.efi的提供者保留,不能泄露。
    2. 将公钥放到BIOS代码中,启动时使用这个公钥校验bootx64.efi,成功的话再加载这个程序,否则就不加载。

    通过这个操作,如果bootx64.efi被修改了,这个程序就不会被执行,这样可以防止执行异常的代码。下面将介绍这两部分的内容。

    私钥加密

    首先是私钥的创建,这通过openssl工具就可以完成。

    1. 创建私钥:
    D:\Gitee\edk2-beni\beni\BeniPkg\Tools>openssl.exe genrsa -out private.pem 2048
    Generating RSA private key, 2048 bit long modulus (2 primes)
    ....................................................................+++++
    .....+++++
    e is 65537 (0x010001)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 从私钥提取出公钥:
    D:\Gitee\edk2-beni\beni\BeniPkg\Tools>openssl.exe rsa -in private.pem -pubout -out public.pem
    writing RSA key
    
    • 1
    • 2
    1. 对bootx64.efi(该文件手上暂时没有,使用helloworld.efi代替)的SHA256散列值进行加密:
    D:\Gitee\edk2-beni\beni\BeniPkg\Tools>openssl.exe dgst -sign private.pem -sha256 -out sign.bin helloworld.efi
    
    • 1

    private.pem是前面创建的私钥,helloworld.efi是被签名的文件(用来替代bootx64.efi做测试),sign.bin是输出文件,表示helloworld.efi的数字签名。

    1. 如果要验证签名,可以通过如下的命令:
    D:\Gitee\edk2-beni\beni\BeniPkg\Tools>openssl.exe dgst -verify public.pem -sha256 -signature sign.bin helloworld.efi
    Verified OK
    
    • 1
    • 2

    可以看到“Verified OK”,表示验证成功了。这里的public.pem是对应的公钥。后续我们的代码就是要实现这个步骤。

    1. 最后将原始文件(这里就是helloworld.efi)和数字签名放在一起,得到最终的efi文件:
    D:\Gitee\edk2-beni\beni\BeniPkg\Tools>copy /b helloworld.efi+sign.bin helloworld_signed.bin
    helloworld.efi
    sign.bin
    已复制         1 个文件。
    
    • 1
    • 2
    • 3
    • 4

    最终得到一个helloworld_signed.efi,它与原始的版本进行比较,其差异如下:

    在这里插入图片描述

    公钥解密

    这一部分需要BIOS的代码来实现,这里使用一个名为exec的命令来进行测试。

    首先看一下原始的代码,它位于beni/BeniPkg/DynamicCommand/ExecuteShellAppCommand/Exec.c · jiangwei/edk2-beni - 码云 - 开源中国 (gitee.com),其主要的实现:

    VOID
    Exec (
      IN  CONST CHAR16                  *AppName
      )
    {
      EFI_STATUS                Status = EFI_ABORTED;
      EFI_DEVICE_PATH_PROTOCOL  *DevPath = NULL;
      CHAR16                    *Str = NULL;
    
      DevPath = gEfiShellProtocol->GetDevicePathFromFilePath (AppName);
      if (NULL == DevPath) {
        DEBUG ((EFI_D_ERROR, "Device path not found!\n"));
        return;
      } else {
        Str = ConvertDevicePathToText (DevPath, TRUE, FALSE);
        if (Str) {
          DEBUG ((EFI_D_ERROR, "DevPath: %s\n", Str));
          FreePool (Str);
        }
      }
    
      Status = gEfiShellProtocol->Execute (&gImageHandle, (CHAR16 *)AppName, NULL, NULL);
      if (EFI_ERROR (Status)) {
        DEBUG ((EFI_D_ERROR, "Execute failed. - %r\n", Status));
      }
    
      return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    这是Shell下的命令,通过如下的命令来执行:

    exec helloworld.efi
    
    • 1

    helloworld.efi是一个Shell应用(未加密),而该操作会执行这个命令。

    但是当加入了安全元素,就有了helloworld_signed.efi,因此代码中也需要加入相关的处理,在Exec()函数中增加判断:

    VOID
    Exec (
      IN  CONST CHAR16                  *AppName
      )
    {
      EFI_STATUS                Status = EFI_ABORTED;
      EFI_DEVICE_PATH_PROTOCOL  *DevPath = NULL;
      CHAR16                    *Str = NULL;
    
      if (!(SecureCheck (AppName))) {
        ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SECURE_ERROR), mExecHiiHandle);
        return;
      } else {
        ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_SECURE_SUCCESS), mExecHiiHandle);
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    SecureCheck()就要用到openssl,其操作流程大致如下:

    读取文件内容
    对读取内容做SHA256散列计算
    将散列值进行公钥验签
    判断验签结果

    下面分别说明上述的步骤:

    1. 读取文件不多做介绍,调用一般接口即可完成。
    2. 对读取内容进行散列计算,使用的是SHA256,因为私钥加密中做的就是SHA256,两者必须要对应,其代码如下(为了方便查看,只保留主干代码):
      HashContext = AllocateZeroPool (Sha256GetContextSize ());
    
      CryptoStatus = Sha256Init (HashContext);
    
      CryptoStatus = Sha256Update (HashContext, FileBuffer, (FileSize - RSA_LEN));
    
      CryptoStatus = Sha256Final (HashContext, Digest);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    这里需要注意的是现在的应用包含了原始内容数字签名两个部分,而散列只针对前一个部分。

    1. 之后是验签。这里需要使用到公钥,这部分在私钥加密的介绍中已经生成,不过需要进行转化才能在代码中使用:
    D:\Gitee\edk2-beni\beni\BeniPkg\Tools>openssl.exe rsa -in private.pem -text
    RSA Private-Key: (2048 bit, 2 primes)
    modulus:
        00:91:ad:ec:3f:4d:85:5f:c0:a7:95:14:92:6c:2f:
        0d:37:6e:58:2d:9a:06:0b:07:c0:15:90:1e:d9:70:
        25:a5:fe:87:68:c3:cd:a2:e5:d4:d7:3c:06:1f:30:
        a3:81:a7:6a:f0:27:aa:26:0c:cb:7d:cb:c2:2c:c6:
        67:b5:76:ef:30:4d:8d:12:6b:4d:20:11:2c:c4:69:
        a6:9b:db:0e:c8:ae:3e:cc:a8:e3:83:b9:80:5b:d2:
        97:3c:e2:e7:85:5a:db:53:23:8a:b4:a0:f8:02:f3:
        03:ec:41:37:97:d0:b5:35:f5:01:d9:3b:e8:24:24:
        ef:39:80:40:5e:c0:c6:b5:3d:32:3b:f1:4b:80:a9:
        2d:93:06:d4:8e:06:b6:b0:3e:ce:6a:17:75:28:32:
        50:a4:c1:86:4c:c0:46:bb:8d:83:6c:8e:53:96:72:
        7d:99:85:6f:19:b5:0c:33:1e:00:57:19:15:59:6b:
        58:30:dc:c5:00:0d:7c:cc:37:05:00:4f:17:a7:41:
        05:e0:d2:f7:67:67:f8:ce:77:a3:1b:9a:45:cf:04:
        14:04:9a:df:58:9d:2a:99:00:f7:16:94:ad:90:77:
        86:ff:6e:6b:03:d3:80:f3:f6:de:d9:cc:89:cc:bc:
        3b:f9:42:06:5d:ba:9b:93:96:b6:f3:e0:fd:98:a1:
        ff:9b
    publicExponent: 65537 (0x10001)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    这里的moduluspublicExponent是代码中需要使用到的值,最终在代码中:

    ///
    /// Public modulus of RSA Key.
    ///
    CONST UINT8 mPublicKey[] = {
      0x91, 0xad, 0xec, 0x3f, 0x4d, 0x85, 0x5f, 0xc0, 0xa7, 0x95, 0x14, 0x92, 0x6c, 0x2f, 0x0d, 0x37,
      0x6e, 0x58, 0x2d, 0x9a, 0x06, 0x0b, 0x07, 0xc0, 0x15, 0x90, 0x1e, 0xd9, 0x70, 0x25, 0xa5, 0xfe,
      0x87, 0x68, 0xc3, 0xcd, 0xa2, 0xe5, 0xd4, 0xd7, 0x3c, 0x06, 0x1f, 0x30, 0xa3, 0x81, 0xa7, 0x6a,
      0xf0, 0x27, 0xaa, 0x26, 0x0c, 0xcb, 0x7d, 0xcb, 0xc2, 0x2c, 0xc6, 0x67, 0xb5, 0x76, 0xef, 0x30,
      0x4d, 0x8d, 0x12, 0x6b, 0x4d, 0x20, 0x11, 0x2c, 0xc4, 0x69, 0xa6, 0x9b, 0xdb, 0x0e, 0xc8, 0xae,
      0x3e, 0xcc, 0xa8, 0xe3, 0x83, 0xb9, 0x80, 0x5b, 0xd2, 0x97, 0x3c, 0xe2, 0xe7, 0x85, 0x5a, 0xdb,
      0x53, 0x23, 0x8a, 0xb4, 0xa0, 0xf8, 0x02, 0xf3, 0x03, 0xec, 0x41, 0x37, 0x97, 0xd0, 0xb5, 0x35,
      0xf5, 0x01, 0xd9, 0x3b, 0xe8, 0x24, 0x24, 0xef, 0x39, 0x80, 0x40, 0x5e, 0xc0, 0xc6, 0xb5, 0x3d,
      0x32, 0x3b, 0xf1, 0x4b, 0x80, 0xa9, 0x2d, 0x93, 0x06, 0xd4, 0x8e, 0x06, 0xb6, 0xb0, 0x3e, 0xce,
      0x6a, 0x17, 0x75, 0x28, 0x32, 0x50, 0xa4, 0xc1, 0x86, 0x4c, 0xc0, 0x46, 0xbb, 0x8d, 0x83, 0x6c,
      0x8e, 0x53, 0x96, 0x72, 0x7d, 0x99, 0x85, 0x6f, 0x19, 0xb5, 0x0c, 0x33, 0x1e, 0x00, 0x57, 0x19,
      0x15, 0x59, 0x6b, 0x58, 0x30, 0xdc, 0xc5, 0x00, 0x0d, 0x7c, 0xcc, 0x37, 0x05, 0x00, 0x4f, 0x17,
      0xa7, 0x41, 0x05, 0xe0, 0xd2, 0xf7, 0x67, 0x67, 0xf8, 0xce, 0x77, 0xa3, 0x1b, 0x9a, 0x45, 0xcf,
      0x04, 0x14, 0x04, 0x9a, 0xdf, 0x58, 0x9d, 0x2a, 0x99, 0x00, 0xf7, 0x16, 0x94, 0xad, 0x90, 0x77,
      0x86, 0xff, 0x6e, 0x6b, 0x03, 0xd3, 0x80, 0xf3, 0xf6, 0xde, 0xd9, 0xcc, 0x89, 0xcc, 0xbc, 0x3b,
      0xf9, 0x42, 0x06, 0x5d, 0xba, 0x9b, 0x93, 0x96, 0xb6, 0xf3, 0xe0, 0xfd, 0x98, 0xa1, 0xff, 0x9b,
    };
    
    ///
    /// Public exponent of RSA Key.
    ///
    CONST UINT8 mRsaE[] = {
      0x01, 0x00, 0x01
      };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    有了公钥之后就可以进行验签,其代码处理也比较简单:

    ![exec_helloworld_signed](BIOS.assets/exec_helloworld_signed.png)  Rsa = RsaNew ();
    
      CryptoStatus = RsaSetKey (Rsa, RsaKeyN, mPublicKey, sizeof (mPublicKey));
    
      CryptoStatus = RsaSetKey (Rsa, RsaKeyE, mRsaE, sizeof (mRsaE));
    
      CryptoStatus = RsaPkcs1Verify (
                      Rsa,
                      Digest,
                      SHA256_DIGEST_SIZE,
                      FileBuffer + (FileSize - RSA_LEN),
                      RSA_LEN
                      );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    RsaPkcs1Verify()接受的参数:

    • Rsa:RSA上下文。
    • DigestSHA256_DIGEST_SIZE:SHA256散列值及其大小。
    • FileBuffer + (FileSize - RSA_LEN)RSA_LEN:数字签名及其大小,因为使用了2046比特的签名,所以这个值是256。

    FileBuffer + (FileSize - RSA_LEN)对应的就是应用程序最后的256个字节。

    测试结果

    以上的二进制和代码都已经包含在edk2-beni: 用于学习和验证UEFI BIOS。 (gitee.com),将helloworld_signed.efi包含在代码中,最终进入到Shell之后会被放到fs0:中,执行该程序可以成功,但是当修改helloworld_signed.efi中的任意一个字节,则会报错:

    在这里插入图片描述

  • 相关阅读:
    记一次edu站点并拿下的过程cnvd
    Kubernetes的原理及应用详解(三)
    能够注入Bean的XXXUtil工具类
    我的OpenAI库发布了!!!
    Linux命令详解
    PTA 7-86 分支结构——大小写字母判断
    代码随想录算法训练营第三十二天 | LeetCode 122. 买卖股票的最佳时机 II、55. 跳跃游戏、45. 跳跃游戏 II
    系统性能指标
    【真实面试】JAVA22年初的开发面试
    Linux发展史
  • 原文地址:https://blog.csdn.net/jiangwei0512/article/details/136334734