mirror of
				https://github.com/yuzu-emu/mbedtls.git
				synced 2025-10-22 08:27:09 +00:00 
			
		
		
		
	Merge branch 'development-psa-proposed' into development
Resolve conflicts by performing the following.
- Take the upstream Mbed TLS ChangeLog verbatim.
- Reject changes to Makefiles and CMake that are related to using Mbed
  Crypto as a submodule. It doesn't make sense to use Mbed Crypto as a
  submodule of itself.
- Reject README changes, as Mbed Crypto has its own, different README.
- Reject PSA-related changes to config.h. We don't want to disable the
  availability of the PSA Crypto API by default in the Mbed Crypto
  config.h.
- Don't inadvertently revert dead code removal in
  mbedtls_cipher_write_tag() which was added in f2a7529403 ("Fix
  double return statement in cipher.c")
- Where Mbed Crypto already had some MBEDTLS_USE_PSA_CRYPTO code (from
  past companion PRs) take the latest version from Mbed TLS which
  includes integration with MBEDTLS_CHECK_PARAMS.
- Update the version of the shared library files to match what's
  currently present in Mbed TLS.
- Reject removal of testing with PSA from config full tests.
- Resolve conflicts in test tests/suites/helpers.function, where both
  Mbed Crypto and Mbed TLS both added documentation for TEST_ASSERT.
  Combine text from both documentation efforts.
- Reject adding a submodule of ourselves.
- Reject addition of submodule tests in all.sh.
- Reject addition of submodule to library path in
  tests/scripts/run-test-suites.pl.
- Avoid using USE_CRYPTO_SUBMODULE=1 in
  component_test_use_psa_crypto_full_cmake_asan() in all.sh.
			
			
This commit is contained in:
		
						commit
						67ea2c5e6d
					
				|  | @ -50,15 +50,15 @@ When backporting to these branches please observe the following rules: | |||
| 
 | ||||
|  2. All bug fixes that correct a defect that is also present in an LTS branch must be backported to that LTS branch. If a bug fix introduces a change to the API such as a new function, the fix should be reworked to avoid the API change. API changes without very strong justification are unlikely to be accepted. | ||||
| 
 | ||||
|  3. If a contribution is a new feature or enhancement, no backporting is required. Exceptions to this may be addtional test cases or quality improvements such as changes to build or test scripts. | ||||
|  3. If a contribution is a new feature or enhancement, no backporting is required. Exceptions to this may be additional test cases or quality improvements such as changes to build or test scripts. | ||||
| 
 | ||||
| It would be highly appreciated if contributions are backported to LTS branches in addition to the [development branch](https://github.com/ARMmbed/mbedtls/tree/development) by contributors. | ||||
| 
 | ||||
| Currently maintained LTS branches are: | ||||
| 
 | ||||
| 1. [mbedtls-2.1](https://github.com/ARMmbed/mbedtls/tree/mbedtls-2.1) | ||||
| 1. [mbedtls-2.7](https://github.com/ARMmbed/mbedtls/tree/mbedtls-2.7) | ||||
| 
 | ||||
| 2. [mbedtls-2.7](https://github.com/ARMmbed/mbedtls/tree/mbedtls-2.7) | ||||
| 1. [mbedtls-2.16](https://github.com/ARMmbed/mbedtls/tree/mbedtls-2.16) | ||||
| 
 | ||||
| 
 | ||||
| Tests | ||||
|  |  | |||
							
								
								
									
										156
									
								
								ChangeLog
									
									
									
									
									
								
							
							
						
						
									
										156
									
								
								ChangeLog
									
									
									
									
									
								
							|  | @ -1,12 +1,117 @@ | |||
| mbed TLS ChangeLog (Sorted per branch, date) | ||||
| 
 | ||||
| = mbed TLS 2.xx.x branch released xxxx-xx-xx | ||||
| = mbed TLS 2.x.x branch released xxxx-xx-xx | ||||
| 
 | ||||
| Bugfix | ||||
|    * Fix a compilation issue with mbedtls_ecp_restart_ctx not being defined | ||||
|      when MBEDTLS_ECP_ALT is defined. Reported by jwhui. Fixes #2242. | ||||
|    * Run the AD too long test only if MBEDTLS_CCM_ALT is not defined. | ||||
|      Raised as a comment in #1996. | ||||
|    * Reduce the stack consumption of mbedtls_mpi_fill_random() which could | ||||
|      previously lead to a stack overflow on constrained targets. | ||||
|    * Add `MBEDTLS_SELF_TEST` for the mbedtls_self_test functions | ||||
|      in the header files, which missed the precompilation check. #971 | ||||
|    * Fix returning the value 1 when mbedtls_ecdsa_genkey failed. | ||||
|    * Remove a duplicate #include in a sample program. Fixed by Masashi Honma #2326. | ||||
|    * Remove the mbedtls namespacing from the header file, to fix a "file not found" | ||||
|      build error. Fixed by Haijun Gu #2319. | ||||
|    * Fix signed-to-unsigned integer conversion warning | ||||
|      in X.509 module. Fixes #2212. | ||||
|    * Reduce stack usage of `mpi_write_hlp()` by eliminating recursion. | ||||
|      Fixes #2190. | ||||
| 
 | ||||
| Changes | ||||
|    * Add unit tests for AES-GCM when called through mbedtls_cipher_auth_xxx() | ||||
|      from the cipher abstraction layer. Fixes #2198. | ||||
|    * Include configuration file in all header files that use configuration, | ||||
|      instead of relying on other header files that they include. | ||||
|      Inserted as an enhancement for #1371 | ||||
|    * Add support for alternative CSR headers, as used by Microsoft and defined | ||||
|      in RFC 7468. Found by Michael Ernst. Fixes #767. | ||||
|    * Correct many misspellings. Fixed by MisterDA #2371. | ||||
|    * Provide an abstraction of vsnprintf to allow alternative implementations | ||||
|      for platforms that don't provide it. Based on contributions by Joris Aerts | ||||
|      and Nathaniel Wesley Filardo. | ||||
|    * Fix clobber list in MIPS assembly for large integer multiplication. | ||||
|      Previously, this could lead to functionally incorrect assembly being | ||||
|      produced by some optimizing compilers, showing up as failures in | ||||
|      e.g. RSA or ECC signature operations. Reported in #1722, fix suggested | ||||
|      by Aurelien Jarno and submitted by Jeffrey Martin. | ||||
| 
 | ||||
| = mbed TLS 2.14.0+01b34fb316a5 branch released xxxx-xx-xx | ||||
| = mbed TLS 2.16.0 branch released 2018-12-21 | ||||
| 
 | ||||
| Features | ||||
|    * Add a new config.h option of MBEDTLS_CHECK_PARAMS that enables validation | ||||
|      of parameters in the API. This allows detection of obvious misuses of the | ||||
|      API, such as passing NULL pointers. The API of existing functions hasn't | ||||
|      changed, but requirements on parameters have been made more explicit in | ||||
|      the documentation. See the corresponding API documentation for each | ||||
|      function to see for which parameter values it is defined. This feature is | ||||
|      disabled by default. See its API documentation in config.h for additional | ||||
|      steps you have to take when enabling it. | ||||
| 
 | ||||
| API Changes | ||||
|    * The following functions in the random generator modules have been | ||||
|      deprecated and replaced as shown below. The new functions change | ||||
|      the return type from void to int to allow returning error codes when | ||||
|      using MBEDTLS_<MODULE>_ALT for the underlying AES or message digest | ||||
|      primitive. Fixes #1798. | ||||
|      mbedtls_ctr_drbg_update() -> mbedtls_ctr_drbg_update_ret() | ||||
|      mbedtls_hmac_drbg_update() -> mbedtls_hmac_drbg_update_ret() | ||||
|    * Extend ECDH interface to enable alternative implementations. | ||||
|    * Deprecate error codes of the form MBEDTLS_ERR_xxx_INVALID_KEY_LENGTH for | ||||
|      ARIA, CAMELLIA and Blowfish. These error codes will be replaced by | ||||
|      the more generic per-module error codes MBEDTLS_ERR_xxx_BAD_INPUT_DATA. | ||||
|    * Additional parameter validation checks have been added for the following | ||||
|      modules - AES, ARIA, Blowfish, CAMELLIA, CCM, GCM, DHM, ECP, ECDSA, ECDH, | ||||
|      ECJPAKE, SHA, Chacha20 and Poly1305, cipher, pk, RSA, and MPI. | ||||
|      Where modules have had parameter validation added, existing parameter | ||||
|      checks may have changed. Some modules, such as Chacha20 had existing | ||||
|      parameter validation whereas other modules had little. This has now been | ||||
|      changed so that the same level of validation is present in all modules, and | ||||
|      that it is now optional with the MBEDTLS_CHECK_PARAMS flag which by default | ||||
|      is off. That means that checks which were previously present by default | ||||
|      will no longer be. | ||||
| 
 | ||||
| New deprecations | ||||
|    * Deprecate mbedtls_ctr_drbg_update and mbedtls_hmac_drbg_update | ||||
|      in favor of functions that can return an error code. | ||||
| 
 | ||||
| Bugfix | ||||
|    * Fix for Clang, which was reporting a warning for the bignum.c inline | ||||
|      assembly for AMD64 targets creating string literals greater than those | ||||
|      permitted by the ISO C99 standard. Found by Aaron Jones. Fixes #482. | ||||
|    * Fix runtime error in `mbedtls_platform_entropy_poll()` when run | ||||
|      through qemu user emulation. Reported and fix suggested by randombit | ||||
|      in #1212. Fixes #1212. | ||||
|    * Fix an unsafe bounds check when restoring an SSL session from a ticket. | ||||
|      This could lead to a buffer overflow, but only in case ticket authentication | ||||
|      was broken. Reported and fix suggested by Guido Vranken in #659. | ||||
|    * Add explicit integer to enumeration type casts to example program | ||||
|      programs/pkey/gen_key which previously led to compilation failure | ||||
|      on some toolchains. Reported by phoenixmcallister. Fixes #2170. | ||||
|    * Fix double initialization of ECC hardware that made some accelerators | ||||
|      hang. | ||||
|    * Clarify documentation of mbedtls_ssl_set_own_cert() regarding the absence | ||||
|      of check for certificate/key matching. Reported by Attila Molnar, #507. | ||||
| 
 | ||||
|  = mbed TLS 2.15.1 branch released 2018-11-30 | ||||
| 
 | ||||
|  Changes | ||||
|     * Update the Mbed Crypto submodule to version 0.1.0b2. | ||||
| 
 | ||||
|  = mbed TLS 2.15.0 branch released 2018-11-23 | ||||
| 
 | ||||
|  Features | ||||
|     * Add an experimental build option, USE_CRYPTO_SUBMODULE, to enable use of | ||||
|       Mbed Crypto as the source of the cryptography implementation. | ||||
|     * Add an experimental configuration option, MBEDTLS_PSA_CRYPTO_C, to enable | ||||
|       the PSA Crypto API from Mbed Crypto when additionally used with the | ||||
|       USE_CRYPTO_SUBMODULE build option. | ||||
| 
 | ||||
|  Changes | ||||
|     * Add unit tests for AES-GCM when called through mbedtls_cipher_auth_xxx() | ||||
|       from the cipher abstraction layer. Fixes #2198. | ||||
| 
 | ||||
| = mbed TLS 2.14.1 branch released 2018-11-30 | ||||
| 
 | ||||
| Security | ||||
|    * Fix timing variations and memory access variations in RSA PKCS#1 v1.5 | ||||
|  | @ -29,34 +134,11 @@ Security | |||
|      modules. | ||||
| 
 | ||||
| API Changes | ||||
|    * The following functions in the random generator modules have been | ||||
|      deprecated and replaced as shown below. The new functions change | ||||
|      the return type from void to int to allow returning error codes when | ||||
|      using MBEDTLS_<MODULE>_ALT for the underlying AES or message digest | ||||
|      primitive. Fixes #1798. | ||||
|      mbedtls_ctr_drbg_update() -> mbedtls_ctr_drbg_update_ret() | ||||
|      mbedtls_hmac_drbg_update() -> mbedtls_hmac_drbg_update_ret() | ||||
|    * Extend ECDH interface to enable alternative implementations. | ||||
| 
 | ||||
| New deprecations | ||||
|    * Deprecate mbedtls_ctr_drbg_update and mbedtls_hmac_drbg_update | ||||
|      in favor of functions that can return an error code. | ||||
| 
 | ||||
| Bugfix | ||||
|    * Fix for Clang, which was reporting a warning for the bignum.c inline | ||||
|      assembly for AMD64 targets creating string literals greater than those | ||||
|      permitted by the ISO C99 standard. Found by Aaron Jones. Fixes #482. | ||||
|    * Fix runtime error in `mbedtls_platform_entropy_poll()` when run | ||||
|      through qemu user emulation. Reported and fix suggested by randombit | ||||
|      in #1212. Fixes #1212. | ||||
|    * Fix an unsafe bounds check when restoring an SSL session from a ticket. | ||||
|      This could lead to a buffer overflow, but only in case ticket authentication | ||||
|      was broken. Reported and fix suggested by Guido Vranken in #659. | ||||
|    * Add explicit integer to enumeration type casts to example program | ||||
|      programs/pkey/gen_key which previously led to compilation failure | ||||
|      on some toolchains. Reported by phoenixmcallister. Fixes #2170. | ||||
|    * Fix double initialization of ECC hardware that made some accelerators | ||||
|      hang. | ||||
|    * The new functions mbedtls_ctr_drbg_update_ret() and | ||||
|      mbedtls_hmac_drbg_update_ret() are similar to mbedtls_ctr_drbg_update() | ||||
|      and mbedtls_hmac_drbg_update() respectively, but the new functions | ||||
|      report errors whereas the old functions return void. We recommend that | ||||
|      applications use the new functions. | ||||
| 
 | ||||
| = mbed TLS 2.14.0 branch released 2018-11-19 | ||||
| 
 | ||||
|  | @ -281,7 +363,7 @@ Security | |||
|      1.2, that allowed a local attacker, able to execute code on the local | ||||
|      machine as well as manipulate network packets, to partially recover the | ||||
|      plaintext of messages under some conditions by using a cache attack | ||||
|      targetting an internal MD/SHA buffer. With TLS or if | ||||
|      targeting an internal MD/SHA buffer. With TLS or if | ||||
|      mbedtls_ssl_conf_dtls_badmac_limit() was used, the attack only worked if | ||||
|      the same secret (for example a HTTP Cookie) has been repeatedly sent over | ||||
|      connections manipulated by the attacker. Connections using GCM or CCM | ||||
|  | @ -1167,7 +1249,7 @@ Bugfix | |||
|    * Fix potential build failures related to the 'apidoc' target, introduced | ||||
|      in the previous patch release. Found by Robert Scheck. #390 #391 | ||||
|    * Fix issue in Makefile that prevented building using armar. #386 | ||||
|    * Fix memory leak that occured only when ECJPAKE was enabled and ECDHE and | ||||
|    * Fix memory leak that occurred only when ECJPAKE was enabled and ECDHE and | ||||
|      ECDSA was disabled in config.h . The leak didn't occur by default. | ||||
|    * Fix an issue that caused valid certificates to be rejected whenever an | ||||
|      expired or not yet valid certificate was parsed before a valid certificate | ||||
|  | @ -1409,7 +1491,7 @@ API Changes | |||
|      You now need to link to all of them if you use TLS for example. | ||||
|    * All public identifiers moved to the mbedtls_* or MBEDTLS_* namespace. | ||||
|      Some names have been further changed to make them more consistent. | ||||
|      Migration helpers scripts/rename.pl and include/mbedlts/compat-1.3.h are | ||||
|      Migration helpers scripts/rename.pl and include/mbedtls/compat-1.3.h are | ||||
|      provided. Full list of renamings in scripts/data_files/rename-1.3-2.0.txt | ||||
|    * Renamings of fields inside structures, not covered by the previous list: | ||||
|      mbedtls_cipher_info_t.key_length -> key_bitlen | ||||
|  | @ -1464,7 +1546,7 @@ API Changes | |||
|    * net_accept() gained new arguments for the size of the client_ip buffer. | ||||
|    * In the threading layer, mbedtls_mutex_init() and mbedtls_mutex_free() now | ||||
|      return void. | ||||
|    * ecdsa_write_signature() gained an addtional md_alg argument and | ||||
|    * ecdsa_write_signature() gained an additional md_alg argument and | ||||
|      ecdsa_write_signature_det() was deprecated. | ||||
|    * pk_sign() no longer accepts md_alg == POLARSSL_MD_NONE with ECDSA. | ||||
|    * Last argument of x509_crt_check_key_usage() and | ||||
|  | @ -2999,7 +3081,7 @@ XySSL ChangeLog | |||
|       not swapped on PadLock; also fixed compilation on older versions | ||||
|       of gcc (bug reported by David Barrett) | ||||
|     * Correctly handle the case in padlock_xcryptcbc() when input or | ||||
|       ouput data is non-aligned by falling back to the software | ||||
|       output data is non-aligned by falling back to the software | ||||
|       implementation, as VIA Nehemiah cannot handle non-aligned buffers | ||||
|     * Fixed a memory leak in x509parse_crt() which was reported by Greg | ||||
|       Robson-Garth; some x509write.c fixes by Pascal Vizeli, thanks to | ||||
|  |  | |||
|  | @ -24,7 +24,7 @@ | |||
|  */ | ||||
| 
 | ||||
| /**
 | ||||
|  * @mainpage mbed TLS v2.14.0 source code documentation | ||||
|  * @mainpage mbed TLS v2.16.0 source code documentation | ||||
|  * | ||||
|  * This documentation describes the internal structure of mbed TLS.  It was | ||||
|  * automatically generated from specially formatted comment blocks in | ||||
|  |  | |||
|  | @ -28,7 +28,7 @@ DOXYFILE_ENCODING      = UTF-8 | |||
| # identify the project. Note that if you do not use Doxywizard you need | ||||
| # to put quotes around the project name if it contains spaces. | ||||
| 
 | ||||
| PROJECT_NAME           = "mbed TLS v2.14.0" | ||||
| PROJECT_NAME           = "mbed TLS v2.16.0" | ||||
| 
 | ||||
| # The PROJECT_NUMBER tag can be used to enter a project or revision number. | ||||
| # This could be handy for archiving the generated documentation or | ||||
|  |  | |||
|  | @ -121,7 +121,7 @@ typedef struct mbedtls_aes_xts_context | |||
|  *                 It must be the first API called before using | ||||
|  *                 the context. | ||||
|  * | ||||
|  * \param ctx      The AES context to initialize. | ||||
|  * \param ctx      The AES context to initialize. This must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_aes_init( mbedtls_aes_context *ctx ); | ||||
| 
 | ||||
|  | @ -129,6 +129,8 @@ void mbedtls_aes_init( mbedtls_aes_context *ctx ); | |||
|  * \brief          This function releases and clears the specified AES context. | ||||
|  * | ||||
|  * \param ctx      The AES context to clear. | ||||
|  *                 If this is \c NULL, this function does nothing. | ||||
|  *                 Otherwise, the context must have been at least initialized. | ||||
|  */ | ||||
| void mbedtls_aes_free( mbedtls_aes_context *ctx ); | ||||
| 
 | ||||
|  | @ -139,7 +141,7 @@ void mbedtls_aes_free( mbedtls_aes_context *ctx ); | |||
|  *                 It must be the first API called before using | ||||
|  *                 the context. | ||||
|  * | ||||
|  * \param ctx      The AES XTS context to initialize. | ||||
|  * \param ctx      The AES XTS context to initialize. This must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx ); | ||||
| 
 | ||||
|  | @ -147,6 +149,8 @@ void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx ); | |||
|  * \brief          This function releases and clears the specified AES XTS context. | ||||
|  * | ||||
|  * \param ctx      The AES XTS context to clear. | ||||
|  *                 If this is \c NULL, this function does nothing. | ||||
|  *                 Otherwise, the context must have been at least initialized. | ||||
|  */ | ||||
| void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx ); | ||||
| #endif /* MBEDTLS_CIPHER_MODE_XTS */ | ||||
|  | @ -155,7 +159,9 @@ void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx ); | |||
|  * \brief          This function sets the encryption key. | ||||
|  * | ||||
|  * \param ctx      The AES context to which the key should be bound. | ||||
|  *                 It must be initialized. | ||||
|  * \param key      The encryption key. | ||||
|  *                 This must be a readable buffer of size \p keybits bits. | ||||
|  * \param keybits  The size of data passed in bits. Valid options are: | ||||
|  *                 <ul><li>128 bits</li> | ||||
|  *                 <li>192 bits</li> | ||||
|  | @ -171,7 +177,9 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, | |||
|  * \brief          This function sets the decryption key. | ||||
|  * | ||||
|  * \param ctx      The AES context to which the key should be bound. | ||||
|  *                 It must be initialized. | ||||
|  * \param key      The decryption key. | ||||
|  *                 This must be a readable buffer of size \p keybits bits. | ||||
|  * \param keybits  The size of data passed. Valid options are: | ||||
|  *                 <ul><li>128 bits</li> | ||||
|  *                 <li>192 bits</li> | ||||
|  | @ -189,8 +197,10 @@ int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key, | |||
|  *                 sets the encryption key. | ||||
|  * | ||||
|  * \param ctx      The AES XTS context to which the key should be bound. | ||||
|  *                 It must be initialized. | ||||
|  * \param key      The encryption key. This is comprised of the XTS key1 | ||||
|  *                 concatenated with the XTS key2. | ||||
|  *                 This must be a readable buffer of size \p keybits bits. | ||||
|  * \param keybits  The size of \p key passed in bits. Valid options are: | ||||
|  *                 <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li> | ||||
|  *                 <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul> | ||||
|  | @ -207,8 +217,10 @@ int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx, | |||
|  *                 sets the decryption key. | ||||
|  * | ||||
|  * \param ctx      The AES XTS context to which the key should be bound. | ||||
|  *                 It must be initialized. | ||||
|  * \param key      The decryption key. This is comprised of the XTS key1 | ||||
|  *                 concatenated with the XTS key2. | ||||
|  *                 This must be a readable buffer of size \p keybits bits. | ||||
|  * \param keybits  The size of \p key passed in bits. Valid options are: | ||||
|  *                 <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li> | ||||
|  *                 <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul> | ||||
|  | @ -234,10 +246,13 @@ int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx, | |||
|  *                 call to this API with the same context. | ||||
|  * | ||||
|  * \param ctx      The AES context to use for encryption or decryption. | ||||
|  *                 It must be initialized and bound to a key. | ||||
|  * \param mode     The AES operation: #MBEDTLS_AES_ENCRYPT or | ||||
|  *                 #MBEDTLS_AES_DECRYPT. | ||||
|  * \param input    The 16-Byte buffer holding the input data. | ||||
|  * \param output   The 16-Byte buffer holding the output data. | ||||
|  * \param input    The buffer holding the input data. | ||||
|  *                 It must be readable and at least \c 16 Bytes long. | ||||
|  * \param output   The buffer where the output data will be written. | ||||
|  *                 It must be writeable and at least \c 16 Bytes long. | ||||
| 
 | ||||
|  * \return         \c 0 on success. | ||||
|  */ | ||||
|  | @ -260,8 +275,8 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx, | |||
|  *         mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called | ||||
|  *         before the first call to this API with the same context. | ||||
|  * | ||||
|  * \note   This function operates on aligned blocks, that is, the input size | ||||
|  *         must be a multiple of the AES block size of 16 Bytes. | ||||
|  * \note   This function operates on full blocks, that is, the input size | ||||
|  *         must be a multiple of the AES block size of \c 16 Bytes. | ||||
|  * | ||||
|  * \note   Upon exit, the content of the IV is updated so that you can | ||||
|  *         call the same function again on the next | ||||
|  | @ -272,13 +287,17 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx, | |||
|  * | ||||
|  * | ||||
|  * \param ctx      The AES context to use for encryption or decryption. | ||||
|  *                 It must be initialized and bound to a key. | ||||
|  * \param mode     The AES operation: #MBEDTLS_AES_ENCRYPT or | ||||
|  *                 #MBEDTLS_AES_DECRYPT. | ||||
|  * \param length   The length of the input data in Bytes. This must be a | ||||
|  *                 multiple of the block size (16 Bytes). | ||||
|  *                 multiple of the block size (\c 16 Bytes). | ||||
|  * \param iv       Initialization vector (updated after use). | ||||
|  *                 It must be a readable and writeable buffer of \c 16 Bytes. | ||||
|  * \param input    The buffer holding the input data. | ||||
|  *                 It must be readable and of size \p length Bytes. | ||||
|  * \param output   The buffer holding the output data. | ||||
|  *                 It must be writeable and of size \p length Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH | ||||
|  | @ -306,9 +325,10 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, | |||
|  *             returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH. | ||||
|  * | ||||
|  * \param ctx          The AES XTS context to use for AES XTS operations. | ||||
|  *                     It must be initialized and bound to a key. | ||||
|  * \param mode         The AES operation: #MBEDTLS_AES_ENCRYPT or | ||||
|  *                     #MBEDTLS_AES_DECRYPT. | ||||
|  * \param length       The length of a data unit in bytes. This can be any | ||||
|  * \param length       The length of a data unit in Bytes. This can be any | ||||
|  *                     length between 16 bytes and 2^24 bytes inclusive | ||||
|  *                     (between 1 and 2^20 block cipher blocks). | ||||
|  * \param data_unit    The address of the data unit encoded as an array of 16 | ||||
|  | @ -316,15 +336,15 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, | |||
|  *                     is typically the index of the block device sector that | ||||
|  *                     contains the data. | ||||
|  * \param input        The buffer holding the input data (which is an entire | ||||
|  *                     data unit). This function reads \p length bytes from \p | ||||
|  *                     data unit). This function reads \p length Bytes from \p | ||||
|  *                     input. | ||||
|  * \param output       The buffer holding the output data (which is an entire | ||||
|  *                     data unit). This function writes \p length bytes to \p | ||||
|  *                     data unit). This function writes \p length Bytes to \p | ||||
|  *                     output. | ||||
|  * | ||||
|  * \return             \c 0 on success. | ||||
|  * \return             #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is | ||||
|  *                     smaller than an AES block in size (16 bytes) or if \p | ||||
|  *                     smaller than an AES block in size (16 Bytes) or if \p | ||||
|  *                     length is larger than 2^20 blocks (16 MiB). | ||||
|  */ | ||||
| int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx, | ||||
|  | @ -360,13 +380,18 @@ int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx, | |||
|  * | ||||
|  * | ||||
|  * \param ctx      The AES context to use for encryption or decryption. | ||||
|  *                 It must be initialized and bound to a key. | ||||
|  * \param mode     The AES operation: #MBEDTLS_AES_ENCRYPT or | ||||
|  *                 #MBEDTLS_AES_DECRYPT. | ||||
|  * \param length   The length of the input data. | ||||
|  * \param length   The length of the input data in Bytes. | ||||
|  * \param iv_off   The offset in IV (updated after use). | ||||
|  *                 It must point to a valid \c size_t. | ||||
|  * \param iv       The initialization vector (updated after use). | ||||
|  *                 It must be a readable and writeable buffer of \c 16 Bytes. | ||||
|  * \param input    The buffer holding the input data. | ||||
|  *                 It must be readable and of size \p length Bytes. | ||||
|  * \param output   The buffer holding the output data. | ||||
|  *                 It must be writeable and of size \p length Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  */ | ||||
|  | @ -401,12 +426,16 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, | |||
|  * | ||||
|  * | ||||
|  * \param ctx      The AES context to use for encryption or decryption. | ||||
|  *                 It must be initialized and bound to a key. | ||||
|  * \param mode     The AES operation: #MBEDTLS_AES_ENCRYPT or | ||||
|  *                 #MBEDTLS_AES_DECRYPT | ||||
|  * \param length   The length of the input data. | ||||
|  * \param iv       The initialization vector (updated after use). | ||||
|  *                 It must be a readable and writeable buffer of \c 16 Bytes. | ||||
|  * \param input    The buffer holding the input data. | ||||
|  *                 It must be readable and of size \p length Bytes. | ||||
|  * \param output   The buffer holding the output data. | ||||
|  *                 It must be writeable and of size \p length Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  */ | ||||
|  | @ -451,11 +480,16 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx, | |||
|  *              will compromise security. | ||||
|  * | ||||
|  * \param ctx      The AES context to use for encryption or decryption. | ||||
|  *                 It must be initialized and bound to a key. | ||||
|  * \param length   The length of the input data. | ||||
|  * \param iv_off   The offset in IV (updated after use). | ||||
|  *                 It must point to a valid \c size_t. | ||||
|  * \param iv       The initialization vector (updated after use). | ||||
|  *                 It must be a readable and writeable buffer of \c 16 Bytes. | ||||
|  * \param input    The buffer holding the input data. | ||||
|  *                 It must be readable and of size \p length Bytes. | ||||
|  * \param output   The buffer holding the output data. | ||||
|  *                 It must be writeable and of size \p length Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  */ | ||||
|  | @ -527,15 +561,21 @@ int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx, | |||
|  *             securely discarded as soon as it's no longer needed. | ||||
|  * | ||||
|  * \param ctx              The AES context to use for encryption or decryption. | ||||
|  *                         It must be initialized and bound to a key. | ||||
|  * \param length           The length of the input data. | ||||
|  * \param nc_off           The offset in the current \p stream_block, for | ||||
|  *                         resuming within the current cipher stream. The | ||||
|  *                         offset pointer should be 0 at the start of a stream. | ||||
|  *                         It must point to a valid \c size_t. | ||||
|  * \param nonce_counter    The 128-bit nonce and counter. | ||||
|  *                         It must be a readable-writeable buffer of \c 16 Bytes. | ||||
|  * \param stream_block     The saved stream block for resuming. This is | ||||
|  *                         overwritten by the function. | ||||
|  *                         It must be a readable-writeable buffer of \c 16 Bytes. | ||||
|  * \param input            The buffer holding the input data. | ||||
|  *                         It must be readable and of size \p length Bytes. | ||||
|  * \param output           The buffer holding the output data. | ||||
|  *                         It must be writeable and of size \p length Bytes. | ||||
|  * | ||||
|  * \return                 \c 0 on success. | ||||
|  */ | ||||
|  | @ -588,7 +628,7 @@ int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx, | |||
|  * \brief           Deprecated internal AES block encryption function | ||||
|  *                  without return value. | ||||
|  * | ||||
|  * \deprecated      Superseded by mbedtls_aes_encrypt_ext() in 2.5.0. | ||||
|  * \deprecated      Superseded by mbedtls_internal_aes_encrypt() | ||||
|  * | ||||
|  * \param ctx       The AES context to use for encryption. | ||||
|  * \param input     Plaintext block. | ||||
|  | @ -602,7 +642,7 @@ MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx, | |||
|  * \brief           Deprecated internal AES block decryption function | ||||
|  *                  without return value. | ||||
|  * | ||||
|  * \deprecated      Superseded by mbedtls_aes_decrypt_ext() in 2.5.0. | ||||
|  * \deprecated      Superseded by mbedtls_internal_aes_decrypt() | ||||
|  * | ||||
|  * \param ctx       The AES context to use for decryption. | ||||
|  * \param input     Ciphertext block. | ||||
|  | @ -615,6 +655,8 @@ MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx, | |||
| #undef MBEDTLS_DEPRECATED | ||||
| #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | ||||
| 
 | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| /**
 | ||||
|  * \brief          Checkup routine. | ||||
|  * | ||||
|  | @ -623,6 +665,8 @@ MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx, | |||
|  */ | ||||
| int mbedtls_aes_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
|  | @ -2,6 +2,9 @@ | |||
|  * \file aesni.h | ||||
|  * | ||||
|  * \brief AES-NI for hardware AES acceleration on some Intel processors | ||||
|  * | ||||
|  * \warning These functions are only for internal use by other library | ||||
|  *          functions; you must not call them directly. | ||||
|  */ | ||||
| /*
 | ||||
|  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved | ||||
|  | @ -24,6 +27,12 @@ | |||
| #ifndef MBEDTLS_AESNI_H | ||||
| #define MBEDTLS_AESNI_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "aes.h" | ||||
| 
 | ||||
| #define MBEDTLS_AESNI_AES      0x02000000u | ||||
|  | @ -42,7 +51,10 @@ extern "C" { | |||
| #endif | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          AES-NI features detection routine | ||||
|  * \brief          Internal function to detect the AES-NI feature in CPUs. | ||||
|  * | ||||
|  * \note           This function is only for internal use by other library | ||||
|  *                 functions; you must not call it directly. | ||||
|  * | ||||
|  * \param what     The feature to detect | ||||
|  *                 (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL) | ||||
|  | @ -52,7 +64,10 @@ extern "C" { | |||
| int mbedtls_aesni_has_support( unsigned int what ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          AES-NI AES-ECB block en(de)cryption | ||||
|  * \brief          Internal AES-NI AES-ECB block encryption and decryption | ||||
|  * | ||||
|  * \note           This function is only for internal use by other library | ||||
|  *                 functions; you must not call it directly. | ||||
|  * | ||||
|  * \param ctx      AES context | ||||
|  * \param mode     MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT | ||||
|  | @ -62,12 +77,15 @@ int mbedtls_aesni_has_support( unsigned int what ); | |||
|  * \return         0 on success (cannot fail) | ||||
|  */ | ||||
| int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx, | ||||
|                      int mode, | ||||
|                      const unsigned char input[16], | ||||
|                      unsigned char output[16] ); | ||||
|                              int mode, | ||||
|                              const unsigned char input[16], | ||||
|                              unsigned char output[16] ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          GCM multiplication: c = a * b in GF(2^128) | ||||
|  * \brief          Internal GCM multiplication: c = a * b in GF(2^128) | ||||
|  * | ||||
|  * \note           This function is only for internal use by other library | ||||
|  *                 functions; you must not call it directly. | ||||
|  * | ||||
|  * \param c        Result | ||||
|  * \param a        First operand | ||||
|  | @ -77,21 +95,29 @@ int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx, | |||
|  *                 elements of GF(2^128) as per the GCM spec. | ||||
|  */ | ||||
| void mbedtls_aesni_gcm_mult( unsigned char c[16], | ||||
|                      const unsigned char a[16], | ||||
|                      const unsigned char b[16] ); | ||||
|                              const unsigned char a[16], | ||||
|                              const unsigned char b[16] ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           Compute decryption round keys from encryption round keys | ||||
|  * \brief           Internal round key inversion. This function computes | ||||
|  *                  decryption round keys from the encryption round keys. | ||||
|  * | ||||
|  * \note            This function is only for internal use by other library | ||||
|  *                  functions; you must not call it directly. | ||||
|  * | ||||
|  * \param invkey    Round keys for the equivalent inverse cipher | ||||
|  * \param fwdkey    Original round keys (for encryption) | ||||
|  * \param nr        Number of rounds (that is, number of round keys minus one) | ||||
|  */ | ||||
| void mbedtls_aesni_inverse_key( unsigned char *invkey, | ||||
|                         const unsigned char *fwdkey, int nr ); | ||||
|                                 const unsigned char *fwdkey, | ||||
|                                 int nr ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           Perform key expansion (for encryption) | ||||
|  * \brief           Internal key expansion for encryption | ||||
|  * | ||||
|  * \note            This function is only for internal use by other library | ||||
|  *                  functions; you must not call it directly. | ||||
|  * | ||||
|  * \param rk        Destination buffer where the round keys are written | ||||
|  * \param key       Encryption key | ||||
|  | @ -100,8 +126,8 @@ void mbedtls_aesni_inverse_key( unsigned char *invkey, | |||
|  * \return          0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH | ||||
|  */ | ||||
| int mbedtls_aesni_setkey_enc( unsigned char *rk, | ||||
|                       const unsigned char *key, | ||||
|                       size_t bits ); | ||||
|                               const unsigned char *key, | ||||
|                               size_t bits ); | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
|  |  | |||
|  | @ -123,6 +123,8 @@ void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key, | |||
| int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input, | ||||
|                 unsigned char *output ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          Checkup routine | ||||
|  * | ||||
|  | @ -135,6 +137,8 @@ int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned | |||
|  */ | ||||
| int mbedtls_arc4_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
|  | @ -39,6 +39,8 @@ | |||
| #include <stddef.h> | ||||
| #include <stdint.h> | ||||
| 
 | ||||
| #include "platform_util.h" | ||||
| 
 | ||||
| #define MBEDTLS_ARIA_ENCRYPT     1 /**< ARIA encryption. */ | ||||
| #define MBEDTLS_ARIA_DECRYPT     0 /**< ARIA decryption. */ | ||||
| 
 | ||||
|  | @ -46,8 +48,12 @@ | |||
| #define MBEDTLS_ARIA_MAX_ROUNDS  16 /**< Maxiumum number of rounds in ARIA. */ | ||||
| #define MBEDTLS_ARIA_MAX_KEYSIZE 32 /**< Maximum size of an ARIA key in bytes. */ | ||||
| 
 | ||||
| #define MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH   -0x005C  /**< Invalid key length. */ | ||||
| #define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E  /**< Invalid data input length. */ | ||||
| #if !defined(MBEDTLS_DEPRECATED_REMOVED) | ||||
| #define MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH   MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x005C ) | ||||
| #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | ||||
| #define MBEDTLS_ERR_ARIA_BAD_INPUT_DATA -0x005C /**< Bad input data. */ | ||||
| 
 | ||||
| #define MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH -0x005E /**< Invalid data input length. */ | ||||
| 
 | ||||
| /* MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE is deprecated and should not be used.
 | ||||
|  */ | ||||
|  | @ -85,14 +91,16 @@ mbedtls_aria_context; | |||
|  *                 It must be the first API called before using | ||||
|  *                 the context. | ||||
|  * | ||||
|  * \param ctx      The ARIA context to initialize. | ||||
|  * \param ctx      The ARIA context to initialize. This must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_aria_init( mbedtls_aria_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          This function releases and clears the specified ARIA context. | ||||
|  * | ||||
|  * \param ctx      The ARIA context to clear. | ||||
|  * \param ctx      The ARIA context to clear. This may be \c NULL, in which | ||||
|  *                 case this function returns immediately. If it is not \c NULL, | ||||
|  *                 it must point to an initialized ARIA context. | ||||
|  */ | ||||
| void mbedtls_aria_free( mbedtls_aria_context *ctx ); | ||||
| 
 | ||||
|  | @ -100,14 +108,16 @@ void mbedtls_aria_free( mbedtls_aria_context *ctx ); | |||
|  * \brief          This function sets the encryption key. | ||||
|  * | ||||
|  * \param ctx      The ARIA context to which the key should be bound. | ||||
|  * \param key      The encryption key. | ||||
|  * \param keybits  The size of data passed in bits. Valid options are: | ||||
|  *                 This must be initialized. | ||||
|  * \param key      The encryption key. This must be a readable buffer | ||||
|  *                 of size \p keybits Bits. | ||||
|  * \param keybits  The size of \p key in Bits. Valid options are: | ||||
|  *                 <ul><li>128 bits</li> | ||||
|  *                 <li>192 bits</li> | ||||
|  *                 <li>256 bits</li></ul> | ||||
|  * | ||||
|  * \return         \c 0 on success or #MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH | ||||
|  *                 on failure. | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx, | ||||
|                              const unsigned char *key, | ||||
|  | @ -117,13 +127,16 @@ int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx, | |||
|  * \brief          This function sets the decryption key. | ||||
|  * | ||||
|  * \param ctx      The ARIA context to which the key should be bound. | ||||
|  * \param key      The decryption key. | ||||
|  *                 This must be initialized. | ||||
|  * \param key      The decryption key. This must be a readable buffer | ||||
|  *                 of size \p keybits Bits. | ||||
|  * \param keybits  The size of data passed. Valid options are: | ||||
|  *                 <ul><li>128 bits</li> | ||||
|  *                 <li>192 bits</li> | ||||
|  *                 <li>256 bits</li></ul> | ||||
|  * | ||||
|  * \return         \c 0 on success, or #MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH on failure. | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx, | ||||
|                              const unsigned char *key, | ||||
|  | @ -142,10 +155,12 @@ int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx, | |||
|  *                 call to this API with the same context. | ||||
|  * | ||||
|  * \param ctx      The ARIA context to use for encryption or decryption. | ||||
|  *                 This must be initialized and bound to a key. | ||||
|  * \param input    The 16-Byte buffer holding the input data. | ||||
|  * \param output   The 16-Byte buffer holding the output data. | ||||
| 
 | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx, | ||||
|                             const unsigned char input[MBEDTLS_ARIA_BLOCKSIZE], | ||||
|  | @ -177,16 +192,21 @@ int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx, | |||
|  * | ||||
|  * | ||||
|  * \param ctx      The ARIA context to use for encryption or decryption. | ||||
|  * \param mode     The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or | ||||
|  *                 #MBEDTLS_ARIA_DECRYPT. | ||||
|  *                 This must be initialized and bound to a key. | ||||
|  * \param mode     The mode of operation. This must be either | ||||
|  *                 #MBEDTLS_ARIA_ENCRYPT for encryption, or | ||||
|  *                 #MBEDTLS_ARIA_DECRYPT for decryption. | ||||
|  * \param length   The length of the input data in Bytes. This must be a | ||||
|  *                 multiple of the block size (16 Bytes). | ||||
|  * \param iv       Initialization vector (updated after use). | ||||
|  * \param input    The buffer holding the input data. | ||||
|  * \param output   The buffer holding the output data. | ||||
|  *                 This must be a readable buffer of size 16 Bytes. | ||||
|  * \param input    The buffer holding the input data. This must | ||||
|  *                 be a readable buffer of length \p length Bytes. | ||||
|  * \param output   The buffer holding the output data. This must | ||||
|  *                 be a writable buffer of length \p length Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success, or #MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH | ||||
|  *                 on failure. | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx, | ||||
|                             int mode, | ||||
|  | @ -221,15 +241,22 @@ int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx, | |||
|  * | ||||
|  * | ||||
|  * \param ctx      The ARIA context to use for encryption or decryption. | ||||
|  * \param mode     The ARIA operation: #MBEDTLS_ARIA_ENCRYPT or | ||||
|  *                 #MBEDTLS_ARIA_DECRYPT. | ||||
|  * \param length   The length of the input data. | ||||
|  *                 This must be initialized and bound to a key. | ||||
|  * \param mode     The mode of operation. This must be either | ||||
|  *                 #MBEDTLS_ARIA_ENCRYPT for encryption, or | ||||
|  *                 #MBEDTLS_ARIA_DECRYPT for decryption. | ||||
|  * \param length   The length of the input data \p input in Bytes. | ||||
|  * \param iv_off   The offset in IV (updated after use). | ||||
|  *                 This must not be larger than 15. | ||||
|  * \param iv       The initialization vector (updated after use). | ||||
|  * \param input    The buffer holding the input data. | ||||
|  * \param output   The buffer holding the output data. | ||||
|  *                 This must be a readable buffer of size 16 Bytes. | ||||
|  * \param input    The buffer holding the input data. This must | ||||
|  *                 be a readable buffer of length \p length Bytes. | ||||
|  * \param output   The buffer holding the output data. This must | ||||
|  *                 be a writable buffer of length \p length Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx, | ||||
|                                int mode, | ||||
|  | @ -299,17 +326,24 @@ int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx, | |||
|  *             securely discarded as soon as it's no longer needed. | ||||
|  * | ||||
|  * \param ctx              The ARIA context to use for encryption or decryption. | ||||
|  * \param length           The length of the input data. | ||||
|  * \param nc_off           The offset in the current \p stream_block, for | ||||
|  *                         resuming within the current cipher stream. The | ||||
|  *                         offset pointer should be 0 at the start of a stream. | ||||
|  * \param nonce_counter    The 128-bit nonce and counter. | ||||
|  * \param stream_block     The saved stream block for resuming. This is | ||||
|  *                         overwritten by the function. | ||||
|  * \param input            The buffer holding the input data. | ||||
|  * \param output           The buffer holding the output data. | ||||
|  *                         This must be initialized and bound to a key. | ||||
|  * \param length           The length of the input data \p input in Bytes. | ||||
|  * \param nc_off           The offset in Bytes in the current \p stream_block, | ||||
|  *                         for resuming within the current cipher stream. The | ||||
|  *                         offset pointer should be \c 0 at the start of a | ||||
|  *                         stream. This must not be larger than \c 15 Bytes. | ||||
|  * \param nonce_counter    The 128-bit nonce and counter. This must point to | ||||
|  *                         a read/write buffer of length \c 16 bytes. | ||||
|  * \param stream_block     The saved stream block for resuming. This must | ||||
|  *                         point to a read/write buffer of length \c 16 bytes. | ||||
|  *                         This is overwritten by the function. | ||||
|  * \param input            The buffer holding the input data. This must | ||||
|  *                         be a readable buffer of length \p length Bytes. | ||||
|  * \param output           The buffer holding the output data. This must | ||||
|  *                         be a writable buffer of length \p length Bytes. | ||||
|  * | ||||
|  * \return     \c 0 on success. | ||||
|  * \return                 \c 0 on success. | ||||
|  * \return                 A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx, | ||||
|                             size_t length, | ||||
|  |  | |||
|  | @ -24,6 +24,12 @@ | |||
| #ifndef MBEDTLS_ASN1_WRITE_H | ||||
| #define MBEDTLS_ASN1_WRITE_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "asn1.h" | ||||
| 
 | ||||
| #define MBEDTLS_ASN1_CHK_ADD(g, f)                      \ | ||||
|  |  | |||
|  | @ -24,6 +24,12 @@ | |||
| #ifndef MBEDTLS_BASE64_H | ||||
| #define MBEDTLS_BASE64_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include <stddef.h> | ||||
| 
 | ||||
| #define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL               -0x002A  /**< Output buffer too small. */ | ||||
|  | @ -75,6 +81,7 @@ int mbedtls_base64_encode( unsigned char *dst, size_t dlen, size_t *olen, | |||
| int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, | ||||
|                    const unsigned char *src, size_t slen ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| /**
 | ||||
|  * \brief          Checkup routine | ||||
|  * | ||||
|  | @ -82,6 +89,8 @@ int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, | |||
|  */ | ||||
| int mbedtls_base64_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							|  | @ -33,6 +33,8 @@ | |||
| #include <stddef.h> | ||||
| #include <stdint.h> | ||||
| 
 | ||||
| #include "platform_util.h" | ||||
| 
 | ||||
| #define MBEDTLS_BLOWFISH_ENCRYPT     1 | ||||
| #define MBEDTLS_BLOWFISH_DECRYPT     0 | ||||
| #define MBEDTLS_BLOWFISH_MAX_KEY_BITS     448 | ||||
|  | @ -40,14 +42,17 @@ | |||
| #define MBEDTLS_BLOWFISH_ROUNDS      16         /**< Rounds to use. When increasing this value, make sure to extend the initialisation vectors */ | ||||
| #define MBEDTLS_BLOWFISH_BLOCKSIZE   8          /* Blowfish uses 64 bit blocks */ | ||||
| 
 | ||||
| #define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH                -0x0016  /**< Invalid key length. */ | ||||
| #if !defined(MBEDTLS_DEPRECATED_REMOVED) | ||||
| #define MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH   MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0016 ) | ||||
| #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | ||||
| #define MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA -0x0016 /**< Bad input data. */ | ||||
| 
 | ||||
| #define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */ | ||||
| 
 | ||||
| /* MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED is deprecated and should not be used.
 | ||||
|  */ | ||||
| #define MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED                   -0x0017  /**< Blowfish hardware accelerator failed. */ | ||||
| 
 | ||||
| #define MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH              -0x0018  /**< Invalid data input length. */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
| #endif | ||||
|  | @ -71,40 +76,53 @@ mbedtls_blowfish_context; | |||
| #endif /* MBEDTLS_BLOWFISH_ALT */ | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          Initialize Blowfish context | ||||
|  * \brief          Initialize a Blowfish context. | ||||
|  * | ||||
|  * \param ctx      Blowfish context to be initialized | ||||
|  * \param ctx      The Blowfish context to be initialized. | ||||
|  *                 This must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          Clear Blowfish context | ||||
|  * \brief          Clear a Blowfish context. | ||||
|  * | ||||
|  * \param ctx      Blowfish context to be cleared | ||||
|  * \param ctx      The Blowfish context to be cleared. | ||||
|  *                 This may be \c NULL, in which case this function | ||||
|  *                 returns immediately. If it is not \c NULL, it must | ||||
|  *                 point to an initialized Blowfish context. | ||||
|  */ | ||||
| void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          Blowfish key schedule | ||||
|  * \brief          Perform a Blowfish key schedule operation. | ||||
|  * | ||||
|  * \param ctx      Blowfish context to be initialized | ||||
|  * \param key      encryption key | ||||
|  * \param keybits  must be between 32 and 448 bits | ||||
|  * \param ctx      The Blowfish context to perform the key schedule on. | ||||
|  * \param key      The encryption key. This must be a readable buffer of | ||||
|  *                 length \p keybits Bits. | ||||
|  * \param keybits  The length of \p key in Bits. This must be between | ||||
|  *                 \c 32 and \c 448 and a multiple of \c 8. | ||||
|  * | ||||
|  * \return         0 if successful, or MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH | ||||
|  * \return         \c 0 if successful. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key, | ||||
|                      unsigned int keybits ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          Blowfish-ECB block encryption/decryption | ||||
|  * \brief          Perform a Blowfish-ECB block encryption/decryption operation. | ||||
|  * | ||||
|  * \param ctx      Blowfish context | ||||
|  * \param mode     MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT | ||||
|  * \param input    8-byte input block | ||||
|  * \param output   8-byte output block | ||||
|  * \param ctx      The Blowfish context to use. This must be initialized | ||||
|  *                 and bound to a key. | ||||
|  * \param mode     The mode of operation. Possible values are | ||||
|  *                 #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or | ||||
|  *                 #MBEDTLS_BLOWFISH_DECRYPT for decryption. | ||||
|  * \param input    The input block. This must be a readable buffer | ||||
|  *                 of size \c 8 Bytes. | ||||
|  * \param output   The output block. This must be a writable buffer | ||||
|  *                 of size \c 8 Bytes. | ||||
|  * | ||||
|  * \return         0 if successful | ||||
|  * \return         \c 0 if successful. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx, | ||||
|                         int mode, | ||||
|  | @ -113,9 +131,7 @@ int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx, | |||
| 
 | ||||
| #if defined(MBEDTLS_CIPHER_MODE_CBC) | ||||
| /**
 | ||||
|  * \brief          Blowfish-CBC buffer encryption/decryption | ||||
|  *                 Length should be a multiple of the block | ||||
|  *                 size (8 bytes) | ||||
|  * \brief          Perform a Blowfish-CBC buffer encryption/decryption operation. | ||||
|  * | ||||
|  * \note           Upon exit, the content of the IV is updated so that you can | ||||
|  *                 call the function same function again on the following | ||||
|  | @ -125,15 +141,22 @@ int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx, | |||
|  *                 IV, you should either save it manually or use the cipher | ||||
|  *                 module instead. | ||||
|  * | ||||
|  * \param ctx      Blowfish context | ||||
|  * \param mode     MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT | ||||
|  * \param length   length of the input data | ||||
|  * \param iv       initialization vector (updated after use) | ||||
|  * \param input    buffer holding the input data | ||||
|  * \param output   buffer holding the output data | ||||
|  * \param ctx      The Blowfish context to use. This must be initialized | ||||
|  *                 and bound to a key. | ||||
|  * \param mode     The mode of operation. Possible values are | ||||
|  *                 #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or | ||||
|  *                 #MBEDTLS_BLOWFISH_DECRYPT for decryption. | ||||
|  * \param length   The length of the input data in Bytes. This must be | ||||
|  *                 multiple of \c 8. | ||||
|  * \param iv       The initialization vector. This must be a read/write buffer | ||||
|  *                 of length \c 8 Bytes. It is updated by this function. | ||||
|  * \param input    The input data. This must be a readable buffer of length | ||||
|  *                 \p length Bytes. | ||||
|  * \param output   The output data. This must be a writable buffer of length | ||||
|  *                 \p length Bytes. | ||||
|  * | ||||
|  * \return         0 if successful, or | ||||
|  *                 MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH | ||||
|  * \return         \c 0 if successful. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx, | ||||
|                         int mode, | ||||
|  | @ -145,7 +168,7 @@ int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx, | |||
| 
 | ||||
| #if defined(MBEDTLS_CIPHER_MODE_CFB) | ||||
| /**
 | ||||
|  * \brief          Blowfish CFB buffer encryption/decryption. | ||||
|  * \brief          Perform a Blowfish CFB buffer encryption/decryption operation. | ||||
|  * | ||||
|  * \note           Upon exit, the content of the IV is updated so that you can | ||||
|  *                 call the function same function again on the following | ||||
|  | @ -155,15 +178,25 @@ int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx, | |||
|  *                 IV, you should either save it manually or use the cipher | ||||
|  *                 module instead. | ||||
|  * | ||||
|  * \param ctx      Blowfish context | ||||
|  * \param mode     MBEDTLS_BLOWFISH_ENCRYPT or MBEDTLS_BLOWFISH_DECRYPT | ||||
|  * \param length   length of the input data | ||||
|  * \param iv_off   offset in IV (updated after use) | ||||
|  * \param iv       initialization vector (updated after use) | ||||
|  * \param input    buffer holding the input data | ||||
|  * \param output   buffer holding the output data | ||||
|  * \param ctx      The Blowfish context to use. This must be initialized | ||||
|  *                 and bound to a key. | ||||
|  * \param mode     The mode of operation. Possible values are | ||||
|  *                 #MBEDTLS_BLOWFISH_ENCRYPT for encryption, or | ||||
|  *                 #MBEDTLS_BLOWFISH_DECRYPT for decryption. | ||||
|  * \param length   The length of the input data in Bytes. | ||||
|  * \param iv_off   The offset in the initialiation vector. | ||||
|  *                 The value pointed to must be smaller than \c 8 Bytes. | ||||
|  *                 It is updated by this function to support the aforementioned | ||||
|  *                 streaming usage. | ||||
|  * \param iv       The initialization vector. This must be a read/write buffer | ||||
|  *                 of size \c 8 Bytes. It is updated after use. | ||||
|  * \param input    The input data. This must be a readable buffer of length | ||||
|  *                 \p length Bytes. | ||||
|  * \param output   The output data. This must be a writable buffer of length | ||||
|  *                 \p length Bytes. | ||||
|  * | ||||
|  * \return         0 if successful | ||||
|  * \return         \c 0 if successful. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx, | ||||
|                           int mode, | ||||
|  | @ -176,7 +209,7 @@ int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx, | |||
| 
 | ||||
| #if defined(MBEDTLS_CIPHER_MODE_CTR) | ||||
| /**
 | ||||
|  * \brief               Blowfish-CTR buffer encryption/decryption | ||||
|  * \brief      Perform a Blowfish-CTR buffer encryption/decryption operation. | ||||
|  * | ||||
|  * \warning    You must never reuse a nonce value with the same key. Doing so | ||||
|  *             would void the encryption for the two messages encrypted with | ||||
|  | @ -219,18 +252,24 @@ int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx, | |||
|  *             content must not be written to insecure storage and should be | ||||
|  *             securely discarded as soon as it's no longer needed. | ||||
|  * | ||||
|  * \param ctx           Blowfish context | ||||
|  * \param length        The length of the data | ||||
|  * \param ctx           The Blowfish context to use. This must be initialized | ||||
|  *                      and bound to a key. | ||||
|  * \param length        The length of the input data in Bytes. | ||||
|  * \param nc_off        The offset in the current stream_block (for resuming | ||||
|  *                      within current cipher stream). The offset pointer to | ||||
|  *                      should be 0 at the start of a stream. | ||||
|  * \param nonce_counter The 64-bit nonce and counter. | ||||
|  * \param stream_block  The saved stream-block for resuming. Is overwritten | ||||
|  *                      by the function. | ||||
|  * \param input         The input data stream | ||||
|  * \param output        The output data stream | ||||
|  *                      within current cipher stream). The offset pointer | ||||
|  *                      should be \c 0 at the start of a stream and must be | ||||
|  *                      smaller than \c 8. It is updated by this function. | ||||
|  * \param nonce_counter The 64-bit nonce and counter. This must point to a | ||||
|  *                      read/write buffer of length \c 8 Bytes. | ||||
|  * \param stream_block  The saved stream-block for resuming. This must point to | ||||
|  *                      a read/write buffer of length \c 8 Bytes. | ||||
|  * \param input         The input data. This must be a readable buffer of | ||||
|  *                      length \p length Bytes. | ||||
|  * \param output        The output data. This must be a writable buffer of | ||||
|  *                      length \p length Bytes. | ||||
|  * | ||||
|  * \return         0 if successful | ||||
|  * \return              \c 0 if successful. | ||||
|  * \return              A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx, | ||||
|                         size_t length, | ||||
|  |  | |||
|  | @ -38,6 +38,12 @@ | |||
| #ifndef MBEDTLS_BN_MUL_H | ||||
| #define MBEDTLS_BN_MUL_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "bignum.h" | ||||
| 
 | ||||
| #if defined(MBEDTLS_HAVE_ASM) | ||||
|  | @ -750,7 +756,7 @@ | |||
|         "sw     $10, %2         \n\t"   \ | ||||
|         : "=m" (c), "=m" (d), "=m" (s)                      \ | ||||
|         : "m" (s), "m" (d), "m" (c), "m" (b)                \ | ||||
|         : "$9", "$10", "$11", "$12", "$13", "$14", "$15"    \ | ||||
|         : "$9", "$10", "$11", "$12", "$13", "$14", "$15", "lo", "hi" \ | ||||
|     ); | ||||
| 
 | ||||
| #endif /* MIPS */ | ||||
|  |  | |||
|  | @ -33,11 +33,17 @@ | |||
| #include <stddef.h> | ||||
| #include <stdint.h> | ||||
| 
 | ||||
| #include "platform_util.h" | ||||
| 
 | ||||
| #define MBEDTLS_CAMELLIA_ENCRYPT     1 | ||||
| #define MBEDTLS_CAMELLIA_DECRYPT     0 | ||||
| 
 | ||||
| #define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH           -0x0024  /**< Invalid key length. */ | ||||
| #define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH         -0x0026  /**< Invalid data input length. */ | ||||
| #if !defined(MBEDTLS_DEPRECATED_REMOVED) | ||||
| #define MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH   MBEDTLS_DEPRECATED_NUMERIC_CONSTANT( -0x0024 ) | ||||
| #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | ||||
| #define MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA -0x0024 /**< Bad input data. */ | ||||
| 
 | ||||
| #define MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */ | ||||
| 
 | ||||
| /* MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED is deprecated and should not be used.
 | ||||
|  */ | ||||
|  | @ -66,52 +72,68 @@ mbedtls_camellia_context; | |||
| #endif /* MBEDTLS_CAMELLIA_ALT */ | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          Initialize CAMELLIA context | ||||
|  * \brief          Initialize a CAMELLIA context. | ||||
|  * | ||||
|  * \param ctx      CAMELLIA context to be initialized | ||||
|  * \param ctx      The CAMELLIA context to be initialized. | ||||
|  *                 This must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_camellia_init( mbedtls_camellia_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          Clear CAMELLIA context | ||||
|  * \brief          Clear a CAMELLIA context. | ||||
|  * | ||||
|  * \param ctx      CAMELLIA context to be cleared | ||||
|  * \param ctx      The CAMELLIA context to be cleared. This may be \c NULL, | ||||
|  *                 in which case this function returns immediately. If it is not | ||||
|  *                 \c NULL, it must be initialized. | ||||
|  */ | ||||
| void mbedtls_camellia_free( mbedtls_camellia_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          CAMELLIA key schedule (encryption) | ||||
|  * \brief          Perform a CAMELLIA key schedule operation for encryption. | ||||
|  * | ||||
|  * \param ctx      CAMELLIA context to be initialized | ||||
|  * \param key      encryption key | ||||
|  * \param keybits  must be 128, 192 or 256 | ||||
|  * \param ctx      The CAMELLIA context to use. This must be initialized. | ||||
|  * \param key      The encryption key to use. This must be a readable buffer | ||||
|  *                 of size \p keybits Bits. | ||||
|  * \param keybits  The length of \p key in Bits. This must be either \c 128, | ||||
|  *                 \c 192 or \c 256. | ||||
|  * | ||||
|  * \return         0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH | ||||
|  * \return         \c 0 if successful. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key, | ||||
|                          unsigned int keybits ); | ||||
| int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, | ||||
|                                  const unsigned char *key, | ||||
|                                  unsigned int keybits ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          CAMELLIA key schedule (decryption) | ||||
|  * \brief          Perform a CAMELLIA key schedule operation for decryption. | ||||
|  * | ||||
|  * \param ctx      CAMELLIA context to be initialized | ||||
|  * \param key      decryption key | ||||
|  * \param keybits  must be 128, 192 or 256 | ||||
|  * \param ctx      The CAMELLIA context to use. This must be initialized. | ||||
|  * \param key      The decryption key. This must be a readable buffer | ||||
|  *                 of size \p keybits Bits. | ||||
|  * \param keybits  The length of \p key in Bits. This must be either \c 128, | ||||
|  *                 \c 192 or \c 256. | ||||
|  * | ||||
|  * \return         0 if successful, or MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH | ||||
|  * \return         \c 0 if successful. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key, | ||||
|                          unsigned int keybits ); | ||||
| int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, | ||||
|                                  const unsigned char *key, | ||||
|                                  unsigned int keybits ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          CAMELLIA-ECB block encryption/decryption | ||||
|  * \brief          Perform a CAMELLIA-ECB block encryption/decryption operation. | ||||
|  * | ||||
|  * \param ctx      CAMELLIA context | ||||
|  * \param mode     MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT | ||||
|  * \param input    16-byte input block | ||||
|  * \param output   16-byte output block | ||||
|  * \param ctx      The CAMELLIA context to use. This must be initialized | ||||
|  *                 and bound to a key. | ||||
|  * \param mode     The mode of operation. This must be either | ||||
|  *                 #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. | ||||
|  * \param input    The input block. This must be a readable buffer | ||||
|  *                 of size \c 16 Bytes. | ||||
|  * \param output   The output block. This must be a writable buffer | ||||
|  *                 of size \c 16 Bytes. | ||||
|  * | ||||
|  * \return         0 if successful | ||||
|  * \return         \c 0 if successful. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx, | ||||
|                     int mode, | ||||
|  | @ -120,9 +142,7 @@ int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx, | |||
| 
 | ||||
| #if defined(MBEDTLS_CIPHER_MODE_CBC) | ||||
| /**
 | ||||
|  * \brief          CAMELLIA-CBC buffer encryption/decryption | ||||
|  *                 Length should be a multiple of the block | ||||
|  *                 size (16 bytes) | ||||
|  * \brief          Perform a CAMELLIA-CBC buffer encryption/decryption operation. | ||||
|  * | ||||
|  * \note           Upon exit, the content of the IV is updated so that you can | ||||
|  *                 call the function same function again on the following | ||||
|  | @ -132,15 +152,22 @@ int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx, | |||
|  *                 IV, you should either save it manually or use the cipher | ||||
|  *                 module instead. | ||||
|  * | ||||
|  * \param ctx      CAMELLIA context | ||||
|  * \param mode     MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT | ||||
|  * \param length   length of the input data | ||||
|  * \param iv       initialization vector (updated after use) | ||||
|  * \param input    buffer holding the input data | ||||
|  * \param output   buffer holding the output data | ||||
|  * \param ctx      The CAMELLIA context to use. This must be initialized | ||||
|  *                 and bound to a key. | ||||
|  * \param mode     The mode of operation. This must be either | ||||
|  *                 #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. | ||||
|  * \param length   The length in Bytes of the input data \p input. | ||||
|  *                 This must be a multiple of \c 16 Bytes. | ||||
|  * \param iv       The initialization vector. This must be a read/write buffer | ||||
|  *                 of length \c 16 Bytes. It is updated to allow streaming | ||||
|  *                 use as explained above. | ||||
|  * \param input    The buffer holding the input data. This must point to a | ||||
|  *                 readable buffer of length \p length Bytes. | ||||
|  * \param output   The buffer holding the output data. This must point to a | ||||
|  *                 writable buffer of length \p length Bytes. | ||||
|  * | ||||
|  * \return         0 if successful, or | ||||
|  *                 MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH | ||||
|  * \return         \c 0 if successful. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx, | ||||
|                     int mode, | ||||
|  | @ -152,11 +179,14 @@ int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx, | |||
| 
 | ||||
| #if defined(MBEDTLS_CIPHER_MODE_CFB) | ||||
| /**
 | ||||
|  * \brief          CAMELLIA-CFB128 buffer encryption/decryption | ||||
|  * \brief          Perform a CAMELLIA-CFB128 buffer encryption/decryption | ||||
|  *                 operation. | ||||
|  * | ||||
|  * Note: Due to the nature of CFB you should use the same key schedule for | ||||
|  * both encryption and decryption. So a context initialized with | ||||
|  * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT. | ||||
|  * \note           Due to the nature of CFB mode, you should use the same | ||||
|  *                 key for both encryption and decryption. In particular, calls | ||||
|  *                 to this function should be preceded by a key-schedule via | ||||
|  *                 mbedtls_camellia_setkey_enc() regardless of whether \p mode | ||||
|  *                 is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. | ||||
|  * | ||||
|  * \note           Upon exit, the content of the IV is updated so that you can | ||||
|  *                 call the function same function again on the following | ||||
|  | @ -166,16 +196,24 @@ int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx, | |||
|  *                 IV, you should either save it manually or use the cipher | ||||
|  *                 module instead. | ||||
|  * | ||||
|  * \param ctx      CAMELLIA context | ||||
|  * \param mode     MBEDTLS_CAMELLIA_ENCRYPT or MBEDTLS_CAMELLIA_DECRYPT | ||||
|  * \param length   length of the input data | ||||
|  * \param iv_off   offset in IV (updated after use) | ||||
|  * \param iv       initialization vector (updated after use) | ||||
|  * \param input    buffer holding the input data | ||||
|  * \param output   buffer holding the output data | ||||
|  * \param ctx      The CAMELLIA context to use. This must be initialized | ||||
|  *                 and bound to a key. | ||||
|  * \param mode     The mode of operation. This must be either | ||||
|  *                 #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. | ||||
|  * \param length   The length of the input data \p input. Any value is allowed. | ||||
|  * \param iv_off   The current offset in the IV. This must be smaller | ||||
|  *                 than \c 16 Bytes. It is updated after this call to allow | ||||
|  *                 the aforementioned streaming usage. | ||||
|  * \param iv       The initialization vector. This must be a read/write buffer | ||||
|  *                 of length \c 16 Bytes. It is updated after this call to | ||||
|  *                 allow the aforementioned streaming usage. | ||||
|  * \param input    The buffer holding the input data. This must be a readable | ||||
|  *                 buffer of size \p length Bytes. | ||||
|  * \param output   The buffer to hold the output data. This must be a writable | ||||
|  *                 buffer of length \p length Bytes. | ||||
|  * | ||||
|  * \return         0 if successful, or | ||||
|  *                 MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH | ||||
|  * \return         \c 0 if successful. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx, | ||||
|                        int mode, | ||||
|  | @ -188,11 +226,13 @@ int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx, | |||
| 
 | ||||
| #if defined(MBEDTLS_CIPHER_MODE_CTR) | ||||
| /**
 | ||||
|  * \brief               CAMELLIA-CTR buffer encryption/decryption | ||||
|  * \brief      Perform a CAMELLIA-CTR buffer encryption/decryption operation. | ||||
|  * | ||||
|  * Note: Due to the nature of CTR you should use the same key schedule for | ||||
|  * both encryption and decryption. So a context initialized with | ||||
|  * mbedtls_camellia_setkey_enc() for both MBEDTLS_CAMELLIA_ENCRYPT and MBEDTLS_CAMELLIA_DECRYPT. | ||||
|  * *note       Due to the nature of CTR mode, you should use the same | ||||
|  *             key for both encryption and decryption. In particular, calls | ||||
|  *             to this function should be preceded by a key-schedule via | ||||
|  *             mbedtls_camellia_setkey_enc() regardless of whether \p mode | ||||
|  *             is #MBEDTLS_CAMELLIA_ENCRYPT or #MBEDTLS_CAMELLIA_DECRYPT. | ||||
|  * | ||||
|  * \warning    You must never reuse a nonce value with the same key. Doing so | ||||
|  *             would void the encryption for the two messages encrypted with | ||||
|  | @ -215,41 +255,49 @@ int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx, | |||
|  *             per-message nonce, handled by yourself, and the second one | ||||
|  *             updated by this function internally. | ||||
|  * | ||||
|  *             For example, you might reserve the first 12 bytes for the | ||||
|  *             per-message nonce, and the last 4 bytes for internal use. In that | ||||
|  *             case, before calling this function on a new message you need to | ||||
|  *             set the first 12 bytes of \p nonce_counter to your chosen nonce | ||||
|  *             value, the last 4 to 0, and \p nc_off to 0 (which will cause \p | ||||
|  *             stream_block to be ignored). That way, you can encrypt at most | ||||
|  *             2**96 messages of up to 2**32 blocks each with the same key. | ||||
|  *             For example, you might reserve the first \c 12 Bytes for the | ||||
|  *             per-message nonce, and the last \c 4 Bytes for internal use. | ||||
|  *             In that case, before calling this function on a new message you | ||||
|  *             need to set the first \c 12 Bytes of \p nonce_counter to your | ||||
|  *             chosen nonce value, the last four to \c 0, and \p nc_off to \c 0 | ||||
|  *             (which will cause \p stream_block to be ignored). That way, you | ||||
|  *             can encrypt at most \c 2**96 messages of up to \c 2**32 blocks | ||||
|  *             each  with the same key. | ||||
|  * | ||||
|  *             The per-message nonce (or information sufficient to reconstruct | ||||
|  *             it) needs to be communicated with the ciphertext and must be unique. | ||||
|  *             The recommended way to ensure uniqueness is to use a message | ||||
|  *             counter. An alternative is to generate random nonces, but this | ||||
|  *             limits the number of messages that can be securely encrypted: | ||||
|  *             for example, with 96-bit random nonces, you should not encrypt | ||||
|  *             more than 2**32 messages with the same key. | ||||
|  *             it) needs to be communicated with the ciphertext and must be | ||||
|  *             unique. The recommended way to ensure uniqueness is to use a | ||||
|  *             message counter. An alternative is to generate random nonces, | ||||
|  *             but this limits the number of messages that can be securely | ||||
|  *             encrypted: for example, with 96-bit random nonces, you should | ||||
|  *             not encrypt more than 2**32 messages with the same key. | ||||
|  * | ||||
|  *             Note that for both stategies, sizes are measured in blocks and | ||||
|  *             that a CAMELLIA block is 16 bytes. | ||||
|  *             that a CAMELLIA block is \c 16 Bytes. | ||||
|  * | ||||
|  * \warning    Upon return, \p stream_block contains sensitive data. Its | ||||
|  *             content must not be written to insecure storage and should be | ||||
|  *             securely discarded as soon as it's no longer needed. | ||||
|  * | ||||
|  * \param ctx           CAMELLIA context | ||||
|  * \param length        The length of the data | ||||
|  * \param nc_off        The offset in the current stream_block (for resuming | ||||
|  * \param ctx           The CAMELLIA context to use. This must be initialized | ||||
|  *                      and bound to a key. | ||||
|  * \param length        The length of the input data \p input in Bytes. | ||||
|  *                      Any value is allowed. | ||||
|  * \param nc_off        The offset in the current \p stream_block (for resuming | ||||
|  *                      within current cipher stream). The offset pointer to | ||||
|  *                      should be 0 at the start of a stream. | ||||
|  * \param nonce_counter The 128-bit nonce and counter. | ||||
|  * \param stream_block  The saved stream-block for resuming. Is overwritten | ||||
|  *                      by the function. | ||||
|  * \param input         The input data stream | ||||
|  * \param output        The output data stream | ||||
|  *                      should be \c 0 at the start of a stream. It is updated | ||||
|  *                      at the end of this call. | ||||
|  * \param nonce_counter The 128-bit nonce and counter. This must be a read/write | ||||
|  *                      buffer of length \c 16 Bytes. | ||||
|  * \param stream_block  The saved stream-block for resuming. This must be a | ||||
|  *                      read/write buffer of length \c 16 Bytes. | ||||
|  * \param input         The input data stream. This must be a readable buffer of | ||||
|  *                      size \p length Bytes. | ||||
|  * \param output        The output data stream. This must be a writable buffer | ||||
|  *                      of size \p length Bytes. | ||||
|  * | ||||
|  * \return         0 if successful | ||||
|  * \return              \c 0 if successful. | ||||
|  * \return              A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx, | ||||
|                        size_t length, | ||||
|  | @ -260,6 +308,8 @@ int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx, | |||
|                        unsigned char *output ); | ||||
| #endif /* MBEDTLS_CIPHER_MODE_CTR */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          Checkup routine | ||||
|  * | ||||
|  | @ -267,6 +317,8 @@ int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx, | |||
|  */ | ||||
| int mbedtls_camellia_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
|  | @ -49,6 +49,12 @@ | |||
| #ifndef MBEDTLS_CCM_H | ||||
| #define MBEDTLS_CCM_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "cipher.h" | ||||
| 
 | ||||
| #define MBEDTLS_ERR_CCM_BAD_INPUT       -0x000D /**< Bad input parameters to the function. */ | ||||
|  | @ -57,7 +63,6 @@ | |||
| /* MBEDTLS_ERR_CCM_HW_ACCEL_FAILED is deprecated and should not be used. */ | ||||
| #define MBEDTLS_ERR_CCM_HW_ACCEL_FAILED -0x0011 /**< CCM hardware accelerator failed. */ | ||||
| 
 | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
| #endif | ||||
|  | @ -85,7 +90,7 @@ mbedtls_ccm_context; | |||
|  *                  to make references valid, and prepare the context | ||||
|  *                  for mbedtls_ccm_setkey() or mbedtls_ccm_free(). | ||||
|  * | ||||
|  * \param ctx       The CCM context to initialize. | ||||
|  * \param ctx       The CCM context to initialize. This must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_ccm_init( mbedtls_ccm_context *ctx ); | ||||
| 
 | ||||
|  | @ -93,9 +98,10 @@ void mbedtls_ccm_init( mbedtls_ccm_context *ctx ); | |||
|  * \brief           This function initializes the CCM context set in the | ||||
|  *                  \p ctx parameter and sets the encryption key. | ||||
|  * | ||||
|  * \param ctx       The CCM context to initialize. | ||||
|  * \param ctx       The CCM context to initialize. This must be an initialized | ||||
|  *                  context. | ||||
|  * \param cipher    The 128-bit block cipher to use. | ||||
|  * \param key       The encryption key. | ||||
|  * \param key       The encryption key. This must not be \c NULL. | ||||
|  * \param keybits   The key size in bits. This must be acceptable by the cipher. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  | @ -110,7 +116,8 @@ int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx, | |||
|  * \brief   This function releases and clears the specified CCM context | ||||
|  *          and underlying cipher sub-context. | ||||
|  * | ||||
|  * \param ctx       The CCM context to clear. | ||||
|  * \param ctx       The CCM context to clear. If this is \c NULL, the function | ||||
|  *                  has no effect. Otherwise, this must be initialized. | ||||
|  */ | ||||
| void mbedtls_ccm_free( mbedtls_ccm_context *ctx ); | ||||
| 
 | ||||
|  | @ -123,19 +130,27 @@ void mbedtls_ccm_free( mbedtls_ccm_context *ctx ); | |||
|  *                  \p tag = \p output + \p length, and make sure that the | ||||
|  *                  output buffer is at least \p length + \p tag_len wide. | ||||
|  * | ||||
|  * \param ctx       The CCM context to use for encryption. | ||||
|  * \param ctx       The CCM context to use for encryption. This must be | ||||
|  *                  initialized and bound to a key. | ||||
|  * \param length    The length of the input data in Bytes. | ||||
|  * \param iv        Initialization vector (nonce). | ||||
|  * \param iv        The initialization vector (nonce). This must be a readable | ||||
|  *                  buffer of at least \p iv_len Bytes. | ||||
|  * \param iv_len    The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12, | ||||
|  *                  or 13. The length L of the message length field is | ||||
|  *                  15 - \p iv_len. | ||||
|  * \param add       The additional data field. | ||||
|  * \param add       The additional data field. If \p add_len is greater than | ||||
|  *                  zero, \p add must be a readable buffer of at least that | ||||
|  *                  length. | ||||
|  * \param add_len   The length of additional data in Bytes. | ||||
|  *                  Must be less than 2^16 - 2^8. | ||||
|  * \param input     The buffer holding the input data. | ||||
|  * \param output    The buffer holding the output data. | ||||
|  *                  Must be at least \p length Bytes wide. | ||||
|  * \param tag       The buffer holding the authentication field. | ||||
|  *                  This must be less than `2^16 - 2^8`. | ||||
|  * \param input     The buffer holding the input data. If \p length is greater | ||||
|  *                  than zero, \p input must be a readable buffer of at least | ||||
|  *                  that length. | ||||
|  * \param output    The buffer holding the output data. If \p length is greater | ||||
|  *                  than zero, \p output must be a writable buffer of at least | ||||
|  *                  that length. | ||||
|  * \param tag       The buffer holding the authentication field. This must be a | ||||
|  *                  readable buffer of at least \p tag_len Bytes. | ||||
|  * \param tag_len   The length of the authentication field to generate in Bytes: | ||||
|  *                  4, 6, 8, 10, 12, 14 or 16. | ||||
|  * | ||||
|  | @ -161,23 +176,30 @@ int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, | |||
|  *                  the tag length has to be encoded into the \p iv passed to | ||||
|  *                  this function. | ||||
|  * | ||||
|  * \param ctx       The CCM context to use for encryption. | ||||
|  * \param ctx       The CCM context to use for encryption. This must be | ||||
|  *                  initialized and bound to a key. | ||||
|  * \param length    The length of the input data in Bytes. | ||||
|  * \param iv        Initialization vector (nonce). | ||||
|  * \param iv        The initialization vector (nonce). This must be a readable | ||||
|  *                  buffer of at least \p iv_len Bytes. | ||||
|  * \param iv_len    The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12, | ||||
|  *                  or 13. The length L of the message length field is | ||||
|  *                  15 - \p iv_len. | ||||
|  * \param add       The additional data field. | ||||
|  * \param add       The additional data field. This must be a readable buffer of | ||||
|  *                  at least \p add_len Bytes. | ||||
|  * \param add_len   The length of additional data in Bytes. | ||||
|  *                  Must be less than 2^16 - 2^8. | ||||
|  * \param input     The buffer holding the input data. | ||||
|  * \param output    The buffer holding the output data. | ||||
|  *                  Must be at least \p length Bytes wide. | ||||
|  * \param tag       The buffer holding the authentication field. | ||||
|  *                  This must be less than 2^16 - 2^8. | ||||
|  * \param input     The buffer holding the input data. If \p length is greater | ||||
|  *                  than zero, \p input must be a readable buffer of at least | ||||
|  *                  that length. | ||||
|  * \param output    The buffer holding the output data. If \p length is greater | ||||
|  *                  than zero, \p output must be a writable buffer of at least | ||||
|  *                  that length. | ||||
|  * \param tag       The buffer holding the authentication field. This must be a | ||||
|  *                  readable buffer of at least \p tag_len Bytes. | ||||
|  * \param tag_len   The length of the authentication field to generate in Bytes: | ||||
|  *                  0, 4, 6, 8, 10, 12, 14 or 16. | ||||
|  * | ||||
|  * \warning         Passing 0 as \p tag_len means that the message is no | ||||
|  * \warning         Passing \c 0 as \p tag_len means that the message is no | ||||
|  *                  longer authenticated. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  | @ -193,20 +215,27 @@ int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, | |||
|  * \brief           This function performs a CCM authenticated decryption of a | ||||
|  *                  buffer. | ||||
|  * | ||||
|  * \param ctx       The CCM context to use for decryption. | ||||
|  * \param ctx       The CCM context to use for decryption. This must be | ||||
|  *                  initialized and bound to a key. | ||||
|  * \param length    The length of the input data in Bytes. | ||||
|  * \param iv        Initialization vector (nonce). | ||||
|  * \param iv        The initialization vector (nonce). This must be a readable | ||||
|  *                  buffer of at least \p iv_len Bytes. | ||||
|  * \param iv_len    The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12, | ||||
|  *                  or 13. The length L of the message length field is | ||||
|  *                  15 - \p iv_len. | ||||
|  * \param add       The additional data field. | ||||
|  * \param add       The additional data field. This must be a readable buffer | ||||
|  *                  of at least that \p add_len Bytes.. | ||||
|  * \param add_len   The length of additional data in Bytes. | ||||
|  *                  Must be less than 2^16 - 2^8. | ||||
|  * \param input     The buffer holding the input data. | ||||
|  * \param output    The buffer holding the output data. | ||||
|  *                  Must be at least \p length Bytes wide. | ||||
|  * \param tag       The buffer holding the authentication field. | ||||
|  * \param tag_len   The length of the authentication field in Bytes. | ||||
|  *                  This must be less than 2^16 - 2^8. | ||||
|  * \param input     The buffer holding the input data. If \p length is greater | ||||
|  *                  than zero, \p input must be a readable buffer of at least | ||||
|  *                  that length. | ||||
|  * \param output    The buffer holding the output data. If \p length is greater | ||||
|  *                  than zero, \p output must be a writable buffer of at least | ||||
|  *                  that length. | ||||
|  * \param tag       The buffer holding the authentication field. This must be a | ||||
|  *                  readable buffer of at least \p tag_len Bytes. | ||||
|  * \param tag_len   The length of the authentication field to generate in Bytes: | ||||
|  *                  4, 6, 8, 10, 12, 14 or 16. | ||||
|  * | ||||
|  * \return          \c 0 on success. This indicates that the message is authentic. | ||||
|  | @ -228,23 +257,30 @@ int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, | |||
|  *                  this function as \p tag_len. (\p tag needs to be adjusted | ||||
|  *                  accordingly.) | ||||
|  * | ||||
|  * \param ctx       The CCM context to use for decryption. | ||||
|  * \param ctx       The CCM context to use for decryption. This must be | ||||
|  *                  initialized and bound to a key. | ||||
|  * \param length    The length of the input data in Bytes. | ||||
|  * \param iv        Initialization vector (nonce). | ||||
|  * \param iv        The initialization vector (nonce). This must be a readable | ||||
|  *                  buffer of at least \p iv_len Bytes. | ||||
|  * \param iv_len    The length of the nonce in Bytes: 7, 8, 9, 10, 11, 12, | ||||
|  *                  or 13. The length L of the message length field is | ||||
|  *                  15 - \p iv_len. | ||||
|  * \param add       The additional data field. | ||||
|  * \param add       The additional data field. This must be a readable buffer of | ||||
|  *                  at least that \p add_len Bytes. | ||||
|  * \param add_len   The length of additional data in Bytes. | ||||
|  *                  Must be less than 2^16 - 2^8. | ||||
|  * \param input     The buffer holding the input data. | ||||
|  * \param output    The buffer holding the output data. | ||||
|  *                  Must be at least \p length Bytes wide. | ||||
|  * \param tag       The buffer holding the authentication field. | ||||
|  *                  This must be less than 2^16 - 2^8. | ||||
|  * \param input     The buffer holding the input data. If \p length is greater | ||||
|  *                  than zero, \p input must be a readable buffer of at least | ||||
|  *                  that length. | ||||
|  * \param output    The buffer holding the output data. If \p length is greater | ||||
|  *                  than zero, \p output must be a writable buffer of at least | ||||
|  *                  that length. | ||||
|  * \param tag       The buffer holding the authentication field. This must be a | ||||
|  *                  readable buffer of at least \p tag_len Bytes. | ||||
|  * \param tag_len   The length of the authentication field in Bytes. | ||||
|  *                  0, 4, 6, 8, 10, 12, 14 or 16. | ||||
|  * | ||||
|  * \warning         Passing 0 as \p tag_len means that the message is no | ||||
|  * \warning         Passing \c 0 as \p tag_len means that the message is nos | ||||
|  *                  longer authenticated. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  |  | |||
|  | @ -24,6 +24,12 @@ | |||
| #ifndef MBEDTLS_CERTS_H | ||||
| #define MBEDTLS_CERTS_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include <stddef.h> | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
|  |  | |||
|  | @ -83,13 +83,18 @@ mbedtls_chacha20_context; | |||
|  *                  \c mbedtls_chacha20_free(). | ||||
|  * | ||||
|  * \param ctx       The ChaCha20 context to initialize. | ||||
|  *                  This must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function releases and clears the specified ChaCha20 context. | ||||
|  * \brief           This function releases and clears the specified | ||||
|  *                  ChaCha20 context. | ||||
|  * | ||||
|  * \param ctx       The ChaCha20 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 context. | ||||
|  * | ||||
|  * \param ctx       The ChaCha20 context to clear. | ||||
|  */ | ||||
| void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx ); | ||||
| 
 | ||||
|  | @ -102,7 +107,9 @@ void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx ); | |||
|  *                  \c mbedtls_chacha_update(). | ||||
|  * | ||||
|  * \param ctx       The ChaCha20 context to which the key should be bound. | ||||
|  * \param key       The encryption/decryption key. Must be 32 bytes in length. | ||||
|  *                  It must be initialized. | ||||
|  * \param key       The encryption/decryption key. This must be \c 32 Bytes | ||||
|  *                  in length. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or key is NULL. | ||||
|  | @ -121,8 +128,9 @@ int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx, | |||
|  *                  messages encrypted with the same nonce and key. | ||||
|  * | ||||
|  * \param ctx       The ChaCha20 context to which the nonce should be bound. | ||||
|  * \param nonce     The nonce. Must be 12 bytes in size. | ||||
|  * \param counter   The initial counter value. This is usually 0. | ||||
|  *                  It must be initialized and bound to a key. | ||||
|  * \param nonce     The nonce. This must be \c 12 Bytes in size. | ||||
|  * \param counter   The initial counter value. This is usually \c 0. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if ctx or nonce is | ||||
|  | @ -150,16 +158,16 @@ int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx, | |||
|  *                  key and nonce. | ||||
|  * | ||||
|  * \param ctx       The ChaCha20 context to use for encryption or decryption. | ||||
|  * \param size      The length of the input data in bytes. | ||||
|  *                  It must be initialized and bound to a key and nonce. | ||||
|  * \param size      The length of the input data in Bytes. | ||||
|  * \param input     The buffer holding the input data. | ||||
|  *                  This pointer can be NULL if size == 0. | ||||
|  *                  This pointer can be \c NULL if `size == 0`. | ||||
|  * \param output    The buffer holding the output data. | ||||
|  *                  Must be able to hold \p size bytes. | ||||
|  *                  This pointer can be NULL if size == 0. | ||||
|  *                  This must be able to hold \p size Bytes. | ||||
|  *                  This pointer can be \c NULL if `size == 0`. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if the ctx, input, or | ||||
|  *                  output pointers are NULL. | ||||
|  * \return          A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx, | ||||
|                              size_t size, | ||||
|  | @ -180,19 +188,19 @@ int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx, | |||
|  * \note            The \p input and \p output pointers must either be equal or | ||||
|  *                  point to non-overlapping buffers. | ||||
|  * | ||||
|  * \param key       The encryption/decryption key. Must be 32 bytes in length. | ||||
|  * \param nonce     The nonce. Must be 12 bytes in size. | ||||
|  * \param counter   The initial counter value. This is usually 0. | ||||
|  * \param size      The length of the input data in bytes. | ||||
|  * \param key       The encryption/decryption key. | ||||
|  *                  This must be \c 32 Bytes in length. | ||||
|  * \param nonce     The nonce. This must be \c 12 Bytes in size. | ||||
|  * \param counter   The initial counter value. This is usually \c 0. | ||||
|  * \param size      The length of the input data in Bytes. | ||||
|  * \param input     The buffer holding the input data. | ||||
|  *                  This pointer can be NULL if size == 0. | ||||
|  *                  This pointer can be \c NULL if `size == 0`. | ||||
|  * \param output    The buffer holding the output data. | ||||
|  *                  Must be able to hold \p size bytes. | ||||
|  *                  This pointer can be NULL if size == 0. | ||||
|  *                  This must be able to hold \p size Bytes. | ||||
|  *                  This pointer can be \c NULL if `size == 0`. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA if key, nonce, input, | ||||
|  *                  or output is NULL. | ||||
|  * \return          A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_chacha20_crypt( const unsigned char key[32], | ||||
|                             const unsigned char nonce[12], | ||||
|  |  | |||
|  | @ -115,27 +115,29 @@ mbedtls_chachapoly_context; | |||
|  *                  all previous outputs of \c mbedtls_chachapoly_update(), | ||||
|  *                  otherwise you can now safely use the plaintext. | ||||
|  * | ||||
|  * \param ctx       The ChachaPoly context to initialize. | ||||
|  * \param ctx       The ChachaPoly context to initialize. Must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function releases and clears the specified ChaCha20-Poly1305 context. | ||||
|  * \brief           This function releases and clears the specified | ||||
|  *                  ChaCha20-Poly1305 context. | ||||
|  * | ||||
|  * \param ctx       The ChachaPoly context to clear. | ||||
|  * \param ctx       The ChachaPoly context to clear. This may be \c NULL, in which | ||||
|  *                  case this function is a no-op. | ||||
|  */ | ||||
| void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function sets the ChaCha20-Poly1305 symmetric encryption key. | ||||
|  * \brief           This function sets the ChaCha20-Poly1305 | ||||
|  *                  symmetric encryption key. | ||||
|  * | ||||
|  * \param ctx       The ChaCha20-Poly1305 context to which the key should be | ||||
|  *                  bound. | ||||
|  * \param key       The 256-bit (32 bytes) key. | ||||
|  *                  bound. This must be initialized. | ||||
|  * \param key       The \c 256 Bit (\c 32 Bytes) key. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA | ||||
|  *                  if \p ctx or \p key are NULL. | ||||
|  * \return          A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx, | ||||
|                                const unsigned char key[32] ); | ||||
|  | @ -155,14 +157,15 @@ int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx, | |||
|  * \warning         Decryption with the piecewise API is discouraged, see the | ||||
|  *                  warning on \c mbedtls_chachapoly_init(). | ||||
|  * | ||||
|  * \param ctx       The ChaCha20-Poly1305 context. | ||||
|  * \param nonce     The nonce/IV to use for the message. Must be 12 bytes. | ||||
|  * \param ctx       The ChaCha20-Poly1305 context. This must be initialized | ||||
|  *                  and bound to a key. | ||||
|  * \param nonce     The nonce/IV to use for the message. | ||||
|  *                  This must be a redable buffer of length \c 12 Bytes. | ||||
|  * \param mode      The operation to perform: #MBEDTLS_CHACHAPOLY_ENCRYPT or | ||||
|  *                  #MBEDTLS_CHACHAPOLY_DECRYPT (discouraged, see warning). | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA | ||||
|  *                  if \p ctx or \p mac are NULL. | ||||
|  * \return          A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx, | ||||
|                                const unsigned char nonce[12], | ||||
|  | @ -193,11 +196,12 @@ int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx, | |||
|  * \warning         Decryption with the piecewise API is discouraged, see the | ||||
|  *                  warning on \c mbedtls_chachapoly_init(). | ||||
|  * | ||||
|  * \param ctx       The ChaCha20-Poly1305 context to use. | ||||
|  * \param aad_len   The length (in bytes) of the AAD. The length has no | ||||
|  * \param ctx       The ChaCha20-Poly1305 context. This must be initialized | ||||
|  *                  and bound to a key. | ||||
|  * \param aad_len   The length in Bytes of the AAD. The length has no | ||||
|  *                  restrictions. | ||||
|  * \param aad       Buffer containing the AAD. | ||||
|  *                  This pointer can be NULL if aad_len == 0. | ||||
|  *                  This pointer can be \c NULL if `aad_len == 0`. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA | ||||
|  | @ -227,20 +231,19 @@ int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx, | |||
|  * \warning         Decryption with the piecewise API is discouraged, see the | ||||
|  *                  warning on \c mbedtls_chachapoly_init(). | ||||
|  * | ||||
|  * \param ctx       The ChaCha20-Poly1305 context to use. | ||||
|  * \param ctx       The ChaCha20-Poly1305 context to use. This must be initialized. | ||||
|  * \param len       The length (in bytes) of the data to encrypt or decrypt. | ||||
|  * \param input     The buffer containing the data to encrypt or decrypt. | ||||
|  *                  This pointer can be NULL if len == 0. | ||||
|  * \param output    The buffer to where the encrypted or decrypted data is written. | ||||
|  *                  Must be able to hold \p len bytes. | ||||
|  *                  This pointer can be NULL if len == 0. | ||||
|  *                  This pointer can be \c NULL if `len == 0`. | ||||
|  * \param output    The buffer to where the encrypted or decrypted data is | ||||
|  *                  written. This must be able to hold \p len bytes. | ||||
|  *                  This pointer can be \c NULL if `len == 0`. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA | ||||
|  *                  if \p ctx, \p input, or \p output are NULL. | ||||
|  * \return          #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE | ||||
|  *                  if the operation has not been started or has been | ||||
|  *                  finished. | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx, | ||||
|                                size_t len, | ||||
|  | @ -251,18 +254,17 @@ int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx, | |||
|  * \brief           This function finished the ChaCha20-Poly1305 operation and | ||||
|  *                  generates the MAC (authentication tag). | ||||
|  * | ||||
|  * \param ctx       The ChaCha20-Poly1305 context to use. | ||||
|  * \param ctx       The ChaCha20-Poly1305 context to use. This must be initialized. | ||||
|  * \param mac       The buffer to where the 128-bit (16 bytes) MAC is written. | ||||
|  * | ||||
|  * \warning         Decryption with the piecewise API is discouraged, see the | ||||
|  *                  warning on \c mbedtls_chachapoly_init(). | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA | ||||
|  *                  if \p ctx or \p mac are NULL. | ||||
|  * \return          #MBEDTLS_ERR_CHACHAPOLY_BAD_STATE | ||||
|  *                  if the operation has not been started or has been | ||||
|  *                  finished. | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx, | ||||
|                                unsigned char mac[16] ); | ||||
|  | @ -280,20 +282,21 @@ int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx, | |||
|  *                  and key. | ||||
|  * | ||||
|  * \param ctx       The ChaCha20-Poly1305 context to use (holds the key). | ||||
|  *                  This must be initialized. | ||||
|  * \param length    The length (in bytes) of the data to encrypt or decrypt. | ||||
|  * \param nonce     The 96-bit (12 bytes) nonce/IV to use. | ||||
|  * \param aad       The buffer containing the additional authenticated data (AAD). | ||||
|  *                  This pointer can be NULL if aad_len == 0. | ||||
|  * \param aad       The buffer containing the additional authenticated | ||||
|  *                  data (AAD). This pointer can be \c NULL if `aad_len == 0`. | ||||
|  * \param aad_len   The length (in bytes) of the AAD data to process. | ||||
|  * \param input     The buffer containing the data to encrypt or decrypt. | ||||
|  *                  This pointer can be NULL if ilen == 0. | ||||
|  * \param output    The buffer to where the encrypted or decrypted data is written. | ||||
|  *                  This pointer can be NULL if ilen == 0. | ||||
|  * \param tag       The buffer to where the computed 128-bit (16 bytes) MAC is written. | ||||
|  *                  This pointer can be \c NULL if `ilen == 0`. | ||||
|  * \param output    The buffer to where the encrypted or decrypted data | ||||
|  *                  is written. This pointer can be \c NULL if `ilen == 0`. | ||||
|  * \param tag       The buffer to where the computed 128-bit (16 bytes) MAC | ||||
|  *                  is written. This must not be \c NULL. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA | ||||
|  *                  if one or more of the required parameters are NULL. | ||||
|  * \return          A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_chachapoly_encrypt_and_tag( mbedtls_chachapoly_context *ctx, | ||||
|                                         size_t length, | ||||
|  | @ -312,22 +315,22 @@ int mbedtls_chachapoly_encrypt_and_tag( mbedtls_chachapoly_context *ctx, | |||
|  *                  \c mbedtls_chachapoly_setkey(). | ||||
|  * | ||||
|  * \param ctx       The ChaCha20-Poly1305 context to use (holds the key). | ||||
|  * \param length    The length (in bytes) of the data to decrypt. | ||||
|  * \param nonce     The 96-bit (12 bytes) nonce/IV to use. | ||||
|  * \param length    The length (in Bytes) of the data to decrypt. | ||||
|  * \param nonce     The \c 96 Bit (\c 12 bytes) nonce/IV to use. | ||||
|  * \param aad       The buffer containing the additional authenticated data (AAD). | ||||
|  *                  This pointer can be NULL if aad_len == 0. | ||||
|  *                  This pointer can be \c NULL if `aad_len == 0`. | ||||
|  * \param aad_len   The length (in bytes) of the AAD data to process. | ||||
|  * \param tag       The buffer holding the authentication tag. | ||||
|  *                  This must be a readable buffer of length \c 16 Bytes. | ||||
|  * \param input     The buffer containing the data to decrypt. | ||||
|  *                  This pointer can be NULL if ilen == 0. | ||||
|  *                  This pointer can be \c NULL if `ilen == 0`. | ||||
|  * \param output    The buffer to where the decrypted data is written. | ||||
|  *                  This pointer can be NULL if ilen == 0. | ||||
|  *                  This pointer can be \c NULL if `ilen == 0`. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA | ||||
|  *                  if one or more of the required parameters are NULL. | ||||
|  * \return          #MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED | ||||
|  *                  if the data was not authentic. | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx, | ||||
|                                      size_t length, | ||||
|  |  | |||
|  | @ -50,6 +50,11 @@ | |||
|     !defined(MBEDTLS_PLATFORM_SNPRINTF_MACRO) | ||||
| #define MBEDTLS_PLATFORM_SNPRINTF_ALT | ||||
| #endif | ||||
| 
 | ||||
| #if !defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT) && \ | ||||
|     !defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO) | ||||
| #define MBEDTLS_PLATFORM_VSNPRINTF_ALT | ||||
| #endif | ||||
| #endif /* _WIN32 */ | ||||
| 
 | ||||
| #if defined(TARGET_LIKE_MBED) && \ | ||||
|  | @ -727,7 +732,7 @@ | |||
| /*
 | ||||
|  * Avoid warning from -pedantic. This is a convenient place for this | ||||
|  * workaround since this is included by every single file before the | ||||
|  * #if defined(MBEDTLS_xxx_C) that results in emtpy translation units. | ||||
|  * #if defined(MBEDTLS_xxx_C) that results in empty translation units. | ||||
|  */ | ||||
| typedef int mbedtls_iso_c_forbids_empty_translation_units; | ||||
| 
 | ||||
|  |  | |||
|  | @ -36,6 +36,7 @@ | |||
| #endif | ||||
| 
 | ||||
| #include <stddef.h> | ||||
| #include "platform_util.h" | ||||
| 
 | ||||
| #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | ||||
| #define MBEDTLS_CIPHER_MODE_AEAD | ||||
|  | @ -354,11 +355,12 @@ const int *mbedtls_cipher_list( void ); | |||
|  * \brief               This function retrieves the cipher-information | ||||
|  *                      structure associated with the given cipher name. | ||||
|  * | ||||
|  * \param cipher_name   Name of the cipher to search for. | ||||
|  * \param cipher_name   Name of the cipher to search for. This must not be | ||||
|  *                      \c NULL. | ||||
|  * | ||||
|  * \return              The cipher information structure associated with the | ||||
|  *                      given \p cipher_name. | ||||
|  * \return              NULL if the associated cipher information is not found. | ||||
|  * \return              \c NULL if the associated cipher information is not found. | ||||
|  */ | ||||
| const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name ); | ||||
| 
 | ||||
|  | @ -370,7 +372,7 @@ const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher | |||
|  * | ||||
|  * \return              The cipher information structure associated with the | ||||
|  *                      given \p cipher_type. | ||||
|  * \return              NULL if the associated cipher information is not found. | ||||
|  * \return              \c NULL if the associated cipher information is not found. | ||||
|  */ | ||||
| const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type ); | ||||
| 
 | ||||
|  | @ -386,7 +388,7 @@ const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher | |||
|  * | ||||
|  * \return              The cipher information structure associated with the | ||||
|  *                      given \p cipher_id. | ||||
|  * \return              NULL if the associated cipher information is not found. | ||||
|  * \return              \c NULL if the associated cipher information is not found. | ||||
|  */ | ||||
| const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id, | ||||
|                                               int key_bitlen, | ||||
|  | @ -394,6 +396,8 @@ const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_ciph | |||
| 
 | ||||
| /**
 | ||||
|  * \brief               This function initializes a \p cipher_context as NONE. | ||||
|  * | ||||
|  * \param ctx           The context to be initialized. This must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx ); | ||||
| 
 | ||||
|  | @ -401,6 +405,10 @@ void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx ); | |||
|  * \brief               This function frees and clears the cipher-specific | ||||
|  *                      context of \p ctx. Freeing \p ctx itself remains the | ||||
|  *                      responsibility of the caller. | ||||
|  * | ||||
|  * \param ctx           The context to be freed. If this is \c NULL, the | ||||
|  *                      function has no effect, otherwise this must point to an | ||||
|  *                      initialized context. | ||||
|  */ | ||||
| void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx ); | ||||
| 
 | ||||
|  | @ -409,7 +417,7 @@ void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx ); | |||
|  * \brief               This function initializes a cipher context for | ||||
|  *                      use with the given cipher primitive. | ||||
|  * | ||||
|  * \param ctx           The context to initialize. May not be NULL. | ||||
|  * \param ctx           The context to initialize. This must be initialized. | ||||
|  * \param cipher_info   The cipher to use. | ||||
|  * | ||||
|  * \return              \c 0 on success. | ||||
|  | @ -455,15 +463,16 @@ int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx, | |||
| /**
 | ||||
|  * \brief        This function returns the block size of the given cipher. | ||||
|  * | ||||
|  * \param ctx    The context of the cipher. Must be initialized. | ||||
|  * \param ctx    The context of the cipher. This must be initialized. | ||||
|  * | ||||
|  * \return       The size of the blocks of the cipher. | ||||
|  * \return       0 if \p ctx has not been initialized. | ||||
|  * \return       The block size of the underlying cipher. | ||||
|  * \return       \c 0 if \p ctx has not been initialized. | ||||
|  */ | ||||
| static inline unsigned int mbedtls_cipher_get_block_size( | ||||
|     const mbedtls_cipher_context_t *ctx ) | ||||
| { | ||||
|     if( NULL == ctx || NULL == ctx->cipher_info ) | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 ); | ||||
|     if( ctx->cipher_info == NULL ) | ||||
|         return 0; | ||||
| 
 | ||||
|     return ctx->cipher_info->block_size; | ||||
|  | @ -473,7 +482,7 @@ static inline unsigned int mbedtls_cipher_get_block_size( | |||
|  * \brief        This function returns the mode of operation for | ||||
|  *               the cipher. For example, MBEDTLS_MODE_CBC. | ||||
|  * | ||||
|  * \param ctx    The context of the cipher. Must be initialized. | ||||
|  * \param ctx    The context of the cipher. This must be initialized. | ||||
|  * | ||||
|  * \return       The mode of operation. | ||||
|  * \return       #MBEDTLS_MODE_NONE if \p ctx has not been initialized. | ||||
|  | @ -481,7 +490,8 @@ static inline unsigned int mbedtls_cipher_get_block_size( | |||
| static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( | ||||
|     const mbedtls_cipher_context_t *ctx ) | ||||
| { | ||||
|     if( NULL == ctx || NULL == ctx->cipher_info ) | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, MBEDTLS_MODE_NONE ); | ||||
|     if( ctx->cipher_info == NULL ) | ||||
|         return MBEDTLS_MODE_NONE; | ||||
| 
 | ||||
|     return ctx->cipher_info->mode; | ||||
|  | @ -491,7 +501,7 @@ static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( | |||
|  * \brief       This function returns the size of the IV or nonce | ||||
|  *              of the cipher, in Bytes. | ||||
|  * | ||||
|  * \param ctx   The context of the cipher. Must be initialized. | ||||
|  * \param ctx   The context of the cipher. This must be initialized. | ||||
|  * | ||||
|  * \return      The recommended IV size if no IV has been set. | ||||
|  * \return      \c 0 for ciphers not using an IV or a nonce. | ||||
|  | @ -500,7 +510,8 @@ static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( | |||
| static inline int mbedtls_cipher_get_iv_size( | ||||
|     const mbedtls_cipher_context_t *ctx ) | ||||
| { | ||||
|     if( NULL == ctx || NULL == ctx->cipher_info ) | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 ); | ||||
|     if( ctx->cipher_info == NULL ) | ||||
|         return 0; | ||||
| 
 | ||||
|     if( ctx->iv_size != 0 ) | ||||
|  | @ -512,7 +523,7 @@ static inline int mbedtls_cipher_get_iv_size( | |||
| /**
 | ||||
|  * \brief               This function returns the type of the given cipher. | ||||
|  * | ||||
|  * \param ctx           The context of the cipher. Must be initialized. | ||||
|  * \param ctx           The context of the cipher. This must be initialized. | ||||
|  * | ||||
|  * \return              The type of the cipher. | ||||
|  * \return              #MBEDTLS_CIPHER_NONE if \p ctx has not been initialized. | ||||
|  | @ -520,7 +531,9 @@ static inline int mbedtls_cipher_get_iv_size( | |||
| static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( | ||||
|     const mbedtls_cipher_context_t *ctx ) | ||||
| { | ||||
|     if( NULL == ctx || NULL == ctx->cipher_info ) | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( | ||||
|         ctx != NULL, MBEDTLS_CIPHER_NONE ); | ||||
|     if( ctx->cipher_info == NULL ) | ||||
|         return MBEDTLS_CIPHER_NONE; | ||||
| 
 | ||||
|     return ctx->cipher_info->type; | ||||
|  | @ -530,7 +543,7 @@ static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( | |||
|  * \brief               This function returns the name of the given cipher | ||||
|  *                      as a string. | ||||
|  * | ||||
|  * \param ctx           The context of the cipher. Must be initialized. | ||||
|  * \param ctx           The context of the cipher. This must be initialized. | ||||
|  * | ||||
|  * \return              The name of the cipher. | ||||
|  * \return              NULL if \p ctx has not been not initialized. | ||||
|  | @ -538,7 +551,8 @@ static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( | |||
| static inline const char *mbedtls_cipher_get_name( | ||||
|     const mbedtls_cipher_context_t *ctx ) | ||||
| { | ||||
|     if( NULL == ctx || NULL == ctx->cipher_info ) | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 ); | ||||
|     if( ctx->cipher_info == NULL ) | ||||
|         return 0; | ||||
| 
 | ||||
|     return ctx->cipher_info->name; | ||||
|  | @ -547,7 +561,7 @@ static inline const char *mbedtls_cipher_get_name( | |||
| /**
 | ||||
|  * \brief               This function returns the key length of the cipher. | ||||
|  * | ||||
|  * \param ctx           The context of the cipher. Must be initialized. | ||||
|  * \param ctx           The context of the cipher. This must be initialized. | ||||
|  * | ||||
|  * \return              The key length of the cipher in bits. | ||||
|  * \return              #MBEDTLS_KEY_LENGTH_NONE if ctx \p has not been | ||||
|  | @ -556,7 +570,9 @@ static inline const char *mbedtls_cipher_get_name( | |||
| static inline int mbedtls_cipher_get_key_bitlen( | ||||
|     const mbedtls_cipher_context_t *ctx ) | ||||
| { | ||||
|     if( NULL == ctx || NULL == ctx->cipher_info ) | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( | ||||
|         ctx != NULL, MBEDTLS_KEY_LENGTH_NONE ); | ||||
|     if( ctx->cipher_info == NULL ) | ||||
|         return MBEDTLS_KEY_LENGTH_NONE; | ||||
| 
 | ||||
|     return (int) ctx->cipher_info->key_bitlen; | ||||
|  | @ -565,7 +581,7 @@ static inline int mbedtls_cipher_get_key_bitlen( | |||
| /**
 | ||||
|  * \brief          This function returns the operation of the given cipher. | ||||
|  * | ||||
|  * \param ctx      The context of the cipher. Must be initialized. | ||||
|  * \param ctx      The context of the cipher. This must be initialized. | ||||
|  * | ||||
|  * \return         The type of operation: #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT. | ||||
|  * \return         #MBEDTLS_OPERATION_NONE if \p ctx has not been initialized. | ||||
|  | @ -573,7 +589,9 @@ static inline int mbedtls_cipher_get_key_bitlen( | |||
| static inline mbedtls_operation_t mbedtls_cipher_get_operation( | ||||
|     const mbedtls_cipher_context_t *ctx ) | ||||
| { | ||||
|     if( NULL == ctx || NULL == ctx->cipher_info ) | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( | ||||
|         ctx != NULL, MBEDTLS_OPERATION_NONE ); | ||||
|     if( ctx->cipher_info == NULL ) | ||||
|         return MBEDTLS_OPERATION_NONE; | ||||
| 
 | ||||
|     return ctx->operation; | ||||
|  | @ -582,11 +600,11 @@ static inline mbedtls_operation_t mbedtls_cipher_get_operation( | |||
| /**
 | ||||
|  * \brief               This function sets the key to use with the given context. | ||||
|  * | ||||
|  * \param ctx           The generic cipher context. May not be NULL. Must have | ||||
|  *                      been initialized using mbedtls_cipher_info_from_type() | ||||
|  *                      or mbedtls_cipher_info_from_string(). | ||||
|  * \param key           The key to use. | ||||
|  * \param key_bitlen    The key length to use, in bits. | ||||
|  * \param ctx           The generic cipher context. This must be initialized and | ||||
|  *                      bound to a cipher information structure. | ||||
|  * \param key           The key to use. This must be a readable buffer of at | ||||
|  *                      least \p key_bitlen Bits. | ||||
|  * \param key_bitlen    The key length to use, in Bits. | ||||
|  * \param operation     The operation that the key will be used for: | ||||
|  *                      #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT. | ||||
|  * | ||||
|  | @ -607,7 +625,8 @@ int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, | |||
|  * | ||||
|  *                      The default passing mode is PKCS7 padding. | ||||
|  * | ||||
|  * \param ctx           The generic cipher context. | ||||
|  * \param ctx           The generic cipher context. This must be initialized and | ||||
|  *                      bound to a cipher information structure. | ||||
|  * \param mode          The padding mode. | ||||
|  * | ||||
|  * \return              \c 0 on success. | ||||
|  | @ -627,8 +646,10 @@ int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, | |||
|  * \note            Some ciphers do not use IVs nor nonce. For these | ||||
|  *                  ciphers, this function has no effect. | ||||
|  * | ||||
|  * \param ctx       The generic cipher context. | ||||
|  * \param iv        The IV to use, or NONCE_COUNTER for CTR-mode ciphers. | ||||
|  * \param ctx       The generic cipher context. This must be initialized and | ||||
|  *                  bound to a cipher information structure. | ||||
|  * \param iv        The IV to use, or NONCE_COUNTER for CTR-mode ciphers. This | ||||
|  *                  must be a readable buffer of at least \p iv_len Bytes. | ||||
|  * \param iv_len    The IV length for ciphers with variable-size IV. | ||||
|  *                  This parameter is discarded by ciphers with fixed-size IV. | ||||
|  * | ||||
|  | @ -637,12 +658,13 @@ int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, | |||
|  *                  parameter-verification failure. | ||||
|  */ | ||||
| int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx, | ||||
|                            const unsigned char *iv, size_t iv_len ); | ||||
|                            const unsigned char *iv, | ||||
|                            size_t iv_len ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief         This function resets the cipher state. | ||||
|  * | ||||
|  * \param ctx     The generic cipher context. | ||||
|  * \param ctx     The generic cipher context. This must be initialized. | ||||
|  * | ||||
|  * \return        \c 0 on success. | ||||
|  * \return        #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on | ||||
|  | @ -652,16 +674,18 @@ int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx ); | |||
| 
 | ||||
| #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | ||||
| /**
 | ||||
|  * \brief             This function adds additional data for AEAD ciphers. | ||||
|  *                    Currently supported with GCM and ChaCha20+Poly1305. | ||||
|  *                    Must be called exactly once, after mbedtls_cipher_reset(). | ||||
|  * \brief               This function adds additional data for AEAD ciphers. | ||||
|  *                      Currently supported with GCM and ChaCha20+Poly1305. | ||||
|  *                      This must be called exactly once, after | ||||
|  *                      mbedtls_cipher_reset(). | ||||
|  * | ||||
|  * \param ctx         The generic cipher context. | ||||
|  * \param ad          The additional data to use. | ||||
|  * \param ad_len      the Length of \p ad. | ||||
|  * \param ctx           The generic cipher context. This must be initialized. | ||||
|  * \param ad            The additional data to use. This must be a readable | ||||
|  *                      buffer of at least \p ad_len Bytes. | ||||
|  * \param ad_len        The length of \p ad in Bytes. | ||||
|  * | ||||
|  * \return            \c 0 on success. | ||||
|  * \return            A specific error code on failure. | ||||
|  * \return              \c 0 on success. | ||||
|  * \return              A specific error code on failure. | ||||
|  */ | ||||
| int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx, | ||||
|                       const unsigned char *ad, size_t ad_len ); | ||||
|  | @ -682,14 +706,17 @@ int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx, | |||
|  *                      mbedtls_cipher_finish(), must have \p ilen as a | ||||
|  *                      multiple of the block size of the cipher. | ||||
|  * | ||||
|  * \param ctx           The generic cipher context. | ||||
|  * \param input         The buffer holding the input data. | ||||
|  * \param ctx           The generic cipher context. This must be initialized and | ||||
|  *                      bound to a key. | ||||
|  * \param input         The buffer holding the input data. This must be a | ||||
|  *                      readable buffer of at least \p ilen Bytes. | ||||
|  * \param ilen          The length of the input data. | ||||
|  * \param output        The buffer for the output data. Must be able to hold at | ||||
|  *                      least \p ilen + block_size. Must not be the same buffer | ||||
|  *                      as input. | ||||
|  * \param output        The buffer for the output data. This must be able to | ||||
|  *                      hold at least `ilen + block_size`. This must not be the | ||||
|  *                      same buffer as \p input. | ||||
|  * \param olen          The length of the output data, to be updated with the | ||||
|  *                      actual number of Bytes written. | ||||
|  *                      actual number of Bytes written. This must not be | ||||
|  *                      \c NULL. | ||||
|  * | ||||
|  * \return              \c 0 on success. | ||||
|  * \return              #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on | ||||
|  | @ -709,9 +736,12 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, | |||
|  *                      contained in it is padded to the size of | ||||
|  *                      the last block, and written to the \p output buffer. | ||||
|  * | ||||
|  * \param ctx           The generic cipher context. | ||||
|  * \param output        The buffer to write data to. Needs block_size available. | ||||
|  * \param ctx           The generic cipher context. This must be initialized and | ||||
|  *                      bound to a key. | ||||
|  * \param output        The buffer to write data to. This needs to be a writable | ||||
|  *                      buffer of at least \p block_size Bytes. | ||||
|  * \param olen          The length of the data written to the \p output buffer. | ||||
|  *                      This may not be \c NULL. | ||||
|  * | ||||
|  * \return              \c 0 on success. | ||||
|  * \return              #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on | ||||
|  | @ -729,10 +759,14 @@ int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx, | |||
| /**
 | ||||
|  * \brief               This function writes a tag for AEAD ciphers. | ||||
|  *                      Currently supported with GCM and ChaCha20+Poly1305. | ||||
|  *                      Must be called after mbedtls_cipher_finish(). | ||||
|  *                      This must be called after mbedtls_cipher_finish(). | ||||
|  * | ||||
|  * \param ctx           The generic cipher context. | ||||
|  * \param tag           The buffer to write the tag to. | ||||
|  * \param ctx           The generic cipher context. This must be initialized, | ||||
|  *                      bound to a key, and have just completed a cipher | ||||
|  *                      operation through mbedtls_cipher_finish() the tag for | ||||
|  *                      which should be written. | ||||
|  * \param tag           The buffer to write the tag to. This must be a writable | ||||
|  *                      buffer of at least \p tag_len Bytes. | ||||
|  * \param tag_len       The length of the tag to write. | ||||
|  * | ||||
|  * \return              \c 0 on success. | ||||
|  | @ -744,10 +778,11 @@ int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx, | |||
| /**
 | ||||
|  * \brief               This function checks the tag for AEAD ciphers. | ||||
|  *                      Currently supported with GCM and ChaCha20+Poly1305. | ||||
|  *                      Must be called after mbedtls_cipher_finish(). | ||||
|  *                      This must be called after mbedtls_cipher_finish(). | ||||
|  * | ||||
|  * \param ctx           The generic cipher context. | ||||
|  * \param tag           The buffer holding the tag. | ||||
|  * \param ctx           The generic cipher context. This must be initialized. | ||||
|  * \param tag           The buffer holding the tag. This must be a readable | ||||
|  *                      buffer of at least \p tag_len Bytes. | ||||
|  * \param tag_len       The length of the tag to check. | ||||
|  * | ||||
|  * \return              \c 0 on success. | ||||
|  | @ -761,18 +796,22 @@ int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx, | |||
|  * \brief               The generic all-in-one encryption/decryption function, | ||||
|  *                      for all ciphers except AEAD constructs. | ||||
|  * | ||||
|  * \param ctx           The generic cipher context. | ||||
|  * \param ctx           The generic cipher context. This must be initialized. | ||||
|  * \param iv            The IV to use, or NONCE_COUNTER for CTR-mode ciphers. | ||||
|  *                      This must be a readable buffer of at least \p iv_len | ||||
|  *                      Bytes. | ||||
|  * \param iv_len        The IV length for ciphers with variable-size IV. | ||||
|  *                      This parameter is discarded by ciphers with fixed-size | ||||
|  *                      IV. | ||||
|  * \param input         The buffer holding the input data. | ||||
|  * \param ilen          The length of the input data. | ||||
|  * \param output        The buffer for the output data. Must be able to hold at | ||||
|  *                      least \p ilen + block_size. Must not be the same buffer | ||||
|  *                      as input. | ||||
|  * \param input         The buffer holding the input data. This must be a | ||||
|  *                      readable buffer of at least \p ilen Bytes. | ||||
|  * \param ilen          The length of the input data in Bytes. | ||||
|  * \param output        The buffer for the output data. This must be able to | ||||
|  *                      hold at least `ilen + block_size`. This must not be the | ||||
|  *                      same buffer as \p input. | ||||
|  * \param olen          The length of the output data, to be updated with the | ||||
|  *                      actual number of Bytes written. | ||||
|  *                      actual number of Bytes written. This must not be | ||||
|  *                      \c NULL. | ||||
|  * | ||||
|  * \note                Some ciphers do not use IVs nor nonce. For these | ||||
|  *                      ciphers, use \p iv = NULL and \p iv_len = 0. | ||||
|  | @ -793,27 +832,34 @@ int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx, | |||
| 
 | ||||
| #if defined(MBEDTLS_CIPHER_MODE_AEAD) | ||||
| /**
 | ||||
|  * \brief             The generic autenticated encryption (AEAD) function. | ||||
|  * \brief               The generic autenticated encryption (AEAD) function. | ||||
|  * | ||||
|  * \param ctx         The generic cipher context. | ||||
|  * \param iv          The IV to use, or NONCE_COUNTER for CTR-mode ciphers. | ||||
|  * \param iv_len      The IV length for ciphers with variable-size IV. | ||||
|  *                    This parameter is discarded by ciphers with fixed-size IV. | ||||
|  * \param ad          The additional data to authenticate. | ||||
|  * \param ad_len      The length of \p ad. | ||||
|  * \param input       The buffer holding the input data. | ||||
|  * \param ilen        The length of the input data. | ||||
|  * \param output      The buffer for the output data. | ||||
|  *                    Must be able to hold at least \p ilen. | ||||
|  * \param olen        The length of the output data, to be updated with the | ||||
|  *                    actual number of Bytes written. | ||||
|  * \param tag         The buffer for the authentication tag. | ||||
|  * \param tag_len     The desired length of the authentication tag. | ||||
|  * \param ctx           The generic cipher context. This must be initialized and | ||||
|  *                      bound to a key. | ||||
|  * \param iv            The IV to use, or NONCE_COUNTER for CTR-mode ciphers. | ||||
|  *                      This must be a readable buffer of at least \p iv_len | ||||
|  *                      Bytes. | ||||
|  * \param iv_len        The IV length for ciphers with variable-size IV. | ||||
|  *                      This parameter is discarded by ciphers with fixed-size IV. | ||||
|  * \param ad            The additional data to authenticate. This must be a | ||||
|  *                      readable buffer of at least \p ad_len Bytes. | ||||
|  * \param ad_len        The length of \p ad. | ||||
|  * \param input         The buffer holding the input data. This must be a | ||||
|  *                      readable buffer of at least \p ilen Bytes. | ||||
|  * \param ilen          The length of the input data. | ||||
|  * \param output        The buffer for the output data. This must be able to | ||||
|  *                      hold at least \p ilen Bytes. | ||||
|  * \param olen          The length of the output data, to be updated with the | ||||
|  *                      actual number of Bytes written. This must not be | ||||
|  *                      \c NULL. | ||||
|  * \param tag           The buffer for the authentication tag. This must be a | ||||
|  *                      writable buffer of at least \p tag_len Bytes. | ||||
|  * \param tag_len       The desired length of the authentication tag. | ||||
|  * | ||||
|  * \return            \c 0 on success. | ||||
|  * \return            #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on | ||||
|  *                    parameter-verification failure. | ||||
|  * \return            A cipher-specific error code on failure. | ||||
|  * \return              \c 0 on success. | ||||
|  * \return              #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on | ||||
|  *                      parameter-verification failure. | ||||
|  * \return              A cipher-specific error code on failure. | ||||
|  */ | ||||
| int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx, | ||||
|                          const unsigned char *iv, size_t iv_len, | ||||
|  | @ -823,32 +869,39 @@ int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx, | |||
|                          unsigned char *tag, size_t tag_len ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief             The generic autenticated decryption (AEAD) function. | ||||
|  * \brief               The generic autenticated decryption (AEAD) function. | ||||
|  * | ||||
|  * \note              If the data is not authentic, then the output buffer | ||||
|  *                    is zeroed out to prevent the unauthentic plaintext being | ||||
|  *                    used, making this interface safer. | ||||
|  * \note                If the data is not authentic, then the output buffer | ||||
|  *                      is zeroed out to prevent the unauthentic plaintext being | ||||
|  *                      used, making this interface safer. | ||||
|  * | ||||
|  * \param ctx         The generic cipher context. | ||||
|  * \param iv          The IV to use, or NONCE_COUNTER for CTR-mode ciphers. | ||||
|  * \param iv_len      The IV length for ciphers with variable-size IV. | ||||
|  *                    This parameter is discarded by ciphers with fixed-size IV. | ||||
|  * \param ad          The additional data to be authenticated. | ||||
|  * \param ad_len      The length of \p ad. | ||||
|  * \param input       The buffer holding the input data. | ||||
|  * \param ilen        The length of the input data. | ||||
|  * \param output      The buffer for the output data. | ||||
|  *                    Must be able to hold at least \p ilen. | ||||
|  * \param olen        The length of the output data, to be updated with the | ||||
|  *                    actual number of Bytes written. | ||||
|  * \param tag         The buffer holding the authentication tag. | ||||
|  * \param tag_len     The length of the authentication tag. | ||||
|  * \param ctx           The generic cipher context. This must be initialized and | ||||
|  *                      and bound to a key. | ||||
|  * \param iv            The IV to use, or NONCE_COUNTER for CTR-mode ciphers. | ||||
|  *                      This must be a readable buffer of at least \p iv_len | ||||
|  *                      Bytes. | ||||
|  * \param iv_len        The IV length for ciphers with variable-size IV. | ||||
|  *                      This parameter is discarded by ciphers with fixed-size IV. | ||||
|  * \param ad            The additional data to be authenticated. This must be a | ||||
|  *                      readable buffer of at least \p ad_len Bytes. | ||||
|  * \param ad_len        The length of \p ad. | ||||
|  * \param input         The buffer holding the input data. This must be a | ||||
|  *                      readable buffer of at least \p ilen Bytes. | ||||
|  * \param ilen          The length of the input data. | ||||
|  * \param output        The buffer for the output data. | ||||
|  *                      This must be able to hold at least \p ilen Bytes. | ||||
|  * \param olen          The length of the output data, to be updated with the | ||||
|  *                      actual number of Bytes written. This must not be | ||||
|  *                      \c NULL. | ||||
|  * \param tag           The buffer holding the authentication tag. This must be | ||||
|  *                      a readable buffer of at least \p tag_len Bytes. | ||||
|  * \param tag_len       The length of the authentication tag. | ||||
|  * | ||||
|  * \return            \c 0 on success. | ||||
|  * \return            #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on | ||||
|  *                    parameter-verification failure. | ||||
|  * \return            #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic. | ||||
|  * \return            A cipher-specific error code on failure. | ||||
|  * \return              \c 0 on success. | ||||
|  * \return              #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on | ||||
|  *                      parameter-verification failure. | ||||
|  * \return              #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic. | ||||
|  * \return              A cipher-specific error code on failure. | ||||
|  */ | ||||
| int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx, | ||||
|                          const unsigned char *iv, size_t iv_len, | ||||
|  |  | |||
|  | @ -28,6 +28,12 @@ | |||
| #ifndef MBEDTLS_CMAC_H | ||||
| #define MBEDTLS_CMAC_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "cipher.h" | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
|  |  | |||
|  | @ -25,6 +25,12 @@ | |||
|  *  This file is part of mbed TLS (https://tls.mbed.org)
 | ||||
|  */ | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #if ! defined(MBEDTLS_DEPRECATED_REMOVED) | ||||
| 
 | ||||
| #if defined(MBEDTLS_DEPRECATED_WARNING) | ||||
|  |  | |||
|  | @ -139,7 +139,7 @@ | |||
|  * | ||||
|  * System has time.h, time(), and an implementation for | ||||
|  * mbedtls_platform_gmtime_r() (see below). | ||||
|  * The time needs to be correct (not necesarily very accurate, but at least | ||||
|  * The time needs to be correct (not necessarily very accurate, but at least | ||||
|  * the date should be correct). This is used to verify the validity period of | ||||
|  * X.509 certificates. | ||||
|  * | ||||
|  | @ -226,6 +226,7 @@ | |||
| //#define MBEDTLS_PLATFORM_FPRINTF_ALT
 | ||||
| //#define MBEDTLS_PLATFORM_PRINTF_ALT
 | ||||
| //#define MBEDTLS_PLATFORM_SNPRINTF_ALT
 | ||||
| //#define MBEDTLS_PLATFORM_VSNPRINTF_ALT
 | ||||
| //#define MBEDTLS_PLATFORM_NV_SEED_ALT
 | ||||
| //#define MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT
 | ||||
| 
 | ||||
|  | @ -256,6 +257,48 @@ | |||
|  */ | ||||
| //#define MBEDTLS_DEPRECATED_REMOVED
 | ||||
| 
 | ||||
| /**
 | ||||
|  * \def MBEDTLS_CHECK_PARAMS | ||||
|  * | ||||
|  * This configuration option controls whether the library validates more of | ||||
|  * the parameters passed to it. | ||||
|  * | ||||
|  * When this flag is not defined, the library only attempts to validate an | ||||
|  * input parameter if: (1) they may come from the outside world (such as the | ||||
|  * network, the filesystem, etc.) or (2) not validating them could result in | ||||
|  * internal memory errors such as overflowing a buffer controlled by the | ||||
|  * library. On the other hand, it doesn't attempt to validate parameters whose | ||||
|  * values are fully controlled by the application (such as pointers). | ||||
|  * | ||||
|  * When this flag is defined, the library additionally attempts to validate | ||||
|  * parameters that are fully controlled by the application, and should always | ||||
|  * be valid if the application code is fully correct and trusted. | ||||
|  * | ||||
|  * For example, when a function accepts as input a pointer to a buffer that may | ||||
|  * contain untrusted data, and its documentation mentions that this pointer | ||||
|  * must not be NULL: | ||||
|  * - the pointer is checked to be non-NULL only if this option is enabled | ||||
|  * - the content of the buffer is always validated | ||||
|  * | ||||
|  * When this flag is defined, if a library function receives a parameter that | ||||
|  * is invalid, it will: | ||||
|  * - invoke the macro MBEDTLS_PARAM_FAILED() which by default expands to a | ||||
|  *   call to the function mbedtls_param_failed() | ||||
|  * - immediately return (with a specific error code unless the function | ||||
|  *   returns void and can't communicate an error). | ||||
|  * | ||||
|  * When defining this flag, you also need to: | ||||
|  * - either provide a definition of the function mbedtls_param_failed() in | ||||
|  *   your application (see platform_util.h for its prototype) as the library | ||||
|  *   calls that function, but does not provide a default definition for it, | ||||
|  * - or provide a different definition of the macro MBEDTLS_PARAM_FAILED() | ||||
|  *   below if the above mechanism is not flexible enough to suit your needs. | ||||
|  *   See the documentation of this macro later in this file. | ||||
|  * | ||||
|  * Uncomment to enable validation of application-controlled parameters. | ||||
|  */ | ||||
| //#define MBEDTLS_CHECK_PARAMS
 | ||||
| 
 | ||||
| /* \} name SECTION: System support */ | ||||
| 
 | ||||
| /**
 | ||||
|  | @ -359,7 +402,7 @@ | |||
|  * \note Because of a signature change, the core AES encryption and decryption routines are | ||||
|  *       currently named mbedtls_aes_internal_encrypt and mbedtls_aes_internal_decrypt, | ||||
|  *       respectively. When setting up alternative implementations, these functions should | ||||
|  *       be overriden, but the wrapper functions mbedtls_aes_decrypt and mbedtls_aes_encrypt | ||||
|  *       be overridden, but the wrapper functions mbedtls_aes_decrypt and mbedtls_aes_encrypt | ||||
|  *       must stay untouched. | ||||
|  * | ||||
|  * \note If you use the AES_xxx_ALT macros, then is is recommended to also set | ||||
|  | @ -1530,7 +1573,7 @@ | |||
|  * \def MBEDTLS_SSL_SESSION_TICKETS | ||||
|  * | ||||
|  * Enable support for RFC 5077 session tickets in SSL. | ||||
|  * Client-side, provides full support for session tickets (maintainance of a | ||||
|  * Client-side, provides full support for session tickets (maintenance of a | ||||
|  * session store remains the responsibility of the application, though). | ||||
|  * Server-side, you also need to provide callbacks for writing and parsing | ||||
|  * tickets, including authenticated encryption and key management. Example | ||||
|  | @ -1714,7 +1757,7 @@ | |||
|  * | ||||
|  * \warning TLS-level compression MAY REDUCE SECURITY! See for example the | ||||
|  * CRIME attack. Before enabling this option, you should examine with care if | ||||
|  * CRIME or similar exploits may be a applicable to your use case. | ||||
|  * CRIME or similar exploits may be applicable to your use case. | ||||
|  * | ||||
|  * \note Currently compression can't be used with DTLS. | ||||
|  * | ||||
|  | @ -3078,7 +3121,7 @@ | |||
| //#define MBEDTLS_PLATFORM_STD_TIME            time /**< Default time to use, can be undefined. MBEDTLS_HAVE_TIME must be enabled */
 | ||||
| //#define MBEDTLS_PLATFORM_STD_FPRINTF      fprintf /**< Default fprintf to use, can be undefined */
 | ||||
| //#define MBEDTLS_PLATFORM_STD_PRINTF        printf /**< Default printf to use, can be undefined */
 | ||||
| /* Note: your snprintf must correclty zero-terminate the buffer! */ | ||||
| /* Note: your snprintf must correctly zero-terminate the buffer! */ | ||||
| //#define MBEDTLS_PLATFORM_STD_SNPRINTF    snprintf /**< Default snprintf to use, can be undefined */
 | ||||
| //#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS       0 /**< Default exit value to use, can be undefined */
 | ||||
| //#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE       1 /**< Default exit value to use, can be undefined */
 | ||||
|  | @ -3095,11 +3138,42 @@ | |||
| //#define MBEDTLS_PLATFORM_TIME_TYPE_MACRO       time_t /**< Default time macro to use, can be undefined. MBEDTLS_HAVE_TIME must be enabled */
 | ||||
| //#define MBEDTLS_PLATFORM_FPRINTF_MACRO      fprintf /**< Default fprintf macro to use, can be undefined */
 | ||||
| //#define MBEDTLS_PLATFORM_PRINTF_MACRO        printf /**< Default printf macro to use, can be undefined */
 | ||||
| /* Note: your snprintf must correclty zero-terminate the buffer! */ | ||||
| /* Note: your snprintf must correctly zero-terminate the buffer! */ | ||||
| //#define MBEDTLS_PLATFORM_SNPRINTF_MACRO    snprintf /**< Default snprintf macro to use, can be undefined */
 | ||||
| //#define MBEDTLS_PLATFORM_VSNPRINTF_MACRO    vsnprintf /**< Default vsnprintf macro to use, can be undefined */
 | ||||
| //#define MBEDTLS_PLATFORM_NV_SEED_READ_MACRO   mbedtls_platform_std_nv_seed_read /**< Default nv_seed_read function to use, can be undefined */
 | ||||
| //#define MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO  mbedtls_platform_std_nv_seed_write /**< Default nv_seed_write function to use, can be undefined */
 | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief       This macro is invoked by the library when an invalid parameter | ||||
|  *              is detected that is only checked with MBEDTLS_CHECK_PARAMS | ||||
|  *              (see the documentation of that option for context). | ||||
|  * | ||||
|  *              When you leave this undefined here, a default definition is | ||||
|  *              provided that invokes the function mbedtls_param_failed(), | ||||
|  *              which is declared in platform_util.h for the benefit of the | ||||
|  *              library, but that you need to define in your application. | ||||
|  * | ||||
|  *              When you define this here, this replaces the default | ||||
|  *              definition in platform_util.h (which no longer declares the | ||||
|  *              function mbedtls_param_failed()) and it is your responsibility | ||||
|  *              to make sure this macro expands to something suitable (in | ||||
|  *              particular, that all the necessary declarations are visible | ||||
|  *              from within the library - you can ensure that by providing | ||||
|  *              them in this file next to the macro definition). | ||||
|  * | ||||
|  *              Note that you may define this macro to expand to nothing, in | ||||
|  *              which case you don't have to worry about declarations or | ||||
|  *              definitions. However, you will then be notified about invalid | ||||
|  *              parameters only in non-void functions, and void function will | ||||
|  *              just silently return early on invalid parameters, which | ||||
|  *              partially negates the benefits of enabling | ||||
|  *              #MBEDTLS_CHECK_PARAMS in the first place, so is discouraged. | ||||
|  * | ||||
|  * \param cond  The expression that should evaluate to true, but doesn't. | ||||
|  */ | ||||
| //#define MBEDTLS_PARAM_FAILED( cond )               assert( cond )
 | ||||
| 
 | ||||
| /* SSL Cache options */ | ||||
| //#define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT       86400 /**< 1 day  */
 | ||||
| //#define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES      50 /**< Maximum entries in cache */
 | ||||
|  | @ -3108,31 +3182,65 @@ | |||
| 
 | ||||
| /** \def MBEDTLS_SSL_MAX_CONTENT_LEN
 | ||||
|  * | ||||
|  * Maximum fragment length in bytes. | ||||
|  * Maximum length (in bytes) of incoming and outgoing plaintext fragments. | ||||
|  * | ||||
|  * Determines the size of both the incoming and outgoing TLS I/O buffers. | ||||
|  * This determines the size of both the incoming and outgoing TLS I/O buffers | ||||
|  * in such a way that both are capable of holding the specified amount of | ||||
|  * plaintext data, regardless of the protection mechanism used. | ||||
|  * | ||||
|  * Uncommenting MBEDTLS_SSL_IN_CONTENT_LEN and/or MBEDTLS_SSL_OUT_CONTENT_LEN | ||||
|  * will override this length by setting maximum incoming and/or outgoing | ||||
|  * fragment length, respectively. | ||||
|  * To configure incoming and outgoing I/O buffers separately, use | ||||
|  * #MBEDTLS_SSL_IN_CONTENT_LEN and #MBEDTLS_SSL_OUT_CONTENT_LEN, | ||||
|  * which overwrite the value set by this option. | ||||
|  * | ||||
|  * \note When using a value less than the default of 16KB on the client, it is | ||||
|  *       recommended to use the Maximum Fragment Length (MFL) extension to | ||||
|  *       inform the server about this limitation. On the server, there | ||||
|  *       is no supported, standardized way of informing the client about | ||||
|  *       restriction on the maximum size of incoming messages, and unless | ||||
|  *       the limitation has been communicated by other means, it is recommended | ||||
|  *       to only change the outgoing buffer size #MBEDTLS_SSL_OUT_CONTENT_LEN | ||||
|  *       while keeping the default value of 16KB for the incoming buffer. | ||||
|  * | ||||
|  * Uncomment to set the maximum plaintext size of both | ||||
|  * incoming and outgoing I/O buffers. | ||||
|  */ | ||||
| //#define MBEDTLS_SSL_MAX_CONTENT_LEN             16384
 | ||||
| 
 | ||||
| /** \def MBEDTLS_SSL_IN_CONTENT_LEN
 | ||||
|  * | ||||
|  * Maximum incoming fragment length in bytes. | ||||
|  * Maximum length (in bytes) of incoming plaintext fragments. | ||||
|  * | ||||
|  * Uncomment to set the size of the inward TLS buffer independently of the | ||||
|  * outward buffer. | ||||
|  * This determines the size of the incoming TLS I/O buffer in such a way | ||||
|  * that it is capable of holding the specified amount of plaintext data, | ||||
|  * regardless of the protection mechanism used. | ||||
|  * | ||||
|  * If this option is undefined, it inherits its value from | ||||
|  * #MBEDTLS_SSL_MAX_CONTENT_LEN. | ||||
|  * | ||||
|  * \note When using a value less than the default of 16KB on the client, it is | ||||
|  *       recommended to use the Maximum Fragment Length (MFL) extension to | ||||
|  *       inform the server about this limitation. On the server, there | ||||
|  *       is no supported, standardized way of informing the client about | ||||
|  *       restriction on the maximum size of incoming messages, and unless | ||||
|  *       the limitation has been communicated by other means, it is recommended | ||||
|  *       to only change the outgoing buffer size #MBEDTLS_SSL_OUT_CONTENT_LEN | ||||
|  *       while keeping the default value of 16KB for the incoming buffer. | ||||
|  * | ||||
|  * Uncomment to set the maximum plaintext size of the incoming I/O buffer | ||||
|  * independently of the outgoing I/O buffer. | ||||
|  */ | ||||
| //#define MBEDTLS_SSL_IN_CONTENT_LEN              16384
 | ||||
| 
 | ||||
| /** \def MBEDTLS_SSL_OUT_CONTENT_LEN
 | ||||
|  * | ||||
|  * Maximum outgoing fragment length in bytes. | ||||
|  * Maximum length (in bytes) of outgoing plaintext fragments. | ||||
|  * | ||||
|  * Uncomment to set the size of the outward TLS buffer independently of the | ||||
|  * inward buffer. | ||||
|  * This determines the size of the outgoing TLS I/O buffer in such a way | ||||
|  * that it is capable of holding the specified amount of plaintext data, | ||||
|  * regardless of the protection mechanism used. | ||||
|  * | ||||
|  * If this option undefined, it inherits its value from | ||||
|  * #MBEDTLS_SSL_MAX_CONTENT_LEN. | ||||
|  * | ||||
|  * It is possible to save RAM by setting a smaller outward buffer, while keeping | ||||
|  * the default inward 16384 byte buffer to conform to the TLS specification. | ||||
|  | @ -3142,11 +3250,8 @@ | |||
|  * The specific size requirement depends on the configured ciphers and any | ||||
|  * certificate data which is sent during the handshake. | ||||
|  * | ||||
|  * For absolute minimum RAM usage, it's best to enable | ||||
|  * MBEDTLS_SSL_MAX_FRAGMENT_LENGTH and reduce MBEDTLS_SSL_MAX_CONTENT_LEN. This | ||||
|  * reduces both incoming and outgoing buffer sizes. However this is only | ||||
|  * guaranteed if the other end of the connection also supports the TLS | ||||
|  * max_fragment_len extension. Otherwise the connection may fail. | ||||
|  * Uncomment to set the maximum plaintext size of the outgoing I/O buffer | ||||
|  * independently of the incoming I/O buffer. | ||||
|  */ | ||||
| //#define MBEDTLS_SSL_OUT_CONTENT_LEN             16384
 | ||||
| 
 | ||||
|  |  | |||
|  | @ -36,6 +36,12 @@ | |||
| #ifndef MBEDTLS_CTR_DRBG_H | ||||
| #define MBEDTLS_CTR_DRBG_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "aes.h" | ||||
| 
 | ||||
| #if defined(MBEDTLS_THREADING_C) | ||||
|  | @ -350,6 +356,8 @@ int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char | |||
| int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path ); | ||||
| #endif /* MBEDTLS_FS_IO */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief               The CTR_DRBG checkup routine. | ||||
|  * | ||||
|  | @ -358,6 +366,8 @@ int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char | |||
|  */ | ||||
| int mbedtls_ctr_drbg_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| /* Internal functions (do not call directly) */ | ||||
| int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *, | ||||
|                                int (*)(void *, unsigned char *, size_t), void *, | ||||
|  |  | |||
|  | @ -338,6 +338,8 @@ int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx, | |||
| void mbedtls_des_setkey( uint32_t SK[32], | ||||
|                          const unsigned char key[MBEDTLS_DES_KEY_SIZE] ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          Checkup routine | ||||
|  * | ||||
|  | @ -345,6 +347,8 @@ void mbedtls_des_setkey( uint32_t SK[32], | |||
|  */ | ||||
| int mbedtls_des_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
|  | @ -127,9 +127,15 @@ mbedtls_dhm_context; | |||
| void mbedtls_dhm_init( mbedtls_dhm_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          This function parses the ServerKeyExchange parameters. | ||||
|  * \brief          This function parses the DHM parameters in a | ||||
|  *                 TLS ServerKeyExchange handshake message | ||||
|  *                 (DHM modulus, generator, and public key). | ||||
|  * | ||||
|  * \param ctx      The DHM context. | ||||
|  * \note           In a TLS handshake, this is the how the client | ||||
|  *                 sets up its DHM context from the server's public | ||||
|  *                 DHM key material. | ||||
|  * | ||||
|  * \param ctx      The DHM context to use. This must be initialized. | ||||
|  * \param p        On input, *p must be the start of the input buffer. | ||||
|  *                 On output, *p is updated to point to the end of the data | ||||
|  *                 that has been read. On success, this is the first byte | ||||
|  | @ -143,31 +149,37 @@ void mbedtls_dhm_init( mbedtls_dhm_context *ctx ); | |||
|  * \return         An \c MBEDTLS_ERR_DHM_XXX error code on failure. | ||||
|  */ | ||||
| int mbedtls_dhm_read_params( mbedtls_dhm_context *ctx, | ||||
|                      unsigned char **p, | ||||
|                      const unsigned char *end ); | ||||
|                              unsigned char **p, | ||||
|                              const unsigned char *end ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          This function sets up and writes the ServerKeyExchange | ||||
|  *                 parameters. | ||||
|  * \brief          This function generates a DHM key pair and exports its | ||||
|  *                 public part together with the DHM parameters in the format | ||||
|  *                 used in a TLS ServerKeyExchange handshake message. | ||||
|  * | ||||
|  * \note           The destination buffer must be large enough to hold | ||||
|  *                 the reduced binary presentation of the modulus, the generator | ||||
|  *                 and the public key, each wrapped with a 2-byte length field. | ||||
|  *                 It is the responsibility of the caller to ensure that enough | ||||
|  *                 space is available. Refer to \c mbedtls_mpi_size to computing | ||||
|  *                 the byte-size of an MPI. | ||||
|  * | ||||
|  * \note           This function assumes that \c ctx->P and \c ctx->G | ||||
|  *                 have already been properly set. For that, use | ||||
|  * \note           This function assumes that the DHM parameters \c ctx->P | ||||
|  *                 and \c ctx->G have already been properly set. For that, use | ||||
|  *                 mbedtls_dhm_set_group() below in conjunction with | ||||
|  *                 mbedtls_mpi_read_binary() and mbedtls_mpi_read_string(). | ||||
|  * | ||||
|  * \param ctx      The DHM context. | ||||
|  * \note           In a TLS handshake, this is the how the server generates | ||||
|  *                 and exports its DHM key material. | ||||
|  * | ||||
|  * \param ctx      The DHM context to use. This must be initialized | ||||
|  *                 and have the DHM parameters set. It may or may not | ||||
|  *                 already have imported the peer's public key. | ||||
|  * \param x_size   The private key size in Bytes. | ||||
|  * \param olen     The number of characters written. | ||||
|  * \param output   The destination buffer. | ||||
|  * \param f_rng    The RNG function. | ||||
|  * \param p_rng    The RNG context. | ||||
|  * \param olen     The address at which to store the number of Bytes | ||||
|  *                 written on success. This must not be \c NULL. | ||||
|  * \param output   The destination buffer. This must be a writable buffer of | ||||
|  *                 sufficient size to hold the reduced binary presentation of | ||||
|  *                 the modulus, the generator and the public key, each wrapped | ||||
|  *                 with a 2-byte length field. It is the responsibility of the | ||||
|  *                 caller to ensure that enough space is available. Refer to | ||||
|  *                 mbedtls_mpi_size() to computing the byte-size of an MPI. | ||||
|  * \param f_rng    The RNG function. Must not be \c NULL. | ||||
|  * \param p_rng    The RNG context to be passed to \p f_rng. This may be | ||||
|  *                 \c NULL if \p f_rng doesn't need a context parameter. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         An \c MBEDTLS_ERR_DHM_XXX error code on failure. | ||||
|  | @ -180,12 +192,14 @@ int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size, | |||
| /**
 | ||||
|  * \brief          This function sets the prime modulus and generator. | ||||
|  * | ||||
|  * \note           This function can be used to set \p P, \p G | ||||
|  * \note           This function can be used to set \c ctx->P, \c ctx->G | ||||
|  *                 in preparation for mbedtls_dhm_make_params(). | ||||
|  * | ||||
|  * \param ctx      The DHM context. | ||||
|  * \param P        The MPI holding the DHM prime modulus. | ||||
|  * \param G        The MPI holding the DHM generator. | ||||
|  * \param ctx      The DHM context to configure. This must be initialized. | ||||
|  * \param P        The MPI holding the DHM prime modulus. This must be | ||||
|  *                 an initialized MPI. | ||||
|  * \param G        The MPI holding the DHM generator. This must be an | ||||
|  *                 initialized MPI. | ||||
|  * | ||||
|  * \return         \c 0 if successful. | ||||
|  * \return         An \c MBEDTLS_ERR_DHM_XXX error code on failure. | ||||
|  | @ -195,11 +209,17 @@ int mbedtls_dhm_set_group( mbedtls_dhm_context *ctx, | |||
|                            const mbedtls_mpi *G ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          This function imports the public value of the peer, G^Y. | ||||
|  * \brief          This function imports the raw public value of the peer. | ||||
|  * | ||||
|  * \param ctx      The DHM context. | ||||
|  * \param input    The input buffer containing the G^Y value of the peer. | ||||
|  * \param ilen     The size of the input buffer. | ||||
|  * \note           In a TLS handshake, this is the how the server imports | ||||
|  *                 the Client's public DHM key. | ||||
|  * | ||||
|  * \param ctx      The DHM context to use. This must be initialized and have | ||||
|  *                 its DHM parameters set, e.g. via mbedtls_dhm_set_group(). | ||||
|  *                 It may or may not already have generated its own private key. | ||||
|  * \param input    The input buffer containing the \c G^Y value of the peer. | ||||
|  *                 This must be a readable buffer of size \p ilen Bytes. | ||||
|  * \param ilen     The size of the input buffer \p input in Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         An \c MBEDTLS_ERR_DHM_XXX error code on failure. | ||||
|  | @ -208,21 +228,25 @@ int mbedtls_dhm_read_public( mbedtls_dhm_context *ctx, | |||
|                      const unsigned char *input, size_t ilen ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          This function creates its own private key, \c X, and | ||||
|  *                 exports \c G^X. | ||||
|  * \brief          This function creates a DHM key pair and exports | ||||
|  *                 the raw public key in big-endian format. | ||||
|  * | ||||
|  * \note           The destination buffer is always fully written | ||||
|  *                 so as to contain a big-endian representation of G^X mod P. | ||||
|  *                 If it is larger than ctx->len, it is padded accordingly | ||||
|  *                 If it is larger than \c ctx->len, it is padded accordingly | ||||
|  *                 with zero-bytes at the beginning. | ||||
|  * | ||||
|  * \param ctx      The DHM context. | ||||
|  * \param ctx      The DHM context to use. This must be initialized and | ||||
|  *                 have the DHM parameters set. It may or may not already | ||||
|  *                 have imported the peer's public key. | ||||
|  * \param x_size   The private key size in Bytes. | ||||
|  * \param output   The destination buffer. | ||||
|  * \param olen     The length of the destination buffer. Must be at least | ||||
|  *                  equal to ctx->len (the size of \c P). | ||||
|  * \param f_rng    The RNG function. | ||||
|  * \param p_rng    The RNG context. | ||||
|  * \param output   The destination buffer. This must be a writable buffer of | ||||
|  *                 size \p olen Bytes. | ||||
|  * \param olen     The length of the destination buffer. This must be at least | ||||
|  *                 equal to `ctx->len` (the size of \c P). | ||||
|  * \param f_rng    The RNG function. This must not be \c NULL. | ||||
|  * \param p_rng    The RNG context to be passed to \p f_rng. This may be \c NULL | ||||
|  *                 if \p f_rng doesn't need a context argument. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         An \c MBEDTLS_ERR_DHM_XXX error code on failure. | ||||
|  | @ -233,22 +257,27 @@ int mbedtls_dhm_make_public( mbedtls_dhm_context *ctx, int x_size, | |||
|                      void *p_rng ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief               This function derives and exports the shared secret | ||||
|  *                      \c (G^Y)^X mod \c P. | ||||
|  * \brief          This function derives and exports the shared secret | ||||
|  *                 \c (G^Y)^X mod \c P. | ||||
|  * | ||||
|  * \note                If \p f_rng is not NULL, it is used to blind the input as | ||||
|  *                      a countermeasure against timing attacks. Blinding is used | ||||
|  *                      only if our private key \c X is re-used, and not used | ||||
|  *                      otherwise. We recommend always passing a non-NULL | ||||
|  *                      \p f_rng argument. | ||||
|  * \note           If \p f_rng is not \c NULL, it is used to blind the input as | ||||
|  *                 a countermeasure against timing attacks. Blinding is used | ||||
|  *                 only if our private key \c X is re-used, and not used | ||||
|  *                 otherwise. We recommend always passing a non-NULL | ||||
|  *                 \p f_rng argument. | ||||
|  * | ||||
|  * \param ctx           The DHM context. | ||||
|  * \param output        The destination buffer. | ||||
|  * \param output_size   The size of the destination buffer. Must be at least | ||||
|  *                      the size of ctx->len (the size of \c P). | ||||
|  * \param ctx           The DHM context to use. This must be initialized | ||||
|  *                      and have its own private key generated and the peer's | ||||
|  *                      public key imported. | ||||
|  * \param output        The buffer to write the generated shared key to. This | ||||
|  *                      must be a writable buffer of size \p output_size Bytes. | ||||
|  * \param output_size   The size of the destination buffer. This must be at | ||||
|  *                      least the size of \c ctx->len (the size of \c P). | ||||
|  * \param olen          On exit, holds the actual number of Bytes written. | ||||
|  * \param f_rng         The RNG function, for blinding purposes. | ||||
|  * \param p_rng         The RNG context. | ||||
|  * \param f_rng         The RNG function, for blinding purposes. This may | ||||
|  *                      b \c NULL if blinding isn't needed. | ||||
|  * \param p_rng         The RNG context. This may be \c NULL if \p f_rng | ||||
|  *                      doesn't need a context argument. | ||||
|  * | ||||
|  * \return              \c 0 on success. | ||||
|  * \return              An \c MBEDTLS_ERR_DHM_XXX error code on failure. | ||||
|  | @ -259,9 +288,12 @@ int mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx, | |||
|                      void *p_rng ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          This function frees and clears the components of a DHM context. | ||||
|  * \brief          This function frees and clears the components | ||||
|  *                 of a DHM context. | ||||
|  * | ||||
|  * \param ctx      The DHM context to free and clear. | ||||
|  * \param ctx      The DHM context to free and 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 DHM context. | ||||
|  */ | ||||
| void mbedtls_dhm_free( mbedtls_dhm_context *ctx ); | ||||
| 
 | ||||
|  | @ -270,17 +302,19 @@ void mbedtls_dhm_free( mbedtls_dhm_context *ctx ); | |||
| /**
 | ||||
|  * \brief             This function parses DHM parameters in PEM or DER format. | ||||
|  * | ||||
|  * \param dhm         The DHM context to initialize. | ||||
|  * \param dhmin       The input buffer. | ||||
|  * \param dhminlen    The size of the buffer, including the terminating null | ||||
|  *                    Byte for PEM data. | ||||
|  * \param dhm         The DHM context to import the DHM parameters into. | ||||
|  *                    This must be initialized. | ||||
|  * \param dhmin       The input buffer. This must be a readable buffer of | ||||
|  *                    length \p dhminlen Bytes. | ||||
|  * \param dhminlen    The size of the input buffer \p dhmin, including the | ||||
|  *                    terminating \c NULL Byte for PEM data. | ||||
|  * | ||||
|  * \return            \c 0 on success. | ||||
|  * \return            An \c MBEDTLS_ERR_DHM_XXX or \c MBEDTLS_ERR_PEM_XXX error code | ||||
|  *                    error code on failure. | ||||
|  * \return            An \c MBEDTLS_ERR_DHM_XXX or \c MBEDTLS_ERR_PEM_XXX error | ||||
|  *                    code on failure. | ||||
|  */ | ||||
| int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin, | ||||
|                    size_t dhminlen ); | ||||
|                            size_t dhminlen ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_FS_IO) | ||||
| /** \ingroup x509_module */ | ||||
|  | @ -288,16 +322,20 @@ int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin, | |||
|  * \brief          This function loads and parses DHM parameters from a file. | ||||
|  * | ||||
|  * \param dhm      The DHM context to load the parameters to. | ||||
|  *                 This must be initialized. | ||||
|  * \param path     The filename to read the DHM parameters from. | ||||
|  *                 This must not be \c NULL. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return            An \c MBEDTLS_ERR_DHM_XXX or \c MBEDTLS_ERR_PEM_XXX error code | ||||
|  *                    error code on failure. | ||||
|  * \return         An \c MBEDTLS_ERR_DHM_XXX or \c MBEDTLS_ERR_PEM_XXX | ||||
|  *                 error code on failure. | ||||
|  */ | ||||
| int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path ); | ||||
| #endif /* MBEDTLS_FS_IO */ | ||||
| #endif /* MBEDTLS_ASN1_PARSE_C */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          The DMH checkup routine. | ||||
|  * | ||||
|  | @ -306,6 +344,7 @@ int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path ); | |||
|  */ | ||||
| int mbedtls_dhm_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  | @ -353,15 +392,6 @@ int mbedtls_dhm_self_test( int verbose ); | |||
| 
 | ||||
| #if !defined(MBEDTLS_DEPRECATED_REMOVED) | ||||
| 
 | ||||
| #if defined(MBEDTLS_DEPRECATED_WARNING) | ||||
| #define MBEDTLS_DEPRECATED __attribute__((deprecated)) | ||||
| MBEDTLS_DEPRECATED typedef char const * mbedtls_deprecated_constant_t; | ||||
| #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL )       \ | ||||
|     ( (mbedtls_deprecated_constant_t) ( VAL ) ) | ||||
| #else | ||||
| #define MBEDTLS_DEPRECATED_STRING_CONSTANT( VAL ) VAL | ||||
| #endif /* ! MBEDTLS_DEPRECATED_WARNING */ | ||||
| 
 | ||||
| /**
 | ||||
|  * \warning The origin of the primes in RFC 5114 is not documented and | ||||
|  *          their use therefore constitutes a security risk! | ||||
|  |  | |||
|  | @ -34,6 +34,12 @@ | |||
| #ifndef MBEDTLS_ECDH_H | ||||
| #define MBEDTLS_ECDH_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "ecp.h" | ||||
| 
 | ||||
| /*
 | ||||
|  | @ -149,11 +155,16 @@ mbedtls_ecdh_context; | |||
|  * | ||||
|  * \see             ecp.h | ||||
|  * | ||||
|  * \param grp       The ECP group. | ||||
|  * \param grp       The ECP group to use. This must be initialized and have | ||||
|  *                  domain parameters loaded, for example through | ||||
|  *                  mbedtls_ecp_load() or mbedtls_ecp_tls_read_group(). | ||||
|  * \param d         The destination MPI (private key). | ||||
|  *                  This must be initialized. | ||||
|  * \param Q         The destination point (public key). | ||||
|  * \param f_rng     The RNG function. | ||||
|  * \param p_rng     The RNG context. | ||||
|  *                  This must be initialized. | ||||
|  * \param f_rng     The RNG function to use. This must not be \c NULL. | ||||
|  * \param p_rng     The RNG context to be passed to \p f_rng. This may be | ||||
|  *                  \c NULL in case \p f_rng doesn't need a context argument. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          Another \c MBEDTLS_ERR_ECP_XXX or | ||||
|  | @ -176,12 +187,22 @@ int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp | |||
|  *                  countermeasures against side-channel attacks. | ||||
|  *                  For more information, see mbedtls_ecp_mul(). | ||||
|  * | ||||
|  * \param grp       The ECP group. | ||||
|  * \param grp       The ECP group to use. This must be initialized and have | ||||
|  *                  domain parameters loaded, for example through | ||||
|  *                  mbedtls_ecp_load() or mbedtls_ecp_tls_read_group(). | ||||
|  * \param z         The destination MPI (shared secret). | ||||
|  *                  This must be initialized. | ||||
|  * \param Q         The public key from another party. | ||||
|  *                  This must be initialized. | ||||
|  * \param d         Our secret exponent (private key). | ||||
|  * \param f_rng     The RNG function. | ||||
|  * \param p_rng     The RNG context. | ||||
|  *                  This must be initialized. | ||||
|  * \param f_rng     The RNG function. This may be \c NULL if randomization | ||||
|  *                  of intermediate results during the ECP computations is | ||||
|  *                  not needed (discouraged). See the documentation of | ||||
|  *                  mbedtls_ecp_mul() for more. | ||||
|  * \param p_rng     The RNG context to be passed to \p f_rng. This may be | ||||
|  *                  \c NULL if \p f_rng is \c NULL or doesn't need a | ||||
|  *                  context argument. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          Another \c MBEDTLS_ERR_ECP_XXX or | ||||
|  | @ -195,7 +216,7 @@ int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z, | |||
| /**
 | ||||
|  * \brief           This function initializes an ECDH context. | ||||
|  * | ||||
|  * \param ctx       The ECDH context to initialize. | ||||
|  * \param ctx       The ECDH context to initialize. This must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx ); | ||||
| 
 | ||||
|  | @ -210,39 +231,42 @@ void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx ); | |||
|  *                  This is the first function used by a TLS server for ECDHE | ||||
|  *                  ciphersuites. | ||||
|  * | ||||
|  * \param ctx       The ECDH context to set up. | ||||
|  * \param ctx       The ECDH context to set up. This must be initialized. | ||||
|  * \param grp_id    The group id of the group to set up the context for. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  */ | ||||
| int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id ); | ||||
| int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, | ||||
|                         mbedtls_ecp_group_id grp_id ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function frees a context. | ||||
|  * | ||||
|  * \param ctx       The context to free. | ||||
|  * \param ctx       The context to free. This may be \c NULL, in which | ||||
|  *                  case this function does nothing. If it is not \c NULL, | ||||
|  *                  it must point to an initialized ECDH context. | ||||
|  */ | ||||
| void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function generates a public key and a TLS | ||||
|  *                  ServerKeyExchange payload. | ||||
|  * \brief           This function generates an EC key pair and exports its | ||||
|  *                  in the format used in a TLS ServerKeyExchange handshake | ||||
|  *                  message. | ||||
|  * | ||||
|  *                  This is the second function used by a TLS server for ECDHE | ||||
|  *                  ciphersuites. (It is called after mbedtls_ecdh_setup().) | ||||
|  * | ||||
|  * \note            This function assumes that the ECP group (grp) of the | ||||
|  *                  \p ctx context has already been properly set, | ||||
|  *                  for example, using mbedtls_ecp_group_load(). | ||||
|  * | ||||
|  * \see             ecp.h | ||||
|  * | ||||
|  * \param ctx       The ECDH context. | ||||
|  * \param olen      The number of characters written. | ||||
|  * \param buf       The destination buffer. | ||||
|  * \param blen      The length of the destination buffer. | ||||
|  * \param f_rng     The RNG function. | ||||
|  * \param p_rng     The RNG context. | ||||
|  * \param ctx       The ECDH context to use. This must be initialized | ||||
|  *                  and bound to a group, for example via mbedtls_ecdh_setup(). | ||||
|  * \param olen      The address at which to store the number of Bytes written. | ||||
|  * \param buf       The destination buffer. This must be a writable buffer of | ||||
|  *                  length \p blen Bytes. | ||||
|  * \param blen      The length of the destination buffer \p buf in Bytes. | ||||
|  * \param f_rng     The RNG function to use. This must not be \c NULL. | ||||
|  * \param p_rng     The RNG context to be passed to \p f_rng. This may be | ||||
|  *                  \c NULL in case \p f_rng doesn't need a context argument. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of | ||||
|  | @ -255,24 +279,32 @@ int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen, | |||
|                       void *p_rng ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function parses and processes a TLS ServerKeyExhange | ||||
|  *                  payload. | ||||
|  * \brief           This function parses the ECDHE parameters in a | ||||
|  *                  TLS ServerKeyExchange handshake message. | ||||
|  * | ||||
|  *                  This is the first function used by a TLS client for ECDHE | ||||
|  *                  ciphersuites. | ||||
|  * \note            In a TLS handshake, this is the how the client | ||||
|  *                  sets up its ECDHE context from the server's public | ||||
|  *                  ECDHE key material. | ||||
|  * | ||||
|  * \see             ecp.h | ||||
|  * | ||||
|  * \param ctx       The ECDH context. | ||||
|  * \param buf       The pointer to the start of the input buffer. | ||||
|  * \param end       The address for one Byte past the end of the buffer. | ||||
|  * \param ctx       The ECDHE context to use. This must be initialized. | ||||
|  * \param buf       On input, \c *buf must be the start of the input buffer. | ||||
|  *                  On output, \c *buf is updated to point to the end of the | ||||
|  *                  data that has been read. On success, this is the first byte | ||||
|  *                  past the end of the ServerKeyExchange parameters. | ||||
|  *                  On error, this is the point at which an error has been | ||||
|  *                  detected, which is usually not useful except to debug | ||||
|  *                  failures. | ||||
|  * \param end       The end of the input buffer. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          An \c MBEDTLS_ERR_ECP_XXX error code on failure. | ||||
|  * | ||||
|  */ | ||||
| int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx, | ||||
|                       const unsigned char **buf, const unsigned char *end ); | ||||
|                               const unsigned char **buf, | ||||
|                               const unsigned char *end ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function sets up an ECDH context from an EC key. | ||||
|  | @ -283,33 +315,40 @@ int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx, | |||
|  * | ||||
|  * \see             ecp.h | ||||
|  * | ||||
|  * \param ctx       The ECDH context to set up. | ||||
|  * \param key       The EC key to use. | ||||
|  * \param side      Defines the source of the key: 1: Our key, or | ||||
|  *                  0: The key of the peer. | ||||
|  * \param ctx       The ECDH context to set up. This must be initialized. | ||||
|  * \param key       The EC key to use. This must be initialized. | ||||
|  * \param side      Defines the source of the key. Possible values are: | ||||
|  *                  - #MBEDTLS_ECDH_OURS: The key is ours. | ||||
|  *                  - #MBEDTLS_ECDH_THEIRS: The key is that of the peer. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          Another \c MBEDTLS_ERR_ECP_XXX error code on failure. | ||||
|  * | ||||
|  */ | ||||
| int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key, | ||||
|                      mbedtls_ecdh_side side ); | ||||
| int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, | ||||
|                              const mbedtls_ecp_keypair *key, | ||||
|                              mbedtls_ecdh_side side ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function generates a public key and a TLS | ||||
|  *                  ClientKeyExchange payload. | ||||
|  * \brief           This function generates a public key and exports it | ||||
|  *                  as a TLS ClientKeyExchange payload. | ||||
|  * | ||||
|  *                  This is the second function used by a TLS client for ECDH(E) | ||||
|  *                  ciphersuites. | ||||
|  * | ||||
|  * \see             ecp.h | ||||
|  * | ||||
|  * \param ctx       The ECDH context. | ||||
|  * \param olen      The number of Bytes written. | ||||
|  * \param buf       The destination buffer. | ||||
|  * \param blen      The size of the destination buffer. | ||||
|  * \param f_rng     The RNG function. | ||||
|  * \param p_rng     The RNG context. | ||||
|  * \param ctx       The ECDH context to use. This must be initialized | ||||
|  *                  and bound to a group, the latter usually by | ||||
|  *                  mbedtls_ecdh_read_params(). | ||||
|  * \param olen      The address at which to store the number of Bytes written. | ||||
|  *                  This must not be \c NULL. | ||||
|  * \param buf       The destination buffer. This must be a writable buffer | ||||
|  *                  of length \p blen Bytes. | ||||
|  * \param blen      The size of the destination buffer \p buf in Bytes. | ||||
|  * \param f_rng     The RNG function to use. This must not be \c NULL. | ||||
|  * \param p_rng     The RNG context to be passed to \p f_rng. This may be | ||||
|  *                  \c NULL in case \p f_rng doesn't need a context argument. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of | ||||
|  | @ -322,8 +361,8 @@ int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen, | |||
|                       void *p_rng ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief       This function parses and processes a TLS ClientKeyExchange | ||||
|  *              payload. | ||||
|  * \brief       This function parses and processes the ECDHE payload of a | ||||
|  *              TLS ClientKeyExchange message. | ||||
|  * | ||||
|  *              This is the third function used by a TLS server for ECDH(E) | ||||
|  *              ciphersuites. (It is called after mbedtls_ecdh_setup() and | ||||
|  | @ -331,15 +370,17 @@ int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen, | |||
|  * | ||||
|  * \see         ecp.h | ||||
|  * | ||||
|  * \param ctx   The ECDH context. | ||||
|  * \param buf   The start of the input buffer. | ||||
|  * \param blen  The length of the input buffer. | ||||
|  * \param ctx   The ECDH context to use. This must be initialized | ||||
|  *              and bound to a group, for example via mbedtls_ecdh_setup(). | ||||
|  * \param buf   The pointer to the ClientKeyExchange payload. This must | ||||
|  *              be a readable buffer of length \p blen Bytes. | ||||
|  * \param blen  The length of the input buffer \p buf in Bytes. | ||||
|  * | ||||
|  * \return      \c 0 on success. | ||||
|  * \return      An \c MBEDTLS_ERR_ECP_XXX error code on failure. | ||||
|  */ | ||||
| int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx, | ||||
|                       const unsigned char *buf, size_t blen ); | ||||
|                               const unsigned char *buf, size_t blen ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function derives and exports the shared secret. | ||||
|  | @ -352,13 +393,19 @@ int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx, | |||
|  *                  For more information, see mbedtls_ecp_mul(). | ||||
|  * | ||||
|  * \see             ecp.h | ||||
|  * | ||||
|  * \param ctx       The ECDH context. | ||||
|  * \param olen      The number of Bytes written. | ||||
|  * \param buf       The destination buffer. | ||||
|  * \param blen      The length of the destination buffer. | ||||
|  * \param f_rng     The RNG function. | ||||
|  * \param p_rng     The RNG context. | ||||
| 
 | ||||
|  * \param ctx       The ECDH context to use. This must be initialized | ||||
|  *                  and have its own private key generated and the peer's | ||||
|  *                  public key imported. | ||||
|  * \param olen      The address at which to store the total number of | ||||
|  *                  Bytes written on success. This must not be \c NULL. | ||||
|  * \param buf       The buffer to write the generated shared key to. This | ||||
|  *                  must be a writable buffer of size \p blen Bytes. | ||||
|  * \param blen      The length of the destination buffer \p buf in Bytes. | ||||
|  * \param f_rng     The RNG function, for blinding purposes. This may | ||||
|  *                  b \c NULL if blinding isn't needed. | ||||
|  * \param p_rng     The RNG context. This may be \c NULL if \p f_rng | ||||
|  *                  doesn't need a context argument. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of | ||||
|  | @ -381,7 +428,7 @@ int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen, | |||
|  *                  computations once enabled, except by free-ing the context, | ||||
|  *                  which cancels possible in-progress operations. | ||||
|  * | ||||
|  * \param ctx       The ECDH context. | ||||
|  * \param ctx       The ECDH context to use. This must be initialized. | ||||
|  */ | ||||
| void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx ); | ||||
| #endif /* MBEDTLS_ECP_RESTARTABLE */ | ||||
|  |  | |||
|  | @ -32,6 +32,12 @@ | |||
| #ifndef MBEDTLS_ECDSA_H | ||||
| #define MBEDTLS_ECDSA_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "ecp.h" | ||||
| #include "md.h" | ||||
| 
 | ||||
|  | @ -123,7 +129,8 @@ typedef void mbedtls_ecdsa_restart_ctx; | |||
|  * \brief           This function computes the ECDSA signature of a | ||||
|  *                  previously-hashed message. | ||||
|  * | ||||
|  * \note            The deterministic version is usually preferred. | ||||
|  * \note            The deterministic version implemented in | ||||
|  *                  mbedtls_ecdsa_sign_det() is usually preferred. | ||||
|  * | ||||
|  * \note            If the bitlength of the message hash is larger than the | ||||
|  *                  bitlength of the group order, then the hash is truncated | ||||
|  | @ -133,14 +140,22 @@ typedef void mbedtls_ecdsa_restart_ctx; | |||
|  * | ||||
|  * \see             ecp.h | ||||
|  * | ||||
|  * \param grp       The ECP group. | ||||
|  * \param r         The first output integer. | ||||
|  * \param s         The second output integer. | ||||
|  * \param d         The private signing key. | ||||
|  * \param buf       The message hash. | ||||
|  * \param blen      The length of \p buf. | ||||
|  * \param f_rng     The RNG function. | ||||
|  * \param p_rng     The RNG context. | ||||
|  * \param grp       The context for the elliptic curve to use. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param r         The MPI context in which to store the first part | ||||
|  *                  the signature. This must be initialized. | ||||
|  * \param s         The MPI context in which to store the second part | ||||
|  *                  the signature. This must be initialized. | ||||
|  * \param d         The private signing key. This must be initialized. | ||||
|  * \param buf       The content to be signed. This is usually the hash of | ||||
|  *                  the original data to be signed. This must be a readable | ||||
|  *                  buffer of length \p blen Bytes. It may be \c NULL if | ||||
|  *                  \p blen is zero. | ||||
|  * \param blen      The length of \p buf in Bytes. | ||||
|  * \param f_rng     The RNG function. This must not be \c NULL. | ||||
|  * \param p_rng     The RNG context to be passed to \p f_rng. This may be | ||||
|  *                  \c NULL if \p f_rng doesn't need a context parameter. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          An \c MBEDTLS_ERR_ECP_XXX | ||||
|  | @ -167,21 +182,29 @@ int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, | |||
|  * | ||||
|  * \see             ecp.h | ||||
|  * | ||||
|  * \param grp       The ECP group. | ||||
|  * \param r         The first output integer. | ||||
|  * \param s         The second output integer. | ||||
|  * \param d         The private signing key. | ||||
|  * \param buf       The message hash. | ||||
|  * \param blen      The length of \p buf. | ||||
|  * \param md_alg    The MD algorithm used to hash the message. | ||||
|  * \param grp       The context for the elliptic curve to use. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param r         The MPI context in which to store the first part | ||||
|  *                  the signature. This must be initialized. | ||||
|  * \param s         The MPI context in which to store the second part | ||||
|  *                  the signature. This must be initialized. | ||||
|  * \param d         The private signing key. This must be initialized | ||||
|  *                  and setup, for example through mbedtls_ecp_gen_privkey(). | ||||
|  * \param buf       The hashed content to be signed. This must be a readable | ||||
|  *                  buffer of length \p blen Bytes. It may be \c NULL if | ||||
|  *                  \p blen is zero. | ||||
|  * \param blen      The length of \p buf in Bytes. | ||||
|  * \param md_alg    The hash algorithm used to hash the original data. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX | ||||
|  *                  error code on failure. | ||||
|  */ | ||||
| int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, | ||||
|                     const mbedtls_mpi *d, const unsigned char *buf, size_t blen, | ||||
|                     mbedtls_md_type_t md_alg ); | ||||
| int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, | ||||
|                             mbedtls_mpi *s, const mbedtls_mpi *d, | ||||
|                             const unsigned char *buf, size_t blen, | ||||
|                             mbedtls_md_type_t md_alg ); | ||||
| #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ | ||||
| 
 | ||||
| /**
 | ||||
|  | @ -196,12 +219,19 @@ int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi | |||
|  * | ||||
|  * \see             ecp.h | ||||
|  * | ||||
|  * \param grp       The ECP group. | ||||
|  * \param buf       The message hash. | ||||
|  * \param blen      The length of \p buf. | ||||
|  * \param Q         The public key to use for verification. | ||||
|  * \param grp       The ECP group to use. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param buf       The hashed content that was signed. This must be a readable | ||||
|  *                  buffer of length \p blen Bytes. It may be \c NULL if | ||||
|  *                  \p blen is zero. | ||||
|  * \param blen      The length of \p buf in Bytes. | ||||
|  * \param Q         The public key to use for verification. This must be | ||||
|  *                  initialized and setup. | ||||
|  * \param r         The first integer of the signature. | ||||
|  *                  This must be initialized. | ||||
|  * \param s         The second integer of the signature. | ||||
|  *                  This must be initialized. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the signature | ||||
|  | @ -210,8 +240,9 @@ int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi | |||
|  *                  error code on failure for any other reason. | ||||
|  */ | ||||
| int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp, | ||||
|                   const unsigned char *buf, size_t blen, | ||||
|                   const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s); | ||||
|                           const unsigned char *buf, size_t blen, | ||||
|                           const mbedtls_ecp_point *Q, const mbedtls_mpi *r, | ||||
|                           const mbedtls_mpi *s); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function computes the ECDSA signature and writes it | ||||
|  | @ -228,11 +259,6 @@ int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp, | |||
|  *                  of the Digital Signature Algorithm (DSA) and Elliptic | ||||
|  *                  Curve Digital Signature Algorithm (ECDSA)</em>. | ||||
|  * | ||||
|  * \note            The \p sig buffer must be at least twice as large as the | ||||
|  *                  size of the curve used, plus 9. For example, 73 Bytes if | ||||
|  *                  a 256-bit curve is used. A buffer length of | ||||
|  *                  #MBEDTLS_ECDSA_MAX_LEN is always safe. | ||||
|  * | ||||
|  * \note            If the bitlength of the message hash is larger than the | ||||
|  *                  bitlength of the group order, then the hash is truncated as | ||||
|  *                  defined in <em>Standards for Efficient Cryptography Group | ||||
|  | @ -241,20 +267,32 @@ int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp, | |||
|  * | ||||
|  * \see             ecp.h | ||||
|  * | ||||
|  * \param ctx       The ECDSA context. | ||||
|  * \param ctx       The ECDSA context to use. This must be initialized | ||||
|  *                  and have a group and private key bound to it, for example | ||||
|  *                  via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair(). | ||||
|  * \param md_alg    The message digest that was used to hash the message. | ||||
|  * \param hash      The message hash. | ||||
|  * \param hlen      The length of the hash. | ||||
|  * \param sig       The buffer that holds the signature. | ||||
|  * \param slen      The length of the signature written. | ||||
|  * \param f_rng     The RNG function. | ||||
|  * \param p_rng     The RNG context. | ||||
|  * \param hash      The message hash to be signed. This must be a readable | ||||
|  *                  buffer of length \p blen Bytes. | ||||
|  * \param hlen      The length of the hash \p hash in Bytes. | ||||
|  * \param sig       The buffer to which to write the signature. This must be a | ||||
|  *                  writable buffer of length at least twice as large as the | ||||
|  *                  size of the curve used, plus 9. For example, 73 Bytes if | ||||
|  *                  a 256-bit curve is used. A buffer length of | ||||
|  *                  #MBEDTLS_ECDSA_MAX_LEN is always safe. | ||||
|  * \param slen      The address at which to store the actual length of | ||||
|  *                  the signature written. Must not be \c NULL. | ||||
|  * \param f_rng     The RNG function. This must not be \c NULL if | ||||
|  *                  #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise, | ||||
|  *                  it is unused and may be set to \c NULL. | ||||
|  * \param p_rng     The RNG context to be passed to \p f_rng. This may be | ||||
|  *                  \c NULL if \p f_rng is \c NULL or doesn't use a context. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or | ||||
|  *                  \c MBEDTLS_ERR_ASN1_XXX error code on failure. | ||||
|  */ | ||||
| int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg, | ||||
| int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, | ||||
|                                    mbedtls_md_type_t md_alg, | ||||
|                            const unsigned char *hash, size_t hlen, | ||||
|                            unsigned char *sig, size_t *slen, | ||||
|                            int (*f_rng)(void *, unsigned char *, size_t), | ||||
|  | @ -270,15 +308,28 @@ int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t | |||
|  *                  but it can return early and restart according to the limit | ||||
|  *                  set with \c mbedtls_ecp_set_max_ops() to reduce blocking. | ||||
|  * | ||||
|  * \param ctx       The ECDSA context. | ||||
|  * \param ctx       The ECDSA context to use. This must be initialized | ||||
|  *                  and have a group and private key bound to it, for example | ||||
|  *                  via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair(). | ||||
|  * \param md_alg    The message digest that was used to hash the message. | ||||
|  * \param hash      The message hash. | ||||
|  * \param hlen      The length of the hash. | ||||
|  * \param sig       The buffer that holds the signature. | ||||
|  * \param slen      The length of the signature written. | ||||
|  * \param f_rng     The RNG function. | ||||
|  * \param p_rng     The RNG context. | ||||
|  * \param rs_ctx    The restart context (NULL disables restart). | ||||
|  * \param hash      The message hash to be signed. This must be a readable | ||||
|  *                  buffer of length \p blen Bytes. | ||||
|  * \param hlen      The length of the hash \p hash in Bytes. | ||||
|  * \param sig       The buffer to which to write the signature. This must be a | ||||
|  *                  writable buffer of length at least twice as large as the | ||||
|  *                  size of the curve used, plus 9. For example, 73 Bytes if | ||||
|  *                  a 256-bit curve is used. A buffer length of | ||||
|  *                  #MBEDTLS_ECDSA_MAX_LEN is always safe. | ||||
|  * \param slen      The address at which to store the actual length of | ||||
|  *                  the signature written. Must not be \c NULL. | ||||
|  * \param f_rng     The RNG function. This must not be \c NULL if | ||||
|  *                  #MBEDTLS_ECDSA_DETERMINISTIC is unset. Otherwise, | ||||
|  *                  it is unused and may be set to \c NULL. | ||||
|  * \param p_rng     The RNG context to be passed to \p f_rng. This may be | ||||
|  *                  \c NULL if \p f_rng is \c NULL or doesn't use a context. | ||||
|  * \param rs_ctx    The restart context to use. This may be \c NULL to disable | ||||
|  *                  restarting. If it is not \c NULL, it must point to an | ||||
|  *                  initialized restart context. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of | ||||
|  | @ -314,11 +365,6 @@ int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx, | |||
|  * \warning         It is not thread-safe to use the same context in | ||||
|  *                  multiple threads. | ||||
|  * | ||||
|  * \note            The \p sig buffer must be at least twice as large as the | ||||
|  *                  size of the curve used, plus 9. For example, 73 Bytes if a | ||||
|  *                  256-bit curve is used. A buffer length of | ||||
|  *                  #MBEDTLS_ECDSA_MAX_LEN is always safe. | ||||
|  * | ||||
|  * \note            If the bitlength of the message hash is larger than the | ||||
|  *                  bitlength of the group order, then the hash is truncated as | ||||
|  *                  defined in <em>Standards for Efficient Cryptography Group | ||||
|  | @ -330,12 +376,20 @@ int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx, | |||
|  * \deprecated      Superseded by mbedtls_ecdsa_write_signature() in | ||||
|  *                  Mbed TLS version 2.0 and later. | ||||
|  * | ||||
|  * \param ctx       The ECDSA context. | ||||
|  * \param hash      The message hash. | ||||
|  * \param hlen      The length of the hash. | ||||
|  * \param sig       The buffer that holds the signature. | ||||
|  * \param slen      The length of the signature written. | ||||
|  * \param md_alg    The MD algorithm used to hash the message. | ||||
|  * \param ctx       The ECDSA context to use. This must be initialized | ||||
|  *                  and have a group and private key bound to it, for example | ||||
|  *                  via mbedtls_ecdsa_genkey() or mbedtls_ecdsa_from_keypair(). | ||||
|  * \param hash      The message hash to be signed. This must be a readable | ||||
|  *                  buffer of length \p blen Bytes. | ||||
|  * \param hlen      The length of the hash \p hash in Bytes. | ||||
|  * \param sig       The buffer to which to write the signature. This must be a | ||||
|  *                  writable buffer of length at least twice as large as the | ||||
|  *                  size of the curve used, plus 9. For example, 73 Bytes if | ||||
|  *                  a 256-bit curve is used. A buffer length of | ||||
|  *                  #MBEDTLS_ECDSA_MAX_LEN is always safe. | ||||
|  * \param slen      The address at which to store the actual length of | ||||
|  *                  the signature written. Must not be \c NULL. | ||||
|  * \param md_alg    The message digest that was used to hash the message. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          An \c MBEDTLS_ERR_ECP_XXX, \c MBEDTLS_ERR_MPI_XXX or | ||||
|  | @ -360,11 +414,14 @@ int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx, | |||
|  * | ||||
|  * \see             ecp.h | ||||
|  * | ||||
|  * \param ctx       The ECDSA context. | ||||
|  * \param hash      The message hash. | ||||
|  * \param hlen      The size of the hash. | ||||
|  * \param sig       The signature to read and verify. | ||||
|  * \param slen      The size of \p sig. | ||||
|  * \param ctx       The ECDSA context to use. This must be initialized | ||||
|  *                  and have a group and public key bound to it. | ||||
|  * \param hash      The message hash that was signed. This must be a readable | ||||
|  *                  buffer of length \p size Bytes. | ||||
|  * \param hlen      The size of the hash \p hash. | ||||
|  * \param sig       The signature to read and verify. This must be a readable | ||||
|  *                  buffer of length \p slen Bytes. | ||||
|  * \param slen      The size of \p sig in Bytes. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid. | ||||
|  | @ -387,12 +444,17 @@ int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx, | |||
|  *                  but it can return early and restart according to the limit | ||||
|  *                  set with \c mbedtls_ecp_set_max_ops() to reduce blocking. | ||||
|  * | ||||
|  * \param ctx       The ECDSA context. | ||||
|  * \param hash      The message hash. | ||||
|  * \param hlen      The size of the hash. | ||||
|  * \param sig       The signature to read and verify. | ||||
|  * \param slen      The size of \p sig. | ||||
|  * \param rs_ctx    The restart context (NULL disables restart). | ||||
|  * \param ctx       The ECDSA context to use. This must be initialized | ||||
|  *                  and have a group and public key bound to it. | ||||
|  * \param hash      The message hash that was signed. This must be a readable | ||||
|  *                  buffer of length \p size Bytes. | ||||
|  * \param hlen      The size of the hash \p hash. | ||||
|  * \param sig       The signature to read and verify. This must be a readable | ||||
|  *                  buffer of length \p slen Bytes. | ||||
|  * \param slen      The size of \p sig in Bytes. | ||||
|  * \param rs_ctx    The restart context to use. This may be \c NULL to disable | ||||
|  *                  restarting. If it is not \c NULL, it must point to an | ||||
|  *                  initialized restart context. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if signature is invalid. | ||||
|  | @ -414,10 +476,12 @@ int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx, | |||
|  * \see            ecp.h | ||||
|  * | ||||
|  * \param ctx      The ECDSA context to store the keypair in. | ||||
|  *                 This must be initialized. | ||||
|  * \param gid      The elliptic curve to use. One of the various | ||||
|  *                 \c MBEDTLS_ECP_DP_XXX macros depending on configuration. | ||||
|  * \param f_rng    The RNG function. | ||||
|  * \param p_rng    The RNG context. | ||||
|  * \param f_rng    The RNG function to use. This must not be \c NULL. | ||||
|  * \param p_rng    The RNG context to be passed to \p f_rng. This may be | ||||
|  *                 \c NULL if \p f_rng doesn't need a context argument. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         An \c MBEDTLS_ERR_ECP_XXX code on failure. | ||||
|  | @ -426,40 +490,55 @@ int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid, | |||
|                   int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function sets an ECDSA context from an EC key pair. | ||||
|  * \brief           This function sets up an ECDSA context from an EC key pair. | ||||
|  * | ||||
|  * \see             ecp.h | ||||
|  * | ||||
|  * \param ctx       The ECDSA context to set. | ||||
|  * \param key       The EC key to use. | ||||
|  * \param ctx       The ECDSA context to setup. This must be initialized. | ||||
|  * \param key       The EC key to use. This must be initialized and hold | ||||
|  *                  a private-public key pair or a public key. In the former | ||||
|  *                  case, the ECDSA context may be used for signature creation | ||||
|  *                  and verification after this call. In the latter case, it | ||||
|  *                  may be used for signature verification. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          An \c MBEDTLS_ERR_ECP_XXX code on failure. | ||||
|  */ | ||||
| int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key ); | ||||
| int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, | ||||
|                                 const mbedtls_ecp_keypair *key ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function initializes an ECDSA context. | ||||
|  * | ||||
|  * \param ctx       The ECDSA context to initialize. | ||||
|  *                  This must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function frees an ECDSA context. | ||||
|  * | ||||
|  * \param ctx       The ECDSA context to free. | ||||
|  * \param ctx       The ECDSA context to free. This may be \c NULL, | ||||
|  *                  in which case this function does nothing. If it | ||||
|  *                  is not \c NULL, it must be initialized. | ||||
|  */ | ||||
| void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_ECP_RESTARTABLE) | ||||
| /**
 | ||||
|  * \brief           Initialize a restart context | ||||
|  * \brief           Initialize a restart context. | ||||
|  * | ||||
|  * \param ctx       The restart context to initialize. | ||||
|  *                  This must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           Free the components of a restart context | ||||
|  * \brief           Free the components of a restart context. | ||||
|  * | ||||
|  * \param ctx       The restart context to free. This may be \c NULL, | ||||
|  *                  in which case this function does nothing. If it | ||||
|  *                  is not \c NULL, it must be initialized. | ||||
|  */ | ||||
| void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx ); | ||||
| #endif /* MBEDTLS_ECP_RESTARTABLE */ | ||||
|  |  | |||
|  | @ -40,6 +40,11 @@ | |||
|  * The payloads are serialized in a way suitable for use in TLS, but could | ||||
|  * also be use outside TLS. | ||||
|  */ | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "ecp.h" | ||||
| #include "md.h" | ||||
|  | @ -92,28 +97,33 @@ typedef struct mbedtls_ecjpake_context | |||
| #endif /* MBEDTLS_ECJPAKE_ALT */ | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           Initialize a context | ||||
|  *                  (just makes it ready for setup() or free()). | ||||
|  * \brief           Initialize an ECJPAKE context. | ||||
|  * | ||||
|  * \param ctx       context to initialize | ||||
|  * \param ctx       The ECJPAKE context to initialize. | ||||
|  *                  This must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           Set up a context for use | ||||
|  * \brief           Set up an ECJPAKE context for use. | ||||
|  * | ||||
|  * \note            Currently the only values for hash/curve allowed by the | ||||
|  *                  standard are MBEDTLS_MD_SHA256/MBEDTLS_ECP_DP_SECP256R1. | ||||
|  *                  standard are #MBEDTLS_MD_SHA256/#MBEDTLS_ECP_DP_SECP256R1. | ||||
|  * | ||||
|  * \param ctx       context to set up | ||||
|  * \param role      Our role: client or server | ||||
|  * \param hash      hash function to use (MBEDTLS_MD_XXX) | ||||
|  * \param curve     elliptic curve identifier (MBEDTLS_ECP_DP_XXX) | ||||
|  * \param secret    pre-shared secret (passphrase) | ||||
|  * \param len       length of the shared secret | ||||
|  * \param ctx       The ECJPAKE context to set up. This must be initialized. | ||||
|  * \param role      The role of the caller. This must be either | ||||
|  *                  #MBEDTLS_ECJPAKE_CLIENT or #MBEDTLS_ECJPAKE_SERVER. | ||||
|  * \param hash      The identifier of the hash function to use, | ||||
|  *                  for example #MBEDTLS_MD_SHA256. | ||||
|  * \param curve     The identifier of the elliptic curve to use, | ||||
|  *                  for example #MBEDTLS_ECP_DP_SECP256R1. | ||||
|  * \param secret    The pre-shared secret (passphrase). This must be | ||||
|  *                  a readable buffer of length \p len Bytes. It need | ||||
|  *                  only be valid for the duration of this call. | ||||
|  * \param len       The length of the pre-shared secret \p secret. | ||||
|  * | ||||
|  * \return          0 if successfull, | ||||
|  *                  a negative error code otherwise | ||||
|  * \return          \c 0 if successful. | ||||
|  * \return          A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx, | ||||
|                            mbedtls_ecjpake_role role, | ||||
|  | @ -123,29 +133,34 @@ int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx, | |||
|                            size_t len ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           Check if a context is ready for use | ||||
|  * \brief           Check if an ECJPAKE context is ready for use. | ||||
|  * | ||||
|  * \param ctx       Context to check | ||||
|  * \param ctx       The ECJPAKE context to check. This must be | ||||
|  *                  initialized. | ||||
|  * | ||||
|  * \return          0 if the context is ready for use, | ||||
|  *                  MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise | ||||
|  * \return          \c 0 if the context is ready for use. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise. | ||||
|  */ | ||||
| int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           Generate and write the first round message | ||||
|  *                  (TLS: contents of the Client/ServerHello extension, | ||||
|  *                  excluding extension type and length bytes) | ||||
|  *                  excluding extension type and length bytes). | ||||
|  * | ||||
|  * \param ctx       Context to use | ||||
|  * \param buf       Buffer to write the contents to | ||||
|  * \param len       Buffer size | ||||
|  * \param olen      Will be updated with the number of bytes written | ||||
|  * \param f_rng     RNG function | ||||
|  * \param p_rng     RNG parameter | ||||
|  * \param ctx       The ECJPAKE context to use. This must be | ||||
|  *                  initialized and set up. | ||||
|  * \param buf       The buffer to write the contents to. This must be a | ||||
|  *                  writable buffer of length \p len Bytes. | ||||
|  * \param len       The length of \p buf in Bytes. | ||||
|  * \param olen      The address at which to store the total number | ||||
|  *                  of Bytes written to \p buf. This must not be \c NULL. | ||||
|  * \param f_rng     The RNG function to use. This must not be \c NULL. | ||||
|  * \param p_rng     The RNG parameter to be passed to \p f_rng. This | ||||
|  *                  may be \c NULL if \p f_rng doesn't use a context. | ||||
|  * | ||||
|  * \return          0 if successfull, | ||||
|  *                  a negative error code otherwise | ||||
|  * \return          \c 0 if successful. | ||||
|  * \return          A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx, | ||||
|                             unsigned char *buf, size_t len, size_t *olen, | ||||
|  | @ -155,14 +170,16 @@ int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx, | |||
| /**
 | ||||
|  * \brief           Read and process the first round message | ||||
|  *                  (TLS: contents of the Client/ServerHello extension, | ||||
|  *                  excluding extension type and length bytes) | ||||
|  *                  excluding extension type and length bytes). | ||||
|  * | ||||
|  * \param ctx       Context to use | ||||
|  * \param buf       Pointer to extension contents | ||||
|  * \param len       Extension length | ||||
|  * \param ctx       The ECJPAKE context to use. This must be initialized | ||||
|  *                  and set up. | ||||
|  * \param buf       The buffer holding the first round message. This must | ||||
|  *                  be a readable buffer of length \p len Bytes. | ||||
|  * \param len       The length in Bytes of \p buf. | ||||
|  * | ||||
|  * \return          0 if successfull, | ||||
|  *                  a negative error code otherwise | ||||
|  * \return          \c 0 if successful. | ||||
|  * \return          A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx, | ||||
|                                     const unsigned char *buf, | ||||
|  | @ -170,17 +187,21 @@ int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx, | |||
| 
 | ||||
| /**
 | ||||
|  * \brief           Generate and write the second round message | ||||
|  *                  (TLS: contents of the Client/ServerKeyExchange) | ||||
|  *                  (TLS: contents of the Client/ServerKeyExchange). | ||||
|  * | ||||
|  * \param ctx       Context to use | ||||
|  * \param buf       Buffer to write the contents to | ||||
|  * \param len       Buffer size | ||||
|  * \param olen      Will be updated with the number of bytes written | ||||
|  * \param f_rng     RNG function | ||||
|  * \param p_rng     RNG parameter | ||||
|  * \param ctx       The ECJPAKE context to use. This must be initialized, | ||||
|  *                  set up, and already have performed round one. | ||||
|  * \param buf       The buffer to write the round two contents to. | ||||
|  *                  This must be a writable buffer of length \p len Bytes. | ||||
|  * \param len       The size of \p buf in Bytes. | ||||
|  * \param olen      The address at which to store the total number of Bytes | ||||
|  *                  written to \p buf. This must not be \c NULL. | ||||
|  * \param f_rng     The RNG function to use. This must not be \c NULL. | ||||
|  * \param p_rng     The RNG parameter to be passed to \p f_rng. This | ||||
|  *                  may be \c NULL if \p f_rng doesn't use a context. | ||||
|  * | ||||
|  * \return          0 if successfull, | ||||
|  *                  a negative error code otherwise | ||||
|  * \return          \c 0 if successful. | ||||
|  * \return          A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx, | ||||
|                             unsigned char *buf, size_t len, size_t *olen, | ||||
|  | @ -189,14 +210,16 @@ int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx, | |||
| 
 | ||||
| /**
 | ||||
|  * \brief           Read and process the second round message | ||||
|  *                  (TLS: contents of the Client/ServerKeyExchange) | ||||
|  *                  (TLS: contents of the Client/ServerKeyExchange). | ||||
|  * | ||||
|  * \param ctx       Context to use | ||||
|  * \param buf       Pointer to the message | ||||
|  * \param len       Message length | ||||
|  * \param ctx       The ECJPAKE context to use. This must be initialized | ||||
|  *                  and set up and already have performed round one. | ||||
|  * \param buf       The buffer holding the second round message. This must | ||||
|  *                  be a readable buffer of length \p len Bytes. | ||||
|  * \param len       The length in Bytes of \p buf. | ||||
|  * | ||||
|  * \return          0 if successfull, | ||||
|  *                  a negative error code otherwise | ||||
|  * \return          \c 0 if successful. | ||||
|  * \return          A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx, | ||||
|                                     const unsigned char *buf, | ||||
|  | @ -204,17 +227,21 @@ int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx, | |||
| 
 | ||||
| /**
 | ||||
|  * \brief           Derive the shared secret | ||||
|  *                  (TLS: Pre-Master Secret) | ||||
|  *                  (TLS: Pre-Master Secret). | ||||
|  * | ||||
|  * \param ctx       Context to use | ||||
|  * \param buf       Buffer to write the contents to | ||||
|  * \param len       Buffer size | ||||
|  * \param olen      Will be updated with the number of bytes written | ||||
|  * \param f_rng     RNG function | ||||
|  * \param p_rng     RNG parameter | ||||
|  * \param ctx       The ECJPAKE context to use. This must be initialized, | ||||
|  *                  set up and have performed both round one and two. | ||||
|  * \param buf       The buffer to write the derived secret to. This must | ||||
|  *                  be a writable buffer of length \p len Bytes. | ||||
|  * \param len       The length of \p buf in Bytes. | ||||
|  * \param olen      The address at which to store the total number of Bytes | ||||
|  *                  written to \p buf. This must not be \c NULL. | ||||
|  * \param f_rng     The RNG function to use. This must not be \c NULL. | ||||
|  * \param p_rng     The RNG parameter to be passed to \p f_rng. This | ||||
|  *                  may be \c NULL if \p f_rng doesn't use a context. | ||||
|  * | ||||
|  * \return          0 if successfull, | ||||
|  *                  a negative error code otherwise | ||||
|  * \return          \c 0 if successful. | ||||
|  * \return          A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx, | ||||
|                             unsigned char *buf, size_t len, size_t *olen, | ||||
|  | @ -222,14 +249,15 @@ int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx, | |||
|                             void *p_rng ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           Free a context's content | ||||
|  * \brief           This clears an ECJPAKE context and frees any | ||||
|  *                  embedded data structure. | ||||
|  * | ||||
|  * \param ctx       context to free | ||||
|  * \param ctx       The ECJPAKE context to free. This may be \c NULL, | ||||
|  *                  in which case this function does nothing. If it is not | ||||
|  *                  \c NULL, it must point to an initialized ECJPAKE context. | ||||
|  */ | ||||
| void mbedtls_ecjpake_free( mbedtls_ecjpake_context *ctx ); | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  |  | |||
|  | @ -36,6 +36,12 @@ | |||
| #ifndef MBEDTLS_ECP_H | ||||
| #define MBEDTLS_ECP_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "bignum.h" | ||||
| 
 | ||||
| /*
 | ||||
|  | @ -189,6 +195,68 @@ typedef struct mbedtls_ecp_group | |||
| } | ||||
| mbedtls_ecp_group; | ||||
| 
 | ||||
| /**
 | ||||
|  * \name SECTION: Module settings | ||||
|  * | ||||
|  * The configuration options you can set for this module are in this section. | ||||
|  * Either change them in config.h, or define them using the compiler command line. | ||||
|  * \{ | ||||
|  */ | ||||
| 
 | ||||
| #if !defined(MBEDTLS_ECP_MAX_BITS) | ||||
| /**
 | ||||
|  * The maximum size of the groups, that is, of \c N and \c P. | ||||
|  */ | ||||
| #define MBEDTLS_ECP_MAX_BITS     521   /**< The maximum size of groups, in bits. */ | ||||
| #endif | ||||
| 
 | ||||
| #define MBEDTLS_ECP_MAX_BYTES    ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 ) | ||||
| #define MBEDTLS_ECP_MAX_PT_LEN   ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 ) | ||||
| 
 | ||||
| #if !defined(MBEDTLS_ECP_WINDOW_SIZE) | ||||
| /*
 | ||||
|  * Maximum "window" size used for point multiplication. | ||||
|  * Default: 6. | ||||
|  * Minimum value: 2. Maximum value: 7. | ||||
|  * | ||||
|  * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) ) | ||||
|  * points used for point multiplication. This value is directly tied to EC | ||||
|  * peak memory usage, so decreasing it by one should roughly cut memory usage | ||||
|  * by two (if large curves are in use). | ||||
|  * | ||||
|  * Reduction in size may reduce speed, but larger curves are impacted first. | ||||
|  * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1): | ||||
|  *      w-size:     6       5       4       3       2 | ||||
|  *      521       145     141     135     120      97 | ||||
|  *      384       214     209     198     177     146 | ||||
|  *      256       320     320     303     262     226 | ||||
|  *      224       475     475     453     398     342 | ||||
|  *      192       640     640     633     587     476 | ||||
|  */ | ||||
| #define MBEDTLS_ECP_WINDOW_SIZE    6   /**< The maximum window size used. */ | ||||
| #endif /* MBEDTLS_ECP_WINDOW_SIZE */ | ||||
| 
 | ||||
| #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM) | ||||
| /*
 | ||||
|  * Trade memory for speed on fixed-point multiplication. | ||||
|  * | ||||
|  * This speeds up repeated multiplication of the generator (that is, the | ||||
|  * multiplication in ECDSA signatures, and half of the multiplications in | ||||
|  * ECDSA verification and ECDHE) by a factor roughly 3 to 4. | ||||
|  * | ||||
|  * The cost is increasing EC peak memory usage by a factor roughly 2. | ||||
|  * | ||||
|  * Change this value to 0 to reduce peak memory usage. | ||||
|  */ | ||||
| #define MBEDTLS_ECP_FIXED_POINT_OPTIM  1   /**< Enable fixed-point speed-up. */ | ||||
| #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */ | ||||
| 
 | ||||
| /* \} name SECTION: Module settings */ | ||||
| 
 | ||||
| #else  /* MBEDTLS_ECP_ALT */ | ||||
| #include "ecp_alt.h" | ||||
| #endif /* MBEDTLS_ECP_ALT */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_ECP_RESTARTABLE) | ||||
| 
 | ||||
| /**
 | ||||
|  | @ -253,68 +321,6 @@ typedef void mbedtls_ecp_restart_ctx; | |||
| 
 | ||||
| #endif /* MBEDTLS_ECP_RESTARTABLE */ | ||||
| 
 | ||||
| /**
 | ||||
|  * \name SECTION: Module settings | ||||
|  * | ||||
|  * The configuration options you can set for this module are in this section. | ||||
|  * Either change them in config.h, or define them using the compiler command line. | ||||
|  * \{ | ||||
|  */ | ||||
| 
 | ||||
| #if !defined(MBEDTLS_ECP_MAX_BITS) | ||||
| /**
 | ||||
|  * The maximum size of the groups, that is, of \c N and \c P. | ||||
|  */ | ||||
| #define MBEDTLS_ECP_MAX_BITS     521   /**< The maximum size of groups, in bits. */ | ||||
| #endif | ||||
| 
 | ||||
| #define MBEDTLS_ECP_MAX_BYTES    ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 ) | ||||
| #define MBEDTLS_ECP_MAX_PT_LEN   ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 ) | ||||
| 
 | ||||
| #if !defined(MBEDTLS_ECP_WINDOW_SIZE) | ||||
| /*
 | ||||
|  * Maximum "window" size used for point multiplication. | ||||
|  * Default: 6. | ||||
|  * Minimum value: 2. Maximum value: 7. | ||||
|  * | ||||
|  * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) ) | ||||
|  * points used for point multiplication. This value is directly tied to EC | ||||
|  * peak memory usage, so decreasing it by one should roughly cut memory usage | ||||
|  * by two (if large curves are in use). | ||||
|  * | ||||
|  * Reduction in size may reduce speed, but larger curves are impacted first. | ||||
|  * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1): | ||||
|  *      w-size:     6       5       4       3       2 | ||||
|  *      521       145     141     135     120      97 | ||||
|  *      384       214     209     198     177     146 | ||||
|  *      256       320     320     303     262     226 | ||||
|  *      224       475     475     453     398     342 | ||||
|  *      192       640     640     633     587     476 | ||||
|  */ | ||||
| #define MBEDTLS_ECP_WINDOW_SIZE    6   /**< The maximum window size used. */ | ||||
| #endif /* MBEDTLS_ECP_WINDOW_SIZE */ | ||||
| 
 | ||||
| #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM) | ||||
| /*
 | ||||
|  * Trade memory for speed on fixed-point multiplication. | ||||
|  * | ||||
|  * This speeds up repeated multiplication of the generator (that is, the | ||||
|  * multiplication in ECDSA signatures, and half of the multiplications in | ||||
|  * ECDSA verification and ECDHE) by a factor roughly 3 to 4. | ||||
|  * | ||||
|  * The cost is increasing EC peak memory usage by a factor roughly 2. | ||||
|  * | ||||
|  * Change this value to 0 to reduce peak memory usage. | ||||
|  */ | ||||
| #define MBEDTLS_ECP_FIXED_POINT_OPTIM  1   /**< Enable fixed-point speed-up. */ | ||||
| #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */ | ||||
| 
 | ||||
| /* \} name SECTION: Module settings */ | ||||
| 
 | ||||
| #else  /* MBEDTLS_ECP_ALT */ | ||||
| #include "ecp_alt.h" | ||||
| #endif /* MBEDTLS_ECP_ALT */ | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief    The ECP key-pair structure. | ||||
|  * | ||||
|  | @ -497,24 +503,37 @@ void mbedtls_ecp_point_free( mbedtls_ecp_point *pt ); | |||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function frees the components of an ECP group. | ||||
|  * \param grp       The group to free. | ||||
|  * | ||||
|  * \param grp       The group to free. This may be \c NULL, in which | ||||
|  *                  case this function returns immediately. If it is not | ||||
|  *                  \c NULL, it must point to an initialized ECP group. | ||||
|  */ | ||||
| void mbedtls_ecp_group_free( mbedtls_ecp_group *grp ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function frees the components of a key pair. | ||||
|  * \param key       The key pair to free. | ||||
|  * | ||||
|  * \param key       The key pair to free. This may be \c NULL, in which | ||||
|  *                  case this function returns immediately. If it is not | ||||
|  *                  \c NULL, it must point to an initialized ECP key pair. | ||||
|  */ | ||||
| void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_ECP_RESTARTABLE) | ||||
| /**
 | ||||
|  * \brief           Initialize a restart context | ||||
|  * \brief           Initialize a restart context. | ||||
|  * | ||||
|  * \param ctx       The restart context to initialize. This must | ||||
|  *                  not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           Free the components of a restart context | ||||
|  * \brief           Free the components of a restart context. | ||||
|  * | ||||
|  * \param ctx       The restart context to free. This may be \c NULL, in which | ||||
|  *                  case this function returns immediately. If it is not | ||||
|  *                  \c NULL, it must point to an initialized restart context. | ||||
|  */ | ||||
| void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx ); | ||||
| #endif /* MBEDTLS_ECP_RESTARTABLE */ | ||||
|  | @ -523,11 +542,12 @@ void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx ); | |||
|  * \brief           This function copies the contents of point \p Q into | ||||
|  *                  point \p P. | ||||
|  * | ||||
|  * \param P         The destination point. | ||||
|  * \param Q         The source point. | ||||
|  * \param P         The destination point. This must be initialized. | ||||
|  * \param Q         The source point. This must be initialized. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. | ||||
|  * \return          Another negative error code for other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q ); | ||||
| 
 | ||||
|  | @ -535,31 +555,35 @@ int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q ); | |||
|  * \brief           This function copies the contents of group \p src into | ||||
|  *                  group \p dst. | ||||
|  * | ||||
|  * \param dst       The destination group. | ||||
|  * \param src       The source group. | ||||
|  * \param dst       The destination group. This must be initialized. | ||||
|  * \param src       The source group. This must be initialized. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src ); | ||||
| int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, | ||||
|                             const mbedtls_ecp_group *src ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function sets a point to zero. | ||||
|  * \brief           This function sets a point to the point at infinity. | ||||
|  * | ||||
|  * \param pt        The point to set. | ||||
|  * \param pt        The point to set. This must be initialized. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function checks if a point is zero. | ||||
|  * \brief           This function checks if a point is the point at infinity. | ||||
|  * | ||||
|  * \param pt        The point to test. | ||||
|  * \param pt        The point to test. This must be initialized. | ||||
|  * | ||||
|  * \return          \c 1 if the point is zero. | ||||
|  * \return          \c 0 if the point is non-zero. | ||||
|  * \return          A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt ); | ||||
| 
 | ||||
|  | @ -569,8 +593,8 @@ int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt ); | |||
|  * \note            This assumes that the points are normalized. Otherwise, | ||||
|  *                  they may compare as "not equal" even if they are. | ||||
|  * | ||||
|  * \param P         The first point to compare. | ||||
|  * \param Q         The second point to compare. | ||||
|  * \param P         The first point to compare. This must be initialized. | ||||
|  * \param Q         The second point to compare. This must be initialized. | ||||
|  * | ||||
|  * \return          \c 0 if the points are equal. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal. | ||||
|  | @ -582,7 +606,7 @@ int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P, | |||
|  * \brief           This function imports a non-zero point from two ASCII | ||||
|  *                  strings. | ||||
|  * | ||||
|  * \param P         The destination point. | ||||
|  * \param P         The destination point. This must be initialized. | ||||
|  * \param radix     The numeric base of the input. | ||||
|  * \param x         The first affine coordinate, as a null-terminated string. | ||||
|  * \param y         The second affine coordinate, as a null-terminated string. | ||||
|  | @ -597,15 +621,21 @@ int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix, | |||
|  * \brief           This function exports a point into unsigned binary data. | ||||
|  * | ||||
|  * \param grp       The group to which the point should belong. | ||||
|  * \param P         The point to export. | ||||
|  * \param format    The point format. Should be an \c MBEDTLS_ECP_PF_XXX macro. | ||||
|  * \param olen      The length of the output. | ||||
|  * \param buf       The output buffer. | ||||
|  * \param buflen    The length of the output buffer. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param P         The point to export. This must be initialized. | ||||
|  * \param format    The point format. This must be either | ||||
|  *                  #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED. | ||||
|  * \param olen      The address at which to store the length of | ||||
|  *                  the output in Bytes. This must not be \c NULL. | ||||
|  * \param buf       The output buffer. This must be a writable buffer | ||||
|  *                  of length \p buflen Bytes. | ||||
|  * \param buflen    The length of the output buffer \p buf in Bytes. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA | ||||
|  *                  or #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output buffer | ||||
|  *                  is too small to hold the point. | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P, | ||||
|                             int format, size_t *olen, | ||||
|  | @ -619,19 +649,23 @@ int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ | |||
|  *                  for that. | ||||
|  * | ||||
|  * \param grp       The group to which the point should belong. | ||||
|  * \param P         The point to import. | ||||
|  * \param buf       The input buffer. | ||||
|  * \param ilen      The length of the input. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param P         The destination context to import the point to. | ||||
|  *                  This must be initialized. | ||||
|  * \param buf       The input buffer. This must be a readable buffer | ||||
|  *                  of length \p ilen Bytes. | ||||
|  * \param ilen      The length of the input buffer \p buf in Bytes. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid. | ||||
|  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. | ||||
|  * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format | ||||
|  *                  is not implemented. | ||||
|  * | ||||
|  */ | ||||
| int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P, | ||||
|                            const unsigned char *buf, size_t ilen ); | ||||
| int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, | ||||
|                                    mbedtls_ecp_point *P, | ||||
|                                    const unsigned char *buf, size_t ilen ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function imports a point from a TLS ECPoint record. | ||||
|  | @ -639,7 +673,9 @@ int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_poi | |||
|  * \note            On function return, \p *buf is updated to point immediately | ||||
|  *                  after the ECPoint record. | ||||
|  * | ||||
|  * \param grp       The ECP group used. | ||||
|  * \param grp       The ECP group to use. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param pt        The destination point. | ||||
|  * \param buf       The address of the pointer to the start of the input buffer. | ||||
|  * \param len       The length of the buffer. | ||||
|  | @ -649,99 +685,122 @@ int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_poi | |||
|  *                  failure. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid. | ||||
|  */ | ||||
| int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt, | ||||
|                         const unsigned char **buf, size_t len ); | ||||
| int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, | ||||
|                                 mbedtls_ecp_point *pt, | ||||
|                                 const unsigned char **buf, size_t len ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function exports a point as a TLS ECPoint record. | ||||
|  * \brief           This function exports a point as a TLS ECPoint record | ||||
|  *                  defined in RFC 4492, Section 5.4. | ||||
|  * | ||||
|  * \param grp       The ECP group used. | ||||
|  * \param pt        The point format to export to. The point format is an | ||||
|  *                  \c MBEDTLS_ECP_PF_XXX constant. | ||||
|  * \param format    The export format. | ||||
|  * \param olen      The length of the data written. | ||||
|  * \param buf       The buffer to write to. | ||||
|  * \param blen      The length of the buffer. | ||||
|  * \param grp       The ECP group to use. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param pt        The point to be exported. This must be initialized. | ||||
|  * \param format    The point format to use. This must be either | ||||
|  *                  #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED. | ||||
|  * \param olen      The address at which to store the length in Bytes | ||||
|  *                  of the data written. | ||||
|  * \param buf       The target buffer. This must be a writable buffer of | ||||
|  *                  length \p blen Bytes. | ||||
|  * \param blen      The length of the target buffer \p buf in Bytes. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA or | ||||
|  *                  #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the target buffer | ||||
|  *                  is too small to hold the exported point. | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt, | ||||
|                          int format, size_t *olen, | ||||
|                          unsigned char *buf, size_t blen ); | ||||
| int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, | ||||
|                                  const mbedtls_ecp_point *pt, | ||||
|                                  int format, size_t *olen, | ||||
|                                  unsigned char *buf, size_t blen ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function sets a group using standardized domain parameters. | ||||
|  * \brief           This function sets up an ECP group context | ||||
|  *                  from a standardized set of domain parameters. | ||||
|  * | ||||
|  * \note            The index should be a value of the NamedCurve enum, | ||||
|  *                  as defined in <em>RFC-4492: Elliptic Curve Cryptography | ||||
|  *                  (ECC) Cipher Suites for Transport Layer Security (TLS)</em>, | ||||
|  *                  usually in the form of an \c MBEDTLS_ECP_DP_XXX macro. | ||||
|  * | ||||
|  * \param grp       The destination group. | ||||
|  * \param grp       The group context to setup. This must be initialized. | ||||
|  * \param id        The identifier of the domain parameter set to load. | ||||
|  * | ||||
|  * \return          \c 0 on success, | ||||
|  * \return          An \c MBEDTLS_ERR_MPI_XXX error code on initialization failure. | ||||
|  * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups. | ||||
| 
 | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p id doesn't | ||||
|  *                  correspond to a known group. | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function sets a group from a TLS ECParameters record. | ||||
|  * \brief           This function sets up an ECP group context from a TLS | ||||
|  *                  ECParameters record as defined in RFC 4492, Section 5.4. | ||||
|  * | ||||
|  * \note            \p buf is updated to point right after the ECParameters | ||||
|  *                  record on exit. | ||||
|  * \note            The read pointer \p buf is updated to point right after | ||||
|  *                  the ECParameters record on exit. | ||||
|  * | ||||
|  * \param grp       The destination group. | ||||
|  * \param grp       The group context to setup. This must be initialized. | ||||
|  * \param buf       The address of the pointer to the start of the input buffer. | ||||
|  * \param len       The length of the buffer. | ||||
|  * \param len       The length of the input buffer \c *buf in Bytes. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          An \c MBEDTLS_ERR_MPI_XXX error code on initialization | ||||
|  *                  failure. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid. | ||||
|  * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not | ||||
|  *                  recognised. | ||||
|  *                  recognized. | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len ); | ||||
| int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, | ||||
|                                 const unsigned char **buf, size_t len ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function reads a group from a TLS ECParameters record. | ||||
|  * \brief           This function extracts an elliptic curve group ID from a | ||||
|  *                  TLS ECParameters record as defined in RFC 4492, Section 5.4. | ||||
|  * | ||||
|  * \note            \p buf is updated to point right after the ECParameters | ||||
|  *                  record on exit. | ||||
|  * \note            The read pointer \p buf is updated to point right after | ||||
|  *                  the ECParameters record on exit. | ||||
|  * | ||||
|  * \param grp       Output parameter to hold the group id. | ||||
|  * \param grp       The address at which to store the group id. | ||||
|  *                  This must not be \c NULL. | ||||
|  * \param buf       The address of the pointer to the start of the input buffer. | ||||
|  * \param len       The length of the buffer. | ||||
|  * \param len       The length of the input buffer \c *buf in Bytes. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid. | ||||
|  * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not | ||||
|  *                  recognised. | ||||
|  *                  recognized. | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_ecp_tls_read_group_id( mbedtls_ecp_group_id *grp, | ||||
|                                    const unsigned char **buf, size_t len ); | ||||
|                                    const unsigned char **buf, | ||||
|                                    size_t len ); | ||||
| /**
 | ||||
|  * \brief           This function writes the TLS ECParameters record for a group. | ||||
|  * \brief           This function exports an elliptic curve as a TLS | ||||
|  *                  ECParameters record as defined in RFC 4492, Section 5.4. | ||||
|  * | ||||
|  * \param grp       The ECP group used. | ||||
|  * \param olen      The number of Bytes written. | ||||
|  * \param buf       The buffer to write to. | ||||
|  * \param blen      The length of the buffer. | ||||
|  * \param grp       The ECP group to be exported. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param olen      The address at which to store the number of Bytes written. | ||||
|  *                  This must not be \c NULL. | ||||
|  * \param buf       The buffer to write to. This must be a writable buffer | ||||
|  *                  of length \p blen Bytes. | ||||
|  * \param blen      The length of the output buffer \p buf in Bytes. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output | ||||
|  *                  buffer is too small to hold the exported group. | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen, | ||||
|                          unsigned char *buf, size_t blen ); | ||||
| int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, | ||||
|                                  size_t *olen, | ||||
|                                  unsigned char *buf, size_t blen ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function performs multiplication of a point by | ||||
|  *                  an integer: \p R = \p m * \p P. | ||||
|  * \brief           This function performs a scalar multiplication of a point | ||||
|  *                  by an integer: \p R = \p m * \p P. | ||||
|  * | ||||
|  *                  It is not thread-safe to use same group in multiple threads. | ||||
|  * | ||||
|  | @ -755,17 +814,22 @@ int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen, | |||
|  *                  targeting these results. We recommend always providing | ||||
|  *                  a non-NULL \p f_rng. The overhead is negligible. | ||||
|  * | ||||
|  * \param grp       The ECP group. | ||||
|  * \param R         The destination point. | ||||
|  * \param m         The integer by which to multiply. | ||||
|  * \param P         The point to multiply. | ||||
|  * \param f_rng     The RNG function. | ||||
|  * \param p_rng     The RNG context. | ||||
|  * \param grp       The ECP group to use. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param R         The point in which to store the result of the calculation. | ||||
|  *                  This must be initialized. | ||||
|  * \param m         The integer by which to multiply. This must be initialized. | ||||
|  * \param P         The point to multiply. This must be initialized. | ||||
|  * \param f_rng     The RNG function. This may be \c NULL if randomization | ||||
|  *                  of intermediate results isn't desired (discouraged). | ||||
|  * \param p_rng     The RNG context to be passed to \p p_rng. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private | ||||
|  *                  key, or \p P is not a valid public key. | ||||
|  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, | ||||
|              const mbedtls_mpi *m, const mbedtls_ecp_point *P, | ||||
|  | @ -781,12 +845,16 @@ int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, | |||
|  *                  it can return early and restart according to the limit set | ||||
|  *                  with \c mbedtls_ecp_set_max_ops() to reduce blocking. | ||||
|  * | ||||
|  * \param grp       The ECP group. | ||||
|  * \param R         The destination point. | ||||
|  * \param m         The integer by which to multiply. | ||||
|  * \param P         The point to multiply. | ||||
|  * \param f_rng     The RNG function. | ||||
|  * \param p_rng     The RNG context. | ||||
|  * \param grp       The ECP group to use. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param R         The point in which to store the result of the calculation. | ||||
|  *                  This must be initialized. | ||||
|  * \param m         The integer by which to multiply. This must be initialized. | ||||
|  * \param P         The point to multiply. This must be initialized. | ||||
|  * \param f_rng     The RNG function. This may be \c NULL if randomization | ||||
|  *                  of intermediate results isn't desired (discouraged). | ||||
|  * \param p_rng     The RNG context to be passed to \p p_rng. | ||||
|  * \param rs_ctx    The restart context (NULL disables restart). | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  | @ -795,6 +863,7 @@ int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, | |||
|  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. | ||||
|  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of | ||||
|  *                  operations was reached: see \c mbedtls_ecp_set_max_ops(). | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, | ||||
|              const mbedtls_mpi *m, const mbedtls_ecp_point *P, | ||||
|  | @ -810,18 +879,25 @@ int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, | |||
|  * \note            In contrast to mbedtls_ecp_mul(), this function does not | ||||
|  *                  guarantee a constant execution flow and timing. | ||||
|  * | ||||
|  * \param grp       The ECP group. | ||||
|  * \param R         The destination point. | ||||
|  * \param grp       The ECP group to use. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param R         The point in which to store the result of the calculation. | ||||
|  *                  This must be initialized. | ||||
|  * \param m         The integer by which to multiply \p P. | ||||
|  * \param P         The point to multiply by \p m. | ||||
|  *                  This must be initialized. | ||||
|  * \param P         The point to multiply by \p m. This must be initialized. | ||||
|  * \param n         The integer by which to multiply \p Q. | ||||
|  *                  This must be initialized. | ||||
|  * \param Q         The point to be multiplied by \p n. | ||||
|  *                  This must be initialized. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not | ||||
|  *                  valid private keys, or \p P or \p Q are not valid public | ||||
|  *                  keys. | ||||
|  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, | ||||
|              const mbedtls_mpi *m, const mbedtls_ecp_point *P, | ||||
|  | @ -838,12 +914,18 @@ int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, | |||
|  *                  but it can return early and restart according to the limit | ||||
|  *                  set with \c mbedtls_ecp_set_max_ops() to reduce blocking. | ||||
|  * | ||||
|  * \param grp       The ECP group. | ||||
|  * \param R         The destination point. | ||||
|  * \param grp       The ECP group to use. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param R         The point in which to store the result of the calculation. | ||||
|  *                  This must be initialized. | ||||
|  * \param m         The integer by which to multiply \p P. | ||||
|  * \param P         The point to multiply by \p m. | ||||
|  *                  This must be initialized. | ||||
|  * \param P         The point to multiply by \p m. This must be initialized. | ||||
|  * \param n         The integer by which to multiply \p Q. | ||||
|  *                  This must be initialized. | ||||
|  * \param Q         The point to be multiplied by \p n. | ||||
|  *                  This must be initialized. | ||||
|  * \param rs_ctx    The restart context (NULL disables restart). | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  | @ -853,6 +935,7 @@ int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, | |||
|  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure. | ||||
|  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of | ||||
|  *                  operations was reached: see \c mbedtls_ecp_set_max_ops(). | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_ecp_muladd_restartable( | ||||
|              mbedtls_ecp_group *grp, mbedtls_ecp_point *R, | ||||
|  | @ -877,38 +960,51 @@ int mbedtls_ecp_muladd_restartable( | |||
|  *                  structures, such as ::mbedtls_ecdh_context or | ||||
|  *                  ::mbedtls_ecdsa_context. | ||||
|  * | ||||
|  * \param grp       The curve the point should lie on. | ||||
|  * \param pt        The point to check. | ||||
|  * \param grp       The ECP group the point should belong to. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param pt        The point to check. This must be initialized. | ||||
|  * | ||||
|  * \return          \c 0 if the point is a valid public key. | ||||
|  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY on failure. | ||||
|  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not | ||||
|  *                  a valid public key for the given curve. | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt ); | ||||
| int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, | ||||
|                               const mbedtls_ecp_point *pt ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function checks that an \p mbedtls_mpi is a valid private | ||||
|  *                  key for this curve. | ||||
|  * \brief           This function checks that an \p mbedtls_mpi is a | ||||
|  *                  valid private key for this curve. | ||||
|  * | ||||
|  * \note            This function uses bare components rather than an | ||||
|  *                  ::mbedtls_ecp_keypair structure to ease use with other | ||||
|  *                  structures, such as ::mbedtls_ecdh_context or | ||||
|  *                  ::mbedtls_ecdsa_context. | ||||
|  * | ||||
|  * \param grp       The group used. | ||||
|  * \param d         The integer to check. | ||||
|  * \param grp       The ECP group the private key should belong to. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param d         The integer to check. This must be initialized. | ||||
|  * | ||||
|  * \return          \c 0 if the point is a valid private key. | ||||
|  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY on failure. | ||||
|  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not a valid | ||||
|  *                  private key for the given curve. | ||||
|  * \return          Another negative error code on other kinds of failure. | ||||
|  */ | ||||
| int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d ); | ||||
| int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, | ||||
|                                const mbedtls_mpi *d ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function generates a private key. | ||||
|  * | ||||
|  * \param grp       The ECP group. | ||||
|  * \param d         The destination MPI (secret part). | ||||
|  * \param f_rng     The RNG function. | ||||
|  * \param p_rng     The RNG parameter. | ||||
|  * \param grp       The ECP group to generate a private key for. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param d         The destination MPI (secret part). This must be initialized. | ||||
|  * \param f_rng     The RNG function. This must not be \c NULL. | ||||
|  * \param p_rng     The RNG parameter to be passed to \p f_rng. This may be | ||||
|  *                  \c NULL if \p f_rng doesn't need a context argument. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code | ||||
|  | @ -928,22 +1024,29 @@ int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp, | |||
|  *                  structures, such as ::mbedtls_ecdh_context or | ||||
|  *                  ::mbedtls_ecdsa_context. | ||||
|  * | ||||
|  * \param grp       The ECP group. | ||||
|  * \param G         The chosen base point. | ||||
|  * \param grp       The ECP group to generate a key pair for. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param G         The base point to use. This must be initialized | ||||
|  *                  and belong to \p grp. It replaces the default base | ||||
|  *                  point \c grp->G used by mbedtls_ecp_gen_keypair(). | ||||
|  * \param d         The destination MPI (secret part). | ||||
|  *                  This must be initialized. | ||||
|  * \param Q         The destination point (public part). | ||||
|  * \param f_rng     The RNG function. | ||||
|  * \param p_rng     The RNG context. | ||||
|  *                  This must be initialized. | ||||
|  * \param f_rng     The RNG function. This must not be \c NULL. | ||||
|  * \param p_rng     The RNG context to be passed to \p f_rng. This may | ||||
|  *                  be \c NULL if \p f_rng doesn't need a context argument. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code | ||||
|  *                  on failure. | ||||
|  */ | ||||
| int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp, | ||||
|                      const mbedtls_ecp_point *G, | ||||
|                      mbedtls_mpi *d, mbedtls_ecp_point *Q, | ||||
|                      int (*f_rng)(void *, unsigned char *, size_t), | ||||
|                      void *p_rng ); | ||||
|                                   const mbedtls_ecp_point *G, | ||||
|                                   mbedtls_mpi *d, mbedtls_ecp_point *Q, | ||||
|                                   int (*f_rng)(void *, unsigned char *, size_t), | ||||
|                                   void *p_rng ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function generates an ECP keypair. | ||||
|  | @ -953,34 +1056,42 @@ int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp, | |||
|  *                  structures, such as ::mbedtls_ecdh_context or | ||||
|  *                  ::mbedtls_ecdsa_context. | ||||
|  * | ||||
|  * \param grp       The ECP group. | ||||
|  * \param grp       The ECP group to generate a key pair for. | ||||
|  *                  This must be initialized and have group parameters | ||||
|  *                  set, for example through mbedtls_ecp_group_load(). | ||||
|  * \param d         The destination MPI (secret part). | ||||
|  *                  This must be initialized. | ||||
|  * \param Q         The destination point (public part). | ||||
|  * \param f_rng     The RNG function. | ||||
|  * \param p_rng     The RNG context. | ||||
|  *                  This must be initialized. | ||||
|  * \param f_rng     The RNG function. This must not be \c NULL. | ||||
|  * \param p_rng     The RNG context to be passed to \p f_rng. This may | ||||
|  *                  be \c NULL if \p f_rng doesn't need a context argument. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code | ||||
|  *                  on failure. | ||||
|  */ | ||||
| int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, | ||||
|                      int (*f_rng)(void *, unsigned char *, size_t), | ||||
|                      void *p_rng ); | ||||
| int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, | ||||
|                              mbedtls_ecp_point *Q, | ||||
|                              int (*f_rng)(void *, unsigned char *, size_t), | ||||
|                              void *p_rng ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function generates an ECP key. | ||||
|  * | ||||
|  * \param grp_id    The ECP group identifier. | ||||
|  * \param key       The destination key. | ||||
|  * \param f_rng     The RNG function. | ||||
|  * \param p_rng     The RNG context. | ||||
|  * \param key       The destination key. This must be initialized. | ||||
|  * \param f_rng     The RNG function to use. This must not be \c NULL. | ||||
|  * \param p_rng     The RNG context to be passed to \p f_rng. This may | ||||
|  *                  be \c NULL if \p f_rng doesn't need a context argument. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code | ||||
|  *                  on failure. | ||||
|  */ | ||||
| int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, | ||||
|                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ); | ||||
|                          int (*f_rng)(void *, unsigned char *, size_t), | ||||
|                          void *p_rng ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           This function checks that the keypair objects | ||||
|  | @ -988,16 +1099,19 @@ int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, | |||
|  *                  same public point, and that the private key in | ||||
|  *                  \p prv is consistent with the public key. | ||||
|  * | ||||
|  * \param pub       The keypair structure holding the public key. | ||||
|  *                  If it contains a private key, that part is ignored. | ||||
|  * \param pub       The keypair structure holding the public key. This | ||||
|  *                  must be initialized. If it contains a private key, that | ||||
|  *                  part is ignored. | ||||
|  * \param prv       The keypair structure holding the full keypair. | ||||
|  *                  This must be initialized. | ||||
|  * | ||||
|  * \return          \c 0 on success, meaning that the keys are valid and match. | ||||
|  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match. | ||||
|  * \return          An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX | ||||
|  *                  error code on calculation failure. | ||||
|  */ | ||||
| int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv ); | ||||
| int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, | ||||
|                                 const mbedtls_ecp_keypair *prv ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
|  |  | |||
|  | @ -61,6 +61,12 @@ | |||
| #ifndef MBEDTLS_ECP_INTERNAL_H | ||||
| #define MBEDTLS_ECP_INTERNAL_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #if defined(MBEDTLS_ECP_INTERNAL_ALT) | ||||
| 
 | ||||
| /**
 | ||||
|  |  | |||
|  | @ -24,6 +24,12 @@ | |||
| #ifndef MBEDTLS_ERROR_H | ||||
| #define MBEDTLS_ERROR_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include <stddef.h> | ||||
| 
 | ||||
| /**
 | ||||
|  | @ -74,9 +80,9 @@ | |||
|  * MD4       1                  0x002D-0x002D | ||||
|  * MD5       1                  0x002F-0x002F | ||||
|  * RIPEMD160 1                  0x0031-0x0031 | ||||
|  * SHA1      1                  0x0035-0x0035 | ||||
|  * SHA256    1                  0x0037-0x0037 | ||||
|  * SHA512    1                  0x0039-0x0039 | ||||
|  * SHA1      1                  0x0035-0x0035 0x0073-0x0073 | ||||
|  * SHA256    1                  0x0037-0x0037 0x0074-0x0074 | ||||
|  * SHA512    1                  0x0039-0x0039 0x0075-0x0075 | ||||
|  * CHACHA20  3                  0x0051-0x0055 | ||||
|  * POLY1305  3                  0x0057-0x005B | ||||
|  * CHACHAPOLY 2 0x0054-0x0056 | ||||
|  |  | |||
|  | @ -33,6 +33,12 @@ | |||
| #ifndef MBEDTLS_GCM_H | ||||
| #define MBEDTLS_GCM_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "cipher.h" | ||||
| 
 | ||||
| #include <stdint.h> | ||||
|  | @ -85,7 +91,7 @@ mbedtls_gcm_context; | |||
|  *                  cipher, nor set the key. For this purpose, use | ||||
|  *                  mbedtls_gcm_setkey(). | ||||
|  * | ||||
|  * \param ctx       The GCM context to initialize. | ||||
|  * \param ctx       The GCM context to initialize. This must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_gcm_init( mbedtls_gcm_context *ctx ); | ||||
| 
 | ||||
|  | @ -93,9 +99,10 @@ void mbedtls_gcm_init( mbedtls_gcm_context *ctx ); | |||
|  * \brief           This function associates a GCM context with a | ||||
|  *                  cipher algorithm and a key. | ||||
|  * | ||||
|  * \param ctx       The GCM context to initialize. | ||||
|  * \param ctx       The GCM context. This must be initialized. | ||||
|  * \param cipher    The 128-bit block cipher to use. | ||||
|  * \param key       The encryption key. | ||||
|  * \param key       The encryption key. This must be a readable buffer of at | ||||
|  *                  least \p keybits bits. | ||||
|  * \param keybits   The key size in bits. Valid options are: | ||||
|  *                  <ul><li>128 bits</li> | ||||
|  *                  <li>192 bits</li> | ||||
|  | @ -122,7 +129,8 @@ int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx, | |||
|  *                  authentic. You should use this function to perform encryption | ||||
|  *                  only. For decryption, use mbedtls_gcm_auth_decrypt() instead. | ||||
|  * | ||||
|  * \param ctx       The GCM context to use for encryption or decryption. | ||||
|  * \param ctx       The GCM context to use for encryption or decryption. This | ||||
|  *                  must be initialized. | ||||
|  * \param mode      The operation to perform: | ||||
|  *                  - #MBEDTLS_GCM_ENCRYPT to perform authenticated encryption. | ||||
|  *                    The ciphertext is written to \p output and the | ||||
|  | @ -136,21 +144,27 @@ int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx, | |||
|  *                    calling this function in decryption mode. | ||||
|  * \param length    The length of the input data, which is equal to the length | ||||
|  *                  of the output data. | ||||
|  * \param iv        The initialization vector. | ||||
|  * \param iv        The initialization vector. This must be a readable buffer of | ||||
|  *                  at least \p iv_len Bytes. | ||||
|  * \param iv_len    The length of the IV. | ||||
|  * \param add       The buffer holding the additional data. | ||||
|  * \param add       The buffer holding the additional data. This must be of at | ||||
|  *                  least that size in Bytes. | ||||
|  * \param add_len   The length of the additional data. | ||||
|  * \param input     The buffer holding the input data. Its size is \b length. | ||||
|  * \param output    The buffer for holding the output data. It must have room | ||||
|  *                  for \b length bytes. | ||||
|  * \param input     The buffer holding the input data. If \p length is greater | ||||
|  *                  than zero, this must be a readable buffer of at least that | ||||
|  *                  size in Bytes. | ||||
|  * \param output    The buffer for holding the output data. If \p length is greater | ||||
|  *                  than zero, this must be a writable buffer of at least that | ||||
|  *                  size in Bytes. | ||||
|  * \param tag_len   The length of the tag to generate. | ||||
|  * \param tag       The buffer for holding the tag. | ||||
|  * \param tag       The buffer for holding the tag. This must be a readable | ||||
|  *                  buffer of at least \p tag_len Bytes. | ||||
|  * | ||||
|  * \return          \c 0 if the encryption or decryption was performed | ||||
|  *                  successfully. Note that in #MBEDTLS_GCM_DECRYPT mode, | ||||
|  *                  this does not indicate that the data is authentic. | ||||
|  * \return          #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths are not valid or | ||||
|  *                  a cipher-specific error code if the encryption | ||||
|  * \return          #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths or pointers are | ||||
|  *                  not valid or a cipher-specific error code if the encryption | ||||
|  *                  or decryption failed. | ||||
|  */ | ||||
| int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx, | ||||
|  | @ -173,23 +187,30 @@ int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx, | |||
|  *                  input buffer. If the buffers overlap, the output buffer | ||||
|  *                  must trail at least 8 Bytes behind the input buffer. | ||||
|  * | ||||
|  * \param ctx       The GCM context. | ||||
|  * \param ctx       The GCM context. This must be initialized. | ||||
|  * \param length    The length of the ciphertext to decrypt, which is also | ||||
|  *                  the length of the decrypted plaintext. | ||||
|  * \param iv        The initialization vector. | ||||
|  * \param iv        The initialization vector. This must be a readable buffer | ||||
|  *                  of at least \p iv_len Bytes. | ||||
|  * \param iv_len    The length of the IV. | ||||
|  * \param add       The buffer holding the additional data. | ||||
|  * \param add       The buffer holding the additional data. This must be of at | ||||
|  *                  least that size in Bytes. | ||||
|  * \param add_len   The length of the additional data. | ||||
|  * \param tag       The buffer holding the tag to verify. | ||||
|  * \param tag       The buffer holding the tag to verify. This must be a | ||||
|  *                  readable buffer of at least \p tag_len Bytes. | ||||
|  * \param tag_len   The length of the tag to verify. | ||||
|  * \param input     The buffer holding the ciphertext. Its size is \b length. | ||||
|  * \param output    The buffer for holding the decrypted plaintext. It must | ||||
|  *                  have room for \b length bytes. | ||||
|  * \param input     The buffer holding the ciphertext. If \p length is greater | ||||
|  *                  than zero, this must be a readable buffer of at least that | ||||
|  *                  size. | ||||
|  * \param output    The buffer for holding the decrypted plaintext. If \p length | ||||
|  *                  is greater than zero, this must be a writable buffer of at | ||||
|  *                  least that size. | ||||
|  * | ||||
|  * \return          \c 0 if successful and authenticated. | ||||
|  * \return          #MBEDTLS_ERR_GCM_AUTH_FAILED if the tag does not match. | ||||
|  * \return          #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths are not valid or | ||||
|  *                  a cipher-specific error code if the decryption failed. | ||||
|  * \return          #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths or pointers are | ||||
|  *                  not valid or a cipher-specific error code if the decryption | ||||
|  *                  failed. | ||||
|  */ | ||||
| int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx, | ||||
|                       size_t length, | ||||
|  | @ -206,15 +227,16 @@ int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx, | |||
|  * \brief           This function starts a GCM encryption or decryption | ||||
|  *                  operation. | ||||
|  * | ||||
|  * \param ctx       The GCM context. | ||||
|  * \param ctx       The GCM context. This must be initialized. | ||||
|  * \param mode      The operation to perform: #MBEDTLS_GCM_ENCRYPT or | ||||
|  *                  #MBEDTLS_GCM_DECRYPT. | ||||
|  * \param iv        The initialization vector. | ||||
|  * \param iv        The initialization vector. This must be a readable buffer of | ||||
|  *                  at least \p iv_len Bytes. | ||||
|  * \param iv_len    The length of the IV. | ||||
|  * \param add       The buffer holding the additional data, or NULL | ||||
|  *                  if \p add_len is 0. | ||||
|  * \param add_len   The length of the additional data. If 0, | ||||
|  *                  \p add is NULL. | ||||
|  * \param add       The buffer holding the additional data, or \c NULL | ||||
|  *                  if \p add_len is \c 0. | ||||
|  * \param add_len   The length of the additional data. If \c 0, | ||||
|  *                  \p add may be \c NULL. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  */ | ||||
|  | @ -237,11 +259,15 @@ int mbedtls_gcm_starts( mbedtls_gcm_context *ctx, | |||
|  *                  input buffer. If the buffers overlap, the output buffer | ||||
|  *                  must trail at least 8 Bytes behind the input buffer. | ||||
|  * | ||||
|  * \param ctx       The GCM context. | ||||
|  * \param ctx       The GCM context. This must be initialized. | ||||
|  * \param length    The length of the input data. This must be a multiple of | ||||
|  *                  16 except in the last call before mbedtls_gcm_finish(). | ||||
|  * \param input     The buffer holding the input data. | ||||
|  * \param output    The buffer for holding the output data. | ||||
|  * \param input     The buffer holding the input data. If \p length is greater | ||||
|  *                  than zero, this must be a readable buffer of at least that | ||||
|  *                  size in Bytes. | ||||
|  * \param output    The buffer for holding the output data. If \p length is | ||||
|  *                  greater than zero, this must be a writable buffer of at | ||||
|  *                  least that size in Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         #MBEDTLS_ERR_GCM_BAD_INPUT on failure. | ||||
|  | @ -258,9 +284,11 @@ int mbedtls_gcm_update( mbedtls_gcm_context *ctx, | |||
|  *                  It wraps up the GCM stream, and generates the | ||||
|  *                  tag. The tag can have a maximum length of 16 Bytes. | ||||
|  * | ||||
|  * \param ctx       The GCM context. | ||||
|  * \param tag       The buffer for holding the tag. | ||||
|  * \param tag_len   The length of the tag to generate. Must be at least four. | ||||
|  * \param ctx       The GCM context. This must be initialized. | ||||
|  * \param tag       The buffer for holding the tag. This must be a readable | ||||
|  *                  buffer of at least \p tag_len Bytes. | ||||
|  * \param tag_len   The length of the tag to generate. This must be at least | ||||
|  *                  four. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_GCM_BAD_INPUT on failure. | ||||
|  | @ -273,10 +301,13 @@ int mbedtls_gcm_finish( mbedtls_gcm_context *ctx, | |||
|  * \brief           This function clears a GCM context and the underlying | ||||
|  *                  cipher sub-context. | ||||
|  * | ||||
|  * \param ctx       The GCM context to clear. | ||||
|  * \param ctx       The GCM context to clear. If this is \c NULL, the call has | ||||
|  *                  no effect. Otherwise, this must be initialized. | ||||
|  */ | ||||
| void mbedtls_gcm_free( mbedtls_gcm_context *ctx ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          The GCM checkup routine. | ||||
|  * | ||||
|  | @ -285,6 +316,8 @@ void mbedtls_gcm_free( mbedtls_gcm_context *ctx ); | |||
|  */ | ||||
| int mbedtls_gcm_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
|  | @ -24,6 +24,12 @@ | |||
| #ifndef MBEDTLS_HAVEGE_H | ||||
| #define MBEDTLS_HAVEGE_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include <stddef.h> | ||||
| 
 | ||||
| #define MBEDTLS_HAVEGE_COLLECT_SIZE 1024 | ||||
|  |  | |||
|  | @ -27,6 +27,12 @@ | |||
| #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" | ||||
| 
 | ||||
| /**
 | ||||
|  |  | |||
|  | @ -24,6 +24,12 @@ | |||
| #ifndef MBEDTLS_HMAC_DRBG_H | ||||
| #define MBEDTLS_HMAC_DRBG_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "md.h" | ||||
| 
 | ||||
| #if defined(MBEDTLS_THREADING_C) | ||||
|  | @ -76,7 +82,7 @@ extern "C" { | |||
|  */ | ||||
| typedef struct mbedtls_hmac_drbg_context | ||||
| { | ||||
|     /* Working state: the key K is not stored explicitely,
 | ||||
|     /* Working state: the key K is not stored explicitly,
 | ||||
|      * but is implied by the HMAC context */ | ||||
|     mbedtls_md_context_t md_ctx;                    /*!< HMAC context (inc. K)  */ | ||||
|     unsigned char V[MBEDTLS_MD_MAX_SIZE];  /*!< V in the spec          */ | ||||
|  |  | |||
|  | @ -283,6 +283,8 @@ MBEDTLS_DEPRECATED void mbedtls_md2( const unsigned char *input, | |||
| #undef MBEDTLS_DEPRECATED | ||||
| #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          Checkup routine | ||||
|  * | ||||
|  | @ -295,6 +297,8 @@ MBEDTLS_DEPRECATED void mbedtls_md2( const unsigned char *input, | |||
|  */ | ||||
| int mbedtls_md2_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
|  | @ -288,6 +288,8 @@ MBEDTLS_DEPRECATED void mbedtls_md4( const unsigned char *input, | |||
| #undef MBEDTLS_DEPRECATED | ||||
| #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          Checkup routine | ||||
|  * | ||||
|  | @ -300,6 +302,8 @@ MBEDTLS_DEPRECATED void mbedtls_md4( const unsigned char *input, | |||
|  */ | ||||
| int mbedtls_md4_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
|  | @ -288,6 +288,8 @@ MBEDTLS_DEPRECATED void mbedtls_md5( const unsigned char *input, | |||
| #undef MBEDTLS_DEPRECATED | ||||
| #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          Checkup routine | ||||
|  * | ||||
|  | @ -300,6 +302,8 @@ MBEDTLS_DEPRECATED void mbedtls_md5( const unsigned char *input, | |||
|  */ | ||||
| int mbedtls_md5_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
|  | @ -23,6 +23,11 @@ | |||
|  * | ||||
|  *  This file is part of mbed TLS (https://tls.mbed.org)
 | ||||
|  */ | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #if !defined(MBEDTLS_DEPRECATED_REMOVED) | ||||
| #include "net_sockets.h" | ||||
|  |  | |||
|  | @ -37,6 +37,12 @@ | |||
| #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 | ||||
|  |  | |||
|  | @ -3,6 +3,9 @@ | |||
|  * | ||||
|  * \brief VIA PadLock ACE for HW encryption/decryption supported by some | ||||
|  *        processors | ||||
|  * | ||||
|  * \warning These functions are only for internal use by other library | ||||
|  *          functions; you must not call them directly. | ||||
|  */ | ||||
| /*
 | ||||
|  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved | ||||
|  | @ -25,6 +28,12 @@ | |||
| #ifndef MBEDTLS_PADLOCK_H | ||||
| #define MBEDTLS_PADLOCK_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "aes.h" | ||||
| 
 | ||||
| #define MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED               -0x0030  /**< Input data should be aligned. */ | ||||
|  | @ -57,7 +66,10 @@ extern "C" { | |||
| #endif | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          PadLock detection routine | ||||
|  * \brief          Internal PadLock detection routine | ||||
|  * | ||||
|  * \note           This function is only for internal use by other library | ||||
|  *                 functions; you must not call it directly. | ||||
|  * | ||||
|  * \param feature  The feature to detect | ||||
|  * | ||||
|  | @ -66,7 +78,10 @@ extern "C" { | |||
| int mbedtls_padlock_has_support( int feature ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          PadLock AES-ECB block en(de)cryption | ||||
|  * \brief          Internal PadLock AES-ECB block en(de)cryption | ||||
|  * | ||||
|  * \note           This function is only for internal use by other library | ||||
|  *                 functions; you must not call it directly. | ||||
|  * | ||||
|  * \param ctx      AES context | ||||
|  * \param mode     MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT | ||||
|  | @ -76,12 +91,15 @@ int mbedtls_padlock_has_support( int feature ); | |||
|  * \return         0 if success, 1 if operation failed | ||||
|  */ | ||||
| int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx, | ||||
|                        int mode, | ||||
|                        const unsigned char input[16], | ||||
|                        unsigned char output[16] ); | ||||
|                                int mode, | ||||
|                                const unsigned char input[16], | ||||
|                                unsigned char output[16] ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          PadLock AES-CBC buffer en(de)cryption | ||||
|  * \brief          Internal PadLock AES-CBC buffer en(de)cryption | ||||
|  * | ||||
|  * \note           This function is only for internal use by other library | ||||
|  *                 functions; you must not call it directly. | ||||
|  * | ||||
|  * \param ctx      AES context | ||||
|  * \param mode     MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT | ||||
|  | @ -93,11 +111,11 @@ int mbedtls_padlock_xcryptecb( mbedtls_aes_context *ctx, | |||
|  * \return         0 if success, 1 if operation failed | ||||
|  */ | ||||
| int mbedtls_padlock_xcryptcbc( mbedtls_aes_context *ctx, | ||||
|                        int mode, | ||||
|                        size_t length, | ||||
|                        unsigned char iv[16], | ||||
|                        const unsigned char *input, | ||||
|                        unsigned char *output ); | ||||
|                                int mode, | ||||
|                                size_t length, | ||||
|                                unsigned char iv[16], | ||||
|                                const unsigned char *input, | ||||
|                                unsigned char *output ); | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
|  |  | |||
|  | @ -24,6 +24,12 @@ | |||
| #ifndef MBEDTLS_PEM_H | ||||
| #define MBEDTLS_PEM_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include <stddef.h> | ||||
| 
 | ||||
| /**
 | ||||
|  |  | |||
|  | @ -202,12 +202,18 @@ typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx ); | |||
| const mbedtls_pk_info_t *mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           Initialize a mbedtls_pk_context (as NONE) | ||||
|  * \brief           Initialize a #mbedtls_pk_context (as NONE). | ||||
|  * | ||||
|  * \param ctx       The context to initialize. | ||||
|  *                  This must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_pk_init( mbedtls_pk_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           Free a mbedtls_pk_context | ||||
|  * \brief           Free the components of a #mbedtls_pk_context. | ||||
|  * | ||||
|  * \param ctx       The context to clear. It must have been initialized. | ||||
|  *                  If this is \c NULL, this function does nothing. | ||||
|  * | ||||
|  * \note            For contexts that have been set up with | ||||
|  *                  mbedtls_pk_setup_opaque(), this does not free the underlying | ||||
|  | @ -219,11 +225,17 @@ void mbedtls_pk_free( mbedtls_pk_context *ctx ); | |||
| #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) | ||||
| /**
 | ||||
|  * \brief           Initialize a restart context | ||||
|  * | ||||
|  * \param ctx       The context to initialize. | ||||
|  *                  This must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           Free the components of a restart context | ||||
|  * | ||||
|  * \param ctx       The context to clear. It must have been initialized. | ||||
|  *                  If this is \c NULL, this function does nothing. | ||||
|  */ | ||||
| void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx ); | ||||
| #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ | ||||
|  | @ -232,7 +244,8 @@ void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx ); | |||
|  * \brief           Initialize a PK context with the information given | ||||
|  *                  and allocates the type-specific PK subcontext. | ||||
|  * | ||||
|  * \param ctx       Context to initialize. Must be empty (type NONE). | ||||
|  * \param ctx       Context to initialize. It must not have been set | ||||
|  *                  up yet (type #MBEDTLS_PK_NONE). | ||||
|  * \param info      Information to use | ||||
|  * | ||||
|  * \return          0 on success, | ||||
|  | @ -280,7 +293,8 @@ int mbedtls_pk_setup_opaque( mbedtls_pk_context *ctx, const psa_key_handle_t key | |||
| /**
 | ||||
|  * \brief           Initialize an RSA-alt context | ||||
|  * | ||||
|  * \param ctx       Context to initialize. Must be empty (type NONE). | ||||
|  * \param ctx       Context to initialize. It must not have been set | ||||
|  *                  up yet (type #MBEDTLS_PK_NONE). | ||||
|  * \param key       RSA key pointer | ||||
|  * \param decrypt_func  Decryption function | ||||
|  * \param sign_func     Signing function | ||||
|  | @ -300,7 +314,7 @@ int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key, | |||
| /**
 | ||||
|  * \brief           Get the size in bits of the underlying key | ||||
|  * | ||||
|  * \param ctx       Context to use | ||||
|  * \param ctx       The context to query. It must have been initialized. | ||||
|  * | ||||
|  * \return          Key size in bits, or 0 on error | ||||
|  */ | ||||
|  | @ -308,7 +322,8 @@ size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx ); | |||
| 
 | ||||
| /**
 | ||||
|  * \brief           Get the length in bytes of the underlying key | ||||
|  * \param ctx       Context to use | ||||
|  * | ||||
|  * \param ctx       The context to query. It must have been initialized. | ||||
|  * | ||||
|  * \return          Key length in bytes, or 0 on error | ||||
|  */ | ||||
|  | @ -320,18 +335,21 @@ static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx ) | |||
| /**
 | ||||
|  * \brief           Tell if a context can do the operation given by type | ||||
|  * | ||||
|  * \param ctx       Context to test | ||||
|  * \param type      Target type | ||||
|  * \param ctx       The context to query. It must have been initialized. | ||||
|  * \param type      The desired type. | ||||
|  * | ||||
|  * \return          0 if context can't do the operations, | ||||
|  *                  1 otherwise. | ||||
|  * \return          1 if the context can do operations on the given type. | ||||
|  * \return          0 if the context cannot do the operations on the given | ||||
|  *                  type. This is always the case for a context that has | ||||
|  *                  been initialized but not set up, or that has been | ||||
|  *                  cleared with mbedtls_pk_free(). | ||||
|  */ | ||||
| int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           Verify signature (including padding if relevant). | ||||
|  * | ||||
|  * \param ctx       PK context to use | ||||
|  * \param ctx       The PK context to use. It must have been set up. | ||||
|  * \param md_alg    Hash algorithm used (see notes) | ||||
|  * \param hash      Hash of the message to sign | ||||
|  * \param hash_len  Hash length or 0 (see notes) | ||||
|  | @ -364,7 +382,7 @@ int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, | |||
|  *                  \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC | ||||
|  *                  operations. For RSA, same as \c mbedtls_pk_verify(). | ||||
|  * | ||||
|  * \param ctx       PK context to use | ||||
|  * \param ctx       The PK context to use. It must have been set up. | ||||
|  * \param md_alg    Hash algorithm used (see notes) | ||||
|  * \param hash      Hash of the message to sign | ||||
|  * \param hash_len  Hash length or 0 (see notes) | ||||
|  | @ -388,7 +406,7 @@ int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx, | |||
|  * | ||||
|  * \param type      Signature type (inc. possible padding type) to verify | ||||
|  * \param options   Pointer to type-specific options, or NULL | ||||
|  * \param ctx       PK context to use | ||||
|  * \param ctx       The PK context to use. It must have been set up. | ||||
|  * \param md_alg    Hash algorithm used (see notes) | ||||
|  * \param hash      Hash of the message to sign | ||||
|  * \param hash_len  Hash length or 0 (see notes) | ||||
|  | @ -419,7 +437,8 @@ int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options, | |||
| /**
 | ||||
|  * \brief           Make signature, including padding if relevant. | ||||
|  * | ||||
|  * \param ctx       PK context to use - must hold a private key | ||||
|  * \param ctx       The PK context to use. It must have been set up | ||||
|  *                  with a private key. | ||||
|  * \param md_alg    Hash algorithm used (see notes) | ||||
|  * \param hash      Hash of the message to sign | ||||
|  * \param hash_len  Hash length or 0 (see notes) | ||||
|  | @ -453,7 +472,8 @@ int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, | |||
|  *                  \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC | ||||
|  *                  operations. For RSA, same as \c mbedtls_pk_sign(). | ||||
|  * | ||||
|  * \param ctx       PK context to use - must hold a private key | ||||
|  * \param ctx       The PK context to use. It must have been set up | ||||
|  *                  with a private key. | ||||
|  * \param md_alg    Hash algorithm used (see notes) | ||||
|  * \param hash      Hash of the message to sign | ||||
|  * \param hash_len  Hash length or 0 (see notes) | ||||
|  | @ -477,7 +497,8 @@ int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx, | |||
| /**
 | ||||
|  * \brief           Decrypt message (including padding if relevant). | ||||
|  * | ||||
|  * \param ctx       PK context to use - must hold a private key | ||||
|  * \param ctx       The PK context to use. It must have been set up | ||||
|  *                  with a private key. | ||||
|  * \param input     Input to decrypt | ||||
|  * \param ilen      Input size | ||||
|  * \param output    Decrypted output | ||||
|  | @ -498,7 +519,7 @@ int mbedtls_pk_decrypt( mbedtls_pk_context *ctx, | |||
| /**
 | ||||
|  * \brief           Encrypt message (including padding if relevant). | ||||
|  * | ||||
|  * \param ctx       PK context to use | ||||
|  * \param ctx       The PK context to use. It must have been set up. | ||||
|  * \param input     Message to encrypt | ||||
|  * \param ilen      Message size | ||||
|  * \param output    Encrypted output | ||||
|  | @ -533,7 +554,7 @@ int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_conte | |||
| /**
 | ||||
|  * \brief           Export debug information | ||||
|  * | ||||
|  * \param ctx       Context to use | ||||
|  * \param ctx       The PK context to use. It must have been initialized. | ||||
|  * \param items     Place to write debug items | ||||
|  * | ||||
|  * \return          0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA | ||||
|  | @ -543,7 +564,7 @@ int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *item | |||
| /**
 | ||||
|  * \brief           Access the type name | ||||
|  * | ||||
|  * \param ctx       Context to use | ||||
|  * \param ctx       The PK context to use. It must have been initialized. | ||||
|  * | ||||
|  * \return          Type name on success, or "invalid PK" | ||||
|  */ | ||||
|  | @ -552,9 +573,10 @@ const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx ); | |||
| /**
 | ||||
|  * \brief           Get the key type | ||||
|  * | ||||
|  * \param ctx       Context to use | ||||
|  * \param ctx       The PK context to use. It must have been initialized. | ||||
|  * | ||||
|  * \return          Type on success, or MBEDTLS_PK_NONE | ||||
|  * \return          Type on success. | ||||
|  * \return          #MBEDTLS_PK_NONE for a context that has not been set up. | ||||
|  */ | ||||
| mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx ); | ||||
| 
 | ||||
|  | @ -563,12 +585,22 @@ mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx ); | |||
| /**
 | ||||
|  * \brief           Parse a private key in PEM or DER format | ||||
|  * | ||||
|  * \param ctx       key to be initialized | ||||
|  * \param key       input buffer | ||||
|  * \param keylen    size of the buffer | ||||
|  *                  (including the terminating null byte for PEM data) | ||||
|  * \param pwd       password for decryption (optional) | ||||
|  * \param pwdlen    size of the password | ||||
|  * \param ctx       The PK context to fill. It must have been initialized | ||||
|  *                  but not set up. | ||||
|  * \param key       Input buffer to parse. | ||||
|  *                  The buffer must contain the input exactly, with no | ||||
|  *                  extra trailing material. For PEM, the buffer must | ||||
|  *                  contain a null-terminated string. | ||||
|  * \param keylen    Size of \b key in bytes. | ||||
|  *                  For PEM data, this includes the terminating null byte, | ||||
|  *                  so \p keylen must be equal to `strlen(key) + 1`. | ||||
|  * \param pwd       Optional password for decryption. | ||||
|  *                  Pass \c NULL if expecting a non-encrypted key. | ||||
|  *                  Pass a string of \p pwdlen bytes if expecting an encrypted | ||||
|  *                  key; a non-encrypted key will also be accepted. | ||||
|  *                  The empty password is not supported. | ||||
|  * \param pwdlen    Size of the password in bytes. | ||||
|  *                  Ignored if \p pwd is \c NULL. | ||||
|  * | ||||
|  * \note            On entry, ctx must be empty, either freshly initialised | ||||
|  *                  with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a | ||||
|  | @ -586,10 +618,15 @@ int mbedtls_pk_parse_key( mbedtls_pk_context *ctx, | |||
| /**
 | ||||
|  * \brief           Parse a public key in PEM or DER format | ||||
|  * | ||||
|  * \param ctx       key to be initialized | ||||
|  * \param key       input buffer | ||||
|  * \param keylen    size of the buffer | ||||
|  *                  (including the terminating null byte for PEM data) | ||||
|  * \param ctx       The PK context to fill. It must have been initialized | ||||
|  *                  but not set up. | ||||
|  * \param key       Input buffer to parse. | ||||
|  *                  The buffer must contain the input exactly, with no | ||||
|  *                  extra trailing material. For PEM, the buffer must | ||||
|  *                  contain a null-terminated string. | ||||
|  * \param keylen    Size of \b key in bytes. | ||||
|  *                  For PEM data, this includes the terminating null byte, | ||||
|  *                  so \p keylen must be equal to `strlen(key) + 1`. | ||||
|  * | ||||
|  * \note            On entry, ctx must be empty, either freshly initialised | ||||
|  *                  with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a | ||||
|  | @ -607,9 +644,14 @@ int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx, | |||
| /**
 | ||||
|  * \brief           Load and parse a private key | ||||
|  * | ||||
|  * \param ctx       key to be initialized | ||||
|  * \param ctx       The PK context to fill. It must have been initialized | ||||
|  *                  but not set up. | ||||
|  * \param path      filename to read the private key from | ||||
|  * \param password  password to decrypt the file (can be NULL) | ||||
|  * \param password  Optional password to decrypt the file. | ||||
|  *                  Pass \c NULL if expecting a non-encrypted key. | ||||
|  *                  Pass a null-terminated string if expecting an encrypted | ||||
|  *                  key; a non-encrypted key will also be accepted. | ||||
|  *                  The empty password is not supported. | ||||
|  * | ||||
|  * \note            On entry, ctx must be empty, either freshly initialised | ||||
|  *                  with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a | ||||
|  | @ -626,7 +668,8 @@ int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx, | |||
| /**
 | ||||
|  * \brief           Load and parse a public key | ||||
|  * | ||||
|  * \param ctx       key to be initialized | ||||
|  * \param ctx       The PK context to fill. It must have been initialized | ||||
|  *                  but not set up. | ||||
|  * \param path      filename to read the public key from | ||||
|  * | ||||
|  * \note            On entry, ctx must be empty, either freshly initialised | ||||
|  | @ -649,7 +692,7 @@ int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path ) | |||
|  *                        return value to determine where you should start | ||||
|  *                        using the buffer | ||||
|  * | ||||
|  * \param ctx       private to write away | ||||
|  * \param ctx       PK context which must contain a valid private key. | ||||
|  * \param buf       buffer to write to | ||||
|  * \param size      size of the buffer | ||||
|  * | ||||
|  | @ -664,7 +707,7 @@ int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_ | |||
|  *                        return value to determine where you should start | ||||
|  *                        using the buffer | ||||
|  * | ||||
|  * \param ctx       public key to write away | ||||
|  * \param ctx       PK context which must contain a valid public or private key. | ||||
|  * \param buf       buffer to write to | ||||
|  * \param size      size of the buffer | ||||
|  * | ||||
|  | @ -677,9 +720,10 @@ int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, si | |||
| /**
 | ||||
|  * \brief           Write a public key to a PEM string | ||||
|  * | ||||
|  * \param ctx       public key to write away | ||||
|  * \param buf       buffer to write to | ||||
|  * \param size      size of the buffer | ||||
|  * \param ctx       PK context which must contain a valid public or private key. | ||||
|  * \param buf       Buffer to write to. The output includes a | ||||
|  *                  terminating null byte. | ||||
|  * \param size      Size of the buffer in bytes. | ||||
|  * | ||||
|  * \return          0 if successful, or a specific error code | ||||
|  */ | ||||
|  | @ -688,9 +732,10 @@ int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, si | |||
| /**
 | ||||
|  * \brief           Write a private key to a PKCS#1 or SEC1 PEM string | ||||
|  * | ||||
|  * \param ctx       private to write away | ||||
|  * \param buf       buffer to write to | ||||
|  * \param size      size of the buffer | ||||
|  * \param ctx       PK context which must contain a valid private key. | ||||
|  * \param buf       Buffer to write to. The output includes a | ||||
|  *                  terminating null byte. | ||||
|  * \param size      Size of the buffer in bytes. | ||||
|  * | ||||
|  * \return          0 if successful, or a specific error code | ||||
|  */ | ||||
|  | @ -709,7 +754,8 @@ int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_ | |||
|  * | ||||
|  * \param p         the position in the ASN.1 data | ||||
|  * \param end       end of the buffer | ||||
|  * \param pk        the key to fill | ||||
|  * \param pk        The PK context to fill. It must have been initialized | ||||
|  *                  but not set up. | ||||
|  * | ||||
|  * \return          0 if successful, or a specific PK error code | ||||
|  */ | ||||
|  | @ -724,7 +770,7 @@ int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end, | |||
|  * | ||||
|  * \param p         reference to current position pointer | ||||
|  * \param start     start of the buffer (for bounds-checking) | ||||
|  * \param key       public key to write away | ||||
|  * \param key       PK context which must contain a valid public or private key. | ||||
|  * | ||||
|  * \return          the length written or a negative error code | ||||
|  */ | ||||
|  |  | |||
|  | @ -24,6 +24,12 @@ | |||
| #ifndef MBEDTLS_PKCS12_H | ||||
| #define MBEDTLS_PKCS12_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "md.h" | ||||
| #include "cipher.h" | ||||
| #include "asn1.h" | ||||
|  |  | |||
|  | @ -26,6 +26,12 @@ | |||
| #ifndef MBEDTLS_PKCS5_H | ||||
| #define MBEDTLS_PKCS5_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "asn1.h" | ||||
| #include "md.h" | ||||
| 
 | ||||
|  | @ -85,6 +91,8 @@ int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx, const unsigned char *p | |||
|                        unsigned int iteration_count, | ||||
|                        uint32_t key_length, unsigned char *output ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          Checkup routine | ||||
|  * | ||||
|  | @ -92,6 +100,8 @@ int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx, const unsigned char *p | |||
|  */ | ||||
| int mbedtls_pkcs5_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
|  | @ -58,17 +58,33 @@ extern "C" { | |||
|  * \{ | ||||
|  */ | ||||
| 
 | ||||
| /* The older Microsoft Windows common runtime provides non-conforming
 | ||||
|  * implementations of some standard library functions, including snprintf | ||||
|  * and vsnprintf. This affects MSVC and MinGW builds. | ||||
|  */ | ||||
| #if defined(__MINGW32__) || (defined(_MSC_VER) && _MSC_VER <= 1900) | ||||
| #define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF | ||||
| #define MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF | ||||
| #endif | ||||
| 
 | ||||
| #if !defined(MBEDTLS_PLATFORM_NO_STD_FUNCTIONS) | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <time.h> | ||||
| #if !defined(MBEDTLS_PLATFORM_STD_SNPRINTF) | ||||
| #if defined(_WIN32) | ||||
| #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF) | ||||
| #define MBEDTLS_PLATFORM_STD_SNPRINTF   mbedtls_platform_win32_snprintf /**< The default \c snprintf function to use.  */ | ||||
| #else | ||||
| #define MBEDTLS_PLATFORM_STD_SNPRINTF   snprintf /**< The default \c snprintf function to use.  */ | ||||
| #endif | ||||
| #endif | ||||
| #if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF) | ||||
| #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF) | ||||
| #define MBEDTLS_PLATFORM_STD_VSNPRINTF   mbedtls_platform_win32_vsnprintf /**< The default \c vsnprintf function to use.  */ | ||||
| #else | ||||
| #define MBEDTLS_PLATFORM_STD_VSNPRINTF   vsnprintf /**< The default \c vsnprintf function to use.  */ | ||||
| #endif | ||||
| #endif | ||||
| #if !defined(MBEDTLS_PLATFORM_STD_PRINTF) | ||||
| #define MBEDTLS_PLATFORM_STD_PRINTF   printf /**< The default \c printf function to use. */ | ||||
| #endif | ||||
|  | @ -204,7 +220,7 @@ int mbedtls_platform_set_printf( int (*printf_func)( const char *, ... ) ); | |||
|  * - however it is acceptable to return -1 instead of the required length when | ||||
|  *   the destination buffer is too short. | ||||
|  */ | ||||
| #if defined(_WIN32) | ||||
| #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF) | ||||
| /* For Windows (inc. MSYS2), we provide our own fixed implementation */ | ||||
| int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... ); | ||||
| #endif | ||||
|  | @ -230,6 +246,41 @@ int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, | |||
| #endif /* MBEDTLS_PLATFORM_SNPRINTF_MACRO */ | ||||
| #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */ | ||||
| 
 | ||||
| /*
 | ||||
|  * The function pointers for vsnprintf | ||||
|  * | ||||
|  * The vsnprintf implementation should conform to C99: | ||||
|  * - it *must* always correctly zero-terminate the buffer | ||||
|  *   (except when n == 0, then it must leave the buffer untouched) | ||||
|  * - however it is acceptable to return -1 instead of the required length when | ||||
|  *   the destination buffer is too short. | ||||
|  */ | ||||
| #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF) | ||||
| /* For Older Windows (inc. MSYS2), we provide our own fixed implementation */ | ||||
| int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg ); | ||||
| #endif | ||||
| 
 | ||||
| #if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT) | ||||
| #include <stdarg.h> | ||||
| extern int (*mbedtls_vsnprintf)( char * s, size_t n, const char * format, va_list arg ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief   Set your own snprintf function pointer | ||||
|  * | ||||
|  * \param   vsnprintf_func   The \c vsnprintf function implementation | ||||
|  * | ||||
|  * \return  \c 0 | ||||
|  */ | ||||
| int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n, | ||||
|                                                  const char * format, va_list arg ) ); | ||||
| #else /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */ | ||||
| #if defined(MBEDTLS_PLATFORM_VSNPRINTF_MACRO) | ||||
| #define mbedtls_vsnprintf   MBEDTLS_PLATFORM_VSNPRINTF_MACRO | ||||
| #else | ||||
| #define mbedtls_vsnprintf   vsnprintf | ||||
| #endif /* MBEDTLS_PLATFORM_VSNPRINTF_MACRO */ | ||||
| #endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */ | ||||
| 
 | ||||
| /*
 | ||||
|  * The function pointers for exit | ||||
|  */ | ||||
|  |  | |||
|  | @ -26,14 +26,14 @@ | |||
| #define MBEDTLS_PLATFORM_UTIL_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "mbedtls/config.h" | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include <stddef.h> | ||||
| #if defined(MBEDTLS_HAVE_TIME_DATE) | ||||
| #include "mbedtls/platform_time.h" | ||||
| #include "platform_time.h" | ||||
| #include <time.h> | ||||
| #endif /* MBEDTLS_HAVE_TIME_DATE */ | ||||
| 
 | ||||
|  | @ -41,6 +41,88 @@ | |||
| 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 | ||||
|  * | ||||
|  |  | |||
|  | @ -34,7 +34,7 @@ | |||
| #define MBEDTLS_POLY1305_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "mbedtls/config.h" | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
|  | @ -84,14 +84,18 @@ mbedtls_poly1305_context; | |||
|  *                  \c mbedtls_poly1305_finish(), then finally | ||||
|  *                  \c mbedtls_poly1305_free(). | ||||
|  * | ||||
|  * \param ctx       The Poly1305 context to initialize. | ||||
|  * \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. | ||||
|  * \brief           This function releases and clears the specified | ||||
|  *                  Poly1305 context. | ||||
|  * | ||||
|  * \param ctx       The Poly1305 context to clear. | ||||
|  * \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 ); | ||||
| 
 | ||||
|  | @ -102,11 +106,11 @@ void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx ); | |||
|  *                  invocation of Poly1305. | ||||
|  * | ||||
|  * \param ctx       The Poly1305 context to which the key should be bound. | ||||
|  * \param key       The buffer containing the 256-bit key. | ||||
|  *                  This must be initialized. | ||||
|  * \param key       The buffer containing the \c 32 Byte (\c 256 Bit) key. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA | ||||
|  *                  if ctx or key are NULL. | ||||
|  * \return          A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx, | ||||
|                              const unsigned char key[32] ); | ||||
|  | @ -120,13 +124,14 @@ int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx, | |||
|  *                  It can be called repeatedly to process a stream of data. | ||||
|  * | ||||
|  * \param ctx       The Poly1305 context to use for the Poly1305 operation. | ||||
|  * \param ilen      The length of the input data (in bytes). Any value is accepted. | ||||
|  *                  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 NULL if ilen == 0. | ||||
|  *                  This pointer can be \c NULL if `ilen == 0`. | ||||
|  * | ||||
|  * \return          \c 0 on success. | ||||
|  * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA | ||||
|  *                  if ctx or input are NULL. | ||||
|  * \return          A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx, | ||||
|                              const unsigned char *input, | ||||
|  | @ -137,12 +142,12 @@ int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx, | |||
|  *                  Authentication Code (MAC). | ||||
|  * | ||||
|  * \param ctx       The Poly1305 context to use for the Poly1305 operation. | ||||
|  * \param mac       The buffer to where the MAC is written. Must be big enough | ||||
|  *                  to hold the 16-byte MAC. | ||||
|  *                  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          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA | ||||
|  *                  if ctx or mac are NULL. | ||||
|  * \return          A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx, | ||||
|                              unsigned char mac[16] ); | ||||
|  | @ -154,16 +159,16 @@ int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx, | |||
|  * \warning         The key must be unique and unpredictable for each | ||||
|  *                  invocation of Poly1305. | ||||
|  * | ||||
|  * \param key       The buffer containing the 256-bit key. | ||||
|  * \param ilen      The length of the input data (in bytes). Any value is accepted. | ||||
|  * \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 NULL if ilen == 0. | ||||
|  * \param mac       The buffer to where the MAC is written. Must be big enough | ||||
|  *                  to hold the 16-byte MAC. | ||||
|  *                  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          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA | ||||
|  *                  if key, input, or mac are NULL. | ||||
|  * \return          A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_poly1305_mac( const unsigned char key[32], | ||||
|                           const unsigned char *input, | ||||
|  |  | |||
|  | @ -219,6 +219,8 @@ MBEDTLS_DEPRECATED void mbedtls_ripemd160( const unsigned char *input, | |||
| #undef MBEDTLS_DEPRECATED | ||||
| #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          Checkup routine | ||||
|  * | ||||
|  | @ -226,6 +228,8 @@ MBEDTLS_DEPRECATED void mbedtls_ripemd160( const unsigned char *input, | |||
|  */ | ||||
| int mbedtls_ripemd160_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
|  | @ -150,23 +150,24 @@ mbedtls_rsa_context; | |||
|  * \note           The choice of padding mode is strictly enforced for private key | ||||
|  *                 operations, since there might be security concerns in | ||||
|  *                 mixing padding modes. For public key operations it is | ||||
|  *                 a default value, which can be overriden by calling specific | ||||
|  *                 a default value, which can be overridden by calling specific | ||||
|  *                 \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions. | ||||
|  * | ||||
|  * \note           The hash selected in \p hash_id is always used for OEAP | ||||
|  *                 encryption. For PSS signatures, it is always used for | ||||
|  *                 making signatures, but can be overriden for verifying them. | ||||
|  *                 If set to #MBEDTLS_MD_NONE, it is always overriden. | ||||
|  *                 making signatures, but can be overridden for verifying them. | ||||
|  *                 If set to #MBEDTLS_MD_NONE, it is always overridden. | ||||
|  * | ||||
|  * \param ctx      The RSA context to initialize. | ||||
|  * \param padding  Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or | ||||
|  *                 #MBEDTLS_RSA_PKCS_V21. | ||||
|  * \param hash_id  The hash identifier of #mbedtls_md_type_t type, if | ||||
|  *                 \p padding is #MBEDTLS_RSA_PKCS_V21. | ||||
|  * \param ctx      The RSA context to initialize. This must not be \c NULL. | ||||
|  * \param padding  The padding mode to use. This must be either | ||||
|  *                 #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21. | ||||
|  * \param hash_id  The hash identifier of ::mbedtls_md_type_t type, if | ||||
|  *                 \p padding is #MBEDTLS_RSA_PKCS_V21. It is unused | ||||
|  *                 otherwise. | ||||
|  */ | ||||
| void mbedtls_rsa_init( mbedtls_rsa_context *ctx, | ||||
|                        int padding, | ||||
|                        int hash_id); | ||||
|                        int hash_id ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          This function imports a set of core parameters into an | ||||
|  | @ -188,11 +189,11 @@ void mbedtls_rsa_init( mbedtls_rsa_context *ctx, | |||
|  *                 for the lifetime of the RSA context being set up. | ||||
|  * | ||||
|  * \param ctx      The initialized RSA context to store the parameters in. | ||||
|  * \param N        The RSA modulus, or NULL. | ||||
|  * \param P        The first prime factor of \p N, or NULL. | ||||
|  * \param Q        The second prime factor of \p N, or NULL. | ||||
|  * \param D        The private exponent, or NULL. | ||||
|  * \param E        The public exponent, or NULL. | ||||
|  * \param N        The RSA modulus. This may be \c NULL. | ||||
|  * \param P        The first prime factor of \p N. This may be \c NULL. | ||||
|  * \param Q        The second prime factor of \p N. This may be \c NULL. | ||||
|  * \param D        The private exponent. This may be \c NULL. | ||||
|  * \param E        The public exponent. This may be \c NULL. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A non-zero error code on failure. | ||||
|  | @ -222,16 +223,16 @@ int mbedtls_rsa_import( mbedtls_rsa_context *ctx, | |||
|  *                 for the lifetime of the RSA context being set up. | ||||
|  * | ||||
|  * \param ctx      The initialized RSA context to store the parameters in. | ||||
|  * \param N        The RSA modulus, or NULL. | ||||
|  * \param N_len    The Byte length of \p N, ignored if \p N == NULL. | ||||
|  * \param P        The first prime factor of \p N, or NULL. | ||||
|  * \param P_len    The Byte length of \p P, ignored if \p P == NULL. | ||||
|  * \param Q        The second prime factor of \p N, or NULL. | ||||
|  * \param Q_len    The Byte length of \p Q, ignored if \p Q == NULL. | ||||
|  * \param D        The private exponent, or NULL. | ||||
|  * \param D_len    The Byte length of \p D, ignored if \p D == NULL. | ||||
|  * \param E        The public exponent, or NULL. | ||||
|  * \param E_len    The Byte length of \p E, ignored if \p E == NULL. | ||||
|  * \param N        The RSA modulus. This may be \c NULL. | ||||
|  * \param N_len    The Byte length of \p N; it is ignored if \p N == NULL. | ||||
|  * \param P        The first prime factor of \p N. This may be \c NULL. | ||||
|  * \param P_len    The Byte length of \p P; it ns ignored if \p P == NULL. | ||||
|  * \param Q        The second prime factor of \p N. This may be \c NULL. | ||||
|  * \param Q_len    The Byte length of \p Q; it is ignored if \p Q == NULL. | ||||
|  * \param D        The private exponent. This may be \c NULL. | ||||
|  * \param D_len    The Byte length of \p D; it is ignored if \p D == NULL. | ||||
|  * \param E        The public exponent. This may be \c NULL. | ||||
|  * \param E_len    The Byte length of \p E; it is ignored if \p E == NULL. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A non-zero error code on failure. | ||||
|  | @ -299,11 +300,16 @@ int mbedtls_rsa_complete( mbedtls_rsa_context *ctx ); | |||
|  *                 the RSA context stays intact and remains usable. | ||||
|  * | ||||
|  * \param ctx      The initialized RSA context. | ||||
|  * \param N        The MPI to hold the RSA modulus, or NULL. | ||||
|  * \param P        The MPI to hold the first prime factor of \p N, or NULL. | ||||
|  * \param Q        The MPI to hold the second prime factor of \p N, or NULL. | ||||
|  * \param D        The MPI to hold the private exponent, or NULL. | ||||
|  * \param E        The MPI to hold the public exponent, or NULL. | ||||
|  * \param N        The MPI to hold the RSA modulus. | ||||
|  *                 This may be \c NULL if this field need not be exported. | ||||
|  * \param P        The MPI to hold the first prime factor of \p N. | ||||
|  *                 This may be \c NULL if this field need not be exported. | ||||
|  * \param Q        The MPI to hold the second prime factor of \p N. | ||||
|  *                 This may be \c NULL if this field need not be exported. | ||||
|  * \param D        The MPI to hold the private exponent. | ||||
|  *                 This may be \c NULL if this field need not be exported. | ||||
|  * \param E        The MPI to hold the public exponent. | ||||
|  *                 This may be \c NULL if this field need not be exported. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED if exporting the | ||||
|  | @ -341,17 +347,20 @@ int mbedtls_rsa_export( const mbedtls_rsa_context *ctx, | |||
|  *                 buffer pointers are NULL. | ||||
|  * | ||||
|  * \param ctx      The initialized RSA context. | ||||
|  * \param N        The Byte array to store the RSA modulus, or NULL. | ||||
|  * \param N        The Byte array to store the RSA modulus, | ||||
|  *                 or \c NULL if this field need not be exported. | ||||
|  * \param N_len    The size of the buffer for the modulus. | ||||
|  * \param P        The Byte array to hold the first prime factor of \p N, or | ||||
|  *                 NULL. | ||||
|  * \param P        The Byte array to hold the first prime factor of \p N, | ||||
|  *                 or \c NULL if this field need not be exported. | ||||
|  * \param P_len    The size of the buffer for the first prime factor. | ||||
|  * \param Q        The Byte array to hold the second prime factor of \p N, or | ||||
|  *                 NULL. | ||||
|  * \param Q        The Byte array to hold the second prime factor of \p N, | ||||
|  *                 or \c NULL if this field need not be exported. | ||||
|  * \param Q_len    The size of the buffer for the second prime factor. | ||||
|  * \param D        The Byte array to hold the private exponent, or NULL. | ||||
|  * \param D        The Byte array to hold the private exponent, | ||||
|  *                 or \c NULL if this field need not be exported. | ||||
|  * \param D_len    The size of the buffer for the private exponent. | ||||
|  * \param E        The Byte array to hold the public exponent, or NULL. | ||||
|  * \param E        The Byte array to hold the public exponent, | ||||
|  *                 or \c NULL if this field need not be exported. | ||||
|  * \param E_len    The size of the buffer for the public exponent. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  | @ -375,9 +384,12 @@ int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx, | |||
|  *                 mbedtls_rsa_deduce_opt(). | ||||
|  * | ||||
|  * \param ctx      The initialized RSA context. | ||||
|  * \param DP       The MPI to hold D modulo P-1, or NULL. | ||||
|  * \param DQ       The MPI to hold D modulo Q-1, or NULL. | ||||
|  * \param QP       The MPI to hold modular inverse of Q modulo P, or NULL. | ||||
|  * \param DP       The MPI to hold \c D modulo `P-1`, | ||||
|  *                 or \c NULL if it need not be exported. | ||||
|  * \param DQ       The MPI to hold \c D modulo `Q-1`, | ||||
|  *                 or \c NULL if it need not be exported. | ||||
|  * \param QP       The MPI to hold modular inverse of \c Q modulo \c P, | ||||
|  *                 or \c NULL if it need not be exported. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A non-zero error code on failure. | ||||
|  | @ -390,13 +402,13 @@ int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx, | |||
|  * \brief          This function sets padding for an already initialized RSA | ||||
|  *                 context. See mbedtls_rsa_init() for details. | ||||
|  * | ||||
|  * \param ctx      The RSA context to be set. | ||||
|  * \param padding  Selects padding mode: #MBEDTLS_RSA_PKCS_V15 or | ||||
|  *                 #MBEDTLS_RSA_PKCS_V21. | ||||
|  * \param ctx      The initialized RSA context to be configured. | ||||
|  * \param padding  The padding mode to use. This must be either | ||||
|  *                 #MBEDTLS_RSA_PKCS_V15 or #MBEDTLS_RSA_PKCS_V21. | ||||
|  * \param hash_id  The #MBEDTLS_RSA_PKCS_V21 hash identifier. | ||||
|  */ | ||||
| void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, | ||||
|                               int hash_id); | ||||
|                               int hash_id ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          This function retrieves the length of RSA modulus in Bytes. | ||||
|  | @ -414,11 +426,14 @@ size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx ); | |||
|  * \note           mbedtls_rsa_init() must be called before this function, | ||||
|  *                 to set up the RSA context. | ||||
|  * | ||||
|  * \param ctx      The RSA context used to hold the key. | ||||
|  * \param f_rng    The RNG function. | ||||
|  * \param p_rng    The RNG context. | ||||
|  * \param ctx      The initialized RSA context used to hold the key. | ||||
|  * \param f_rng    The RNG function to be used for key generation. | ||||
|  *                 This must not be \c NULL. | ||||
|  * \param p_rng    The RNG context to be passed to \p f_rng. | ||||
|  *                 This may be \c NULL if \p f_rng doesn't need a context. | ||||
|  * \param nbits    The size of the public key in bits. | ||||
|  * \param exponent The public exponent. For example, 65537. | ||||
|  * \param exponent The public exponent to use. For example, \c 65537. | ||||
|  *                 This must be odd and greater than \c 1. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -436,7 +451,7 @@ int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx, | |||
|  *                 enough information is present to perform an RSA public key | ||||
|  *                 operation using mbedtls_rsa_public(). | ||||
|  * | ||||
|  * \param ctx      The RSA context to check. | ||||
|  * \param ctx      The initialized RSA context to check. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -475,7 +490,7 @@ int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx ); | |||
|  *             parameters, which goes beyond what is effectively checkable | ||||
|  *             by the library.</li></ul> | ||||
|  * | ||||
|  * \param ctx  The RSA context to check. | ||||
|  * \param ctx  The initialized RSA context to check. | ||||
|  * | ||||
|  * \return     \c 0 on success. | ||||
|  * \return     An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -487,8 +502,8 @@ int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx ); | |||
|  * | ||||
|  *                 It checks each of the contexts, and makes sure they match. | ||||
|  * | ||||
|  * \param pub      The RSA context holding the public key. | ||||
|  * \param prv      The RSA context holding the private key. | ||||
|  * \param pub      The initialized RSA context holding the public key. | ||||
|  * \param prv      The initialized RSA context holding the private key. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -499,18 +514,19 @@ int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, | |||
| /**
 | ||||
|  * \brief          This function performs an RSA public key operation. | ||||
|  * | ||||
|  * \param ctx      The initialized RSA context to use. | ||||
|  * \param input    The input buffer. This must be a readable buffer | ||||
|  *                 of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                 for an 2048-bit RSA modulus. | ||||
|  * \param output   The output buffer. This must be a writable buffer | ||||
|  *                 of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                 for an 2048-bit RSA modulus. | ||||
|  * | ||||
|  * \note           This function does not handle message padding. | ||||
|  * | ||||
|  * \note           Make sure to set \p input[0] = 0 or ensure that | ||||
|  *                 input is smaller than \p N. | ||||
|  * | ||||
|  * \note           The input and output buffers must be large | ||||
|  *                 enough. For example, 128 Bytes if RSA-1024 is used. | ||||
|  * | ||||
|  * \param ctx      The RSA context. | ||||
|  * \param input    The input buffer. | ||||
|  * \param output   The output buffer. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  */ | ||||
|  | @ -521,9 +537,6 @@ int mbedtls_rsa_public( mbedtls_rsa_context *ctx, | |||
| /**
 | ||||
|  * \brief          This function performs an RSA private key operation. | ||||
|  * | ||||
|  * \note           The input and output buffers must be large | ||||
|  *                 enough. For example, 128 Bytes if RSA-1024 is used. | ||||
|  * | ||||
|  * \note           Blinding is used if and only if a PRNG is provided. | ||||
|  * | ||||
|  * \note           If blinding is used, both the base of exponentation | ||||
|  | @ -535,11 +548,18 @@ int mbedtls_rsa_public( mbedtls_rsa_context *ctx, | |||
|  *                 Future versions of the library may enforce the presence | ||||
|  *                 of a PRNG. | ||||
|  * | ||||
|  * \param ctx      The RSA context. | ||||
|  * \param f_rng    The RNG function. Needed for blinding. | ||||
|  * \param p_rng    The RNG context. | ||||
|  * \param input    The input buffer. | ||||
|  * \param output   The output buffer. | ||||
|  * \param ctx      The initialized RSA context to use. | ||||
|  * \param f_rng    The RNG function, used for blinding. It is discouraged | ||||
|  *                 and deprecated to pass \c NULL here, in which case | ||||
|  *                 blinding will be omitted. | ||||
|  * \param p_rng    The RNG context to pass to \p f_rng. This may be \c NULL | ||||
|  *                 if \p f_rng is \c NULL or if \p f_rng doesn't need a context. | ||||
|  * \param input    The input buffer. This must be a readable buffer | ||||
|  *                 of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                 for an 2048-bit RSA modulus. | ||||
|  * \param output   The output buffer. This must be a writable buffer | ||||
|  *                 of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                 for an 2048-bit RSA modulus. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -558,9 +578,6 @@ int mbedtls_rsa_private( mbedtls_rsa_context *ctx, | |||
|  *                 It is the generic wrapper for performing a PKCS#1 encryption | ||||
|  *                 operation using the \p mode from the context. | ||||
|  * | ||||
|  * \note           The input and output buffers must be as large as the size | ||||
|  *                 of \p ctx->N. For example, 128 Bytes if RSA-1024 is used. | ||||
|  * | ||||
|  * \deprecated     It is deprecated and discouraged to call this function | ||||
|  *                 in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library | ||||
|  *                 are likely to remove the \p mode argument and have it | ||||
|  | @ -570,14 +587,25 @@ int mbedtls_rsa_private( mbedtls_rsa_context *ctx, | |||
|  *                 mode being set to #MBEDTLS_RSA_PRIVATE and might instead | ||||
|  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED. | ||||
|  * | ||||
|  * \param ctx      The RSA context. | ||||
|  * \param f_rng    The RNG function. Needed for padding, PKCS#1 v2.1 | ||||
|  *                 encoding, and #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param p_rng    The RNG context. | ||||
|  * \param mode     #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param ilen     The length of the plaintext. | ||||
|  * \param input    The buffer holding the data to encrypt. | ||||
|  * \param output   The buffer used to hold the ciphertext. | ||||
|  * \param ctx      The initialized RSA context to use. | ||||
|  * \param f_rng    The RNG to use. It is mandatory for PKCS#1 v2.1 padding | ||||
|  *                 encoding, and for PKCS#1 v1.5 padding encoding when used | ||||
|  *                 with \p mode set to #MBEDTLS_RSA_PUBLIC. For PKCS#1 v1.5 | ||||
|  *                 padding encoding and \p mode set to #MBEDTLS_RSA_PRIVATE, | ||||
|  *                 it is used for blinding and should be provided in this | ||||
|  *                 case; see mbedtls_rsa_private() for more. | ||||
|  * \param p_rng    The RNG context to be passed to \p f_rng. May be | ||||
|  *                 \c NULL if \p f_rng is \c NULL or if \p f_rng doesn't | ||||
|  *                 need a context argument. | ||||
|  * \param mode     The mode of operation. This must be either | ||||
|  *                 #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated). | ||||
|  * \param ilen     The length of the plaintext in Bytes. | ||||
|  * \param input    The input data to encrypt. This must be a readable | ||||
|  *                 buffer of size \p ilen Bytes. It may be \c NULL if | ||||
|  *                 `ilen == 0`. | ||||
|  * \param output   The output buffer. This must be a writable buffer | ||||
|  *                 of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                 for an 2048-bit RSA modulus. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -593,9 +621,6 @@ int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx, | |||
|  * \brief          This function performs a PKCS#1 v1.5 encryption operation | ||||
|  *                 (RSAES-PKCS1-v1_5-ENCRYPT). | ||||
|  * | ||||
|  * \note           The output buffer must be as large as the size | ||||
|  *                 of \p ctx->N. For example, 128 Bytes if RSA-1024 is used. | ||||
|  * | ||||
|  * \deprecated     It is deprecated and discouraged to call this function | ||||
|  *                 in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library | ||||
|  *                 are likely to remove the \p mode argument and have it | ||||
|  | @ -605,14 +630,23 @@ int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx, | |||
|  *                 mode being set to #MBEDTLS_RSA_PRIVATE and might instead | ||||
|  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED. | ||||
|  * | ||||
|  * \param ctx      The RSA context. | ||||
|  * \param f_rng    The RNG function. Needed for padding and | ||||
|  *                 #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param p_rng    The RNG context. | ||||
|  * \param mode     #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param ilen     The length of the plaintext. | ||||
|  * \param input    The buffer holding the data to encrypt. | ||||
|  * \param output   The buffer used to hold the ciphertext. | ||||
|  * \param ctx      The initialized RSA context to use. | ||||
|  * \param f_rng    The RNG function to use. It is needed for padding generation | ||||
|  *                 if \p mode is #MBEDTLS_RSA_PUBLIC. If \p mode is | ||||
|  *                 #MBEDTLS_RSA_PRIVATE (discouraged), it is used for | ||||
|  *                 blinding and should be provided; see mbedtls_rsa_private(). | ||||
|  * \param p_rng    The RNG context to be passed to \p f_rng. This may | ||||
|  *                 be \c NULL if \p f_rng is \c NULL or if \p f_rng | ||||
|  *                 doesn't need a context argument. | ||||
|  * \param mode     The mode of operation. This must be either | ||||
|  *                 #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated). | ||||
|  * \param ilen     The length of the plaintext in Bytes. | ||||
|  * \param input    The input data to encrypt. This must be a readable | ||||
|  *                 buffer of size \p ilen Bytes. It may be \c NULL if | ||||
|  *                 `ilen == 0`. | ||||
|  * \param output   The output buffer. This must be a writable buffer | ||||
|  *                 of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                 for an 2048-bit RSA modulus. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -640,16 +674,24 @@ int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx, | |||
|  *                   mode being set to #MBEDTLS_RSA_PRIVATE and might instead | ||||
|  *                   return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED. | ||||
|  * | ||||
|  * \param ctx        The RSA context. | ||||
|  * \param f_rng      The RNG function. Needed for padding and PKCS#1 v2.1 | ||||
|  *                   encoding and #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param p_rng      The RNG context. | ||||
|  * \param mode       #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param ctx        The initnialized RSA context to use. | ||||
|  * \param f_rng      The RNG function to use. This is needed for padding | ||||
|  *                   generation and must be provided. | ||||
|  * \param p_rng      The RNG context to be passed to \p f_rng. This may | ||||
|  *                   be \c NULL if \p f_rng doesn't need a context argument. | ||||
|  * \param mode       The mode of operation. This must be either | ||||
|  *                   #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated). | ||||
|  * \param label      The buffer holding the custom label to use. | ||||
|  * \param label_len  The length of the label. | ||||
|  * \param ilen       The length of the plaintext. | ||||
|  * \param input      The buffer holding the data to encrypt. | ||||
|  * \param output     The buffer used to hold the ciphertext. | ||||
|  *                   This must be a readable buffer of length \p label_len | ||||
|  *                   Bytes. It may be \c NULL if \p label_len is \c 0. | ||||
|  * \param label_len  The length of the label in Bytes. | ||||
|  * \param ilen       The length of the plaintext buffer \p input in Bytes. | ||||
|  * \param input      The input data to encrypt. This must be a readable | ||||
|  *                   buffer of size \p ilen Bytes. It may be \c NULL if | ||||
|  *                   `ilen == 0`. | ||||
|  * \param output     The output buffer. This must be a writable buffer | ||||
|  *                   of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                   for an 2048-bit RSA modulus. | ||||
|  * | ||||
|  * \return           \c 0 on success. | ||||
|  * \return           An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -677,9 +719,6 @@ int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx, | |||
|  *                 hold the decryption of the particular ciphertext provided, | ||||
|  *                 the function returns \c MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE. | ||||
|  * | ||||
|  * \note           The input buffer must be as large as the size | ||||
|  *                 of \p ctx->N. For example, 128 Bytes if RSA-1024 is used. | ||||
|  * | ||||
|  * \deprecated     It is deprecated and discouraged to call this function | ||||
|  *                 in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library | ||||
|  *                 are likely to remove the \p mode argument and have it | ||||
|  | @ -689,14 +728,23 @@ int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx, | |||
|  *                 mode being set to #MBEDTLS_RSA_PUBLIC and might instead | ||||
|  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED. | ||||
|  * | ||||
|  * \param ctx      The RSA context. | ||||
|  * \param f_rng    The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param p_rng    The RNG context. | ||||
|  * \param mode     #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param olen     The length of the plaintext. | ||||
|  * \param input    The buffer holding the encrypted data. | ||||
|  * \param output   The buffer used to hold the plaintext. | ||||
|  * \param output_max_len    The maximum length of the output buffer. | ||||
|  * \param ctx      The initialized RSA context to use. | ||||
|  * \param f_rng    The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE, | ||||
|  *                 this is used for blinding and should be provided; see | ||||
|  *                 mbedtls_rsa_private() for more. If \p mode is | ||||
|  *                 #MBEDTLS_RSA_PUBLIC, it is ignored. | ||||
|  * \param p_rng    The RNG context to be passed to \p f_rng. This may be | ||||
|  *                 \c NULL if \p f_rng is \c NULL or doesn't need a context. | ||||
|  * \param mode     The mode of operation. This must be either | ||||
|  *                 #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated). | ||||
|  * \param olen     The address at which to store the length of | ||||
|  *                 the plaintext. This must not be \c NULL. | ||||
|  * \param input    The ciphertext buffer. This must be a readable buffer | ||||
|  *                 of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                 for an 2048-bit RSA modulus. | ||||
|  * \param output   The buffer used to hold the plaintext. This must | ||||
|  *                 be a writable buffer of length \p output_max_len Bytes. | ||||
|  * \param output_max_len The length in Bytes of the output buffer \p output. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -720,9 +768,6 @@ int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx, | |||
|  *                 hold the decryption of the particular ciphertext provided, | ||||
|  *                 the function returns #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE. | ||||
|  * | ||||
|  * \note           The input buffer must be as large as the size | ||||
|  *                 of \p ctx->N. For example, 128 Bytes if RSA-1024 is used. | ||||
|  * | ||||
|  * \deprecated     It is deprecated and discouraged to call this function | ||||
|  *                 in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library | ||||
|  *                 are likely to remove the \p mode argument and have it | ||||
|  | @ -732,14 +777,23 @@ int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx, | |||
|  *                 mode being set to #MBEDTLS_RSA_PUBLIC and might instead | ||||
|  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED. | ||||
|  * | ||||
|  * \param ctx      The RSA context. | ||||
|  * \param f_rng    The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param p_rng    The RNG context. | ||||
|  * \param mode     #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param olen     The length of the plaintext. | ||||
|  * \param input    The buffer holding the encrypted data. | ||||
|  * \param output   The buffer to hold the plaintext. | ||||
|  * \param output_max_len    The maximum length of the output buffer. | ||||
|  * \param ctx      The initialized RSA context to use. | ||||
|  * \param f_rng    The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE, | ||||
|  *                 this is used for blinding and should be provided; see | ||||
|  *                 mbedtls_rsa_private() for more. If \p mode is | ||||
|  *                 #MBEDTLS_RSA_PUBLIC, it is ignored. | ||||
|  * \param p_rng    The RNG context to be passed to \p f_rng. This may be | ||||
|  *                 \c NULL if \p f_rng is \c NULL or doesn't need a context. | ||||
|  * \param mode     The mode of operation. This must be either | ||||
|  *                 #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated). | ||||
|  * \param olen     The address at which to store the length of | ||||
|  *                 the plaintext. This must not be \c NULL. | ||||
|  * \param input    The ciphertext buffer. This must be a readable buffer | ||||
|  *                 of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                 for an 2048-bit RSA modulus. | ||||
|  * \param output   The buffer used to hold the plaintext. This must | ||||
|  *                 be a writable buffer of length \p output_max_len Bytes. | ||||
|  * \param output_max_len The length in Bytes of the output buffer \p output. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -765,9 +819,6 @@ int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx, | |||
|  *                   ciphertext provided, the function returns | ||||
|  *                   #MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE. | ||||
|  * | ||||
|  * \note             The input buffer must be as large as the size | ||||
|  *                   of \p ctx->N. For example, 128 Bytes if RSA-1024 is used. | ||||
|  * | ||||
|  * \deprecated       It is deprecated and discouraged to call this function | ||||
|  *                   in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library | ||||
|  *                   are likely to remove the \p mode argument and have it | ||||
|  | @ -777,16 +828,27 @@ int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx, | |||
|  *                   mode being set to #MBEDTLS_RSA_PUBLIC and might instead | ||||
|  *                   return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED. | ||||
|  * | ||||
|  * \param ctx        The RSA context. | ||||
|  * \param f_rng      The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param p_rng      The RNG context. | ||||
|  * \param mode       #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param ctx        The initialized RSA context to use. | ||||
|  * \param f_rng      The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE, | ||||
|  *                   this is used for blinding and should be provided; see | ||||
|  *                   mbedtls_rsa_private() for more. If \p mode is | ||||
|  *                   #MBEDTLS_RSA_PUBLIC, it is ignored. | ||||
|  * \param p_rng      The RNG context to be passed to \p f_rng. This may be | ||||
|  *                   \c NULL if \p f_rng is \c NULL or doesn't need a context. | ||||
|  * \param mode       The mode of operation. This must be either | ||||
|  *                   #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated). | ||||
|  * \param label      The buffer holding the custom label to use. | ||||
|  * \param label_len  The length of the label. | ||||
|  * \param olen       The length of the plaintext. | ||||
|  * \param input      The buffer holding the encrypted data. | ||||
|  * \param output     The buffer to hold the plaintext. | ||||
|  * \param output_max_len    The maximum length of the output buffer. | ||||
|  *                   This must be a readable buffer of length \p label_len | ||||
|  *                   Bytes. It may be \c NULL if \p label_len is \c 0. | ||||
|  * \param label_len  The length of the label in Bytes. | ||||
|  * \param olen       The address at which to store the length of | ||||
|  *                   the plaintext. This must not be \c NULL. | ||||
|  * \param input      The ciphertext buffer. This must be a readable buffer | ||||
|  *                   of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                   for an 2048-bit RSA modulus. | ||||
|  * \param output     The buffer used to hold the plaintext. This must | ||||
|  *                   be a writable buffer of length \p output_max_len Bytes. | ||||
|  * \param output_max_len The length in Bytes of the output buffer \p output. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -824,16 +886,28 @@ int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx, | |||
|  *                 mode being set to #MBEDTLS_RSA_PUBLIC and might instead | ||||
|  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED. | ||||
|  * | ||||
|  * \param ctx      The RSA context. | ||||
|  * \param f_rng    The RNG function. Needed for PKCS#1 v2.1 encoding and for | ||||
|  *                 #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param p_rng    The RNG context. | ||||
|  * \param mode     #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param ctx      The initialized RSA context to use. | ||||
|  * \param f_rng    The RNG function to use. If the padding mode is PKCS#1 v2.1, | ||||
|  *                 this must be provided. If the padding mode is PKCS#1 v1.5 and | ||||
|  *                 \p mode is #MBEDTLS_RSA_PRIVATE, it is used for blinding | ||||
|  *                 and should be provided; see mbedtls_rsa_private() for more | ||||
|  *                 more. It is ignored otherwise. | ||||
|  * \param p_rng    The RNG context to be passed to \p f_rng. This may be \c NULL | ||||
|  *                 if \p f_rng is \c NULL or doesn't need a context argument. | ||||
|  * \param mode     The mode of operation. This must be either | ||||
|  *                 #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated). | ||||
|  * \param md_alg   The message-digest algorithm used to hash the original data. | ||||
|  *                 Use #MBEDTLS_MD_NONE for signing raw data. | ||||
|  * \param hashlen  The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE. | ||||
|  * \param hash     The buffer holding the message digest. | ||||
|  * \param sig      The buffer to hold the ciphertext. | ||||
|  * \param hashlen  The length of the message digest. | ||||
|  *                 Ths is only used if \p md_alg is #MBEDTLS_MD_NONE. | ||||
|  * \param hash     The buffer holding the message digest or raw data. | ||||
|  *                 If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable | ||||
|  *                 buffer of length \p hashlen Bytes. If \p md_alg is not | ||||
|  *                 #MBEDTLS_MD_NONE, it must be a readable buffer of length | ||||
|  *                 the size of the hash corresponding to \p md_alg. | ||||
|  * \param sig      The buffer to hold the signature. This must be a writable | ||||
|  *                 buffer of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                 for an 2048-bit RSA modulus. | ||||
|  * | ||||
|  * \return         \c 0 if the signing operation was successful. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -851,9 +925,6 @@ int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx, | |||
|  * \brief          This function performs a PKCS#1 v1.5 signature | ||||
|  *                 operation (RSASSA-PKCS1-v1_5-SIGN). | ||||
|  * | ||||
|  * \note           The \p sig buffer must be as large as the size | ||||
|  *                 of \p ctx->N. For example, 128 Bytes if RSA-1024 is used. | ||||
|  * | ||||
|  * \deprecated     It is deprecated and discouraged to call this function | ||||
|  *                 in #MBEDTLS_RSA_PUBLIC mode. Future versions of the library | ||||
|  *                 are likely to remove the \p mode argument and have it | ||||
|  | @ -863,15 +934,27 @@ int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx, | |||
|  *                 mode being set to #MBEDTLS_RSA_PUBLIC and might instead | ||||
|  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED. | ||||
|  * | ||||
|  * \param ctx      The RSA context. | ||||
|  * \param f_rng    The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param p_rng    The RNG context. | ||||
|  * \param mode     #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param ctx      The initialized RSA context to use. | ||||
|  * \param f_rng    The RNG function. If \p mode is #MBEDTLS_RSA_PRIVATE, | ||||
|  *                 this is used for blinding and should be provided; see | ||||
|  *                 mbedtls_rsa_private() for more. If \p mode is | ||||
|  *                 #MBEDTLS_RSA_PUBLIC, it is ignored. | ||||
|  * \param p_rng    The RNG context to be passed to \p f_rng. This may be \c NULL | ||||
|  *                 if \p f_rng is \c NULL or doesn't need a context argument. | ||||
|  * \param mode     The mode of operation. This must be either | ||||
|  *                 #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated). | ||||
|  * \param md_alg   The message-digest algorithm used to hash the original data. | ||||
|  *                 Use #MBEDTLS_MD_NONE for signing raw data. | ||||
|  * \param hashlen  The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE. | ||||
|  * \param hash     The buffer holding the message digest. | ||||
|  * \param sig      The buffer to hold the ciphertext. | ||||
|  * \param hashlen  The length of the message digest. | ||||
|  *                 Ths is only used if \p md_alg is #MBEDTLS_MD_NONE. | ||||
|  * \param hash     The buffer holding the message digest or raw data. | ||||
|  *                 If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable | ||||
|  *                 buffer of length \p hashlen Bytes. If \p md_alg is not | ||||
|  *                 #MBEDTLS_MD_NONE, it must be a readable buffer of length | ||||
|  *                 the size of the hash corresponding to \p md_alg. | ||||
|  * \param sig      The buffer to hold the signature. This must be a writable | ||||
|  *                 buffer of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                 for an 2048-bit RSA modulus. | ||||
|  * | ||||
|  * \return         \c 0 if the signing operation was successful. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -889,9 +972,6 @@ int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx, | |||
|  * \brief          This function performs a PKCS#1 v2.1 PSS signature | ||||
|  *                 operation (RSASSA-PSS-SIGN). | ||||
|  * | ||||
|  * \note           The \p sig buffer must be as large as the size | ||||
|  *                 of \p ctx->N. For example, 128 Bytes if RSA-1024 is used. | ||||
|  * | ||||
|  * \note           The \p hash_id in the RSA context is the one used for the | ||||
|  *                 encoding. \p md_alg in the function call is the type of hash | ||||
|  *                 that is encoded. According to <em>RFC-3447: Public-Key | ||||
|  | @ -918,16 +998,24 @@ int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx, | |||
|  *                 mode being set to #MBEDTLS_RSA_PUBLIC and might instead | ||||
|  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED. | ||||
|  * | ||||
|  * \param ctx      The RSA context. | ||||
|  * \param f_rng    The RNG function. Needed for PKCS#1 v2.1 encoding and for | ||||
|  *                 #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param p_rng    The RNG context. | ||||
|  * \param mode     #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param ctx      The initialized RSA context to use. | ||||
|  * \param f_rng    The RNG function. It must not be \c NULL. | ||||
|  * \param p_rng    The RNG context to be passed to \p f_rng. This may be \c NULL | ||||
|  *                 if \p f_rng doesn't need a context argument. | ||||
|  * \param mode     The mode of operation. This must be either | ||||
|  *                 #MBEDTLS_RSA_PRIVATE or #MBEDTLS_RSA_PUBLIC (deprecated). | ||||
|  * \param md_alg   The message-digest algorithm used to hash the original data. | ||||
|  *                 Use #MBEDTLS_MD_NONE for signing raw data. | ||||
|  * \param hashlen  The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE. | ||||
|  * \param hash     The buffer holding the message digest. | ||||
|  * \param sig      The buffer to hold the ciphertext. | ||||
|  * \param hashlen  The length of the message digest. | ||||
|  *                 Ths is only used if \p md_alg is #MBEDTLS_MD_NONE. | ||||
|  * \param hash     The buffer holding the message digest or raw data. | ||||
|  *                 If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable | ||||
|  *                 buffer of length \p hashlen Bytes. If \p md_alg is not | ||||
|  *                 #MBEDTLS_MD_NONE, it must be a readable buffer of length | ||||
|  *                 the size of the hash corresponding to \p md_alg. | ||||
|  * \param sig      The buffer to hold the signature. This must be a writable | ||||
|  *                 buffer of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                 for an 2048-bit RSA modulus. | ||||
|  * | ||||
|  * \return         \c 0 if the signing operation was successful. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -948,9 +1036,6 @@ int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx, | |||
|  *                 This is the generic wrapper for performing a PKCS#1 | ||||
|  *                 verification using the mode from the context. | ||||
|  * | ||||
|  * \note           The \p sig buffer must be as large as the size | ||||
|  *                 of \p ctx->N. For example, 128 Bytes if RSA-1024 is used. | ||||
|  * | ||||
|  * \note           For PKCS#1 v2.1 encoding, see comments on | ||||
|  *                 mbedtls_rsa_rsassa_pss_verify() about \p md_alg and | ||||
|  *                 \p hash_id. | ||||
|  | @ -964,15 +1049,26 @@ int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx, | |||
|  *                 mode being set to #MBEDTLS_RSA_PRIVATE and might instead | ||||
|  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED. | ||||
|  * | ||||
|  * \param ctx      The RSA public key context. | ||||
|  * \param f_rng    The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param p_rng    The RNG context. | ||||
|  * \param mode     #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param ctx      The initialized RSA public key context to use. | ||||
|  * \param f_rng    The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE, | ||||
|  *                 this is used for blinding and should be provided; see | ||||
|  *                 mbedtls_rsa_private() for more. Otherwise, it is ignored. | ||||
|  * \param p_rng    The RNG context to be passed to \p f_rng. This may be | ||||
|  *                 \c NULL if \p f_rng is \c NULL or doesn't need a context. | ||||
|  * \param mode     The mode of operation. This must be either | ||||
|  *                 #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated). | ||||
|  * \param md_alg   The message-digest algorithm used to hash the original data. | ||||
|  *                 Use #MBEDTLS_MD_NONE for signing raw data. | ||||
|  * \param hashlen  The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE. | ||||
|  * \param hash     The buffer holding the message digest. | ||||
|  * \param sig      The buffer holding the ciphertext. | ||||
|  * \param hashlen  The length of the message digest. | ||||
|  *                 This is only used if \p md_alg is #MBEDTLS_MD_NONE. | ||||
|  * \param hash     The buffer holding the message digest or raw data. | ||||
|  *                 If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable | ||||
|  *                 buffer of length \p hashlen Bytes. If \p md_alg is not | ||||
|  *                 #MBEDTLS_MD_NONE, it must be a readable buffer of length | ||||
|  *                 the size of the hash corresponding to \p md_alg. | ||||
|  * \param sig      The buffer holding the signature. This must be a readable | ||||
|  *                 buffer of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                 for an 2048-bit RSA modulus. | ||||
|  * | ||||
|  * \return         \c 0 if the verify operation was successful. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -990,9 +1086,6 @@ int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx, | |||
|  * \brief          This function performs a PKCS#1 v1.5 verification | ||||
|  *                 operation (RSASSA-PKCS1-v1_5-VERIFY). | ||||
|  * | ||||
|  * \note           The \p sig buffer must be as large as the size | ||||
|  *                 of \p ctx->N. For example, 128 Bytes if RSA-1024 is used. | ||||
|  * | ||||
|  * \deprecated     It is deprecated and discouraged to call this function | ||||
|  *                 in #MBEDTLS_RSA_PRIVATE mode. Future versions of the library | ||||
|  *                 are likely to remove the \p mode argument and have it | ||||
|  | @ -1002,15 +1095,26 @@ int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx, | |||
|  *                 mode being set to #MBEDTLS_RSA_PRIVATE and might instead | ||||
|  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED. | ||||
|  * | ||||
|  * \param ctx      The RSA public key context. | ||||
|  * \param f_rng    The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param p_rng    The RNG context. | ||||
|  * \param mode     #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param ctx      The initialized RSA public key context to use. | ||||
|  * \param f_rng    The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE, | ||||
|  *                 this is used for blinding and should be provided; see | ||||
|  *                 mbedtls_rsa_private() for more. Otherwise, it is ignored. | ||||
|  * \param p_rng    The RNG context to be passed to \p f_rng. This may be | ||||
|  *                 \c NULL if \p f_rng is \c NULL or doesn't need a context. | ||||
|  * \param mode     The mode of operation. This must be either | ||||
|  *                 #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated). | ||||
|  * \param md_alg   The message-digest algorithm used to hash the original data. | ||||
|  *                 Use #MBEDTLS_MD_NONE for signing raw data. | ||||
|  * \param hashlen  The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE. | ||||
|  * \param hash     The buffer holding the message digest. | ||||
|  * \param sig      The buffer holding the ciphertext. | ||||
|  * \param hashlen  The length of the message digest. | ||||
|  *                 This is only used if \p md_alg is #MBEDTLS_MD_NONE. | ||||
|  * \param hash     The buffer holding the message digest or raw data. | ||||
|  *                 If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable | ||||
|  *                 buffer of length \p hashlen Bytes. If \p md_alg is not | ||||
|  *                 #MBEDTLS_MD_NONE, it must be a readable buffer of length | ||||
|  *                 the size of the hash corresponding to \p md_alg. | ||||
|  * \param sig      The buffer holding the signature. This must be a readable | ||||
|  *                 buffer of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                 for an 2048-bit RSA modulus. | ||||
|  * | ||||
|  * \return         \c 0 if the verify operation was successful. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -1031,9 +1135,6 @@ int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx, | |||
|  *                 The hash function for the MGF mask generating function | ||||
|  *                 is that specified in the RSA context. | ||||
|  * | ||||
|  * \note           The \p sig buffer must be as large as the size | ||||
|  *                 of \p ctx->N. For example, 128 Bytes if RSA-1024 is used. | ||||
|  * | ||||
|  * \note           The \p hash_id in the RSA context is the one used for the | ||||
|  *                 verification. \p md_alg in the function call is the type of | ||||
|  *                 hash that is verified. According to <em>RFC-3447: Public-Key | ||||
|  | @ -1051,15 +1152,26 @@ int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx, | |||
|  *                 mode being set to #MBEDTLS_RSA_PRIVATE and might instead | ||||
|  *                 return #MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED. | ||||
|  * | ||||
|  * \param ctx      The RSA public key context. | ||||
|  * \param f_rng    The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param p_rng    The RNG context. | ||||
|  * \param mode     #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param ctx      The initialized RSA public key context to use. | ||||
|  * \param f_rng    The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE, | ||||
|  *                 this is used for blinding and should be provided; see | ||||
|  *                 mbedtls_rsa_private() for more. Otherwise, it is ignored. | ||||
|  * \param p_rng    The RNG context to be passed to \p f_rng. This may be | ||||
|  *                 \c NULL if \p f_rng is \c NULL or doesn't need a context. | ||||
|  * \param mode     The mode of operation. This must be either | ||||
|  *                 #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE (deprecated). | ||||
|  * \param md_alg   The message-digest algorithm used to hash the original data. | ||||
|  *                 Use #MBEDTLS_MD_NONE for signing raw data. | ||||
|  * \param hashlen  The length of the message digest. Only used if \p md_alg is #MBEDTLS_MD_NONE. | ||||
|  * \param hash     The buffer holding the message digest. | ||||
|  * \param sig      The buffer holding the ciphertext. | ||||
|  * \param hashlen  The length of the message digest. | ||||
|  *                 This is only used if \p md_alg is #MBEDTLS_MD_NONE. | ||||
|  * \param hash     The buffer holding the message digest or raw data. | ||||
|  *                 If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable | ||||
|  *                 buffer of length \p hashlen Bytes. If \p md_alg is not | ||||
|  *                 #MBEDTLS_MD_NONE, it must be a readable buffer of length | ||||
|  *                 the size of the hash corresponding to \p md_alg. | ||||
|  * \param sig      The buffer holding the signature. This must be a readable | ||||
|  *                 buffer of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                 for an 2048-bit RSA modulus. | ||||
|  * | ||||
|  * \return         \c 0 if the verify operation was successful. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -1085,19 +1197,29 @@ int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx, | |||
|  * | ||||
|  * \note           The \p hash_id in the RSA context is ignored. | ||||
|  * | ||||
|  * \param ctx      The RSA public key context. | ||||
|  * \param f_rng    The RNG function. Only needed for #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param p_rng    The RNG context. | ||||
|  * \param mode     #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param ctx      The initialized RSA public key context to use. | ||||
|  * \param f_rng    The RNG function to use. If \p mode is #MBEDTLS_RSA_PRIVATE, | ||||
|  *                 this is used for blinding and should be provided; see | ||||
|  *                 mbedtls_rsa_private() for more. Otherwise, it is ignored. | ||||
|  * \param p_rng    The RNG context to be passed to \p f_rng. This may be | ||||
|  *                 \c NULL if \p f_rng is \c NULL or doesn't need a context. | ||||
|  * \param mode     The mode of operation. This must be either | ||||
|  *                 #MBEDTLS_RSA_PUBLIC or #MBEDTLS_RSA_PRIVATE. | ||||
|  * \param md_alg   The message-digest algorithm used to hash the original data. | ||||
|  *                 Use #MBEDTLS_MD_NONE for signing raw data. | ||||
|  * \param hashlen  The length of the message digest. Only used if \p md_alg is | ||||
|  *                 #MBEDTLS_MD_NONE. | ||||
|  * \param hash     The buffer holding the message digest. | ||||
|  * \param mgf1_hash_id       The message digest used for mask generation. | ||||
|  * \param expected_salt_len  The length of the salt used in padding. Use | ||||
|  *                           #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length. | ||||
|  * \param sig      The buffer holding the ciphertext. | ||||
|  * \param hashlen  The length of the message digest. | ||||
|  *                 This is only used if \p md_alg is #MBEDTLS_MD_NONE. | ||||
|  * \param hash     The buffer holding the message digest or raw data. | ||||
|  *                 If \p md_alg is #MBEDTLS_MD_NONE, this must be a readable | ||||
|  *                 buffer of length \p hashlen Bytes. If \p md_alg is not | ||||
|  *                 #MBEDTLS_MD_NONE, it must be a readable buffer of length | ||||
|  *                 the size of the hash corresponding to \p md_alg. | ||||
|  * \param mgf1_hash_id      The message digest used for mask generation. | ||||
|  * \param expected_salt_len The length of the salt used in padding. Use | ||||
|  *                          #MBEDTLS_RSA_SALT_LEN_ANY to accept any salt length. | ||||
|  * \param sig      The buffer holding the signature. This must be a readable | ||||
|  *                 buffer of length \c ctx->len Bytes. For example, \c 256 Bytes | ||||
|  *                 for an 2048-bit RSA modulus. | ||||
|  * | ||||
|  * \return         \c 0 if the verify operation was successful. | ||||
|  * \return         An \c MBEDTLS_ERR_RSA_XXX error code on failure. | ||||
|  | @ -1116,8 +1238,8 @@ int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx, | |||
| /**
 | ||||
|  * \brief          This function copies the components of an RSA context. | ||||
|  * | ||||
|  * \param dst      The destination context. | ||||
|  * \param src      The source context. | ||||
|  * \param dst      The destination context. This must be initialized. | ||||
|  * \param src      The source context. This must be initialized. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory allocation failure. | ||||
|  | @ -1127,10 +1249,14 @@ int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src ) | |||
| /**
 | ||||
|  * \brief          This function frees the components of an RSA key. | ||||
|  * | ||||
|  * \param ctx      The RSA Context to free. | ||||
|  * \param ctx      The RSA context to free. 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 RSA context. | ||||
|  */ | ||||
| void mbedtls_rsa_free( mbedtls_rsa_context *ctx ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          The RSA checkup routine. | ||||
|  * | ||||
|  | @ -1139,6 +1265,8 @@ void mbedtls_rsa_free( mbedtls_rsa_context *ctx ); | |||
|  */ | ||||
| int mbedtls_rsa_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
|  | @ -42,6 +42,7 @@ | |||
| 
 | ||||
| /* MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED is deprecated and should not be used. */ | ||||
| #define MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED                  -0x0035  /**< SHA-1 hardware accelerator failed */ | ||||
| #define MBEDTLS_ERR_SHA1_BAD_INPUT_DATA                   -0x0073  /**< SHA-1 input data was malformed. */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
|  | @ -79,6 +80,7 @@ mbedtls_sha1_context; | |||
|  *                 stronger message digests instead. | ||||
|  * | ||||
|  * \param ctx      The SHA-1 context to initialize. | ||||
|  *                 This must not be \c NULL. | ||||
|  * | ||||
|  */ | ||||
| void mbedtls_sha1_init( mbedtls_sha1_context *ctx ); | ||||
|  | @ -90,7 +92,10 @@ void mbedtls_sha1_init( mbedtls_sha1_context *ctx ); | |||
|  *                 constitutes a security risk. We recommend considering | ||||
|  *                 stronger message digests instead. | ||||
|  * | ||||
|  * \param ctx      The SHA-1 context to clear. | ||||
|  * \param ctx      The SHA-1 context to clear. This may be \c NULL, | ||||
|  *                 in which case this function does nothing. If it is | ||||
|  *                 not \c NULL, it must point to an initialized | ||||
|  *                 SHA-1 context. | ||||
|  * | ||||
|  */ | ||||
| void mbedtls_sha1_free( mbedtls_sha1_context *ctx ); | ||||
|  | @ -102,8 +107,8 @@ void mbedtls_sha1_free( mbedtls_sha1_context *ctx ); | |||
|  *                 constitutes a security risk. We recommend considering | ||||
|  *                 stronger message digests instead. | ||||
|  * | ||||
|  * \param dst      The SHA-1 context to clone to. | ||||
|  * \param src      The SHA-1 context to clone from. | ||||
|  * \param dst      The SHA-1 context to clone to. This must be initialized. | ||||
|  * \param src      The SHA-1 context to clone from. This must be initialized. | ||||
|  * | ||||
|  */ | ||||
| void mbedtls_sha1_clone( mbedtls_sha1_context *dst, | ||||
|  | @ -116,9 +121,10 @@ void mbedtls_sha1_clone( mbedtls_sha1_context *dst, | |||
|  *                 constitutes a security risk. We recommend considering | ||||
|  *                 stronger message digests instead. | ||||
|  * | ||||
|  * \param ctx      The SHA-1 context to initialize. | ||||
|  * \param ctx      The SHA-1 context to initialize. This must be initialized. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  * | ||||
|  */ | ||||
| int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx ); | ||||
|  | @ -131,11 +137,14 @@ int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx ); | |||
|  *                 constitutes a security risk. We recommend considering | ||||
|  *                 stronger message digests instead. | ||||
|  * | ||||
|  * \param ctx      The SHA-1 context. | ||||
|  * \param ctx      The SHA-1 context. This must be initialized | ||||
|  *                 and have a hash operation started. | ||||
|  * \param input    The buffer holding the input data. | ||||
|  * \param ilen     The length of the input data. | ||||
|  *                 This must be a readable buffer of length \p ilen Bytes. | ||||
|  * \param ilen     The length of the input data \p input in Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, | ||||
|                              const unsigned char *input, | ||||
|  | @ -149,10 +158,13 @@ int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, | |||
|  *                 constitutes a security risk. We recommend considering | ||||
|  *                 stronger message digests instead. | ||||
|  * | ||||
|  * \param ctx      The SHA-1 context. | ||||
|  * \param output   The SHA-1 checksum result. | ||||
|  * \param ctx      The SHA-1 context to use. This must be initialized and | ||||
|  *                 have a hash operation started. | ||||
|  * \param output   The SHA-1 checksum result. This must be a writable | ||||
|  *                 buffer of length \c 20 Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx, | ||||
|                              unsigned char output[20] ); | ||||
|  | @ -164,10 +176,12 @@ int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx, | |||
|  *                 constitutes a security risk. We recommend considering | ||||
|  *                 stronger message digests instead. | ||||
|  * | ||||
|  * \param ctx      The SHA-1 context. | ||||
|  * \param data     The data block being processed. | ||||
|  * \param ctx      The SHA-1 context to use. This must be initialized. | ||||
|  * \param data     The data block being processed. This must be a | ||||
|  *                 readable buffer of length \c 64 Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  * | ||||
|  */ | ||||
| int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, | ||||
|  | @ -188,7 +202,7 @@ int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, | |||
|  * | ||||
|  * \deprecated     Superseded by mbedtls_sha1_starts_ret() in 2.7.0. | ||||
|  * | ||||
|  * \param ctx      The SHA-1 context to initialize. | ||||
|  * \param ctx      The SHA-1 context to initialize. This must be initialized. | ||||
|  * | ||||
|  */ | ||||
| MBEDTLS_DEPRECATED void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ); | ||||
|  | @ -203,9 +217,11 @@ MBEDTLS_DEPRECATED void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ); | |||
|  * | ||||
|  * \deprecated     Superseded by mbedtls_sha1_update_ret() in 2.7.0. | ||||
|  * | ||||
|  * \param ctx      The SHA-1 context. | ||||
|  * \param ctx      The SHA-1 context. This must be initialized and | ||||
|  *                 have a hash operation started. | ||||
|  * \param input    The buffer holding the input data. | ||||
|  * \param ilen     The length of the input data. | ||||
|  *                 This must be a readable buffer of length \p ilen Bytes. | ||||
|  * \param ilen     The length of the input data \p input in Bytes. | ||||
|  * | ||||
|  */ | ||||
| MBEDTLS_DEPRECATED void mbedtls_sha1_update( mbedtls_sha1_context *ctx, | ||||
|  | @ -222,9 +238,10 @@ MBEDTLS_DEPRECATED void mbedtls_sha1_update( mbedtls_sha1_context *ctx, | |||
|  * | ||||
|  * \deprecated     Superseded by mbedtls_sha1_finish_ret() in 2.7.0. | ||||
|  * | ||||
|  * \param ctx      The SHA-1 context. | ||||
|  * \param ctx      The SHA-1 context. This must be initialized and | ||||
|  *                 have a hash operation started. | ||||
|  * \param output   The SHA-1 checksum result. | ||||
|  * | ||||
|  *                 This must be a writable buffer of length \c 20 Bytes. | ||||
|  */ | ||||
| MBEDTLS_DEPRECATED void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, | ||||
|                                              unsigned char output[20] ); | ||||
|  | @ -238,8 +255,9 @@ MBEDTLS_DEPRECATED void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, | |||
|  * | ||||
|  * \deprecated     Superseded by mbedtls_internal_sha1_process() in 2.7.0. | ||||
|  * | ||||
|  * \param ctx      The SHA-1 context. | ||||
|  * \param ctx      The SHA-1 context. This must be initialized. | ||||
|  * \param data     The data block being processed. | ||||
|  *                 This must be a readable buffer of length \c 64 bytes. | ||||
|  * | ||||
|  */ | ||||
| MBEDTLS_DEPRECATED void mbedtls_sha1_process( mbedtls_sha1_context *ctx, | ||||
|  | @ -262,10 +280,13 @@ MBEDTLS_DEPRECATED void mbedtls_sha1_process( mbedtls_sha1_context *ctx, | |||
|  *                 stronger message digests instead. | ||||
|  * | ||||
|  * \param input    The buffer holding the input data. | ||||
|  * \param ilen     The length of the input data. | ||||
|  *                 This must be a readable buffer of length \p ilen Bytes. | ||||
|  * \param ilen     The length of the input data \p input in Bytes. | ||||
|  * \param output   The SHA-1 checksum result. | ||||
|  *                 This must be a writable buffer of length \c 20 Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  * | ||||
|  */ | ||||
| int mbedtls_sha1_ret( const unsigned char *input, | ||||
|  | @ -294,8 +315,10 @@ int mbedtls_sha1_ret( const unsigned char *input, | |||
|  * \deprecated     Superseded by mbedtls_sha1_ret() in 2.7.0 | ||||
|  * | ||||
|  * \param input    The buffer holding the input data. | ||||
|  * \param ilen     The length of the input data. | ||||
|  * \param output   The SHA-1 checksum result. | ||||
|  *                 This must be a readable buffer of length \p ilen Bytes. | ||||
|  * \param ilen     The length of the input data \p input in Bytes. | ||||
|  * \param output   The SHA-1 checksum result. This must be a writable | ||||
|  *                 buffer of size \c 20 Bytes. | ||||
|  * | ||||
|  */ | ||||
| MBEDTLS_DEPRECATED void mbedtls_sha1( const unsigned char *input, | ||||
|  | @ -305,6 +328,8 @@ MBEDTLS_DEPRECATED void mbedtls_sha1( const unsigned char *input, | |||
| #undef MBEDTLS_DEPRECATED | ||||
| #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          The SHA-1 checkup routine. | ||||
|  * | ||||
|  | @ -318,6 +343,8 @@ MBEDTLS_DEPRECATED void mbedtls_sha1( const unsigned char *input, | |||
|  */ | ||||
| int mbedtls_sha1_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
|  | @ -38,6 +38,7 @@ | |||
| 
 | ||||
| /* MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED is deprecated and should not be used. */ | ||||
| #define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED                -0x0037  /**< SHA-256 hardware accelerator failed */ | ||||
| #define MBEDTLS_ERR_SHA256_BAD_INPUT_DATA                 -0x0074  /**< SHA-256 input data was malformed. */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
|  | @ -71,22 +72,24 @@ mbedtls_sha256_context; | |||
| /**
 | ||||
|  * \brief          This function initializes a SHA-256 context. | ||||
|  * | ||||
|  * \param ctx      The SHA-256 context to initialize. | ||||
|  * \param ctx      The SHA-256 context to initialize. This must not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_sha256_init( mbedtls_sha256_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          This function clears a SHA-256 context. | ||||
|  * | ||||
|  * \param ctx      The SHA-256 context to clear. | ||||
|  * \param ctx      The SHA-256 context to clear. This may be \c NULL, in which | ||||
|  *                 case this function returns immediately. If it is not \c NULL, | ||||
|  *                 it must point to an initialized SHA-256 context. | ||||
|  */ | ||||
| void mbedtls_sha256_free( mbedtls_sha256_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          This function clones the state of a SHA-256 context. | ||||
|  * | ||||
|  * \param dst      The destination context. | ||||
|  * \param src      The context to clone. | ||||
|  * \param dst      The destination context. This must be initialized. | ||||
|  * \param src      The context to clone. This must be initialized. | ||||
|  */ | ||||
| void mbedtls_sha256_clone( mbedtls_sha256_context *dst, | ||||
|                            const mbedtls_sha256_context *src ); | ||||
|  | @ -95,11 +98,12 @@ void mbedtls_sha256_clone( mbedtls_sha256_context *dst, | |||
|  * \brief          This function starts a SHA-224 or SHA-256 checksum | ||||
|  *                 calculation. | ||||
|  * | ||||
|  * \param ctx      The context to initialize. | ||||
|  * \param is224    Determines which function to use: | ||||
|  *                 0: Use SHA-256, or 1: Use SHA-224. | ||||
|  * \param ctx      The context to use. This must be initialized. | ||||
|  * \param is224    This determines which function to use. This must be | ||||
|  *                 either \c 0 for SHA-256, or \c 1 for SHA-224. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 ); | ||||
| 
 | ||||
|  | @ -107,11 +111,14 @@ int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 ); | |||
|  * \brief          This function feeds an input buffer into an ongoing | ||||
|  *                 SHA-256 checksum calculation. | ||||
|  * | ||||
|  * \param ctx      The SHA-256 context. | ||||
|  * \param input    The buffer holding the data. | ||||
|  * \param ilen     The length of the input data. | ||||
|  * \param ctx      The SHA-256 context. This must be initialized | ||||
|  *                 and have a hash operation started. | ||||
|  * \param input    The buffer holding the data. This must be a readable | ||||
|  *                 buffer of length \p ilen Bytes. | ||||
|  * \param ilen     The length of the input data in Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, | ||||
|                                const unsigned char *input, | ||||
|  | @ -121,10 +128,13 @@ int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, | |||
|  * \brief          This function finishes the SHA-256 operation, and writes | ||||
|  *                 the result to the output buffer. | ||||
|  * | ||||
|  * \param ctx      The SHA-256 context. | ||||
|  * \param ctx      The SHA-256 context. This must be initialized | ||||
|  *                 and have a hash operation started. | ||||
|  * \param output   The SHA-224 or SHA-256 checksum result. | ||||
|  *                 This must be a writable buffer of length \c 32 Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, | ||||
|                                unsigned char output[32] ); | ||||
|  | @ -134,10 +144,12 @@ int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, | |||
|  *                 the ongoing SHA-256 computation. This function is for | ||||
|  *                 internal use only. | ||||
|  * | ||||
|  * \param ctx      The SHA-256 context. | ||||
|  * \param data     The buffer holding one block of data. | ||||
|  * \param ctx      The SHA-256 context. This must be initialized. | ||||
|  * \param data     The buffer holding one block of data. This must | ||||
|  *                 be a readable buffer of length \c 64 Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, | ||||
|                                      const unsigned char data[64] ); | ||||
|  | @ -152,12 +164,11 @@ int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, | |||
|  * \brief          This function starts a SHA-224 or SHA-256 checksum | ||||
|  *                 calculation. | ||||
|  * | ||||
|  * | ||||
|  * \deprecated     Superseded by mbedtls_sha256_starts_ret() in 2.7.0. | ||||
|  * | ||||
|  * \param ctx      The context to initialize. | ||||
|  * \param is224    Determines which function to use: | ||||
|  *                 0: Use SHA-256, or 1: Use SHA-224. | ||||
|  * \param ctx      The context to use. This must be initialized. | ||||
|  * \param is224    Determines which function to use. This must be | ||||
|  *                 either \c 0 for SHA-256, or \c 1 for SHA-224. | ||||
|  */ | ||||
| MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, | ||||
|                                                int is224 ); | ||||
|  | @ -168,9 +179,11 @@ MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, | |||
|  * | ||||
|  * \deprecated     Superseded by mbedtls_sha256_update_ret() in 2.7.0. | ||||
|  * | ||||
|  * \param ctx      The SHA-256 context to initialize. | ||||
|  * \param input    The buffer holding the data. | ||||
|  * \param ilen     The length of the input data. | ||||
|  * \param ctx      The SHA-256 context to use. This must be | ||||
|  *                 initialized and have a hash operation started. | ||||
|  * \param input    The buffer holding the data. This must be a readable | ||||
|  *                 buffer of length \p ilen Bytes. | ||||
|  * \param ilen     The length of the input data in Bytes. | ||||
|  */ | ||||
| MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx, | ||||
|                                                const unsigned char *input, | ||||
|  | @ -182,8 +195,10 @@ MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx, | |||
|  * | ||||
|  * \deprecated     Superseded by mbedtls_sha256_finish_ret() in 2.7.0. | ||||
|  * | ||||
|  * \param ctx      The SHA-256 context. | ||||
|  * \param output   The SHA-224 or SHA-256 checksum result. | ||||
|  * \param ctx      The SHA-256 context. This must be initialized and | ||||
|  *                 have a hash operation started. | ||||
|  * \param output   The SHA-224 or SHA-256 checksum result. This must be | ||||
|  *                 a writable buffer of length \c 32 Bytes. | ||||
|  */ | ||||
| MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, | ||||
|                                                unsigned char output[32] ); | ||||
|  | @ -195,8 +210,9 @@ MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, | |||
|  * | ||||
|  * \deprecated     Superseded by mbedtls_internal_sha256_process() in 2.7.0. | ||||
|  * | ||||
|  * \param ctx      The SHA-256 context. | ||||
|  * \param data     The buffer holding one block of data. | ||||
|  * \param ctx      The SHA-256 context. This must be initialized. | ||||
|  * \param data     The buffer holding one block of data. This must be | ||||
|  *                 a readable buffer of size \c 64 Bytes. | ||||
|  */ | ||||
| MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx, | ||||
|                                                 const unsigned char data[64] ); | ||||
|  | @ -214,11 +230,13 @@ MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx, | |||
|  *                 The SHA-256 result is calculated as | ||||
|  *                 output = SHA-256(input buffer). | ||||
|  * | ||||
|  * \param input    The buffer holding the input data. | ||||
|  * \param ilen     The length of the input data. | ||||
|  * \param output   The SHA-224 or SHA-256 checksum result. | ||||
|  * \param is224    Determines which function to use: | ||||
|  *                 0: Use SHA-256, or 1: Use SHA-224. | ||||
|  * \param input    The buffer holding the data. This must be a readable | ||||
|  *                 buffer of length \p ilen Bytes. | ||||
|  * \param ilen     The length of the input data in Bytes. | ||||
|  * \param output   The SHA-224 or SHA-256 checksum result. This must | ||||
|  *                 be a writable buffer of length \c 32 Bytes. | ||||
|  * \param is224    Determines which function to use. This must be | ||||
|  *                 either \c 0 for SHA-256, or \c 1 for SHA-224. | ||||
|  */ | ||||
| int mbedtls_sha256_ret( const unsigned char *input, | ||||
|                         size_t ilen, | ||||
|  | @ -244,11 +262,13 @@ int mbedtls_sha256_ret( const unsigned char *input, | |||
|  * | ||||
|  * \deprecated     Superseded by mbedtls_sha256_ret() in 2.7.0. | ||||
|  * | ||||
|  * \param input    The buffer holding the data. | ||||
|  * \param ilen     The length of the input data. | ||||
|  * \param output   The SHA-224 or SHA-256 checksum result. | ||||
|  * \param is224    Determines which function to use: | ||||
|  *                 0: Use SHA-256, or 1: Use SHA-224. | ||||
|  * \param input    The buffer holding the data. This must be a readable | ||||
|  *                 buffer of length \p ilen Bytes. | ||||
|  * \param ilen     The length of the input data in Bytes. | ||||
|  * \param output   The SHA-224 or SHA-256 checksum result. This must be | ||||
|  *                 a writable buffer of length \c 32 Bytes. | ||||
|  * \param is224    Determines which function to use. This must be either | ||||
|  *                 \c 0 for SHA-256, or \c 1 for SHA-224. | ||||
|  */ | ||||
| MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input, | ||||
|                                         size_t ilen, | ||||
|  | @ -258,6 +278,8 @@ MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input, | |||
| #undef MBEDTLS_DEPRECATED | ||||
| #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          The SHA-224 and SHA-256 checkup routine. | ||||
|  * | ||||
|  | @ -266,6 +288,8 @@ MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input, | |||
|  */ | ||||
| int mbedtls_sha256_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
|  | @ -37,6 +37,7 @@ | |||
| 
 | ||||
| /* MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED is deprecated and should not be used. */ | ||||
| #define MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED                -0x0039  /**< SHA-512 hardware accelerator failed */ | ||||
| #define MBEDTLS_ERR_SHA512_BAD_INPUT_DATA                 -0x0075  /**< SHA-512 input data was malformed. */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
|  | @ -70,22 +71,26 @@ mbedtls_sha512_context; | |||
| /**
 | ||||
|  * \brief          This function initializes a SHA-512 context. | ||||
|  * | ||||
|  * \param ctx      The SHA-512 context to initialize. | ||||
|  * \param ctx      The SHA-512 context to initialize. This must | ||||
|  *                 not be \c NULL. | ||||
|  */ | ||||
| void mbedtls_sha512_init( mbedtls_sha512_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          This function clears a SHA-512 context. | ||||
|  * | ||||
|  * \param ctx      The SHA-512 context to clear. | ||||
|  * \param ctx      The SHA-512 context to clear. This may be \c NULL, | ||||
|  *                 in which case this function does nothing. If it | ||||
|  *                 is not \c NULL, it must point to an initialized | ||||
|  *                 SHA-512 context. | ||||
|  */ | ||||
| void mbedtls_sha512_free( mbedtls_sha512_context *ctx ); | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          This function clones the state of a SHA-512 context. | ||||
|  * | ||||
|  * \param dst      The destination context. | ||||
|  * \param src      The context to clone. | ||||
|  * \param dst      The destination context. This must be initialized. | ||||
|  * \param src      The context to clone. This must be initialized. | ||||
|  */ | ||||
| void mbedtls_sha512_clone( mbedtls_sha512_context *dst, | ||||
|                            const mbedtls_sha512_context *src ); | ||||
|  | @ -94,11 +99,12 @@ void mbedtls_sha512_clone( mbedtls_sha512_context *dst, | |||
|  * \brief          This function starts a SHA-384 or SHA-512 checksum | ||||
|  *                 calculation. | ||||
|  * | ||||
|  * \param ctx      The SHA-512 context to initialize. | ||||
|  * \param is384    Determines which function to use: | ||||
|  *                 0: Use SHA-512, or 1: Use SHA-384. | ||||
|  * \param ctx      The SHA-512 context to use. This must be initialized. | ||||
|  * \param is384    Determines which function to use. This must be | ||||
|  *                 either \c for SHA-512, or \c 1 for SHA-384. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 ); | ||||
| 
 | ||||
|  | @ -106,11 +112,14 @@ int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 ); | |||
|  * \brief          This function feeds an input buffer into an ongoing | ||||
|  *                 SHA-512 checksum calculation. | ||||
|  * | ||||
|  * \param ctx      The SHA-512 context. | ||||
|  * \param input    The buffer holding the input data. | ||||
|  * \param ilen     The length of the input data. | ||||
|  * \param ctx      The SHA-512 context. This must be initialized | ||||
|  *                 and have a hash operation started. | ||||
|  * \param input    The buffer holding the input data. This must | ||||
|  *                 be a readable buffer of length \p ilen Bytes. | ||||
|  * \param ilen     The length of the input data in Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx, | ||||
|                     const unsigned char *input, | ||||
|  | @ -121,10 +130,13 @@ int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx, | |||
|  *                 the result to the output buffer. This function is for | ||||
|  *                 internal use only. | ||||
|  * | ||||
|  * \param ctx      The SHA-512 context. | ||||
|  * \param ctx      The SHA-512 context. This must be initialized | ||||
|  *                 and have a hash operation started. | ||||
|  * \param output   The SHA-384 or SHA-512 checksum result. | ||||
|  *                 This must be a writable buffer of length \c 64 Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx, | ||||
|                                unsigned char output[64] ); | ||||
|  | @ -133,10 +145,12 @@ int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx, | |||
|  * \brief          This function processes a single data block within | ||||
|  *                 the ongoing SHA-512 computation. | ||||
|  * | ||||
|  * \param ctx      The SHA-512 context. | ||||
|  * \param data     The buffer holding one block of data. | ||||
|  * \param ctx      The SHA-512 context. This must be initialized. | ||||
|  * \param data     The buffer holding one block of data. This | ||||
|  *                 must be a readable buffer of length \c 128 Bytes. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, | ||||
|                                      const unsigned char data[128] ); | ||||
|  | @ -152,9 +166,9 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, | |||
|  * | ||||
|  * \deprecated     Superseded by mbedtls_sha512_starts_ret() in 2.7.0 | ||||
|  * | ||||
|  * \param ctx      The SHA-512 context to initialize. | ||||
|  * \param is384    Determines which function to use: | ||||
|  *                 0: Use SHA-512, or 1: Use SHA-384. | ||||
|  * \param ctx      The SHA-512 context to use. This must be initialized. | ||||
|  * \param is384    Determines which function to use. This must be either | ||||
|  *                 \c 0 for SHA-512 or \c 1 for SHA-384. | ||||
|  */ | ||||
| MBEDTLS_DEPRECATED void mbedtls_sha512_starts( mbedtls_sha512_context *ctx, | ||||
|                                                int is384 ); | ||||
|  | @ -165,9 +179,11 @@ MBEDTLS_DEPRECATED void mbedtls_sha512_starts( mbedtls_sha512_context *ctx, | |||
|  * | ||||
|  * \deprecated     Superseded by mbedtls_sha512_update_ret() in 2.7.0. | ||||
|  * | ||||
|  * \param ctx      The SHA-512 context. | ||||
|  * \param input    The buffer holding the data. | ||||
|  * \param ilen     The length of the input data. | ||||
|  * \param ctx      The SHA-512 context. This must be initialized | ||||
|  *                 and have a hash operation started. | ||||
|  * \param input    The buffer holding the data. This must be a readable | ||||
|  *                 buffer of length \p ilen Bytes. | ||||
|  * \param ilen     The length of the input data in Bytes. | ||||
|  */ | ||||
| MBEDTLS_DEPRECATED void mbedtls_sha512_update( mbedtls_sha512_context *ctx, | ||||
|                                                const unsigned char *input, | ||||
|  | @ -179,8 +195,10 @@ MBEDTLS_DEPRECATED void mbedtls_sha512_update( mbedtls_sha512_context *ctx, | |||
|  * | ||||
|  * \deprecated     Superseded by mbedtls_sha512_finish_ret() in 2.7.0. | ||||
|  * | ||||
|  * \param ctx      The SHA-512 context. | ||||
|  * \param output   The SHA-384 or SHA-512 checksum result. | ||||
|  * \param ctx      The SHA-512 context. This must be initialized | ||||
|  *                 and have a hash operation started. | ||||
|  * \param output   The SHA-384 or SHA-512 checksum result. This must | ||||
|  *                 be a writable buffer of size \c 64 Bytes. | ||||
|  */ | ||||
| MBEDTLS_DEPRECATED void mbedtls_sha512_finish( mbedtls_sha512_context *ctx, | ||||
|                                                unsigned char output[64] ); | ||||
|  | @ -192,8 +210,9 @@ MBEDTLS_DEPRECATED void mbedtls_sha512_finish( mbedtls_sha512_context *ctx, | |||
|  * | ||||
|  * \deprecated     Superseded by mbedtls_internal_sha512_process() in 2.7.0. | ||||
|  * | ||||
|  * \param ctx      The SHA-512 context. | ||||
|  * \param data     The buffer holding one block of data. | ||||
|  * \param ctx      The SHA-512 context. This must be initialized. | ||||
|  * \param data     The buffer holding one block of data. This must be | ||||
|  *                 a readable buffer of length \c 128 Bytes. | ||||
|  */ | ||||
| MBEDTLS_DEPRECATED void mbedtls_sha512_process( | ||||
|                                             mbedtls_sha512_context *ctx, | ||||
|  | @ -212,13 +231,16 @@ MBEDTLS_DEPRECATED void mbedtls_sha512_process( | |||
|  *                 The SHA-512 result is calculated as | ||||
|  *                 output = SHA-512(input buffer). | ||||
|  * | ||||
|  * \param input    The buffer holding the input data. | ||||
|  * \param ilen     The length of the input data. | ||||
|  * \param input    The buffer holding the input data. This must be | ||||
|  *                 a readable buffer of length \p ilen Bytes. | ||||
|  * \param ilen     The length of the input data in Bytes. | ||||
|  * \param output   The SHA-384 or SHA-512 checksum result. | ||||
|  * \param is384    Determines which function to use: | ||||
|  *                 0: Use SHA-512, or 1: Use SHA-384. | ||||
|  *                 This must be a writable buffer of length \c 64 Bytes. | ||||
|  * \param is384    Determines which function to use. This must be either | ||||
|  *                 \c 0 for SHA-512, or \c 1 for SHA-384. | ||||
|  * | ||||
|  * \return         \c 0 on success. | ||||
|  * \return         A negative error code on failure. | ||||
|  */ | ||||
| int mbedtls_sha512_ret( const unsigned char *input, | ||||
|                         size_t ilen, | ||||
|  | @ -231,6 +253,7 @@ int mbedtls_sha512_ret( const unsigned char *input, | |||
| #else | ||||
| #define MBEDTLS_DEPRECATED | ||||
| #endif | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          This function calculates the SHA-512 or SHA-384 | ||||
|  *                 checksum of a buffer. | ||||
|  | @ -243,11 +266,13 @@ int mbedtls_sha512_ret( const unsigned char *input, | |||
|  * | ||||
|  * \deprecated     Superseded by mbedtls_sha512_ret() in 2.7.0 | ||||
|  * | ||||
|  * \param input    The buffer holding the data. | ||||
|  * \param ilen     The length of the input data. | ||||
|  * \param output   The SHA-384 or SHA-512 checksum result. | ||||
|  * \param is384    Determines which function to use: | ||||
|  *                 0: Use SHA-512, or 1: Use SHA-384. | ||||
|  * \param input    The buffer holding the data. This must be a | ||||
|  *                 readable buffer of length \p ilen Bytes. | ||||
|  * \param ilen     The length of the input data in Bytes. | ||||
|  * \param output   The SHA-384 or SHA-512 checksum result. This must | ||||
|  *                 be a writable buffer of length \c 64 Bytes. | ||||
|  * \param is384    Determines which function to use. This must be either | ||||
|  *                 \c 0 for SHA-512, or \c 1 for SHA-384. | ||||
|  */ | ||||
| MBEDTLS_DEPRECATED void mbedtls_sha512( const unsigned char *input, | ||||
|                                         size_t ilen, | ||||
|  | @ -256,6 +281,9 @@ MBEDTLS_DEPRECATED void mbedtls_sha512( const unsigned char *input, | |||
| 
 | ||||
| #undef MBEDTLS_DEPRECATED | ||||
| #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
|  /**
 | ||||
|  * \brief          The SHA-384 or SHA-512 checkup routine. | ||||
|  * | ||||
|  | @ -263,6 +291,7 @@ MBEDTLS_DEPRECATED void mbedtls_sha512( const unsigned char *input, | |||
|  * \return         \c 1 on failure. | ||||
|  */ | ||||
| int mbedtls_sha512_self_test( int verbose ); | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
|  |  | |||
|  | @ -2055,7 +2055,7 @@ void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, | |||
|  *                 provision more than one cert/key pair (eg one ECDSA, one | ||||
|  *                 RSA with SHA-256, one RSA with SHA-1). An adequate | ||||
|  *                 certificate will be selected according to the client's | ||||
|  *                 advertised capabilities. In case mutliple certificates are | ||||
|  *                 advertised capabilities. In case multiple certificates are | ||||
|  *                 adequate, preference is given to the one set by the first | ||||
|  *                 call to this function, then second, etc. | ||||
|  * | ||||
|  | @ -2066,6 +2066,14 @@ void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf, | |||
|  *                 whether it matches those preferences - the server can then | ||||
|  *                 decide what it wants to do with it. | ||||
|  * | ||||
|  * \note           The provided \p pk_key needs to match the public key in the | ||||
|  *                 first certificate in \p own_cert, or all handshakes using | ||||
|  *                 that certificate will fail. It is your responsibility | ||||
|  *                 to ensure that; this function will not perform any check. | ||||
|  *                 You may use mbedtls_pk_check_pair() in order to perform | ||||
|  *                 this check yourself, but be aware that this function can | ||||
|  *                 be computationally expensive on some key types. | ||||
|  * | ||||
|  * \param conf     SSL configuration | ||||
|  * \param own_cert own public certificate chain | ||||
|  * \param pk_key   own private key | ||||
|  | @ -3292,7 +3300,7 @@ void mbedtls_ssl_free( mbedtls_ssl_context *ssl ); | |||
|  *                 mbedtls_ssl_config_defaults() or mbedtls_ssl_config_free(). | ||||
|  * | ||||
|  * \note           You need to call mbedtls_ssl_config_defaults() unless you | ||||
|  *                 manually set all of the relevent fields yourself. | ||||
|  *                 manually set all of the relevant fields yourself. | ||||
|  * | ||||
|  * \param conf     SSL configuration context | ||||
|  */ | ||||
|  |  | |||
|  | @ -24,6 +24,12 @@ | |||
| #ifndef MBEDTLS_SSL_CACHE_H | ||||
| #define MBEDTLS_SSL_CACHE_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "ssl.h" | ||||
| 
 | ||||
| #if defined(MBEDTLS_THREADING_C) | ||||
|  |  | |||
|  | @ -24,6 +24,12 @@ | |||
| #ifndef MBEDTLS_SSL_CIPHERSUITES_H | ||||
| #define MBEDTLS_SSL_CIPHERSUITES_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "pk.h" | ||||
| #include "cipher.h" | ||||
| #include "md.h" | ||||
|  |  | |||
|  | @ -24,6 +24,12 @@ | |||
| #ifndef MBEDTLS_SSL_COOKIE_H | ||||
| #define MBEDTLS_SSL_COOKIE_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "ssl.h" | ||||
| 
 | ||||
| #if defined(MBEDTLS_THREADING_C) | ||||
|  |  | |||
|  | @ -24,9 +24,19 @@ | |||
| #ifndef MBEDTLS_SSL_INTERNAL_H | ||||
| #define MBEDTLS_SSL_INTERNAL_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| #include "ssl.h" | ||||
| #include "cipher.h" | ||||
| 
 | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
| #include "psa/crypto.h" | ||||
| #endif | ||||
| 
 | ||||
| #if defined(MBEDTLS_MD5_C) | ||||
| #include "md5.h" | ||||
| #endif | ||||
|  | @ -370,11 +380,19 @@ struct mbedtls_ssl_handshake_params | |||
| #endif | ||||
| #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
| #if defined(MBEDTLS_SHA256_C) | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     psa_hash_operation_t fin_sha256_psa; | ||||
| #else | ||||
|     mbedtls_sha256_context fin_sha256; | ||||
| #endif | ||||
| #endif | ||||
| #if defined(MBEDTLS_SHA512_C) | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     psa_hash_operation_t fin_sha384_psa; | ||||
| #else | ||||
|     mbedtls_sha512_context fin_sha512; | ||||
| #endif | ||||
| #endif | ||||
| #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | ||||
| 
 | ||||
|     void (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t); | ||||
|  | @ -765,6 +783,7 @@ int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, | |||
| 
 | ||||
| #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | ||||
|     defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
| /* The hash buffer must have at least MBEDTLS_MD_MAX_SIZE bytes of length. */ | ||||
| int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, | ||||
|                                             unsigned char *hash, size_t *hashlen, | ||||
|                                             unsigned char *data, size_t data_len, | ||||
|  |  | |||
|  | @ -24,6 +24,12 @@ | |||
| #ifndef MBEDTLS_SSL_TICKET_H | ||||
| #define MBEDTLS_SSL_TICKET_H | ||||
| 
 | ||||
| #if !defined(MBEDTLS_CONFIG_FILE) | ||||
| #include "config.h" | ||||
| #else | ||||
| #include MBEDTLS_CONFIG_FILE | ||||
| #endif | ||||
| 
 | ||||
| /*
 | ||||
|  * This implementation of the session ticket callbacks includes key | ||||
|  * management, rotating the keys periodically in order to preserve forward | ||||
|  | @ -111,14 +117,14 @@ int mbedtls_ssl_ticket_setup( mbedtls_ssl_ticket_context *ctx, | |||
| /**
 | ||||
|  * \brief           Implementation of the ticket write callback | ||||
|  * | ||||
|  * \note            See \c mbedlts_ssl_ticket_write_t for description | ||||
|  * \note            See \c mbedtls_ssl_ticket_write_t for description | ||||
|  */ | ||||
| mbedtls_ssl_ticket_write_t mbedtls_ssl_ticket_write; | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief           Implementation of the ticket parse callback | ||||
|  * | ||||
|  * \note            See \c mbedlts_ssl_ticket_parse_t for description | ||||
|  * \note            See \c mbedtls_ssl_ticket_parse_t for description | ||||
|  */ | ||||
| mbedtls_ssl_ticket_parse_t mbedtls_ssl_ticket_parse; | ||||
| 
 | ||||
|  |  | |||
|  | @ -39,7 +39,7 @@ | |||
|  * Major, Minor, Patchlevel | ||||
|  */ | ||||
| #define MBEDTLS_VERSION_MAJOR  2 | ||||
| #define MBEDTLS_VERSION_MINOR  14 | ||||
| #define MBEDTLS_VERSION_MINOR  16 | ||||
| #define MBEDTLS_VERSION_PATCH  0 | ||||
| 
 | ||||
| /**
 | ||||
|  | @ -47,9 +47,9 @@ | |||
|  *    MMNNPP00 | ||||
|  *    Major version | Minor version | Patch version | ||||
|  */ | ||||
| #define MBEDTLS_VERSION_NUMBER         0x020E0000 | ||||
| #define MBEDTLS_VERSION_STRING         "2.14.0" | ||||
| #define MBEDTLS_VERSION_STRING_FULL    "mbed TLS 2.14.0" | ||||
| #define MBEDTLS_VERSION_NUMBER         0x02100000 | ||||
| #define MBEDTLS_VERSION_STRING         "2.16.0" | ||||
| #define MBEDTLS_VERSION_STRING_FULL    "mbed TLS 2.16.0" | ||||
| 
 | ||||
| #if defined(MBEDTLS_VERSION_C) | ||||
| 
 | ||||
|  |  | |||
|  | @ -77,7 +77,7 @@ | |||
| #define MBEDTLS_ERR_X509_ALLOC_FAILED                     -0x2880  /**< Allocation of memory failed. */ | ||||
| #define MBEDTLS_ERR_X509_FILE_IO_ERROR                    -0x2900  /**< Read/write of file failed. */ | ||||
| #define MBEDTLS_ERR_X509_BUFFER_TOO_SMALL                 -0x2980  /**< Destination buffer is too small. */ | ||||
| #define MBEDTLS_ERR_X509_FATAL_ERROR                      -0x3000  /**< A fatal error occured, eg the chain is too long or the vrfy callback failed. */ | ||||
| #define MBEDTLS_ERR_X509_FATAL_ERROR                      -0x3000  /**< A fatal error occurred, eg the chain is too long or the vrfy callback failed. */ | ||||
| /* \} name */ | ||||
| 
 | ||||
| /**
 | ||||
|  | @ -250,7 +250,7 @@ int mbedtls_x509_serial_gets( char *buf, size_t size, const mbedtls_x509_buf *se | |||
|  * | ||||
|  * \param to       mbedtls_x509_time to check | ||||
|  * | ||||
|  * \return         1 if the given time is in the past or an error occured, | ||||
|  * \return         1 if the given time is in the past or an error occurred, | ||||
|  *                 0 otherwise. | ||||
|  */ | ||||
| int mbedtls_x509_time_is_past( const mbedtls_x509_time *to ); | ||||
|  | @ -264,11 +264,13 @@ int mbedtls_x509_time_is_past( const mbedtls_x509_time *to ); | |||
|  * | ||||
|  * \param from     mbedtls_x509_time to check | ||||
|  * | ||||
|  * \return         1 if the given time is in the future or an error occured, | ||||
|  * \return         1 if the given time is in the future or an error occurred, | ||||
|  *                 0 otherwise. | ||||
|  */ | ||||
| int mbedtls_x509_time_is_future( const mbedtls_x509_time *from ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          Checkup routine | ||||
|  * | ||||
|  | @ -276,6 +278,8 @@ int mbedtls_x509_time_is_future( const mbedtls_x509_time *from ); | |||
|  */ | ||||
| int mbedtls_x509_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| /*
 | ||||
|  * Internal module functions. You probably do not want to use these unless you | ||||
|  * know you do. | ||||
|  |  | |||
|  | @ -111,7 +111,7 @@ int mbedtls_x509_crl_parse_der( mbedtls_x509_crl *chain, | |||
| /**
 | ||||
|  * \brief          Parse one or more CRLs and append them to the chained list | ||||
|  * | ||||
|  * \note           Mutliple CRLs are accepted only if using PEM format | ||||
|  * \note           Multiple CRLs are accepted only if using PEM format | ||||
|  * | ||||
|  * \param chain    points to the start of the chain | ||||
|  * \param buf      buffer holding the CRL data in PEM or DER format | ||||
|  | @ -126,7 +126,7 @@ int mbedtls_x509_crl_parse( mbedtls_x509_crl *chain, const unsigned char *buf, s | |||
| /**
 | ||||
|  * \brief          Load one or more CRLs and append them to the chained list | ||||
|  * | ||||
|  * \note           Mutliple CRLs are accepted only if using PEM format | ||||
|  * \note           Multiple CRLs are accepted only if using PEM format | ||||
|  * | ||||
|  * \param chain    points to the start of the chain | ||||
|  * \param path     filename to read the CRLs from (in PEM or DER encoding) | ||||
|  |  | |||
|  | @ -121,6 +121,8 @@ int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, | |||
|                     unsigned char *output); | ||||
| #endif /* MBEDTLS_CIPHER_MODE_CBC */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_SELF_TEST) | ||||
| 
 | ||||
| /**
 | ||||
|  * \brief          Checkup routine | ||||
|  * | ||||
|  | @ -128,6 +130,8 @@ int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, | |||
|  */ | ||||
| int mbedtls_xtea_self_test( int verbose ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  |  | |||
|  | @ -179,7 +179,7 @@ endif(USE_STATIC_MBEDTLS_LIBRARY) | |||
| 
 | ||||
| if(USE_SHARED_MBEDTLS_LIBRARY) | ||||
|     add_library(mbedcrypto SHARED ${src_crypto}) | ||||
|     set_target_properties(mbedcrypto PROPERTIES VERSION 2.14.0 SOVERSION 3) | ||||
|     set_target_properties(mbedcrypto PROPERTIES VERSION 2.16.0 SOVERSION 3) | ||||
|     target_link_libraries(mbedcrypto ${libs}) | ||||
|     target_include_directories(mbedcrypto | ||||
|         PUBLIC ${CMAKE_SOURCE_DIR}/include/ | ||||
|  | @ -191,11 +191,11 @@ if(USE_SHARED_MBEDTLS_LIBRARY) | |||
|                 PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) | ||||
|     else() | ||||
|         add_library(mbedx509 SHARED ${src_x509}) | ||||
|         set_target_properties(mbedx509 PROPERTIES VERSION 2.14.0 SOVERSION 0) | ||||
|         set_target_properties(mbedx509 PROPERTIES VERSION 2.16.0 SOVERSION 0) | ||||
|         target_link_libraries(mbedx509 ${libs} mbedcrypto) | ||||
| 
 | ||||
|         add_library(mbedtls SHARED ${src_tls}) | ||||
|         set_target_properties(mbedtls PROPERTIES VERSION 2.14.0 SOVERSION 12) | ||||
|         set_target_properties(mbedtls PROPERTIES VERSION 2.16.0 SOVERSION 12) | ||||
|         target_link_libraries(mbedtls ${libs} mbedx509) | ||||
| 
 | ||||
|         install(TARGETS mbedtls mbedx509 mbedcrypto | ||||
|  |  | |||
|  | @ -40,7 +40,7 @@ SOEXT_TLS=so.12 | |||
| SOEXT_X509=so.0 | ||||
| SOEXT_CRYPTO=so.3 | ||||
| 
 | ||||
| # Set AR_DASH= (empty string) to use an ar implentation that does not accept
 | ||||
| # Set AR_DASH= (empty string) to use an ar implementation that does not accept
 | ||||
| # the - prefix for command line options (e.g. llvm-ar)
 | ||||
| AR_DASH ?= - | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										121
									
								
								library/aes.c
									
									
									
									
									
								
							
							
						
						
									
										121
									
								
								library/aes.c
									
									
									
									
									
								
							|  | @ -56,6 +56,12 @@ | |||
| 
 | ||||
| #if !defined(MBEDTLS_AES_ALT) | ||||
| 
 | ||||
| /* Parameter validation macros based on platform_util.h */ | ||||
| #define AES_VALIDATE_RET( cond )    \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_AES_BAD_INPUT_DATA ) | ||||
| #define AES_VALIDATE( cond )        \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| /*
 | ||||
|  * 32-bit integer manipulation macros (little endian) | ||||
|  */ | ||||
|  | @ -511,6 +517,8 @@ static void aes_gen_tables( void ) | |||
| 
 | ||||
| void mbedtls_aes_init( mbedtls_aes_context *ctx ) | ||||
| { | ||||
|     AES_VALIDATE( ctx != NULL ); | ||||
| 
 | ||||
|     memset( ctx, 0, sizeof( mbedtls_aes_context ) ); | ||||
| } | ||||
| 
 | ||||
|  | @ -525,12 +533,17 @@ void mbedtls_aes_free( mbedtls_aes_context *ctx ) | |||
| #if defined(MBEDTLS_CIPHER_MODE_XTS) | ||||
| void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx ) | ||||
| { | ||||
|     AES_VALIDATE( ctx != NULL ); | ||||
| 
 | ||||
|     mbedtls_aes_init( &ctx->crypt ); | ||||
|     mbedtls_aes_init( &ctx->tweak ); | ||||
| } | ||||
| 
 | ||||
| void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx ) | ||||
| { | ||||
|     if( ctx == NULL ) | ||||
|         return; | ||||
| 
 | ||||
|     mbedtls_aes_free( &ctx->crypt ); | ||||
|     mbedtls_aes_free( &ctx->tweak ); | ||||
| } | ||||
|  | @ -546,14 +559,8 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, | |||
|     unsigned int i; | ||||
|     uint32_t *RK; | ||||
| 
 | ||||
| #if !defined(MBEDTLS_AES_ROM_TABLES) | ||||
|     if( aes_init_done == 0 ) | ||||
|     { | ||||
|         aes_gen_tables(); | ||||
|         aes_init_done = 1; | ||||
| 
 | ||||
|     } | ||||
| #endif | ||||
|     AES_VALIDATE_RET( ctx != NULL ); | ||||
|     AES_VALIDATE_RET( key != NULL ); | ||||
| 
 | ||||
|     switch( keybits ) | ||||
|     { | ||||
|  | @ -563,6 +570,14 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, | |||
|         default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH ); | ||||
|     } | ||||
| 
 | ||||
| #if !defined(MBEDTLS_AES_ROM_TABLES) | ||||
|     if( aes_init_done == 0 ) | ||||
|     { | ||||
|         aes_gen_tables(); | ||||
|         aes_init_done = 1; | ||||
|     } | ||||
| #endif | ||||
| 
 | ||||
| #if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_PADLOCK_ALIGN16) | ||||
|     if( aes_padlock_ace == -1 ) | ||||
|         aes_padlock_ace = mbedtls_padlock_has_support( MBEDTLS_PADLOCK_ACE ); | ||||
|  | @ -662,6 +677,9 @@ int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key, | |||
|     uint32_t *RK; | ||||
|     uint32_t *SK; | ||||
| 
 | ||||
|     AES_VALIDATE_RET( ctx != NULL ); | ||||
|     AES_VALIDATE_RET( key != NULL ); | ||||
| 
 | ||||
|     mbedtls_aes_init( &cty ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_PADLOCK_ALIGN16) | ||||
|  | @ -752,6 +770,9 @@ int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx, | |||
|     const unsigned char *key1, *key2; | ||||
|     unsigned int key1bits, key2bits; | ||||
| 
 | ||||
|     AES_VALIDATE_RET( ctx != NULL ); | ||||
|     AES_VALIDATE_RET( key != NULL ); | ||||
| 
 | ||||
|     ret = mbedtls_aes_xts_decode_keys( key, keybits, &key1, &key1bits, | ||||
|                                        &key2, &key2bits ); | ||||
|     if( ret != 0 ) | ||||
|  | @ -774,6 +795,9 @@ int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx, | |||
|     const unsigned char *key1, *key2; | ||||
|     unsigned int key1bits, key2bits; | ||||
| 
 | ||||
|     AES_VALIDATE_RET( ctx != NULL ); | ||||
|     AES_VALIDATE_RET( key != NULL ); | ||||
| 
 | ||||
|     ret = mbedtls_aes_xts_decode_keys( key, keybits, &key1, &key1bits, | ||||
|                                        &key2, &key2bits ); | ||||
|     if( ret != 0 ) | ||||
|  | @ -977,10 +1001,16 @@ void mbedtls_aes_decrypt( mbedtls_aes_context *ctx, | |||
|  * AES-ECB block encryption/decryption | ||||
|  */ | ||||
| int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx, | ||||
|                     int mode, | ||||
|                     const unsigned char input[16], | ||||
|                     unsigned char output[16] ) | ||||
|                            int mode, | ||||
|                            const unsigned char input[16], | ||||
|                            unsigned char output[16] ) | ||||
| { | ||||
|     AES_VALIDATE_RET( ctx != NULL ); | ||||
|     AES_VALIDATE_RET( input != NULL ); | ||||
|     AES_VALIDATE_RET( output != NULL ); | ||||
|     AES_VALIDATE_RET( mode == MBEDTLS_AES_ENCRYPT || | ||||
|                       mode == MBEDTLS_AES_DECRYPT ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64) | ||||
|     if( mbedtls_aesni_has_support( MBEDTLS_AESNI_AES ) ) | ||||
|         return( mbedtls_aesni_crypt_ecb( ctx, mode, input, output ) ); | ||||
|  | @ -1018,6 +1048,13 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, | |||
|     int i; | ||||
|     unsigned char temp[16]; | ||||
| 
 | ||||
|     AES_VALIDATE_RET( ctx != NULL ); | ||||
|     AES_VALIDATE_RET( mode == MBEDTLS_AES_ENCRYPT || | ||||
|                       mode == MBEDTLS_AES_DECRYPT ); | ||||
|     AES_VALIDATE_RET( iv != NULL ); | ||||
|     AES_VALIDATE_RET( input != NULL ); | ||||
|     AES_VALIDATE_RET( output != NULL ); | ||||
| 
 | ||||
|     if( length % 16 ) | ||||
|         return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ); | ||||
| 
 | ||||
|  | @ -1143,6 +1180,13 @@ int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx, | |||
|     unsigned char prev_tweak[16]; | ||||
|     unsigned char tmp[16]; | ||||
| 
 | ||||
|     AES_VALIDATE_RET( ctx != NULL ); | ||||
|     AES_VALIDATE_RET( mode == MBEDTLS_AES_ENCRYPT || | ||||
|                       mode == MBEDTLS_AES_DECRYPT ); | ||||
|     AES_VALIDATE_RET( data_unit != NULL ); | ||||
|     AES_VALIDATE_RET( input != NULL ); | ||||
|     AES_VALIDATE_RET( output != NULL ); | ||||
| 
 | ||||
|     /* Data units must be at least 16 bytes long. */ | ||||
|     if( length < 16 ) | ||||
|         return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH; | ||||
|  | @ -1242,7 +1286,20 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, | |||
|                        unsigned char *output ) | ||||
| { | ||||
|     int c; | ||||
|     size_t n = *iv_off; | ||||
|     size_t n; | ||||
| 
 | ||||
|     AES_VALIDATE_RET( ctx != NULL ); | ||||
|     AES_VALIDATE_RET( mode == MBEDTLS_AES_ENCRYPT || | ||||
|                       mode == MBEDTLS_AES_DECRYPT ); | ||||
|     AES_VALIDATE_RET( iv_off != NULL ); | ||||
|     AES_VALIDATE_RET( iv != NULL ); | ||||
|     AES_VALIDATE_RET( input != NULL ); | ||||
|     AES_VALIDATE_RET( output != NULL ); | ||||
| 
 | ||||
|     n = *iv_off; | ||||
| 
 | ||||
|     if( n > 15 ) | ||||
|         return( MBEDTLS_ERR_AES_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( mode == MBEDTLS_AES_DECRYPT ) | ||||
|     { | ||||
|  | @ -1280,15 +1337,21 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx, | |||
|  * AES-CFB8 buffer encryption/decryption | ||||
|  */ | ||||
| int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx, | ||||
|                        int mode, | ||||
|                        size_t length, | ||||
|                        unsigned char iv[16], | ||||
|                        const unsigned char *input, | ||||
|                        unsigned char *output ) | ||||
|                             int mode, | ||||
|                             size_t length, | ||||
|                             unsigned char iv[16], | ||||
|                             const unsigned char *input, | ||||
|                             unsigned char *output ) | ||||
| { | ||||
|     unsigned char c; | ||||
|     unsigned char ov[17]; | ||||
| 
 | ||||
|     AES_VALIDATE_RET( ctx != NULL ); | ||||
|     AES_VALIDATE_RET( mode == MBEDTLS_AES_ENCRYPT || | ||||
|                       mode == MBEDTLS_AES_DECRYPT ); | ||||
|     AES_VALIDATE_RET( iv != NULL ); | ||||
|     AES_VALIDATE_RET( input != NULL ); | ||||
|     AES_VALIDATE_RET( output != NULL ); | ||||
|     while( length-- ) | ||||
|     { | ||||
|         memcpy( ov, iv, 16 ); | ||||
|  | @ -1321,7 +1384,18 @@ int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx, | |||
|                            unsigned char *output ) | ||||
| { | ||||
|     int ret = 0; | ||||
|     size_t n = *iv_off; | ||||
|     size_t n; | ||||
| 
 | ||||
|     AES_VALIDATE_RET( ctx != NULL ); | ||||
|     AES_VALIDATE_RET( iv_off != NULL ); | ||||
|     AES_VALIDATE_RET( iv != NULL ); | ||||
|     AES_VALIDATE_RET( input != NULL ); | ||||
|     AES_VALIDATE_RET( output != NULL ); | ||||
| 
 | ||||
|     n = *iv_off; | ||||
| 
 | ||||
|     if( n > 15 ) | ||||
|         return( MBEDTLS_ERR_AES_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     while( length-- ) | ||||
|     { | ||||
|  | @ -1356,7 +1430,16 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx, | |||
|                        unsigned char *output ) | ||||
| { | ||||
|     int c, i; | ||||
|     size_t n = *nc_off; | ||||
|     size_t n; | ||||
| 
 | ||||
|     AES_VALIDATE_RET( ctx != NULL ); | ||||
|     AES_VALIDATE_RET( nc_off != NULL ); | ||||
|     AES_VALIDATE_RET( nonce_counter != NULL ); | ||||
|     AES_VALIDATE_RET( stream_block != NULL ); | ||||
|     AES_VALIDATE_RET( input != NULL ); | ||||
|     AES_VALIDATE_RET( output != NULL ); | ||||
| 
 | ||||
|     n = *nc_off; | ||||
| 
 | ||||
|     if ( n > 0x0F ) | ||||
|         return( MBEDTLS_ERR_AES_BAD_INPUT_DATA ); | ||||
|  |  | |||
|  | @ -55,6 +55,12 @@ | |||
| #define inline __inline | ||||
| #endif | ||||
| 
 | ||||
| /* Parameter validation macros */ | ||||
| #define ARIA_VALIDATE_RET( cond )                                       \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ARIA_BAD_INPUT_DATA ) | ||||
| #define ARIA_VALIDATE( cond )                                           \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| /*
 | ||||
|  * 32-bit integer manipulation macros (little endian) | ||||
|  */ | ||||
|  | @ -449,9 +455,11 @@ int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx, | |||
| 
 | ||||
|     int i; | ||||
|     uint32_t w[4][4], *w2; | ||||
|     ARIA_VALIDATE_RET( ctx != NULL ); | ||||
|     ARIA_VALIDATE_RET( key != NULL ); | ||||
| 
 | ||||
|     if( keybits != 128 && keybits != 192 && keybits != 256 ) | ||||
|         return( MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH ); | ||||
|         return( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     /* Copy key to W0 (and potential remainder to W1) */ | ||||
|     GET_UINT32_LE( w[0][0], key,  0 ); | ||||
|  | @ -503,6 +511,8 @@ int mbedtls_aria_setkey_dec( mbedtls_aria_context *ctx, | |||
|                              const unsigned char *key, unsigned int keybits ) | ||||
| { | ||||
|     int i, j, k, ret; | ||||
|     ARIA_VALIDATE_RET( ctx != NULL ); | ||||
|     ARIA_VALIDATE_RET( key != NULL ); | ||||
| 
 | ||||
|     ret = mbedtls_aria_setkey_enc( ctx, key, keybits ); | ||||
|     if( ret != 0 ) | ||||
|  | @ -539,6 +549,9 @@ int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx, | |||
|     int i; | ||||
| 
 | ||||
|     uint32_t a, b, c, d; | ||||
|     ARIA_VALIDATE_RET( ctx != NULL ); | ||||
|     ARIA_VALIDATE_RET( input != NULL ); | ||||
|     ARIA_VALIDATE_RET( output != NULL ); | ||||
| 
 | ||||
|     GET_UINT32_LE( a, input,  0 ); | ||||
|     GET_UINT32_LE( b, input,  4 ); | ||||
|  | @ -586,6 +599,7 @@ int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx, | |||
| /* Initialize context */ | ||||
| void mbedtls_aria_init( mbedtls_aria_context *ctx ) | ||||
| { | ||||
|     ARIA_VALIDATE( ctx != NULL ); | ||||
|     memset( ctx, 0, sizeof( mbedtls_aria_context ) ); | ||||
| } | ||||
| 
 | ||||
|  | @ -612,6 +626,13 @@ int mbedtls_aria_crypt_cbc( mbedtls_aria_context *ctx, | |||
|     int i; | ||||
|     unsigned char temp[MBEDTLS_ARIA_BLOCKSIZE]; | ||||
| 
 | ||||
|     ARIA_VALIDATE_RET( ctx != NULL ); | ||||
|     ARIA_VALIDATE_RET( mode == MBEDTLS_ARIA_ENCRYPT || | ||||
|                        mode == MBEDTLS_ARIA_DECRYPT ); | ||||
|     ARIA_VALIDATE_RET( length == 0 || input  != NULL ); | ||||
|     ARIA_VALIDATE_RET( length == 0 || output != NULL ); | ||||
|     ARIA_VALIDATE_RET( iv != NULL ); | ||||
| 
 | ||||
|     if( length % MBEDTLS_ARIA_BLOCKSIZE ) | ||||
|         return( MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH ); | ||||
| 
 | ||||
|  | @ -665,7 +686,23 @@ int mbedtls_aria_crypt_cfb128( mbedtls_aria_context *ctx, | |||
|                                unsigned char *output ) | ||||
| { | ||||
|     unsigned char c; | ||||
|     size_t n = *iv_off; | ||||
|     size_t n; | ||||
| 
 | ||||
|     ARIA_VALIDATE_RET( ctx != NULL ); | ||||
|     ARIA_VALIDATE_RET( mode == MBEDTLS_ARIA_ENCRYPT || | ||||
|                        mode == MBEDTLS_ARIA_DECRYPT ); | ||||
|     ARIA_VALIDATE_RET( length == 0 || input  != NULL ); | ||||
|     ARIA_VALIDATE_RET( length == 0 || output != NULL ); | ||||
|     ARIA_VALIDATE_RET( iv != NULL ); | ||||
|     ARIA_VALIDATE_RET( iv_off != NULL ); | ||||
| 
 | ||||
|     n = *iv_off; | ||||
| 
 | ||||
|     /* An overly large value of n can lead to an unlimited
 | ||||
|      * buffer overflow. Therefore, guard against this | ||||
|      * outside of parameter validation. */ | ||||
|     if( n >= MBEDTLS_ARIA_BLOCKSIZE ) | ||||
|         return( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( mode == MBEDTLS_ARIA_DECRYPT ) | ||||
|     { | ||||
|  | @ -713,7 +750,21 @@ int mbedtls_aria_crypt_ctr( mbedtls_aria_context *ctx, | |||
|                             unsigned char *output ) | ||||
| { | ||||
|     int c, i; | ||||
|     size_t n = *nc_off; | ||||
|     size_t n; | ||||
| 
 | ||||
|     ARIA_VALIDATE_RET( ctx != NULL ); | ||||
|     ARIA_VALIDATE_RET( length == 0 || input  != NULL ); | ||||
|     ARIA_VALIDATE_RET( length == 0 || output != NULL ); | ||||
|     ARIA_VALIDATE_RET( nonce_counter != NULL ); | ||||
|     ARIA_VALIDATE_RET( stream_block  != NULL ); | ||||
|     ARIA_VALIDATE_RET( nc_off != NULL ); | ||||
| 
 | ||||
|     n = *nc_off; | ||||
|     /* An overly large value of n can lead to an unlimited
 | ||||
|      * buffer overflow. Therefore, guard against this | ||||
|      * outside of parameter validation. */ | ||||
|     if( n >= MBEDTLS_ARIA_BLOCKSIZE ) | ||||
|         return( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     while( length-- ) | ||||
|     { | ||||
|  |  | |||
							
								
								
									
										303
									
								
								library/bignum.c
									
									
									
									
									
								
							
							
						
						
									
										303
									
								
								library/bignum.c
									
									
									
									
									
								
							|  | @ -59,6 +59,11 @@ | |||
| #define mbedtls_free       free | ||||
| #endif | ||||
| 
 | ||||
| #define MPI_VALIDATE_RET( cond )                                       \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_MPI_BAD_INPUT_DATA ) | ||||
| #define MPI_VALIDATE( cond )                                           \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| #define ciL    (sizeof(mbedtls_mpi_uint))         /* chars in limb  */ | ||||
| #define biL    (ciL << 3)               /* bits  in limb  */ | ||||
| #define biH    (ciL << 2)               /* half limb size */ | ||||
|  | @ -83,8 +88,7 @@ static void mbedtls_mpi_zeroize( mbedtls_mpi_uint *v, size_t n ) | |||
|  */ | ||||
| void mbedtls_mpi_init( mbedtls_mpi *X ) | ||||
| { | ||||
|     if( X == NULL ) | ||||
|         return; | ||||
|     MPI_VALIDATE( X != NULL ); | ||||
| 
 | ||||
|     X->s = 1; | ||||
|     X->n = 0; | ||||
|  | @ -116,6 +120,7 @@ void mbedtls_mpi_free( mbedtls_mpi *X ) | |||
| int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs ) | ||||
| { | ||||
|     mbedtls_mpi_uint *p; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
| 
 | ||||
|     if( nblimbs > MBEDTLS_MPI_MAX_LIMBS ) | ||||
|         return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); | ||||
|  | @ -147,6 +152,10 @@ int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs ) | |||
| { | ||||
|     mbedtls_mpi_uint *p; | ||||
|     size_t i; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
| 
 | ||||
|     if( nblimbs > MBEDTLS_MPI_MAX_LIMBS ) | ||||
|         return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); | ||||
| 
 | ||||
|     /* Actually resize up in this case */ | ||||
|     if( X->n <= nblimbs ) | ||||
|  | @ -183,6 +192,8 @@ int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y ) | |||
| { | ||||
|     int ret = 0; | ||||
|     size_t i; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( Y != NULL ); | ||||
| 
 | ||||
|     if( X == Y ) | ||||
|         return( 0 ); | ||||
|  | @ -222,6 +233,8 @@ cleanup: | |||
| void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y ) | ||||
| { | ||||
|     mbedtls_mpi T; | ||||
|     MPI_VALIDATE( X != NULL ); | ||||
|     MPI_VALIDATE( Y != NULL ); | ||||
| 
 | ||||
|     memcpy( &T,  X, sizeof( mbedtls_mpi ) ); | ||||
|     memcpy(  X,  Y, sizeof( mbedtls_mpi ) ); | ||||
|  | @ -237,6 +250,8 @@ int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned | |||
| { | ||||
|     int ret = 0; | ||||
|     size_t i; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( Y != NULL ); | ||||
| 
 | ||||
|     /* make sure assign is 0 or 1 in a time-constant manner */ | ||||
|     assign = (assign | (unsigned char)-assign) >> 7; | ||||
|  | @ -266,6 +281,8 @@ int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char sw | |||
|     int ret, s; | ||||
|     size_t i; | ||||
|     mbedtls_mpi_uint tmp; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( Y != NULL ); | ||||
| 
 | ||||
|     if( X == Y ) | ||||
|         return( 0 ); | ||||
|  | @ -298,6 +315,7 @@ cleanup: | |||
| int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z ) | ||||
| { | ||||
|     int ret; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
| 
 | ||||
|     MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) ); | ||||
|     memset( X->p, 0, X->n * ciL ); | ||||
|  | @ -315,6 +333,8 @@ cleanup: | |||
|  */ | ||||
| int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos ) | ||||
| { | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
| 
 | ||||
|     if( X->n * biL <= pos ) | ||||
|         return( 0 ); | ||||
| 
 | ||||
|  | @ -333,6 +353,7 @@ int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val ) | |||
|     int ret = 0; | ||||
|     size_t off = pos / biL; | ||||
|     size_t idx = pos % biL; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
| 
 | ||||
|     if( val != 0 && val != 1 ) | ||||
|         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); | ||||
|  | @ -359,6 +380,7 @@ cleanup: | |||
| size_t mbedtls_mpi_lsb( const mbedtls_mpi *X ) | ||||
| { | ||||
|     size_t i, j, count = 0; | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( X != NULL, 0 ); | ||||
| 
 | ||||
|     for( i = 0; i < X->n; i++ ) | ||||
|         for( j = 0; j < biL; j++, count++ ) | ||||
|  | @ -439,6 +461,8 @@ int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s ) | |||
|     size_t i, j, slen, n; | ||||
|     mbedtls_mpi_uint d; | ||||
|     mbedtls_mpi T; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( s != NULL ); | ||||
| 
 | ||||
|     if( radix < 2 || radix > 16 ) | ||||
|         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); | ||||
|  | @ -503,26 +527,38 @@ cleanup: | |||
| } | ||||
| 
 | ||||
| /*
 | ||||
|  * Helper to write the digits high-order first | ||||
|  * Helper to write the digits high-order first. | ||||
|  */ | ||||
| static int mpi_write_hlp( mbedtls_mpi *X, int radix, char **p ) | ||||
| static int mpi_write_hlp( mbedtls_mpi *X, int radix, | ||||
|                           char **p, const size_t buflen ) | ||||
| { | ||||
|     int ret; | ||||
|     mbedtls_mpi_uint r; | ||||
|     size_t length = 0; | ||||
|     char *p_end = *p + buflen; | ||||
| 
 | ||||
|     if( radix < 2 || radix > 16 ) | ||||
|         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); | ||||
|     do | ||||
|     { | ||||
|         if( length >= buflen ) | ||||
|         { | ||||
|             return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); | ||||
|         } | ||||
| 
 | ||||
|     MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) ); | ||||
|     MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) ); | ||||
|         MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) ); | ||||
|         MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) ); | ||||
|         /*
 | ||||
|          * Write the residue in the current position, as an ASCII character. | ||||
|          */ | ||||
|         if( r < 0xA ) | ||||
|             *(--p_end) = (char)( '0' + r ); | ||||
|         else | ||||
|             *(--p_end) = (char)( 'A' + ( r - 0xA ) ); | ||||
| 
 | ||||
|     if( mbedtls_mpi_cmp_int( X, 0 ) != 0 ) | ||||
|         MBEDTLS_MPI_CHK( mpi_write_hlp( X, radix, p ) ); | ||||
|         length++; | ||||
|     } while( mbedtls_mpi_cmp_int( X, 0 ) != 0 ); | ||||
| 
 | ||||
|     if( r < 10 ) | ||||
|         *(*p)++ = (char)( r + 0x30 ); | ||||
|     else | ||||
|         *(*p)++ = (char)( r + 0x37 ); | ||||
|     memmove( *p, p_end, length ); | ||||
|     *p += length; | ||||
| 
 | ||||
| cleanup: | ||||
| 
 | ||||
|  | @ -539,6 +575,9 @@ int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, | |||
|     size_t n; | ||||
|     char *p; | ||||
|     mbedtls_mpi T; | ||||
|     MPI_VALIDATE_RET( X    != NULL ); | ||||
|     MPI_VALIDATE_RET( olen != NULL ); | ||||
|     MPI_VALIDATE_RET( buflen == 0 || buf != NULL ); | ||||
| 
 | ||||
|     if( radix < 2 || radix > 16 ) | ||||
|         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); | ||||
|  | @ -592,7 +631,7 @@ int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, | |||
|         if( T.s == -1 ) | ||||
|             T.s = 1; | ||||
| 
 | ||||
|         MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p ) ); | ||||
|         MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) ); | ||||
|     } | ||||
| 
 | ||||
|     *p++ = '\0'; | ||||
|  | @ -620,6 +659,12 @@ int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin ) | |||
|      */ | ||||
|     char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ]; | ||||
| 
 | ||||
|     MPI_VALIDATE_RET( X   != NULL ); | ||||
|     MPI_VALIDATE_RET( fin != NULL ); | ||||
| 
 | ||||
|     if( radix < 2 || radix > 16 ) | ||||
|         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     memset( s, 0, sizeof( s ) ); | ||||
|     if( fgets( s, sizeof( s ) - 1, fin ) == NULL ) | ||||
|         return( MBEDTLS_ERR_MPI_FILE_IO_ERROR ); | ||||
|  | @ -651,6 +696,10 @@ int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE | |||
|      * newline characters and '\0' | ||||
|      */ | ||||
|     char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ]; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
| 
 | ||||
|     if( radix < 2 || radix > 16 ) | ||||
|         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     memset( s, 0, sizeof( s ) ); | ||||
| 
 | ||||
|  | @ -678,14 +727,104 @@ cleanup: | |||
| } | ||||
| #endif /* MBEDTLS_FS_IO */ | ||||
| 
 | ||||
| 
 | ||||
| /* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
 | ||||
|  * into the storage form used by mbedtls_mpi. */ | ||||
| 
 | ||||
| static mbedtls_mpi_uint mpi_uint_bigendian_to_host_c( mbedtls_mpi_uint x ) | ||||
| { | ||||
|     uint8_t i; | ||||
|     mbedtls_mpi_uint tmp = 0; | ||||
|     /* This works regardless of the endianness. */ | ||||
|     for( i = 0; i < ciL; i++, x >>= 8 ) | ||||
|         tmp |= ( x & 0xFF ) << ( ( ciL - 1 - i ) << 3 ); | ||||
|     return( tmp ); | ||||
| } | ||||
| 
 | ||||
| static mbedtls_mpi_uint mpi_uint_bigendian_to_host( mbedtls_mpi_uint x ) | ||||
| { | ||||
| #if defined(__BYTE_ORDER__) | ||||
| 
 | ||||
| /* Nothing to do on bigendian systems. */ | ||||
| #if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ ) | ||||
|     return( x ); | ||||
| #endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */ | ||||
| 
 | ||||
| #if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ) | ||||
| 
 | ||||
| /* For GCC and Clang, have builtins for byte swapping. */ | ||||
| #if defined(__GNUC__) && defined(__GNUC_PREREQ) | ||||
| #if __GNUC_PREREQ(4,3) | ||||
| #define have_bswap | ||||
| #endif | ||||
| #endif | ||||
| 
 | ||||
| #if defined(__clang__) && defined(__has_builtin) | ||||
| #if __has_builtin(__builtin_bswap32)  &&                 \ | ||||
|     __has_builtin(__builtin_bswap64) | ||||
| #define have_bswap | ||||
| #endif | ||||
| #endif | ||||
| 
 | ||||
| #if defined(have_bswap) | ||||
|     /* The compiler is hopefully able to statically evaluate this! */ | ||||
|     switch( sizeof(mbedtls_mpi_uint) ) | ||||
|     { | ||||
|         case 4: | ||||
|             return( __builtin_bswap32(x) ); | ||||
|         case 8: | ||||
|             return( __builtin_bswap64(x) ); | ||||
|     } | ||||
| #endif | ||||
| #endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */ | ||||
| #endif /* __BYTE_ORDER__ */ | ||||
| 
 | ||||
|     /* Fall back to C-based reordering if we don't know the byte order
 | ||||
|      * or we couldn't use a compiler-specific builtin. */ | ||||
|     return( mpi_uint_bigendian_to_host_c( x ) ); | ||||
| } | ||||
| 
 | ||||
| static void mpi_bigendian_to_host( mbedtls_mpi_uint * const p, size_t limbs ) | ||||
| { | ||||
|     mbedtls_mpi_uint *cur_limb_left; | ||||
|     mbedtls_mpi_uint *cur_limb_right; | ||||
|     if( limbs == 0 ) | ||||
|         return; | ||||
| 
 | ||||
|     /*
 | ||||
|      * Traverse limbs and | ||||
|      * - adapt byte-order in each limb | ||||
|      * - swap the limbs themselves. | ||||
|      * For that, simultaneously traverse the limbs from left to right | ||||
|      * and from right to left, as long as the left index is not bigger | ||||
|      * than the right index (it's not a problem if limbs is odd and the | ||||
|      * indices coincide in the last iteration). | ||||
|      */ | ||||
|     for( cur_limb_left = p, cur_limb_right = p + ( limbs - 1 ); | ||||
|          cur_limb_left <= cur_limb_right; | ||||
|          cur_limb_left++, cur_limb_right-- ) | ||||
|     { | ||||
|         mbedtls_mpi_uint tmp; | ||||
|         /* Note that if cur_limb_left == cur_limb_right,
 | ||||
|          * this code effectively swaps the bytes only once. */ | ||||
|         tmp             = mpi_uint_bigendian_to_host( *cur_limb_left  ); | ||||
|         *cur_limb_left  = mpi_uint_bigendian_to_host( *cur_limb_right ); | ||||
|         *cur_limb_right = tmp; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| /*
 | ||||
|  * Import X from unsigned binary data, big endian | ||||
|  */ | ||||
| int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen ) | ||||
| { | ||||
|     int ret; | ||||
|     size_t i, j; | ||||
|     size_t const limbs = CHARS_TO_LIMBS( buflen ); | ||||
|     size_t const limbs    = CHARS_TO_LIMBS( buflen ); | ||||
|     size_t const overhead = ( limbs * ciL ) - buflen; | ||||
|     unsigned char *Xp; | ||||
| 
 | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( buflen == 0 || buf != NULL ); | ||||
| 
 | ||||
|     /* Ensure that target MPI has exactly the necessary number of limbs */ | ||||
|     if( X->n != limbs ) | ||||
|  | @ -694,11 +833,17 @@ int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t bu | |||
|         mbedtls_mpi_init( X ); | ||||
|         MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) ); | ||||
|     } | ||||
| 
 | ||||
|     MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) ); | ||||
| 
 | ||||
|     for( i = buflen, j = 0; i > 0; i--, j++ ) | ||||
|         X->p[j / ciL] |= ((mbedtls_mpi_uint) buf[i - 1]) << ((j % ciL) << 3); | ||||
|     /* Avoid calling `memcpy` with NULL source argument,
 | ||||
|      * even if buflen is 0. */ | ||||
|     if( buf != NULL ) | ||||
|     { | ||||
|         Xp = (unsigned char*) X->p; | ||||
|         memcpy( Xp + overhead, buf, buflen ); | ||||
| 
 | ||||
|         mpi_bigendian_to_host( X->p, limbs ); | ||||
|     } | ||||
| 
 | ||||
| cleanup: | ||||
| 
 | ||||
|  | @ -711,11 +856,16 @@ cleanup: | |||
| int mbedtls_mpi_write_binary( const mbedtls_mpi *X, | ||||
|                               unsigned char *buf, size_t buflen ) | ||||
| { | ||||
|     size_t stored_bytes = X->n * ciL; | ||||
|     size_t stored_bytes; | ||||
|     size_t bytes_to_copy; | ||||
|     unsigned char *p; | ||||
|     size_t i; | ||||
| 
 | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( buflen == 0 || buf != NULL ); | ||||
| 
 | ||||
|     stored_bytes = X->n * ciL; | ||||
| 
 | ||||
|     if( stored_bytes < buflen ) | ||||
|     { | ||||
|         /* There is enough space in the output buffer. Write initial
 | ||||
|  | @ -754,6 +904,7 @@ int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count ) | |||
|     int ret; | ||||
|     size_t i, v0, t1; | ||||
|     mbedtls_mpi_uint r0 = 0, r1; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
| 
 | ||||
|     v0 = count / (biL    ); | ||||
|     t1 = count & (biL - 1); | ||||
|  | @ -803,6 +954,7 @@ int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count ) | |||
| { | ||||
|     size_t i, v0, v1; | ||||
|     mbedtls_mpi_uint r0 = 0, r1; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
| 
 | ||||
|     v0 = count /  biL; | ||||
|     v1 = count & (biL - 1); | ||||
|  | @ -845,6 +997,8 @@ int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count ) | |||
| int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y ) | ||||
| { | ||||
|     size_t i, j; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( Y != NULL ); | ||||
| 
 | ||||
|     for( i = X->n; i > 0; i-- ) | ||||
|         if( X->p[i - 1] != 0 ) | ||||
|  | @ -875,6 +1029,8 @@ int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y ) | |||
| int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y ) | ||||
| { | ||||
|     size_t i, j; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( Y != NULL ); | ||||
| 
 | ||||
|     for( i = X->n; i > 0; i-- ) | ||||
|         if( X->p[i - 1] != 0 ) | ||||
|  | @ -909,6 +1065,7 @@ int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z ) | |||
| { | ||||
|     mbedtls_mpi Y; | ||||
|     mbedtls_mpi_uint p[1]; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
| 
 | ||||
|     *p  = ( z < 0 ) ? -z : z; | ||||
|     Y.s = ( z < 0 ) ? -1 : 1; | ||||
|  | @ -926,6 +1083,9 @@ int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi | |||
|     int ret; | ||||
|     size_t i, j; | ||||
|     mbedtls_mpi_uint *o, *p, c, tmp; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( A != NULL ); | ||||
|     MPI_VALIDATE_RET( B != NULL ); | ||||
| 
 | ||||
|     if( X == B ) | ||||
|     { | ||||
|  | @ -1003,6 +1163,9 @@ int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi | |||
|     mbedtls_mpi TB; | ||||
|     int ret; | ||||
|     size_t n; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( A != NULL ); | ||||
|     MPI_VALIDATE_RET( B != NULL ); | ||||
| 
 | ||||
|     if( mbedtls_mpi_cmp_abs( A, B ) < 0 ) | ||||
|         return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE ); | ||||
|  | @ -1043,8 +1206,12 @@ cleanup: | |||
|  */ | ||||
| int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) | ||||
| { | ||||
|     int ret, s = A->s; | ||||
|     int ret, s; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( A != NULL ); | ||||
|     MPI_VALIDATE_RET( B != NULL ); | ||||
| 
 | ||||
|     s = A->s; | ||||
|     if( A->s * B->s < 0 ) | ||||
|     { | ||||
|         if( mbedtls_mpi_cmp_abs( A, B ) >= 0 ) | ||||
|  | @ -1074,8 +1241,12 @@ cleanup: | |||
|  */ | ||||
| int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) | ||||
| { | ||||
|     int ret, s = A->s; | ||||
|     int ret, s; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( A != NULL ); | ||||
|     MPI_VALIDATE_RET( B != NULL ); | ||||
| 
 | ||||
|     s = A->s; | ||||
|     if( A->s * B->s > 0 ) | ||||
|     { | ||||
|         if( mbedtls_mpi_cmp_abs( A, B ) >= 0 ) | ||||
|  | @ -1107,6 +1278,8 @@ int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint | |||
| { | ||||
|     mbedtls_mpi _B; | ||||
|     mbedtls_mpi_uint p[1]; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( A != NULL ); | ||||
| 
 | ||||
|     p[0] = ( b < 0 ) ? -b : b; | ||||
|     _B.s = ( b < 0 ) ? -1 : 1; | ||||
|  | @ -1123,6 +1296,8 @@ int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint | |||
| { | ||||
|     mbedtls_mpi _B; | ||||
|     mbedtls_mpi_uint p[1]; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( A != NULL ); | ||||
| 
 | ||||
|     p[0] = ( b < 0 ) ? -b : b; | ||||
|     _B.s = ( b < 0 ) ? -1 : 1; | ||||
|  | @ -1212,6 +1387,9 @@ int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi | |||
|     int ret; | ||||
|     size_t i, j; | ||||
|     mbedtls_mpi TA, TB; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( A != NULL ); | ||||
|     MPI_VALIDATE_RET( B != NULL ); | ||||
| 
 | ||||
|     mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB ); | ||||
| 
 | ||||
|  | @ -1248,6 +1426,8 @@ int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint | |||
| { | ||||
|     mbedtls_mpi _B; | ||||
|     mbedtls_mpi_uint p[1]; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( A != NULL ); | ||||
| 
 | ||||
|     _B.s = 1; | ||||
|     _B.n = 1; | ||||
|  | @ -1356,11 +1536,14 @@ static mbedtls_mpi_uint mbedtls_int_div_int( mbedtls_mpi_uint u1, | |||
| /*
 | ||||
|  * Division by mbedtls_mpi: A = Q * B + R  (HAC 14.20) | ||||
|  */ | ||||
| int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B ) | ||||
| int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, | ||||
|                          const mbedtls_mpi *B ) | ||||
| { | ||||
|     int ret; | ||||
|     size_t i, n, t, k; | ||||
|     mbedtls_mpi X, Y, Z, T1, T2; | ||||
|     MPI_VALIDATE_RET( A != NULL ); | ||||
|     MPI_VALIDATE_RET( B != NULL ); | ||||
| 
 | ||||
|     if( mbedtls_mpi_cmp_int( B, 0 ) == 0 ) | ||||
|         return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO ); | ||||
|  | @ -1471,10 +1654,13 @@ cleanup: | |||
| /*
 | ||||
|  * Division by int: A = Q * b + R | ||||
|  */ | ||||
| int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b ) | ||||
| int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, | ||||
|                          const mbedtls_mpi *A, | ||||
|                          mbedtls_mpi_sint b ) | ||||
| { | ||||
|     mbedtls_mpi _B; | ||||
|     mbedtls_mpi_uint p[1]; | ||||
|     MPI_VALIDATE_RET( A != NULL ); | ||||
| 
 | ||||
|     p[0] = ( b < 0 ) ? -b : b; | ||||
|     _B.s = ( b < 0 ) ? -1 : 1; | ||||
|  | @ -1490,6 +1676,9 @@ int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, m | |||
| int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B ) | ||||
| { | ||||
|     int ret; | ||||
|     MPI_VALIDATE_RET( R != NULL ); | ||||
|     MPI_VALIDATE_RET( A != NULL ); | ||||
|     MPI_VALIDATE_RET( B != NULL ); | ||||
| 
 | ||||
|     if( mbedtls_mpi_cmp_int( B, 0 ) < 0 ) | ||||
|         return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE ); | ||||
|  | @ -1514,6 +1703,8 @@ int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_ | |||
| { | ||||
|     size_t i; | ||||
|     mbedtls_mpi_uint x, y, z; | ||||
|     MPI_VALIDATE_RET( r != NULL ); | ||||
|     MPI_VALIDATE_RET( A != NULL ); | ||||
| 
 | ||||
|     if( b == 0 ) | ||||
|         return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO ); | ||||
|  | @ -1627,7 +1818,8 @@ static int mpi_montmul( mbedtls_mpi *A, const mbedtls_mpi *B, const mbedtls_mpi | |||
| /*
 | ||||
|  * Montgomery reduction: A = A * R^-1 mod N | ||||
|  */ | ||||
| static int mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N, mbedtls_mpi_uint mm, const mbedtls_mpi *T ) | ||||
| static int mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N, | ||||
|                         mbedtls_mpi_uint mm, const mbedtls_mpi *T ) | ||||
| { | ||||
|     mbedtls_mpi_uint z = 1; | ||||
|     mbedtls_mpi U; | ||||
|  | @ -1641,7 +1833,9 @@ static int mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N, mbedtls_mpi_uint m | |||
| /*
 | ||||
|  * Sliding-window exponentiation: X = A^E mod N  (HAC 14.85) | ||||
|  */ | ||||
| int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR ) | ||||
| int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, | ||||
|                          const mbedtls_mpi *E, const mbedtls_mpi *N, | ||||
|                          mbedtls_mpi *_RR ) | ||||
| { | ||||
|     int ret; | ||||
|     size_t wbits, wsize, one = 1; | ||||
|  | @ -1651,6 +1845,11 @@ int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi | |||
|     mbedtls_mpi RR, T, W[ 2 << MBEDTLS_MPI_WINDOW_SIZE ], Apos; | ||||
|     int neg; | ||||
| 
 | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( A != NULL ); | ||||
|     MPI_VALIDATE_RET( E != NULL ); | ||||
|     MPI_VALIDATE_RET( N != NULL ); | ||||
| 
 | ||||
|     if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || ( N->p[0] & 1 ) == 0 ) | ||||
|         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|  | @ -1855,6 +2054,10 @@ int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B | |||
|     size_t lz, lzt; | ||||
|     mbedtls_mpi TG, TA, TB; | ||||
| 
 | ||||
|     MPI_VALIDATE_RET( G != NULL ); | ||||
|     MPI_VALIDATE_RET( A != NULL ); | ||||
|     MPI_VALIDATE_RET( B != NULL ); | ||||
| 
 | ||||
|     mbedtls_mpi_init( &TG ); mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB ); | ||||
| 
 | ||||
|     MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); | ||||
|  | @ -1910,16 +2113,28 @@ int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size, | |||
|                      void *p_rng ) | ||||
| { | ||||
|     int ret; | ||||
|     unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; | ||||
|     size_t const limbs = CHARS_TO_LIMBS( size ); | ||||
|     size_t const overhead = ( limbs * ciL ) - size; | ||||
|     unsigned char *Xp; | ||||
| 
 | ||||
|     if( size > MBEDTLS_MPI_MAX_SIZE ) | ||||
|         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); | ||||
|     MPI_VALIDATE_RET( X     != NULL ); | ||||
|     MPI_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
|     MBEDTLS_MPI_CHK( f_rng( p_rng, buf, size ) ); | ||||
|     MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( X, buf, size ) ); | ||||
|     /* Ensure that target MPI has exactly the necessary number of limbs */ | ||||
|     if( X->n != limbs ) | ||||
|     { | ||||
|         mbedtls_mpi_free( X ); | ||||
|         mbedtls_mpi_init( X ); | ||||
|         MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, limbs ) ); | ||||
|     } | ||||
|     MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) ); | ||||
| 
 | ||||
|     Xp = (unsigned char*) X->p; | ||||
|     f_rng( p_rng, Xp + overhead, size ); | ||||
| 
 | ||||
|     mpi_bigendian_to_host( X->p, limbs ); | ||||
| 
 | ||||
| cleanup: | ||||
|     mbedtls_platform_zeroize( buf, sizeof( buf ) ); | ||||
|     return( ret ); | ||||
| } | ||||
| 
 | ||||
|  | @ -1930,6 +2145,9 @@ int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi | |||
| { | ||||
|     int ret; | ||||
|     mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2; | ||||
|     MPI_VALIDATE_RET( X != NULL ); | ||||
|     MPI_VALIDATE_RET( A != NULL ); | ||||
|     MPI_VALIDATE_RET( N != NULL ); | ||||
| 
 | ||||
|     if( mbedtls_mpi_cmp_int( N, 1 ) <= 0 ) | ||||
|         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); | ||||
|  | @ -2089,7 +2307,11 @@ static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds, | |||
|     size_t i, j, k, s; | ||||
|     mbedtls_mpi W, R, T, A, RR; | ||||
| 
 | ||||
|     mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A ); | ||||
|     MPI_VALIDATE_RET( X     != NULL ); | ||||
|     MPI_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
|     mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R ); | ||||
|     mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A ); | ||||
|     mbedtls_mpi_init( &RR ); | ||||
| 
 | ||||
|     /*
 | ||||
|  | @ -2161,7 +2383,8 @@ static int mpi_miller_rabin( const mbedtls_mpi *X, size_t rounds, | |||
|     } | ||||
| 
 | ||||
| cleanup: | ||||
|     mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A ); | ||||
|     mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R ); | ||||
|     mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A ); | ||||
|     mbedtls_mpi_free( &RR ); | ||||
| 
 | ||||
|     return( ret ); | ||||
|  | @ -2176,6 +2399,8 @@ int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds, | |||
| { | ||||
|     int ret; | ||||
|     mbedtls_mpi XX; | ||||
|     MPI_VALIDATE_RET( X     != NULL ); | ||||
|     MPI_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
|     XX.s = 1; | ||||
|     XX.n = X->n; | ||||
|  | @ -2207,12 +2432,15 @@ int mbedtls_mpi_is_prime( const mbedtls_mpi *X, | |||
|                   int (*f_rng)(void *, unsigned char *, size_t), | ||||
|                   void *p_rng ) | ||||
| { | ||||
|     MPI_VALIDATE_RET( X     != NULL ); | ||||
|     MPI_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
|     /*
 | ||||
|      * In the past our key generation aimed for an error rate of at most | ||||
|      * 2^-80. Since this function is deprecated, aim for the same certainty | ||||
|      * here as well. | ||||
|      */ | ||||
|     return mbedtls_mpi_is_prime_ext( X, 40, f_rng, p_rng ); | ||||
|     return( mbedtls_mpi_is_prime_ext( X, 40, f_rng, p_rng ) ); | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | @ -2240,6 +2468,9 @@ int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags, | |||
|     mbedtls_mpi_uint r; | ||||
|     mbedtls_mpi Y; | ||||
| 
 | ||||
|     MPI_VALIDATE_RET( X     != NULL ); | ||||
|     MPI_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
|     if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS ) | ||||
|         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|  |  | |||
|  | @ -40,6 +40,12 @@ | |||
| 
 | ||||
| #if !defined(MBEDTLS_BLOWFISH_ALT) | ||||
| 
 | ||||
| /* Parameter validation macros */ | ||||
| #define BLOWFISH_VALIDATE_RET( cond )                                       \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA ) | ||||
| #define BLOWFISH_VALIDATE( cond )                                           \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| /*
 | ||||
|  * 32-bit integer manipulation macros (big endian) | ||||
|  */ | ||||
|  | @ -153,6 +159,7 @@ static void blowfish_dec( mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t | |||
| 
 | ||||
| void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx ) | ||||
| { | ||||
|     BLOWFISH_VALIDATE( ctx != NULL ); | ||||
|     memset( ctx, 0, sizeof( mbedtls_blowfish_context ) ); | ||||
| } | ||||
| 
 | ||||
|  | @ -167,16 +174,20 @@ void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx ) | |||
| /*
 | ||||
|  * Blowfish key schedule | ||||
|  */ | ||||
| int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key, | ||||
|                      unsigned int keybits ) | ||||
| int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, | ||||
|                              const unsigned char *key, | ||||
|                              unsigned int keybits ) | ||||
| { | ||||
|     unsigned int i, j, k; | ||||
|     uint32_t data, datal, datar; | ||||
|     BLOWFISH_VALIDATE_RET( ctx != NULL ); | ||||
|     BLOWFISH_VALIDATE_RET( key != NULL ); | ||||
| 
 | ||||
|     if( keybits < MBEDTLS_BLOWFISH_MIN_KEY_BITS || keybits > MBEDTLS_BLOWFISH_MAX_KEY_BITS || | ||||
|         ( keybits % 8 ) ) | ||||
|     if( keybits < MBEDTLS_BLOWFISH_MIN_KEY_BITS    || | ||||
|         keybits > MBEDTLS_BLOWFISH_MAX_KEY_BITS    || | ||||
|         keybits % 8 != 0 ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH ); | ||||
|         return( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA ); | ||||
|     } | ||||
| 
 | ||||
|     keybits >>= 3; | ||||
|  | @ -231,6 +242,11 @@ int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx, | |||
|                     unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] ) | ||||
| { | ||||
|     uint32_t X0, X1; | ||||
|     BLOWFISH_VALIDATE_RET( ctx != NULL ); | ||||
|     BLOWFISH_VALIDATE_RET( mode == MBEDTLS_BLOWFISH_ENCRYPT || | ||||
|                            mode == MBEDTLS_BLOWFISH_DECRYPT ); | ||||
|     BLOWFISH_VALIDATE_RET( input  != NULL ); | ||||
|     BLOWFISH_VALIDATE_RET( output != NULL ); | ||||
| 
 | ||||
|     GET_UINT32_BE( X0, input,  0 ); | ||||
|     GET_UINT32_BE( X1, input,  4 ); | ||||
|  | @ -263,6 +279,12 @@ int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx, | |||
| { | ||||
|     int i; | ||||
|     unsigned char temp[MBEDTLS_BLOWFISH_BLOCKSIZE]; | ||||
|     BLOWFISH_VALIDATE_RET( ctx != NULL ); | ||||
|     BLOWFISH_VALIDATE_RET( mode == MBEDTLS_BLOWFISH_ENCRYPT || | ||||
|                            mode == MBEDTLS_BLOWFISH_DECRYPT ); | ||||
|     BLOWFISH_VALIDATE_RET( iv != NULL ); | ||||
|     BLOWFISH_VALIDATE_RET( length == 0 || input  != NULL ); | ||||
|     BLOWFISH_VALIDATE_RET( length == 0 || output != NULL ); | ||||
| 
 | ||||
|     if( length % MBEDTLS_BLOWFISH_BLOCKSIZE ) | ||||
|         return( MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH ); | ||||
|  | @ -317,7 +339,19 @@ int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx, | |||
|                        unsigned char *output ) | ||||
| { | ||||
|     int c; | ||||
|     size_t n = *iv_off; | ||||
|     size_t n; | ||||
| 
 | ||||
|     BLOWFISH_VALIDATE_RET( ctx != NULL ); | ||||
|     BLOWFISH_VALIDATE_RET( mode == MBEDTLS_BLOWFISH_ENCRYPT || | ||||
|                            mode == MBEDTLS_BLOWFISH_DECRYPT ); | ||||
|     BLOWFISH_VALIDATE_RET( iv     != NULL ); | ||||
|     BLOWFISH_VALIDATE_RET( iv_off != NULL ); | ||||
|     BLOWFISH_VALIDATE_RET( length == 0 || input  != NULL ); | ||||
|     BLOWFISH_VALIDATE_RET( length == 0 || output != NULL ); | ||||
| 
 | ||||
|     n = *iv_off; | ||||
|     if( n >= 8 ) | ||||
|         return( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( mode == MBEDTLS_BLOWFISH_DECRYPT ) | ||||
|     { | ||||
|  | @ -365,7 +399,17 @@ int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx, | |||
|                        unsigned char *output ) | ||||
| { | ||||
|     int c, i; | ||||
|     size_t n = *nc_off; | ||||
|     size_t n; | ||||
|     BLOWFISH_VALIDATE_RET( ctx != NULL ); | ||||
|     BLOWFISH_VALIDATE_RET( nonce_counter != NULL ); | ||||
|     BLOWFISH_VALIDATE_RET( stream_block  != NULL ); | ||||
|     BLOWFISH_VALIDATE_RET( nc_off != NULL ); | ||||
|     BLOWFISH_VALIDATE_RET( length == 0 || input  != NULL ); | ||||
|     BLOWFISH_VALIDATE_RET( length == 0 || output != NULL ); | ||||
| 
 | ||||
|     n = *nc_off; | ||||
|     if( n >= 8 ) | ||||
|         return( MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     while( length-- ) | ||||
|     { | ||||
|  |  | |||
|  | @ -49,6 +49,12 @@ | |||
| 
 | ||||
| #if !defined(MBEDTLS_CAMELLIA_ALT) | ||||
| 
 | ||||
| /* Parameter validation macros */ | ||||
| #define CAMELLIA_VALIDATE_RET( cond )                                       \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA ) | ||||
| #define CAMELLIA_VALIDATE( cond )                                           \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| /*
 | ||||
|  * 32-bit integer manipulation macros (big endian) | ||||
|  */ | ||||
|  | @ -321,6 +327,7 @@ static void camellia_feistel( const uint32_t x[2], const uint32_t k[2], | |||
| 
 | ||||
| void mbedtls_camellia_init( mbedtls_camellia_context *ctx ) | ||||
| { | ||||
|     CAMELLIA_VALIDATE( ctx != NULL ); | ||||
|     memset( ctx, 0, sizeof( mbedtls_camellia_context ) ); | ||||
| } | ||||
| 
 | ||||
|  | @ -335,8 +342,9 @@ void mbedtls_camellia_free( mbedtls_camellia_context *ctx ) | |||
| /*
 | ||||
|  * Camellia key schedule (encryption) | ||||
|  */ | ||||
| int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key, | ||||
|                          unsigned int keybits ) | ||||
| int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, | ||||
|                                  const unsigned char *key, | ||||
|                                  unsigned int keybits ) | ||||
| { | ||||
|     int idx; | ||||
|     size_t i; | ||||
|  | @ -346,6 +354,9 @@ int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned c | |||
|     uint32_t KC[16]; | ||||
|     uint32_t TK[20]; | ||||
| 
 | ||||
|     CAMELLIA_VALIDATE_RET( ctx != NULL ); | ||||
|     CAMELLIA_VALIDATE_RET( key != NULL ); | ||||
| 
 | ||||
|     RK = ctx->rk; | ||||
| 
 | ||||
|     memset( t, 0, 64 ); | ||||
|  | @ -356,7 +367,7 @@ int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned c | |||
|         case 128: ctx->nr = 3; idx = 0; break; | ||||
|         case 192: | ||||
|         case 256: ctx->nr = 4; idx = 1; break; | ||||
|         default : return( MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH ); | ||||
|         default : return( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA ); | ||||
|     } | ||||
| 
 | ||||
|     for( i = 0; i < keybits / 8; ++i ) | ||||
|  | @ -440,14 +451,17 @@ int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned c | |||
| /*
 | ||||
|  * Camellia key schedule (decryption) | ||||
|  */ | ||||
| int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key, | ||||
|                          unsigned int keybits ) | ||||
| int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, | ||||
|                                  const unsigned char *key, | ||||
|                                  unsigned int keybits ) | ||||
| { | ||||
|     int idx, ret; | ||||
|     size_t i; | ||||
|     mbedtls_camellia_context cty; | ||||
|     uint32_t *RK; | ||||
|     uint32_t *SK; | ||||
|     CAMELLIA_VALIDATE_RET( ctx != NULL ); | ||||
|     CAMELLIA_VALIDATE_RET( key != NULL ); | ||||
| 
 | ||||
|     mbedtls_camellia_init( &cty ); | ||||
| 
 | ||||
|  | @ -495,6 +509,11 @@ int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx, | |||
| { | ||||
|     int NR; | ||||
|     uint32_t *RK, X[4]; | ||||
|     CAMELLIA_VALIDATE_RET( ctx != NULL ); | ||||
|     CAMELLIA_VALIDATE_RET( mode == MBEDTLS_CAMELLIA_ENCRYPT || | ||||
|                            mode == MBEDTLS_CAMELLIA_DECRYPT ); | ||||
|     CAMELLIA_VALIDATE_RET( input  != NULL ); | ||||
|     CAMELLIA_VALIDATE_RET( output != NULL ); | ||||
| 
 | ||||
|     ( (void) mode ); | ||||
| 
 | ||||
|  | @ -552,14 +571,20 @@ int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx, | |||
|  * Camellia-CBC buffer encryption/decryption | ||||
|  */ | ||||
| int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx, | ||||
|                     int mode, | ||||
|                     size_t length, | ||||
|                     unsigned char iv[16], | ||||
|                     const unsigned char *input, | ||||
|                     unsigned char *output ) | ||||
|                                 int mode, | ||||
|                                 size_t length, | ||||
|                                 unsigned char iv[16], | ||||
|                                 const unsigned char *input, | ||||
|                                 unsigned char *output ) | ||||
| { | ||||
|     int i; | ||||
|     unsigned char temp[16]; | ||||
|     CAMELLIA_VALIDATE_RET( ctx != NULL ); | ||||
|     CAMELLIA_VALIDATE_RET( mode == MBEDTLS_CAMELLIA_ENCRYPT || | ||||
|                            mode == MBEDTLS_CAMELLIA_DECRYPT ); | ||||
|     CAMELLIA_VALIDATE_RET( iv != NULL ); | ||||
|     CAMELLIA_VALIDATE_RET( length == 0 || input  != NULL ); | ||||
|     CAMELLIA_VALIDATE_RET( length == 0 || output != NULL ); | ||||
| 
 | ||||
|     if( length % 16 ) | ||||
|         return( MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH ); | ||||
|  | @ -614,7 +639,18 @@ int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx, | |||
|                        unsigned char *output ) | ||||
| { | ||||
|     int c; | ||||
|     size_t n = *iv_off; | ||||
|     size_t n; | ||||
|     CAMELLIA_VALIDATE_RET( ctx != NULL ); | ||||
|     CAMELLIA_VALIDATE_RET( mode == MBEDTLS_CAMELLIA_ENCRYPT || | ||||
|                            mode == MBEDTLS_CAMELLIA_DECRYPT ); | ||||
|     CAMELLIA_VALIDATE_RET( iv     != NULL ); | ||||
|     CAMELLIA_VALIDATE_RET( iv_off != NULL ); | ||||
|     CAMELLIA_VALIDATE_RET( length == 0 || input  != NULL ); | ||||
|     CAMELLIA_VALIDATE_RET( length == 0 || output != NULL ); | ||||
| 
 | ||||
|     n = *iv_off; | ||||
|     if( n >= 16 ) | ||||
|         return( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( mode == MBEDTLS_CAMELLIA_DECRYPT ) | ||||
|     { | ||||
|  | @ -662,7 +698,17 @@ int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx, | |||
|                        unsigned char *output ) | ||||
| { | ||||
|     int c, i; | ||||
|     size_t n = *nc_off; | ||||
|     size_t n; | ||||
|     CAMELLIA_VALIDATE_RET( ctx != NULL ); | ||||
|     CAMELLIA_VALIDATE_RET( nonce_counter != NULL ); | ||||
|     CAMELLIA_VALIDATE_RET( stream_block  != NULL ); | ||||
|     CAMELLIA_VALIDATE_RET( nc_off != NULL ); | ||||
|     CAMELLIA_VALIDATE_RET( length == 0 || input  != NULL ); | ||||
|     CAMELLIA_VALIDATE_RET( length == 0 || output != NULL ); | ||||
| 
 | ||||
|     n = *nc_off; | ||||
|     if( n >= 16 ) | ||||
|         return( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     while( length-- ) | ||||
|     { | ||||
|  |  | |||
|  | @ -52,6 +52,11 @@ | |||
| 
 | ||||
| #if !defined(MBEDTLS_CCM_ALT) | ||||
| 
 | ||||
| #define CCM_VALIDATE_RET( cond ) \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CCM_BAD_INPUT ) | ||||
| #define CCM_VALIDATE( cond ) \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| #define CCM_ENCRYPT 0 | ||||
| #define CCM_DECRYPT 1 | ||||
| 
 | ||||
|  | @ -60,6 +65,7 @@ | |||
|  */ | ||||
| void mbedtls_ccm_init( mbedtls_ccm_context *ctx ) | ||||
| { | ||||
|     CCM_VALIDATE( ctx != NULL ); | ||||
|     memset( ctx, 0, sizeof( mbedtls_ccm_context ) ); | ||||
| } | ||||
| 
 | ||||
|  | @ -71,6 +77,9 @@ int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx, | |||
|     int ret; | ||||
|     const mbedtls_cipher_info_t *cipher_info; | ||||
| 
 | ||||
|     CCM_VALIDATE_RET( ctx != NULL ); | ||||
|     CCM_VALIDATE_RET( key != NULL ); | ||||
| 
 | ||||
|     cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, MBEDTLS_MODE_ECB ); | ||||
|     if( cipher_info == NULL ) | ||||
|         return( MBEDTLS_ERR_CCM_BAD_INPUT ); | ||||
|  | @ -97,6 +106,8 @@ int mbedtls_ccm_setkey( mbedtls_ccm_context *ctx, | |||
|  */ | ||||
| void mbedtls_ccm_free( mbedtls_ccm_context *ctx ) | ||||
| { | ||||
|     if( ctx == NULL ) | ||||
|         return; | ||||
|     mbedtls_cipher_free( &ctx->cipher_ctx ); | ||||
|     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ccm_context ) ); | ||||
| } | ||||
|  | @ -310,6 +321,12 @@ int mbedtls_ccm_star_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, | |||
|                          const unsigned char *input, unsigned char *output, | ||||
|                          unsigned char *tag, size_t tag_len ) | ||||
| { | ||||
|     CCM_VALIDATE_RET( ctx != NULL ); | ||||
|     CCM_VALIDATE_RET( iv != NULL ); | ||||
|     CCM_VALIDATE_RET( add_len == 0 || add != NULL ); | ||||
|     CCM_VALIDATE_RET( length == 0 || input != NULL ); | ||||
|     CCM_VALIDATE_RET( length == 0 || output != NULL ); | ||||
|     CCM_VALIDATE_RET( tag_len == 0 || tag != NULL ); | ||||
|     return( ccm_auth_crypt( ctx, CCM_ENCRYPT, length, iv, iv_len, | ||||
|                             add, add_len, input, output, tag, tag_len ) ); | ||||
| } | ||||
|  | @ -320,6 +337,12 @@ int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length, | |||
|                          const unsigned char *input, unsigned char *output, | ||||
|                          unsigned char *tag, size_t tag_len ) | ||||
| { | ||||
|     CCM_VALIDATE_RET( ctx != NULL ); | ||||
|     CCM_VALIDATE_RET( iv != NULL ); | ||||
|     CCM_VALIDATE_RET( add_len == 0 || add != NULL ); | ||||
|     CCM_VALIDATE_RET( length == 0 || input != NULL ); | ||||
|     CCM_VALIDATE_RET( length == 0 || output != NULL ); | ||||
|     CCM_VALIDATE_RET( tag_len == 0 || tag != NULL ); | ||||
|     if( tag_len == 0 ) | ||||
|         return( MBEDTLS_ERR_CCM_BAD_INPUT ); | ||||
| 
 | ||||
|  | @ -341,6 +364,13 @@ int mbedtls_ccm_star_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, | |||
|     unsigned char i; | ||||
|     int diff; | ||||
| 
 | ||||
|     CCM_VALIDATE_RET( ctx != NULL ); | ||||
|     CCM_VALIDATE_RET( iv != NULL ); | ||||
|     CCM_VALIDATE_RET( add_len == 0 || add != NULL ); | ||||
|     CCM_VALIDATE_RET( length == 0 || input != NULL ); | ||||
|     CCM_VALIDATE_RET( length == 0 || output != NULL ); | ||||
|     CCM_VALIDATE_RET( tag_len == 0 || tag != NULL ); | ||||
| 
 | ||||
|     if( ( ret = ccm_auth_crypt( ctx, CCM_DECRYPT, length, | ||||
|                                 iv, iv_len, add, add_len, | ||||
|                                 input, output, check_tag, tag_len ) ) != 0 ) | ||||
|  | @ -367,6 +397,13 @@ int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length, | |||
|                       const unsigned char *input, unsigned char *output, | ||||
|                       const unsigned char *tag, size_t tag_len ) | ||||
| { | ||||
|     CCM_VALIDATE_RET( ctx != NULL ); | ||||
|     CCM_VALIDATE_RET( iv != NULL ); | ||||
|     CCM_VALIDATE_RET( add_len == 0 || add != NULL ); | ||||
|     CCM_VALIDATE_RET( length == 0 || input != NULL ); | ||||
|     CCM_VALIDATE_RET( length == 0 || output != NULL ); | ||||
|     CCM_VALIDATE_RET( tag_len == 0 || tag != NULL ); | ||||
| 
 | ||||
|     if( tag_len == 0 ) | ||||
|         return( MBEDTLS_ERR_CCM_BAD_INPUT ); | ||||
| 
 | ||||
|  |  | |||
|  | @ -53,6 +53,12 @@ | |||
| #define inline __inline | ||||
| #endif | ||||
| 
 | ||||
| /* Parameter validation macros */ | ||||
| #define CHACHA20_VALIDATE_RET( cond )                                       \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA ) | ||||
| #define CHACHA20_VALIDATE( cond )                                           \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| #define BYTES_TO_U32_LE( data, offset )                           \ | ||||
|     ( (uint32_t) data[offset]                                     \ | ||||
|           | (uint32_t) ( (uint32_t) data[( offset ) + 1] << 8 )   \ | ||||
|  | @ -181,14 +187,13 @@ static void chacha20_block( const uint32_t initial_state[16], | |||
| 
 | ||||
| void mbedtls_chacha20_init( mbedtls_chacha20_context *ctx ) | ||||
| { | ||||
|     if( ctx != NULL ) | ||||
|     { | ||||
|         mbedtls_platform_zeroize( ctx->state, sizeof( ctx->state ) ); | ||||
|         mbedtls_platform_zeroize( ctx->keystream8, sizeof( ctx->keystream8 ) ); | ||||
|     CHACHA20_VALIDATE( ctx != NULL ); | ||||
| 
 | ||||
|         /* Initially, there's no keystream bytes available */ | ||||
|         ctx->keystream_bytes_used = CHACHA20_BLOCK_SIZE_BYTES; | ||||
|     } | ||||
|     mbedtls_platform_zeroize( ctx->state, sizeof( ctx->state ) ); | ||||
|     mbedtls_platform_zeroize( ctx->keystream8, sizeof( ctx->keystream8 ) ); | ||||
| 
 | ||||
|     /* Initially, there's no keystream bytes available */ | ||||
|     ctx->keystream_bytes_used = CHACHA20_BLOCK_SIZE_BYTES; | ||||
| } | ||||
| 
 | ||||
| void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx ) | ||||
|  | @ -202,10 +207,8 @@ void mbedtls_chacha20_free( mbedtls_chacha20_context *ctx ) | |||
| int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx, | ||||
|                             const unsigned char key[32] ) | ||||
| { | ||||
|     if( ( ctx == NULL ) || ( key == NULL ) ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA ); | ||||
|     } | ||||
|     CHACHA20_VALIDATE_RET( ctx != NULL ); | ||||
|     CHACHA20_VALIDATE_RET( key != NULL ); | ||||
| 
 | ||||
|     /* ChaCha20 constants - the string "expand 32-byte k" */ | ||||
|     ctx->state[0] = 0x61707865; | ||||
|  | @ -230,10 +233,8 @@ int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx, | |||
|                              const unsigned char nonce[12], | ||||
|                              uint32_t counter ) | ||||
| { | ||||
|     if( ( ctx == NULL ) || ( nonce == NULL ) ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA ); | ||||
|     } | ||||
|     CHACHA20_VALIDATE_RET( ctx != NULL ); | ||||
|     CHACHA20_VALIDATE_RET( nonce != NULL ); | ||||
| 
 | ||||
|     /* Counter */ | ||||
|     ctx->state[12] = counter; | ||||
|  | @ -259,15 +260,9 @@ int mbedtls_chacha20_update( mbedtls_chacha20_context *ctx, | |||
|     size_t offset = 0U; | ||||
|     size_t i; | ||||
| 
 | ||||
|     if( ctx == NULL ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA ); | ||||
|     } | ||||
|     else if( ( size > 0U ) && ( ( input == NULL ) || ( output == NULL ) ) ) | ||||
|     { | ||||
|         /* input and output pointers are allowed to be NULL only if size == 0 */ | ||||
|         return( MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA ); | ||||
|     } | ||||
|     CHACHA20_VALIDATE_RET( ctx != NULL ); | ||||
|     CHACHA20_VALIDATE_RET( size == 0 || input  != NULL ); | ||||
|     CHACHA20_VALIDATE_RET( size == 0 || output != NULL ); | ||||
| 
 | ||||
|     /* Use leftover keystream bytes, if available */ | ||||
|     while( size > 0U && ctx->keystream_bytes_used < CHACHA20_BLOCK_SIZE_BYTES ) | ||||
|  | @ -332,6 +327,11 @@ int mbedtls_chacha20_crypt( const unsigned char key[32], | |||
|     mbedtls_chacha20_context ctx; | ||||
|     int ret; | ||||
| 
 | ||||
|     CHACHA20_VALIDATE_RET( key != NULL ); | ||||
|     CHACHA20_VALIDATE_RET( nonce != NULL ); | ||||
|     CHACHA20_VALIDATE_RET( data_len == 0 || input  != NULL ); | ||||
|     CHACHA20_VALIDATE_RET( data_len == 0 || output != NULL ); | ||||
| 
 | ||||
|     mbedtls_chacha20_init( &ctx ); | ||||
| 
 | ||||
|     ret = mbedtls_chacha20_setkey( &ctx, key ); | ||||
|  |  | |||
|  | @ -44,6 +44,12 @@ | |||
| 
 | ||||
| #if !defined(MBEDTLS_CHACHAPOLY_ALT) | ||||
| 
 | ||||
| /* Parameter validation macros */ | ||||
| #define CHACHAPOLY_VALIDATE_RET( cond )                                       \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ) | ||||
| #define CHACHAPOLY_VALIDATE( cond )                                           \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| #define CHACHAPOLY_STATE_INIT       ( 0 ) | ||||
| #define CHACHAPOLY_STATE_AAD        ( 1 ) | ||||
| #define CHACHAPOLY_STATE_CIPHERTEXT ( 2 ) /* Encrypting or decrypting */ | ||||
|  | @ -90,39 +96,35 @@ static int chachapoly_pad_ciphertext( mbedtls_chachapoly_context *ctx ) | |||
| 
 | ||||
| void mbedtls_chachapoly_init( mbedtls_chachapoly_context *ctx ) | ||||
| { | ||||
|     if( ctx != NULL ) | ||||
|     { | ||||
|         mbedtls_chacha20_init( &ctx->chacha20_ctx ); | ||||
|         mbedtls_poly1305_init( &ctx->poly1305_ctx ); | ||||
|         ctx->aad_len        = 0U; | ||||
|         ctx->ciphertext_len = 0U; | ||||
|         ctx->state          = CHACHAPOLY_STATE_INIT; | ||||
|         ctx->mode           = MBEDTLS_CHACHAPOLY_ENCRYPT; | ||||
|     } | ||||
|     CHACHAPOLY_VALIDATE( ctx != NULL ); | ||||
| 
 | ||||
|     mbedtls_chacha20_init( &ctx->chacha20_ctx ); | ||||
|     mbedtls_poly1305_init( &ctx->poly1305_ctx ); | ||||
|     ctx->aad_len        = 0U; | ||||
|     ctx->ciphertext_len = 0U; | ||||
|     ctx->state          = CHACHAPOLY_STATE_INIT; | ||||
|     ctx->mode           = MBEDTLS_CHACHAPOLY_ENCRYPT; | ||||
| } | ||||
| 
 | ||||
| void mbedtls_chachapoly_free( mbedtls_chachapoly_context *ctx ) | ||||
| { | ||||
|     if( ctx != NULL ) | ||||
|     { | ||||
|         mbedtls_chacha20_free( &ctx->chacha20_ctx ); | ||||
|         mbedtls_poly1305_free( &ctx->poly1305_ctx ); | ||||
|         ctx->aad_len        = 0U; | ||||
|         ctx->ciphertext_len = 0U; | ||||
|         ctx->state          = CHACHAPOLY_STATE_INIT; | ||||
|         ctx->mode           = MBEDTLS_CHACHAPOLY_ENCRYPT; | ||||
|     } | ||||
|     if( ctx == NULL ) | ||||
|         return; | ||||
| 
 | ||||
|     mbedtls_chacha20_free( &ctx->chacha20_ctx ); | ||||
|     mbedtls_poly1305_free( &ctx->poly1305_ctx ); | ||||
|     ctx->aad_len        = 0U; | ||||
|     ctx->ciphertext_len = 0U; | ||||
|     ctx->state          = CHACHAPOLY_STATE_INIT; | ||||
|     ctx->mode           = MBEDTLS_CHACHAPOLY_ENCRYPT; | ||||
| } | ||||
| 
 | ||||
| int mbedtls_chachapoly_setkey( mbedtls_chachapoly_context *ctx, | ||||
|                                const unsigned char key[32] ) | ||||
| { | ||||
|     int ret; | ||||
| 
 | ||||
|     if( ( ctx == NULL ) || ( key == NULL ) ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | ||||
|     } | ||||
|     CHACHAPOLY_VALIDATE_RET( ctx != NULL ); | ||||
|     CHACHAPOLY_VALIDATE_RET( key != NULL ); | ||||
| 
 | ||||
|     ret = mbedtls_chacha20_setkey( &ctx->chacha20_ctx, key ); | ||||
| 
 | ||||
|  | @ -135,11 +137,8 @@ int mbedtls_chachapoly_starts( mbedtls_chachapoly_context *ctx, | |||
| { | ||||
|     int ret; | ||||
|     unsigned char poly1305_key[64]; | ||||
| 
 | ||||
|     if( ( ctx == NULL ) || ( nonce == NULL ) ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | ||||
|     } | ||||
|     CHACHAPOLY_VALIDATE_RET( ctx != NULL ); | ||||
|     CHACHAPOLY_VALIDATE_RET( nonce != NULL ); | ||||
| 
 | ||||
|     /* Set counter = 0, will be update to 1 when generating Poly1305 key */ | ||||
|     ret = mbedtls_chacha20_starts( &ctx->chacha20_ctx, nonce, 0U ); | ||||
|  | @ -176,19 +175,11 @@ int mbedtls_chachapoly_update_aad( mbedtls_chachapoly_context *ctx, | |||
|                                    const unsigned char *aad, | ||||
|                                    size_t aad_len ) | ||||
| { | ||||
|     if( ctx == NULL ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | ||||
|     } | ||||
|     else if( ( aad_len > 0U ) && ( aad == NULL ) ) | ||||
|     { | ||||
|         /* aad pointer is allowed to be NULL if aad_len == 0 */ | ||||
|         return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | ||||
|     } | ||||
|     else if( ctx->state != CHACHAPOLY_STATE_AAD ) | ||||
|     { | ||||
|     CHACHAPOLY_VALIDATE_RET( ctx != NULL ); | ||||
|     CHACHAPOLY_VALIDATE_RET( aad_len == 0 || aad != NULL ); | ||||
| 
 | ||||
|     if( ctx->state != CHACHAPOLY_STATE_AAD ) | ||||
|         return( MBEDTLS_ERR_CHACHAPOLY_BAD_STATE ); | ||||
|     } | ||||
| 
 | ||||
|     ctx->aad_len += aad_len; | ||||
| 
 | ||||
|  | @ -201,18 +192,12 @@ int mbedtls_chachapoly_update( mbedtls_chachapoly_context *ctx, | |||
|                                unsigned char *output ) | ||||
| { | ||||
|     int ret; | ||||
|     CHACHAPOLY_VALIDATE_RET( ctx != NULL ); | ||||
|     CHACHAPOLY_VALIDATE_RET( len == 0 || input != NULL ); | ||||
|     CHACHAPOLY_VALIDATE_RET( len == 0 || output != NULL ); | ||||
| 
 | ||||
|     if( ctx == NULL ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | ||||
|     } | ||||
|     else if( ( len > 0U ) && ( ( input == NULL ) || ( output == NULL ) ) ) | ||||
|     { | ||||
|         /* input and output pointers are allowed to be NULL if len == 0 */ | ||||
|         return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | ||||
|     } | ||||
|     else if( ( ctx->state != CHACHAPOLY_STATE_AAD ) && | ||||
|               ( ctx->state != CHACHAPOLY_STATE_CIPHERTEXT ) ) | ||||
|     if( ( ctx->state != CHACHAPOLY_STATE_AAD ) && | ||||
|         ( ctx->state != CHACHAPOLY_STATE_CIPHERTEXT ) ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_CHACHAPOLY_BAD_STATE ); | ||||
|     } | ||||
|  | @ -257,12 +242,10 @@ int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx, | |||
| { | ||||
|     int ret; | ||||
|     unsigned char len_block[16]; | ||||
|     CHACHAPOLY_VALIDATE_RET( ctx != NULL ); | ||||
|     CHACHAPOLY_VALIDATE_RET( mac != NULL ); | ||||
| 
 | ||||
|     if( ( ctx == NULL ) || ( mac == NULL ) ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | ||||
|     } | ||||
|     else if( ctx->state == CHACHAPOLY_STATE_INIT ) | ||||
|     if( ctx->state == CHACHAPOLY_STATE_INIT ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_CHACHAPOLY_BAD_STATE ); | ||||
|     } | ||||
|  | @ -350,6 +333,13 @@ int mbedtls_chachapoly_encrypt_and_tag( mbedtls_chachapoly_context *ctx, | |||
|                                         unsigned char *output, | ||||
|                                         unsigned char tag[16] ) | ||||
| { | ||||
|     CHACHAPOLY_VALIDATE_RET( ctx   != NULL ); | ||||
|     CHACHAPOLY_VALIDATE_RET( nonce != NULL ); | ||||
|     CHACHAPOLY_VALIDATE_RET( tag   != NULL ); | ||||
|     CHACHAPOLY_VALIDATE_RET( aad_len == 0 || aad    != NULL ); | ||||
|     CHACHAPOLY_VALIDATE_RET( length  == 0 || input  != NULL ); | ||||
|     CHACHAPOLY_VALIDATE_RET( length  == 0 || output != NULL ); | ||||
| 
 | ||||
|     return( chachapoly_crypt_and_tag( ctx, MBEDTLS_CHACHAPOLY_ENCRYPT, | ||||
|                                       length, nonce, aad, aad_len, | ||||
|                                       input, output, tag ) ); | ||||
|  | @ -368,9 +358,12 @@ int mbedtls_chachapoly_auth_decrypt( mbedtls_chachapoly_context *ctx, | |||
|     unsigned char check_tag[16]; | ||||
|     size_t i; | ||||
|     int diff; | ||||
| 
 | ||||
|     if( tag == NULL ) | ||||
|         return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | ||||
|     CHACHAPOLY_VALIDATE_RET( ctx   != NULL ); | ||||
|     CHACHAPOLY_VALIDATE_RET( nonce != NULL ); | ||||
|     CHACHAPOLY_VALIDATE_RET( tag   != NULL ); | ||||
|     CHACHAPOLY_VALIDATE_RET( aad_len == 0 || aad    != NULL ); | ||||
|     CHACHAPOLY_VALIDATE_RET( length  == 0 || input  != NULL ); | ||||
|     CHACHAPOLY_VALIDATE_RET( length  == 0 || output != NULL ); | ||||
| 
 | ||||
|     if( ( ret = chachapoly_crypt_and_tag( ctx, | ||||
|                         MBEDTLS_CHACHAPOLY_DECRYPT, length, nonce, | ||||
|  |  | |||
							
								
								
									
										134
									
								
								library/cipher.c
									
									
									
									
									
								
							
							
						
						
									
										134
									
								
								library/cipher.c
									
									
									
									
									
								
							|  | @ -70,6 +70,11 @@ | |||
| #define mbedtls_free   free | ||||
| #endif | ||||
| 
 | ||||
| #define CIPHER_VALIDATE_RET( cond )    \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ) | ||||
| #define CIPHER_VALIDATE( cond )        \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | ||||
| /* Compare the contents of two buffers in constant time.
 | ||||
|  * Returns 0 if the contents are bitwise identical, otherwise returns | ||||
|  | @ -87,7 +92,7 @@ static int mbedtls_constant_time_memcmp( const void *v1, const void *v2, | |||
|     for( diff = 0, i = 0; i < len; i++ ) | ||||
|         diff |= p1[i] ^ p2[i]; | ||||
| 
 | ||||
|     return (int)diff; | ||||
|     return( (int)diff ); | ||||
| } | ||||
| #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */ | ||||
| 
 | ||||
|  | @ -159,6 +164,7 @@ const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( | |||
| 
 | ||||
| void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx ) | ||||
| { | ||||
|     CIPHER_VALIDATE( ctx != NULL ); | ||||
|     memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) ); | ||||
| } | ||||
| 
 | ||||
|  | @ -208,7 +214,8 @@ void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx ) | |||
| int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, | ||||
|                           const mbedtls_cipher_info_t *cipher_info ) | ||||
| { | ||||
|     if( NULL == cipher_info || NULL == ctx ) | ||||
|     CIPHER_VALIDATE_RET( ctx != NULL ); | ||||
|     if( cipher_info == NULL ) | ||||
|         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) ); | ||||
|  | @ -269,17 +276,12 @@ int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, | |||
|                            int key_bitlen, | ||||
|                            const mbedtls_operation_t operation ) | ||||
| { | ||||
|     if( NULL == ctx || NULL == ctx->cipher_info || | ||||
|         NULL == ctx->cipher_ctx ) | ||||
|     { | ||||
|     CIPHER_VALIDATE_RET( ctx != NULL ); | ||||
|     CIPHER_VALIDATE_RET( key != NULL ); | ||||
|     CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT || | ||||
|                          operation == MBEDTLS_DECRYPT ); | ||||
|     if( ctx->cipher_info == NULL ) | ||||
|         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | ||||
|     } | ||||
| 
 | ||||
|     if( operation != MBEDTLS_DECRYPT && | ||||
|         operation != MBEDTLS_ENCRYPT ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | ||||
|     } | ||||
| 
 | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     if( ctx->psa_enabled == 1 ) | ||||
|  | @ -364,27 +366,27 @@ int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, | |||
|         MBEDTLS_MODE_OFB == ctx->cipher_info->mode || | ||||
|         MBEDTLS_MODE_CTR == ctx->cipher_info->mode ) | ||||
|     { | ||||
|         return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key, | ||||
|                                                         ctx->key_bitlen ); | ||||
|         return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key, | ||||
|                                                          ctx->key_bitlen ) ); | ||||
|     } | ||||
| 
 | ||||
|     if( MBEDTLS_DECRYPT == operation ) | ||||
|         return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key, | ||||
|                                                         ctx->key_bitlen ); | ||||
| 
 | ||||
|         return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key, | ||||
|                                                          ctx->key_bitlen ) ); | ||||
| 
 | ||||
|     return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | ||||
| } | ||||
| 
 | ||||
| int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx, | ||||
|                    const unsigned char *iv, size_t iv_len ) | ||||
|                            const unsigned char *iv, | ||||
|                            size_t iv_len ) | ||||
| { | ||||
|     size_t actual_iv_size; | ||||
|     if( NULL == ctx || NULL == ctx->cipher_info ) | ||||
|         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | ||||
|     else if( NULL == iv && iv_len != 0  ) | ||||
|         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     CIPHER_VALIDATE_RET( ctx != NULL ); | ||||
|     CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL ); | ||||
|     if( ctx->cipher_info == NULL ) | ||||
|         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     if( ctx->psa_enabled == 1 ) | ||||
|     { | ||||
|  | @ -395,9 +397,6 @@ int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx, | |||
|     } | ||||
| #endif /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
| 
 | ||||
|     if( NULL == iv && iv_len == 0 ) | ||||
|         ctx->iv_size = 0; | ||||
| 
 | ||||
|     /* avoid buffer overflow in ctx->iv */ | ||||
|     if( iv_len > MBEDTLS_MAX_IV_LENGTH ) | ||||
|         return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ); | ||||
|  | @ -436,7 +435,8 @@ int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx, | |||
| 
 | ||||
| int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx ) | ||||
| { | ||||
|     if( NULL == ctx || NULL == ctx->cipher_info ) | ||||
|     CIPHER_VALIDATE_RET( ctx != NULL ); | ||||
|     if( ctx->cipher_info == NULL ) | ||||
|         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|  | @ -457,7 +457,9 @@ int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx ) | |||
| int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx, | ||||
|                       const unsigned char *ad, size_t ad_len ) | ||||
| { | ||||
|     if( NULL == ctx || NULL == ctx->cipher_info ) | ||||
|     CIPHER_VALIDATE_RET( ctx != NULL ); | ||||
|     CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL ); | ||||
|     if( ctx->cipher_info == NULL ) | ||||
|         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|  | @ -473,8 +475,8 @@ int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx, | |||
| #if defined(MBEDTLS_GCM_C) | ||||
|     if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode ) | ||||
|     { | ||||
|         return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation, | ||||
|                            ctx->iv, ctx->iv_size, ad, ad_len ); | ||||
|         return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation, | ||||
|                                     ctx->iv, ctx->iv_size, ad, ad_len ) ); | ||||
|     } | ||||
| #endif | ||||
| 
 | ||||
|  | @ -494,8 +496,8 @@ int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx, | |||
|         if ( result != 0 ) | ||||
|             return( result ); | ||||
| 
 | ||||
|         return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx, | ||||
|                                                           ad, ad_len ); | ||||
|         return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx, | ||||
|                                                ad, ad_len ) ); | ||||
|     } | ||||
| #endif | ||||
| 
 | ||||
|  | @ -507,12 +509,14 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i | |||
|                    size_t ilen, unsigned char *output, size_t *olen ) | ||||
| { | ||||
|     int ret; | ||||
|     size_t block_size = 0; | ||||
|     size_t block_size; | ||||
| 
 | ||||
|     if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ) | ||||
|     { | ||||
|     CIPHER_VALIDATE_RET( ctx != NULL ); | ||||
|     CIPHER_VALIDATE_RET( ilen == 0 || input != NULL ); | ||||
|     CIPHER_VALIDATE_RET( output != NULL ); | ||||
|     CIPHER_VALIDATE_RET( olen != NULL ); | ||||
|     if( ctx->cipher_info == NULL ) | ||||
|         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | ||||
|     } | ||||
| 
 | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     if( ctx->psa_enabled == 1 ) | ||||
|  | @ -547,8 +551,8 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i | |||
|     if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM ) | ||||
|     { | ||||
|         *olen = ilen; | ||||
|         return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input, | ||||
|                            output ); | ||||
|         return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input, | ||||
|                                     output ) ); | ||||
|     } | ||||
| #endif | ||||
| 
 | ||||
|  | @ -556,14 +560,14 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i | |||
|     if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 ) | ||||
|     { | ||||
|         *olen = ilen; | ||||
|         return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx, | ||||
|                                                       ilen, input, output ); | ||||
|         return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx, | ||||
|                                            ilen, input, output ) ); | ||||
|     } | ||||
| #endif | ||||
| 
 | ||||
|     if ( 0 == block_size ) | ||||
|     { | ||||
|         return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT; | ||||
|         return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT ); | ||||
|     } | ||||
| 
 | ||||
|     if( input == output && | ||||
|  | @ -626,7 +630,7 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i | |||
|         { | ||||
|             if( 0 == block_size ) | ||||
|             { | ||||
|                 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT; | ||||
|                 return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT ); | ||||
|             } | ||||
| 
 | ||||
|             /* Encryption: only cache partial blocks
 | ||||
|  | @ -927,7 +931,10 @@ static int get_no_padding( unsigned char *input, size_t input_len, | |||
| int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx, | ||||
|                    unsigned char *output, size_t *olen ) | ||||
| { | ||||
|     if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ) | ||||
|     CIPHER_VALIDATE_RET( ctx != NULL ); | ||||
|     CIPHER_VALIDATE_RET( output != NULL ); | ||||
|     CIPHER_VALIDATE_RET( olen != NULL ); | ||||
|     if( ctx->cipher_info == NULL ) | ||||
|         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|  | @ -1007,8 +1014,8 @@ int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx, | |||
| 
 | ||||
|         /* Set output size for decryption */ | ||||
|         if( MBEDTLS_DECRYPT == ctx->operation ) | ||||
|             return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ), | ||||
|                                      olen ); | ||||
|             return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ), | ||||
|                                       olen ) ); | ||||
| 
 | ||||
|         /* Set output size for encryption */ | ||||
|         *olen = mbedtls_cipher_get_block_size( ctx ); | ||||
|  | @ -1025,8 +1032,9 @@ int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx, | |||
| int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, | ||||
|                                      mbedtls_cipher_padding_t mode ) | ||||
| { | ||||
|     if( NULL == ctx || | ||||
|         MBEDTLS_MODE_CBC != ctx->cipher_info->mode ) | ||||
|     CIPHER_VALIDATE_RET( ctx != NULL ); | ||||
| 
 | ||||
|     if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | ||||
|     } | ||||
|  | @ -1087,7 +1095,9 @@ int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, | |||
| int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx, | ||||
|                       unsigned char *tag, size_t tag_len ) | ||||
| { | ||||
|     if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag ) | ||||
|     CIPHER_VALIDATE_RET( ctx != NULL ); | ||||
|     CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL ); | ||||
|     if( ctx->cipher_info == NULL ) | ||||
|         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( MBEDTLS_ENCRYPT != ctx->operation ) | ||||
|  | @ -1130,8 +1140,12 @@ int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx, | |||
|     unsigned char check_tag[16]; | ||||
|     int ret; | ||||
| 
 | ||||
|     if( NULL == ctx || NULL == ctx->cipher_info || | ||||
|         MBEDTLS_DECRYPT != ctx->operation ) | ||||
|     CIPHER_VALIDATE_RET( ctx != NULL ); | ||||
|     CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL ); | ||||
|     if( ctx->cipher_info == NULL ) | ||||
|         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( MBEDTLS_DECRYPT != ctx->operation ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | ||||
|     } | ||||
|  | @ -1204,6 +1218,12 @@ int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx, | |||
|     int ret; | ||||
|     size_t finish_olen; | ||||
| 
 | ||||
|     CIPHER_VALIDATE_RET( ctx != NULL ); | ||||
|     CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL ); | ||||
|     CIPHER_VALIDATE_RET( ilen == 0 || input != NULL ); | ||||
|     CIPHER_VALIDATE_RET( output != NULL ); | ||||
|     CIPHER_VALIDATE_RET( olen != NULL ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     if( ctx->psa_enabled == 1 ) | ||||
|     { | ||||
|  | @ -1292,6 +1312,14 @@ int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx, | |||
|                          unsigned char *output, size_t *olen, | ||||
|                          unsigned char *tag, size_t tag_len ) | ||||
| { | ||||
|     CIPHER_VALIDATE_RET( ctx != NULL ); | ||||
|     CIPHER_VALIDATE_RET( iv != NULL ); | ||||
|     CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL ); | ||||
|     CIPHER_VALIDATE_RET( ilen == 0 || input != NULL ); | ||||
|     CIPHER_VALIDATE_RET( output != NULL ); | ||||
|     CIPHER_VALIDATE_RET( olen != NULL ); | ||||
|     CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     if( ctx->psa_enabled == 1 ) | ||||
|     { | ||||
|  | @ -1371,6 +1399,14 @@ int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx, | |||
|                          unsigned char *output, size_t *olen, | ||||
|                          const unsigned char *tag, size_t tag_len ) | ||||
| { | ||||
|     CIPHER_VALIDATE_RET( ctx != NULL ); | ||||
|     CIPHER_VALIDATE_RET( iv != NULL ); | ||||
|     CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL ); | ||||
|     CIPHER_VALIDATE_RET( ilen == 0 || input != NULL ); | ||||
|     CIPHER_VALIDATE_RET( output != NULL ); | ||||
|     CIPHER_VALIDATE_RET( olen != NULL ); | ||||
|     CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     if( ctx->psa_enabled == 1 ) | ||||
|     { | ||||
|  |  | |||
|  | @ -102,7 +102,8 @@ int mbedtls_ctr_drbg_seed_entropy_len( | |||
|     /*
 | ||||
|      * Initialize with an empty key | ||||
|      */ | ||||
|     if( ( ret = mbedtls_aes_setkey_enc( &ctx->aes_ctx, key, MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 ) | ||||
|     if( ( ret = mbedtls_aes_setkey_enc( &ctx->aes_ctx, key, | ||||
|                                         MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 ) | ||||
|     { | ||||
|         return( ret ); | ||||
|     } | ||||
|  | @ -120,8 +121,9 @@ int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx, | |||
|                    const unsigned char *custom, | ||||
|                    size_t len ) | ||||
| { | ||||
|     return( mbedtls_ctr_drbg_seed_entropy_len( ctx, f_entropy, p_entropy, custom, len, | ||||
|                                        MBEDTLS_CTR_DRBG_ENTROPY_LEN ) ); | ||||
|     return( mbedtls_ctr_drbg_seed_entropy_len( ctx, f_entropy, p_entropy, | ||||
|                                                custom, len, | ||||
|                                                MBEDTLS_CTR_DRBG_ENTROPY_LEN ) ); | ||||
| } | ||||
| 
 | ||||
| void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx ) | ||||
|  | @ -136,17 +138,20 @@ void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx ) | |||
|     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ctr_drbg_context ) ); | ||||
| } | ||||
| 
 | ||||
| void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx, int resistance ) | ||||
| void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx, | ||||
|                                                  int resistance ) | ||||
| { | ||||
|     ctx->prediction_resistance = resistance; | ||||
| } | ||||
| 
 | ||||
| void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx, size_t len ) | ||||
| void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx, | ||||
|                                        size_t len ) | ||||
| { | ||||
|     ctx->entropy_len = len; | ||||
| } | ||||
| 
 | ||||
| void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx, int interval ) | ||||
| void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx, | ||||
|                                            int interval ) | ||||
| { | ||||
|     ctx->reseed_interval = interval; | ||||
| } | ||||
|  | @ -154,7 +159,8 @@ void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx, int in | |||
| static int block_cipher_df( unsigned char *output, | ||||
|                             const unsigned char *data, size_t data_len ) | ||||
| { | ||||
|     unsigned char buf[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16]; | ||||
|     unsigned char buf[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + | ||||
|                       MBEDTLS_CTR_DRBG_BLOCKSIZE + 16]; | ||||
|     unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN]; | ||||
|     unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE]; | ||||
|     unsigned char chain[MBEDTLS_CTR_DRBG_BLOCKSIZE]; | ||||
|  | @ -168,7 +174,8 @@ static int block_cipher_df( unsigned char *output, | |||
|     if( data_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT ) | ||||
|         return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG ); | ||||
| 
 | ||||
|     memset( buf, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16 ); | ||||
|     memset( buf, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + | ||||
|             MBEDTLS_CTR_DRBG_BLOCKSIZE + 16 ); | ||||
|     mbedtls_aes_init( &aes_ctx ); | ||||
| 
 | ||||
|     /*
 | ||||
|  | @ -193,7 +200,8 @@ static int block_cipher_df( unsigned char *output, | |||
|     for( i = 0; i < MBEDTLS_CTR_DRBG_KEYSIZE; i++ ) | ||||
|         key[i] = i; | ||||
| 
 | ||||
|     if( ( ret = mbedtls_aes_setkey_enc( &aes_ctx, key, MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 ) | ||||
|     if( ( ret = mbedtls_aes_setkey_enc( &aes_ctx, key, | ||||
|                                         MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 ) | ||||
|     { | ||||
|         goto exit; | ||||
|     } | ||||
|  | @ -215,7 +223,8 @@ static int block_cipher_df( unsigned char *output, | |||
|             use_len -= ( use_len >= MBEDTLS_CTR_DRBG_BLOCKSIZE ) ? | ||||
|                        MBEDTLS_CTR_DRBG_BLOCKSIZE : use_len; | ||||
| 
 | ||||
|             if( ( ret = mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, chain, chain ) ) != 0 ) | ||||
|             if( ( ret = mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, | ||||
|                                                chain, chain ) ) != 0 ) | ||||
|             { | ||||
|                 goto exit; | ||||
|             } | ||||
|  | @ -232,7 +241,8 @@ static int block_cipher_df( unsigned char *output, | |||
|     /*
 | ||||
|      * Do final encryption with reduced data | ||||
|      */ | ||||
|     if( ( ret = mbedtls_aes_setkey_enc( &aes_ctx, tmp, MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 ) | ||||
|     if( ( ret = mbedtls_aes_setkey_enc( &aes_ctx, tmp, | ||||
|                                         MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 ) | ||||
|     { | ||||
|         goto exit; | ||||
|     } | ||||
|  | @ -241,7 +251,8 @@ static int block_cipher_df( unsigned char *output, | |||
| 
 | ||||
|     for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE ) | ||||
|     { | ||||
|         if( ( ret = mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, iv, iv ) ) != 0 ) | ||||
|         if( ( ret = mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, | ||||
|                                            iv, iv ) ) != 0 ) | ||||
|         { | ||||
|             goto exit; | ||||
|         } | ||||
|  | @ -277,7 +288,7 @@ exit: | |||
|  *   ctx->counter = V | ||||
|  */ | ||||
| static int ctr_drbg_update_internal( mbedtls_ctr_drbg_context *ctx, | ||||
|                               const unsigned char data[MBEDTLS_CTR_DRBG_SEEDLEN] ) | ||||
|                           const unsigned char data[MBEDTLS_CTR_DRBG_SEEDLEN] ) | ||||
| { | ||||
|     unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN]; | ||||
|     unsigned char *p = tmp; | ||||
|  | @ -298,8 +309,11 @@ static int ctr_drbg_update_internal( mbedtls_ctr_drbg_context *ctx, | |||
|         /*
 | ||||
|          * Crypt counter block | ||||
|          */ | ||||
|         if( ( ret = mbedtls_aes_crypt_ecb( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, ctx->counter, p ) ) != 0 ) | ||||
|         if( ( ret = mbedtls_aes_crypt_ecb( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, | ||||
|                                            ctx->counter, p ) ) != 0 ) | ||||
|         { | ||||
|             goto exit; | ||||
|         } | ||||
| 
 | ||||
|         p += MBEDTLS_CTR_DRBG_BLOCKSIZE; | ||||
|     } | ||||
|  | @ -310,9 +324,13 @@ static int ctr_drbg_update_internal( mbedtls_ctr_drbg_context *ctx, | |||
|     /*
 | ||||
|      * Update key and counter | ||||
|      */ | ||||
|     if( ( ret = mbedtls_aes_setkey_enc( &ctx->aes_ctx, tmp, MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 ) | ||||
|     if( ( ret = mbedtls_aes_setkey_enc( &ctx->aes_ctx, tmp, | ||||
|                                         MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 ) | ||||
|     { | ||||
|         goto exit; | ||||
|     memcpy( ctx->counter, tmp + MBEDTLS_CTR_DRBG_KEYSIZE, MBEDTLS_CTR_DRBG_BLOCKSIZE ); | ||||
|     } | ||||
|     memcpy( ctx->counter, tmp + MBEDTLS_CTR_DRBG_KEYSIZE, | ||||
|             MBEDTLS_CTR_DRBG_BLOCKSIZE ); | ||||
| 
 | ||||
| exit: | ||||
|     mbedtls_platform_zeroize( tmp, sizeof( tmp ) ); | ||||
|  | @ -496,11 +514,14 @@ int mbedtls_ctr_drbg_random_with_add( void *p_rng, | |||
|         /*
 | ||||
|          * Crypt counter block | ||||
|          */ | ||||
|         if( ( ret = mbedtls_aes_crypt_ecb( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, ctx->counter, tmp ) ) != 0 ) | ||||
|         if( ( ret = mbedtls_aes_crypt_ecb( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, | ||||
|                                            ctx->counter, tmp ) ) != 0 ) | ||||
|         { | ||||
|             goto exit; | ||||
|         } | ||||
| 
 | ||||
|         use_len = ( output_len > MBEDTLS_CTR_DRBG_BLOCKSIZE ) ? MBEDTLS_CTR_DRBG_BLOCKSIZE : | ||||
|                                                        output_len; | ||||
|         use_len = ( output_len > MBEDTLS_CTR_DRBG_BLOCKSIZE ) | ||||
|             ? MBEDTLS_CTR_DRBG_BLOCKSIZE : output_len; | ||||
|         /*
 | ||||
|          * Copy random block to destination | ||||
|          */ | ||||
|  | @ -520,7 +541,8 @@ exit: | |||
|     return( 0 ); | ||||
| } | ||||
| 
 | ||||
| int mbedtls_ctr_drbg_random( void *p_rng, unsigned char *output, size_t output_len ) | ||||
| int mbedtls_ctr_drbg_random( void *p_rng, unsigned char *output, | ||||
|                              size_t output_len ) | ||||
| { | ||||
|     int ret; | ||||
|     mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng; | ||||
|  | @ -541,7 +563,8 @@ int mbedtls_ctr_drbg_random( void *p_rng, unsigned char *output, size_t output_l | |||
| } | ||||
| 
 | ||||
| #if defined(MBEDTLS_FS_IO) | ||||
| int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path ) | ||||
| int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, | ||||
|                                       const char *path ) | ||||
| { | ||||
|     int ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR; | ||||
|     FILE *f; | ||||
|  | @ -550,13 +573,19 @@ int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char | |||
|     if( ( f = fopen( path, "wb" ) ) == NULL ) | ||||
|         return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_ctr_drbg_random( ctx, buf, MBEDTLS_CTR_DRBG_MAX_INPUT ) ) != 0 ) | ||||
|     if( ( ret = mbedtls_ctr_drbg_random( ctx, buf, | ||||
|                                          MBEDTLS_CTR_DRBG_MAX_INPUT ) ) != 0 ) | ||||
|         goto exit; | ||||
| 
 | ||||
|     if( fwrite( buf, 1, MBEDTLS_CTR_DRBG_MAX_INPUT, f ) != MBEDTLS_CTR_DRBG_MAX_INPUT ) | ||||
|     if( fwrite( buf, 1, MBEDTLS_CTR_DRBG_MAX_INPUT, f ) != | ||||
|         MBEDTLS_CTR_DRBG_MAX_INPUT ) | ||||
|     { | ||||
|         ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR; | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|         ret = 0; | ||||
|     } | ||||
| 
 | ||||
| exit: | ||||
|     mbedtls_platform_zeroize( buf, sizeof( buf ) ); | ||||
|  | @ -565,7 +594,8 @@ exit: | |||
|     return( ret ); | ||||
| } | ||||
| 
 | ||||
| int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path ) | ||||
| int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, | ||||
|                                        const char *path ) | ||||
| { | ||||
|     int ret = 0; | ||||
|     FILE *f = NULL; | ||||
|  | @ -679,7 +709,7 @@ int mbedtls_ctr_drbg_self_test( int verbose ) | |||
| 
 | ||||
|     test_offset = 0; | ||||
|     CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy, | ||||
|                                 (void *) entropy_source_pr, nonce_pers_pr, 16, 32 ) ); | ||||
|                          (void *) entropy_source_pr, nonce_pers_pr, 16, 32 ) ); | ||||
|     mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON ); | ||||
|     CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) ); | ||||
|     CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) ); | ||||
|  | @ -700,7 +730,7 @@ int mbedtls_ctr_drbg_self_test( int verbose ) | |||
| 
 | ||||
|     test_offset = 0; | ||||
|     CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy, | ||||
|                             (void *) entropy_source_nopr, nonce_pers_nopr, 16, 32 ) ); | ||||
|                      (void *) entropy_source_nopr, nonce_pers_nopr, 16, 32 ) ); | ||||
|     CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) ); | ||||
|     CHK( mbedtls_ctr_drbg_reseed( &ctx, NULL, 0 ) ); | ||||
|     CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) ); | ||||
|  |  | |||
|  | @ -35,6 +35,7 @@ | |||
| #define mbedtls_free        free | ||||
| #define mbedtls_time_t      time_t | ||||
| #define mbedtls_snprintf    snprintf | ||||
| #define mbedtls_vsnprintf   vsnprintf | ||||
| #endif | ||||
| 
 | ||||
| #include "mbedtls/debug.h" | ||||
|  | @ -90,20 +91,7 @@ void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level, | |||
|         return; | ||||
| 
 | ||||
|     va_start( argp, format ); | ||||
| #if defined(_WIN32) | ||||
| #if defined(_TRUNCATE) && !defined(__MINGW32__) | ||||
|     ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp ); | ||||
| #else | ||||
|     ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); | ||||
|     if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE ) | ||||
|     { | ||||
|         str[DEBUG_BUF_SIZE-1] = '\0'; | ||||
|         ret = -1; | ||||
|     } | ||||
| #endif | ||||
| #else | ||||
|     ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); | ||||
| #endif | ||||
|     ret = mbedtls_vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); | ||||
|     va_end( argp ); | ||||
| 
 | ||||
|     if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 ) | ||||
|  |  | |||
|  | @ -60,6 +60,11 @@ | |||
| 
 | ||||
| #if !defined(MBEDTLS_DHM_ALT) | ||||
| 
 | ||||
| #define DHM_VALIDATE_RET( cond )    \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_DHM_BAD_INPUT_DATA ) | ||||
| #define DHM_VALIDATE( cond )        \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| /*
 | ||||
|  * helper to validate the mbedtls_mpi size and import it | ||||
|  */ | ||||
|  | @ -121,6 +126,7 @@ cleanup: | |||
| 
 | ||||
| void mbedtls_dhm_init( mbedtls_dhm_context *ctx ) | ||||
| { | ||||
|     DHM_VALIDATE( ctx != NULL ); | ||||
|     memset( ctx, 0, sizeof( mbedtls_dhm_context ) ); | ||||
| } | ||||
| 
 | ||||
|  | @ -132,6 +138,9 @@ int mbedtls_dhm_read_params( mbedtls_dhm_context *ctx, | |||
|                      const unsigned char *end ) | ||||
| { | ||||
|     int ret; | ||||
|     DHM_VALIDATE_RET( ctx != NULL ); | ||||
|     DHM_VALIDATE_RET( p != NULL && *p != NULL ); | ||||
|     DHM_VALIDATE_RET( end != NULL ); | ||||
| 
 | ||||
|     if( ( ret = dhm_read_bignum( &ctx->P,  p, end ) ) != 0 || | ||||
|         ( ret = dhm_read_bignum( &ctx->G,  p, end ) ) != 0 || | ||||
|  | @ -157,6 +166,10 @@ int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size, | |||
|     int ret, count = 0; | ||||
|     size_t n1, n2, n3; | ||||
|     unsigned char *p; | ||||
|     DHM_VALIDATE_RET( ctx != NULL ); | ||||
|     DHM_VALIDATE_RET( output != NULL ); | ||||
|     DHM_VALIDATE_RET( olen != NULL ); | ||||
|     DHM_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
|     if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ) | ||||
|         return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA ); | ||||
|  | @ -227,9 +240,9 @@ int mbedtls_dhm_set_group( mbedtls_dhm_context *ctx, | |||
|                            const mbedtls_mpi *G ) | ||||
| { | ||||
|     int ret; | ||||
| 
 | ||||
|     if( ctx == NULL || P == NULL || G == NULL ) | ||||
|         return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA ); | ||||
|     DHM_VALIDATE_RET( ctx != NULL ); | ||||
|     DHM_VALIDATE_RET( P != NULL ); | ||||
|     DHM_VALIDATE_RET( G != NULL ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 || | ||||
|         ( ret = mbedtls_mpi_copy( &ctx->G, G ) ) != 0 ) | ||||
|  | @ -248,8 +261,10 @@ int mbedtls_dhm_read_public( mbedtls_dhm_context *ctx, | |||
|                      const unsigned char *input, size_t ilen ) | ||||
| { | ||||
|     int ret; | ||||
|     DHM_VALIDATE_RET( ctx != NULL ); | ||||
|     DHM_VALIDATE_RET( input != NULL ); | ||||
| 
 | ||||
|     if( ctx == NULL || ilen < 1 || ilen > ctx->len ) | ||||
|     if( ilen < 1 || ilen > ctx->len ) | ||||
|         return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 ) | ||||
|  | @ -267,8 +282,11 @@ int mbedtls_dhm_make_public( mbedtls_dhm_context *ctx, int x_size, | |||
|                      void *p_rng ) | ||||
| { | ||||
|     int ret, count = 0; | ||||
|     DHM_VALIDATE_RET( ctx != NULL ); | ||||
|     DHM_VALIDATE_RET( output != NULL ); | ||||
|     DHM_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
|     if( ctx == NULL || olen < 1 || olen > ctx->len ) | ||||
|     if( olen < 1 || olen > ctx->len ) | ||||
|         return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ) | ||||
|  | @ -380,8 +398,11 @@ int mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx, | |||
| { | ||||
|     int ret; | ||||
|     mbedtls_mpi GYb; | ||||
|     DHM_VALIDATE_RET( ctx != NULL ); | ||||
|     DHM_VALIDATE_RET( output != NULL ); | ||||
|     DHM_VALIDATE_RET( olen != NULL ); | ||||
| 
 | ||||
|     if( ctx == NULL || output_size < ctx->len ) | ||||
|     if( output_size < ctx->len ) | ||||
|         return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 ) | ||||
|  | @ -428,11 +449,19 @@ cleanup: | |||
|  */ | ||||
| void mbedtls_dhm_free( mbedtls_dhm_context *ctx ) | ||||
| { | ||||
|     mbedtls_mpi_free( &ctx->pX ); mbedtls_mpi_free( &ctx->Vf ); | ||||
|     mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->RP ); | ||||
|     mbedtls_mpi_free( &ctx->K  ); mbedtls_mpi_free( &ctx->GY ); | ||||
|     mbedtls_mpi_free( &ctx->GX ); mbedtls_mpi_free( &ctx->X  ); | ||||
|     mbedtls_mpi_free( &ctx->G  ); mbedtls_mpi_free( &ctx->P  ); | ||||
|     if( ctx == NULL ) | ||||
|         return; | ||||
| 
 | ||||
|     mbedtls_mpi_free( &ctx->pX ); | ||||
|     mbedtls_mpi_free( &ctx->Vf ); | ||||
|     mbedtls_mpi_free( &ctx->Vi ); | ||||
|     mbedtls_mpi_free( &ctx->RP ); | ||||
|     mbedtls_mpi_free( &ctx->K  ); | ||||
|     mbedtls_mpi_free( &ctx->GY ); | ||||
|     mbedtls_mpi_free( &ctx->GX ); | ||||
|     mbedtls_mpi_free( &ctx->X  ); | ||||
|     mbedtls_mpi_free( &ctx->G  ); | ||||
|     mbedtls_mpi_free( &ctx->P  ); | ||||
| 
 | ||||
|     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_dhm_context ) ); | ||||
| } | ||||
|  | @ -449,7 +478,12 @@ int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin, | |||
|     unsigned char *p, *end; | ||||
| #if defined(MBEDTLS_PEM_PARSE_C) | ||||
|     mbedtls_pem_context pem; | ||||
| #endif /* MBEDTLS_PEM_PARSE_C */ | ||||
| 
 | ||||
|     DHM_VALIDATE_RET( dhm != NULL ); | ||||
|     DHM_VALIDATE_RET( dhmin != NULL ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_PEM_PARSE_C) | ||||
|     mbedtls_pem_init( &pem ); | ||||
| 
 | ||||
|     /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ | ||||
|  | @ -596,6 +630,8 @@ int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path ) | |||
|     int ret; | ||||
|     size_t n; | ||||
|     unsigned char *buf; | ||||
|     DHM_VALIDATE_RET( dhm != NULL ); | ||||
|     DHM_VALIDATE_RET( path != NULL ); | ||||
| 
 | ||||
|     if( ( ret = load_file( path, &buf, &n ) ) != 0 ) | ||||
|         return( ret ); | ||||
|  |  | |||
|  | @ -35,9 +35,16 @@ | |||
| #if defined(MBEDTLS_ECDH_C) | ||||
| 
 | ||||
| #include "mbedtls/ecdh.h" | ||||
| #include "mbedtls/platform_util.h" | ||||
| 
 | ||||
| #include <string.h> | ||||
| 
 | ||||
| /* Parameter validation macros based on platform_util.h */ | ||||
| #define ECDH_VALIDATE_RET( cond )    \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA ) | ||||
| #define ECDH_VALIDATE( cond )        \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) | ||||
| typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed; | ||||
| #endif | ||||
|  | @ -78,6 +85,10 @@ int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp | |||
|                      int (*f_rng)(void *, unsigned char *, size_t), | ||||
|                      void *p_rng ) | ||||
| { | ||||
|     ECDH_VALIDATE_RET( grp != NULL ); | ||||
|     ECDH_VALIDATE_RET( d != NULL ); | ||||
|     ECDH_VALIDATE_RET( Q != NULL ); | ||||
|     ECDH_VALIDATE_RET( f_rng != NULL ); | ||||
|     return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) ); | ||||
| } | ||||
| #endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */ | ||||
|  | @ -123,6 +134,10 @@ int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z, | |||
|                          int (*f_rng)(void *, unsigned char *, size_t), | ||||
|                          void *p_rng ) | ||||
| { | ||||
|     ECDH_VALIDATE_RET( grp != NULL ); | ||||
|     ECDH_VALIDATE_RET( Q != NULL ); | ||||
|     ECDH_VALIDATE_RET( d != NULL ); | ||||
|     ECDH_VALIDATE_RET( z != NULL ); | ||||
|     return( ecdh_compute_shared_restartable( grp, z, Q, d, | ||||
|                                              f_rng, p_rng, NULL ) ); | ||||
| } | ||||
|  | @ -146,6 +161,8 @@ static void ecdh_init_internal( mbedtls_ecdh_context_mbed *ctx ) | |||
|  */ | ||||
| void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx ) | ||||
| { | ||||
|     ECDH_VALIDATE( ctx != NULL ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) | ||||
|     ecdh_init_internal( ctx ); | ||||
|     mbedtls_ecp_point_init( &ctx->Vi  ); | ||||
|  | @ -181,8 +198,7 @@ static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *ctx, | |||
|  */ | ||||
| int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id ) | ||||
| { | ||||
|     if( ctx == NULL ) | ||||
|         return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); | ||||
|     ECDH_VALIDATE_RET( ctx != NULL ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) | ||||
|     return( ecdh_setup_internal( ctx, grp_id ) ); | ||||
|  | @ -218,8 +234,7 @@ static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx ) | |||
|  */ | ||||
| void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx ) | ||||
| { | ||||
|     if( ctx == NULL ) | ||||
|         return; | ||||
|     ECDH_VALIDATE( ctx != NULL ); | ||||
| 
 | ||||
|     ctx->restart_enabled = 1; | ||||
| } | ||||
|  | @ -318,9 +333,10 @@ int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen, | |||
|                               void *p_rng ) | ||||
| { | ||||
|     int restart_enabled = 0; | ||||
| 
 | ||||
|     if( ctx == NULL ) | ||||
|         return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); | ||||
|     ECDH_VALIDATE_RET( ctx != NULL ); | ||||
|     ECDH_VALIDATE_RET( olen != NULL ); | ||||
|     ECDH_VALIDATE_RET( buf != NULL ); | ||||
|     ECDH_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_ECP_RESTARTABLE) | ||||
|     restart_enabled = ctx->restart_enabled; | ||||
|  | @ -366,9 +382,10 @@ int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx, | |||
| { | ||||
|     int ret; | ||||
|     mbedtls_ecp_group_id grp_id; | ||||
| 
 | ||||
|     if( ctx == NULL ) | ||||
|         return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); | ||||
|     ECDH_VALIDATE_RET( ctx != NULL ); | ||||
|     ECDH_VALIDATE_RET( buf != NULL ); | ||||
|     ECDH_VALIDATE_RET( *buf != NULL ); | ||||
|     ECDH_VALIDATE_RET( end != NULL ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) ) | ||||
|             != 0 ) | ||||
|  | @ -420,9 +437,10 @@ int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, | |||
|                              mbedtls_ecdh_side side ) | ||||
| { | ||||
|     int ret; | ||||
| 
 | ||||
|     if( ctx == NULL ) | ||||
|         return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); | ||||
|     ECDH_VALIDATE_RET( ctx != NULL ); | ||||
|     ECDH_VALIDATE_RET( key != NULL ); | ||||
|     ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS || | ||||
|                        side == MBEDTLS_ECDH_THEIRS ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 ) | ||||
|         return( ret ); | ||||
|  | @ -488,9 +506,10 @@ int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen, | |||
|                               void *p_rng ) | ||||
| { | ||||
|     int restart_enabled = 0; | ||||
| 
 | ||||
|     if( ctx == NULL ) | ||||
|         return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); | ||||
|     ECDH_VALIDATE_RET( ctx != NULL ); | ||||
|     ECDH_VALIDATE_RET( olen != NULL ); | ||||
|     ECDH_VALIDATE_RET( buf != NULL ); | ||||
|     ECDH_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_ECP_RESTARTABLE) | ||||
|     restart_enabled = ctx->restart_enabled; | ||||
|  | @ -535,8 +554,8 @@ static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx, | |||
| int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx, | ||||
|                               const unsigned char *buf, size_t blen ) | ||||
| { | ||||
|     if( ctx == NULL ) | ||||
|         return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); | ||||
|     ECDH_VALIDATE_RET( ctx != NULL ); | ||||
|     ECDH_VALIDATE_RET( buf != NULL ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) | ||||
|     return( ecdh_read_public_internal( ctx, buf, blen ) ); | ||||
|  | @ -607,9 +626,9 @@ int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen, | |||
|                               void *p_rng ) | ||||
| { | ||||
|     int restart_enabled = 0; | ||||
| 
 | ||||
|     if( ctx == NULL ) | ||||
|         return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); | ||||
|     ECDH_VALIDATE_RET( ctx != NULL ); | ||||
|     ECDH_VALIDATE_RET( olen != NULL ); | ||||
|     ECDH_VALIDATE_RET( buf != NULL ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_ECP_RESTARTABLE) | ||||
|     restart_enabled = ctx->restart_enabled; | ||||
|  |  | |||
|  | @ -50,6 +50,14 @@ | |||
| #define mbedtls_free       free | ||||
| #endif | ||||
| 
 | ||||
| #include "mbedtls/platform_util.h" | ||||
| 
 | ||||
| /* Parameter validation macros based on platform_util.h */ | ||||
| #define ECDSA_VALIDATE_RET( cond )    \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA ) | ||||
| #define ECDSA_VALIDATE( cond )        \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| #if defined(MBEDTLS_ECP_RESTARTABLE) | ||||
| 
 | ||||
| /*
 | ||||
|  | @ -377,6 +385,13 @@ int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s, | |||
|                 const mbedtls_mpi *d, const unsigned char *buf, size_t blen, | ||||
|                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) | ||||
| { | ||||
|     ECDSA_VALIDATE_RET( grp   != NULL ); | ||||
|     ECDSA_VALIDATE_RET( r     != NULL ); | ||||
|     ECDSA_VALIDATE_RET( s     != NULL ); | ||||
|     ECDSA_VALIDATE_RET( d     != NULL ); | ||||
|     ECDSA_VALIDATE_RET( f_rng != NULL ); | ||||
|     ECDSA_VALIDATE_RET( buf   != NULL || blen == 0 ); | ||||
| 
 | ||||
|     return( ecdsa_sign_restartable( grp, r, s, d, buf, blen, | ||||
|                                     f_rng, p_rng, NULL ) ); | ||||
| } | ||||
|  | @ -456,6 +471,12 @@ int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi | |||
|                     const mbedtls_mpi *d, const unsigned char *buf, size_t blen, | ||||
|                     mbedtls_md_type_t md_alg ) | ||||
| { | ||||
|     ECDSA_VALIDATE_RET( grp   != NULL ); | ||||
|     ECDSA_VALIDATE_RET( r     != NULL ); | ||||
|     ECDSA_VALIDATE_RET( s     != NULL ); | ||||
|     ECDSA_VALIDATE_RET( d     != NULL ); | ||||
|     ECDSA_VALIDATE_RET( buf   != NULL || blen == 0 ); | ||||
| 
 | ||||
|     return( ecdsa_sign_det_restartable( grp, r, s, d, buf, blen, md_alg, NULL ) ); | ||||
| } | ||||
| #endif /* MBEDTLS_ECDSA_DETERMINISTIC */ | ||||
|  | @ -574,9 +595,17 @@ cleanup: | |||
|  * Verify ECDSA signature of hashed message | ||||
|  */ | ||||
| int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp, | ||||
|                   const unsigned char *buf, size_t blen, | ||||
|                   const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s) | ||||
|                           const unsigned char *buf, size_t blen, | ||||
|                           const mbedtls_ecp_point *Q, | ||||
|                           const mbedtls_mpi *r, | ||||
|                           const mbedtls_mpi *s) | ||||
| { | ||||
|     ECDSA_VALIDATE_RET( grp != NULL ); | ||||
|     ECDSA_VALIDATE_RET( Q   != NULL ); | ||||
|     ECDSA_VALIDATE_RET( r   != NULL ); | ||||
|     ECDSA_VALIDATE_RET( s   != NULL ); | ||||
|     ECDSA_VALIDATE_RET( buf != NULL || blen == 0 ); | ||||
| 
 | ||||
|     return( ecdsa_verify_restartable( grp, buf, blen, Q, r, s, NULL ) ); | ||||
| } | ||||
| #endif /* !MBEDTLS_ECDSA_VERIFY_ALT */ | ||||
|  | @ -618,6 +647,10 @@ int mbedtls_ecdsa_write_signature_restartable( mbedtls_ecdsa_context *ctx, | |||
| { | ||||
|     int ret; | ||||
|     mbedtls_mpi r, s; | ||||
|     ECDSA_VALIDATE_RET( ctx  != NULL ); | ||||
|     ECDSA_VALIDATE_RET( hash != NULL ); | ||||
|     ECDSA_VALIDATE_RET( sig  != NULL ); | ||||
|     ECDSA_VALIDATE_RET( slen != NULL ); | ||||
| 
 | ||||
|     mbedtls_mpi_init( &r ); | ||||
|     mbedtls_mpi_init( &s ); | ||||
|  | @ -652,12 +685,17 @@ cleanup: | |||
| /*
 | ||||
|  * Compute and write signature | ||||
|  */ | ||||
| int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg, | ||||
|                            const unsigned char *hash, size_t hlen, | ||||
|                            unsigned char *sig, size_t *slen, | ||||
|                            int (*f_rng)(void *, unsigned char *, size_t), | ||||
|                            void *p_rng ) | ||||
| int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, | ||||
|                                  mbedtls_md_type_t md_alg, | ||||
|                                  const unsigned char *hash, size_t hlen, | ||||
|                                  unsigned char *sig, size_t *slen, | ||||
|                                  int (*f_rng)(void *, unsigned char *, size_t), | ||||
|                                  void *p_rng ) | ||||
| { | ||||
|     ECDSA_VALIDATE_RET( ctx  != NULL ); | ||||
|     ECDSA_VALIDATE_RET( hash != NULL ); | ||||
|     ECDSA_VALIDATE_RET( sig  != NULL ); | ||||
|     ECDSA_VALIDATE_RET( slen != NULL ); | ||||
|     return( mbedtls_ecdsa_write_signature_restartable( | ||||
|                 ctx, md_alg, hash, hlen, sig, slen, f_rng, p_rng, NULL ) ); | ||||
| } | ||||
|  | @ -669,6 +707,10 @@ int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx, | |||
|                                unsigned char *sig, size_t *slen, | ||||
|                                mbedtls_md_type_t md_alg ) | ||||
| { | ||||
|     ECDSA_VALIDATE_RET( ctx  != NULL ); | ||||
|     ECDSA_VALIDATE_RET( hash != NULL ); | ||||
|     ECDSA_VALIDATE_RET( sig  != NULL ); | ||||
|     ECDSA_VALIDATE_RET( slen != NULL ); | ||||
|     return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen, | ||||
|                                    NULL, NULL ) ); | ||||
| } | ||||
|  | @ -681,6 +723,9 @@ int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx, | |||
|                           const unsigned char *hash, size_t hlen, | ||||
|                           const unsigned char *sig, size_t slen ) | ||||
| { | ||||
|     ECDSA_VALIDATE_RET( ctx  != NULL ); | ||||
|     ECDSA_VALIDATE_RET( hash != NULL ); | ||||
|     ECDSA_VALIDATE_RET( sig  != NULL ); | ||||
|     return( mbedtls_ecdsa_read_signature_restartable( | ||||
|                 ctx, hash, hlen, sig, slen, NULL ) ); | ||||
| } | ||||
|  | @ -698,6 +743,9 @@ int mbedtls_ecdsa_read_signature_restartable( mbedtls_ecdsa_context *ctx, | |||
|     const unsigned char *end = sig + slen; | ||||
|     size_t len; | ||||
|     mbedtls_mpi r, s; | ||||
|     ECDSA_VALIDATE_RET( ctx  != NULL ); | ||||
|     ECDSA_VALIDATE_RET( hash != NULL ); | ||||
|     ECDSA_VALIDATE_RET( sig  != NULL ); | ||||
| 
 | ||||
|     mbedtls_mpi_init( &r ); | ||||
|     mbedtls_mpi_init( &s ); | ||||
|  | @ -752,8 +800,16 @@ cleanup: | |||
| int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid, | ||||
|                   int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) | ||||
| { | ||||
|     return( mbedtls_ecp_group_load( &ctx->grp, gid ) || | ||||
|             mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) ); | ||||
|     int ret = 0; | ||||
|     ECDSA_VALIDATE_RET( ctx   != NULL ); | ||||
|     ECDSA_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
|     ret = mbedtls_ecp_group_load( &ctx->grp, gid ); | ||||
|     if( ret != 0 ) | ||||
|         return( ret ); | ||||
| 
 | ||||
|    return( mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d, | ||||
|                                     &ctx->Q, f_rng, p_rng ) ); | ||||
| } | ||||
| #endif /* !MBEDTLS_ECDSA_GENKEY_ALT */ | ||||
| 
 | ||||
|  | @ -763,6 +819,8 @@ int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid, | |||
| int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key ) | ||||
| { | ||||
|     int ret; | ||||
|     ECDSA_VALIDATE_RET( ctx != NULL ); | ||||
|     ECDSA_VALIDATE_RET( key != NULL ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 || | ||||
|         ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 || | ||||
|  | @ -779,6 +837,8 @@ int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_ke | |||
|  */ | ||||
| void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx ) | ||||
| { | ||||
|     ECDSA_VALIDATE( ctx != NULL ); | ||||
| 
 | ||||
|     mbedtls_ecp_keypair_init( ctx ); | ||||
| } | ||||
| 
 | ||||
|  | @ -787,6 +847,9 @@ void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx ) | |||
|  */ | ||||
| void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx ) | ||||
| { | ||||
|     if( ctx == NULL ) | ||||
|         return; | ||||
| 
 | ||||
|     mbedtls_ecp_keypair_free( ctx ); | ||||
| } | ||||
| 
 | ||||
|  | @ -796,6 +859,8 @@ void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx ) | |||
|  */ | ||||
| void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx ) | ||||
| { | ||||
|     ECDSA_VALIDATE( ctx != NULL ); | ||||
| 
 | ||||
|     mbedtls_ecp_restart_init( &ctx->ecp ); | ||||
| 
 | ||||
|     ctx->ver = NULL; | ||||
|  | @ -810,6 +875,9 @@ void mbedtls_ecdsa_restart_init( mbedtls_ecdsa_restart_ctx *ctx ) | |||
|  */ | ||||
| void mbedtls_ecdsa_restart_free( mbedtls_ecdsa_restart_ctx *ctx ) | ||||
| { | ||||
|     if( ctx == NULL ) | ||||
|         return; | ||||
| 
 | ||||
|     mbedtls_ecp_restart_free( &ctx->ecp ); | ||||
| 
 | ||||
|     ecdsa_restart_ver_free( ctx->ver ); | ||||
|  |  | |||
|  | @ -33,11 +33,18 @@ | |||
| #if defined(MBEDTLS_ECJPAKE_C) | ||||
| 
 | ||||
| #include "mbedtls/ecjpake.h" | ||||
| #include "mbedtls/platform_util.h" | ||||
| 
 | ||||
| #include <string.h> | ||||
| 
 | ||||
| #if !defined(MBEDTLS_ECJPAKE_ALT) | ||||
| 
 | ||||
| /* Parameter validation macros based on platform_util.h */ | ||||
| #define ECJPAKE_VALIDATE_RET( cond )    \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA ) | ||||
| #define ECJPAKE_VALIDATE( cond )        \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| /*
 | ||||
|  * Convert a mbedtls_ecjpake_role to identifier string | ||||
|  */ | ||||
|  | @ -54,8 +61,7 @@ static const char * const ecjpake_id[] = { | |||
|  */ | ||||
| void mbedtls_ecjpake_init( mbedtls_ecjpake_context *ctx ) | ||||
| { | ||||
|     if( ctx == NULL ) | ||||
|         return; | ||||
|     ECJPAKE_VALIDATE( ctx != NULL ); | ||||
| 
 | ||||
|     ctx->md_info = NULL; | ||||
|     mbedtls_ecp_group_init( &ctx->grp ); | ||||
|  | @ -106,6 +112,11 @@ int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx, | |||
| { | ||||
|     int ret; | ||||
| 
 | ||||
|     ECJPAKE_VALIDATE_RET( ctx != NULL ); | ||||
|     ECJPAKE_VALIDATE_RET( role == MBEDTLS_ECJPAKE_CLIENT || | ||||
|                           role == MBEDTLS_ECJPAKE_SERVER ); | ||||
|     ECJPAKE_VALIDATE_RET( secret != NULL || len == 0 ); | ||||
| 
 | ||||
|     ctx->role = role; | ||||
| 
 | ||||
|     if( ( ctx->md_info = mbedtls_md_info_from_type( hash ) ) == NULL ) | ||||
|  | @ -127,6 +138,8 @@ cleanup: | |||
|  */ | ||||
| int mbedtls_ecjpake_check( const mbedtls_ecjpake_context *ctx ) | ||||
| { | ||||
|     ECJPAKE_VALIDATE_RET( ctx != NULL ); | ||||
| 
 | ||||
|     if( ctx->md_info == NULL || | ||||
|         ctx->grp.id == MBEDTLS_ECP_DP_NONE || | ||||
|         ctx->s.p == NULL ) | ||||
|  | @ -504,6 +517,9 @@ int mbedtls_ecjpake_read_round_one( mbedtls_ecjpake_context *ctx, | |||
|                                     const unsigned char *buf, | ||||
|                                     size_t len ) | ||||
| { | ||||
|     ECJPAKE_VALIDATE_RET( ctx != NULL ); | ||||
|     ECJPAKE_VALIDATE_RET( buf != NULL ); | ||||
| 
 | ||||
|     return( ecjpake_kkpp_read( ctx->md_info, &ctx->grp, ctx->point_format, | ||||
|                                &ctx->grp.G, | ||||
|                                &ctx->Xp1, &ctx->Xp2, ID_PEER, | ||||
|  | @ -518,6 +534,11 @@ int mbedtls_ecjpake_write_round_one( mbedtls_ecjpake_context *ctx, | |||
|                             int (*f_rng)(void *, unsigned char *, size_t), | ||||
|                             void *p_rng ) | ||||
| { | ||||
|     ECJPAKE_VALIDATE_RET( ctx   != NULL ); | ||||
|     ECJPAKE_VALIDATE_RET( buf   != NULL ); | ||||
|     ECJPAKE_VALIDATE_RET( olen  != NULL ); | ||||
|     ECJPAKE_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
|     return( ecjpake_kkpp_write( ctx->md_info, &ctx->grp, ctx->point_format, | ||||
|                                 &ctx->grp.G, | ||||
|                                 &ctx->xm1, &ctx->Xm1, &ctx->xm2, &ctx->Xm2, | ||||
|  | @ -560,6 +581,9 @@ int mbedtls_ecjpake_read_round_two( mbedtls_ecjpake_context *ctx, | |||
|     mbedtls_ecp_group grp; | ||||
|     mbedtls_ecp_point G;    /* C: GB, S: GA */ | ||||
| 
 | ||||
|     ECJPAKE_VALIDATE_RET( ctx != NULL ); | ||||
|     ECJPAKE_VALIDATE_RET( buf != NULL ); | ||||
| 
 | ||||
|     mbedtls_ecp_group_init( &grp ); | ||||
|     mbedtls_ecp_point_init( &G ); | ||||
| 
 | ||||
|  | @ -652,6 +676,11 @@ int mbedtls_ecjpake_write_round_two( mbedtls_ecjpake_context *ctx, | |||
|     const unsigned char *end = buf + len; | ||||
|     size_t ec_len; | ||||
| 
 | ||||
|     ECJPAKE_VALIDATE_RET( ctx   != NULL ); | ||||
|     ECJPAKE_VALIDATE_RET( buf   != NULL ); | ||||
|     ECJPAKE_VALIDATE_RET( olen  != NULL ); | ||||
|     ECJPAKE_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
|     mbedtls_ecp_point_init( &G ); | ||||
|     mbedtls_ecp_point_init( &Xm ); | ||||
|     mbedtls_mpi_init( &xm ); | ||||
|  | @ -727,6 +756,11 @@ int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx, | |||
|     unsigned char kx[MBEDTLS_ECP_MAX_BYTES]; | ||||
|     size_t x_bytes; | ||||
| 
 | ||||
|     ECJPAKE_VALIDATE_RET( ctx   != NULL ); | ||||
|     ECJPAKE_VALIDATE_RET( buf   != NULL ); | ||||
|     ECJPAKE_VALIDATE_RET( olen  != NULL ); | ||||
|     ECJPAKE_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
|     *olen = mbedtls_md_get_size( ctx->md_info ); | ||||
|     if( len < *olen ) | ||||
|         return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); | ||||
|  | @ -917,7 +951,7 @@ static const unsigned char ecjpake_test_pms[] = { | |||
|     0xb4, 0x38, 0xf7, 0x19, 0xd3, 0xc4, 0xf3, 0x51 | ||||
| }; | ||||
| 
 | ||||
| /* Load my private keys and generate the correponding public keys */ | ||||
| /* Load my private keys and generate the corresponding public keys */ | ||||
| static int ecjpake_test_load( mbedtls_ecjpake_context *ctx, | ||||
|                               const unsigned char *xm1, size_t len1, | ||||
|                               const unsigned char *xm2, size_t len2 ) | ||||
|  |  | |||
							
								
								
									
										144
									
								
								library/ecp.c
									
									
									
									
									
								
							
							
						
						
									
										144
									
								
								library/ecp.c
									
									
									
									
									
								
							|  | @ -86,6 +86,12 @@ | |||
| 
 | ||||
| #if !defined(MBEDTLS_ECP_ALT) | ||||
| 
 | ||||
| /* Parameter validation macros based on platform_util.h */ | ||||
| #define ECP_VALIDATE_RET( cond )    \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA ) | ||||
| #define ECP_VALIDATE( cond )        \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| #if defined(MBEDTLS_PLATFORM_C) | ||||
| #include "mbedtls/platform.h" | ||||
| #else | ||||
|  | @ -238,6 +244,7 @@ static void ecp_restart_ma_free( mbedtls_ecp_restart_muladd_ctx *ctx ) | |||
|  */ | ||||
| void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx ) | ||||
| { | ||||
|     ECP_VALIDATE( ctx != NULL ); | ||||
|     ctx->ops_done = 0; | ||||
|     ctx->depth = 0; | ||||
|     ctx->rsm = NULL; | ||||
|  | @ -268,6 +275,8 @@ int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp, | |||
|                               mbedtls_ecp_restart_ctx *rs_ctx, | ||||
|                               unsigned ops ) | ||||
| { | ||||
|     ECP_VALIDATE_RET( grp != NULL ); | ||||
| 
 | ||||
|     if( rs_ctx != NULL && ecp_max_ops != 0 ) | ||||
|     { | ||||
|         /* scale depending on curve size: the chosen reference is 256-bit,
 | ||||
|  | @ -496,6 +505,9 @@ const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name | |||
| { | ||||
|     const mbedtls_ecp_curve_info *curve_info; | ||||
| 
 | ||||
|     if( name == NULL ) | ||||
|         return( NULL ); | ||||
| 
 | ||||
|     for( curve_info = mbedtls_ecp_curve_list(); | ||||
|          curve_info->grp_id != MBEDTLS_ECP_DP_NONE; | ||||
|          curve_info++ ) | ||||
|  | @ -526,8 +538,7 @@ static inline ecp_curve_type ecp_get_type( const mbedtls_ecp_group *grp ) | |||
|  */ | ||||
| void mbedtls_ecp_point_init( mbedtls_ecp_point *pt ) | ||||
| { | ||||
|     if( pt == NULL ) | ||||
|         return; | ||||
|     ECP_VALIDATE( pt != NULL ); | ||||
| 
 | ||||
|     mbedtls_mpi_init( &pt->X ); | ||||
|     mbedtls_mpi_init( &pt->Y ); | ||||
|  | @ -539,8 +550,7 @@ void mbedtls_ecp_point_init( mbedtls_ecp_point *pt ) | |||
|  */ | ||||
| void mbedtls_ecp_group_init( mbedtls_ecp_group *grp ) | ||||
| { | ||||
|     if( grp == NULL ) | ||||
|         return; | ||||
|     ECP_VALIDATE( grp != NULL ); | ||||
| 
 | ||||
|     grp->id = MBEDTLS_ECP_DP_NONE; | ||||
|     mbedtls_mpi_init( &grp->P ); | ||||
|  | @ -564,8 +574,7 @@ void mbedtls_ecp_group_init( mbedtls_ecp_group *grp ) | |||
|  */ | ||||
| void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key ) | ||||
| { | ||||
|     if( key == NULL ) | ||||
|         return; | ||||
|     ECP_VALIDATE( key != NULL ); | ||||
| 
 | ||||
|     mbedtls_ecp_group_init( &key->grp ); | ||||
|     mbedtls_mpi_init( &key->d ); | ||||
|  | @ -633,6 +642,8 @@ void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key ) | |||
| int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q ) | ||||
| { | ||||
|     int ret; | ||||
|     ECP_VALIDATE_RET( P != NULL ); | ||||
|     ECP_VALIDATE_RET( Q != NULL ); | ||||
| 
 | ||||
|     MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->X, &Q->X ) ); | ||||
|     MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Y, &Q->Y ) ); | ||||
|  | @ -647,7 +658,10 @@ cleanup: | |||
|  */ | ||||
| int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src ) | ||||
| { | ||||
|     return mbedtls_ecp_group_load( dst, src->id ); | ||||
|     ECP_VALIDATE_RET( dst != NULL ); | ||||
|     ECP_VALIDATE_RET( src != NULL ); | ||||
| 
 | ||||
|     return( mbedtls_ecp_group_load( dst, src->id ) ); | ||||
| } | ||||
| 
 | ||||
| /*
 | ||||
|  | @ -656,6 +670,7 @@ int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src | |||
| int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt ) | ||||
| { | ||||
|     int ret; | ||||
|     ECP_VALIDATE_RET( pt != NULL ); | ||||
| 
 | ||||
|     MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->X , 1 ) ); | ||||
|     MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Y , 1 ) ); | ||||
|  | @ -670,6 +685,8 @@ cleanup: | |||
|  */ | ||||
| int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt ) | ||||
| { | ||||
|     ECP_VALIDATE_RET( pt != NULL ); | ||||
| 
 | ||||
|     return( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 ); | ||||
| } | ||||
| 
 | ||||
|  | @ -679,6 +696,9 @@ int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt ) | |||
| int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P, | ||||
|                            const mbedtls_ecp_point *Q ) | ||||
| { | ||||
|     ECP_VALIDATE_RET( P != NULL ); | ||||
|     ECP_VALIDATE_RET( Q != NULL ); | ||||
| 
 | ||||
|     if( mbedtls_mpi_cmp_mpi( &P->X, &Q->X ) == 0 && | ||||
|         mbedtls_mpi_cmp_mpi( &P->Y, &Q->Y ) == 0 && | ||||
|         mbedtls_mpi_cmp_mpi( &P->Z, &Q->Z ) == 0 ) | ||||
|  | @ -696,6 +716,9 @@ int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix, | |||
|                            const char *x, const char *y ) | ||||
| { | ||||
|     int ret; | ||||
|     ECP_VALIDATE_RET( P != NULL ); | ||||
|     ECP_VALIDATE_RET( x != NULL ); | ||||
|     ECP_VALIDATE_RET( y != NULL ); | ||||
| 
 | ||||
|     MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->X, radix, x ) ); | ||||
|     MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->Y, radix, y ) ); | ||||
|  | @ -708,16 +731,19 @@ cleanup: | |||
| /*
 | ||||
|  * Export a point into unsigned binary data (SEC1 2.3.3) | ||||
|  */ | ||||
| int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P, | ||||
|                             int format, size_t *olen, | ||||
|                             unsigned char *buf, size_t buflen ) | ||||
| int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, | ||||
|                                     const mbedtls_ecp_point *P, | ||||
|                                     int format, size_t *olen, | ||||
|                                     unsigned char *buf, size_t buflen ) | ||||
| { | ||||
|     int ret = 0; | ||||
|     size_t plen; | ||||
| 
 | ||||
|     if( format != MBEDTLS_ECP_PF_UNCOMPRESSED && | ||||
|         format != MBEDTLS_ECP_PF_COMPRESSED ) | ||||
|         return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); | ||||
|     ECP_VALIDATE_RET( grp  != NULL ); | ||||
|     ECP_VALIDATE_RET( P    != NULL ); | ||||
|     ECP_VALIDATE_RET( olen != NULL ); | ||||
|     ECP_VALIDATE_RET( buf  != NULL ); | ||||
|     ECP_VALIDATE_RET( format == MBEDTLS_ECP_PF_UNCOMPRESSED || | ||||
|                       format == MBEDTLS_ECP_PF_COMPRESSED ); | ||||
| 
 | ||||
|     /*
 | ||||
|      * Common case: P == 0 | ||||
|  | @ -764,11 +790,15 @@ cleanup: | |||
| /*
 | ||||
|  * Import a point from unsigned binary data (SEC1 2.3.4) | ||||
|  */ | ||||
| int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt, | ||||
|                            const unsigned char *buf, size_t ilen ) | ||||
| int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, | ||||
|                                    mbedtls_ecp_point *pt, | ||||
|                                    const unsigned char *buf, size_t ilen ) | ||||
| { | ||||
|     int ret; | ||||
|     size_t plen; | ||||
|     ECP_VALIDATE_RET( grp != NULL ); | ||||
|     ECP_VALIDATE_RET( pt  != NULL ); | ||||
|     ECP_VALIDATE_RET( buf != NULL ); | ||||
| 
 | ||||
|     if( ilen < 1 ) | ||||
|         return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); | ||||
|  | @ -803,11 +833,16 @@ cleanup: | |||
|  *          opaque point <1..2^8-1>; | ||||
|  *      } ECPoint; | ||||
|  */ | ||||
| int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt, | ||||
|                         const unsigned char **buf, size_t buf_len ) | ||||
| int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, | ||||
|                                 mbedtls_ecp_point *pt, | ||||
|                                 const unsigned char **buf, size_t buf_len ) | ||||
| { | ||||
|     unsigned char data_len; | ||||
|     const unsigned char *buf_start; | ||||
|     ECP_VALIDATE_RET( grp != NULL ); | ||||
|     ECP_VALIDATE_RET( pt  != NULL ); | ||||
|     ECP_VALIDATE_RET( buf != NULL ); | ||||
|     ECP_VALIDATE_RET( *buf != NULL ); | ||||
| 
 | ||||
|     /*
 | ||||
|      * We must have at least two bytes (1 for length, at least one for data) | ||||
|  | @ -825,7 +860,7 @@ int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point | |||
|     buf_start = *buf; | ||||
|     *buf += data_len; | ||||
| 
 | ||||
|     return mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len ); | ||||
|     return( mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len ) ); | ||||
| } | ||||
| 
 | ||||
| /*
 | ||||
|  | @ -839,6 +874,12 @@ int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp | |||
|                          unsigned char *buf, size_t blen ) | ||||
| { | ||||
|     int ret; | ||||
|     ECP_VALIDATE_RET( grp  != NULL ); | ||||
|     ECP_VALIDATE_RET( pt   != NULL ); | ||||
|     ECP_VALIDATE_RET( olen != NULL ); | ||||
|     ECP_VALIDATE_RET( buf  != NULL ); | ||||
|     ECP_VALIDATE_RET( format == MBEDTLS_ECP_PF_UNCOMPRESSED || | ||||
|                       format == MBEDTLS_ECP_PF_COMPRESSED ); | ||||
| 
 | ||||
|     /*
 | ||||
|      * buffer length must be at least one, for our length byte | ||||
|  | @ -867,11 +908,14 @@ int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, | |||
| { | ||||
|     int ret; | ||||
|     mbedtls_ecp_group_id grp_id; | ||||
|     ECP_VALIDATE_RET( grp  != NULL ); | ||||
|     ECP_VALIDATE_RET( buf  != NULL ); | ||||
|     ECP_VALIDATE_RET( *buf != NULL ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, len ) ) != 0 ) | ||||
|         return( ret ); | ||||
| 
 | ||||
|     return mbedtls_ecp_group_load( grp, grp_id ); | ||||
|     return( mbedtls_ecp_group_load( grp, grp_id ) ); | ||||
| } | ||||
| 
 | ||||
| /*
 | ||||
|  | @ -883,6 +927,9 @@ int mbedtls_ecp_tls_read_group_id( mbedtls_ecp_group_id *grp, | |||
| { | ||||
|     uint16_t tls_id; | ||||
|     const mbedtls_ecp_curve_info *curve_info; | ||||
|     ECP_VALIDATE_RET( grp  != NULL ); | ||||
|     ECP_VALIDATE_RET( buf  != NULL ); | ||||
|     ECP_VALIDATE_RET( *buf != NULL ); | ||||
| 
 | ||||
|     /*
 | ||||
|      * We expect at least three bytes (see below) | ||||
|  | @ -918,6 +965,9 @@ int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen, | |||
|                          unsigned char *buf, size_t blen ) | ||||
| { | ||||
|     const mbedtls_ecp_curve_info *curve_info; | ||||
|     ECP_VALIDATE_RET( grp  != NULL ); | ||||
|     ECP_VALIDATE_RET( buf  != NULL ); | ||||
|     ECP_VALIDATE_RET( olen != NULL ); | ||||
| 
 | ||||
|     if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id( grp->id ) ) == NULL ) | ||||
|         return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); | ||||
|  | @ -2276,6 +2326,10 @@ int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, | |||
| #if defined(MBEDTLS_ECP_INTERNAL_ALT) | ||||
|     char is_grp_capable = 0; | ||||
| #endif | ||||
|     ECP_VALIDATE_RET( grp != NULL ); | ||||
|     ECP_VALIDATE_RET( R   != NULL ); | ||||
|     ECP_VALIDATE_RET( m   != NULL ); | ||||
|     ECP_VALIDATE_RET( P   != NULL ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_ECP_RESTARTABLE) | ||||
|     /* reset ops count for this call if top-level */ | ||||
|  | @ -2333,6 +2387,10 @@ int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, | |||
|              const mbedtls_mpi *m, const mbedtls_ecp_point *P, | ||||
|              int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) | ||||
| { | ||||
|     ECP_VALIDATE_RET( grp != NULL ); | ||||
|     ECP_VALIDATE_RET( R   != NULL ); | ||||
|     ECP_VALIDATE_RET( m   != NULL ); | ||||
|     ECP_VALIDATE_RET( P   != NULL ); | ||||
|     return( mbedtls_ecp_mul_restartable( grp, R, m, P, f_rng, p_rng, NULL ) ); | ||||
| } | ||||
| 
 | ||||
|  | @ -2435,6 +2493,12 @@ int mbedtls_ecp_muladd_restartable( | |||
| #if defined(MBEDTLS_ECP_INTERNAL_ALT) | ||||
|     char is_grp_capable = 0; | ||||
| #endif | ||||
|     ECP_VALIDATE_RET( grp != NULL ); | ||||
|     ECP_VALIDATE_RET( R   != NULL ); | ||||
|     ECP_VALIDATE_RET( m   != NULL ); | ||||
|     ECP_VALIDATE_RET( P   != NULL ); | ||||
|     ECP_VALIDATE_RET( n   != NULL ); | ||||
|     ECP_VALIDATE_RET( Q   != NULL ); | ||||
| 
 | ||||
|     if( ecp_get_type( grp ) != ECP_TYPE_SHORT_WEIERSTRASS ) | ||||
|         return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ); | ||||
|  | @ -2517,6 +2581,12 @@ int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R, | |||
|              const mbedtls_mpi *m, const mbedtls_ecp_point *P, | ||||
|              const mbedtls_mpi *n, const mbedtls_ecp_point *Q ) | ||||
| { | ||||
|     ECP_VALIDATE_RET( grp != NULL ); | ||||
|     ECP_VALIDATE_RET( R   != NULL ); | ||||
|     ECP_VALIDATE_RET( m   != NULL ); | ||||
|     ECP_VALIDATE_RET( P   != NULL ); | ||||
|     ECP_VALIDATE_RET( n   != NULL ); | ||||
|     ECP_VALIDATE_RET( Q   != NULL ); | ||||
|     return( mbedtls_ecp_muladd_restartable( grp, R, m, P, n, Q, NULL ) ); | ||||
| } | ||||
| 
 | ||||
|  | @ -2539,8 +2609,12 @@ static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_ | |||
| /*
 | ||||
|  * Check that a point is valid as a public key | ||||
|  */ | ||||
| int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt ) | ||||
| int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, | ||||
|                               const mbedtls_ecp_point *pt ) | ||||
| { | ||||
|     ECP_VALIDATE_RET( grp != NULL ); | ||||
|     ECP_VALIDATE_RET( pt  != NULL ); | ||||
| 
 | ||||
|     /* Must use affine coordinates */ | ||||
|     if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 ) | ||||
|         return( MBEDTLS_ERR_ECP_INVALID_KEY ); | ||||
|  | @ -2559,8 +2633,12 @@ int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_po | |||
| /*
 | ||||
|  * Check that an mbedtls_mpi is valid as a private key | ||||
|  */ | ||||
| int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d ) | ||||
| int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, | ||||
|                                const mbedtls_mpi *d ) | ||||
| { | ||||
|     ECP_VALIDATE_RET( grp != NULL ); | ||||
|     ECP_VALIDATE_RET( d   != NULL ); | ||||
| 
 | ||||
| #if defined(ECP_MONTGOMERY) | ||||
|     if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY ) | ||||
|     { | ||||
|  | @ -2601,7 +2679,13 @@ int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp, | |||
|                      void *p_rng ) | ||||
| { | ||||
|     int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; | ||||
|     size_t n_size = ( grp->nbits + 7 ) / 8; | ||||
|     size_t n_size; | ||||
| 
 | ||||
|     ECP_VALIDATE_RET( grp   != NULL ); | ||||
|     ECP_VALIDATE_RET( d     != NULL ); | ||||
|     ECP_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
|     n_size = ( grp->nbits + 7 ) / 8; | ||||
| 
 | ||||
| #if defined(ECP_MONTGOMERY) | ||||
|     if( ecp_get_type( grp ) == ECP_TYPE_MONTGOMERY ) | ||||
|  | @ -2680,6 +2764,11 @@ int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp, | |||
|                      void *p_rng ) | ||||
| { | ||||
|     int ret; | ||||
|     ECP_VALIDATE_RET( grp   != NULL ); | ||||
|     ECP_VALIDATE_RET( d     != NULL ); | ||||
|     ECP_VALIDATE_RET( G     != NULL ); | ||||
|     ECP_VALIDATE_RET( Q     != NULL ); | ||||
|     ECP_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
|     MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) ); | ||||
|     MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, Q, d, G, f_rng, p_rng ) ); | ||||
|  | @ -2696,6 +2785,11 @@ int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, | |||
|                              int (*f_rng)(void *, unsigned char *, size_t), | ||||
|                              void *p_rng ) | ||||
| { | ||||
|     ECP_VALIDATE_RET( grp   != NULL ); | ||||
|     ECP_VALIDATE_RET( d     != NULL ); | ||||
|     ECP_VALIDATE_RET( Q     != NULL ); | ||||
|     ECP_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
|     return( mbedtls_ecp_gen_keypair_base( grp, &grp->G, d, Q, f_rng, p_rng ) ); | ||||
| } | ||||
| 
 | ||||
|  | @ -2706,6 +2800,8 @@ int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key, | |||
|                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) | ||||
| { | ||||
|     int ret; | ||||
|     ECP_VALIDATE_RET( key   != NULL ); | ||||
|     ECP_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_ecp_group_load( &key->grp, grp_id ) ) != 0 ) | ||||
|         return( ret ); | ||||
|  | @ -2721,6 +2817,8 @@ int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ec | |||
|     int ret; | ||||
|     mbedtls_ecp_point Q; | ||||
|     mbedtls_ecp_group grp; | ||||
|     ECP_VALIDATE_RET( pub != NULL ); | ||||
|     ECP_VALIDATE_RET( prv != NULL ); | ||||
| 
 | ||||
|     if( pub->grp.id == MBEDTLS_ECP_DP_NONE || | ||||
|         pub->grp.id != prv->grp.id || | ||||
|  |  | |||
|  | @ -28,11 +28,18 @@ | |||
| #if defined(MBEDTLS_ECP_C) | ||||
| 
 | ||||
| #include "mbedtls/ecp.h" | ||||
| #include "mbedtls/platform_util.h" | ||||
| 
 | ||||
| #include <string.h> | ||||
| 
 | ||||
| #if !defined(MBEDTLS_ECP_ALT) | ||||
| 
 | ||||
| /* Parameter validation macros based on platform_util.h */ | ||||
| #define ECP_VALIDATE_RET( cond )    \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA ) | ||||
| #define ECP_VALIDATE( cond )        \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ | ||||
|     !defined(inline) && !defined(__cplusplus) | ||||
| #define inline __inline | ||||
|  | @ -746,6 +753,7 @@ cleanup: | |||
|  */ | ||||
| int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id ) | ||||
| { | ||||
|     ECP_VALIDATE_RET( grp != NULL ); | ||||
|     mbedtls_ecp_group_free( grp ); | ||||
| 
 | ||||
|     grp->id = id; | ||||
|  |  | |||
|  | @ -567,7 +567,7 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen ) | |||
|         if( use_ret == -(MBEDTLS_ERR_X509_BUFFER_TOO_SMALL) ) | ||||
|             mbedtls_snprintf( buf, buflen, "X509 - Destination buffer is too small" ); | ||||
|         if( use_ret == -(MBEDTLS_ERR_X509_FATAL_ERROR) ) | ||||
|             mbedtls_snprintf( buf, buflen, "X509 - A fatal error occured, eg the chain is too long or the vrfy callback failed" ); | ||||
|             mbedtls_snprintf( buf, buflen, "X509 - A fatal error occurred, eg the chain is too long or the vrfy callback failed" ); | ||||
| #endif /* MBEDTLS_X509_USE_C || MBEDTLS_X509_CREATE_C */ | ||||
|         // END generated code
 | ||||
| 
 | ||||
|  | @ -618,8 +618,8 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen ) | |||
| #endif /* MBEDTLS_ARC4_C */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_ARIA_C) | ||||
|     if( use_ret == -(MBEDTLS_ERR_ARIA_INVALID_KEY_LENGTH) ) | ||||
|         mbedtls_snprintf( buf, buflen, "ARIA - Invalid key length" ); | ||||
|     if( use_ret == -(MBEDTLS_ERR_ARIA_BAD_INPUT_DATA) ) | ||||
|         mbedtls_snprintf( buf, buflen, "ARIA - Bad input data" ); | ||||
|     if( use_ret == -(MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH) ) | ||||
|         mbedtls_snprintf( buf, buflen, "ARIA - Invalid data input length" ); | ||||
|     if( use_ret == -(MBEDTLS_ERR_ARIA_FEATURE_UNAVAILABLE) ) | ||||
|  | @ -672,17 +672,17 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen ) | |||
| #endif /* MBEDTLS_BIGNUM_C */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_BLOWFISH_C) | ||||
|     if( use_ret == -(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH) ) | ||||
|         mbedtls_snprintf( buf, buflen, "BLOWFISH - Invalid key length" ); | ||||
|     if( use_ret == -(MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED) ) | ||||
|         mbedtls_snprintf( buf, buflen, "BLOWFISH - Blowfish hardware accelerator failed" ); | ||||
|     if( use_ret == -(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA) ) | ||||
|         mbedtls_snprintf( buf, buflen, "BLOWFISH - Bad input data" ); | ||||
|     if( use_ret == -(MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH) ) | ||||
|         mbedtls_snprintf( buf, buflen, "BLOWFISH - Invalid data input length" ); | ||||
|     if( use_ret == -(MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED) ) | ||||
|         mbedtls_snprintf( buf, buflen, "BLOWFISH - Blowfish hardware accelerator failed" ); | ||||
| #endif /* MBEDTLS_BLOWFISH_C */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_CAMELLIA_C) | ||||
|     if( use_ret == -(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH) ) | ||||
|         mbedtls_snprintf( buf, buflen, "CAMELLIA - Invalid key length" ); | ||||
|     if( use_ret == -(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA) ) | ||||
|         mbedtls_snprintf( buf, buflen, "CAMELLIA - Bad input data" ); | ||||
|     if( use_ret == -(MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH) ) | ||||
|         mbedtls_snprintf( buf, buflen, "CAMELLIA - Invalid data input length" ); | ||||
|     if( use_ret == -(MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED) ) | ||||
|  | @ -855,16 +855,22 @@ void mbedtls_strerror( int ret, char *buf, size_t buflen ) | |||
| #if defined(MBEDTLS_SHA1_C) | ||||
|     if( use_ret == -(MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED) ) | ||||
|         mbedtls_snprintf( buf, buflen, "SHA1 - SHA-1 hardware accelerator failed" ); | ||||
|     if( use_ret == -(MBEDTLS_ERR_SHA1_BAD_INPUT_DATA) ) | ||||
|         mbedtls_snprintf( buf, buflen, "SHA1 - SHA-1 input data was malformed" ); | ||||
| #endif /* MBEDTLS_SHA1_C */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_SHA256_C) | ||||
|     if( use_ret == -(MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED) ) | ||||
|         mbedtls_snprintf( buf, buflen, "SHA256 - SHA-256 hardware accelerator failed" ); | ||||
|     if( use_ret == -(MBEDTLS_ERR_SHA256_BAD_INPUT_DATA) ) | ||||
|         mbedtls_snprintf( buf, buflen, "SHA256 - SHA-256 input data was malformed" ); | ||||
| #endif /* MBEDTLS_SHA256_C */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_SHA512_C) | ||||
|     if( use_ret == -(MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED) ) | ||||
|         mbedtls_snprintf( buf, buflen, "SHA512 - SHA-512 hardware accelerator failed" ); | ||||
|     if( use_ret == -(MBEDTLS_ERR_SHA512_BAD_INPUT_DATA) ) | ||||
|         mbedtls_snprintf( buf, buflen, "SHA512 - SHA-512 input data was malformed" ); | ||||
| #endif /* MBEDTLS_SHA512_C */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_THREADING_C) | ||||
|  |  | |||
|  | @ -57,6 +57,12 @@ | |||
| 
 | ||||
| #if !defined(MBEDTLS_GCM_ALT) | ||||
| 
 | ||||
| /* Parameter validation macros */ | ||||
| #define GCM_VALIDATE_RET( cond ) \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_GCM_BAD_INPUT ) | ||||
| #define GCM_VALIDATE( cond ) \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| /*
 | ||||
|  * 32-bit integer manipulation macros (big endian) | ||||
|  */ | ||||
|  | @ -85,6 +91,7 @@ | |||
|  */ | ||||
| void mbedtls_gcm_init( mbedtls_gcm_context *ctx ) | ||||
| { | ||||
|     GCM_VALIDATE( ctx != NULL ); | ||||
|     memset( ctx, 0, sizeof( mbedtls_gcm_context ) ); | ||||
| } | ||||
| 
 | ||||
|  | @ -164,6 +171,10 @@ int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx, | |||
|     int ret; | ||||
|     const mbedtls_cipher_info_t *cipher_info; | ||||
| 
 | ||||
|     GCM_VALIDATE_RET( ctx != NULL ); | ||||
|     GCM_VALIDATE_RET( key != NULL ); | ||||
|     GCM_VALIDATE_RET( keybits == 128 || keybits == 192 || keybits == 256 ); | ||||
| 
 | ||||
|     cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, MBEDTLS_MODE_ECB ); | ||||
|     if( cipher_info == NULL ) | ||||
|         return( MBEDTLS_ERR_GCM_BAD_INPUT ); | ||||
|  | @ -274,6 +285,10 @@ int mbedtls_gcm_starts( mbedtls_gcm_context *ctx, | |||
|     const unsigned char *p; | ||||
|     size_t use_len, olen = 0; | ||||
| 
 | ||||
|     GCM_VALIDATE_RET( ctx != NULL ); | ||||
|     GCM_VALIDATE_RET( iv != NULL ); | ||||
|     GCM_VALIDATE_RET( add_len == 0 || add != NULL ); | ||||
| 
 | ||||
|     /* IV and AD are limited to 2^64 bits, so 2^61 bytes */ | ||||
|     /* IV is not allowed to be zero length */ | ||||
|     if( iv_len == 0 || | ||||
|  | @ -356,6 +371,10 @@ int mbedtls_gcm_update( mbedtls_gcm_context *ctx, | |||
|     unsigned char *out_p = output; | ||||
|     size_t use_len, olen = 0; | ||||
| 
 | ||||
|     GCM_VALIDATE_RET( ctx != NULL ); | ||||
|     GCM_VALIDATE_RET( length == 0 || input != NULL ); | ||||
|     GCM_VALIDATE_RET( length == 0 || output != NULL ); | ||||
| 
 | ||||
|     if( output > input && (size_t) ( output - input ) < length ) | ||||
|         return( MBEDTLS_ERR_GCM_BAD_INPUT ); | ||||
| 
 | ||||
|  | @ -409,8 +428,14 @@ int mbedtls_gcm_finish( mbedtls_gcm_context *ctx, | |||
| { | ||||
|     unsigned char work_buf[16]; | ||||
|     size_t i; | ||||
|     uint64_t orig_len = ctx->len * 8; | ||||
|     uint64_t orig_add_len = ctx->add_len * 8; | ||||
|     uint64_t orig_len; | ||||
|     uint64_t orig_add_len; | ||||
| 
 | ||||
|     GCM_VALIDATE_RET( ctx != NULL ); | ||||
|     GCM_VALIDATE_RET( tag != NULL ); | ||||
| 
 | ||||
|     orig_len = ctx->len * 8; | ||||
|     orig_add_len = ctx->add_len * 8; | ||||
| 
 | ||||
|     if( tag_len > 16 || tag_len < 4 ) | ||||
|         return( MBEDTLS_ERR_GCM_BAD_INPUT ); | ||||
|  | @ -452,6 +477,13 @@ int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx, | |||
| { | ||||
|     int ret; | ||||
| 
 | ||||
|     GCM_VALIDATE_RET( ctx != NULL ); | ||||
|     GCM_VALIDATE_RET( iv != NULL ); | ||||
|     GCM_VALIDATE_RET( add_len == 0 || add != NULL ); | ||||
|     GCM_VALIDATE_RET( length == 0 || input != NULL ); | ||||
|     GCM_VALIDATE_RET( length == 0 || output != NULL ); | ||||
|     GCM_VALIDATE_RET( tag != NULL ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_gcm_starts( ctx, mode, iv, iv_len, add, add_len ) ) != 0 ) | ||||
|         return( ret ); | ||||
| 
 | ||||
|  | @ -480,6 +512,13 @@ int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx, | |||
|     size_t i; | ||||
|     int diff; | ||||
| 
 | ||||
|     GCM_VALIDATE_RET( ctx != NULL ); | ||||
|     GCM_VALIDATE_RET( iv != NULL ); | ||||
|     GCM_VALIDATE_RET( add_len == 0 || add != NULL ); | ||||
|     GCM_VALIDATE_RET( tag != NULL ); | ||||
|     GCM_VALIDATE_RET( length == 0 || input != NULL ); | ||||
|     GCM_VALIDATE_RET( length == 0 || output != NULL ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_gcm_crypt_and_tag( ctx, MBEDTLS_GCM_DECRYPT, length, | ||||
|                                    iv, iv_len, add, add_len, | ||||
|                                    input, output, tag_len, check_tag ) ) != 0 ) | ||||
|  | @ -502,6 +541,8 @@ int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx, | |||
| 
 | ||||
| void mbedtls_gcm_free( mbedtls_gcm_context *ctx ) | ||||
| { | ||||
|     if( ctx == NULL ) | ||||
|         return; | ||||
|     mbedtls_cipher_free( &ctx->cipher_ctx ); | ||||
|     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_gcm_context ) ); | ||||
| } | ||||
|  |  | |||
|  | @ -311,7 +311,7 @@ cleanup: | |||
|     } | ||||
|     mbedtls_platform_zeroize( inbuff, KW_SEMIBLOCK_LENGTH * 2 ); | ||||
|     mbedtls_platform_zeroize( outbuff, KW_SEMIBLOCK_LENGTH * 2 ); | ||||
|     mbedtls_cipher_finish( &ctx->cipher_ctx, NULL, &olen ); | ||||
| 
 | ||||
|     return( ret ); | ||||
| } | ||||
| 
 | ||||
|  | @ -528,7 +528,7 @@ cleanup: | |||
|     mbedtls_platform_zeroize( &bad_padding, sizeof( bad_padding) ); | ||||
|     mbedtls_platform_zeroize( &diff, sizeof( diff ) ); | ||||
|     mbedtls_platform_zeroize( A, sizeof( A ) ); | ||||
|     mbedtls_cipher_finish( &ctx->cipher_ctx, NULL, &olen ); | ||||
| 
 | ||||
|     return( ret ); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -423,9 +423,11 @@ int mbedtls_pem_read_buffer( mbedtls_pem_context *ctx, const char *header, const | |||
| 
 | ||||
| void mbedtls_pem_free( mbedtls_pem_context *ctx ) | ||||
| { | ||||
|     if( ctx->buf != NULL ) | ||||
|     if ( ctx->buf != NULL ) | ||||
|     { | ||||
|         mbedtls_platform_zeroize( ctx->buf, ctx->buflen ); | ||||
|     mbedtls_free( ctx->buf ); | ||||
|         mbedtls_free( ctx->buf ); | ||||
|     } | ||||
|     mbedtls_free( ctx->info ); | ||||
| 
 | ||||
|     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pem_context ) ); | ||||
|  |  | |||
							
								
								
									
										72
									
								
								library/pk.c
									
									
									
									
									
								
							
							
						
						
									
										72
									
								
								library/pk.c
									
									
									
									
									
								
							|  | @ -48,13 +48,18 @@ | |||
| #include <limits.h> | ||||
| #include <stdint.h> | ||||
| 
 | ||||
| /* Parameter validation macros based on platform_util.h */ | ||||
| #define PK_VALIDATE_RET( cond )    \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA ) | ||||
| #define PK_VALIDATE( cond )        \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| /*
 | ||||
|  * Initialise a mbedtls_pk_context | ||||
|  */ | ||||
| void mbedtls_pk_init( mbedtls_pk_context *ctx ) | ||||
| { | ||||
|     if( ctx == NULL ) | ||||
|         return; | ||||
|     PK_VALIDATE( ctx != NULL ); | ||||
| 
 | ||||
|     ctx->pk_info = NULL; | ||||
|     ctx->pk_ctx = NULL; | ||||
|  | @ -65,10 +70,11 @@ void mbedtls_pk_init( mbedtls_pk_context *ctx ) | |||
|  */ | ||||
| void mbedtls_pk_free( mbedtls_pk_context *ctx ) | ||||
| { | ||||
|     if( ctx == NULL || ctx->pk_info == NULL ) | ||||
|     if( ctx == NULL ) | ||||
|         return; | ||||
| 
 | ||||
|     ctx->pk_info->ctx_free_func( ctx->pk_ctx ); | ||||
|     if ( ctx->pk_info != NULL ) | ||||
|         ctx->pk_info->ctx_free_func( ctx->pk_ctx ); | ||||
| 
 | ||||
|     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_pk_context ) ); | ||||
| } | ||||
|  | @ -79,6 +85,7 @@ void mbedtls_pk_free( mbedtls_pk_context *ctx ) | |||
|  */ | ||||
| void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx ) | ||||
| { | ||||
|     PK_VALIDATE( ctx != NULL ); | ||||
|     ctx->pk_info = NULL; | ||||
|     ctx->rs_ctx = NULL; | ||||
| } | ||||
|  | @ -132,7 +139,8 @@ const mbedtls_pk_info_t * mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type ) | |||
|  */ | ||||
| int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info ) | ||||
| { | ||||
|     if( ctx == NULL || info == NULL || ctx->pk_info != NULL ) | ||||
|     PK_VALIDATE_RET( ctx != NULL ); | ||||
|     if( info == NULL || ctx->pk_info != NULL ) | ||||
|         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL ) | ||||
|  | @ -187,7 +195,8 @@ int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key, | |||
|     mbedtls_rsa_alt_context *rsa_alt; | ||||
|     const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info; | ||||
| 
 | ||||
|     if( ctx == NULL || ctx->pk_info != NULL ) | ||||
|     PK_VALIDATE_RET( ctx != NULL ); | ||||
|     if( ctx->pk_info != NULL ) | ||||
|         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( ( ctx->pk_ctx = info->ctx_alloc_func() ) == NULL ) | ||||
|  | @ -211,7 +220,9 @@ int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key, | |||
|  */ | ||||
| int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type ) | ||||
| { | ||||
|     /* null or NONE context can't do anything */ | ||||
|     /* A context with null pk_info is not set up yet and can't do anything.
 | ||||
|      * For backward compatibility, also accept NULL instead of a context | ||||
|      * pointer. */ | ||||
|     if( ctx == NULL || ctx->pk_info == NULL ) | ||||
|         return( 0 ); | ||||
| 
 | ||||
|  | @ -268,7 +279,12 @@ int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx, | |||
|                const unsigned char *sig, size_t sig_len, | ||||
|                mbedtls_pk_restart_ctx *rs_ctx ) | ||||
| { | ||||
|     if( ctx == NULL || ctx->pk_info == NULL || | ||||
|     PK_VALIDATE_RET( ctx != NULL ); | ||||
|     PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) || | ||||
|                      hash != NULL ); | ||||
|     PK_VALIDATE_RET( sig != NULL ); | ||||
| 
 | ||||
|     if( ctx->pk_info == NULL || | ||||
|         pk_hashlen_helper( md_alg, &hash_len ) != 0 ) | ||||
|         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|  | @ -321,7 +337,12 @@ int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options, | |||
|                    const unsigned char *hash, size_t hash_len, | ||||
|                    const unsigned char *sig, size_t sig_len ) | ||||
| { | ||||
|     if( ctx == NULL || ctx->pk_info == NULL ) | ||||
|     PK_VALIDATE_RET( ctx != NULL ); | ||||
|     PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) || | ||||
|                      hash != NULL ); | ||||
|     PK_VALIDATE_RET( sig != NULL ); | ||||
| 
 | ||||
|     if( ctx->pk_info == NULL ) | ||||
|         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( ! mbedtls_pk_can_do( ctx, type ) ) | ||||
|  | @ -381,7 +402,12 @@ int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx, | |||
|              int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, | ||||
|              mbedtls_pk_restart_ctx *rs_ctx ) | ||||
| { | ||||
|     if( ctx == NULL || ctx->pk_info == NULL || | ||||
|     PK_VALIDATE_RET( ctx != NULL ); | ||||
|     PK_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && hash_len == 0 ) || | ||||
|                      hash != NULL ); | ||||
|     PK_VALIDATE_RET( sig != NULL ); | ||||
| 
 | ||||
|     if( ctx->pk_info == NULL || | ||||
|         pk_hashlen_helper( md_alg, &hash_len ) != 0 ) | ||||
|         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|  | @ -435,7 +461,12 @@ int mbedtls_pk_decrypt( mbedtls_pk_context *ctx, | |||
|                 unsigned char *output, size_t *olen, size_t osize, | ||||
|                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) | ||||
| { | ||||
|     if( ctx == NULL || ctx->pk_info == NULL ) | ||||
|     PK_VALIDATE_RET( ctx != NULL ); | ||||
|     PK_VALIDATE_RET( input != NULL || ilen == 0 ); | ||||
|     PK_VALIDATE_RET( output != NULL || osize == 0 ); | ||||
|     PK_VALIDATE_RET( olen != NULL ); | ||||
| 
 | ||||
|     if( ctx->pk_info == NULL ) | ||||
|         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( ctx->pk_info->decrypt_func == NULL ) | ||||
|  | @ -453,7 +484,12 @@ int mbedtls_pk_encrypt( mbedtls_pk_context *ctx, | |||
|                 unsigned char *output, size_t *olen, size_t osize, | ||||
|                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) | ||||
| { | ||||
|     if( ctx == NULL || ctx->pk_info == NULL ) | ||||
|     PK_VALIDATE_RET( ctx != NULL ); | ||||
|     PK_VALIDATE_RET( input != NULL || ilen == 0 ); | ||||
|     PK_VALIDATE_RET( output != NULL || osize == 0 ); | ||||
|     PK_VALIDATE_RET( olen != NULL ); | ||||
| 
 | ||||
|     if( ctx->pk_info == NULL ) | ||||
|         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( ctx->pk_info->encrypt_func == NULL ) | ||||
|  | @ -468,8 +504,11 @@ int mbedtls_pk_encrypt( mbedtls_pk_context *ctx, | |||
|  */ | ||||
| int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv ) | ||||
| { | ||||
|     if( pub == NULL || pub->pk_info == NULL || | ||||
|         prv == NULL || prv->pk_info == NULL ) | ||||
|     PK_VALIDATE_RET( pub != NULL ); | ||||
|     PK_VALIDATE_RET( prv != NULL ); | ||||
| 
 | ||||
|     if( pub->pk_info == NULL || | ||||
|         prv->pk_info == NULL ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | ||||
|     } | ||||
|  | @ -496,6 +535,8 @@ int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_conte | |||
|  */ | ||||
| size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx ) | ||||
| { | ||||
|     /* For backward compatibility, accept NULL or a context that
 | ||||
|      * isn't set up yet, and return a fake value that should be safe. */ | ||||
|     if( ctx == NULL || ctx->pk_info == NULL ) | ||||
|         return( 0 ); | ||||
| 
 | ||||
|  | @ -507,7 +548,8 @@ size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx ) | |||
|  */ | ||||
| int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items ) | ||||
| { | ||||
|     if( ctx == NULL || ctx->pk_info == NULL ) | ||||
|     PK_VALIDATE_RET( ctx != NULL ); | ||||
|     if( ctx->pk_info == NULL ) | ||||
|         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( ctx->pk_info->debug_func == NULL ) | ||||
|  |  | |||
|  | @ -564,7 +564,7 @@ static int ecdsa_verify_wrap( void *ctx, mbedtls_md_type_t md_alg, | |||
|         return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     /* mbedtls_pk_write_pubkey() expects a full PK context;
 | ||||
|      * re-construct one to make it happy. */ | ||||
|      * re-construct one to make it happy */ | ||||
|     key.pk_info = &pk_info; | ||||
|     key.pk_ctx = ctx; | ||||
|     p = buf + sizeof( buf ); | ||||
|  |  | |||
|  | @ -61,6 +61,12 @@ | |||
| #define mbedtls_free       free | ||||
| #endif | ||||
| 
 | ||||
| /* Parameter validation macros based on platform_util.h */ | ||||
| #define PK_VALIDATE_RET( cond )    \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA ) | ||||
| #define PK_VALIDATE( cond )        \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| #if defined(MBEDTLS_FS_IO) | ||||
| /*
 | ||||
|  * Load all data from a file into a given buffer. | ||||
|  | @ -74,6 +80,10 @@ int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n ) | |||
|     FILE *f; | ||||
|     long size; | ||||
| 
 | ||||
|     PK_VALIDATE_RET( path != NULL ); | ||||
|     PK_VALIDATE_RET( buf != NULL ); | ||||
|     PK_VALIDATE_RET( n != NULL ); | ||||
| 
 | ||||
|     if( ( f = fopen( path, "rb" ) ) == NULL ) | ||||
|         return( MBEDTLS_ERR_PK_FILE_IO_ERROR ); | ||||
| 
 | ||||
|  | @ -124,6 +134,9 @@ int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx, | |||
|     size_t n; | ||||
|     unsigned char *buf; | ||||
| 
 | ||||
|     PK_VALIDATE_RET( ctx != NULL ); | ||||
|     PK_VALIDATE_RET( path != NULL ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 ) | ||||
|         return( ret ); | ||||
| 
 | ||||
|  | @ -148,6 +161,9 @@ int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path ) | |||
|     size_t n; | ||||
|     unsigned char *buf; | ||||
| 
 | ||||
|     PK_VALIDATE_RET( ctx != NULL ); | ||||
|     PK_VALIDATE_RET( path != NULL ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 ) | ||||
|         return( ret ); | ||||
| 
 | ||||
|  | @ -605,6 +621,11 @@ int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end, | |||
|     mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE; | ||||
|     const mbedtls_pk_info_t *pk_info; | ||||
| 
 | ||||
|     PK_VALIDATE_RET( p != NULL ); | ||||
|     PK_VALIDATE_RET( *p != NULL ); | ||||
|     PK_VALIDATE_RET( end != NULL ); | ||||
|     PK_VALIDATE_RET( pk != NULL ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_asn1_get_tag( p, end, &len, | ||||
|                     MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) | ||||
|     { | ||||
|  | @ -1145,16 +1166,22 @@ int mbedtls_pk_parse_key( mbedtls_pk_context *pk, | |||
| { | ||||
|     int ret; | ||||
|     const mbedtls_pk_info_t *pk_info; | ||||
| 
 | ||||
| #if defined(MBEDTLS_PEM_PARSE_C) | ||||
|     size_t len; | ||||
|     mbedtls_pem_context pem; | ||||
| #endif | ||||
| 
 | ||||
|     mbedtls_pem_init( &pem ); | ||||
|     PK_VALIDATE_RET( pk != NULL ); | ||||
|     if( keylen == 0 ) | ||||
|         return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); | ||||
|     PK_VALIDATE_RET( key != NULL ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_PEM_PARSE_C) | ||||
|    mbedtls_pem_init( &pem ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_RSA_C) | ||||
|     /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ | ||||
|     if( keylen == 0 || key[keylen - 1] != '\0' ) | ||||
|     if( key[keylen - 1] != '\0' ) | ||||
|         ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; | ||||
|     else | ||||
|         ret = mbedtls_pem_read_buffer( &pem, | ||||
|  | @ -1185,7 +1212,7 @@ int mbedtls_pk_parse_key( mbedtls_pk_context *pk, | |||
| 
 | ||||
| #if defined(MBEDTLS_ECP_C) | ||||
|     /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ | ||||
|     if( keylen == 0 || key[keylen - 1] != '\0' ) | ||||
|     if( key[keylen - 1] != '\0' ) | ||||
|         ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; | ||||
|     else | ||||
|         ret = mbedtls_pem_read_buffer( &pem, | ||||
|  | @ -1215,7 +1242,7 @@ int mbedtls_pk_parse_key( mbedtls_pk_context *pk, | |||
| #endif /* MBEDTLS_ECP_C */ | ||||
| 
 | ||||
|     /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ | ||||
|     if( keylen == 0 || key[keylen - 1] != '\0' ) | ||||
|     if( key[keylen - 1] != '\0' ) | ||||
|         ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; | ||||
|     else | ||||
|         ret = mbedtls_pem_read_buffer( &pem, | ||||
|  | @ -1238,7 +1265,7 @@ int mbedtls_pk_parse_key( mbedtls_pk_context *pk, | |||
| 
 | ||||
| #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C) | ||||
|     /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ | ||||
|     if( keylen == 0 || key[keylen - 1] != '\0' ) | ||||
|     if( key[keylen - 1] != '\0' ) | ||||
|         ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; | ||||
|     else | ||||
|         ret = mbedtls_pem_read_buffer( &pem, | ||||
|  | @ -1276,9 +1303,6 @@ int mbedtls_pk_parse_key( mbedtls_pk_context *pk, | |||
|     { | ||||
|         unsigned char *key_copy; | ||||
| 
 | ||||
|         if( keylen == 0 ) | ||||
|             return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); | ||||
| 
 | ||||
|         if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL ) | ||||
|             return( MBEDTLS_ERR_PK_ALLOC_FAILED ); | ||||
| 
 | ||||
|  | @ -1360,11 +1384,18 @@ int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx, | |||
| #if defined(MBEDTLS_PEM_PARSE_C) | ||||
|     size_t len; | ||||
|     mbedtls_pem_context pem; | ||||
| #endif | ||||
| 
 | ||||
|     PK_VALIDATE_RET( ctx != NULL ); | ||||
|     if( keylen == 0 ) | ||||
|         return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); | ||||
|     PK_VALIDATE_RET( key != NULL || keylen == 0 ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_PEM_PARSE_C) | ||||
|     mbedtls_pem_init( &pem ); | ||||
| #if defined(MBEDTLS_RSA_C) | ||||
|     /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ | ||||
|     if( keylen == 0 || key[keylen - 1] != '\0' ) | ||||
|     if( key[keylen - 1] != '\0' ) | ||||
|         ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; | ||||
|     else | ||||
|         ret = mbedtls_pem_read_buffer( &pem, | ||||
|  | @ -1395,7 +1426,7 @@ int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx, | |||
| #endif /* MBEDTLS_RSA_C */ | ||||
| 
 | ||||
|     /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ | ||||
|     if( keylen == 0 || key[keylen - 1] != '\0' ) | ||||
|     if( key[keylen - 1] != '\0' ) | ||||
|         ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; | ||||
|     else | ||||
|         ret = mbedtls_pem_read_buffer( &pem, | ||||
|  |  | |||
|  | @ -30,6 +30,7 @@ | |||
| #include "mbedtls/pk.h" | ||||
| #include "mbedtls/asn1write.h" | ||||
| #include "mbedtls/oid.h" | ||||
| #include "mbedtls/platform_util.h" | ||||
| 
 | ||||
| #include <string.h> | ||||
| 
 | ||||
|  | @ -58,6 +59,12 @@ | |||
| #define mbedtls_free       free | ||||
| #endif | ||||
| 
 | ||||
| /* Parameter validation macros based on platform_util.h */ | ||||
| #define PK_VALIDATE_RET( cond )    \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA ) | ||||
| #define PK_VALIDATE( cond )        \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| #if defined(MBEDTLS_RSA_C) | ||||
| /*
 | ||||
|  *  RSAPublicKey ::= SEQUENCE { | ||||
|  | @ -155,6 +162,11 @@ int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start, | |||
|     int ret; | ||||
|     size_t len = 0; | ||||
| 
 | ||||
|     PK_VALIDATE_RET( p != NULL ); | ||||
|     PK_VALIDATE_RET( *p != NULL ); | ||||
|     PK_VALIDATE_RET( start != NULL ); | ||||
|     PK_VALIDATE_RET( key != NULL ); | ||||
| 
 | ||||
| #if defined(MBEDTLS_RSA_C) | ||||
|     if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA ) | ||||
|         MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) ); | ||||
|  | @ -201,6 +213,11 @@ int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, si | |||
|     mbedtls_pk_type_t pk_type; | ||||
|     const char *oid; | ||||
| 
 | ||||
|     PK_VALIDATE_RET( key != NULL ); | ||||
|     if( size == 0 ) | ||||
|         return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); | ||||
|     PK_VALIDATE_RET( buf != NULL ); | ||||
| 
 | ||||
|     c = buf + size; | ||||
| 
 | ||||
|     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) ); | ||||
|  | @ -278,9 +295,16 @@ int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, si | |||
| int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size ) | ||||
| { | ||||
|     int ret; | ||||
|     unsigned char *c = buf + size; | ||||
|     unsigned char *c; | ||||
|     size_t len = 0; | ||||
| 
 | ||||
|     PK_VALIDATE_RET( key != NULL ); | ||||
|     if( size == 0 ) | ||||
|         return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); | ||||
|     PK_VALIDATE_RET( buf != NULL ); | ||||
| 
 | ||||
|     c = buf + size; | ||||
| 
 | ||||
| #if defined(MBEDTLS_RSA_C) | ||||
|     if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA ) | ||||
|     { | ||||
|  | @ -518,6 +542,9 @@ int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, si | |||
|     unsigned char output_buf[PUB_DER_MAX_BYTES]; | ||||
|     size_t olen = 0; | ||||
| 
 | ||||
|     PK_VALIDATE_RET( key != NULL ); | ||||
|     PK_VALIDATE_RET( buf != NULL || size == 0 ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf, | ||||
|                                      sizeof(output_buf) ) ) < 0 ) | ||||
|     { | ||||
|  | @ -541,6 +568,9 @@ int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_ | |||
|     const char *begin, *end; | ||||
|     size_t olen = 0; | ||||
| 
 | ||||
|     PK_VALIDATE_RET( key != NULL ); | ||||
|     PK_VALIDATE_RET( buf != NULL || size == 0 ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 ) | ||||
|         return( ret ); | ||||
| 
 | ||||
|  |  | |||
|  | @ -82,28 +82,15 @@ int mbedtls_platform_set_calloc_free( void * (*calloc_func)( size_t, size_t ), | |||
|           !( defined(MBEDTLS_PLATFORM_CALLOC_MACRO) && | ||||
|              defined(MBEDTLS_PLATFORM_FREE_MACRO) ) */ | ||||
| 
 | ||||
| #if defined(_WIN32) | ||||
| #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_SNPRINTF) | ||||
| #include <stdarg.h> | ||||
| int mbedtls_platform_win32_snprintf( char *s, size_t n, const char *fmt, ... ) | ||||
| { | ||||
|     int ret; | ||||
|     va_list argp; | ||||
| 
 | ||||
|     /* Avoid calling the invalid parameter handler by checking ourselves */ | ||||
|     if( s == NULL || n == 0 || fmt == NULL ) | ||||
|         return( -1 ); | ||||
| 
 | ||||
|     va_start( argp, fmt ); | ||||
| #if defined(_TRUNCATE) && !defined(__MINGW32__) | ||||
|     ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp ); | ||||
| #else | ||||
|     ret = _vsnprintf( s, n, fmt, argp ); | ||||
|     if( ret < 0 || (size_t) ret == n ) | ||||
|     { | ||||
|         s[n-1] = '\0'; | ||||
|         ret = -1; | ||||
|     } | ||||
| #endif | ||||
|     ret = mbedtls_vsnprintf( s, n, fmt, argp ); | ||||
|     va_end( argp ); | ||||
| 
 | ||||
|     return( ret ); | ||||
|  | @ -140,6 +127,62 @@ int mbedtls_platform_set_snprintf( int (*snprintf_func)( char * s, size_t n, | |||
| } | ||||
| #endif /* MBEDTLS_PLATFORM_SNPRINTF_ALT */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_PLATFORM_HAS_NON_CONFORMING_VSNPRINTF) | ||||
| #include <stdarg.h> | ||||
| int mbedtls_platform_win32_vsnprintf( char *s, size_t n, const char *fmt, va_list arg ) | ||||
| { | ||||
|     int ret; | ||||
| 
 | ||||
|     /* Avoid calling the invalid parameter handler by checking ourselves */ | ||||
|     if( s == NULL || n == 0 || fmt == NULL ) | ||||
|         return( -1 ); | ||||
| 
 | ||||
| #if defined(_TRUNCATE) | ||||
|     ret = vsnprintf_s( s, n, _TRUNCATE, fmt, arg ); | ||||
| #else | ||||
|     ret = vsnprintf( s, n, fmt, arg ); | ||||
|     if( ret < 0 || (size_t) ret == n ) | ||||
|     { | ||||
|         s[n-1] = '\0'; | ||||
|         ret = -1; | ||||
|     } | ||||
| #endif | ||||
| 
 | ||||
|     return( ret ); | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
| #if defined(MBEDTLS_PLATFORM_VSNPRINTF_ALT) | ||||
| #if !defined(MBEDTLS_PLATFORM_STD_VSNPRINTF) | ||||
| /*
 | ||||
|  * Make dummy function to prevent NULL pointer dereferences | ||||
|  */ | ||||
| static int platform_vsnprintf_uninit( char * s, size_t n, | ||||
|                                      const char * format, va_list arg ) | ||||
| { | ||||
|     ((void) s); | ||||
|     ((void) n); | ||||
|     ((void) format); | ||||
|     ((void) arg); | ||||
|     return( -1 ); | ||||
| } | ||||
| 
 | ||||
| #define MBEDTLS_PLATFORM_STD_VSNPRINTF    platform_vsnprintf_uninit | ||||
| #endif /* !MBEDTLS_PLATFORM_STD_VSNPRINTF */ | ||||
| 
 | ||||
| int (*mbedtls_vsnprintf)( char * s, size_t n, | ||||
|                           const char * format, | ||||
|                           va_list arg ) = MBEDTLS_PLATFORM_STD_VSNPRINTF; | ||||
| 
 | ||||
| int mbedtls_platform_set_vsnprintf( int (*vsnprintf_func)( char * s, size_t n, | ||||
|                                                  const char * format, | ||||
|                                                  va_list arg ) ) | ||||
| { | ||||
|     mbedtls_vsnprintf = vsnprintf_func; | ||||
|     return( 0 ); | ||||
| } | ||||
| #endif /* MBEDTLS_PLATFORM_VSNPRINTF_ALT */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_PLATFORM_PRINTF_ALT) | ||||
| #if !defined(MBEDTLS_PLATFORM_STD_PRINTF) | ||||
| /*
 | ||||
|  |  | |||
|  | @ -35,6 +35,7 @@ | |||
| #endif | ||||
| 
 | ||||
| #include "mbedtls/platform_util.h" | ||||
| #include "mbedtls/platform.h" | ||||
| #include "mbedtls/threading.h" | ||||
| 
 | ||||
| #include <stddef.h> | ||||
|  |  | |||
|  | @ -49,6 +49,12 @@ | |||
| #define inline __inline | ||||
| #endif | ||||
| 
 | ||||
| /* Parameter validation macros */ | ||||
| #define POLY1305_VALIDATE_RET( cond )                                       \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ) | ||||
| #define POLY1305_VALIDATE( cond )                                           \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| #define POLY1305_BLOCK_SIZE_BYTES ( 16U ) | ||||
| 
 | ||||
| #define BYTES_TO_U32_LE( data, offset )                           \ | ||||
|  | @ -276,27 +282,24 @@ static void poly1305_compute_mac( const mbedtls_poly1305_context *ctx, | |||
| 
 | ||||
| void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx ) | ||||
| { | ||||
|     if( ctx != NULL ) | ||||
|     { | ||||
|         mbedtls_platform_zeroize( ctx, sizeof( mbedtls_poly1305_context ) ); | ||||
|     } | ||||
|     POLY1305_VALIDATE( ctx != NULL ); | ||||
| 
 | ||||
|     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_poly1305_context ) ); | ||||
| } | ||||
| 
 | ||||
| void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx ) | ||||
| { | ||||
|     if( ctx != NULL ) | ||||
|     { | ||||
|         mbedtls_platform_zeroize( ctx, sizeof( mbedtls_poly1305_context ) ); | ||||
|     } | ||||
|     if( ctx == NULL ) | ||||
|         return; | ||||
| 
 | ||||
|     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_poly1305_context ) ); | ||||
| } | ||||
| 
 | ||||
| int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx, | ||||
|                              const unsigned char key[32] ) | ||||
| { | ||||
|     if( ctx == NULL || key == NULL ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | ||||
|     } | ||||
|     POLY1305_VALIDATE_RET( ctx != NULL ); | ||||
|     POLY1305_VALIDATE_RET( key != NULL ); | ||||
| 
 | ||||
|     /* r &= 0x0ffffffc0ffffffc0ffffffc0fffffff */ | ||||
|     ctx->r[0] = BYTES_TO_U32_LE( key, 0 )  & 0x0FFFFFFFU; | ||||
|  | @ -331,16 +334,8 @@ int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx, | |||
|     size_t remaining = ilen; | ||||
|     size_t queue_free_len; | ||||
|     size_t nblocks; | ||||
| 
 | ||||
|     if( ctx == NULL ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | ||||
|     } | ||||
|     else if( ( ilen > 0U ) && ( input == NULL ) ) | ||||
|     { | ||||
|         /* input pointer is allowed to be NULL only if ilen == 0 */ | ||||
|         return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | ||||
|     } | ||||
|     POLY1305_VALIDATE_RET( ctx != NULL ); | ||||
|     POLY1305_VALIDATE_RET( ilen == 0 || input != NULL ); | ||||
| 
 | ||||
|     if( ( remaining > 0U ) && ( ctx->queue_len > 0U ) ) | ||||
|     { | ||||
|  | @ -398,10 +393,8 @@ int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx, | |||
| int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx, | ||||
|                              unsigned char mac[16] ) | ||||
| { | ||||
|     if( ( ctx == NULL ) || ( mac == NULL ) ) | ||||
|     { | ||||
|         return( MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA ); | ||||
|     } | ||||
|     POLY1305_VALIDATE_RET( ctx != NULL ); | ||||
|     POLY1305_VALIDATE_RET( mac != NULL ); | ||||
| 
 | ||||
|     /* Process any leftover data */ | ||||
|     if( ctx->queue_len > 0U ) | ||||
|  | @ -431,6 +424,9 @@ int mbedtls_poly1305_mac( const unsigned char key[32], | |||
| { | ||||
|     mbedtls_poly1305_context ctx; | ||||
|     int ret; | ||||
|     POLY1305_VALIDATE_RET( key != NULL ); | ||||
|     POLY1305_VALIDATE_RET( mac != NULL ); | ||||
|     POLY1305_VALIDATE_RET( ilen == 0 || input != NULL ); | ||||
| 
 | ||||
|     mbedtls_poly1305_init( &ctx ); | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										222
									
								
								library/rsa.c
									
									
									
									
									
								
							
							
						
						
									
										222
									
								
								library/rsa.c
									
									
									
									
									
								
							|  | @ -71,6 +71,12 @@ | |||
| 
 | ||||
| #if !defined(MBEDTLS_RSA_ALT) | ||||
| 
 | ||||
| /* Parameter validation macros */ | ||||
| #define RSA_VALIDATE_RET( cond )                                       \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_RSA_BAD_INPUT_DATA ) | ||||
| #define RSA_VALIDATE( cond )                                           \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| #if defined(MBEDTLS_PKCS1_V15) | ||||
| /* constant-time buffer comparison */ | ||||
| static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n ) | ||||
|  | @ -93,6 +99,7 @@ int mbedtls_rsa_import( mbedtls_rsa_context *ctx, | |||
|                         const mbedtls_mpi *D, const mbedtls_mpi *E ) | ||||
| { | ||||
|     int ret; | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
| 
 | ||||
|     if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) || | ||||
|         ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) || | ||||
|  | @ -117,6 +124,7 @@ int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx, | |||
|                             unsigned char const *E, size_t E_len ) | ||||
| { | ||||
|     int ret = 0; | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
| 
 | ||||
|     if( N != NULL ) | ||||
|     { | ||||
|  | @ -240,12 +248,16 @@ static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv, | |||
| int mbedtls_rsa_complete( mbedtls_rsa_context *ctx ) | ||||
| { | ||||
|     int ret = 0; | ||||
|     int have_N, have_P, have_Q, have_D, have_E; | ||||
|     int n_missing, pq_missing, d_missing, is_pub, is_priv; | ||||
| 
 | ||||
|     const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 ); | ||||
|     const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 ); | ||||
|     const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 ); | ||||
|     const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 ); | ||||
|     const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 ); | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
| 
 | ||||
|     have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 ); | ||||
|     have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 ); | ||||
|     have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 ); | ||||
|     have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 ); | ||||
|     have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 ); | ||||
| 
 | ||||
|     /*
 | ||||
|      * Check whether provided parameters are enough | ||||
|  | @ -257,13 +269,13 @@ int mbedtls_rsa_complete( mbedtls_rsa_context *ctx ) | |||
|      * | ||||
|      */ | ||||
| 
 | ||||
|     const int n_missing  =              have_P &&  have_Q &&  have_D && have_E; | ||||
|     const int pq_missing =   have_N && !have_P && !have_Q &&  have_D && have_E; | ||||
|     const int d_missing  =              have_P &&  have_Q && !have_D && have_E; | ||||
|     const int is_pub     =   have_N && !have_P && !have_Q && !have_D && have_E; | ||||
|     n_missing  =              have_P &&  have_Q &&  have_D && have_E; | ||||
|     pq_missing =   have_N && !have_P && !have_Q &&  have_D && have_E; | ||||
|     d_missing  =              have_P &&  have_Q && !have_D && have_E; | ||||
|     is_pub     =   have_N && !have_P && !have_Q && !have_D && have_E; | ||||
| 
 | ||||
|     /* These three alternatives are mutually exclusive */ | ||||
|     const int is_priv = n_missing || pq_missing || d_missing; | ||||
|     is_priv = n_missing || pq_missing || d_missing; | ||||
| 
 | ||||
|     if( !is_priv && !is_pub ) | ||||
|         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | ||||
|  | @ -336,9 +348,11 @@ int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx, | |||
|                             unsigned char *E, size_t E_len ) | ||||
| { | ||||
|     int ret = 0; | ||||
|     int is_priv; | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
| 
 | ||||
|     /* Check if key is private or public */ | ||||
|     const int is_priv = | ||||
|     is_priv = | ||||
|         mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 && | ||||
|         mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 && | ||||
|         mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 && | ||||
|  | @ -379,9 +393,11 @@ int mbedtls_rsa_export( const mbedtls_rsa_context *ctx, | |||
|                         mbedtls_mpi *D, mbedtls_mpi *E ) | ||||
| { | ||||
|     int ret; | ||||
|     int is_priv; | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
| 
 | ||||
|     /* Check if key is private or public */ | ||||
|     int is_priv = | ||||
|     is_priv = | ||||
|         mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 && | ||||
|         mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 && | ||||
|         mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 && | ||||
|  | @ -421,9 +437,11 @@ int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx, | |||
|                             mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP ) | ||||
| { | ||||
|     int ret; | ||||
|     int is_priv; | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
| 
 | ||||
|     /* Check if key is private or public */ | ||||
|     int is_priv = | ||||
|     is_priv = | ||||
|         mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 && | ||||
|         mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 && | ||||
|         mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 && | ||||
|  | @ -459,6 +477,10 @@ void mbedtls_rsa_init( mbedtls_rsa_context *ctx, | |||
|                int padding, | ||||
|                int hash_id ) | ||||
| { | ||||
|     RSA_VALIDATE( ctx != NULL ); | ||||
|     RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 || | ||||
|                   padding == MBEDTLS_RSA_PKCS_V21 ); | ||||
| 
 | ||||
|     memset( ctx, 0, sizeof( mbedtls_rsa_context ) ); | ||||
| 
 | ||||
|     mbedtls_rsa_set_padding( ctx, padding, hash_id ); | ||||
|  | @ -471,8 +493,13 @@ void mbedtls_rsa_init( mbedtls_rsa_context *ctx, | |||
| /*
 | ||||
|  * Set padding for an existing RSA context | ||||
|  */ | ||||
| void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id ) | ||||
| void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, | ||||
|                               int hash_id ) | ||||
| { | ||||
|     RSA_VALIDATE( ctx != NULL ); | ||||
|     RSA_VALIDATE( padding == MBEDTLS_RSA_PKCS_V15 || | ||||
|                   padding == MBEDTLS_RSA_PKCS_V21 ); | ||||
| 
 | ||||
|     ctx->padding = padding; | ||||
|     ctx->hash_id = hash_id; | ||||
| } | ||||
|  | @ -503,11 +530,10 @@ int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx, | |||
|     int ret; | ||||
|     mbedtls_mpi H, G, L; | ||||
|     int prime_quality = 0; | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
|     RSA_VALIDATE_RET( f_rng != NULL ); | ||||
| 
 | ||||
|     if( f_rng == NULL || nbits < 128 || exponent < 3 ) | ||||
|         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     if( nbits % 2 ) | ||||
|     if( nbits < 128 || exponent < 3 || nbits % 2 != 0 ) | ||||
|         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     /*
 | ||||
|  | @ -612,6 +638,8 @@ cleanup: | |||
|  */ | ||||
| int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx ) | ||||
| { | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
| 
 | ||||
|     if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 ) | ||||
|         return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); | ||||
| 
 | ||||
|  | @ -635,6 +663,8 @@ int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx ) | |||
|  */ | ||||
| int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx ) | ||||
| { | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
| 
 | ||||
|     if( mbedtls_rsa_check_pubkey( ctx ) != 0 || | ||||
|         rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 ) | ||||
|     { | ||||
|  | @ -664,6 +694,9 @@ int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx ) | |||
| int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, | ||||
|                                 const mbedtls_rsa_context *prv ) | ||||
| { | ||||
|     RSA_VALIDATE_RET( pub != NULL ); | ||||
|     RSA_VALIDATE_RET( prv != NULL ); | ||||
| 
 | ||||
|     if( mbedtls_rsa_check_pubkey( pub )  != 0 || | ||||
|         mbedtls_rsa_check_privkey( prv ) != 0 ) | ||||
|     { | ||||
|  | @ -689,6 +722,9 @@ int mbedtls_rsa_public( mbedtls_rsa_context *ctx, | |||
|     int ret; | ||||
|     size_t olen; | ||||
|     mbedtls_mpi T; | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
|     RSA_VALIDATE_RET( input != NULL ); | ||||
|     RSA_VALIDATE_RET( output != NULL ); | ||||
| 
 | ||||
|     if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) ) | ||||
|         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | ||||
|  | @ -831,6 +867,10 @@ int mbedtls_rsa_private( mbedtls_rsa_context *ctx, | |||
|      * checked result; should be the same in the end. */ | ||||
|     mbedtls_mpi I, C; | ||||
| 
 | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
|     RSA_VALIDATE_RET( input  != NULL ); | ||||
|     RSA_VALIDATE_RET( output != NULL ); | ||||
| 
 | ||||
|     if( rsa_check_context( ctx, 1             /* private key checks */, | ||||
|                                 f_rng != NULL /* blinding y/n       */ ) != 0 ) | ||||
|     { | ||||
|  | @ -1091,6 +1131,13 @@ int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx, | |||
|     const mbedtls_md_info_t *md_info; | ||||
|     mbedtls_md_context_t md_ctx; | ||||
| 
 | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
|     RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || | ||||
|                       mode == MBEDTLS_RSA_PUBLIC ); | ||||
|     RSA_VALIDATE_RET( output != NULL ); | ||||
|     RSA_VALIDATE_RET( ilen == 0 || input != NULL ); | ||||
|     RSA_VALIDATE_RET( label_len == 0 || label != NULL ); | ||||
| 
 | ||||
|     if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) | ||||
|         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|  | @ -1168,13 +1215,13 @@ int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx, | |||
|     int ret; | ||||
|     unsigned char *p = output; | ||||
| 
 | ||||
|     if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) | ||||
|         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
|     RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || | ||||
|                       mode == MBEDTLS_RSA_PUBLIC ); | ||||
|     RSA_VALIDATE_RET( output != NULL ); | ||||
|     RSA_VALIDATE_RET( ilen == 0 || input != NULL ); | ||||
| 
 | ||||
|     // We don't check p_rng because it won't be dereferenced here
 | ||||
|     if( f_rng == NULL || output == NULL ) | ||||
|         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | ||||
|     if( ilen != 0 && input == NULL ) | ||||
|     if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) | ||||
|         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|     olen = ctx->len; | ||||
|  | @ -1188,6 +1235,9 @@ int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx, | |||
|     *p++ = 0; | ||||
|     if( mode == MBEDTLS_RSA_PUBLIC ) | ||||
|     { | ||||
|         if( f_rng == NULL ) | ||||
|             return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|         *p++ = MBEDTLS_RSA_CRYPT; | ||||
| 
 | ||||
|         while( nb_pad-- > 0 ) | ||||
|  | @ -1233,6 +1283,12 @@ int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx, | |||
|                        const unsigned char *input, | ||||
|                        unsigned char *output ) | ||||
| { | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
|     RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || | ||||
|                       mode == MBEDTLS_RSA_PUBLIC ); | ||||
|     RSA_VALIDATE_RET( output != NULL ); | ||||
|     RSA_VALIDATE_RET( ilen == 0 || input != NULL ); | ||||
| 
 | ||||
|     switch( ctx->padding ) | ||||
|     { | ||||
| #if defined(MBEDTLS_PKCS1_V15) | ||||
|  | @ -1275,6 +1331,14 @@ int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx, | |||
|     const mbedtls_md_info_t *md_info; | ||||
|     mbedtls_md_context_t md_ctx; | ||||
| 
 | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
|     RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || | ||||
|                       mode == MBEDTLS_RSA_PUBLIC ); | ||||
|     RSA_VALIDATE_RET( output_max_len == 0 || output != NULL ); | ||||
|     RSA_VALIDATE_RET( label_len == 0 || label != NULL ); | ||||
|     RSA_VALIDATE_RET( input != NULL ); | ||||
|     RSA_VALIDATE_RET( olen != NULL ); | ||||
| 
 | ||||
|     /*
 | ||||
|      * Parameters sanity checks | ||||
|      */ | ||||
|  | @ -1495,11 +1559,7 @@ int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx, | |||
|                                  size_t output_max_len ) | ||||
| { | ||||
|     int ret; | ||||
|     size_t ilen = ctx->len; | ||||
|     size_t i; | ||||
|     size_t plaintext_max_size = ( output_max_len > ilen - 11 ? | ||||
|                                   ilen - 11 : | ||||
|                                   output_max_len ); | ||||
|     size_t ilen, i, plaintext_max_size; | ||||
|     unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; | ||||
|     /* The following variables take sensitive values: their value must
 | ||||
|      * not leak into the observable behavior of the function other than | ||||
|  | @ -1517,6 +1577,18 @@ int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx, | |||
|     size_t plaintext_size = 0; | ||||
|     unsigned output_too_large; | ||||
| 
 | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
|     RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || | ||||
|                       mode == MBEDTLS_RSA_PUBLIC ); | ||||
|     RSA_VALIDATE_RET( output_max_len == 0 || output != NULL ); | ||||
|     RSA_VALIDATE_RET( input != NULL ); | ||||
|     RSA_VALIDATE_RET( olen != NULL ); | ||||
| 
 | ||||
|     ilen = ctx->len; | ||||
|     plaintext_max_size = ( output_max_len > ilen - 11 ? | ||||
|                            ilen - 11 : | ||||
|                            output_max_len ); | ||||
| 
 | ||||
|     if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) | ||||
|         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|  | @ -1658,6 +1730,13 @@ int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx, | |||
|                        unsigned char *output, | ||||
|                        size_t output_max_len) | ||||
| { | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
|     RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || | ||||
|                       mode == MBEDTLS_RSA_PUBLIC ); | ||||
|     RSA_VALIDATE_RET( output_max_len == 0 || output != NULL ); | ||||
|     RSA_VALIDATE_RET( input != NULL ); | ||||
|     RSA_VALIDATE_RET( olen != NULL ); | ||||
| 
 | ||||
|     switch( ctx->padding ) | ||||
|     { | ||||
| #if defined(MBEDTLS_PKCS1_V15) | ||||
|  | @ -1699,6 +1778,13 @@ int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx, | |||
|     size_t msb; | ||||
|     const mbedtls_md_info_t *md_info; | ||||
|     mbedtls_md_context_t md_ctx; | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
|     RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || | ||||
|                       mode == MBEDTLS_RSA_PUBLIC ); | ||||
|     RSA_VALIDATE_RET( ( md_alg  == MBEDTLS_MD_NONE && | ||||
|                         hashlen == 0 ) || | ||||
|                       hash != NULL ); | ||||
|     RSA_VALIDATE_RET( sig != NULL ); | ||||
| 
 | ||||
|     if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) | ||||
|         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | ||||
|  | @ -1946,6 +2032,14 @@ int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx, | |||
|     int ret; | ||||
|     unsigned char *sig_try = NULL, *verif = NULL; | ||||
| 
 | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
|     RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || | ||||
|                       mode == MBEDTLS_RSA_PUBLIC ); | ||||
|     RSA_VALIDATE_RET( ( md_alg  == MBEDTLS_MD_NONE && | ||||
|                         hashlen == 0 ) || | ||||
|                       hash != NULL ); | ||||
|     RSA_VALIDATE_RET( sig != NULL ); | ||||
| 
 | ||||
|     if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) | ||||
|         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|  | @ -2015,6 +2109,14 @@ int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx, | |||
|                     const unsigned char *hash, | ||||
|                     unsigned char *sig ) | ||||
| { | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
|     RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || | ||||
|                       mode == MBEDTLS_RSA_PUBLIC ); | ||||
|     RSA_VALIDATE_RET( ( md_alg  == MBEDTLS_MD_NONE && | ||||
|                         hashlen == 0 ) || | ||||
|                       hash != NULL ); | ||||
|     RSA_VALIDATE_RET( sig != NULL ); | ||||
| 
 | ||||
|     switch( ctx->padding ) | ||||
|     { | ||||
| #if defined(MBEDTLS_PKCS1_V15) | ||||
|  | @ -2061,6 +2163,14 @@ int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx, | |||
|     mbedtls_md_context_t md_ctx; | ||||
|     unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; | ||||
| 
 | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
|     RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || | ||||
|                       mode == MBEDTLS_RSA_PUBLIC ); | ||||
|     RSA_VALIDATE_RET( sig != NULL ); | ||||
|     RSA_VALIDATE_RET( ( md_alg  == MBEDTLS_MD_NONE && | ||||
|                         hashlen == 0 ) || | ||||
|                       hash != NULL ); | ||||
| 
 | ||||
|     if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) | ||||
|         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|  | @ -2189,7 +2299,16 @@ int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx, | |||
|                            const unsigned char *hash, | ||||
|                            const unsigned char *sig ) | ||||
| { | ||||
|     mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE ) | ||||
|     mbedtls_md_type_t mgf1_hash_id; | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
|     RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || | ||||
|                       mode == MBEDTLS_RSA_PUBLIC ); | ||||
|     RSA_VALIDATE_RET( sig != NULL ); | ||||
|     RSA_VALIDATE_RET( ( md_alg  == MBEDTLS_MD_NONE && | ||||
|                         hashlen == 0 ) || | ||||
|                       hash != NULL ); | ||||
| 
 | ||||
|     mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE ) | ||||
|                              ? (mbedtls_md_type_t) ctx->hash_id | ||||
|                              : md_alg; | ||||
| 
 | ||||
|  | @ -2215,9 +2334,19 @@ int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx, | |||
|                                  const unsigned char *sig ) | ||||
| { | ||||
|     int ret = 0; | ||||
|     const size_t sig_len = ctx->len; | ||||
|     size_t sig_len; | ||||
|     unsigned char *encoded = NULL, *encoded_expected = NULL; | ||||
| 
 | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
|     RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || | ||||
|                       mode == MBEDTLS_RSA_PUBLIC ); | ||||
|     RSA_VALIDATE_RET( sig != NULL ); | ||||
|     RSA_VALIDATE_RET( ( md_alg  == MBEDTLS_MD_NONE && | ||||
|                         hashlen == 0 ) || | ||||
|                       hash != NULL ); | ||||
| 
 | ||||
|     sig_len = ctx->len; | ||||
| 
 | ||||
|     if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) | ||||
|         return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | ||||
| 
 | ||||
|  | @ -2287,6 +2416,14 @@ int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx, | |||
|                       const unsigned char *hash, | ||||
|                       const unsigned char *sig ) | ||||
| { | ||||
|     RSA_VALIDATE_RET( ctx != NULL ); | ||||
|     RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || | ||||
|                       mode == MBEDTLS_RSA_PUBLIC ); | ||||
|     RSA_VALIDATE_RET( sig != NULL ); | ||||
|     RSA_VALIDATE_RET( ( md_alg  == MBEDTLS_MD_NONE && | ||||
|                         hashlen == 0 ) || | ||||
|                       hash != NULL ); | ||||
| 
 | ||||
|     switch( ctx->padding ) | ||||
|     { | ||||
| #if defined(MBEDTLS_PKCS1_V15) | ||||
|  | @ -2312,6 +2449,8 @@ int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx, | |||
| int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src ) | ||||
| { | ||||
|     int ret; | ||||
|     RSA_VALIDATE_RET( dst != NULL ); | ||||
|     RSA_VALIDATE_RET( src != NULL ); | ||||
| 
 | ||||
|     dst->ver = src->ver; | ||||
|     dst->len = src->len; | ||||
|  | @ -2351,14 +2490,23 @@ cleanup: | |||
|  */ | ||||
| void mbedtls_rsa_free( mbedtls_rsa_context *ctx ) | ||||
| { | ||||
|     mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf ); | ||||
|     mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D  ); | ||||
|     mbedtls_mpi_free( &ctx->Q  ); mbedtls_mpi_free( &ctx->P  ); | ||||
|     mbedtls_mpi_free( &ctx->E  ); mbedtls_mpi_free( &ctx->N  ); | ||||
|     if( ctx == NULL ) | ||||
|         return; | ||||
| 
 | ||||
|     mbedtls_mpi_free( &ctx->Vi ); | ||||
|     mbedtls_mpi_free( &ctx->Vf ); | ||||
|     mbedtls_mpi_free( &ctx->RN ); | ||||
|     mbedtls_mpi_free( &ctx->D  ); | ||||
|     mbedtls_mpi_free( &ctx->Q  ); | ||||
|     mbedtls_mpi_free( &ctx->P  ); | ||||
|     mbedtls_mpi_free( &ctx->E  ); | ||||
|     mbedtls_mpi_free( &ctx->N  ); | ||||
| 
 | ||||
| #if !defined(MBEDTLS_RSA_NO_CRT) | ||||
|     mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); | ||||
|     mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); | ||||
|     mbedtls_mpi_free( &ctx->RQ ); | ||||
|     mbedtls_mpi_free( &ctx->RP ); | ||||
|     mbedtls_mpi_free( &ctx->QP ); | ||||
|     mbedtls_mpi_free( &ctx->DQ ); | ||||
|     mbedtls_mpi_free( &ctx->DP ); | ||||
| #endif /* MBEDTLS_RSA_NO_CRT */ | ||||
| 
 | ||||
|  |  | |||
|  | @ -46,6 +46,11 @@ | |||
| #endif /* MBEDTLS_PLATFORM_C */ | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #define SHA1_VALIDATE_RET(cond)                             \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA ) | ||||
| 
 | ||||
| #define SHA1_VALIDATE(cond)  MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| #if !defined(MBEDTLS_SHA1_ALT) | ||||
| 
 | ||||
| /*
 | ||||
|  | @ -73,6 +78,8 @@ | |||
| 
 | ||||
| void mbedtls_sha1_init( mbedtls_sha1_context *ctx ) | ||||
| { | ||||
|     SHA1_VALIDATE( ctx != NULL ); | ||||
| 
 | ||||
|     memset( ctx, 0, sizeof( mbedtls_sha1_context ) ); | ||||
| } | ||||
| 
 | ||||
|  | @ -87,6 +94,9 @@ void mbedtls_sha1_free( mbedtls_sha1_context *ctx ) | |||
| void mbedtls_sha1_clone( mbedtls_sha1_context *dst, | ||||
|                          const mbedtls_sha1_context *src ) | ||||
| { | ||||
|     SHA1_VALIDATE( dst != NULL ); | ||||
|     SHA1_VALIDATE( src != NULL ); | ||||
| 
 | ||||
|     *dst = *src; | ||||
| } | ||||
| 
 | ||||
|  | @ -95,6 +105,8 @@ void mbedtls_sha1_clone( mbedtls_sha1_context *dst, | |||
|  */ | ||||
| int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx ) | ||||
| { | ||||
|     SHA1_VALIDATE_RET( ctx != NULL ); | ||||
| 
 | ||||
|     ctx->total[0] = 0; | ||||
|     ctx->total[1] = 0; | ||||
| 
 | ||||
|  | @ -120,6 +132,9 @@ int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, | |||
| { | ||||
|     uint32_t temp, W[16], A, B, C, D, E; | ||||
| 
 | ||||
|     SHA1_VALIDATE_RET( ctx != NULL ); | ||||
|     SHA1_VALIDATE_RET( (const unsigned char *)data != NULL ); | ||||
| 
 | ||||
|     GET_UINT32_BE( W[ 0], data,  0 ); | ||||
|     GET_UINT32_BE( W[ 1], data,  4 ); | ||||
|     GET_UINT32_BE( W[ 2], data,  8 ); | ||||
|  | @ -294,6 +309,9 @@ int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, | |||
|     size_t fill; | ||||
|     uint32_t left; | ||||
| 
 | ||||
|     SHA1_VALIDATE_RET( ctx != NULL ); | ||||
|     SHA1_VALIDATE_RET( ilen == 0 || input != NULL ); | ||||
| 
 | ||||
|     if( ilen == 0 ) | ||||
|         return( 0 ); | ||||
| 
 | ||||
|  | @ -352,6 +370,9 @@ int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx, | |||
|     uint32_t used; | ||||
|     uint32_t high, low; | ||||
| 
 | ||||
|     SHA1_VALIDATE_RET( ctx != NULL ); | ||||
|     SHA1_VALIDATE_RET( (unsigned char *)output != NULL ); | ||||
| 
 | ||||
|     /*
 | ||||
|      * Add padding: 0x80 then 0x00 until 8 bytes remain for the length | ||||
|      */ | ||||
|  | @ -420,6 +441,9 @@ int mbedtls_sha1_ret( const unsigned char *input, | |||
|     int ret; | ||||
|     mbedtls_sha1_context ctx; | ||||
| 
 | ||||
|     SHA1_VALIDATE_RET( ilen == 0 || input != NULL ); | ||||
|     SHA1_VALIDATE_RET( (unsigned char *)output != NULL ); | ||||
| 
 | ||||
|     mbedtls_sha1_init( &ctx ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 ) | ||||
|  |  | |||
|  | @ -49,6 +49,10 @@ | |||
| #endif /* MBEDTLS_PLATFORM_C */ | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #define SHA256_VALIDATE_RET(cond)                           \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA256_BAD_INPUT_DATA ) | ||||
| #define SHA256_VALIDATE(cond)  MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| #if !defined(MBEDTLS_SHA256_ALT) | ||||
| 
 | ||||
| /*
 | ||||
|  | @ -76,6 +80,8 @@ do {                                                    \ | |||
| 
 | ||||
| void mbedtls_sha256_init( mbedtls_sha256_context *ctx ) | ||||
| { | ||||
|     SHA256_VALIDATE( ctx != NULL ); | ||||
| 
 | ||||
|     memset( ctx, 0, sizeof( mbedtls_sha256_context ) ); | ||||
| } | ||||
| 
 | ||||
|  | @ -90,6 +96,9 @@ void mbedtls_sha256_free( mbedtls_sha256_context *ctx ) | |||
| void mbedtls_sha256_clone( mbedtls_sha256_context *dst, | ||||
|                            const mbedtls_sha256_context *src ) | ||||
| { | ||||
|     SHA256_VALIDATE( dst != NULL ); | ||||
|     SHA256_VALIDATE( src != NULL ); | ||||
| 
 | ||||
|     *dst = *src; | ||||
| } | ||||
| 
 | ||||
|  | @ -98,6 +107,9 @@ void mbedtls_sha256_clone( mbedtls_sha256_context *dst, | |||
|  */ | ||||
| int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 ) | ||||
| { | ||||
|     SHA256_VALIDATE_RET( ctx != NULL ); | ||||
|     SHA256_VALIDATE_RET( is224 == 0 || is224 == 1 ); | ||||
| 
 | ||||
|     ctx->total[0] = 0; | ||||
|     ctx->total[1] = 0; | ||||
| 
 | ||||
|  | @ -192,6 +204,9 @@ int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, | |||
|     uint32_t A[8]; | ||||
|     unsigned int i; | ||||
| 
 | ||||
|     SHA256_VALIDATE_RET( ctx != NULL ); | ||||
|     SHA256_VALIDATE_RET( (const unsigned char *)data != NULL ); | ||||
| 
 | ||||
|     for( i = 0; i < 8; i++ ) | ||||
|         A[i] = ctx->state[i]; | ||||
| 
 | ||||
|  | @ -263,6 +278,9 @@ int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, | |||
|     size_t fill; | ||||
|     uint32_t left; | ||||
| 
 | ||||
|     SHA256_VALIDATE_RET( ctx != NULL ); | ||||
|     SHA256_VALIDATE_RET( ilen == 0 || input != NULL ); | ||||
| 
 | ||||
|     if( ilen == 0 ) | ||||
|         return( 0 ); | ||||
| 
 | ||||
|  | @ -321,6 +339,9 @@ int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, | |||
|     uint32_t used; | ||||
|     uint32_t high, low; | ||||
| 
 | ||||
|     SHA256_VALIDATE_RET( ctx != NULL ); | ||||
|     SHA256_VALIDATE_RET( (unsigned char *)output != NULL ); | ||||
| 
 | ||||
|     /*
 | ||||
|      * Add padding: 0x80 then 0x00 until 8 bytes remain for the length | ||||
|      */ | ||||
|  | @ -395,6 +416,10 @@ int mbedtls_sha256_ret( const unsigned char *input, | |||
|     int ret; | ||||
|     mbedtls_sha256_context ctx; | ||||
| 
 | ||||
|     SHA256_VALIDATE_RET( is224 == 0 || is224 == 1 ); | ||||
|     SHA256_VALIDATE_RET( ilen == 0 || input != NULL ); | ||||
|     SHA256_VALIDATE_RET( (unsigned char *)output != NULL ); | ||||
| 
 | ||||
|     mbedtls_sha256_init( &ctx ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_sha256_starts_ret( &ctx, is224 ) ) != 0 ) | ||||
|  |  | |||
|  | @ -55,6 +55,10 @@ | |||
| #endif /* MBEDTLS_PLATFORM_C */ | ||||
| #endif /* MBEDTLS_SELF_TEST */ | ||||
| 
 | ||||
| #define SHA512_VALIDATE_RET(cond)                           \ | ||||
|     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA512_BAD_INPUT_DATA ) | ||||
| #define SHA512_VALIDATE(cond)  MBEDTLS_INTERNAL_VALIDATE( cond ) | ||||
| 
 | ||||
| #if !defined(MBEDTLS_SHA512_ALT) | ||||
| 
 | ||||
| /*
 | ||||
|  | @ -90,6 +94,8 @@ | |||
| 
 | ||||
| void mbedtls_sha512_init( mbedtls_sha512_context *ctx ) | ||||
| { | ||||
|     SHA512_VALIDATE( ctx != NULL ); | ||||
| 
 | ||||
|     memset( ctx, 0, sizeof( mbedtls_sha512_context ) ); | ||||
| } | ||||
| 
 | ||||
|  | @ -104,6 +110,9 @@ void mbedtls_sha512_free( mbedtls_sha512_context *ctx ) | |||
| void mbedtls_sha512_clone( mbedtls_sha512_context *dst, | ||||
|                            const mbedtls_sha512_context *src ) | ||||
| { | ||||
|     SHA512_VALIDATE( dst != NULL ); | ||||
|     SHA512_VALIDATE( src != NULL ); | ||||
| 
 | ||||
|     *dst = *src; | ||||
| } | ||||
| 
 | ||||
|  | @ -112,6 +121,9 @@ void mbedtls_sha512_clone( mbedtls_sha512_context *dst, | |||
|  */ | ||||
| int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 ) | ||||
| { | ||||
|     SHA512_VALIDATE_RET( ctx != NULL ); | ||||
|     SHA512_VALIDATE_RET( is384 == 0 || is384 == 1 ); | ||||
| 
 | ||||
|     ctx->total[0] = 0; | ||||
|     ctx->total[1] = 0; | ||||
| 
 | ||||
|  | @ -209,6 +221,9 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, | |||
|     uint64_t temp1, temp2, W[80]; | ||||
|     uint64_t A, B, C, D, E, F, G, H; | ||||
| 
 | ||||
|     SHA512_VALIDATE_RET( ctx != NULL ); | ||||
|     SHA512_VALIDATE_RET( (const unsigned char *)data != NULL ); | ||||
| 
 | ||||
| #define  SHR(x,n) (x >> n) | ||||
| #define ROTR(x,n) (SHR(x,n) | (x << (64 - n))) | ||||
| 
 | ||||
|  | @ -294,6 +309,9 @@ int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx, | |||
|     size_t fill; | ||||
|     unsigned int left; | ||||
| 
 | ||||
|     SHA512_VALIDATE_RET( ctx != NULL ); | ||||
|     SHA512_VALIDATE_RET( ilen == 0 || input != NULL ); | ||||
| 
 | ||||
|     if( ilen == 0 ) | ||||
|         return( 0 ); | ||||
| 
 | ||||
|  | @ -351,6 +369,9 @@ int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx, | |||
|     unsigned used; | ||||
|     uint64_t high, low; | ||||
| 
 | ||||
|     SHA512_VALIDATE_RET( ctx != NULL ); | ||||
|     SHA512_VALIDATE_RET( (unsigned char *)output != NULL ); | ||||
| 
 | ||||
|     /*
 | ||||
|      * Add padding: 0x80 then 0x00 until 16 bytes remain for the length | ||||
|      */ | ||||
|  | @ -427,6 +448,10 @@ int mbedtls_sha512_ret( const unsigned char *input, | |||
|     int ret; | ||||
|     mbedtls_sha512_context ctx; | ||||
| 
 | ||||
|     SHA512_VALIDATE_RET( is384 == 0 || is384 == 1 ); | ||||
|     SHA512_VALIDATE_RET( ilen == 0 || input != NULL ); | ||||
|     SHA512_VALIDATE_RET( (unsigned char *)output != NULL ); | ||||
| 
 | ||||
|     mbedtls_sha512_init( &ctx ); | ||||
| 
 | ||||
|     if( ( ret = mbedtls_sha512_starts_ret( &ctx, is384 ) ) != 0 ) | ||||
|  |  | |||
|  | @ -1489,7 +1489,7 @@ read_record_header: | |||
|      */ | ||||
| 
 | ||||
|     /*
 | ||||
|      * Minimal length (with everything empty and extensions ommitted) is | ||||
|      * Minimal length (with everything empty and extensions omitted) is | ||||
|      * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can | ||||
|      * read at least up to session id length without worrying. | ||||
|      */ | ||||
|  |  | |||
|  | @ -50,10 +50,19 @@ | |||
| 
 | ||||
| #include <string.h> | ||||
| 
 | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
| #include "mbedtls/psa_util.h" | ||||
| #include "psa/crypto.h" | ||||
| #endif | ||||
| 
 | ||||
| #if defined(MBEDTLS_X509_CRT_PARSE_C) | ||||
| #include "mbedtls/oid.h" | ||||
| #endif | ||||
| 
 | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
| #include "mbedtls/psa_util.h" | ||||
| #endif | ||||
| 
 | ||||
| static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ); | ||||
| static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl ); | ||||
| 
 | ||||
|  | @ -490,6 +499,76 @@ static int tls1_prf( const unsigned char *secret, size_t slen, | |||
| #endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
| static int tls_prf_generic( mbedtls_md_type_t md_type, | ||||
|                             const unsigned char *secret, size_t slen, | ||||
|                             const char *label, | ||||
|                             const unsigned char *random, size_t rlen, | ||||
|                             unsigned char *dstbuf, size_t dlen ) | ||||
| { | ||||
|     psa_status_t status; | ||||
|     psa_algorithm_t alg; | ||||
|     psa_key_policy_t policy; | ||||
|     psa_key_handle_t master_slot; | ||||
|     psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; | ||||
| 
 | ||||
|     if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS ) | ||||
|         return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | ||||
| 
 | ||||
|     if( md_type == MBEDTLS_MD_SHA384 ) | ||||
|         alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384); | ||||
|     else | ||||
|         alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256); | ||||
| 
 | ||||
|     policy = psa_key_policy_init(); | ||||
|     psa_key_policy_set_usage( &policy, | ||||
|                               PSA_KEY_USAGE_DERIVE, | ||||
|                               alg ); | ||||
|     status = psa_set_key_policy( master_slot, &policy ); | ||||
|     if( status != PSA_SUCCESS ) | ||||
|         return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | ||||
| 
 | ||||
|     status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen ); | ||||
|     if( status != PSA_SUCCESS ) | ||||
|         return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | ||||
| 
 | ||||
|     status = psa_key_derivation( &generator, | ||||
|                                  master_slot, alg, | ||||
|                                  random, rlen, | ||||
|                                  (unsigned char const *) label, | ||||
|                                  (size_t) strlen( label ), | ||||
|                                  dlen ); | ||||
|     if( status != PSA_SUCCESS ) | ||||
|     { | ||||
|         psa_generator_abort( &generator ); | ||||
|         psa_destroy_key( master_slot ); | ||||
|         return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | ||||
|     } | ||||
| 
 | ||||
|     status = psa_generator_read( &generator, dstbuf, dlen ); | ||||
|     if( status != PSA_SUCCESS ) | ||||
|     { | ||||
|         psa_generator_abort( &generator ); | ||||
|         psa_destroy_key( master_slot ); | ||||
|         return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | ||||
|     } | ||||
| 
 | ||||
|     status = psa_generator_abort( &generator ); | ||||
|     if( status != PSA_SUCCESS ) | ||||
|     { | ||||
|         psa_destroy_key( master_slot ); | ||||
|         return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | ||||
|     } | ||||
| 
 | ||||
|     status = psa_destroy_key( master_slot ); | ||||
|     if( status != PSA_SUCCESS ) | ||||
|         return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED ); | ||||
| 
 | ||||
|     return( 0 ); | ||||
| } | ||||
| 
 | ||||
| #else /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
| 
 | ||||
| static int tls_prf_generic( mbedtls_md_type_t md_type, | ||||
|                             const unsigned char *secret, size_t slen, | ||||
|                             const char *label, | ||||
|  | @ -552,7 +631,7 @@ static int tls_prf_generic( mbedtls_md_type_t md_type, | |||
| 
 | ||||
|     return( 0 ); | ||||
| } | ||||
| 
 | ||||
| #endif /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
| #if defined(MBEDTLS_SHA256_C) | ||||
| static int tls_prf_sha256( const unsigned char *secret, size_t slen, | ||||
|                            const char *label, | ||||
|  | @ -1347,7 +1426,7 @@ void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] ) | |||
|     mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 ); | ||||
|     mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 ); | ||||
| 
 | ||||
|      mbedtls_md5_finish_ret( &md5,  hash ); | ||||
|     mbedtls_md5_finish_ret( &md5,  hash ); | ||||
|     mbedtls_sha1_finish_ret( &sha1, hash + 16 ); | ||||
| 
 | ||||
|     MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 ); | ||||
|  | @ -1364,6 +1443,28 @@ void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] ) | |||
| #if defined(MBEDTLS_SHA256_C) | ||||
| void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] ) | ||||
| { | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     size_t hash_size; | ||||
|     psa_status_t status; | ||||
|     psa_hash_operation_t sha256_psa = psa_hash_operation_init(); | ||||
| 
 | ||||
|     MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) ); | ||||
|     status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); | ||||
|     if( status != PSA_SUCCESS ) | ||||
|     { | ||||
|         MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size ); | ||||
|     if( status != PSA_SUCCESS ) | ||||
|     { | ||||
|         MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | ||||
|         return; | ||||
|     } | ||||
|     MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 ); | ||||
|     MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); | ||||
| #else | ||||
|     mbedtls_sha256_context sha256; | ||||
| 
 | ||||
|     mbedtls_sha256_init( &sha256 ); | ||||
|  | @ -1377,7 +1478,7 @@ void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32 | |||
|     MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | ||||
| 
 | ||||
|     mbedtls_sha256_free( &sha256 ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
|     return; | ||||
| } | ||||
| #endif /* MBEDTLS_SHA256_C */ | ||||
|  | @ -1385,6 +1486,28 @@ void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32 | |||
| #if defined(MBEDTLS_SHA512_C) | ||||
| void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] ) | ||||
| { | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     size_t hash_size; | ||||
|     psa_status_t status; | ||||
|     psa_hash_operation_t sha384_psa = psa_hash_operation_init(); | ||||
| 
 | ||||
|     MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) ); | ||||
|     status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); | ||||
|     if( status != PSA_SUCCESS ) | ||||
|     { | ||||
|         MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size ); | ||||
|     if( status != PSA_SUCCESS ) | ||||
|     { | ||||
|         MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | ||||
|         return; | ||||
|     } | ||||
|     MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 ); | ||||
|     MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) ); | ||||
| #else | ||||
|     mbedtls_sha512_context sha512; | ||||
| 
 | ||||
|     mbedtls_sha512_init( &sha512 ); | ||||
|  | @ -1398,7 +1521,7 @@ void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48 | |||
|     MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) ); | ||||
| 
 | ||||
|     mbedtls_sha512_free( &sha512 ); | ||||
| 
 | ||||
| #endif /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
|     return; | ||||
| } | ||||
| #endif /* MBEDTLS_SHA512_C */ | ||||
|  | @ -2772,7 +2895,7 @@ int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want ) | |||
|         } | ||||
| 
 | ||||
|         /*
 | ||||
|          * A record can't be split accross datagrams. If we need to read but | ||||
|          * A record can't be split across datagrams. If we need to read but | ||||
|          * are not at the beginning of a new record, the caller did something | ||||
|          * wrong. | ||||
|          */ | ||||
|  | @ -6172,11 +6295,21 @@ void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl ) | |||
| #endif | ||||
| #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
| #if defined(MBEDTLS_SHA256_C) | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     psa_hash_abort( &ssl->handshake->fin_sha256_psa ); | ||||
|     psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); | ||||
| #else | ||||
|     mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 ); | ||||
| #endif | ||||
| #endif | ||||
| #if defined(MBEDTLS_SHA512_C) | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     psa_hash_abort( &ssl->handshake->fin_sha384_psa ); | ||||
|     psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); | ||||
| #else | ||||
|     mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 ); | ||||
| #endif | ||||
| #endif | ||||
| #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | ||||
| } | ||||
| 
 | ||||
|  | @ -6190,11 +6323,19 @@ static void ssl_update_checksum_start( mbedtls_ssl_context *ssl, | |||
| #endif | ||||
| #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
| #if defined(MBEDTLS_SHA256_C) | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); | ||||
| #else | ||||
|     mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); | ||||
| #endif | ||||
| #endif | ||||
| #if defined(MBEDTLS_SHA512_C) | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); | ||||
| #else | ||||
|     mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); | ||||
| #endif | ||||
| #endif | ||||
| #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | ||||
| } | ||||
| 
 | ||||
|  | @ -6213,7 +6354,11 @@ static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl, | |||
| static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, | ||||
|                                         const unsigned char *buf, size_t len ) | ||||
| { | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len ); | ||||
| #else | ||||
|     mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len ); | ||||
| #endif | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
|  | @ -6221,7 +6366,11 @@ static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl, | |||
| static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl, | ||||
|                                         const unsigned char *buf, size_t len ) | ||||
| { | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len ); | ||||
| #else | ||||
|     mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len ); | ||||
| #endif | ||||
| } | ||||
| #endif | ||||
| #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | ||||
|  | @ -6377,13 +6526,44 @@ static void ssl_calc_finished_tls_sha256( | |||
| { | ||||
|     int len = 12; | ||||
|     const char *sender; | ||||
|     mbedtls_sha256_context sha256; | ||||
|     unsigned char padbuf[32]; | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     size_t hash_size; | ||||
|     psa_hash_operation_t sha256_psa; | ||||
|     psa_status_t status; | ||||
| #else | ||||
|     mbedtls_sha256_context sha256; | ||||
| #endif | ||||
| 
 | ||||
|     mbedtls_ssl_session *session = ssl->session_negotiate; | ||||
|     if( !session ) | ||||
|         session = ssl->session; | ||||
| 
 | ||||
|     sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | ||||
|              ? "client finished" | ||||
|              : "server finished"; | ||||
| 
 | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     sha256_psa = psa_hash_operation_init(); | ||||
| 
 | ||||
|     MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) ); | ||||
| 
 | ||||
|     status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa ); | ||||
|     if( status != PSA_SUCCESS ) | ||||
|     { | ||||
|         MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size ); | ||||
|     if( status != PSA_SUCCESS ) | ||||
|     { | ||||
|         MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | ||||
|         return; | ||||
|     } | ||||
|     MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 ); | ||||
| #else | ||||
| 
 | ||||
|     mbedtls_sha256_init( &sha256 ); | ||||
| 
 | ||||
|     MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls sha256" ) ); | ||||
|  | @ -6401,19 +6581,15 @@ static void ssl_calc_finished_tls_sha256( | |||
|                    sha256.state, sizeof( sha256.state ) ); | ||||
| #endif | ||||
| 
 | ||||
|     sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | ||||
|              ? "client finished" | ||||
|              : "server finished"; | ||||
| 
 | ||||
|     mbedtls_sha256_finish_ret( &sha256, padbuf ); | ||||
|     mbedtls_sha256_free( &sha256 ); | ||||
| #endif /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
| 
 | ||||
|     ssl->handshake->tls_prf( session->master, 48, sender, | ||||
|                              padbuf, 32, buf, len ); | ||||
| 
 | ||||
|     MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | ||||
| 
 | ||||
|     mbedtls_sha256_free( &sha256 ); | ||||
| 
 | ||||
|     mbedtls_platform_zeroize(  padbuf, sizeof(  padbuf ) ); | ||||
| 
 | ||||
|     MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | ||||
|  | @ -6426,13 +6602,43 @@ static void ssl_calc_finished_tls_sha384( | |||
| { | ||||
|     int len = 12; | ||||
|     const char *sender; | ||||
|     mbedtls_sha512_context sha512; | ||||
|     unsigned char padbuf[48]; | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     size_t hash_size; | ||||
|     psa_hash_operation_t sha384_psa; | ||||
|     psa_status_t status; | ||||
| #else | ||||
|     mbedtls_sha512_context sha512; | ||||
| #endif | ||||
| 
 | ||||
|     mbedtls_ssl_session *session = ssl->session_negotiate; | ||||
|     if( !session ) | ||||
|         session = ssl->session; | ||||
| 
 | ||||
|     sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | ||||
|                 ? "client finished" | ||||
|                 : "server finished"; | ||||
| 
 | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     sha384_psa = psa_hash_operation_init(); | ||||
| 
 | ||||
|     MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) ); | ||||
| 
 | ||||
|     status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa ); | ||||
|     if( status != PSA_SUCCESS ) | ||||
|     { | ||||
|         MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) ); | ||||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size ); | ||||
|     if( status != PSA_SUCCESS ) | ||||
|     { | ||||
|         MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) ); | ||||
|         return; | ||||
|     } | ||||
|     MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 ); | ||||
| #else | ||||
|     mbedtls_sha512_init( &sha512 ); | ||||
| 
 | ||||
|     MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc  finished tls sha384" ) ); | ||||
|  | @ -6450,19 +6656,15 @@ static void ssl_calc_finished_tls_sha384( | |||
|                    sha512.state, sizeof( sha512.state ) ); | ||||
| #endif | ||||
| 
 | ||||
|     sender = ( from == MBEDTLS_SSL_IS_CLIENT ) | ||||
|              ? "client finished" | ||||
|              : "server finished"; | ||||
| 
 | ||||
|     mbedtls_sha512_finish_ret( &sha512, padbuf ); | ||||
|     mbedtls_sha512_free( &sha512 ); | ||||
| #endif | ||||
| 
 | ||||
|     ssl->handshake->tls_prf( session->master, 48, sender, | ||||
|                              padbuf, 48, buf, len ); | ||||
| 
 | ||||
|     MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len ); | ||||
| 
 | ||||
|     mbedtls_sha512_free( &sha512 ); | ||||
| 
 | ||||
|     mbedtls_platform_zeroize(  padbuf, sizeof( padbuf ) ); | ||||
| 
 | ||||
|     MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc  finished" ) ); | ||||
|  | @ -6773,13 +6975,23 @@ static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake ) | |||
| #endif | ||||
| #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
| #if defined(MBEDTLS_SHA256_C) | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     handshake->fin_sha256_psa = psa_hash_operation_init(); | ||||
|     psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 ); | ||||
| #else | ||||
|     mbedtls_sha256_init(   &handshake->fin_sha256    ); | ||||
|     mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 ); | ||||
| #endif | ||||
| #endif | ||||
| #if defined(MBEDTLS_SHA512_C) | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     handshake->fin_sha384_psa = psa_hash_operation_init(); | ||||
|     psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 ); | ||||
| #else | ||||
|     mbedtls_sha512_init(   &handshake->fin_sha512    ); | ||||
|     mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 ); | ||||
| #endif | ||||
| #endif | ||||
| #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | ||||
| 
 | ||||
|     handshake->update_checksum = ssl_update_checksum_start; | ||||
|  | @ -9082,11 +9294,19 @@ void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) | |||
| #endif | ||||
| #if defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
| #if defined(MBEDTLS_SHA256_C) | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     psa_hash_abort( &handshake->fin_sha256_psa ); | ||||
| #else | ||||
|     mbedtls_sha256_free(   &handshake->fin_sha256    ); | ||||
| #endif | ||||
| #endif | ||||
| #if defined(MBEDTLS_SHA512_C) | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
|     psa_hash_abort( &handshake->fin_sha384_psa ); | ||||
| #else | ||||
|     mbedtls_sha512_free(   &handshake->fin_sha512    ); | ||||
| #endif | ||||
| #endif | ||||
| #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ | ||||
| 
 | ||||
| #if defined(MBEDTLS_DHM_C) | ||||
|  | @ -9975,6 +10195,70 @@ exit: | |||
| 
 | ||||
| #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ | ||||
|     defined(MBEDTLS_SSL_PROTO_TLS1_2) | ||||
| 
 | ||||
| #if defined(MBEDTLS_USE_PSA_CRYPTO) | ||||
| int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, | ||||
|                                             unsigned char *hash, size_t *hashlen, | ||||
|                                             unsigned char *data, size_t data_len, | ||||
|                                             mbedtls_md_type_t md_alg ) | ||||
| { | ||||
|     psa_status_t status; | ||||
|     psa_hash_operation_t hash_operation; | ||||
|     psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); | ||||
| 
 | ||||
|     MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); | ||||
| 
 | ||||
|     if( ( status = psa_hash_setup( &hash_operation, | ||||
|                                    hash_alg ) ) != PSA_SUCCESS ) | ||||
|     { | ||||
|         MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); | ||||
|         goto exit; | ||||
|     } | ||||
| 
 | ||||
|     if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, | ||||
|                                     64 ) ) != PSA_SUCCESS ) | ||||
|     { | ||||
|         MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); | ||||
|         goto exit; | ||||
|     } | ||||
| 
 | ||||
|     if( ( status = psa_hash_update( &hash_operation, | ||||
|                                     data, data_len ) ) != PSA_SUCCESS ) | ||||
|     { | ||||
|         MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); | ||||
|         goto exit; | ||||
|     } | ||||
| 
 | ||||
|     if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE, | ||||
|                                     hashlen ) ) != PSA_SUCCESS ) | ||||
|     { | ||||
|          MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); | ||||
|          goto exit; | ||||
|     } | ||||
| 
 | ||||
| exit: | ||||
|     if( status != PSA_SUCCESS ) | ||||
|     { | ||||
|         mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, | ||||
|                                         MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); | ||||
|         switch( status ) | ||||
|         { | ||||
|             case PSA_ERROR_NOT_SUPPORTED: | ||||
|                 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); | ||||
|             case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ | ||||
|             case PSA_ERROR_BUFFER_TOO_SMALL: | ||||
|                 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); | ||||
|             case PSA_ERROR_INSUFFICIENT_MEMORY: | ||||
|                 return( MBEDTLS_ERR_MD_ALLOC_FAILED ); | ||||
|             default: | ||||
|                 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED ); | ||||
|         } | ||||
|     } | ||||
|     return( 0 ); | ||||
| } | ||||
| 
 | ||||
| #else | ||||
| 
 | ||||
| int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, | ||||
|                                             unsigned char *hash, size_t *hashlen, | ||||
|                                             unsigned char *data, size_t data_len, | ||||
|  | @ -9985,6 +10269,8 @@ int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, | |||
|     const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); | ||||
|     *hashlen = mbedtls_md_get_size( md_info ); | ||||
| 
 | ||||
|     MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); | ||||
| 
 | ||||
|     mbedtls_md_init( &ctx ); | ||||
| 
 | ||||
|     /*
 | ||||
|  | @ -10029,6 +10315,8 @@ exit: | |||
| 
 | ||||
|     return( ret ); | ||||
| } | ||||
| #endif /* MBEDTLS_USE_PSA_CRYPTO */ | ||||
| 
 | ||||
| #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ | ||||
|           MBEDTLS_SSL_PROTO_TLS1_2 */ | ||||
| 
 | ||||
|  |  | |||
Some files were not shown because too many files have changed in this diff Show more
		Loading…
	
		Reference in a new issue