ECDSA_SIG_new.pod 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. If the argument is NULL, nothing is done.
  26. ECDSA_SIG_get0() returns internal pointers the I<r> and I<s> values contained
  27. in I<sig> and stores them in I<*pr> and I<*ps>, respectively.
  28. The pointer I<pr> or I<ps> can be NULL, in which case the corresponding value
  29. is not returned.
  30. The values I<r>, I<s> can also be retrieved separately by the corresponding
  31. function ECDSA_SIG_get0_r() and ECDSA_SIG_get0_s(), respectively.
  32. Non-NULL I<r> and I<s> values can be set on the I<sig> by calling
  33. ECDSA_SIG_set0(). Calling this function transfers the memory management of the
  34. values to the B<ECDSA_SIG> object, and therefore the values that have been
  35. passed in should not be freed by the caller.
  36. See L<i2d_ECDSA_SIG(3)> and L<d2i_ECDSA_SIG(3)> for information about encoding
  37. and decoding ECDSA signatures to/from DER.
  38. =head1 RETURN VALUES
  39. ECDSA_SIG_new() returns NULL if the allocation fails.
  40. ECDSA_SIG_set0() returns 1 on success or 0 on failure.
  41. ECDSA_SIG_get0_r() and ECDSA_SIG_get0_s() return the corresponding value,
  42. or NULL if it is unset.
  43. =head1 EXAMPLES
  44. Extract signature I<r> and I<s> values from a ECDSA I<signature>
  45. of size I<signaturelen>:
  46. ECDSA_SIG *obj;
  47. const BIGNUM *r, *s;
  48. /* Load a signature into the ECDSA_SIG object */
  49. obj = d2i_ECDSA_SIG(NULL, &signature, signaturelen);
  50. if (obj == NULL)
  51. /* error */
  52. r = ECDSA_SIG_get0_r(obj);
  53. s = ECDSA_SIG_get0_s(obj);
  54. if (r == NULL || s == NULL)
  55. /* error */
  56. /* Use BN_bn2binpad() here to convert to r and s into byte arrays */
  57. /*
  58. * Do not try to access I<r> or I<s> after calling ECDSA_SIG_free(),
  59. * as they are both freed by this call.
  60. */
  61. ECDSA_SIG_free(obj);
  62. Convert I<r> and I<s> byte arrays into an ECDSA_SIG I<signature> of
  63. size I<signaturelen>:
  64. ECDSA_SIG *obj = NULL;
  65. unsigned char *signature = NULL;
  66. size_t signaturelen;
  67. BIGNUM *rbn = NULL, *sbn = NULL;
  68. obj = ECDSA_SIG_new();
  69. if (obj == NULL)
  70. /* error */
  71. rbn = BN_bin2bn(r, rlen, NULL);
  72. sbn = BN_bin2bn(s, slen, NULL);
  73. if (rbn == NULL || sbn == NULL)
  74. /* error */
  75. if (!ECDSA_SIG_set0(obj, rbn, sbn))
  76. /* error */
  77. /* Set these to NULL since they are now owned by obj */
  78. rbn = sbn = NULL;
  79. signaturelen = i2d_ECDSA_SIG(obj, &signature);
  80. if (signaturelen <= 0)
  81. /* error */
  82. /*
  83. * This signature could now be passed to L<EVP_DigestVerify(3)>
  84. * or L<EVP_DigestVerifyFinal(3)>
  85. */
  86. BN_free(rbn);
  87. BN_free(sbn);
  88. OPENSSL_free(signature);
  89. ECDSA_SIG_free(obj);
  90. =head1 CONFORMING TO
  91. ANSI X9.62,
  92. US Federal Information Processing Standard FIPS186-4
  93. (Digital Signature Standard, DSS)
  94. =head1 SEE ALSO
  95. L<EC_KEY_new(3)>,
  96. L<EVP_DigestSignInit(3)>,
  97. L<EVP_DigestVerifyInit(3)>,
  98. L<EVP_PKEY_sign(3)>
  99. L<i2d_ECDSA_SIG(3)>,
  100. L<d2i_ECDSA_SIG(3)>,
  101. L<ECDSA_sign(3)>
  102. =head1 COPYRIGHT
  103. Copyright 2004-2024 The OpenSSL Project Authors. All Rights Reserved.
  104. Licensed under the Apache License 2.0 (the "License"). You may not use
  105. this file except in compliance with the License. You can obtain a copy
  106. in the file LICENSE in the source distribution or at
  107. L<https://www.openssl.org/source/license.html>.
  108. =cut