Import mbedtls-2.16.5

Imports Mbed TLS 2.16.5 from https://github.com/ARMmbed/mbedtls.git
commit 0fce215851cc ("Merge pull request #3053 from
yanesca/bump-version-2.16.5") (tag mbedtls-2.16.5).

Certain files will bever be needed and are thus removed:
 rm -f .gitignore .globalrc .pylintrc .travis.yml
 rm -f CMakeLists.txt DartConfiguration.tcl Makefile
 rm -f include/{.gitignore,CMakeLists.txt}
 rm -f library/{.gitignore,CMakeLists.txt,Makefile}
 rm -rf .git .github configs docs doxygen programs scripts tests visualc

This is a complete overwrite of previous code so earlier changes in the
branch import/mbedtls-2.16.0 will be added on top of this commit to bring
the changes forward.

Signed-off-by: Jerome Forissier <jerome@forissier.org>
Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
diff --git a/lib/libmbedtls/mbedtls/library/cipher.c b/lib/libmbedtls/mbedtls/library/cipher.c
index e722313..8d010b5 100644
--- a/lib/libmbedtls/mbedtls/library/cipher.c
+++ b/lib/libmbedtls/mbedtls/library/cipher.c
@@ -1,4 +1,3 @@
-// SPDX-License-Identifier: Apache-2.0
 /**
  * \file cipher.c
  *
@@ -7,6 +6,7 @@
  * \author Adriaan de Jong <dejong@fox-it.com>
  *
  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
+ *  SPDX-License-Identifier: Apache-2.0
  *
  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
  *  not use this file except in compliance with the License.
@@ -179,36 +179,6 @@
     mbedtls_platform_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
 }
 
-int mbedtls_cipher_clone( mbedtls_cipher_context_t *dst,
-                          const mbedtls_cipher_context_t *src )
-{
-    if( dst == NULL || dst->cipher_info == NULL ||
-        src == NULL || src->cipher_info == NULL)
-    {
-        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
-    }
-
-    dst->cipher_info = src->cipher_info;
-    dst->key_bitlen = src->key_bitlen;
-    dst->operation = src->operation;
-#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
-    dst->add_padding = src->add_padding;
-    dst->get_padding = src->get_padding;
-#endif
-    memcpy( dst->unprocessed_data, src->unprocessed_data, MBEDTLS_MAX_BLOCK_LENGTH );
-    dst->unprocessed_len = src->unprocessed_len;
-    memcpy( dst->iv, src->iv, MBEDTLS_MAX_IV_LENGTH );
-    dst->iv_size = src->iv_size;
-    if( dst->cipher_info->base->ctx_clone_func )
-        dst->cipher_info->base->ctx_clone_func( dst->cipher_ctx, src->cipher_ctx );
-
-#if defined(MBEDTLS_CMAC_C)
-    if( dst->cmac_ctx != NULL && src->cmac_ctx != NULL )
-        memcpy( dst->cmac_ctx, src->cmac_ctx, sizeof( mbedtls_cmac_context_t ) );
-#endif
-    return( 0 );
-}
-
 int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
 {
     CIPHER_VALIDATE_RET( ctx != NULL );
@@ -236,15 +206,6 @@
     return( 0 );
 }
 
-int mbedtls_cipher_setup_info( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
-{
-    if( NULL == cipher_info || NULL == ctx )
-        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
-
-    ctx->cipher_info = cipher_info;
-    return( 0 );
-}
-
 int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
                            const unsigned char *key,
                            int key_bitlen,
@@ -400,6 +361,10 @@
 
     *olen = 0;
     block_size = mbedtls_cipher_get_block_size( ctx );
+    if ( 0 == block_size )
+    {
+        return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
+    }
 
     if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
     {
@@ -435,11 +400,6 @@
     }
 #endif
 
-    if ( 0 == block_size )
-    {
-        return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
-    }
-
     if( input == output &&
        ( ctx->unprocessed_len != 0 || ilen % block_size ) )
     {
@@ -498,11 +458,6 @@
          */
         if( 0 != ilen )
         {
-            if( 0 == block_size )
-            {
-                return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
-            }
-
             /* Encryption: only cache partial blocks
              * Decryption w/ padding: always keep at least one whole block
              * Decryption w/o padding: only cache partial blocks