sigstruct.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2016-20 Intel Corporation. */
  3. #define _GNU_SOURCE
  4. #include <assert.h>
  5. #include <getopt.h>
  6. #include <stdbool.h>
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <sys/stat.h>
  12. #include <sys/types.h>
  13. #include <unistd.h>
  14. #include <openssl/err.h>
  15. #include <openssl/pem.h>
  16. #include "defines.h"
  17. #include "main.h"
  18. /*
  19. * FIXME: OpenSSL 3.0 has deprecated some functions. For now just ignore
  20. * the warnings.
  21. */
  22. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  23. struct q1q2_ctx {
  24. BN_CTX *bn_ctx;
  25. BIGNUM *m;
  26. BIGNUM *s;
  27. BIGNUM *q1;
  28. BIGNUM *qr;
  29. BIGNUM *q2;
  30. };
  31. static void free_q1q2_ctx(struct q1q2_ctx *ctx)
  32. {
  33. BN_CTX_free(ctx->bn_ctx);
  34. BN_free(ctx->m);
  35. BN_free(ctx->s);
  36. BN_free(ctx->q1);
  37. BN_free(ctx->qr);
  38. BN_free(ctx->q2);
  39. }
  40. static bool alloc_q1q2_ctx(const uint8_t *s, const uint8_t *m,
  41. struct q1q2_ctx *ctx)
  42. {
  43. ctx->bn_ctx = BN_CTX_new();
  44. ctx->s = BN_bin2bn(s, SGX_MODULUS_SIZE, NULL);
  45. ctx->m = BN_bin2bn(m, SGX_MODULUS_SIZE, NULL);
  46. ctx->q1 = BN_new();
  47. ctx->qr = BN_new();
  48. ctx->q2 = BN_new();
  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. return true;
  55. }
  56. static void reverse_bytes(void *data, int length)
  57. {
  58. int i = 0;
  59. int j = length - 1;
  60. uint8_t temp;
  61. uint8_t *ptr = data;
  62. while (i < j) {
  63. temp = ptr[i];
  64. ptr[i] = ptr[j];
  65. ptr[j] = temp;
  66. i++;
  67. j--;
  68. }
  69. }
  70. static bool calc_q1q2(const uint8_t *s, const uint8_t *m, uint8_t *q1,
  71. uint8_t *q2)
  72. {
  73. struct q1q2_ctx ctx;
  74. int len;
  75. if (!alloc_q1q2_ctx(s, m, &ctx)) {
  76. fprintf(stderr, "Not enough memory for Q1Q2 calculation\n");
  77. return false;
  78. }
  79. if (!BN_mul(ctx.q1, ctx.s, ctx.s, ctx.bn_ctx))
  80. goto out;
  81. if (!BN_div(ctx.q1, ctx.qr, ctx.q1, ctx.m, ctx.bn_ctx))
  82. goto out;
  83. if (BN_num_bytes(ctx.q1) > SGX_MODULUS_SIZE) {
  84. fprintf(stderr, "Too large Q1 %d bytes\n",
  85. BN_num_bytes(ctx.q1));
  86. goto out;
  87. }
  88. if (!BN_mul(ctx.q2, ctx.s, ctx.qr, ctx.bn_ctx))
  89. goto out;
  90. if (!BN_div(ctx.q2, NULL, ctx.q2, ctx.m, ctx.bn_ctx))
  91. goto out;
  92. if (BN_num_bytes(ctx.q2) > SGX_MODULUS_SIZE) {
  93. fprintf(stderr, "Too large Q2 %d bytes\n",
  94. BN_num_bytes(ctx.q2));
  95. goto out;
  96. }
  97. len = BN_bn2bin(ctx.q1, q1);
  98. reverse_bytes(q1, len);
  99. len = BN_bn2bin(ctx.q2, q2);
  100. reverse_bytes(q2, len);
  101. free_q1q2_ctx(&ctx);
  102. return true;
  103. out:
  104. free_q1q2_ctx(&ctx);
  105. return false;
  106. }
  107. struct sgx_sigstruct_payload {
  108. struct sgx_sigstruct_header header;
  109. struct sgx_sigstruct_body body;
  110. };
  111. static bool check_crypto_errors(void)
  112. {
  113. int err;
  114. bool had_errors = false;
  115. const char *filename;
  116. int line;
  117. char str[256];
  118. for ( ; ; ) {
  119. if (ERR_peek_error() == 0)
  120. break;
  121. had_errors = true;
  122. err = ERR_get_error_line(&filename, &line);
  123. ERR_error_string_n(err, str, sizeof(str));
  124. fprintf(stderr, "crypto: %s: %s:%d\n", str, filename, line);
  125. }
  126. return had_errors;
  127. }
  128. static inline const BIGNUM *get_modulus(RSA *key)
  129. {
  130. const BIGNUM *n;
  131. RSA_get0_key(key, &n, NULL, NULL);
  132. return n;
  133. }
  134. static RSA *gen_sign_key(void)
  135. {
  136. unsigned long sign_key_length;
  137. BIO *bio;
  138. RSA *key;
  139. sign_key_length = (unsigned long)&sign_key_end -
  140. (unsigned long)&sign_key;
  141. bio = BIO_new_mem_buf(&sign_key, sign_key_length);
  142. if (!bio)
  143. return NULL;
  144. key = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL);
  145. BIO_free(bio);
  146. return key;
  147. }
  148. enum mrtags {
  149. MRECREATE = 0x0045544145524345,
  150. MREADD = 0x0000000044444145,
  151. MREEXTEND = 0x00444E4554584545,
  152. };
  153. static bool mrenclave_update(EVP_MD_CTX *ctx, const void *data)
  154. {
  155. if (!EVP_DigestUpdate(ctx, data, 64)) {
  156. fprintf(stderr, "digest update failed\n");
  157. return false;
  158. }
  159. return true;
  160. }
  161. static bool mrenclave_commit(EVP_MD_CTX *ctx, uint8_t *mrenclave)
  162. {
  163. unsigned int size;
  164. if (!EVP_DigestFinal_ex(ctx, (unsigned char *)mrenclave, &size)) {
  165. fprintf(stderr, "digest commit failed\n");
  166. return false;
  167. }
  168. if (size != 32) {
  169. fprintf(stderr, "invalid digest size = %u\n", size);
  170. return false;
  171. }
  172. return true;
  173. }
  174. struct mrecreate {
  175. uint64_t tag;
  176. uint32_t ssaframesize;
  177. uint64_t size;
  178. uint8_t reserved[44];
  179. } __attribute__((__packed__));
  180. static bool mrenclave_ecreate(EVP_MD_CTX *ctx, uint64_t blob_size)
  181. {
  182. struct mrecreate mrecreate;
  183. uint64_t encl_size;
  184. for (encl_size = 0x1000; encl_size < blob_size; )
  185. encl_size <<= 1;
  186. memset(&mrecreate, 0, sizeof(mrecreate));
  187. mrecreate.tag = MRECREATE;
  188. mrecreate.ssaframesize = 1;
  189. mrecreate.size = encl_size;
  190. if (!EVP_DigestInit_ex(ctx, EVP_sha256(), NULL))
  191. return false;
  192. return mrenclave_update(ctx, &mrecreate);
  193. }
  194. struct mreadd {
  195. uint64_t tag;
  196. uint64_t offset;
  197. uint64_t flags; /* SECINFO flags */
  198. uint8_t reserved[40];
  199. } __attribute__((__packed__));
  200. static bool mrenclave_eadd(EVP_MD_CTX *ctx, uint64_t offset, uint64_t flags)
  201. {
  202. struct mreadd mreadd;
  203. memset(&mreadd, 0, sizeof(mreadd));
  204. mreadd.tag = MREADD;
  205. mreadd.offset = offset;
  206. mreadd.flags = flags;
  207. return mrenclave_update(ctx, &mreadd);
  208. }
  209. struct mreextend {
  210. uint64_t tag;
  211. uint64_t offset;
  212. uint8_t reserved[48];
  213. } __attribute__((__packed__));
  214. static bool mrenclave_eextend(EVP_MD_CTX *ctx, uint64_t offset,
  215. const uint8_t *data)
  216. {
  217. struct mreextend mreextend;
  218. int i;
  219. for (i = 0; i < 0x1000; i += 0x100) {
  220. memset(&mreextend, 0, sizeof(mreextend));
  221. mreextend.tag = MREEXTEND;
  222. mreextend.offset = offset + i;
  223. if (!mrenclave_update(ctx, &mreextend))
  224. return false;
  225. if (!mrenclave_update(ctx, &data[i + 0x00]))
  226. return false;
  227. if (!mrenclave_update(ctx, &data[i + 0x40]))
  228. return false;
  229. if (!mrenclave_update(ctx, &data[i + 0x80]))
  230. return false;
  231. if (!mrenclave_update(ctx, &data[i + 0xC0]))
  232. return false;
  233. }
  234. return true;
  235. }
  236. static bool mrenclave_segment(EVP_MD_CTX *ctx, struct encl *encl,
  237. struct encl_segment *seg)
  238. {
  239. uint64_t end = seg->size;
  240. uint64_t offset;
  241. for (offset = 0; offset < end; offset += PAGE_SIZE) {
  242. if (!mrenclave_eadd(ctx, seg->offset + offset, seg->flags))
  243. return false;
  244. if (seg->measure) {
  245. if (!mrenclave_eextend(ctx, seg->offset + offset, seg->src + offset))
  246. return false;
  247. }
  248. }
  249. return true;
  250. }
  251. bool encl_measure(struct encl *encl)
  252. {
  253. uint64_t header1[2] = {0x000000E100000006, 0x0000000000010000};
  254. uint64_t header2[2] = {0x0000006000000101, 0x0000000100000060};
  255. struct sgx_sigstruct *sigstruct = &encl->sigstruct;
  256. struct sgx_sigstruct_payload payload;
  257. uint8_t digest[SHA256_DIGEST_LENGTH];
  258. unsigned int siglen;
  259. RSA *key = NULL;
  260. EVP_MD_CTX *ctx;
  261. int i;
  262. memset(sigstruct, 0, sizeof(*sigstruct));
  263. sigstruct->header.header1[0] = header1[0];
  264. sigstruct->header.header1[1] = header1[1];
  265. sigstruct->header.header2[0] = header2[0];
  266. sigstruct->header.header2[1] = header2[1];
  267. sigstruct->exponent = 3;
  268. sigstruct->body.attributes = SGX_ATTR_MODE64BIT;
  269. sigstruct->body.xfrm = 3;
  270. /* sanity check */
  271. if (check_crypto_errors())
  272. goto err;
  273. key = gen_sign_key();
  274. if (!key) {
  275. ERR_print_errors_fp(stdout);
  276. goto err;
  277. }
  278. BN_bn2bin(get_modulus(key), sigstruct->modulus);
  279. ctx = EVP_MD_CTX_create();
  280. if (!ctx)
  281. goto err;
  282. if (!mrenclave_ecreate(ctx, encl->src_size))
  283. goto err;
  284. for (i = 0; i < encl->nr_segments; i++) {
  285. struct encl_segment *seg = &encl->segment_tbl[i];
  286. if (!mrenclave_segment(ctx, encl, seg))
  287. goto err;
  288. }
  289. if (!mrenclave_commit(ctx, sigstruct->body.mrenclave))
  290. goto err;
  291. memcpy(&payload.header, &sigstruct->header, sizeof(sigstruct->header));
  292. memcpy(&payload.body, &sigstruct->body, sizeof(sigstruct->body));
  293. SHA256((unsigned char *)&payload, sizeof(payload), digest);
  294. if (!RSA_sign(NID_sha256, digest, SHA256_DIGEST_LENGTH,
  295. sigstruct->signature, &siglen, key))
  296. goto err;
  297. if (!calc_q1q2(sigstruct->signature, sigstruct->modulus, sigstruct->q1,
  298. sigstruct->q2))
  299. goto err;
  300. /* BE -> LE */
  301. reverse_bytes(sigstruct->signature, SGX_MODULUS_SIZE);
  302. reverse_bytes(sigstruct->modulus, SGX_MODULUS_SIZE);
  303. EVP_MD_CTX_destroy(ctx);
  304. RSA_free(key);
  305. return true;
  306. err:
  307. EVP_MD_CTX_destroy(ctx);
  308. RSA_free(key);
  309. return false;
  310. }