[t6001]: chip Use hardware acceleration of Encryption

This commit is contained in:
liuhan
2016-08-05 17:40:32 +08:00
committed by Wu Jian Gang
parent 42357990aa
commit 30be5f6eb5
10 changed files with 3965 additions and 0 deletions

View File

@@ -0,0 +1,306 @@
/*
* FIPS-197 compliant AES implementation
*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* The AES block cipher was designed by Vincent Rijmen and Joan Daemen.
*
* http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf
* http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
*/
#include "port/aes_alt.h"
#if defined(ESP_AES_C)
#include <string.h>
/* Implementation that should never be optimized out by the compiler */
static void aes_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}
void aes_init( AES_CTX *ctx )
{
memset( ctx, 0, sizeof( AES_CTX ) );
ets_aes_enable();
}
void aes_free( AES_CTX *ctx )
{
if( ctx == NULL )
return;
aes_zeroize( ctx, sizeof( AES_CTX ) );
ets_aes_disable();
}
/*
* AES key schedule (encryption)
*/
int aes_setkey_enc( AES_CTX *ctx, const unsigned char *key,
unsigned int keybits )
{
enum AES_BITS keybit;
switch (keybits){
case 128:
keybit = AES128;
break;
case 192:
keybit = AES192;
break;
case 256:
keybit = AES256;
break;
default : return( ERR_AES_INVALID_KEY_LENGTH );
}
ets_aes_setkey_enc(key, keybit);
return 0;
}
/*
* AES key schedule (decryption)
*/
int aes_setkey_dec( AES_CTX *ctx, const unsigned char *key,
unsigned int keybits )
{
enum AES_BITS keybit;
switch (keybits){
case 128:
keybit = AES128;
break;
case 192:
keybit = AES192;
break;
case 256:
keybit = AES256;
break;
default : return( ERR_AES_INVALID_KEY_LENGTH );
}
ets_aes_setkey_dec(key, keybit);
return 0;
}
/*
* AES-ECB block encryption
*/
void aes_encrypt( AES_CTX *ctx,
const unsigned char input[16],
unsigned char output[16] )
{
ets_aes_crypt(input, output);
return ;
}
/*
* AES-ECB block decryption
*/
void aes_decrypt( AES_CTX *ctx,
const unsigned char input[16],
unsigned char output[16] )
{
ets_aes_crypt(input, output);
return ;
}
/*
* AES-ECB block encryption/decryption
*/
int aes_crypt_ecb( AES_CTX *ctx,
int mode,
const unsigned char input[16],
unsigned char output[16] )
{
if( mode == AES_ENCRYPT )
aes_encrypt( ctx, input, output );
else
aes_decrypt( ctx, input, output );
return 0;
}
/*
* AES-CBC buffer encryption/decryption
*/
int aes_crypt_cbc( AES_CTX *ctx,
int mode,
size_t length,
unsigned char iv[16],
const unsigned char *input,
unsigned char *output )
{
int i;
unsigned char temp[16];
if( length % 16 )
return( ERR_AES_INVALID_INPUT_LENGTH );
if( mode == AES_DECRYPT )
{
while( length > 0 )
{
memcpy( temp, input, 16 );
aes_crypt_ecb( ctx, mode, input, output );
for( i = 0; i < 16; i++ )
output[i] = (unsigned char)( output[i] ^ iv[i] );
memcpy( iv, temp, 16 );
input += 16;
output += 16;
length -= 16;
}
}
else
{
while( length > 0 )
{
for( i = 0; i < 16; i++ )
output[i] = (unsigned char)( input[i] ^ iv[i] );
aes_crypt_ecb( ctx, mode, output, output );
memcpy( iv, output, 16 );
input += 16;
output += 16;
length -= 16;
}
}
return 0;
}
/*
* AES-CFB128 buffer encryption/decryption
*/
int aes_crypt_cfb128( AES_CTX *ctx,
int mode,
size_t length,
size_t *iv_off,
unsigned char iv[16],
const unsigned char *input,
unsigned char *output )
{
int c;
size_t n = *iv_off;
if( mode == AES_DECRYPT )
{
while( length-- )
{
if( n == 0 )
aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
c = *input++;
*output++ = (unsigned char)( c ^ iv[n] );
iv[n] = (unsigned char) c;
n = ( n + 1 ) & 0x0F;
}
}
else
{
while( length-- )
{
if( n == 0 )
aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
n = ( n + 1 ) & 0x0F;
}
}
*iv_off = n;
return 0;
}
/*
* AES-CFB8 buffer encryption/decryption
*/
int aes_crypt_cfb8( AES_CTX *ctx,
int mode,
size_t length,
unsigned char iv[16],
const unsigned char *input,
unsigned char *output )
{
unsigned char c;
unsigned char ov[17];
while( length-- )
{
memcpy( ov, iv, 16 );
aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
if( mode == AES_DECRYPT )
ov[16] = *input;
c = *output++ = (unsigned char)( iv[0] ^ *input++ );
if( mode == AES_ENCRYPT )
ov[16] = c;
memcpy( iv, ov + 1, 16 );
}
return 0;
}
/*
* AES-CTR buffer encryption/decryption
*/
int aes_crypt_ctr( AES_CTX *ctx,
size_t length,
size_t *nc_off,
unsigned char nonce_counter[16],
unsigned char stream_block[16],
const unsigned char *input,
unsigned char *output )
{
int c, i;
size_t n = *nc_off;
while( length-- )
{
if( n == 0 ) {
aes_crypt_ecb( ctx, AES_ENCRYPT, nonce_counter, stream_block );
for( i = 16; i > 0; i-- )
if( ++nonce_counter[i - 1] != 0 )
break;
}
c = *input++;
*output++ = (unsigned char)( c ^ stream_block[n] );
n = ( n + 1 ) & 0x0F;
}
*nc_off = n;
return 0;
}
#endif /* AES_ALT_C */

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,102 @@
/*
* FIPS-180-1 compliant SHA-1 implementation
*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* The SHA-1 standard was published by NIST in 1993.
*
* http://www.itl.nist.gov/fipspubs/fip180-1.htm
*/
#include "port/sha1_alt.h"
#if defined(ESP_SHA1_C)
#include <string.h>
/* Implementation that should never be optimized out by the compiler */
static void sha1_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}
void sha1_init( SHA1_CTX *ctx )
{
memset( ctx, 0, sizeof( SHA1_CTX ) );
ets_sha_enable();
}
void sha1_free( SHA1_CTX *ctx )
{
if( ctx == NULL )
return;
sha1_zeroize( ctx, sizeof( SHA1_CTX ) );
ets_sha_disable();
}
void sha1_clone( SHA1_CTX *dst, const SHA1_CTX *src )
{
*dst = *src;
}
void sha1_process(SHA1_CTX *ctx, const unsigned char data[64])
{
}
/*
* SHA-1 context setup
*/
void sha1_starts( SHA1_CTX *ctx )
{
ets_sha_init(&ctx->context);
ctx->context_type = SHA1;
}
/*
* SHA-1 process buffer
*/
void sha1_update( SHA1_CTX *ctx, const unsigned char *input, size_t ilen )
{
ets_sha_update(&ctx->context, ctx->context_type, input, ilen * 8);
}
/*
* SHA-1 final digest
*/
void sha1_finish( SHA1_CTX *ctx, unsigned char output[20] )
{
ets_sha_finish(&ctx->context, ctx->context_type, output);
}
/*
* output = SHA-1( input buffer )
*/
void sha1_output( const unsigned char *input, size_t ilen, unsigned char output[20] )
{
SHA1_CTX ctx;
sha1_init( &ctx );
sha1_starts( &ctx );
sha1_update( &ctx, input, ilen );
sha1_finish( &ctx, output );
sha1_free( &ctx );
}
#endif /* _SHA1_C */

