ecdsa_utils.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * MIT License
  3. *
  4. * Copyright (c) 2018 Infineon Technologies AG
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE
  23. *
  24. *
  25. * \file ecdsa_utils.h
  26. *
  27. * \brief This file contains utilities for ECDSA signature encoding/decoding
  28. *
  29. *
  30. * \addtogroup grOptigaUtil
  31. * @{
  32. */
  33. #ifndef _H_ECDSA_UTILS_H_
  34. #define _H_ECDSA_UTILS_H_
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. #include <stdbool.h>
  39. #include <stdint.h>
  40. #include <stddef.h>
  41. // Encode two integers in DER format
  42. // TAG + LENGTH needs 2 bytes for this implementation.
  43. // If the highest bit of the integer is set we need an extra byte.
  44. /** @brief Maximum overhead of the ASN.1 encoding for the R and S components as concatenated DER INTEGERs */
  45. #define ECDSA_RS_MAX_ASN1_OVERHEAD ((2 + 1) * 2)
  46. // Encode two integers as SEQUENCE of INTEGERs in DER format
  47. // TAG + LENGTH fields need 2 bytes and if the highest bit of the integer is set,
  48. // we need an extra byte, so we have 3 bytes overhead per INTEGER.
  49. // SEQUENCE encoding needs an addtional 2 bytes overhead, for TAG + LENGTH fields.
  50. // By limiting the LENGTH field to one byte, we are limited to 127 bytes for the two DER INTEGERS,
  51. // this allows a maximum of 60 bytes per R and S component which is sufficient for the
  52. // NIST P-256 and NIST P-384 curves we deal with.
  53. /** @brief Maximum overhead of the ASN.1 encoding for the R and S components as SEQUENCE of INTEGERs */
  54. #define ECDSA_SIGNATURE_MAX_ASN1_OVERHEAD (ECDSA_RS_MAX_ASN1_OVERHEAD + 2)
  55. /**
  56. * @brief Encodes the ECDSA signature components (r, s) as two concatenated ASN.1 INTEGERs. This is the format
  57. * the OPTIGA hostcode expects.
  58. *
  59. * @param[in] r Component R of the ECDSA signature
  60. * @param[in] s Component S of the ECDSA signature
  61. * @param[in] rs_len Length of each buffer for the R and S components of the ECDSA signature, must be smaller than 127
  62. * @param[out] asn_sig Buffer where the ASN.1 encoded result will be stored
  63. * @param[out] asn_sig_len Length of the ASN.1 encoded data that was copied into the output buffer
  64. * @returns true on success, false on error
  65. * @note The output buffer must be at least 2*rs_len + ECDSA_SIGNATURE_MAX_ASN1_OVERHEAD to fit the result in all cases.
  66. * @note If the function returns false, all output values are invalid.
  67. */
  68. bool ecdsa_rs_to_asn1_integers(const uint8_t* r, const uint8_t* s, size_t rs_len,
  69. uint8_t* asn_sig, size_t* asn_sig_len);
  70. /**
  71. * @brief Encodes the ECDSA signature components (r, s) as ASN.1 SEQUENCE of two INTEGERs. This is the format
  72. * mbedTLS and OpenSSL use.
  73. *
  74. * @param[in] r Component R of the ECDSA signature
  75. * @param[in] s Component S of the ECDSA signature
  76. * @param[in] rs_len Length of each buffer for the R and S components of the ECDSA signature, must be smaller than 127
  77. * @param[out] asn_sig Buffer where the ASN.1-encoded ECDSA signature will be stored
  78. * @param[out] asn_sig_len Length of the actual data that was copied into the output buffer
  79. * @returns true on success, false on error
  80. * @note The output buffer must be at least 2*rs_len + ECDSA_RS_MAX_ASN1_OVERHEAD to fit the result in all cases
  81. * @note If the function returns false, all output values are invalid.
  82. */
  83. bool ecdsa_rs_to_asn1_signature(const uint8_t* r, const uint8_t* s, size_t rs_len,
  84. uint8_t* asn_sig, size_t* asn_sig_len);
  85. /**
  86. * @brief decodes two concatenated ASN.1 integers to the R and S components of an ECDSA signature
  87. * @param[in] asn1 Buffer containing the ASN.1 encoded R and S values as two concatenated DER INTEGERs
  88. * @param[in] asn1_len Length of the asn1 buffer
  89. * @param[out] rs Output buffer for the concatenated R and S values
  90. * @param[in] rs_len Length of the rs buffer
  91. * @returns true on success, false else
  92. * @note The R and S components will be padded with zeros in the output buffer and each component will take rs_len/2 bytes.
  93. * e.g.: [ (0x00) R | S ], where '|' denotes the border for half the rs buffer,
  94. * 'R' and 'S' the bytes of the R and S components and '(0x00)' one or multiple padding bytes
  95. * needed to completely fill the buffer.
  96. * If you need to know the exact length of R and S use asn1_to_ecdsa_rs_sep(...)
  97. * @note If the function returns false, all output values are invalid.
  98. */
  99. bool asn1_to_ecdsa_rs(const uint8_t* asn1, size_t asn1_len,
  100. uint8_t* rs, size_t rs_len);
  101. /**
  102. * @brief decodes two concatenated ASN.1 integers to the R and S components of an ECDSA signature with separate buffers for R and S
  103. * @param[in] asn1 Buffer containing the ASN.1 encoded R and S values as two concatenated DER INTEGERs
  104. * @param[in] asn1_len Length of the asn1 buffer
  105. * @param[out] r Output buffer for the R value
  106. * @param[in,out] r_len Length of the r buffer, contains the number of non padding bytes afterwards
  107. * @param[out] s Output buffer for the S value
  108. * @param[in,out] s_len Length of the s buffer, contains the number of non padding bytes afterwards
  109. * @returns true on success, false else
  110. * @note The r and s buffers will be padded at the least significant byte with zeros to the length of the buffer.
  111. * @note If the function returns false, all output values are invalid.
  112. */
  113. bool asn1_to_ecdsa_rs_sep(const uint8_t* asn1, size_t asn1_len,
  114. uint8_t* r, size_t* r_len,
  115. uint8_t* s, size_t* s_len);
  116. #ifdef __cplusplus
  117. }
  118. #endif
  119. #endif // _H_ECDSA_UTILS_H_
  120. /**
  121. * @}
  122. */