ectest.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <gmssl/sm2.h>
  13. #include <gmssl/ec.h>
  14. #include <gmssl/error.h>
  15. static int test_ec_named_curve(void)
  16. {
  17. uint8_t buf[256];
  18. uint8_t *p = buf;
  19. const uint8_t *cp = buf;
  20. size_t len = 0;
  21. char *curves[] = {
  22. "sm2p256v1",
  23. "prime192v1",
  24. "prime256v1",
  25. "secp256k1",
  26. "secp384r1",
  27. "secp521r1",
  28. };
  29. int oid;
  30. int i;
  31. for (i = 0; i < sizeof(curves)/sizeof(curves[0]); i++) {
  32. if ((oid = ec_named_curve_from_name(curves[i])) == OID_undef) {
  33. error_print();
  34. return -1;
  35. }
  36. if (ec_named_curve_to_der(oid, &p, &len) != 1) {
  37. error_print();
  38. return -1;
  39. }
  40. }
  41. for (i = 0; i < sizeof(curves)/sizeof(curves[0]); i++) {
  42. if (ec_named_curve_from_der(&oid, &cp, &len) != 1) {
  43. error_print();
  44. return -1;
  45. }
  46. if (oid != ec_named_curve_from_name(curves[i])) {
  47. error_print();
  48. return -1;
  49. }
  50. format_print(stderr, 0, 4, "%s\n", ec_named_curve_name(oid));
  51. }
  52. (void)asn1_length_is_zero(len);
  53. printf("%s() ok\n", __FUNCTION__);
  54. return 1;
  55. }
  56. static int test_ec_point_print(void)
  57. {
  58. SM2_KEY sm2_key;
  59. uint8_t buf[256];
  60. uint8_t *p = buf;
  61. size_t len = 0;
  62. if (sm2_key_generate(&sm2_key) != 1) {
  63. error_print();
  64. return -1;
  65. }
  66. if (sm2_point_to_der(&(sm2_key.public_key), &p, &len) != 1) {
  67. error_print();
  68. return -1;
  69. }
  70. ec_point_print(stderr, 0, 4, "ECPoint", buf, len);
  71. printf("%s() ok\n", __FUNCTION__);
  72. return 1;
  73. }
  74. static int test_ec_private_key_print(void)
  75. {
  76. SM2_KEY sm2_key;
  77. uint8_t buf[256];
  78. uint8_t *p = buf;
  79. const uint8_t *cp = buf;
  80. size_t len = 0;
  81. const uint8_t *d;
  82. size_t dlen;
  83. if (sm2_key_generate(&sm2_key) != 1) {
  84. error_print();
  85. return -1;
  86. }
  87. if (sm2_private_key_to_der(&sm2_key, &p, &len) != 1
  88. || asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
  89. || asn1_length_is_zero(len) != 1) {
  90. error_print();
  91. return -1;
  92. }
  93. ec_private_key_print(stderr, 0, 4, "ECPrivateKey", d, dlen);
  94. printf("%s() ok\n", __FUNCTION__);
  95. return 1;
  96. }
  97. int main(void)
  98. {
  99. if (test_ec_named_curve() != 1) goto err;
  100. if (test_ec_point_print() != 1) goto err;
  101. if (test_ec_private_key_print() != 1) goto err;
  102. printf("%s all tests passed\n", __FILE__);
  103. return 0;
  104. err:
  105. error_print();
  106. return -1;
  107. }