nist_kw.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * \file nist_kw.h
  3. *
  4. * \brief This file provides an API for key wrapping (KW) and key wrapping with
  5. * padding (KWP) as defined in NIST SP 800-38F.
  6. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38F.pdf
  7. *
  8. * Key wrapping specifies a deterministic authenticated-encryption mode
  9. * of operation, according to <em>NIST SP 800-38F: Recommendation for
  10. * Block Cipher Modes of Operation: Methods for Key Wrapping</em>. Its
  11. * purpose is to protect cryptographic keys.
  12. *
  13. * Its equivalent is RFC 3394 for KW, and RFC 5649 for KWP.
  14. * https://tools.ietf.org/html/rfc3394
  15. * https://tools.ietf.org/html/rfc5649
  16. *
  17. */
  18. /*
  19. * Copyright The Mbed TLS Contributors
  20. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  21. */
  22. #ifndef MBEDTLS_NIST_KW_H
  23. #define MBEDTLS_NIST_KW_H
  24. #include "mbedtls/private_access.h"
  25. #include "mbedtls/build_info.h"
  26. #include "mbedtls/cipher.h"
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. typedef enum {
  31. MBEDTLS_KW_MODE_KW = 0,
  32. MBEDTLS_KW_MODE_KWP = 1
  33. } mbedtls_nist_kw_mode_t;
  34. #if !defined(MBEDTLS_NIST_KW_ALT)
  35. // Regular implementation
  36. //
  37. /**
  38. * \brief The key wrapping context-type definition. The key wrapping context is passed
  39. * to the APIs called.
  40. *
  41. * \note The definition of this type may change in future library versions.
  42. * Don't make any assumptions on this context!
  43. */
  44. typedef struct {
  45. mbedtls_cipher_context_t MBEDTLS_PRIVATE(cipher_ctx); /*!< The cipher context used. */
  46. } mbedtls_nist_kw_context;
  47. #else /* MBEDTLS_NIST_key wrapping_ALT */
  48. #include "nist_kw_alt.h"
  49. #endif /* MBEDTLS_NIST_KW_ALT */
  50. /**
  51. * \brief This function initializes the specified key wrapping context
  52. * to make references valid and prepare the context
  53. * for mbedtls_nist_kw_setkey() or mbedtls_nist_kw_free().
  54. *
  55. * \param ctx The key wrapping context to initialize.
  56. *
  57. */
  58. void mbedtls_nist_kw_init(mbedtls_nist_kw_context *ctx);
  59. /**
  60. * \brief This function initializes the key wrapping context set in the
  61. * \p ctx parameter and sets the encryption key.
  62. *
  63. * \param ctx The key wrapping context.
  64. * \param cipher The 128-bit block cipher to use. Only AES is supported.
  65. * \param key The Key Encryption Key (KEK).
  66. * \param keybits The KEK size in bits. This must be acceptable by the cipher.
  67. * \param is_wrap Specify whether the operation within the context is wrapping or unwrapping
  68. *
  69. * \return \c 0 on success.
  70. * \return \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for any invalid input.
  71. * \return \c MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE for 128-bit block ciphers
  72. * which are not supported.
  73. * \return cipher-specific error code on failure of the underlying cipher.
  74. */
  75. int mbedtls_nist_kw_setkey(mbedtls_nist_kw_context *ctx,
  76. mbedtls_cipher_id_t cipher,
  77. const unsigned char *key,
  78. unsigned int keybits,
  79. const int is_wrap);
  80. /**
  81. * \brief This function releases and clears the specified key wrapping context
  82. * and underlying cipher sub-context.
  83. *
  84. * \param ctx The key wrapping context to clear.
  85. */
  86. void mbedtls_nist_kw_free(mbedtls_nist_kw_context *ctx);
  87. /**
  88. * \brief This function encrypts a buffer using key wrapping.
  89. *
  90. * \param ctx The key wrapping context to use for encryption.
  91. * \param mode The key wrapping mode to use (MBEDTLS_KW_MODE_KW or MBEDTLS_KW_MODE_KWP)
  92. * \param input The buffer holding the input data.
  93. * \param in_len The length of the input data in Bytes.
  94. * The input uses units of 8 Bytes called semiblocks.
  95. * <ul><li>For KW mode: a multiple of 8 bytes between 16 and 2^57-8 inclusive. </li>
  96. * <li>For KWP mode: any length between 1 and 2^32-1 inclusive.</li></ul>
  97. * \param[out] output The buffer holding the output data.
  98. * <ul><li>For KW mode: Must be at least 8 bytes larger than \p in_len.</li>
  99. * <li>For KWP mode: Must be at least 8 bytes larger rounded up to a multiple of
  100. * 8 bytes for KWP (15 bytes at most).</li></ul>
  101. * \param[out] out_len The number of bytes written to the output buffer. \c 0 on failure.
  102. * \param[in] out_size The capacity of the output buffer.
  103. *
  104. * \return \c 0 on success.
  105. * \return \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for invalid input length.
  106. * \return cipher-specific error code on failure of the underlying cipher.
  107. */
  108. int mbedtls_nist_kw_wrap(mbedtls_nist_kw_context *ctx, mbedtls_nist_kw_mode_t mode,
  109. const unsigned char *input, size_t in_len,
  110. unsigned char *output, size_t *out_len, size_t out_size);
  111. /**
  112. * \brief This function decrypts a buffer using key wrapping.
  113. *
  114. * \param ctx The key wrapping context to use for decryption.
  115. * \param mode The key wrapping mode to use (MBEDTLS_KW_MODE_KW or MBEDTLS_KW_MODE_KWP)
  116. * \param input The buffer holding the input data.
  117. * \param in_len The length of the input data in Bytes.
  118. * The input uses units of 8 Bytes called semiblocks.
  119. * The input must be a multiple of semiblocks.
  120. * <ul><li>For KW mode: a multiple of 8 bytes between 24 and 2^57 inclusive. </li>
  121. * <li>For KWP mode: a multiple of 8 bytes between 16 and 2^32 inclusive.</li></ul>
  122. * \param[out] output The buffer holding the output data.
  123. * The output buffer's minimal length is 8 bytes shorter than \p in_len.
  124. * \param[out] out_len The number of bytes written to the output buffer. \c 0 on failure.
  125. * For KWP mode, the length could be up to 15 bytes shorter than \p in_len,
  126. * depending on how much padding was added to the data.
  127. * \param[in] out_size The capacity of the output buffer.
  128. *
  129. * \return \c 0 on success.
  130. * \return \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for invalid input length.
  131. * \return \c MBEDTLS_ERR_CIPHER_AUTH_FAILED for verification failure of the ciphertext.
  132. * \return cipher-specific error code on failure of the underlying cipher.
  133. */
  134. int mbedtls_nist_kw_unwrap(mbedtls_nist_kw_context *ctx, mbedtls_nist_kw_mode_t mode,
  135. const unsigned char *input, size_t in_len,
  136. unsigned char *output, size_t *out_len, size_t out_size);
  137. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  138. /**
  139. * \brief The key wrapping checkup routine.
  140. *
  141. * \return \c 0 on success.
  142. * \return \c 1 on failure.
  143. */
  144. int mbedtls_nist_kw_self_test(int verbose);
  145. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. #endif /* MBEDTLS_NIST_KW_H */