ecdsa-libcrypto.c 8.0 KB

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