zkp_ecdsa.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**
  2. * Copyright (c) SatoshiLabs
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  18. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <assert.h>
  23. #include <stdbool.h>
  24. #include <string.h>
  25. #include "memzero.h"
  26. #include "secp256k1.h"
  27. #include "zkp_context.h"
  28. #include "vendor/secp256k1-zkp/include/secp256k1.h"
  29. #include "vendor/secp256k1-zkp/include/secp256k1_extrakeys.h"
  30. #include "vendor/secp256k1-zkp/include/secp256k1_preallocated.h"
  31. #include "vendor/secp256k1-zkp/include/secp256k1_recovery.h"
  32. #include "zkp_ecdsa.h"
  33. static bool is_zero_digest(const uint8_t *digest) {
  34. const uint8_t zeroes[32] = {0};
  35. return memcmp(digest, zeroes, 32) == 0;
  36. }
  37. // ECDSA compressed public key derivation
  38. // curve has to be &secp256k1
  39. // private_key_bytes has 32 bytes
  40. // public_key_bytes has 33 bytes
  41. // returns 0 on success
  42. int zkp_ecdsa_get_public_key33(const ecdsa_curve *curve,
  43. const uint8_t *private_key_bytes,
  44. uint8_t *public_key_bytes) {
  45. assert(curve == &secp256k1);
  46. if (curve != &secp256k1) {
  47. return 1;
  48. }
  49. int result = 0;
  50. secp256k1_context *context_writable = NULL;
  51. if (result == 0) {
  52. context_writable = zkp_context_acquire_writable();
  53. if (context_writable == NULL) {
  54. result = 1;
  55. }
  56. }
  57. if (result == 0) {
  58. if (secp256k1_context_writable_randomize(context_writable) != 0) {
  59. result = 1;
  60. }
  61. }
  62. secp256k1_pubkey public_key = {0};
  63. if (result == 0) {
  64. if (secp256k1_ec_pubkey_create(context_writable, &public_key,
  65. private_key_bytes) != 1) {
  66. result = 1;
  67. }
  68. }
  69. if (context_writable) {
  70. zkp_context_release_writable();
  71. context_writable = NULL;
  72. }
  73. if (result == 0) {
  74. size_t written = 33;
  75. const secp256k1_context *context_read_only = zkp_context_get_read_only();
  76. int returned = secp256k1_ec_pubkey_serialize(
  77. context_read_only, public_key_bytes, &written, &public_key,
  78. SECP256K1_EC_COMPRESSED);
  79. if (returned != 1 || written != 33) {
  80. result = 1;
  81. }
  82. }
  83. memzero(&public_key, sizeof(public_key));
  84. return result;
  85. }
  86. // ECDSA uncompressed public key derivation
  87. // curve has to be &secp256k1
  88. // private_key_bytes has 32 bytes
  89. // public_key_bytes has 65 bytes
  90. // returns 0 on success
  91. int zkp_ecdsa_get_public_key65(const ecdsa_curve *curve,
  92. const uint8_t *private_key_bytes,
  93. uint8_t *public_key_bytes) {
  94. assert(curve == &secp256k1);
  95. if (curve != &secp256k1) {
  96. return 1;
  97. }
  98. int result = 0;
  99. secp256k1_context *context_writable = NULL;
  100. if (result == 0) {
  101. context_writable = zkp_context_acquire_writable();
  102. if (context_writable == NULL) {
  103. result = 1;
  104. }
  105. }
  106. if (result == 0) {
  107. if (secp256k1_context_writable_randomize(context_writable) != 0) {
  108. result = 1;
  109. }
  110. }
  111. secp256k1_pubkey public_key = {0};
  112. if (result == 0) {
  113. if (secp256k1_ec_pubkey_create(context_writable, &public_key,
  114. private_key_bytes) != 1) {
  115. result = 1;
  116. }
  117. }
  118. if (context_writable) {
  119. zkp_context_release_writable();
  120. context_writable = NULL;
  121. }
  122. if (result == 0) {
  123. size_t written = 65;
  124. const secp256k1_context *context_read_only = zkp_context_get_read_only();
  125. int returned = secp256k1_ec_pubkey_serialize(
  126. context_read_only, public_key_bytes, &written, &public_key,
  127. SECP256K1_EC_UNCOMPRESSED);
  128. if (returned != 1 || written != 65) {
  129. result = 1;
  130. }
  131. }
  132. memzero(&public_key, sizeof(public_key));
  133. return result;
  134. }
  135. // ECDSA signing
  136. // curve has to be &secp256k1
  137. // private_key_bytes has 32 bytes
  138. // digest has 32 bytes
  139. // signature_bytes has 64 bytes
  140. // pby is one byte
  141. // is_canonical has to be NULL
  142. // returns 0 on success
  143. int zkp_ecdsa_sign_digest(
  144. const ecdsa_curve *curve, const uint8_t *private_key_bytes,
  145. const uint8_t *digest, uint8_t *signature_bytes, uint8_t *pby,
  146. int (*is_canonical)(uint8_t by, uint8_t signature_bytes[64])) {
  147. assert(curve == &secp256k1);
  148. assert(is_canonical == NULL);
  149. if (curve != &secp256k1 || is_canonical != NULL) {
  150. return 1;
  151. }
  152. int result = 0;
  153. if (result == 0) {
  154. if (is_zero_digest(digest)) {
  155. // The probability of the digest being all-zero by chance is
  156. // infinitesimal, so this is most likely an indication of a bug.
  157. // Furthermore, the signature has no value, because in this case it can be
  158. // easily forged for any public key, see zkp_ecdsa_verify_digest().
  159. result = 1;
  160. }
  161. }
  162. secp256k1_context *context_writable = NULL;
  163. if (result == 0) {
  164. context_writable = zkp_context_acquire_writable();
  165. if (context_writable == NULL) {
  166. result = 1;
  167. }
  168. }
  169. if (result == 0) {
  170. if (secp256k1_context_writable_randomize(context_writable) != 0) {
  171. result = 1;
  172. }
  173. }
  174. secp256k1_ecdsa_recoverable_signature recoverable_signature = {0};
  175. if (result == 0) {
  176. if (secp256k1_ecdsa_sign_recoverable(context_writable,
  177. &recoverable_signature, digest,
  178. private_key_bytes, NULL, NULL) != 1) {
  179. result = 1;
  180. }
  181. }
  182. if (context_writable) {
  183. zkp_context_release_writable();
  184. context_writable = NULL;
  185. }
  186. if (result == 0) {
  187. int recid = 0;
  188. const secp256k1_context *context_read_only = zkp_context_get_read_only();
  189. if (secp256k1_ecdsa_recoverable_signature_serialize_compact(
  190. context_read_only, signature_bytes, &recid,
  191. &recoverable_signature) != 1) {
  192. result = 1;
  193. }
  194. if (pby != NULL) {
  195. *pby = (uint8_t)recid;
  196. }
  197. }
  198. memzero(&recoverable_signature, sizeof(recoverable_signature));
  199. return result;
  200. }
  201. // ECDSA public key recovery
  202. // public_key_bytes has 65 bytes
  203. // signature_bytes has 64 bytes
  204. // digest has 32 bytes
  205. // recid is 0, 1, 2 or 3
  206. // returns 0 on success
  207. int zkp_ecdsa_recover_pub_from_sig(const ecdsa_curve *curve,
  208. uint8_t *public_key_bytes,
  209. const uint8_t *signature_bytes,
  210. const uint8_t *digest, int recid) {
  211. assert(curve == &secp256k1);
  212. if (curve != &secp256k1) {
  213. return 1;
  214. }
  215. int result = 0;
  216. const secp256k1_context *context_read_only = zkp_context_get_read_only();
  217. secp256k1_ecdsa_recoverable_signature recoverable_signature = {0};
  218. if (result == 0) {
  219. if (secp256k1_ecdsa_recoverable_signature_parse_compact(
  220. context_read_only, &recoverable_signature, signature_bytes,
  221. recid) != 1) {
  222. result = 1;
  223. }
  224. }
  225. secp256k1_pubkey public_key = {0};
  226. if (result == 0) {
  227. if (secp256k1_ecdsa_recover(context_read_only, &public_key,
  228. &recoverable_signature, digest) != 1) {
  229. result = 1;
  230. }
  231. }
  232. memzero(&recoverable_signature, sizeof(recoverable_signature));
  233. if (result == 0) {
  234. size_t written = 65;
  235. int returned = secp256k1_ec_pubkey_serialize(
  236. context_read_only, public_key_bytes, &written, &public_key,
  237. SECP256K1_EC_UNCOMPRESSED);
  238. if (returned != 1 || written != 65) {
  239. result = 1;
  240. }
  241. }
  242. memzero(&public_key, sizeof(public_key));
  243. return result;
  244. }
  245. // ECDSA verification
  246. // curve has to be &secp256k1
  247. // public_key_bytes has 33 or 65 bytes
  248. // signature_bytes has 64 bytes
  249. // digest has 32 bytes
  250. // returns 0 if verification succeeded
  251. int zkp_ecdsa_verify_digest(const ecdsa_curve *curve,
  252. const uint8_t *public_key_bytes,
  253. const uint8_t *signature_bytes,
  254. const uint8_t *digest) {
  255. assert(curve == &secp256k1);
  256. if (curve != &secp256k1) {
  257. return 1;
  258. }
  259. int result = 0;
  260. int public_key_length = 0;
  261. if (result == 0) {
  262. if (public_key_bytes[0] == 0x04) {
  263. public_key_length = 65;
  264. } else if (public_key_bytes[0] == 0x02 || public_key_bytes[0] == 0x03) {
  265. public_key_length = 33;
  266. } else {
  267. result = 1;
  268. }
  269. }
  270. if (result == 0) {
  271. if (is_zero_digest(digest)) {
  272. // The digest was all-zero. The probability of this happening by chance is
  273. // infinitesimal, but it could be induced by a fault injection. In this
  274. // case the signature (r,s) can be forged by taking r := (t * Q).x mod n
  275. // and s := r * t^-1 mod n for any t in [1, n-1]. We fail verification,
  276. // because there is no guarantee that the signature was created by the
  277. // owner of the private key.
  278. result = 3;
  279. }
  280. }
  281. const secp256k1_context *context_read_only = zkp_context_get_read_only();
  282. secp256k1_pubkey public_key = {0};
  283. if (result == 0) {
  284. if (secp256k1_ec_pubkey_parse(context_read_only, &public_key,
  285. public_key_bytes, public_key_length) != 1) {
  286. result = 1;
  287. }
  288. }
  289. secp256k1_ecdsa_signature signature = {0};
  290. if (result == 0) {
  291. if (secp256k1_ecdsa_signature_parse_compact(context_read_only, &signature,
  292. signature_bytes) != 1) {
  293. result = 2;
  294. }
  295. }
  296. if (result == 0) {
  297. secp256k1_ecdsa_signature_normalize(context_read_only, &signature,
  298. &signature);
  299. if (secp256k1_ecdsa_verify(context_read_only, &signature, digest,
  300. &public_key) != 1) {
  301. result = 5;
  302. }
  303. }
  304. memzero(&public_key, sizeof(public_key));
  305. memzero(&signature, sizeof(signature));
  306. return result;
  307. }
  308. // ECDSA verification
  309. // curve has to be &secp256k1
  310. // public_key_bytes has 33 or 65 bytes
  311. // signature_bytes has 64 bytes
  312. // returns 0 if verification succeeded
  313. int zkp_ecdsa_verify(const ecdsa_curve *curve, HasherType hasher_type,
  314. const uint8_t *public_key_bytes,
  315. const uint8_t *signature_bytes, const uint8_t *message,
  316. uint32_t message_length) {
  317. assert(curve == &secp256k1);
  318. if (curve != &secp256k1) {
  319. return 1;
  320. }
  321. uint8_t hash[32] = {0};
  322. hasher_Raw(hasher_type, message, message_length, hash);
  323. int result =
  324. zkp_ecdsa_verify_digest(curve, public_key_bytes, signature_bytes, hash);
  325. memzero(hash, sizeof(hash));
  326. return result;
  327. }