Path: blob/master/thirdparty/mbedtls/include/mbedtls/nist_kw.h
9904 views
/**1* \file nist_kw.h2*3* \brief This file provides an API for key wrapping (KW) and key wrapping with4* padding (KWP) as defined in NIST SP 800-38F.5* https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38F.pdf6*7* Key wrapping specifies a deterministic authenticated-encryption mode8* of operation, according to <em>NIST SP 800-38F: Recommendation for9* Block Cipher Modes of Operation: Methods for Key Wrapping</em>. Its10* purpose is to protect cryptographic keys.11*12* Its equivalent is RFC 3394 for KW, and RFC 5649 for KWP.13* https://tools.ietf.org/html/rfc339414* https://tools.ietf.org/html/rfc564915*16*/17/*18* Copyright The Mbed TLS Contributors19* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later20*/2122#ifndef MBEDTLS_NIST_KW_H23#define MBEDTLS_NIST_KW_H24#include "mbedtls/private_access.h"2526#include "mbedtls/build_info.h"2728#include "mbedtls/cipher.h"2930#ifdef __cplusplus31extern "C" {32#endif3334typedef enum {35MBEDTLS_KW_MODE_KW = 0,36MBEDTLS_KW_MODE_KWP = 137} mbedtls_nist_kw_mode_t;3839#if !defined(MBEDTLS_NIST_KW_ALT)40// Regular implementation41//4243/**44* \brief The key wrapping context-type definition. The key wrapping context is passed45* to the APIs called.46*47* \note The definition of this type may change in future library versions.48* Don't make any assumptions on this context!49*/50typedef struct {51mbedtls_cipher_context_t MBEDTLS_PRIVATE(cipher_ctx); /*!< The cipher context used. */52} mbedtls_nist_kw_context;5354#else /* MBEDTLS_NIST_key wrapping_ALT */55#include "nist_kw_alt.h"56#endif /* MBEDTLS_NIST_KW_ALT */5758/**59* \brief This function initializes the specified key wrapping context60* to make references valid and prepare the context61* for mbedtls_nist_kw_setkey() or mbedtls_nist_kw_free().62*63* \param ctx The key wrapping context to initialize.64*65*/66void mbedtls_nist_kw_init(mbedtls_nist_kw_context *ctx);6768/**69* \brief This function initializes the key wrapping context set in the70* \p ctx parameter and sets the encryption key.71*72* \param ctx The key wrapping context.73* \param cipher The 128-bit block cipher to use. Only AES is supported.74* \param key The Key Encryption Key (KEK).75* \param keybits The KEK size in bits. This must be acceptable by the cipher.76* \param is_wrap Specify whether the operation within the context is wrapping or unwrapping77*78* \return \c 0 on success.79* \return \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for any invalid input.80* \return \c MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE for 128-bit block ciphers81* which are not supported.82* \return cipher-specific error code on failure of the underlying cipher.83*/84int mbedtls_nist_kw_setkey(mbedtls_nist_kw_context *ctx,85mbedtls_cipher_id_t cipher,86const unsigned char *key,87unsigned int keybits,88const int is_wrap);8990/**91* \brief This function releases and clears the specified key wrapping context92* and underlying cipher sub-context.93*94* \param ctx The key wrapping context to clear.95*/96void mbedtls_nist_kw_free(mbedtls_nist_kw_context *ctx);9798/**99* \brief This function encrypts a buffer using key wrapping.100*101* \param ctx The key wrapping context to use for encryption.102* \param mode The key wrapping mode to use (MBEDTLS_KW_MODE_KW or MBEDTLS_KW_MODE_KWP)103* \param input The buffer holding the input data.104* \param in_len The length of the input data in Bytes.105* The input uses units of 8 Bytes called semiblocks.106* <ul><li>For KW mode: a multiple of 8 bytes between 16 and 2^57-8 inclusive. </li>107* <li>For KWP mode: any length between 1 and 2^32-1 inclusive.</li></ul>108* \param[out] output The buffer holding the output data.109* <ul><li>For KW mode: Must be at least 8 bytes larger than \p in_len.</li>110* <li>For KWP mode: Must be at least 8 bytes larger rounded up to a multiple of111* 8 bytes for KWP (15 bytes at most).</li></ul>112* \param[out] out_len The number of bytes written to the output buffer. \c 0 on failure.113* \param[in] out_size The capacity of the output buffer.114*115* \return \c 0 on success.116* \return \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for invalid input length.117* \return cipher-specific error code on failure of the underlying cipher.118*/119int mbedtls_nist_kw_wrap(mbedtls_nist_kw_context *ctx, mbedtls_nist_kw_mode_t mode,120const unsigned char *input, size_t in_len,121unsigned char *output, size_t *out_len, size_t out_size);122123/**124* \brief This function decrypts a buffer using key wrapping.125*126* \param ctx The key wrapping context to use for decryption.127* \param mode The key wrapping mode to use (MBEDTLS_KW_MODE_KW or MBEDTLS_KW_MODE_KWP)128* \param input The buffer holding the input data.129* \param in_len The length of the input data in Bytes.130* The input uses units of 8 Bytes called semiblocks.131* The input must be a multiple of semiblocks.132* <ul><li>For KW mode: a multiple of 8 bytes between 24 and 2^57 inclusive. </li>133* <li>For KWP mode: a multiple of 8 bytes between 16 and 2^32 inclusive.</li></ul>134* \param[out] output The buffer holding the output data.135* The output buffer's minimal length is 8 bytes shorter than \p in_len.136* \param[out] out_len The number of bytes written to the output buffer. \c 0 on failure.137* For KWP mode, the length could be up to 15 bytes shorter than \p in_len,138* depending on how much padding was added to the data.139* \param[in] out_size The capacity of the output buffer.140*141* \return \c 0 on success.142* \return \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for invalid input length.143* \return \c MBEDTLS_ERR_CIPHER_AUTH_FAILED for verification failure of the ciphertext.144* \return cipher-specific error code on failure of the underlying cipher.145*/146int mbedtls_nist_kw_unwrap(mbedtls_nist_kw_context *ctx, mbedtls_nist_kw_mode_t mode,147const unsigned char *input, size_t in_len,148unsigned char *output, size_t *out_len, size_t out_size);149150151#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)152/**153* \brief The key wrapping checkup routine.154*155* \return \c 0 on success.156* \return \c 1 on failure.157*/158int mbedtls_nist_kw_self_test(int verbose);159#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */160161#ifdef __cplusplus162}163#endif164165#endif /* MBEDTLS_NIST_KW_H */166167168