View File

@@ -0,0 +1,107 @@
/*
* FIPS-180-2 compliant SHA-256 implementation
*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* The SHA-256 Secure Hash Standard was published by NIST in 2002.
*
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
*/
#include "port/sha256_alt.h"
#if defined(ESP_SHA256_C)
#include <string.h>
/* Implementation that should never be optimized out by the compiler */
static void sha256_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}
void sha256_init( SHA256_CTX *ctx )
{
memset( ctx, 0, sizeof( SHA256_CTX ) );
ets_sha_enable();
}
void sha256_process(SHA256_CTX *ctx, const unsigned char data[64])
{
}
void sha256_free( SHA256_CTX *ctx )
{
if( ctx == NULL )
return;
sha256_zeroize( ctx, sizeof( SHA256_CTX ) );
ets_sha_disable();
}
void sha256_clone( SHA256_CTX *dst, const SHA256_CTX *src )
{
*dst = *src;
}
/*
* SHA-256 context setup
*/
void sha256_starts( SHA256_CTX *ctx, int is224 )
{
ets_sha_init(&ctx->context);
if( is224 == 0 )
{
/* SHA-256 */
ctx->context_type = SHA256;
}else{
/* SHA-224 */
ctx->context_type = SHA224;
}
}
/*
* SHA-256 process buffer
*/
void sha256_update( SHA256_CTX *ctx, const unsigned char *input, size_t ilen )
{
ets_sha_update(&ctx->context, ctx->context_type, input, ilen * 8);
}
/*
* SHA-256 final digest
*/
void sha256_finish( SHA256_CTX *ctx, unsigned char output[32] )
{
ets_sha_finish(&ctx->context, ctx->context_type, output);
}
/*
* output = SHA-256( input buffer )
*/
void sha256_output( const unsigned char *input, size_t ilen, unsigned char output[32], int is224 )
{
SHA256_CTX ctx;
sha256_init( &ctx );
sha256_starts( &ctx, is224 );
sha256_update( &ctx, input, ilen );
sha256_finish( &ctx, output );
sha256_free( &ctx );
}
#endif /* SHA256_C */

