[tip: x86/sgx] selftests/sgx: Use a statically generated 3072-bit RSA key

From: tip-bot2 for Jarkko Sakkinen
Date: Wed Nov 18 2020 - 12:40:37 EST


The following commit has been merged into the x86/sgx branch of tip:

Commit-ID: 0eaa8d153a1d573e53b8283c90db44057d1376f6
Gitweb: https://git.kernel.org/tip/0eaa8d153a1d573e53b8283c90db44057d1376f6
Author: Jarkko Sakkinen <jarkko@xxxxxxxxxx>
AuthorDate: Wed, 18 Nov 2020 19:06:40 +02:00
Committer: Borislav Petkov <bp@xxxxxxx>
CommitterDate: Wed, 18 Nov 2020 18:26:00 +01:00

selftests/sgx: Use a statically generated 3072-bit RSA key

Use a statically generated key for signing the enclave, because
generating keys on the fly can eat the kernel entropy pool. Another
good reason for doing this is predictable builds. The RSA has been
arbitrarily selected. It's contents do not matter.

This also makes the selftest execute a lot quicker instead of the delay
that it had before (because of slow key generation).

[ bp: Disambiguate "static key" which means something else in the
kernel, fix typos. ]

Signed-off-by: Jarkko Sakkinen <jarkko@xxxxxxxxxx>
Signed-off-by: Borislav Petkov <bp@xxxxxxx>
Cc: linux-kselftest@xxxxxxxxxxxxxxx
Link: https://lkml.kernel.org/r/20201118170640.39629-1-jarkko@xxxxxxxxxx
---
tools/testing/selftests/sgx/Makefile | 6 +++-
tools/testing/selftests/sgx/main.h | 3 ++-
tools/testing/selftests/sgx/sign_key.S | 12 +++++++-
tools/testing/selftests/sgx/sign_key.pem | 39 +++++++++++++++++++++++-
tools/testing/selftests/sgx/sigstruct.c | 34 +++++++-------------
5 files changed, 71 insertions(+), 23 deletions(-)
create mode 100644 tools/testing/selftests/sgx/sign_key.S
create mode 100644 tools/testing/selftests/sgx/sign_key.pem

diff --git a/tools/testing/selftests/sgx/Makefile b/tools/testing/selftests/sgx/Makefile
index d51c906..7f12d55 100644
--- a/tools/testing/selftests/sgx/Makefile
+++ b/tools/testing/selftests/sgx/Makefile
@@ -25,7 +25,8 @@ endif
$(OUTPUT)/test_sgx: $(OUTPUT)/main.o \
$(OUTPUT)/load.o \
$(OUTPUT)/sigstruct.o \
- $(OUTPUT)/call.o
+ $(OUTPUT)/call.o \
+ $(OUTPUT)/sign_key.o
$(CC) $(HOST_CFLAGS) -o $@ $^ -lcrypto

$(OUTPUT)/main.o: main.c
@@ -40,6 +41,9 @@ $(OUTPUT)/sigstruct.o: sigstruct.c
$(OUTPUT)/call.o: call.S
$(CC) $(HOST_CFLAGS) -c $< -o $@

+$(OUTPUT)/sign_key.o: sign_key.S
+ $(CC) $(HOST_CFLAGS) -c $< -o $@
+
$(OUTPUT)/test_encl.elf: test_encl.lds test_encl.c test_encl_bootstrap.S
$(CC) $(ENCL_CFLAGS) -T $^ -o $@

diff --git a/tools/testing/selftests/sgx/main.h b/tools/testing/selftests/sgx/main.h
index 45e6ab6..67211a7 100644
--- a/tools/testing/selftests/sgx/main.h
+++ b/tools/testing/selftests/sgx/main.h
@@ -27,6 +27,9 @@ struct encl {
struct sgx_sigstruct sigstruct;
};

+extern unsigned char sign_key[];
+extern unsigned char sign_key_end[];
+
void encl_delete(struct encl *ctx);
bool encl_load(const char *path, struct encl *encl);
bool encl_measure(struct encl *encl);
diff --git a/tools/testing/selftests/sgx/sign_key.S b/tools/testing/selftests/sgx/sign_key.S
new file mode 100644
index 0000000..e4fbe94
--- /dev/null
+++ b/tools/testing/selftests/sgx/sign_key.S
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/**
+* Copyright(c) 2016-20 Intel Corporation.
+*/
+
+ .section ".rodata", "a"
+
+sign_key:
+ .globl sign_key
+ .incbin "sign_key.pem"
+sign_key_end:
+ .globl sign_key_end
diff --git a/tools/testing/selftests/sgx/sign_key.pem b/tools/testing/selftests/sgx/sign_key.pem
new file mode 100644
index 0000000..d76f21f
--- /dev/null
+++ b/tools/testing/selftests/sgx/sign_key.pem
@@ -0,0 +1,39 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIG4wIBAAKCAYEApalGbq7Q+usM91CPtksu3D+b0Prc8gAF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Nea4LVZ2kn4hBXXlvw/4HD+YqcTt4wmif
+5JQlDvjNobUiKJZpzy7hklVhF7wZFl4pCF7Yh43q9iQ7gKTaeUG7MiaK+G8Zz8aY
+HW9rsiihbdZkccMvnPfO9334XMxl3HtBRzLstjUlbLB7Sdh+7tZ3JQidCOFNs5pE
+XyWwnASPu4tKfDahH1UUTp1uJcq/6716CSWg080avYxFcn75qqsb
+-----END RSA PRIVATE KEY-----
diff --git a/tools/testing/selftests/sgx/sigstruct.c b/tools/testing/selftests/sgx/sigstruct.c
index cc06f10..dee7a3d 100644
--- a/tools/testing/selftests/sgx/sigstruct.c
+++ b/tools/testing/selftests/sgx/sigstruct.c
@@ -135,33 +135,21 @@ static inline const BIGNUM *get_modulus(RSA *key)

static RSA *gen_sign_key(void)
{
- BIGNUM *e;
+ unsigned long sign_key_length;
+ BIO *bio;
RSA *key;
- int ret;

- e = BN_new();
- key = RSA_new();
+ sign_key_length = (unsigned long)&sign_key_end -
+ (unsigned long)&sign_key;

- if (!e || !key)
- goto err;
-
- ret = BN_set_word(e, RSA_3);
- if (ret != 1)
- goto err;
-
- ret = RSA_generate_key_ex(key, 3072, e, NULL);
- if (ret != 1)
- goto err;
+ bio = BIO_new_mem_buf(&sign_key, sign_key_length);
+ if (!bio)
+ return NULL;

- BN_free(e);
+ key = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL);
+ BIO_free(bio);

return key;
-
-err:
- RSA_free(key);
- BN_free(e);
-
- return NULL;
}

static void reverse_bytes(void *data, int length)
@@ -339,8 +327,10 @@ bool encl_measure(struct encl *encl)
goto err;

key = gen_sign_key();
- if (!key)
+ if (!key) {
+ ERR_print_errors_fp(stdout);
goto err;
+ }

BN_bn2bin(get_modulus(key), sigstruct->modulus);