ecdsa-libcrypto.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ECDSA image signing implementation using libcrypto backend
  4. *
  5. * The signature is a binary representation of the (R, S) points, padded to the
  6. * key size. The signature will be (2 * key_size_bits) / 8 bytes.
  7. *
  8. * Deviations from behavior of RSA equivalent:
  9. * - Verification uses private key. This is not technically required, but a
  10. * limitation on how clumsy the openssl API is to use.
  11. * - Handling of keys and key paths:
  12. * - The '-K' key directory option must contain path to the key file,
  13. * instead of the key directory.
  14. * - No assumptions are made about the file extension of the key
  15. * - The 'key-name-hint' property is only used for naming devicetree nodes,
  16. * but is not used for looking up keys on the filesystem.
  17. *
  18. * Copyright (c) 2020,2021, Alexandru Gagniuc <mr.nuke.me@gmail.com>
  19. */
  20. #define OPENSSL_API_COMPAT 0x10101000L
  21. #include <u-boot/ecdsa.h>
  22. #include <u-boot/fdt-libcrypto.h>
  23. #include <openssl/ssl.h>
  24. #include <openssl/ec.h>
  25. #include <openssl/bn.h>
  26. /* Image signing context for openssl-libcrypto */
  27. struct signer {
  28. EVP_PKEY *evp_key; /* Pointer to EVP_PKEY object */
  29. EC_KEY *ecdsa_key; /* Pointer to EC_KEY object */
  30. void *hash; /* Pointer to hash used for verification */
  31. void *signature; /* Pointer to output signature. Do not free()!*/
  32. };
  33. static int alloc_ctx(struct signer *ctx, const struct image_sign_info *info)
  34. {
  35. memset(ctx, 0, sizeof(*ctx));
  36. if (!OPENSSL_init_ssl(0, NULL)) {
  37. fprintf(stderr, "Failure to init SSL library\n");
  38. return -1;
  39. }
  40. ctx->hash = malloc(info->checksum->checksum_len);
  41. ctx->signature = malloc(info->crypto->key_len * 2);
  42. if (!ctx->hash || !ctx->signature)
  43. return -ENOMEM;
  44. return 0;
  45. }
  46. static void free_ctx(struct signer *ctx)
  47. {
  48. if (ctx->ecdsa_key)
  49. EC_KEY_free(ctx->ecdsa_key);
  50. if (ctx->evp_key)
  51. EVP_PKEY_free(ctx->evp_key);
  52. if (ctx->hash)
  53. free(ctx->hash);
  54. }
  55. /*
  56. * Convert an ECDSA signature to raw format
  57. *
  58. * openssl DER-encodes 'binary' signatures. We want the signature in a raw
  59. * (R, S) point pair. So we have to dance a bit.
  60. */
  61. static void ecdsa_sig_encode_raw(void *buf, const ECDSA_SIG *sig, size_t order)
  62. {
  63. int point_bytes = order;
  64. const BIGNUM *r, *s;
  65. uintptr_t s_buf;
  66. ECDSA_SIG_get0(sig, &r, &s);
  67. s_buf = (uintptr_t)buf + point_bytes;
  68. BN_bn2binpad(r, buf, point_bytes);
  69. BN_bn2binpad(s, (void *)s_buf, point_bytes);
  70. }
  71. /* Get a signature from a raw encoding */
  72. static ECDSA_SIG *ecdsa_sig_from_raw(void *buf, size_t order)
  73. {
  74. int point_bytes = order;
  75. uintptr_t s_buf;
  76. ECDSA_SIG *sig;
  77. BIGNUM *r, *s;
  78. sig = ECDSA_SIG_new();
  79. if (!sig)
  80. return NULL;
  81. s_buf = (uintptr_t)buf + point_bytes;
  82. r = BN_bin2bn(buf, point_bytes, NULL);
  83. s = BN_bin2bn((void *)s_buf, point_bytes, NULL);
  84. ECDSA_SIG_set0(sig, r, s);
  85. return sig;
  86. }
  87. /* ECDSA key size in bytes */
  88. static size_t ecdsa_key_size_bytes(const EC_KEY *key)
  89. {
  90. const EC_GROUP *group;
  91. group = EC_KEY_get0_group(key);
  92. return EC_GROUP_order_bits(group) / 8;
  93. }
  94. static int read_key(struct signer *ctx, const char *key_name)
  95. {
  96. FILE *f = fopen(key_name, "r");
  97. if (!f) {
  98. fprintf(stderr, "Can not get key file '%s'\n", key_name);
  99. return -ENOENT;
  100. }
  101. ctx->evp_key = PEM_read_PrivateKey(f, NULL, NULL, NULL);
  102. fclose(f);
  103. if (!ctx->evp_key) {
  104. fprintf(stderr, "Can not read key from '%s'\n", key_name);
  105. return -EIO;
  106. }
  107. if (EVP_PKEY_id(ctx->evp_key) != EVP_PKEY_EC) {
  108. fprintf(stderr, "'%s' is not an ECDSA key\n", key_name);
  109. return -EINVAL;
  110. }
  111. ctx->ecdsa_key = EVP_PKEY_get1_EC_KEY(ctx->evp_key);
  112. if (!ctx->ecdsa_key)
  113. fprintf(stderr, "Can not extract ECDSA key\n");
  114. return (ctx->ecdsa_key) ? 0 : -EINVAL;
  115. }
  116. /* Prepare a 'signer' context that's ready to sign and verify. */
  117. static int prepare_ctx(struct signer *ctx, const struct image_sign_info *info)
  118. {
  119. int key_len_bytes, ret;
  120. char kname[1024];
  121. memset(ctx, 0, sizeof(*ctx));
  122. if (info->keyfile) {
  123. snprintf(kname, sizeof(kname), "%s", info->keyfile);
  124. } else if (info->keydir && info->keyname) {
  125. snprintf(kname, sizeof(kname), "%s/%s.pem", info->keydir,
  126. info->keyname);
  127. } else {
  128. fprintf(stderr, "keyfile, keyname, or key-name-hint missing\n");
  129. return -EINVAL;
  130. }
  131. ret = alloc_ctx(ctx, info);
  132. if (ret)
  133. return ret;
  134. ret = read_key(ctx, kname);
  135. if (ret)
  136. return ret;
  137. key_len_bytes = ecdsa_key_size_bytes(ctx->ecdsa_key);
  138. if (key_len_bytes != info->crypto->key_len) {
  139. fprintf(stderr, "Expected a %u-bit key, got %u-bit key\n",
  140. info->crypto->key_len * 8, key_len_bytes * 8);
  141. return -EINVAL;
  142. }
  143. return 0;
  144. }
  145. static int do_sign(struct signer *ctx, struct image_sign_info *info,
  146. const struct image_region region[], int region_count)
  147. {
  148. const struct checksum_algo *algo = info->checksum;
  149. ECDSA_SIG *sig;
  150. algo->calculate(algo->name, region, region_count, ctx->hash);
  151. sig = ECDSA_do_sign(ctx->hash, algo->checksum_len, ctx->ecdsa_key);
  152. ecdsa_sig_encode_raw(ctx->signature, sig, info->crypto->key_len);
  153. return 0;
  154. }
  155. static int ecdsa_check_signature(struct signer *ctx, struct image_sign_info *info)
  156. {
  157. ECDSA_SIG *sig;
  158. int okay;
  159. sig = ecdsa_sig_from_raw(ctx->signature, info->crypto->key_len);
  160. if (!sig)
  161. return -ENOMEM;
  162. okay = ECDSA_do_verify(ctx->hash, info->checksum->checksum_len,
  163. sig, ctx->ecdsa_key);
  164. if (!okay)
  165. fprintf(stderr, "WARNING: Signature is fake news!\n");
  166. ECDSA_SIG_free(sig);
  167. return !okay;
  168. }
  169. static int do_verify(struct signer *ctx, struct image_sign_info *info,
  170. const struct image_region region[], int region_count,
  171. uint8_t *raw_sig, uint sig_len)
  172. {
  173. const struct checksum_algo *algo = info->checksum;
  174. if (sig_len != info->crypto->key_len * 2) {
  175. fprintf(stderr, "Signature has wrong length\n");
  176. return -EINVAL;
  177. }
  178. memcpy(ctx->signature, raw_sig, sig_len);
  179. algo->calculate(algo->name, region, region_count, ctx->hash);
  180. return ecdsa_check_signature(ctx, info);
  181. }
  182. int ecdsa_sign(struct image_sign_info *info, const struct image_region region[],
  183. int region_count, uint8_t **sigp, uint *sig_len)
  184. {
  185. struct signer ctx;
  186. int ret;
  187. ret = prepare_ctx(&ctx, info);
  188. if (ret >= 0) {
  189. do_sign(&ctx, info, region, region_count);
  190. *sigp = ctx.signature;
  191. *sig_len = info->crypto->key_len * 2;
  192. ret = ecdsa_check_signature(&ctx, info);
  193. }
  194. free_ctx(&ctx);
  195. return ret;
  196. }
  197. int ecdsa_verify(struct image_sign_info *info,
  198. const struct image_region region[], int region_count,
  199. uint8_t *sig, uint sig_len)
  200. {
  201. struct signer ctx;
  202. int ret;
  203. ret = prepare_ctx(&ctx, info);
  204. if (ret >= 0)
  205. ret = do_verify(&ctx, info, region, region_count, sig, sig_len);
  206. free_ctx(&ctx);
  207. return ret;
  208. }
  209. static int do_add(struct signer *ctx, void *fdt, const char *key_node_name)
  210. {
  211. int signature_node, key_node, ret, key_bits;
  212. const char *curve_name;
  213. const EC_GROUP *group;
  214. const EC_POINT *point;
  215. BIGNUM *x, *y;
  216. signature_node = fdt_subnode_offset(fdt, 0, FIT_SIG_NODENAME);
  217. if (signature_node < 0) {
  218. fprintf(stderr, "Could not find 'signature node: %s\n",
  219. fdt_strerror(signature_node));
  220. return signature_node;
  221. }
  222. key_node = fdt_add_subnode(fdt, signature_node, key_node_name);
  223. if (key_node < 0) {
  224. fprintf(stderr, "Could not create '%s' node: %s\n",
  225. key_node_name, fdt_strerror(key_node));
  226. return key_node;
  227. }
  228. group = EC_KEY_get0_group(ctx->ecdsa_key);
  229. key_bits = EC_GROUP_order_bits(group);
  230. curve_name = OBJ_nid2sn(EC_GROUP_get_curve_name(group));
  231. /* Let 'x' and 'y' memory leak by not BN_free()'ing them. */
  232. x = BN_new();
  233. y = BN_new();
  234. point = EC_KEY_get0_public_key(ctx->ecdsa_key);
  235. EC_POINT_get_affine_coordinates(group, point, x, y, NULL);
  236. ret = fdt_setprop_string(fdt, key_node, "ecdsa,curve", curve_name);
  237. if (ret < 0)
  238. return ret;
  239. ret = fdt_add_bignum(fdt, key_node, "ecdsa,x-point", x, key_bits);
  240. if (ret < 0)
  241. return ret;
  242. ret = fdt_add_bignum(fdt, key_node, "ecdsa,y-point", y, key_bits);
  243. if (ret < 0)
  244. return ret;
  245. return 0;
  246. }
  247. int ecdsa_add_verify_data(struct image_sign_info *info, void *fdt)
  248. {
  249. const char *fdt_key_name;
  250. struct signer ctx;
  251. int ret;
  252. fdt_key_name = info->keyname ? info->keyname : "default-key";
  253. ret = prepare_ctx(&ctx, info);
  254. if (ret >= 0)
  255. do_add(&ctx, fdt, fdt_key_name);
  256. free_ctx(&ctx);
  257. return ret;
  258. }