| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /** | ||
| * \file hkdf.h | ||
| * | ||
| * \brief This file contains the HKDF interface. | ||
| * | ||
| * The HMAC-based Extract-and-Expand Key Derivation Function (HKDF) is | ||
| * specified by RFC 5869. | ||
| */ | ||
| /* | ||
| * Copyright (C) 2016-2018, ARM Limited, All Rights Reserved | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * This file is part of mbed TLS (https://tls.mbed.org) | ||
| */ | ||
| #ifndef MBEDTLS_HKDF_H | ||
| #define MBEDTLS_HKDF_H | ||
|
|
||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||
| #include "config.h" | ||
| #else | ||
| #include MBEDTLS_CONFIG_FILE | ||
| #endif | ||
|
|
||
| #include "md.h" | ||
|
|
||
| /** | ||
| * \name HKDF Error codes | ||
| * \{ | ||
| */ | ||
| #define MBEDTLS_ERR_HKDF_BAD_INPUT_DATA -0x5F80 /**< Bad input parameters to function. */ | ||
| /* \} name */ | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief This is the HMAC-based Extract-and-Expand Key Derivation Function | ||
| * (HKDF). | ||
| * | ||
| * \param md A hash function; md.size denotes the length of the hash | ||
| * function output in bytes. | ||
| * \param salt An optional salt value (a non-secret random value); | ||
| * if the salt is not provided, a string of all zeros of | ||
| * md.size length is used as the salt. | ||
| * \param salt_len The length in bytes of the optional \p salt. | ||
| * \param ikm The input keying material. | ||
| * \param ikm_len The length in bytes of \p ikm. | ||
| * \param info An optional context and application specific information | ||
| * string. This can be a zero-length string. | ||
| * \param info_len The length of \p info in bytes. | ||
| * \param okm The output keying material of \p okm_len bytes. | ||
| * \param okm_len The length of the output keying material in bytes. This | ||
| * must be less than or equal to 255 * md.size bytes. | ||
| * | ||
| * \return 0 on success. | ||
| * \return #MBEDTLS_ERR_HKDF_BAD_INPUT_DATA when the parameters are invalid. | ||
| * \return An MBEDTLS_ERR_MD_* error for errors returned from the underlying | ||
| * MD layer. | ||
| */ | ||
| int mbedtls_hkdf( const mbedtls_md_info_t *md, const unsigned char *salt, | ||
| size_t salt_len, const unsigned char *ikm, size_t ikm_len, | ||
| const unsigned char *info, size_t info_len, | ||
| unsigned char *okm, size_t okm_len ); | ||
|
|
||
| /** | ||
| * \brief Take the input keying material \p ikm and extract from it a | ||
| * fixed-length pseudorandom key \p prk. | ||
| * | ||
| * \warning This function should only be used if the security of it has been | ||
| * studied and established in that particular context (eg. TLS 1.3 | ||
| * key schedule). For standard HKDF security guarantees use | ||
| * \c mbedtls_hkdf instead. | ||
| * | ||
| * \param md A hash function; md.size denotes the length of the | ||
| * hash function output in bytes. | ||
| * \param salt An optional salt value (a non-secret random value); | ||
| * if the salt is not provided, a string of all zeros | ||
| * of md.size length is used as the salt. | ||
| * \param salt_len The length in bytes of the optional \p salt. | ||
| * \param ikm The input keying material. | ||
| * \param ikm_len The length in bytes of \p ikm. | ||
| * \param[out] prk A pseudorandom key of at least md.size bytes. | ||
| * | ||
| * \return 0 on success. | ||
| * \return #MBEDTLS_ERR_HKDF_BAD_INPUT_DATA when the parameters are invalid. | ||
| * \return An MBEDTLS_ERR_MD_* error for errors returned from the underlying | ||
| * MD layer. | ||
| */ | ||
| int mbedtls_hkdf_extract( const mbedtls_md_info_t *md, | ||
| const unsigned char *salt, size_t salt_len, | ||
| const unsigned char *ikm, size_t ikm_len, | ||
| unsigned char *prk ); | ||
|
|
||
| /** | ||
| * \brief Expand the supplied \p prk into several additional pseudorandom | ||
| * keys, which is the output of the HKDF. | ||
| * | ||
| * \warning This function should only be used if the security of it has been | ||
| * studied and established in that particular context (eg. TLS 1.3 | ||
| * key schedule). For standard HKDF security guarantees use | ||
| * \c mbedtls_hkdf instead. | ||
| * | ||
| * \param md A hash function; md.size denotes the length of the hash | ||
| * function output in bytes. | ||
| * \param prk A pseudorandom key of at least md.size bytes. \p prk is | ||
| * usually the output from the HKDF extract step. | ||
| * \param prk_len The length in bytes of \p prk. | ||
| * \param info An optional context and application specific information | ||
| * string. This can be a zero-length string. | ||
| * \param info_len The length of \p info in bytes. | ||
| * \param okm The output keying material of \p okm_len bytes. | ||
| * \param okm_len The length of the output keying material in bytes. This | ||
| * must be less than or equal to 255 * md.size bytes. | ||
| * | ||
| * \return 0 on success. | ||
| * \return #MBEDTLS_ERR_HKDF_BAD_INPUT_DATA when the parameters are invalid. | ||
| * \return An MBEDTLS_ERR_MD_* error for errors returned from the underlying | ||
| * MD layer. | ||
| */ | ||
| int mbedtls_hkdf_expand( const mbedtls_md_info_t *md, const unsigned char *prk, | ||
| size_t prk_len, const unsigned char *info, | ||
| size_t info_len, unsigned char *okm, size_t okm_len ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* hkdf.h */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| /** | ||
| * \file nist_kw.h | ||
| * | ||
| * \brief This file provides an API for key wrapping (KW) and key wrapping with | ||
| * padding (KWP) as defined in NIST SP 800-38F. | ||
| * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38F.pdf | ||
| * | ||
| * Key wrapping specifies a deterministic authenticated-encryption mode | ||
| * of operation, according to <em>NIST SP 800-38F: Recommendation for | ||
| * Block Cipher Modes of Operation: Methods for Key Wrapping</em>. Its | ||
| * purpose is to protect cryptographic keys. | ||
| * | ||
| * Its equivalent is RFC 3394 for KW, and RFC 5649 for KWP. | ||
| * https://tools.ietf.org/html/rfc3394 | ||
| * https://tools.ietf.org/html/rfc5649 | ||
| * | ||
| */ | ||
| /* | ||
| * Copyright (C) 2018, Arm Limited (or its affiliates), All Rights Reserved | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * This file is part of Mbed TLS (https://tls.mbed.org) | ||
| */ | ||
|
|
||
| #ifndef MBEDTLS_NIST_KW_H | ||
| #define MBEDTLS_NIST_KW_H | ||
|
|
||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||
| #include "config.h" | ||
| #else | ||
| #include MBEDTLS_CONFIG_FILE | ||
| #endif | ||
|
|
||
| #include "cipher.h" | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| typedef enum | ||
| { | ||
| MBEDTLS_KW_MODE_KW = 0, | ||
| MBEDTLS_KW_MODE_KWP = 1 | ||
| } mbedtls_nist_kw_mode_t; | ||
|
|
||
| #if !defined(MBEDTLS_NIST_KW_ALT) | ||
| // Regular implementation | ||
| // | ||
|
|
||
| /** | ||
| * \brief The key wrapping context-type definition. The key wrapping context is passed | ||
| * to the APIs called. | ||
| * | ||
| * \note The definition of this type may change in future library versions. | ||
| * Don't make any assumptions on this context! | ||
| */ | ||
| typedef struct { | ||
| mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */ | ||
| } mbedtls_nist_kw_context; | ||
|
|
||
| #else /* MBEDTLS_NIST_key wrapping_ALT */ | ||
| #include "nist_kw_alt.h" | ||
| #endif /* MBEDTLS_NIST_KW_ALT */ | ||
|
|
||
| /** | ||
| * \brief This function initializes the specified key wrapping context | ||
| * to make references valid and prepare the context | ||
| * for mbedtls_nist_kw_setkey() or mbedtls_nist_kw_free(). | ||
| * | ||
| * \param ctx The key wrapping context to initialize. | ||
| * | ||
| */ | ||
| void mbedtls_nist_kw_init( mbedtls_nist_kw_context *ctx ); | ||
|
|
||
| /** | ||
| * \brief This function initializes the key wrapping context set in the | ||
| * \p ctx parameter and sets the encryption key. | ||
| * | ||
| * \param ctx The key wrapping context. | ||
| * \param cipher The 128-bit block cipher to use. Only AES is supported. | ||
| * \param key The Key Encryption Key (KEK). | ||
| * \param keybits The KEK size in bits. This must be acceptable by the cipher. | ||
| * \param is_wrap Specify whether the operation within the context is wrapping or unwrapping | ||
| * | ||
| * \return \c 0 on success. | ||
| * \return \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for any invalid input. | ||
| * \return \c MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE for 128-bit block ciphers | ||
| * which are not supported. | ||
| * \return cipher-specific error code on failure of the underlying cipher. | ||
| */ | ||
| int mbedtls_nist_kw_setkey( mbedtls_nist_kw_context *ctx, | ||
| mbedtls_cipher_id_t cipher, | ||
| const unsigned char *key, | ||
| unsigned int keybits, | ||
| const int is_wrap ); | ||
|
|
||
| /** | ||
| * \brief This function releases and clears the specified key wrapping context | ||
| * and underlying cipher sub-context. | ||
| * | ||
| * \param ctx The key wrapping context to clear. | ||
| */ | ||
| void mbedtls_nist_kw_free( mbedtls_nist_kw_context *ctx ); | ||
|
|
||
| /** | ||
| * \brief This function encrypts a buffer using key wrapping. | ||
| * | ||
| * \param ctx The key wrapping context to use for encryption. | ||
| * \param mode The key wrapping mode to use (MBEDTLS_KW_MODE_KW or MBEDTLS_KW_MODE_KWP) | ||
| * \param input The buffer holding the input data. | ||
| * \param in_len The length of the input data in Bytes. | ||
| * The input uses units of 8 Bytes called semiblocks. | ||
| * <ul><li>For KW mode: a multiple of 8 bytes between 16 and 2^57-8 inclusive. </li> | ||
| * <li>For KWP mode: any length between 1 and 2^32-1 inclusive.</li></ul> | ||
| * \param[out] output The buffer holding the output data. | ||
| * <ul><li>For KW mode: Must be at least 8 bytes larger than \p in_len.</li> | ||
| * <li>For KWP mode: Must be at least 8 bytes larger rounded up to a multiple of | ||
| * 8 bytes for KWP (15 bytes at most).</li></ul> | ||
| * \param[out] out_len The number of bytes written to the output buffer. \c 0 on failure. | ||
| * \param[in] out_size The capacity of the output buffer. | ||
| * | ||
| * \return \c 0 on success. | ||
| * \return \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for invalid input length. | ||
| * \return cipher-specific error code on failure of the underlying cipher. | ||
| */ | ||
| int mbedtls_nist_kw_wrap( mbedtls_nist_kw_context *ctx, mbedtls_nist_kw_mode_t mode, | ||
| const unsigned char *input, size_t in_len, | ||
| unsigned char *output, size_t* out_len, size_t out_size ); | ||
|
|
||
| /** | ||
| * \brief This function decrypts a buffer using key wrapping. | ||
| * | ||
| * \param ctx The key wrapping context to use for decryption. | ||
| * \param mode The key wrapping mode to use (MBEDTLS_KW_MODE_KW or MBEDTLS_KW_MODE_KWP) | ||
| * \param input The buffer holding the input data. | ||
| * \param in_len The length of the input data in Bytes. | ||
| * The input uses units of 8 Bytes called semiblocks. | ||
| * The input must be a multiple of semiblocks. | ||
| * <ul><li>For KW mode: a multiple of 8 bytes between 24 and 2^57 inclusive. </li> | ||
| * <li>For KWP mode: a multiple of 8 bytes between 16 and 2^32 inclusive.</li></ul> | ||
| * \param[out] output The buffer holding the output data. | ||
| * The output buffer's minimal length is 8 bytes shorter than \p in_len. | ||
| * \param[out] out_len The number of bytes written to the output buffer. \c 0 on failure. | ||
| * For KWP mode, the length could be up to 15 bytes shorter than \p in_len, | ||
| * depending on how much padding was added to the data. | ||
| * \param[in] out_size The capacity of the output buffer. | ||
| * | ||
| * \return \c 0 on success. | ||
| * \return \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for invalid input length. | ||
| * \return \c MBEDTLS_ERR_CIPHER_AUTH_FAILED for verification failure of the ciphertext. | ||
| * \return cipher-specific error code on failure of the underlying cipher. | ||
| */ | ||
| int mbedtls_nist_kw_unwrap( mbedtls_nist_kw_context *ctx, mbedtls_nist_kw_mode_t mode, | ||
| const unsigned char *input, size_t in_len, | ||
| unsigned char *output, size_t* out_len, size_t out_size); | ||
|
|
||
|
|
||
| #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) | ||
| /** | ||
| * \brief The key wrapping checkup routine. | ||
| * | ||
| * \return \c 0 on success. | ||
| * \return \c 1 on failure. | ||
| */ | ||
| int mbedtls_nist_kw_self_test( int verbose ); | ||
| #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* MBEDTLS_NIST_KW_H */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| /** | ||
| * \file platform_util.h | ||
| * | ||
| * \brief Common and shared functions used by multiple modules in the Mbed TLS | ||
| * library. | ||
| */ | ||
| /* | ||
| * Copyright (C) 2018, Arm Limited, All Rights Reserved | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * This file is part of Mbed TLS (https://tls.mbed.org) | ||
| */ | ||
| #ifndef MBEDTLS_PLATFORM_UTIL_H | ||
| #define MBEDTLS_PLATFORM_UTIL_H | ||
|
|
||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||
| #include "config.h" | ||
| #else | ||
| #include MBEDTLS_CONFIG_FILE | ||
| #endif | ||
|
|
||
| #include <stddef.h> | ||
| #if defined(MBEDTLS_HAVE_TIME_DATE) | ||
| #include "platform_time.h" | ||
| #include <time.h> | ||
| #endif /* MBEDTLS_HAVE_TIME_DATE */ | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| #if defined(MBEDTLS_CHECK_PARAMS) | ||
|
|
||
| #if defined(MBEDTLS_PARAM_FAILED) | ||
| /** An alternative definition of MBEDTLS_PARAM_FAILED has been set in config.h. | ||
| * | ||
| * This flag can be used to check whether it is safe to assume that | ||
| * MBEDTLS_PARAM_FAILED() will expand to a call to mbedtls_param_failed(). | ||
| */ | ||
| #define MBEDTLS_PARAM_FAILED_ALT | ||
| #else /* MBEDTLS_PARAM_FAILED */ | ||
| #define MBEDTLS_PARAM_FAILED( cond ) \ | ||
| mbedtls_param_failed( #cond, __FILE__, __LINE__ ) | ||
|
|
||
| /** | ||
| * \brief User supplied callback function for parameter validation failure. | ||
| * See #MBEDTLS_CHECK_PARAMS for context. | ||
| * | ||
| * This function will be called unless an alternative treatement | ||
| * is defined through the #MBEDTLS_PARAM_FAILED macro. | ||
| * | ||
| * This function can return, and the operation will be aborted, or | ||
| * alternatively, through use of setjmp()/longjmp() can resume | ||
| * execution in the application code. | ||
| * | ||
| * \param failure_condition The assertion that didn't hold. | ||
| * \param file The file where the assertion failed. | ||
| * \param line The line in the file where the assertion failed. | ||
| */ | ||
| void mbedtls_param_failed( const char *failure_condition, | ||
| const char *file, | ||
| int line ); | ||
| #endif /* MBEDTLS_PARAM_FAILED */ | ||
|
|
||
| /* Internal macro meant to be called only from within the library. */ | ||
| #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) \ | ||
| do { \ | ||
| if( !(cond) ) \ | ||
| { \ | ||
| MBEDTLS_PARAM_FAILED( cond ); \ | ||
| return( ret ); \ | ||
| } \ | ||
| } while( 0 ) | ||
|
|
||
| /* Internal macro meant to be called only from within the library. */ | ||
| #define MBEDTLS_INTERNAL_VALIDATE( cond ) \ | ||
| do { \ | ||
| if( !(cond) ) \ | ||
| { \ | ||
| MBEDTLS_PARAM_FAILED( cond ); \ | ||
| return; \ | ||
| } \ | ||
| } while( 0 ) | ||
|
|
||
| #else /* MBEDTLS_CHECK_PARAMS */ | ||
|
|
||
| /* Internal macros meant to be called only from within the library. */ | ||
| #define MBEDTLS_INTERNAL_VALIDATE_RET( cond, ret ) do { } while( 0 ) | ||
| #define MBEDTLS_INTERNAL_VALIDATE( cond ) do { } while( 0 ) | ||
|
|
||
| #endif /* MBEDTLS_CHECK_PARAMS */ | ||
|
|
||
| /* Internal helper macros for deprecating API constants. */ | ||
| #if !defined(MBEDTLS_DEPRECATED_REMOVED) | ||
| #if defined(MBEDTLS_DEPRECATED_WARNING) | ||
| /* Deliberately don't (yet) export MBEDTLS_DEPRECATED here | ||
| * to avoid conflict with other headers which define and use | ||
| * it, too. We might want to move all these definitions here at | ||
| * some point for uniformity. */ | ||
| #define MBEDTLS_DEPRECATED __attribute__((deprecated)) | ||
| MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_string_constant_t; | ||
| #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) \ | ||
| ( (mbedtls_deprecated_string_constant_t) ( VAL ) ) | ||
| MBEDTLS_DEPRECATED typedef int mbedtls_deprecated_numeric_constant_t; | ||
| #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) \ | ||
| ( (mbedtls_deprecated_numeric_constant_t) ( VAL ) ) | ||
| #undef MBEDTLS_DEPRECATED | ||
| #else /* MBEDTLS_DEPRECATED_WARNING */ | ||
| #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL | ||
| #define MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( VAL ) VAL | ||
| #endif /* MBEDTLS_DEPRECATED_WARNING */ | ||
| #endif /* MBEDTLS_DEPRECATED_REMOVED */ | ||
|
|
||
| /** | ||
| * \brief Securely zeroize a buffer | ||
| * | ||
| * The function is meant to wipe the data contained in a buffer so | ||
| * that it can no longer be recovered even if the program memory | ||
| * is later compromised. Call this function on sensitive data | ||
| * stored on the stack before returning from a function, and on | ||
| * sensitive data stored on the heap before freeing the heap | ||
| * object. | ||
| * | ||
| * It is extremely difficult to guarantee that calls to | ||
| * mbedtls_platform_zeroize() are not removed by aggressive | ||
| * compiler optimizations in a portable way. For this reason, Mbed | ||
| * TLS provides the configuration option | ||
| * MBEDTLS_PLATFORM_ZEROIZE_ALT, which allows users to configure | ||
| * mbedtls_platform_zeroize() to use a suitable implementation for | ||
| * their platform and needs | ||
| * | ||
| * \param buf Buffer to be zeroized | ||
| * \param len Length of the buffer in bytes | ||
| * | ||
| */ | ||
| void mbedtls_platform_zeroize( void *buf, size_t len ); | ||
|
|
||
| #if defined(MBEDTLS_HAVE_TIME_DATE) | ||
| /** | ||
| * \brief Platform-specific implementation of gmtime_r() | ||
| * | ||
| * The function is a thread-safe abstraction that behaves | ||
| * similarly to the gmtime_r() function from Unix/POSIX. | ||
| * | ||
| * Mbed TLS will try to identify the underlying platform and | ||
| * make use of an appropriate underlying implementation (e.g. | ||
| * gmtime_r() for POSIX and gmtime_s() for Windows). If this is | ||
| * not possible, then gmtime() will be used. In this case, calls | ||
| * from the library to gmtime() will be guarded by the mutex | ||
| * mbedtls_threading_gmtime_mutex if MBEDTLS_THREADING_C is | ||
| * enabled. It is recommended that calls from outside the library | ||
| * are also guarded by this mutex. | ||
| * | ||
| * If MBEDTLS_PLATFORM_GMTIME_R_ALT is defined, then Mbed TLS will | ||
| * unconditionally use the alternative implementation for | ||
| * mbedtls_platform_gmtime_r() supplied by the user at compile time. | ||
| * | ||
| * \param tt Pointer to an object containing time (in seconds) since the | ||
| * epoch to be converted | ||
| * \param tm_buf Pointer to an object where the results will be stored | ||
| * | ||
| * \return Pointer to an object of type struct tm on success, otherwise | ||
| * NULL | ||
| */ | ||
| struct tm *mbedtls_platform_gmtime_r( const mbedtls_time_t *tt, | ||
| struct tm *tm_buf ); | ||
| #endif /* MBEDTLS_HAVE_TIME_DATE */ | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* MBEDTLS_PLATFORM_UTIL_H */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| /** | ||
| * \file poly1305.h | ||
| * | ||
| * \brief This file contains Poly1305 definitions and functions. | ||
| * | ||
| * Poly1305 is a one-time message authenticator that can be used to | ||
| * authenticate messages. Poly1305-AES was created by Daniel | ||
| * Bernstein https://cr.yp.to/mac/poly1305-20050329.pdf The generic | ||
| * Poly1305 algorithm (not tied to AES) was also standardized in RFC | ||
| * 7539. | ||
| * | ||
| * \author Daniel King <damaki.gh@gmail.com> | ||
| */ | ||
|
|
||
| /* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * This file is part of Mbed TLS (https://tls.mbed.org) | ||
| */ | ||
|
|
||
| #ifndef MBEDTLS_POLY1305_H | ||
| #define MBEDTLS_POLY1305_H | ||
|
|
||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||
| #include "config.h" | ||
| #else | ||
| #include MBEDTLS_CONFIG_FILE | ||
| #endif | ||
|
|
||
| #include <stdint.h> | ||
| #include <stddef.h> | ||
|
|
||
| #define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA -0x0057 /**< Invalid input parameter(s). */ | ||
|
|
||
| /* MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE is deprecated and should not be | ||
| * used. */ | ||
| #define MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE -0x0059 /**< Feature not available. For example, s part of the API is not implemented. */ | ||
|
|
||
| /* MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED is deprecated and should not be used. | ||
| */ | ||
| #define MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED -0x005B /**< Poly1305 hardware accelerator failed. */ | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| #if !defined(MBEDTLS_POLY1305_ALT) | ||
|
|
||
| typedef struct mbedtls_poly1305_context | ||
| { | ||
| uint32_t r[4]; /** The value for 'r' (low 128 bits of the key). */ | ||
| uint32_t s[4]; /** The value for 's' (high 128 bits of the key). */ | ||
| uint32_t acc[5]; /** The accumulator number. */ | ||
| uint8_t queue[16]; /** The current partial block of data. */ | ||
| size_t queue_len; /** The number of bytes stored in 'queue'. */ | ||
| } | ||
| mbedtls_poly1305_context; | ||
|
|
||
| #else /* MBEDTLS_POLY1305_ALT */ | ||
| #include "poly1305_alt.h" | ||
| #endif /* MBEDTLS_POLY1305_ALT */ | ||
|
|
||
| /** | ||
| * \brief This function initializes the specified Poly1305 context. | ||
| * | ||
| * It must be the first API called before using | ||
| * the context. | ||
| * | ||
| * It is usually followed by a call to | ||
| * \c mbedtls_poly1305_starts(), then one or more calls to | ||
| * \c mbedtls_poly1305_update(), then one call to | ||
| * \c mbedtls_poly1305_finish(), then finally | ||
| * \c mbedtls_poly1305_free(). | ||
| * | ||
| * \param ctx The Poly1305 context to initialize. This must | ||
| * not be \c NULL. | ||
| */ | ||
| void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx ); | ||
|
|
||
| /** | ||
| * \brief This function releases and clears the specified | ||
| * Poly1305 context. | ||
| * | ||
| * \param ctx The Poly1305 context to clear. This may be \c NULL, in which | ||
| * case this function is a no-op. If it is not \c NULL, it must | ||
| * point to an initialized Poly1305 context. | ||
| */ | ||
| void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx ); | ||
|
|
||
| /** | ||
| * \brief This function sets the one-time authentication key. | ||
| * | ||
| * \warning The key must be unique and unpredictable for each | ||
| * invocation of Poly1305. | ||
| * | ||
| * \param ctx The Poly1305 context to which the key should be bound. | ||
| * This must be initialized. | ||
| * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key. | ||
| * | ||
| * \return \c 0 on success. | ||
| * \return A negative error code on failure. | ||
| */ | ||
| int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx, | ||
| const unsigned char key[32] ); | ||
|
|
||
| /** | ||
| * \brief This functions feeds an input buffer into an ongoing | ||
| * Poly1305 computation. | ||
| * | ||
| * It is called between \c mbedtls_cipher_poly1305_starts() and | ||
| * \c mbedtls_cipher_poly1305_finish(). | ||
| * It can be called repeatedly to process a stream of data. | ||
| * | ||
| * \param ctx The Poly1305 context to use for the Poly1305 operation. | ||
| * This must be initialized and bound to a key. | ||
| * \param ilen The length of the input data in Bytes. | ||
| * Any value is accepted. | ||
| * \param input The buffer holding the input data. | ||
| * This pointer can be \c NULL if `ilen == 0`. | ||
| * | ||
| * \return \c 0 on success. | ||
| * \return A negative error code on failure. | ||
| */ | ||
| int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx, | ||
| const unsigned char *input, | ||
| size_t ilen ); | ||
|
|
||
| /** | ||
| * \brief This function generates the Poly1305 Message | ||
| * Authentication Code (MAC). | ||
| * | ||
| * \param ctx The Poly1305 context to use for the Poly1305 operation. | ||
| * This must be initialized and bound to a key. | ||
| * \param mac The buffer to where the MAC is written. This must | ||
| * be a writable buffer of length \c 16 Bytes. | ||
| * | ||
| * \return \c 0 on success. | ||
| * \return A negative error code on failure. | ||
| */ | ||
| int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx, | ||
| unsigned char mac[16] ); | ||
|
|
||
| /** | ||
| * \brief This function calculates the Poly1305 MAC of the input | ||
| * buffer with the provided key. | ||
| * | ||
| * \warning The key must be unique and unpredictable for each | ||
| * invocation of Poly1305. | ||
| * | ||
| * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key. | ||
| * \param ilen The length of the input data in Bytes. | ||
| * Any value is accepted. | ||
| * \param input The buffer holding the input data. | ||
| * This pointer can be \c NULL if `ilen == 0`. | ||
| * \param mac The buffer to where the MAC is written. This must be | ||
| * a writable buffer of length \c 16 Bytes. | ||
| * | ||
| * \return \c 0 on success. | ||
| * \return A negative error code on failure. | ||
| */ | ||
| int mbedtls_poly1305_mac( const unsigned char key[32], | ||
| const unsigned char *input, | ||
| size_t ilen, | ||
| unsigned char mac[16] ); | ||
|
|
||
| #if defined(MBEDTLS_SELF_TEST) | ||
| /** | ||
| * \brief The Poly1305 checkup routine. | ||
| * | ||
| * \return \c 0 on success. | ||
| * \return \c 1 on failure. | ||
| */ | ||
| int mbedtls_poly1305_self_test( int verbose ); | ||
| #endif /* MBEDTLS_SELF_TEST */ | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* MBEDTLS_POLY1305_H */ |