optiga_backend_utils.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef OPTIGA_BACKEND_UTILS_H__
  41. #define OPTIGA_BACKEND_UTILS_H__
  42. #include <stdint.h>
  43. #include <stdbool.h>
  44. #include <stddef.h>
  45. #include "sdk_config.h"
  46. #include "nordic_common.h"
  47. #include "sdk_errors.h"
  48. /** @brief Define for a integer tag in DER encoding. */
  49. #define DER_TAG_INTEGER 0x02
  50. /** @brief Max size of integer in DER encoding.
  51. *
  52. * @note This limit is for this implementation only, ASN.1 DER supports more
  53. */
  54. #define DER_INTEGER_MAX_LEN 0x7F
  55. /** @brief Define for overhead to encode a DER of two integers
  56. *
  57. * @details TAG + LENGTH needs 2 bytes if the highest bit of the integer is set
  58. * we need an extra byte
  59. */
  60. #define DER_OVERHEAD ((2 + 1) * 2)
  61. /**
  62. * @brief Decodes two ASN.1 integers to the R and S components of a ECC signature.
  63. *
  64. * @param[in] p_asn1 Pointer to buffer containing the ASN.1 encoded R and S values.
  65. * @param[in] asn1_len Length of the asn1 buffer.
  66. * @param[out] p_rs Pointer to buffer where to write the R and S values
  67. * @param[in,out] p_rs_len pointer to variable containing length of the rs buffer,
  68. * updated to actual length after the call.
  69. *
  70. * @returns NRF_SUCCESS on success, otherwise NRF_ERROR_CRYPTO_INTERNAL.
  71. */
  72. ret_code_t asn1_to_ecdsa_rs(uint8_t const * p_asn1,
  73. size_t asn1_len,
  74. uint8_t * p_rs,
  75. size_t * p_rs_len);
  76. /**
  77. * @brief Encodes the ECDSA signature components (r, s) in ASN.1 format.
  78. *
  79. * @param[in] p_r Pointer to buffer containing component r of the ECDSA signature.
  80. * @param[in] r_len Length of the r component of the ECDSA signature.
  81. * @param[in] p_s Pointer to buffer containing component s of the ECDSA signature.
  82. * @param[in] s_len Length of the s component of the ECDSA signature.
  83. * @param[out] p_asn_sig Pointer to buffer to hold the resulting ASN.1-encoded ECDSA signature.
  84. * @param[out] p_asn_sig_len Pointer to variable holding the length of the buffer for ASN.1-encoded
  85. * ECDSA signature. This will be updated to the actual size when the
  86. * function is called.
  87. *
  88. * @returns True on success, otherwise false.
  89. */
  90. bool ecdsa_rs_to_asn1(uint8_t const * p_r,
  91. size_t r_len,
  92. uint8_t const * p_s,
  93. size_t s_len,
  94. uint8_t * p_asn_sig,
  95. size_t * p_asn_sig_len);
  96. #endif // OPTIGA_BACKEND_UTILS_H__