mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-30 22:43:08 +03:00
The Great Renaming
A simple execution of tmp/invoke-rename.pl
This commit is contained in:
186
library/pkcs5.c
186
library/pkcs5.c
@ -30,13 +30,13 @@
|
||||
* http://tools.ietf.org/html/rfc6070 (Test vectors)
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#if !defined(MBEDTLS_CONFIG_FILE)
|
||||
#include "mbedtls/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#include MBEDTLS_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PKCS5_C)
|
||||
#if defined(MBEDTLS_PKCS5_C)
|
||||
|
||||
#include "mbedtls/pkcs5.h"
|
||||
#include "mbedtls/asn1.h"
|
||||
@ -45,25 +45,25 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
#include "mbedtls/platform.h"
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#define polarssl_printf printf
|
||||
#define mbedtls_printf printf
|
||||
#endif
|
||||
|
||||
static int pkcs5_parse_pbkdf2_params( const asn1_buf *params,
|
||||
asn1_buf *salt, int *iterations,
|
||||
int *keylen, md_type_t *md_type )
|
||||
static int pkcs5_parse_pbkdf2_params( const mbedtls_asn1_buf *params,
|
||||
mbedtls_asn1_buf *salt, int *iterations,
|
||||
int *keylen, mbedtls_md_type_t *md_type )
|
||||
{
|
||||
int ret;
|
||||
asn1_buf prf_alg_oid;
|
||||
mbedtls_asn1_buf prf_alg_oid;
|
||||
unsigned char *p = params->p;
|
||||
const unsigned char *end = params->p + params->len;
|
||||
|
||||
if( params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
|
||||
return( POLARSSL_ERR_PKCS5_INVALID_FORMAT +
|
||||
POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
|
||||
if( params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
|
||||
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
|
||||
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
|
||||
/*
|
||||
* PBKDF2-params ::= SEQUENCE {
|
||||
* salt OCTET STRING,
|
||||
@ -73,59 +73,59 @@ static int pkcs5_parse_pbkdf2_params( const asn1_buf *params,
|
||||
* }
|
||||
*
|
||||
*/
|
||||
if( ( ret = asn1_get_tag( &p, end, &salt->len, ASN1_OCTET_STRING ) ) != 0 )
|
||||
return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
|
||||
if( ( ret = mbedtls_asn1_get_tag( &p, end, &salt->len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
|
||||
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
|
||||
|
||||
salt->p = p;
|
||||
p += salt->len;
|
||||
|
||||
if( ( ret = asn1_get_int( &p, end, iterations ) ) != 0 )
|
||||
return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
|
||||
if( ( ret = mbedtls_asn1_get_int( &p, end, iterations ) ) != 0 )
|
||||
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
|
||||
|
||||
if( p == end )
|
||||
return( 0 );
|
||||
|
||||
if( ( ret = asn1_get_int( &p, end, keylen ) ) != 0 )
|
||||
if( ( ret = mbedtls_asn1_get_int( &p, end, keylen ) ) != 0 )
|
||||
{
|
||||
if( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
|
||||
return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
|
||||
if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
||||
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
|
||||
}
|
||||
|
||||
if( p == end )
|
||||
return( 0 );
|
||||
|
||||
if( ( ret = asn1_get_alg_null( &p, end, &prf_alg_oid ) ) != 0 )
|
||||
return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
|
||||
if( ( ret = mbedtls_asn1_get_alg_null( &p, end, &prf_alg_oid ) ) != 0 )
|
||||
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
|
||||
|
||||
if( OID_CMP( OID_HMAC_SHA1, &prf_alg_oid ) != 0 )
|
||||
return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
|
||||
if( MBEDTLS_OID_CMP( MBEDTLS_OID_HMAC_SHA1, &prf_alg_oid ) != 0 )
|
||||
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
|
||||
|
||||
*md_type = POLARSSL_MD_SHA1;
|
||||
*md_type = MBEDTLS_MD_SHA1;
|
||||
|
||||
if( p != end )
|
||||
return( POLARSSL_ERR_PKCS5_INVALID_FORMAT +
|
||||
POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
|
||||
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
|
||||
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
int pkcs5_pbes2( const asn1_buf *pbe_params, int mode,
|
||||
int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
|
||||
const unsigned char *pwd, size_t pwdlen,
|
||||
const unsigned char *data, size_t datalen,
|
||||
unsigned char *output )
|
||||
{
|
||||
int ret, iterations = 0, keylen = 0;
|
||||
unsigned char *p, *end;
|
||||
asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
|
||||
asn1_buf salt;
|
||||
md_type_t md_type = POLARSSL_MD_SHA1;
|
||||
mbedtls_asn1_buf kdf_alg_oid, enc_scheme_oid, kdf_alg_params, enc_scheme_params;
|
||||
mbedtls_asn1_buf salt;
|
||||
mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
|
||||
unsigned char key[32], iv[32];
|
||||
size_t olen = 0;
|
||||
const md_info_t *md_info;
|
||||
const cipher_info_t *cipher_info;
|
||||
md_context_t md_ctx;
|
||||
cipher_type_t cipher_alg;
|
||||
cipher_context_t cipher_ctx;
|
||||
const mbedtls_md_info_t *md_info;
|
||||
const mbedtls_cipher_info_t *cipher_info;
|
||||
mbedtls_md_context_t md_ctx;
|
||||
mbedtls_cipher_type_t cipher_alg;
|
||||
mbedtls_cipher_context_t cipher_ctx;
|
||||
|
||||
p = pbe_params->p;
|
||||
end = p + pbe_params->len;
|
||||
@ -136,17 +136,17 @@ int pkcs5_pbes2( const asn1_buf *pbe_params, int mode,
|
||||
* encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
|
||||
* }
|
||||
*/
|
||||
if( pbe_params->tag != ( ASN1_CONSTRUCTED | ASN1_SEQUENCE ) )
|
||||
return( POLARSSL_ERR_PKCS5_INVALID_FORMAT +
|
||||
POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
|
||||
if( pbe_params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
|
||||
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT +
|
||||
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
|
||||
|
||||
if( ( ret = asn1_get_alg( &p, end, &kdf_alg_oid, &kdf_alg_params ) ) != 0 )
|
||||
return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
|
||||
if( ( ret = mbedtls_asn1_get_alg( &p, end, &kdf_alg_oid, &kdf_alg_params ) ) != 0 )
|
||||
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
|
||||
|
||||
// Only PBKDF2 supported at the moment
|
||||
//
|
||||
if( OID_CMP( OID_PKCS5_PBKDF2, &kdf_alg_oid ) != 0 )
|
||||
return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
|
||||
if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBKDF2, &kdf_alg_oid ) != 0 )
|
||||
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
|
||||
|
||||
if( ( ret = pkcs5_parse_pbkdf2_params( &kdf_alg_params,
|
||||
&salt, &iterations, &keylen,
|
||||
@ -155,22 +155,22 @@ int pkcs5_pbes2( const asn1_buf *pbe_params, int mode,
|
||||
return( ret );
|
||||
}
|
||||
|
||||
md_info = md_info_from_type( md_type );
|
||||
md_info = mbedtls_md_info_from_type( md_type );
|
||||
if( md_info == NULL )
|
||||
return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
|
||||
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
|
||||
|
||||
if( ( ret = asn1_get_alg( &p, end, &enc_scheme_oid,
|
||||
if( ( ret = mbedtls_asn1_get_alg( &p, end, &enc_scheme_oid,
|
||||
&enc_scheme_params ) ) != 0 )
|
||||
{
|
||||
return( POLARSSL_ERR_PKCS5_INVALID_FORMAT + ret );
|
||||
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT + ret );
|
||||
}
|
||||
|
||||
if( oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 )
|
||||
return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
|
||||
if( mbedtls_oid_get_cipher_alg( &enc_scheme_oid, &cipher_alg ) != 0 )
|
||||
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
|
||||
|
||||
cipher_info = cipher_info_from_type( cipher_alg );
|
||||
cipher_info = mbedtls_cipher_info_from_type( cipher_alg );
|
||||
if( cipher_info == NULL )
|
||||
return( POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE );
|
||||
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );
|
||||
|
||||
/*
|
||||
* The value of keylen from pkcs5_parse_pbkdf2_params() is ignored
|
||||
@ -178,53 +178,53 @@ int pkcs5_pbes2( const asn1_buf *pbe_params, int mode,
|
||||
*/
|
||||
keylen = cipher_info->key_length / 8;
|
||||
|
||||
if( enc_scheme_params.tag != ASN1_OCTET_STRING ||
|
||||
if( enc_scheme_params.tag != MBEDTLS_ASN1_OCTET_STRING ||
|
||||
enc_scheme_params.len != cipher_info->iv_size )
|
||||
{
|
||||
return( POLARSSL_ERR_PKCS5_INVALID_FORMAT );
|
||||
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT );
|
||||
}
|
||||
|
||||
md_init( &md_ctx );
|
||||
cipher_init( &cipher_ctx );
|
||||
mbedtls_md_init( &md_ctx );
|
||||
mbedtls_cipher_init( &cipher_ctx );
|
||||
|
||||
memcpy( iv, enc_scheme_params.p, enc_scheme_params.len );
|
||||
|
||||
if( ( ret = md_setup( &md_ctx, md_info, 1 ) ) != 0 )
|
||||
if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
|
||||
goto exit;
|
||||
|
||||
if( ( ret = pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len,
|
||||
if( ( ret = mbedtls_pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len,
|
||||
iterations, keylen, key ) ) != 0 )
|
||||
{
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 )
|
||||
if( ( ret = mbedtls_cipher_init_ctx( &cipher_ctx, cipher_info ) ) != 0 )
|
||||
goto exit;
|
||||
|
||||
if( ( ret = cipher_setkey( &cipher_ctx, key, 8 * keylen, (operation_t) mode ) ) != 0 )
|
||||
if( ( ret = mbedtls_cipher_setkey( &cipher_ctx, key, 8 * keylen, (mbedtls_operation_t) mode ) ) != 0 )
|
||||
goto exit;
|
||||
|
||||
if( ( ret = cipher_crypt( &cipher_ctx, iv, enc_scheme_params.len,
|
||||
if( ( ret = mbedtls_cipher_crypt( &cipher_ctx, iv, enc_scheme_params.len,
|
||||
data, datalen, output, &olen ) ) != 0 )
|
||||
ret = POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH;
|
||||
ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH;
|
||||
|
||||
exit:
|
||||
md_free( &md_ctx );
|
||||
cipher_free( &cipher_ctx );
|
||||
mbedtls_md_free( &md_ctx );
|
||||
mbedtls_cipher_free( &cipher_ctx );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
|
||||
int pkcs5_pbkdf2_hmac( md_context_t *ctx, const unsigned char *password,
|
||||
int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx, const unsigned char *password,
|
||||
size_t plen, const unsigned char *salt, size_t slen,
|
||||
unsigned int iteration_count,
|
||||
uint32_t key_length, unsigned char *output )
|
||||
{
|
||||
int ret, j;
|
||||
unsigned int i;
|
||||
unsigned char md1[POLARSSL_MD_MAX_SIZE];
|
||||
unsigned char work[POLARSSL_MD_MAX_SIZE];
|
||||
unsigned char md_size = md_get_size( ctx->md_info );
|
||||
unsigned char md1[MBEDTLS_MD_MAX_SIZE];
|
||||
unsigned char work[MBEDTLS_MD_MAX_SIZE];
|
||||
unsigned char md_size = mbedtls_md_get_size( ctx->md_info );
|
||||
size_t use_len;
|
||||
unsigned char *out_p = output;
|
||||
unsigned char counter[4];
|
||||
@ -233,22 +233,22 @@ int pkcs5_pbkdf2_hmac( md_context_t *ctx, const unsigned char *password,
|
||||
counter[3] = 1;
|
||||
|
||||
if( iteration_count > 0xFFFFFFFF )
|
||||
return( POLARSSL_ERR_PKCS5_BAD_INPUT_DATA );
|
||||
return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
|
||||
|
||||
while( key_length )
|
||||
{
|
||||
// U1 ends up in work
|
||||
//
|
||||
if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
|
||||
if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
if( ( ret = md_hmac_update( ctx, salt, slen ) ) != 0 )
|
||||
if( ( ret = mbedtls_md_hmac_update( ctx, salt, slen ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
if( ( ret = md_hmac_update( ctx, counter, 4 ) ) != 0 )
|
||||
if( ( ret = mbedtls_md_hmac_update( ctx, counter, 4 ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
if( ( ret = md_hmac_finish( ctx, work ) ) != 0 )
|
||||
if( ( ret = mbedtls_md_hmac_finish( ctx, work ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
memcpy( md1, work, md_size );
|
||||
@ -257,13 +257,13 @@ int pkcs5_pbkdf2_hmac( md_context_t *ctx, const unsigned char *password,
|
||||
{
|
||||
// U2 ends up in md1
|
||||
//
|
||||
if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
|
||||
if( ( ret = mbedtls_md_hmac_starts( ctx, password, plen ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
if( ( ret = md_hmac_update( ctx, md1, md_size ) ) != 0 )
|
||||
if( ( ret = mbedtls_md_hmac_update( ctx, md1, md_size ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
if( ( ret = md_hmac_finish( ctx, md1 ) ) != 0 )
|
||||
if( ( ret = mbedtls_md_hmac_finish( ctx, md1 ) ) != 0 )
|
||||
return( ret );
|
||||
|
||||
// U1 xor U2
|
||||
@ -286,13 +286,13 @@ int pkcs5_pbkdf2_hmac( md_context_t *ctx, const unsigned char *password,
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
#if defined(POLARSSL_SELF_TEST)
|
||||
#if defined(MBEDTLS_SELF_TEST)
|
||||
|
||||
#if !defined(POLARSSL_SHA1_C)
|
||||
int pkcs5_self_test( int verbose )
|
||||
#if !defined(MBEDTLS_SHA1_C)
|
||||
int mbedtls_pkcs5_self_test( int verbose )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( " PBKDF2 (SHA1): skipped\n\n" );
|
||||
mbedtls_printf( " PBKDF2 (SHA1): skipped\n\n" );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
@ -349,23 +349,23 @@ static const unsigned char result_key[MAX_TESTS][32] =
|
||||
0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
|
||||
};
|
||||
|
||||
int pkcs5_self_test( int verbose )
|
||||
int mbedtls_pkcs5_self_test( int verbose )
|
||||
{
|
||||
md_context_t sha1_ctx;
|
||||
const md_info_t *info_sha1;
|
||||
mbedtls_md_context_t sha1_ctx;
|
||||
const mbedtls_md_info_t *info_sha1;
|
||||
int ret, i;
|
||||
unsigned char key[64];
|
||||
|
||||
md_init( &sha1_ctx );
|
||||
mbedtls_md_init( &sha1_ctx );
|
||||
|
||||
info_sha1 = md_info_from_type( POLARSSL_MD_SHA1 );
|
||||
info_sha1 = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
|
||||
if( info_sha1 == NULL )
|
||||
{
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( ( ret = md_setup( &sha1_ctx, info_sha1, 1 ) ) != 0 )
|
||||
if( ( ret = mbedtls_md_setup( &sha1_ctx, info_sha1, 1 ) ) != 0 )
|
||||
{
|
||||
ret = 1;
|
||||
goto exit;
|
||||
@ -374,33 +374,33 @@ int pkcs5_self_test( int verbose )
|
||||
for( i = 0; i < MAX_TESTS; i++ )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( " PBKDF2 (SHA1) #%d: ", i );
|
||||
mbedtls_printf( " PBKDF2 (SHA1) #%d: ", i );
|
||||
|
||||
ret = pkcs5_pbkdf2_hmac( &sha1_ctx, password[i], plen[i], salt[i],
|
||||
ret = mbedtls_pkcs5_pbkdf2_hmac( &sha1_ctx, password[i], plen[i], salt[i],
|
||||
slen[i], it_cnt[i], key_len[i], key );
|
||||
if( ret != 0 ||
|
||||
memcmp( result_key[i], key, key_len[i] ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "failed\n" );
|
||||
mbedtls_printf( "failed\n" );
|
||||
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
polarssl_printf( "passed\n" );
|
||||
mbedtls_printf( "passed\n" );
|
||||
}
|
||||
|
||||
polarssl_printf( "\n" );
|
||||
mbedtls_printf( "\n" );
|
||||
|
||||
exit:
|
||||
md_free( &sha1_ctx );
|
||||
mbedtls_md_free( &sha1_ctx );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
#endif /* POLARSSL_SHA1_C */
|
||||
#endif /* MBEDTLS_SHA1_C */
|
||||
|
||||
#endif /* POLARSSL_SELF_TEST */
|
||||
#endif /* MBEDTLS_SELF_TEST */
|
||||
|
||||
#endif /* POLARSSL_PKCS5_C */
|
||||
#endif /* MBEDTLS_PKCS5_C */
|
||||
|
Reference in New Issue
Block a user