ECDSA_SIG_new.pod 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. =pod
  2. =head1 NAME
  3. ECDSA_SIG_new, ECDSA_SIG_free,
  4. ECDSA_SIG_get0, ECDSA_SIG_get0_r, ECDSA_SIG_get0_s, ECDSA_SIG_set0
  5. - Functions for creating, destroying and manipulating ECDSA_SIG objects
  6. =head1 SYNOPSIS
  7. #include <openssl/ecdsa.h>
  8. ECDSA_SIG *ECDSA_SIG_new(void);
  9. void ECDSA_SIG_free(ECDSA_SIG *sig);
  10. void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
  11. const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig);
  12. const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig);
  13. int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
  14. =head1 DESCRIPTION
  15. B<ECDSA_SIG> is an opaque structure consisting of two BIGNUMs for the
  16. I<r> and I<s> value of an Elliptic Curve Digital Signature Algorithm (ECDSA) signature
  17. (see FIPS186-4 or X9.62).
  18. The B<ECDSA_SIG> object was mainly used by the deprecated low level functions described in
  19. L<ECDSA_sign(3)>, it is still required in order to be able to set or get the values of
  20. I<r> and I<s> into or from a signature. This is mainly used for testing purposes as shown
  21. in the L</EXAMPLES>.
  22. ECDSA_SIG_new() allocates an empty B<ECDSA_SIG> structure.
  23. Note: before OpenSSL 1.1.0, the I<r> and I<s> components were initialised.
  24. ECDSA_SIG_free() frees the B<ECDSA_SIG> structure I<sig>.
  25. ECDSA_SIG_get0() returns internal pointers the I<r> and I<s> values contained
  26. in I<sig> and stores them in I<*pr> and I<*ps>, respectively.
  27. The pointer I<pr> or I<ps> can be NULL, in which case the corresponding value
  28. is not returned.
  29. The values I<r>, I<s> can also be retrieved separately by the corresponding
  30. function ECDSA_SIG_get0_r() and ECDSA_SIG_get0_s(), respectively.
  31. Non-NULL I<r> and I<s> values can be set on the I<sig> by calling
  32. ECDSA_SIG_set0(). Calling this function transfers the memory management of the
  33. values to the B<ECDSA_SIG> object, and therefore the values that have been
  34. passed in should not be freed by the caller.
  35. See L<i2d_ECDSA_SIG(3)> and L<d2i_ECDSA_SIG(3)> for information about encoding
  36. and decoding ECDSA signatures to/from DER.
  37. =head1 RETURN VALUES
  38. ECDSA_SIG_new() returns NULL if the allocation fails.
  39. ECDSA_SIG_set0() returns 1 on success or 0 on failure.
  40. ECDSA_SIG_get0_r() and ECDSA_SIG_get0_s() return the corresponding value,
  41. or NULL if it is unset.
  42. =head1 EXAMPLES
  43. Extract signature I<r> and I<s> values from a ECDSA I<signature>
  44. of size I<signaturelen>:
  45. ECDSA_SIG *obj;
  46. const BIGNUM *r, *s;
  47. /* Load a signature into the ECDSA_SIG object */
  48. obj = d2i_ECDSA_SIG(NULL, &signature, signaturelen);
  49. if (obj == NULL)
  50. /* error */
  51. r = ECDSA_SIG_get0_r(obj);
  52. s = ECDSA_SIG_get0_s(obj);
  53. if (r == NULL || s == NULL)
  54. /* error */
  55. /* Use BN_bn2binpad() here to convert to r and s into byte arrays */
  56. /*
  57. * Do not try to access I<r> or I<s> after calling ECDSA_SIG_free(),
  58. * as they are both freed by this call.
  59. */
  60. ECDSA_SIG_free(obj);
  61. Convert I<r> and I<s> byte arrays into an ECDSA_SIG I<signature> of
  62. size I<signaturelen>:
  63. ECDSA_SIG *obj = NULL;
  64. unsigned char *signature = NULL;
  65. size_t signaturelen;
  66. BIGNUM *rbn = NULL, *sbn = NULL;
  67. obj = ECDSA_SIG_new();
  68. if (obj == NULL)
  69. /* error */
  70. rbn = BN_bin2bn(r, rlen, NULL);
  71. sbn = BN_bin2bn(s, slen, NULL);
  72. if (rbn == NULL || sbn == NULL)
  73. /* error */
  74. if (!ECDSA_SIG_set0(obj, rbn, sbn))
  75. /* error */
  76. /* Set these to NULL since they are now owned by obj */
  77. rbn = sbn = NULL;
  78. signaturelen = i2d_ECDSA_SIG(obj, &signature);
  79. if (signaturelen <= 0)
  80. /* error */
  81. /*
  82. * This signature could now be passed to L<EVP_DigestVerify(3)>
  83. * or L<EVP_DigestVerifyFinal(3)>
  84. */
  85. BN_free(rbn);
  86. BN_free(sbn);
  87. OPENSSL_free(signature);
  88. ECDSA_SIG_free(obj);
  89. =head1 CONFORMING TO
  90. ANSI X9.62,
  91. US Federal Information Processing Standard FIPS186-4
  92. (Digital Signature Standard, DSS)
  93. =head1 SEE ALSO
  94. L<EC_KEY_new(3)>,
  95. L<EVP_DigestSignInit(3)>,
  96. L<EVP_DigestVerifyInit(3)>,
  97. L<EVP_PKEY_sign(3)>
  98. L<i2d_ECDSA_SIG(3)>,
  99. L<d2i_ECDSA_SIG(3)>,
  100. L<ECDSA_sign(3)>
  101. =head1 COPYRIGHT
  102. Copyright 2004-2022 The OpenSSL Project Authors. All Rights Reserved.
  103. Licensed under the Apache License 2.0 (the "License"). You may not use
  104. this file except in compliance with the License. You can obtain a copy
  105. in the file LICENSE in the source distribution or at
  106. L<https://www.openssl.org/source/license.html>.
  107. =cut