nist_kw.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 (C) 2018, Arm Limited (or its affiliates), All Rights Reserved
  20. * SPDX-License-Identifier: Apache-2.0
  21. *
  22. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  23. * not use this file except in compliance with the License.
  24. * You may obtain a copy of the License at
  25. *
  26. * http://www.apache.org/licenses/LICENSE-2.0
  27. *
  28. * Unless required by applicable law or agreed to in writing, software
  29. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  30. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  31. * See the License for the specific language governing permissions and
  32. * limitations under the License.
  33. *
  34. * This file is part of Mbed TLS (https://tls.mbed.org)
  35. */
  36. #ifndef MBEDTLS_NIST_KW_H
  37. #define MBEDTLS_NIST_KW_H
  38. #include "cipher.h"
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. typedef enum
  43. {
  44. MBEDTLS_KW_MODE_KW = 0,
  45. MBEDTLS_KW_MODE_KWP = 1
  46. } mbedtls_nist_kw_mode_t;
  47. #if !defined(MBEDTLS_NIST_KW_ALT)
  48. // Regular implementation
  49. //
  50. /**
  51. * \brief The key wrapping context-type definition. The key wrapping context is passed
  52. * to the APIs called.
  53. *
  54. * \note The definition of this type may change in future library versions.
  55. * Don't make any assumptions on this context!
  56. */
  57. typedef struct {
  58. mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
  59. } mbedtls_nist_kw_context;
  60. #else /* MBEDTLS_NIST_key wrapping_ALT */
  61. #include "nist_kw_alt.h"
  62. #endif /* MBEDTLS_NIST_KW_ALT */
  63. /**
  64. * \brief This function initializes the specified key wrapping context
  65. * to make references valid and prepare the context
  66. * for mbedtls_nist_kw_setkey() or mbedtls_nist_kw_free().
  67. *
  68. * \param ctx The key wrapping context to initialize.
  69. *
  70. */
  71. void mbedtls_nist_kw_init( mbedtls_nist_kw_context *ctx );
  72. /**
  73. * \brief This function initializes the key wrapping context set in the
  74. * \p ctx parameter and sets the encryption key.
  75. *
  76. * \param ctx The key wrapping context.
  77. * \param cipher The 128-bit block cipher to use. Only AES is supported.
  78. * \param key The Key Encryption Key (KEK).
  79. * \param keybits The KEK size in bits. This must be acceptable by the cipher.
  80. * \param is_wrap Specify whether the operation within the context is wrapping or unwrapping
  81. *
  82. * \return \c 0 on success.
  83. * \return \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for any invalid input.
  84. * \return \c MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE for 128-bit block ciphers
  85. * which are not supported.
  86. * \return cipher-specific error code on failure of the underlying cipher.
  87. */
  88. int mbedtls_nist_kw_setkey( mbedtls_nist_kw_context *ctx,
  89. mbedtls_cipher_id_t cipher,
  90. const unsigned char *key,
  91. unsigned int keybits,
  92. const int is_wrap );
  93. /**
  94. * \brief This function releases and clears the specified key wrapping context
  95. * and underlying cipher sub-context.
  96. *
  97. * \param ctx The key wrapping context to clear.
  98. */
  99. void mbedtls_nist_kw_free( mbedtls_nist_kw_context *ctx );
  100. /**
  101. * \brief This function encrypts a buffer using key wrapping.
  102. *
  103. * \param ctx The key wrapping context to use for encryption.
  104. * \param mode The key wrapping mode to use (MBEDTLS_KW_MODE_KW or MBEDTLS_KW_MODE_KWP)
  105. * \param input The buffer holding the input data.
  106. * \param in_len The length of the input data in Bytes.
  107. * The input uses units of 8 Bytes called semiblocks.
  108. * <ul><li>For KW mode: a multiple of 8 bytes between 16 and 2^57-8 inclusive. </li>
  109. * <li>For KWP mode: any length between 1 and 2^32-1 inclusive.</li></ul>
  110. * \param[out] output The buffer holding the output data.
  111. * <ul><li>For KW mode: Must be at least 8 bytes larger than \p in_len.</li>
  112. * <li>For KWP mode: Must be at least 8 bytes larger rounded up to a multiple of
  113. * 8 bytes for KWP (15 bytes at most).</li></ul>
  114. * \param[out] out_len The number of bytes written to the output buffer. \c 0 on failure.
  115. * \param[in] out_size The capacity of the output buffer.
  116. *
  117. * \return \c 0 on success.
  118. * \return \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for invalid input length.
  119. * \return cipher-specific error code on failure of the underlying cipher.
  120. */
  121. int mbedtls_nist_kw_wrap( mbedtls_nist_kw_context *ctx, mbedtls_nist_kw_mode_t mode,
  122. const unsigned char *input, size_t in_len,
  123. unsigned char *output, size_t* out_len, size_t out_size );
  124. /**
  125. * \brief This function decrypts a buffer using key wrapping.
  126. *
  127. * \param ctx The key wrapping context to use for decryption.
  128. * \param mode The key wrapping mode to use (MBEDTLS_KW_MODE_KW or MBEDTLS_KW_MODE_KWP)
  129. * \param input The buffer holding the input data.
  130. * \param in_len The length of the input data in Bytes.
  131. * The input uses units of 8 Bytes called semiblocks.
  132. * The input must be a multiple of semiblocks.
  133. * <ul><li>For KW mode: a multiple of 8 bytes between 24 and 2^57 inclusive. </li>
  134. * <li>For KWP mode: a multiple of 8 bytes between 16 and 2^32 inclusive.</li></ul>
  135. * \param[out] output The buffer holding the output data.
  136. * The output buffer's minimal length is 8 bytes shorter than \p in_len.
  137. * \param[out] out_len The number of bytes written to the output buffer. \c 0 on failure.
  138. * For KWP mode, the length could be up to 15 bytes shorter than \p in_len,
  139. * depending on how much padding was added to the data.
  140. * \param[in] out_size The capacity of the output buffer.
  141. *
  142. * \return \c 0 on success.
  143. * \return \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for invalid input length.
  144. * \return \c MBEDTLS_ERR_CIPHER_AUTH_FAILED for verification failure of the ciphertext.
  145. * \return cipher-specific error code on failure of the underlying cipher.
  146. */
  147. int mbedtls_nist_kw_unwrap( mbedtls_nist_kw_context *ctx, mbedtls_nist_kw_mode_t mode,
  148. const unsigned char *input, size_t in_len,
  149. unsigned char *output, size_t* out_len, size_t out_size);
  150. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  151. /**
  152. * \brief The key wrapping checkup routine.
  153. *
  154. * \return \c 0 on success.
  155. * \return \c 1 on failure.
  156. */
  157. int mbedtls_nist_kw_self_test( int verbose );
  158. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  159. #ifdef __cplusplus
  160. }
  161. #endif
  162. #endif /* MBEDTLS_NIST_KW_H */