View File

@@ -0,0 +1,103 @@
/*
* FIPS-180-2 compliant SHA-384/512 implementation
*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* The SHA-512 Secure Hash Standard was published by NIST in 2002.
*
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
*/
#include "port/sha512_alt.h"
#if defined(ESP_SHA512_C)
#include <string.h>
/* Implementation that should never be optimized out by the compiler */
static void sha512_zeroize( void *v, size_t n ) {
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}
void sha512_init( SHA512_CTX *ctx )
{
memset( ctx, 0, sizeof( SHA512_CTX ) );
ets_sha_enable();
}
void sha512_free( SHA512_CTX *ctx )
{
if( ctx == NULL )
return;
sha512_zeroize( ctx, sizeof( SHA512_CTX ) );
ets_sha_disable();
}
void sha512_clone( SHA512_CTX *dst, const SHA512_CTX *src )
{
*dst = *src;
}
/*
* SHA-512 context setup
*/
void sha512_starts( SHA512_CTX *ctx, int is384 )
{
ets_sha_init(&ctx->context);
if( is384 == 0 )
{
/* SHA-512 */
ctx->context_type = SHA2_512;
}
else
{
/* SHA-384 */
ctx->context_type = SHA2_384;
}
}
/*
* SHA-512 process buffer
*/
void sha512_update( SHA512_CTX *ctx, const unsigned char *input,size_t ilen )
{
ets_sha_update(&ctx->context, ctx->context_type, input, ilen * 8);
}
/*
* SHA-512 final digest
*/
void sha512_finish( SHA512_CTX *ctx, unsigned char output[64] )
{
ets_sha_finish(&ctx->context, ctx->context_type, output);
}
/*
* output = SHA-512( input buffer )
*/
void sha512_output( const unsigned char *input, size_t ilen,unsigned char output[64], int is384 )
{
SHA512_CTX ctx;
sha512_init( &ctx );
sha512_starts( &ctx, is384 );
sha512_update( &ctx, input, ilen );
sha512_finish( &ctx, output );
sha512_free( &ctx );
}
#endif /* SHA512_C */