test_openssl.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * Copyright (c) 2013-2014 Tomas Dzetkulic
  3. * Copyright (c) 2013-2014 Pavol Rusnak
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  19. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. /* OpenSSL's SHA256_CTX/SHA512_CTX conflicts with our own */
  24. #define SHA256_CTX _openssl_SHA256_CTX
  25. #define SHA512_CTX _openssl_SHA512_CTX
  26. #include <openssl/bn.h>
  27. #include <openssl/ecdsa.h>
  28. #include <openssl/obj_mac.h>
  29. #include <openssl/opensslv.h>
  30. #include <openssl/sha.h>
  31. #undef SHA256_CTX
  32. #undef SHA512_CTX
  33. #include <stdint.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include "ecdsa.h"
  37. #include "hasher.h"
  38. #include "rand.h"
  39. #include "nist256p1.h"
  40. #include "secp256k1.h"
  41. #include "memzero.h"
  42. void openssl_check(unsigned int iterations, int nid, const ecdsa_curve *curve) {
  43. uint8_t sig[64], pub_key33[33], pub_key65[65], priv_key[32], msg[256],
  44. hash[32];
  45. struct SHA256state_st sha256;
  46. EC_GROUP *ecgroup;
  47. ecgroup = EC_GROUP_new_by_curve_name(nid);
  48. for (unsigned int iter = 0; iter < iterations; iter++) {
  49. // random message len between 1 and 256
  50. int msg_len = (random32() & 0xFF) + 1;
  51. // create random message
  52. random_buffer(msg, msg_len);
  53. // new ECDSA key
  54. EC_KEY *eckey = EC_KEY_new();
  55. EC_KEY_set_group(eckey, ecgroup);
  56. // generate the key
  57. EC_KEY_generate_key(eckey);
  58. // copy key to buffer
  59. const BIGNUM *K = EC_KEY_get0_private_key(eckey);
  60. int bn_off = sizeof(priv_key) - BN_num_bytes(K);
  61. memzero(priv_key, bn_off);
  62. BN_bn2bin(K, priv_key + bn_off);
  63. // use our ECDSA signer to sign the message with the key
  64. if (ecdsa_sign(curve, HASHER_SHA2, priv_key, msg, msg_len, sig, NULL,
  65. NULL) != 0) {
  66. printf("trezor-crypto signing failed\n");
  67. return;
  68. }
  69. // generate public key from private key
  70. if (ecdsa_get_public_key33(curve, priv_key, pub_key33) != 0) {
  71. printf("ecdsa_get_public_key33 failed\n");
  72. return;
  73. }
  74. if (ecdsa_get_public_key65(curve, priv_key, pub_key65) != 0) {
  75. printf("ecdsa_get_public_key65 failed\n");
  76. return;
  77. }
  78. // use our ECDSA verifier to verify the message signature
  79. if (ecdsa_verify(curve, HASHER_SHA2, pub_key65, sig, msg, msg_len) != 0) {
  80. printf("trezor-crypto verification failed (pub_key_len = 65)\n");
  81. return;
  82. }
  83. if (ecdsa_verify(curve, HASHER_SHA2, pub_key33, sig, msg, msg_len) != 0) {
  84. printf("trezor-crypto verification failed (pub_key_len = 33)\n");
  85. return;
  86. }
  87. // copy signature to the OpenSSL struct
  88. ECDSA_SIG *signature = ECDSA_SIG_new();
  89. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  90. BN_bin2bn(sig, 32, signature->r);
  91. BN_bin2bn(sig + 32, 32, signature->s);
  92. #else
  93. BIGNUM *R = BN_bin2bn(sig, 32, NULL);
  94. BIGNUM *S = BN_bin2bn(sig + 32, 32, NULL);
  95. ECDSA_SIG_set0(signature, R, S);
  96. #endif
  97. // compute the digest of the message
  98. // note: these are OpenSSL functions, not our own
  99. SHA256_Init(&sha256);
  100. SHA256_Update(&sha256, msg, msg_len);
  101. SHA256_Final(hash, &sha256);
  102. // verify all went well, i.e. we can decrypt our signature with OpenSSL
  103. int v = ECDSA_do_verify(hash, 32, signature, eckey);
  104. if (v != 1) {
  105. printf("OpenSSL verification failed (%d)\n", v);
  106. return;
  107. }
  108. ECDSA_SIG_free(signature);
  109. EC_KEY_free(eckey);
  110. if (((iter + 1) % 100) == 0) printf("Passed ... %d\n", iter + 1);
  111. }
  112. EC_GROUP_free(ecgroup);
  113. printf("All OK\n");
  114. }
  115. int main(int argc, char *argv[]) {
  116. if (argc != 2) {
  117. printf("Usage: test_openssl iterations\n");
  118. return 1;
  119. }
  120. unsigned int iterations;
  121. sscanf(argv[1], "%u", &iterations);
  122. printf("Testing secp256k1:\n");
  123. openssl_check(iterations, NID_secp256k1, &secp256k1);
  124. printf("Testing nist256p1:\n");
  125. openssl_check(iterations, NID_X9_62_prime256v1, &nist256p1);
  126. return 0;
  127. }