Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  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: GPL-2.0
  21.  *
  22.  *  This program is free software; you can redistribute it and/or modify
  23.  *  it under the terms of the GNU General Public License as published by
  24.  *  the Free Software Foundation; either version 2 of the License, or
  25.  *  (at your option) any later version.
  26.  *
  27.  *  This program is distributed in the hope that it will be useful,
  28.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  29.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30.  *  GNU General Public License for more details.
  31.  *
  32.  *  You should have received a copy of the GNU General Public License along
  33.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  34.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  35.  *
  36.  *  This file is part of Mbed TLS (https://tls.mbed.org)
  37.  */
  38.  
  39. #ifndef MBEDTLS_NIST_KW_H
  40. #define MBEDTLS_NIST_KW_H
  41.  
  42. #if !defined(MBEDTLS_CONFIG_FILE)
  43. #include "config.h"
  44. #else
  45. #include MBEDTLS_CONFIG_FILE
  46. #endif
  47.  
  48. #include "cipher.h"
  49.  
  50. #ifdef __cplusplus
  51. extern "C" {
  52. #endif
  53.  
  54. typedef enum
  55. {
  56.     MBEDTLS_KW_MODE_KW = 0,
  57.     MBEDTLS_KW_MODE_KWP = 1
  58. } mbedtls_nist_kw_mode_t;
  59.  
  60. #if !defined(MBEDTLS_NIST_KW_ALT)
  61. // Regular implementation
  62. //
  63.  
  64. /**
  65.  * \brief    The key wrapping context-type definition. The key wrapping context is passed
  66.  *           to the APIs called.
  67.  *
  68.  * \note     The definition of this type may change in future library versions.
  69.  *           Don't make any assumptions on this context!
  70.  */
  71. typedef struct {
  72.     mbedtls_cipher_context_t cipher_ctx;    /*!< The cipher context used. */
  73. } mbedtls_nist_kw_context;
  74.  
  75. #else  /* MBEDTLS_NIST_key wrapping_ALT */
  76. #include "nist_kw_alt.h"
  77. #endif /* MBEDTLS_NIST_KW_ALT */
  78.  
  79. /**
  80.  * \brief           This function initializes the specified key wrapping context
  81.  *                  to make references valid and prepare the context
  82.  *                  for mbedtls_nist_kw_setkey() or mbedtls_nist_kw_free().
  83.  *
  84.  * \param ctx       The key wrapping context to initialize.
  85.  *
  86.  */
  87. void mbedtls_nist_kw_init( mbedtls_nist_kw_context *ctx );
  88.  
  89. /**
  90.  * \brief           This function initializes the key wrapping context set in the
  91.  *                  \p ctx parameter and sets the encryption key.
  92.  *
  93.  * \param ctx       The key wrapping context.
  94.  * \param cipher    The 128-bit block cipher to use. Only AES is supported.
  95.  * \param key       The Key Encryption Key (KEK).
  96.  * \param keybits   The KEK size in bits. This must be acceptable by the cipher.
  97.  * \param is_wrap   Specify whether the operation within the context is wrapping or unwrapping
  98.  *
  99.  * \return          \c 0 on success.
  100.  * \return          \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for any invalid input.
  101.  * \return          \c MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE for 128-bit block ciphers
  102.  *                  which are not supported.
  103.  * \return          cipher-specific error code on failure of the underlying cipher.
  104.  */
  105. int mbedtls_nist_kw_setkey( mbedtls_nist_kw_context *ctx,
  106.                             mbedtls_cipher_id_t cipher,
  107.                             const unsigned char *key,
  108.                             unsigned int keybits,
  109.                             const int is_wrap );
  110.  
  111. /**
  112.  * \brief   This function releases and clears the specified key wrapping context
  113.  *          and underlying cipher sub-context.
  114.  *
  115.  * \param ctx       The key wrapping context to clear.
  116.  */
  117. void mbedtls_nist_kw_free( mbedtls_nist_kw_context *ctx );
  118.  
  119. /**
  120.  * \brief           This function encrypts a buffer using key wrapping.
  121.  *
  122.  * \param ctx       The key wrapping context to use for encryption.
  123.  * \param mode      The key wrapping mode to use (MBEDTLS_KW_MODE_KW or MBEDTLS_KW_MODE_KWP)
  124.  * \param input     The buffer holding the input data.
  125.  * \param in_len    The length of the input data in Bytes.
  126.  *                  The input uses units of 8 Bytes called semiblocks.
  127.  *                  <ul><li>For KW mode: a multiple of 8 bytes between 16 and 2^57-8 inclusive. </li>
  128.  *                  <li>For KWP mode: any length between 1 and 2^32-1 inclusive.</li></ul>
  129.  * \param[out] output    The buffer holding the output data.
  130.  *                  <ul><li>For KW mode: Must be at least 8 bytes larger than \p in_len.</li>
  131.  *                  <li>For KWP mode: Must be at least 8 bytes larger rounded up to a multiple of
  132.  *                  8 bytes for KWP (15 bytes at most).</li></ul>
  133.  * \param[out] out_len The number of bytes written to the output buffer. \c 0 on failure.
  134.  * \param[in] out_size The capacity of the output buffer.
  135.  *
  136.  * \return          \c 0 on success.
  137.  * \return          \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for invalid input length.
  138.  * \return          cipher-specific error code on failure of the underlying cipher.
  139.  */
  140. int mbedtls_nist_kw_wrap( mbedtls_nist_kw_context *ctx, mbedtls_nist_kw_mode_t mode,
  141.                           const unsigned char *input, size_t in_len,
  142.                           unsigned char *output, size_t* out_len, size_t out_size );
  143.  
  144. /**
  145.  * \brief           This function decrypts a buffer using key wrapping.
  146.  *
  147.  * \param ctx       The key wrapping context to use for decryption.
  148.  * \param mode      The key wrapping mode to use (MBEDTLS_KW_MODE_KW or MBEDTLS_KW_MODE_KWP)
  149.  * \param input     The buffer holding the input data.
  150.  * \param in_len    The length of the input data in Bytes.
  151.  *                  The input uses units of 8 Bytes called semiblocks.
  152.  *                  The input must be a multiple of semiblocks.
  153.  *                  <ul><li>For KW mode: a multiple of 8 bytes between 24 and 2^57 inclusive. </li>
  154.  *                  <li>For KWP mode: a multiple of 8 bytes between 16 and 2^32 inclusive.</li></ul>
  155.  * \param[out] output    The buffer holding the output data.
  156.  *                  The output buffer's minimal length is 8 bytes shorter than \p in_len.
  157.  * \param[out] out_len The number of bytes written to the output buffer. \c 0 on failure.
  158.  *                  For KWP mode, the length could be up to 15 bytes shorter than \p in_len,
  159.  *                  depending on how much padding was added to the data.
  160.  * \param[in] out_size The capacity of the output buffer.
  161.  *
  162.  * \return          \c 0 on success.
  163.  * \return          \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for invalid input length.
  164.  * \return          \c MBEDTLS_ERR_CIPHER_AUTH_FAILED for verification failure of the ciphertext.
  165.  * \return          cipher-specific error code on failure of the underlying cipher.
  166.  */
  167. int mbedtls_nist_kw_unwrap( mbedtls_nist_kw_context *ctx, mbedtls_nist_kw_mode_t mode,
  168.                             const unsigned char *input, size_t in_len,
  169.                             unsigned char *output, size_t* out_len, size_t out_size);
  170.  
  171.  
  172. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  173. /**
  174.  * \brief          The key wrapping checkup routine.
  175.  *
  176.  * \return         \c 0 on success.
  177.  * \return         \c 1 on failure.
  178.  */
  179. int mbedtls_nist_kw_self_test( int verbose );
  180. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  181.  
  182. #ifdef __cplusplus
  183. }
  184. #endif
  185.  
  186. #endif /* MBEDTLS_NIST_KW_H */
  187.