selftests/x86: Add a selftest for SGX
[linux-2.6-block.git] / tools / testing / selftests / sgx / sigstruct.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*  Copyright(c) 2016-20 Intel Corporation. */
3
4 #define _GNU_SOURCE
5 #include <assert.h>
6 #include <getopt.h>
7 #include <stdbool.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/stat.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15 #include <openssl/err.h>
16 #include <openssl/pem.h>
17 #include "defines.h"
18 #include "main.h"
19
20 struct q1q2_ctx {
21         BN_CTX *bn_ctx;
22         BIGNUM *m;
23         BIGNUM *s;
24         BIGNUM *q1;
25         BIGNUM *qr;
26         BIGNUM *q2;
27 };
28
29 static void free_q1q2_ctx(struct q1q2_ctx *ctx)
30 {
31         BN_CTX_free(ctx->bn_ctx);
32         BN_free(ctx->m);
33         BN_free(ctx->s);
34         BN_free(ctx->q1);
35         BN_free(ctx->qr);
36         BN_free(ctx->q2);
37 }
38
39 static bool alloc_q1q2_ctx(const uint8_t *s, const uint8_t *m,
40                            struct q1q2_ctx *ctx)
41 {
42         ctx->bn_ctx = BN_CTX_new();
43         ctx->s = BN_bin2bn(s, SGX_MODULUS_SIZE, NULL);
44         ctx->m = BN_bin2bn(m, SGX_MODULUS_SIZE, NULL);
45         ctx->q1 = BN_new();
46         ctx->qr = BN_new();
47         ctx->q2 = BN_new();
48
49         if (!ctx->bn_ctx || !ctx->s || !ctx->m || !ctx->q1 || !ctx->qr ||
50             !ctx->q2) {
51                 free_q1q2_ctx(ctx);
52                 return false;
53         }
54
55         return true;
56 }
57
58 static bool calc_q1q2(const uint8_t *s, const uint8_t *m, uint8_t *q1,
59                       uint8_t *q2)
60 {
61         struct q1q2_ctx ctx;
62
63         if (!alloc_q1q2_ctx(s, m, &ctx)) {
64                 fprintf(stderr, "Not enough memory for Q1Q2 calculation\n");
65                 return false;
66         }
67
68         if (!BN_mul(ctx.q1, ctx.s, ctx.s, ctx.bn_ctx))
69                 goto out;
70
71         if (!BN_div(ctx.q1, ctx.qr, ctx.q1, ctx.m, ctx.bn_ctx))
72                 goto out;
73
74         if (BN_num_bytes(ctx.q1) > SGX_MODULUS_SIZE) {
75                 fprintf(stderr, "Too large Q1 %d bytes\n",
76                         BN_num_bytes(ctx.q1));
77                 goto out;
78         }
79
80         if (!BN_mul(ctx.q2, ctx.s, ctx.qr, ctx.bn_ctx))
81                 goto out;
82
83         if (!BN_div(ctx.q2, NULL, ctx.q2, ctx.m, ctx.bn_ctx))
84                 goto out;
85
86         if (BN_num_bytes(ctx.q2) > SGX_MODULUS_SIZE) {
87                 fprintf(stderr, "Too large Q2 %d bytes\n",
88                         BN_num_bytes(ctx.q2));
89                 goto out;
90         }
91
92         BN_bn2bin(ctx.q1, q1);
93         BN_bn2bin(ctx.q2, q2);
94
95         free_q1q2_ctx(&ctx);
96         return true;
97 out:
98         free_q1q2_ctx(&ctx);
99         return false;
100 }
101
102 struct sgx_sigstruct_payload {
103         struct sgx_sigstruct_header header;
104         struct sgx_sigstruct_body body;
105 };
106
107 static bool check_crypto_errors(void)
108 {
109         int err;
110         bool had_errors = false;
111         const char *filename;
112         int line;
113         char str[256];
114
115         for ( ; ; ) {
116                 if (ERR_peek_error() == 0)
117                         break;
118
119                 had_errors = true;
120                 err = ERR_get_error_line(&filename, &line);
121                 ERR_error_string_n(err, str, sizeof(str));
122                 fprintf(stderr, "crypto: %s: %s:%d\n", str, filename, line);
123         }
124
125         return had_errors;
126 }
127
128 static inline const BIGNUM *get_modulus(RSA *key)
129 {
130         const BIGNUM *n;
131
132         RSA_get0_key(key, &n, NULL, NULL);
133         return n;
134 }
135
136 static RSA *gen_sign_key(void)
137 {
138         BIGNUM *e;
139         RSA *key;
140         int ret;
141
142         e = BN_new();
143         key = RSA_new();
144
145         if (!e || !key)
146                 goto err;
147
148         ret = BN_set_word(e, RSA_3);
149         if (ret != 1)
150                 goto err;
151
152         ret = RSA_generate_key_ex(key, 3072, e, NULL);
153         if (ret != 1)
154                 goto err;
155
156         BN_free(e);
157
158         return key;
159
160 err:
161         RSA_free(key);
162         BN_free(e);
163
164         return NULL;
165 }
166
167 static void reverse_bytes(void *data, int length)
168 {
169         int i = 0;
170         int j = length - 1;
171         uint8_t temp;
172         uint8_t *ptr = data;
173
174         while (i < j) {
175                 temp = ptr[i];
176                 ptr[i] = ptr[j];
177                 ptr[j] = temp;
178                 i++;
179                 j--;
180         }
181 }
182
183 enum mrtags {
184         MRECREATE = 0x0045544145524345,
185         MREADD = 0x0000000044444145,
186         MREEXTEND = 0x00444E4554584545,
187 };
188
189 static bool mrenclave_update(EVP_MD_CTX *ctx, const void *data)
190 {
191         if (!EVP_DigestUpdate(ctx, data, 64)) {
192                 fprintf(stderr, "digest update failed\n");
193                 return false;
194         }
195
196         return true;
197 }
198
199 static bool mrenclave_commit(EVP_MD_CTX *ctx, uint8_t *mrenclave)
200 {
201         unsigned int size;
202
203         if (!EVP_DigestFinal_ex(ctx, (unsigned char *)mrenclave, &size)) {
204                 fprintf(stderr, "digest commit failed\n");
205                 return false;
206         }
207
208         if (size != 32) {
209                 fprintf(stderr, "invalid digest size = %u\n", size);
210                 return false;
211         }
212
213         return true;
214 }
215
216 struct mrecreate {
217         uint64_t tag;
218         uint32_t ssaframesize;
219         uint64_t size;
220         uint8_t reserved[44];
221 } __attribute__((__packed__));
222
223
224 static bool mrenclave_ecreate(EVP_MD_CTX *ctx, uint64_t blob_size)
225 {
226         struct mrecreate mrecreate;
227         uint64_t encl_size;
228
229         for (encl_size = 0x1000; encl_size < blob_size; )
230                 encl_size <<= 1;
231
232         memset(&mrecreate, 0, sizeof(mrecreate));
233         mrecreate.tag = MRECREATE;
234         mrecreate.ssaframesize = 1;
235         mrecreate.size = encl_size;
236
237         if (!EVP_DigestInit_ex(ctx, EVP_sha256(), NULL))
238                 return false;
239
240         return mrenclave_update(ctx, &mrecreate);
241 }
242
243 struct mreadd {
244         uint64_t tag;
245         uint64_t offset;
246         uint64_t flags; /* SECINFO flags */
247         uint8_t reserved[40];
248 } __attribute__((__packed__));
249
250 static bool mrenclave_eadd(EVP_MD_CTX *ctx, uint64_t offset, uint64_t flags)
251 {
252         struct mreadd mreadd;
253
254         memset(&mreadd, 0, sizeof(mreadd));
255         mreadd.tag = MREADD;
256         mreadd.offset = offset;
257         mreadd.flags = flags;
258
259         return mrenclave_update(ctx, &mreadd);
260 }
261
262 struct mreextend {
263         uint64_t tag;
264         uint64_t offset;
265         uint8_t reserved[48];
266 } __attribute__((__packed__));
267
268 static bool mrenclave_eextend(EVP_MD_CTX *ctx, uint64_t offset,
269                               const uint8_t *data)
270 {
271         struct mreextend mreextend;
272         int i;
273
274         for (i = 0; i < 0x1000; i += 0x100) {
275                 memset(&mreextend, 0, sizeof(mreextend));
276                 mreextend.tag = MREEXTEND;
277                 mreextend.offset = offset + i;
278
279                 if (!mrenclave_update(ctx, &mreextend))
280                         return false;
281
282                 if (!mrenclave_update(ctx, &data[i + 0x00]))
283                         return false;
284
285                 if (!mrenclave_update(ctx, &data[i + 0x40]))
286                         return false;
287
288                 if (!mrenclave_update(ctx, &data[i + 0x80]))
289                         return false;
290
291                 if (!mrenclave_update(ctx, &data[i + 0xC0]))
292                         return false;
293         }
294
295         return true;
296 }
297
298 static bool mrenclave_segment(EVP_MD_CTX *ctx, struct encl *encl,
299                               struct encl_segment *seg)
300 {
301         uint64_t end = seg->offset + seg->size;
302         uint64_t offset;
303
304         for (offset = seg->offset; offset < end; offset += PAGE_SIZE) {
305                 if (!mrenclave_eadd(ctx, offset, seg->flags))
306                         return false;
307
308                 if (!mrenclave_eextend(ctx, offset, encl->src + offset))
309                         return false;
310         }
311
312         return true;
313 }
314
315 bool encl_measure(struct encl *encl)
316 {
317         uint64_t header1[2] = {0x000000E100000006, 0x0000000000010000};
318         uint64_t header2[2] = {0x0000006000000101, 0x0000000100000060};
319         struct sgx_sigstruct *sigstruct = &encl->sigstruct;
320         struct sgx_sigstruct_payload payload;
321         uint8_t digest[SHA256_DIGEST_LENGTH];
322         unsigned int siglen;
323         RSA *key = NULL;
324         EVP_MD_CTX *ctx;
325         int i;
326
327         memset(sigstruct, 0, sizeof(*sigstruct));
328
329         sigstruct->header.header1[0] = header1[0];
330         sigstruct->header.header1[1] = header1[1];
331         sigstruct->header.header2[0] = header2[0];
332         sigstruct->header.header2[1] = header2[1];
333         sigstruct->exponent = 3;
334         sigstruct->body.attributes = SGX_ATTR_MODE64BIT;
335         sigstruct->body.xfrm = 3;
336
337         /* sanity check */
338         if (check_crypto_errors())
339                 goto err;
340
341         key = gen_sign_key();
342         if (!key)
343                 goto err;
344
345         BN_bn2bin(get_modulus(key), sigstruct->modulus);
346
347         ctx = EVP_MD_CTX_create();
348         if (!ctx)
349                 goto err;
350
351         if (!mrenclave_ecreate(ctx, encl->src_size))
352                 goto err;
353
354         for (i = 0; i < encl->nr_segments; i++) {
355                 struct encl_segment *seg = &encl->segment_tbl[i];
356
357                 if (!mrenclave_segment(ctx, encl, seg))
358                         goto err;
359         }
360
361         if (!mrenclave_commit(ctx, sigstruct->body.mrenclave))
362                 goto err;
363
364         memcpy(&payload.header, &sigstruct->header, sizeof(sigstruct->header));
365         memcpy(&payload.body, &sigstruct->body, sizeof(sigstruct->body));
366
367         SHA256((unsigned char *)&payload, sizeof(payload), digest);
368
369         if (!RSA_sign(NID_sha256, digest, SHA256_DIGEST_LENGTH,
370                       sigstruct->signature, &siglen, key))
371                 goto err;
372
373         if (!calc_q1q2(sigstruct->signature, sigstruct->modulus, sigstruct->q1,
374                        sigstruct->q2))
375                 goto err;
376
377         /* BE -> LE */
378         reverse_bytes(sigstruct->signature, SGX_MODULUS_SIZE);
379         reverse_bytes(sigstruct->modulus, SGX_MODULUS_SIZE);
380         reverse_bytes(sigstruct->q1, SGX_MODULUS_SIZE);
381         reverse_bytes(sigstruct->q2, SGX_MODULUS_SIZE);
382
383         EVP_MD_CTX_destroy(ctx);
384         RSA_free(key);
385         return true;
386
387 err:
388         EVP_MD_CTX_destroy(ctx);
389         RSA_free(key);
390         return false;
391 }