ec_print.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include <openssl/crypto.h>
  10. #include <openssl/err.h>
  11. #include "ec_local.h"
  12. BIGNUM *EC_POINT_point2bn(const EC_GROUP *group,
  13. const EC_POINT *point,
  14. point_conversion_form_t form,
  15. BIGNUM *ret, BN_CTX *ctx)
  16. {
  17. size_t buf_len = 0;
  18. unsigned char *buf;
  19. buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
  20. if (buf_len == 0)
  21. return NULL;
  22. ret = BN_bin2bn(buf, buf_len, ret);
  23. OPENSSL_free(buf);
  24. return ret;
  25. }
  26. EC_POINT *EC_POINT_bn2point(const EC_GROUP *group,
  27. const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx)
  28. {
  29. size_t buf_len = 0;
  30. unsigned char *buf;
  31. EC_POINT *ret;
  32. if ((buf_len = BN_num_bytes(bn)) == 0)
  33. buf_len = 1;
  34. if ((buf = OPENSSL_malloc(buf_len)) == NULL) {
  35. ECerr(EC_F_EC_POINT_BN2POINT, ERR_R_MALLOC_FAILURE);
  36. return NULL;
  37. }
  38. if (!BN_bn2binpad(bn, buf, buf_len)) {
  39. OPENSSL_free(buf);
  40. return NULL;
  41. }
  42. if (point == NULL) {
  43. if ((ret = EC_POINT_new(group)) == NULL) {
  44. OPENSSL_free(buf);
  45. return NULL;
  46. }
  47. } else
  48. ret = point;
  49. if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) {
  50. if (ret != point)
  51. EC_POINT_clear_free(ret);
  52. OPENSSL_free(buf);
  53. return NULL;
  54. }
  55. OPENSSL_free(buf);
  56. return ret;
  57. }
  58. static const char *HEX_DIGITS = "0123456789ABCDEF";
  59. /* the return value must be freed (using OPENSSL_free()) */
  60. char *EC_POINT_point2hex(const EC_GROUP *group,
  61. const EC_POINT *point,
  62. point_conversion_form_t form, BN_CTX *ctx)
  63. {
  64. char *ret, *p;
  65. size_t buf_len = 0, i;
  66. unsigned char *buf = NULL, *pbuf;
  67. buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
  68. if (buf_len == 0)
  69. return NULL;
  70. ret = OPENSSL_malloc(buf_len * 2 + 2);
  71. if (ret == NULL) {
  72. OPENSSL_free(buf);
  73. return NULL;
  74. }
  75. p = ret;
  76. pbuf = buf;
  77. for (i = buf_len; i > 0; i--) {
  78. int v = (int)*(pbuf++);
  79. *(p++) = HEX_DIGITS[v >> 4];
  80. *(p++) = HEX_DIGITS[v & 0x0F];
  81. }
  82. *p = '\0';
  83. OPENSSL_free(buf);
  84. return ret;
  85. }
  86. EC_POINT *EC_POINT_hex2point(const EC_GROUP *group,
  87. const char *buf, EC_POINT *point, BN_CTX *ctx)
  88. {
  89. EC_POINT *ret = NULL;
  90. BIGNUM *tmp_bn = NULL;
  91. if (!BN_hex2bn(&tmp_bn, buf))
  92. return NULL;
  93. ret = EC_POINT_bn2point(group, tmp_bn, point, ctx);
  94. BN_clear_free(tmp_bn);
  95. return ret;
  96. }