mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-11-25 19:25:45 +01:00
PKCS#5 module added. Moved PBKDF2 functionality inside and deprecated
old PBKDF2 module.
(cherry picked from commit 19bd297dc8
)
Conflicts:
include/polarssl/error.h
scripts/generate_errors.pl
This commit is contained in:
parent
fc4f46fa9a
commit
b0c19a4b3d
@ -916,10 +916,11 @@
|
|||||||
* \def POLARSSL_PBKDF2_C
|
* \def POLARSSL_PBKDF2_C
|
||||||
*
|
*
|
||||||
* Enable PKCS#5 PBKDF2 key derivation function
|
* Enable PKCS#5 PBKDF2 key derivation function
|
||||||
|
* DEPRECATED: Use POLARSSL_PKCS5_C instead
|
||||||
*
|
*
|
||||||
* Module: library/pbkdf2.c
|
* Module: library/pbkdf2.c
|
||||||
*
|
*
|
||||||
* Requires: POLARSSL_MD_C
|
* Requires: POLARSSL_PKCS5_C
|
||||||
*
|
*
|
||||||
* This module adds support for the PKCS#5 PBKDF2 key derivation function.
|
* This module adds support for the PKCS#5 PBKDF2 key derivation function.
|
||||||
*/
|
*/
|
||||||
@ -939,6 +940,19 @@
|
|||||||
*/
|
*/
|
||||||
#define POLARSSL_PEM_C
|
#define POLARSSL_PEM_C
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \def POLARSSL_PKCS5_C
|
||||||
|
*
|
||||||
|
* Enable PKCS#5 functions
|
||||||
|
*
|
||||||
|
* Module: library/pkcs5.c
|
||||||
|
*
|
||||||
|
* Requires: POLARSSL_MD_C
|
||||||
|
*
|
||||||
|
* This module adds support for the PKCS#5 functions.
|
||||||
|
*/
|
||||||
|
#define POLARSSL_PKCS5_C
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \def POLARSSL_PKCS11_C
|
* \def POLARSSL_PKCS11_C
|
||||||
*
|
*
|
||||||
|
@ -72,6 +72,7 @@
|
|||||||
* SHA4 1 0x007A-0x007A
|
* SHA4 1 0x007A-0x007A
|
||||||
* PBKDF2 1 0x007C-0x007C
|
* PBKDF2 1 0x007C-0x007C
|
||||||
* ECP 1 0x007E-0x007E
|
* ECP 1 0x007E-0x007E
|
||||||
|
* PKCS5 1 0x007C-0x007C
|
||||||
*
|
*
|
||||||
* High-level module nr (3 bits - 0x1...-0x8...)
|
* High-level module nr (3 bits - 0x1...-0x8...)
|
||||||
* Name ID Nr of Errors
|
* Name ID Nr of Errors
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
* \file pbkdf2.h
|
* \file pbkdf2.h
|
||||||
*
|
*
|
||||||
* \brief Password-Based Key Derivation Function 2 (from PKCS#5)
|
* \brief Password-Based Key Derivation Function 2 (from PKCS#5)
|
||||||
|
* DEPRECATED: use pkcs5.h instead.
|
||||||
*
|
*
|
||||||
* \author Mathias Olsson <mathias@kompetensum.com>
|
* \author Mathias Olsson <mathias@kompetensum.com>
|
||||||
*
|
*
|
||||||
@ -48,6 +49,7 @@ extern "C" {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief PKCS#5 PBKDF2 using HMAC
|
* \brief PKCS#5 PBKDF2 using HMAC
|
||||||
|
* DEPRECATED: Use pkcs5_pbkdf2_hmac() instead!
|
||||||
*
|
*
|
||||||
* \param ctx Generic HMAC context
|
* \param ctx Generic HMAC context
|
||||||
* \param password Password to use when generating key
|
* \param password Password to use when generating key
|
||||||
@ -65,9 +67,9 @@ int pbkdf2_hmac( md_context_t *ctx, const unsigned char *password,
|
|||||||
unsigned int iteration_count,
|
unsigned int iteration_count,
|
||||||
uint32_t key_length, unsigned char *output );
|
uint32_t key_length, unsigned char *output );
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Checkup routine
|
* \brief Checkup routine
|
||||||
|
* DEPRECATED: Use pkcs5_self_test() instead!
|
||||||
*
|
*
|
||||||
* \return 0 if successful, or 1 if the test failed
|
* \return 0 if successful, or 1 if the test failed
|
||||||
*/
|
*/
|
||||||
|
79
include/polarssl/pkcs5.h
Normal file
79
include/polarssl/pkcs5.h
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
/**
|
||||||
|
* \file pkcs#5.h
|
||||||
|
*
|
||||||
|
* \brief PKCS#5 functions
|
||||||
|
*
|
||||||
|
* \author Mathias Olsson <mathias@kompetensum.com>
|
||||||
|
*
|
||||||
|
* Copyright (C) 2006-2013, Brainspark B.V.
|
||||||
|
*
|
||||||
|
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||||
|
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||||
|
*
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*/
|
||||||
|
#ifndef POLARSSL_PKCS5_H
|
||||||
|
#define POLARSSL_PKCS5_H
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "md.h"
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#include <basetsd.h>
|
||||||
|
typedef UINT32 uint32_t;
|
||||||
|
#else
|
||||||
|
#include <inttypes.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define POLARSSL_ERR_PKCS5_BAD_INPUT_DATA -0x007C /**< Bad input parameters to function. */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief PKCS#5 PBKDF2 using HMAC
|
||||||
|
*
|
||||||
|
* \param ctx Generic HMAC context
|
||||||
|
* \param password Password to use when generating key
|
||||||
|
* \param plen Length of password
|
||||||
|
* \param salt Salt to use when generating key
|
||||||
|
* \param slen Length of salt
|
||||||
|
* \param iteration_count Iteration count
|
||||||
|
* \param key_length Length of generated key
|
||||||
|
* \param output Generated key. Must be at least as big as key_length
|
||||||
|
*
|
||||||
|
* \returns 0 on success, or a PolarSSL error code if verification fails.
|
||||||
|
*/
|
||||||
|
int pkcs5_pbkdf2_hmac( 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 );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Checkup routine
|
||||||
|
*
|
||||||
|
* \return 0 if successful, or 1 if the test failed
|
||||||
|
*/
|
||||||
|
int pkcs5_self_test( int verbose );
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* pkcs5.h */
|
@ -34,6 +34,7 @@ set(src
|
|||||||
padlock.c
|
padlock.c
|
||||||
pbkdf2.c
|
pbkdf2.c
|
||||||
pem.c
|
pem.c
|
||||||
|
pkcs5.c
|
||||||
pkcs11.c
|
pkcs11.c
|
||||||
pkcs12.c
|
pkcs12.c
|
||||||
rsa.c
|
rsa.c
|
||||||
|
@ -47,7 +47,7 @@ OBJS= aes.o arc4.o asn1parse.o \
|
|||||||
md4.o md5.o net.o \
|
md4.o md5.o net.o \
|
||||||
oid.o \
|
oid.o \
|
||||||
padlock.o pbkdf2.o pem.o \
|
padlock.o pbkdf2.o pem.o \
|
||||||
pkcs11.o pkcs12.o \
|
pkcs5.o pkcs11.o pkcs12.o \
|
||||||
rsa.o sha1.o sha2.o \
|
rsa.o sha1.o sha2.o \
|
||||||
sha4.o ssl_cache.o ssl_cli.o \
|
sha4.o ssl_cache.o ssl_cli.o \
|
||||||
ssl_srv.o ssl_ciphersuites.o \
|
ssl_srv.o ssl_ciphersuites.o \
|
||||||
|
@ -117,6 +117,10 @@
|
|||||||
#include "polarssl/pkcs12.h"
|
#include "polarssl/pkcs12.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(POLARSSL_PKCS5_C)
|
||||||
|
#include "polarssl/pkcs5.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(POLARSSL_RSA_C)
|
#if defined(POLARSSL_RSA_C)
|
||||||
#include "polarssl/rsa.h"
|
#include "polarssl/rsa.h"
|
||||||
#endif
|
#endif
|
||||||
@ -566,6 +570,11 @@ void error_strerror( int ret, char *buf, size_t buflen )
|
|||||||
snprintf( buf, buflen, "PBKDF2 - Bad input parameters to function" );
|
snprintf( buf, buflen, "PBKDF2 - Bad input parameters to function" );
|
||||||
#endif /* POLARSSL_PBKDF2_C */
|
#endif /* POLARSSL_PBKDF2_C */
|
||||||
|
|
||||||
|
#if defined(POLARSSL_PKCS5_C)
|
||||||
|
if( use_ret == -(POLARSSL_ERR_PKCS5_BAD_INPUT_DATA) )
|
||||||
|
snprintf( buf, buflen, "PKCS5 - Bad input parameters to function" );
|
||||||
|
#endif /* POLARSSL_PKCS5_C */
|
||||||
|
|
||||||
#if defined(POLARSSL_SHA1_C)
|
#if defined(POLARSSL_SHA1_C)
|
||||||
if( use_ret == -(POLARSSL_ERR_SHA1_FILE_IO_ERROR) )
|
if( use_ret == -(POLARSSL_ERR_SHA1_FILE_IO_ERROR) )
|
||||||
snprintf( buf, buflen, "SHA1 - Read/write error in file" );
|
snprintf( buf, buflen, "SHA1 - Read/write error in file" );
|
||||||
|
165
library/pbkdf2.c
165
library/pbkdf2.c
@ -2,6 +2,7 @@
|
|||||||
* \file pbkdf2.c
|
* \file pbkdf2.c
|
||||||
*
|
*
|
||||||
* \brief Password-Based Key Derivation Function 2 (from PKCS#5)
|
* \brief Password-Based Key Derivation Function 2 (from PKCS#5)
|
||||||
|
* DEPRECATED: Use pkcs5.c instead
|
||||||
*
|
*
|
||||||
* \author Mathias Olsson <mathias@kompetensum.com>
|
* \author Mathias Olsson <mathias@kompetensum.com>
|
||||||
*
|
*
|
||||||
@ -38,178 +39,22 @@
|
|||||||
#if defined(POLARSSL_PBKDF2_C)
|
#if defined(POLARSSL_PBKDF2_C)
|
||||||
|
|
||||||
#include "polarssl/pbkdf2.h"
|
#include "polarssl/pbkdf2.h"
|
||||||
|
#include "polarssl/pkcs5.h"
|
||||||
|
|
||||||
int pbkdf2_hmac( md_context_t *ctx, const unsigned char *password, size_t plen,
|
int pbkdf2_hmac( md_context_t *ctx, const unsigned char *password, size_t plen,
|
||||||
const unsigned char *salt, size_t slen,
|
const unsigned char *salt, size_t slen,
|
||||||
unsigned int iteration_count,
|
unsigned int iteration_count,
|
||||||
uint32_t key_length, unsigned char *output )
|
uint32_t key_length, unsigned char *output )
|
||||||
{
|
{
|
||||||
int ret, j;
|
return pkcs5_pbkdf2_hmac( ctx, password, plen, salt, slen, iteration_count,
|
||||||
unsigned int i;
|
key_length, output );
|
||||||
unsigned char md1[POLARSSL_MD_MAX_SIZE];
|
|
||||||
unsigned char work[POLARSSL_MD_MAX_SIZE];
|
|
||||||
unsigned char md_size = md_get_size( ctx->md_info );
|
|
||||||
size_t use_len;
|
|
||||||
unsigned char *out_p = output;
|
|
||||||
unsigned char counter[4];
|
|
||||||
|
|
||||||
memset( counter, 0, 4 );
|
|
||||||
counter[3] = 1;
|
|
||||||
|
|
||||||
if( iteration_count > 0xFFFFFFFF )
|
|
||||||
return( POLARSSL_ERR_PBKDF2_BAD_INPUT_DATA );
|
|
||||||
|
|
||||||
while( key_length )
|
|
||||||
{
|
|
||||||
// U1 ends up in work
|
|
||||||
//
|
|
||||||
if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
|
|
||||||
return( ret );
|
|
||||||
|
|
||||||
if( ( ret = md_hmac_update( ctx, salt, slen ) ) != 0 )
|
|
||||||
return( ret );
|
|
||||||
|
|
||||||
if( ( ret = md_hmac_update( ctx, counter, 4 ) ) != 0 )
|
|
||||||
return( ret );
|
|
||||||
|
|
||||||
if( ( ret = md_hmac_finish( ctx, work ) ) != 0 )
|
|
||||||
return( ret );
|
|
||||||
|
|
||||||
memcpy( md1, work, md_size );
|
|
||||||
|
|
||||||
for ( i = 1; i < iteration_count; i++ )
|
|
||||||
{
|
|
||||||
// U2 ends up in md1
|
|
||||||
//
|
|
||||||
if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
|
|
||||||
return( ret );
|
|
||||||
|
|
||||||
if( ( ret = md_hmac_update( ctx, md1, md_size ) ) != 0 )
|
|
||||||
return( ret );
|
|
||||||
|
|
||||||
if( ( ret = md_hmac_finish( ctx, md1 ) ) != 0 )
|
|
||||||
return( ret );
|
|
||||||
|
|
||||||
// U1 xor U2
|
|
||||||
//
|
|
||||||
for( j = 0; j < md_size; j++ )
|
|
||||||
work[j] ^= md1[j];
|
|
||||||
}
|
|
||||||
|
|
||||||
use_len = ( key_length < md_size ) ? key_length : md_size;
|
|
||||||
memcpy( out_p, work, use_len );
|
|
||||||
|
|
||||||
key_length -= use_len;
|
|
||||||
out_p += use_len;
|
|
||||||
|
|
||||||
for( i = 4; i > 0; i-- )
|
|
||||||
if( ++counter[i - 1] != 0 )
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return( 0 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#if defined(POLARSSL_SELF_TEST)
|
#if defined(POLARSSL_SELF_TEST)
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
#define MAX_TESTS 6
|
|
||||||
|
|
||||||
size_t plen[MAX_TESTS] =
|
|
||||||
{ 8, 8, 8, 8, 24, 9 };
|
|
||||||
|
|
||||||
unsigned char password[MAX_TESTS][32] =
|
|
||||||
{
|
|
||||||
"password",
|
|
||||||
"password",
|
|
||||||
"password",
|
|
||||||
"password",
|
|
||||||
"passwordPASSWORDpassword",
|
|
||||||
"pass\0word",
|
|
||||||
};
|
|
||||||
|
|
||||||
size_t slen[MAX_TESTS] =
|
|
||||||
{ 4, 4, 4, 4, 36, 5 };
|
|
||||||
|
|
||||||
unsigned char salt[MAX_TESTS][40] =
|
|
||||||
{
|
|
||||||
"salt",
|
|
||||||
"salt",
|
|
||||||
"salt",
|
|
||||||
"salt",
|
|
||||||
"saltSALTsaltSALTsaltSALTsaltSALTsalt",
|
|
||||||
"sa\0lt",
|
|
||||||
};
|
|
||||||
|
|
||||||
uint32_t it_cnt[MAX_TESTS] =
|
|
||||||
{ 1, 2, 4096, 16777216, 4096, 4096 };
|
|
||||||
|
|
||||||
uint32_t key_len[MAX_TESTS] =
|
|
||||||
{ 20, 20, 20, 20, 25, 16 };
|
|
||||||
|
|
||||||
|
|
||||||
unsigned char result_key[MAX_TESTS][32] =
|
|
||||||
{
|
|
||||||
{ 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
|
|
||||||
0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
|
|
||||||
0x2f, 0xe0, 0x37, 0xa6 },
|
|
||||||
{ 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
|
|
||||||
0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
|
|
||||||
0xd8, 0xde, 0x89, 0x57 },
|
|
||||||
{ 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
|
|
||||||
0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
|
|
||||||
0x65, 0xa4, 0x29, 0xc1 },
|
|
||||||
{ 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4,
|
|
||||||
0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c,
|
|
||||||
0x26, 0x34, 0xe9, 0x84 },
|
|
||||||
{ 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
|
|
||||||
0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
|
|
||||||
0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
|
|
||||||
0x38 },
|
|
||||||
{ 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
|
|
||||||
0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
|
|
||||||
};
|
|
||||||
|
|
||||||
int pbkdf2_self_test( int verbose )
|
int pbkdf2_self_test( int verbose )
|
||||||
{
|
{
|
||||||
md_context_t sha1_ctx;
|
return pkcs5_self_test( verbose );
|
||||||
const md_info_t *info_sha1;
|
|
||||||
int ret, i;
|
|
||||||
unsigned char key[64];
|
|
||||||
|
|
||||||
info_sha1 = md_info_from_type( POLARSSL_MD_SHA1 );
|
|
||||||
if( info_sha1 == NULL )
|
|
||||||
return( 1 );
|
|
||||||
|
|
||||||
if( ( ret = md_init_ctx( &sha1_ctx, info_sha1 ) ) != 0 )
|
|
||||||
return( 1 );
|
|
||||||
|
|
||||||
for( i = 0; i < MAX_TESTS; i++ )
|
|
||||||
{
|
|
||||||
printf( " PBKDF2 (SHA1) #%d: ", i );
|
|
||||||
|
|
||||||
ret = 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 )
|
|
||||||
printf( "failed\n" );
|
|
||||||
|
|
||||||
return( 1 );
|
|
||||||
}
|
|
||||||
|
|
||||||
if( verbose != 0 )
|
|
||||||
printf( "passed\n" );
|
|
||||||
}
|
|
||||||
|
|
||||||
printf( "\n" );
|
|
||||||
|
|
||||||
return( 0 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* POLARSSL_SELF_TEST */
|
#endif /* POLARSSL_SELF_TEST */
|
||||||
|
|
||||||
#endif /* POLARSSL_PBKDF2_C */
|
#endif /* POLARSSL_PBKDF2_C */
|
||||||
|
214
library/pkcs5.c
Normal file
214
library/pkcs5.c
Normal file
@ -0,0 +1,214 @@
|
|||||||
|
/**
|
||||||
|
* \file pkcs5.c
|
||||||
|
*
|
||||||
|
* \brief PKCS#5 functions
|
||||||
|
*
|
||||||
|
* \author Mathias Olsson <mathias@kompetensum.com>
|
||||||
|
*
|
||||||
|
* Copyright (C) 2006-2013, Brainspark B.V.
|
||||||
|
*
|
||||||
|
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||||
|
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||||
|
*
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
* PKCS#5 includes PBKDF2 and more
|
||||||
|
*
|
||||||
|
* http://tools.ietf.org/html/rfc2898 (Specification)
|
||||||
|
* http://tools.ietf.org/html/rfc6070 (Test vectors)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "polarssl/config.h"
|
||||||
|
|
||||||
|
#if defined(POLARSSL_PKCS5_C)
|
||||||
|
|
||||||
|
#include "polarssl/pkcs5.h"
|
||||||
|
|
||||||
|
int pkcs5_pbkdf2_hmac( 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 );
|
||||||
|
size_t use_len;
|
||||||
|
unsigned char *out_p = output;
|
||||||
|
unsigned char counter[4];
|
||||||
|
|
||||||
|
memset( counter, 0, 4 );
|
||||||
|
counter[3] = 1;
|
||||||
|
|
||||||
|
if( iteration_count > 0xFFFFFFFF )
|
||||||
|
return( POLARSSL_ERR_PKCS5_BAD_INPUT_DATA );
|
||||||
|
|
||||||
|
while( key_length )
|
||||||
|
{
|
||||||
|
// U1 ends up in work
|
||||||
|
//
|
||||||
|
if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
|
||||||
|
return( ret );
|
||||||
|
|
||||||
|
if( ( ret = md_hmac_update( ctx, salt, slen ) ) != 0 )
|
||||||
|
return( ret );
|
||||||
|
|
||||||
|
if( ( ret = md_hmac_update( ctx, counter, 4 ) ) != 0 )
|
||||||
|
return( ret );
|
||||||
|
|
||||||
|
if( ( ret = md_hmac_finish( ctx, work ) ) != 0 )
|
||||||
|
return( ret );
|
||||||
|
|
||||||
|
memcpy( md1, work, md_size );
|
||||||
|
|
||||||
|
for ( i = 1; i < iteration_count; i++ )
|
||||||
|
{
|
||||||
|
// U2 ends up in md1
|
||||||
|
//
|
||||||
|
if( ( ret = md_hmac_starts( ctx, password, plen ) ) != 0 )
|
||||||
|
return( ret );
|
||||||
|
|
||||||
|
if( ( ret = md_hmac_update( ctx, md1, md_size ) ) != 0 )
|
||||||
|
return( ret );
|
||||||
|
|
||||||
|
if( ( ret = md_hmac_finish( ctx, md1 ) ) != 0 )
|
||||||
|
return( ret );
|
||||||
|
|
||||||
|
// U1 xor U2
|
||||||
|
//
|
||||||
|
for( j = 0; j < md_size; j++ )
|
||||||
|
work[j] ^= md1[j];
|
||||||
|
}
|
||||||
|
|
||||||
|
use_len = ( key_length < md_size ) ? key_length : md_size;
|
||||||
|
memcpy( out_p, work, use_len );
|
||||||
|
|
||||||
|
key_length -= use_len;
|
||||||
|
out_p += use_len;
|
||||||
|
|
||||||
|
for( i = 4; i > 0; i-- )
|
||||||
|
if( ++counter[i - 1] != 0 )
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return( 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(POLARSSL_SELF_TEST)
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define MAX_TESTS 6
|
||||||
|
|
||||||
|
size_t plen[MAX_TESTS] =
|
||||||
|
{ 8, 8, 8, 8, 24, 9 };
|
||||||
|
|
||||||
|
unsigned char password[MAX_TESTS][32] =
|
||||||
|
{
|
||||||
|
"password",
|
||||||
|
"password",
|
||||||
|
"password",
|
||||||
|
"password",
|
||||||
|
"passwordPASSWORDpassword",
|
||||||
|
"pass\0word",
|
||||||
|
};
|
||||||
|
|
||||||
|
size_t slen[MAX_TESTS] =
|
||||||
|
{ 4, 4, 4, 4, 36, 5 };
|
||||||
|
|
||||||
|
unsigned char salt[MAX_TESTS][40] =
|
||||||
|
{
|
||||||
|
"salt",
|
||||||
|
"salt",
|
||||||
|
"salt",
|
||||||
|
"salt",
|
||||||
|
"saltSALTsaltSALTsaltSALTsaltSALTsalt",
|
||||||
|
"sa\0lt",
|
||||||
|
};
|
||||||
|
|
||||||
|
uint32_t it_cnt[MAX_TESTS] =
|
||||||
|
{ 1, 2, 4096, 16777216, 4096, 4096 };
|
||||||
|
|
||||||
|
uint32_t key_len[MAX_TESTS] =
|
||||||
|
{ 20, 20, 20, 20, 25, 16 };
|
||||||
|
|
||||||
|
|
||||||
|
unsigned char result_key[MAX_TESTS][32] =
|
||||||
|
{
|
||||||
|
{ 0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
|
||||||
|
0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
|
||||||
|
0x2f, 0xe0, 0x37, 0xa6 },
|
||||||
|
{ 0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
|
||||||
|
0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
|
||||||
|
0xd8, 0xde, 0x89, 0x57 },
|
||||||
|
{ 0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
|
||||||
|
0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
|
||||||
|
0x65, 0xa4, 0x29, 0xc1 },
|
||||||
|
{ 0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4,
|
||||||
|
0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c,
|
||||||
|
0x26, 0x34, 0xe9, 0x84 },
|
||||||
|
{ 0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
|
||||||
|
0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
|
||||||
|
0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
|
||||||
|
0x38 },
|
||||||
|
{ 0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
|
||||||
|
0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3 },
|
||||||
|
};
|
||||||
|
|
||||||
|
int pkcs5_self_test( int verbose )
|
||||||
|
{
|
||||||
|
md_context_t sha1_ctx;
|
||||||
|
const md_info_t *info_sha1;
|
||||||
|
int ret, i;
|
||||||
|
unsigned char key[64];
|
||||||
|
|
||||||
|
info_sha1 = md_info_from_type( POLARSSL_MD_SHA1 );
|
||||||
|
if( info_sha1 == NULL )
|
||||||
|
return( 1 );
|
||||||
|
|
||||||
|
if( ( ret = md_init_ctx( &sha1_ctx, info_sha1 ) ) != 0 )
|
||||||
|
return( 1 );
|
||||||
|
|
||||||
|
for( i = 0; i < MAX_TESTS; i++ )
|
||||||
|
{
|
||||||
|
printf( " PBKDF2 (SHA1) #%d: ", i );
|
||||||
|
|
||||||
|
ret = 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 )
|
||||||
|
printf( "failed\n" );
|
||||||
|
|
||||||
|
return( 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
if( verbose != 0 )
|
||||||
|
printf( "passed\n" );
|
||||||
|
}
|
||||||
|
|
||||||
|
printf( "\n" );
|
||||||
|
|
||||||
|
return( 0 );
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* POLARSSL_SELF_TEST */
|
||||||
|
|
||||||
|
#endif /* POLARSSL_PKCS5_C */
|
@ -9,7 +9,7 @@ my $error_file = shift or die "Missing destination file";
|
|||||||
my $error_format_file = $data_dir.'/error.fmt';
|
my $error_format_file = $data_dir.'/error.fmt';
|
||||||
|
|
||||||
my @low_level_modules = ( "AES", "ASN1", "BLOWFISH", "CAMELLIA", "BIGNUM",
|
my @low_level_modules = ( "AES", "ASN1", "BLOWFISH", "CAMELLIA", "BIGNUM",
|
||||||
"BASE64", "XTEA", "PBKDF2", "OID",
|
"BASE64", "XTEA", "PBKDF2", "OID", "PKCS5",
|
||||||
"PADLOCK", "DES", "NET", "CTR_DRBG", "ENTROPY",
|
"PADLOCK", "DES", "NET", "CTR_DRBG", "ENTROPY",
|
||||||
"MD2", "MD4", "MD5", "SHA1", "SHA2", "SHA4", "GCM" );
|
"MD2", "MD4", "MD5", "SHA1", "SHA2", "SHA4", "GCM" );
|
||||||
my @high_level_modules = ( "PEM", "X509", "DHM", "RSA", "ECP", "MD", "CIPHER", "SSL",
|
my @high_level_modules = ( "PEM", "X509", "DHM", "RSA", "ECP", "MD", "CIPHER", "SSL",
|
||||||
|
@ -64,6 +64,7 @@ add_test_suite(mdx)
|
|||||||
add_test_suite(mpi)
|
add_test_suite(mpi)
|
||||||
add_test_suite(pbkdf2)
|
add_test_suite(pbkdf2)
|
||||||
add_test_suite(pkcs1_v21)
|
add_test_suite(pkcs1_v21)
|
||||||
|
add_test_suite(pkcs5)
|
||||||
add_test_suite(shax)
|
add_test_suite(shax)
|
||||||
add_test_suite(rsa)
|
add_test_suite(rsa)
|
||||||
add_test_suite(version)
|
add_test_suite(version)
|
||||||
|
@ -44,7 +44,7 @@ APPS = test_suite_aes.ecb test_suite_aes.cbc \
|
|||||||
test_suite_hmac_shax \
|
test_suite_hmac_shax \
|
||||||
test_suite_md test_suite_mdx \
|
test_suite_md test_suite_mdx \
|
||||||
test_suite_mpi test_suite_pbkdf2 \
|
test_suite_mpi test_suite_pbkdf2 \
|
||||||
test_suite_pkcs1_v21 \
|
test_suite_pkcs1_v21 test_suite_pkcs5 \
|
||||||
test_suite_rsa test_suite_shax \
|
test_suite_rsa test_suite_shax \
|
||||||
test_suite_x509parse test_suite_x509write \
|
test_suite_x509parse test_suite_x509write \
|
||||||
test_suite_xtea test_suite_version
|
test_suite_xtea test_suite_version
|
||||||
@ -245,6 +245,10 @@ test_suite_pkcs1_v21: test_suite_pkcs1_v21.c ../library/libpolarssl.a
|
|||||||
echo " CC $@.c"
|
echo " CC $@.c"
|
||||||
$(CC) $(CFLAGS) $(OFLAGS) $@.c $(LDFLAGS) -o $@
|
$(CC) $(CFLAGS) $(OFLAGS) $@.c $(LDFLAGS) -o $@
|
||||||
|
|
||||||
|
test_suite_pkcs5: test_suite_pkcs5.c ../library/libpolarssl.a
|
||||||
|
echo " CC $@.c"
|
||||||
|
$(CC) $(CFLAGS) $(OFLAGS) $@.c $(LDFLAGS) -o $@
|
||||||
|
|
||||||
test_suite_rsa: test_suite_rsa.c ../library/libpolarssl.a
|
test_suite_rsa: test_suite_rsa.c ../library/libpolarssl.a
|
||||||
echo " CC $@.c"
|
echo " CC $@.c"
|
||||||
$(CC) $(CFLAGS) $(OFLAGS) $@.c $(LDFLAGS) -o $@
|
$(CC) $(CFLAGS) $(OFLAGS) $@.c $(LDFLAGS) -o $@
|
||||||
|
14
tests/suites/test_suite_pkcs5.data
Normal file
14
tests/suites/test_suite_pkcs5.data
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
PBKDF2 RFC 6070 Test Vector #1 (SHA1)
|
||||||
|
pbkdf2_hmac:POLARSSL_MD_SHA1:"70617373776f7264":"73616c74":1:20:"0c60c80f961f0e71f3a9b524af6012062fe037a6"
|
||||||
|
|
||||||
|
PBKDF2 RFC 6070 Test Vector #2 (SHA1)
|
||||||
|
pbkdf2_hmac:POLARSSL_MD_SHA1:"70617373776f7264":"73616c74":2:20:"ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957"
|
||||||
|
|
||||||
|
PBKDF2 RFC 6070 Test Vector #3 (SHA1)
|
||||||
|
pbkdf2_hmac:POLARSSL_MD_SHA1:"70617373776f7264":"73616c74":4096:20:"4b007901b765489abead49d926f721d065a429c1"
|
||||||
|
|
||||||
|
PBKDF2 RFC 6070 Test Vector #5 (SHA1)
|
||||||
|
pbkdf2_hmac:POLARSSL_MD_SHA1:"70617373776f726450415353574f524470617373776f7264":"73616c7453414c5473616c7453414c5473616c7453414c5473616c7453414c5473616c74":4096:25:"3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038"
|
||||||
|
|
||||||
|
PBKDF2 RFC 6070 Test Vector #6 (SHA1)
|
||||||
|
pbkdf2_hmac:POLARSSL_MD_SHA1:"7061737300776f7264":"7361006c74":4096:16:"56fa6aa75548099dcc37d7f03425e0c3"
|
39
tests/suites/test_suite_pkcs5.function
Normal file
39
tests/suites/test_suite_pkcs5.function
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
BEGIN_HEADER
|
||||||
|
#include <polarssl/pkcs5.h>
|
||||||
|
END_HEADER
|
||||||
|
|
||||||
|
BEGIN_DEPENDENCIES
|
||||||
|
depends_on:POLARSSL_PKCS5_C
|
||||||
|
END_DEPENDENCIES
|
||||||
|
|
||||||
|
BEGIN_CASE
|
||||||
|
pbkdf2_hmac:hash:hex_password_string:hex_salt_string:it_cnt:key_len:result_key_string
|
||||||
|
{
|
||||||
|
unsigned char pw_str[100];
|
||||||
|
unsigned char salt_str[100];
|
||||||
|
unsigned char dst_str[100];
|
||||||
|
|
||||||
|
md_context_t ctx;
|
||||||
|
const md_info_t *info;
|
||||||
|
|
||||||
|
int pw_len, salt_len;
|
||||||
|
unsigned char key[100];
|
||||||
|
|
||||||
|
memset(pw_str, 0x00, 100);
|
||||||
|
memset(salt_str, 0x00, 100);
|
||||||
|
memset(dst_str, 0x00, 100);
|
||||||
|
|
||||||
|
pw_len = unhexify( pw_str, {hex_password_string} );
|
||||||
|
salt_len = unhexify( salt_str, {hex_salt_string} );
|
||||||
|
|
||||||
|
|
||||||
|
info = md_info_from_type( {hash} );
|
||||||
|
TEST_ASSERT( info != NULL );
|
||||||
|
TEST_ASSERT( md_init_ctx( &ctx, info ) == 0 );
|
||||||
|
TEST_ASSERT( pkcs5_pbkdf2_hmac( &ctx, pw_str, pw_len, salt_str, salt_len,
|
||||||
|
{it_cnt}, {key_len}, key ) == 0 );
|
||||||
|
|
||||||
|
hexify( dst_str, key, {key_len} );
|
||||||
|
TEST_ASSERT( strcmp( (char *) dst_str, {result_key_string} ) == 0 );
|
||||||
|
}
|
||||||
|
END_CASE
|
Loading…
Reference in New Issue
Block a user