2
0

ec_print.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * ECDSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/crypto.h>
  15. #include <openssl/err.h>
  16. #include "ec_local.h"
  17. BIGNUM *EC_POINT_point2bn(const EC_GROUP *group,
  18. const EC_POINT *point,
  19. point_conversion_form_t form,
  20. BIGNUM *ret, BN_CTX *ctx)
  21. {
  22. size_t buf_len = 0;
  23. unsigned char *buf;
  24. buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
  25. if (buf_len == 0)
  26. return NULL;
  27. ret = BN_bin2bn(buf, buf_len, ret);
  28. OPENSSL_free(buf);
  29. return ret;
  30. }
  31. EC_POINT *EC_POINT_bn2point(const EC_GROUP *group,
  32. const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx)
  33. {
  34. size_t buf_len = 0;
  35. unsigned char *buf;
  36. EC_POINT *ret;
  37. if ((buf_len = BN_num_bytes(bn)) == 0)
  38. buf_len = 1;
  39. if ((buf = OPENSSL_malloc(buf_len)) == NULL) {
  40. ECerr(EC_F_EC_POINT_BN2POINT, ERR_R_MALLOC_FAILURE);
  41. return NULL;
  42. }
  43. if (!BN_bn2binpad(bn, buf, buf_len)) {
  44. OPENSSL_free(buf);
  45. return NULL;
  46. }
  47. if (point == NULL) {
  48. if ((ret = EC_POINT_new(group)) == NULL) {
  49. OPENSSL_free(buf);
  50. return NULL;
  51. }
  52. } else
  53. ret = point;
  54. if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) {
  55. if (ret != point)
  56. EC_POINT_clear_free(ret);
  57. OPENSSL_free(buf);
  58. return NULL;
  59. }
  60. OPENSSL_free(buf);
  61. return ret;
  62. }
  63. static const char *HEX_DIGITS = "0123456789ABCDEF";
  64. /* the return value must be freed (using OPENSSL_free()) */
  65. char *EC_POINT_point2hex(const EC_GROUP *group,
  66. const EC_POINT *point,
  67. point_conversion_form_t form, BN_CTX *ctx)
  68. {
  69. char *ret, *p;
  70. size_t buf_len = 0, i;
  71. unsigned char *buf = NULL, *pbuf;
  72. buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
  73. if (buf_len == 0)
  74. return NULL;
  75. ret = OPENSSL_malloc(buf_len * 2 + 2);
  76. if (ret == NULL) {
  77. OPENSSL_free(buf);
  78. return NULL;
  79. }
  80. p = ret;
  81. pbuf = buf;
  82. for (i = buf_len; i > 0; i--) {
  83. int v = (int)*(pbuf++);
  84. *(p++) = HEX_DIGITS[v >> 4];
  85. *(p++) = HEX_DIGITS[v & 0x0F];
  86. }
  87. *p = '\0';
  88. OPENSSL_free(buf);
  89. return ret;
  90. }
  91. EC_POINT *EC_POINT_hex2point(const EC_GROUP *group,
  92. const char *buf, EC_POINT *point, BN_CTX *ctx)
  93. {
  94. EC_POINT *ret = NULL;
  95. BIGNUM *tmp_bn = NULL;
  96. if (!BN_hex2bn(&tmp_bn, buf))
  97. return NULL;
  98. ret = EC_POINT_bn2point(group, tmp_bn, point, ctx);
  99. BN_clear_free(tmp_bn);
  100. return ret;
  101. }