os/security/crypto/weakcryptospi/inc/pkcs5kdf.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
* ** IMPORTANT ** PublishedPartner API's in this file are published to 3rd party developers via the 
sl@0
    16
* Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted.
sl@0
    17
*
sl@0
    18
*/
sl@0
    19
sl@0
    20
sl@0
    21
/**
sl@0
    22
 @file
sl@0
    23
 @publishedPartner
sl@0
    24
 @released
sl@0
    25
*/
sl@0
    26
sl@0
    27
#ifndef __PKCS5KDF_H__
sl@0
    28
#define __PKCS5KDF_H__
sl@0
    29
sl@0
    30
#include <hash.h>
sl@0
    31
sl@0
    32
/** The number of times the hashing algorithm is run. */
sl@0
    33
const TUint KDefaultIterations = 1000;
sl@0
    34
sl@0
    35
/**
sl@0
    36
 * A PKCS#5 compliant Key Derivation Function (KDF).
sl@0
    37
 *
sl@0
    38
 * This class allows the derivation of deterministic arbitrary length byte 
sl@0
    39
 * streams from an input string. The output byte stream is generated using 
sl@0
    40
 * multiple iterations of a CSHA1 message digest and is suitable for use 
sl@0
    41
 * as a cryptographic symmetric key.
sl@0
    42
 *
sl@0
    43
 * @since v7.0s
sl@0
    44
 */
sl@0
    45
class TPKCS5KDF
sl@0
    46
	{
sl@0
    47
public:
sl@0
    48
	/** 
sl@0
    49
	 * Derives deterministic arbitrary length byte streams (aKey) from an input
sl@0
    50
	 * string (aPasswd) and a randomly chosen salt (aSalt) for use as a
sl@0
    51
	 * symmetric key.
sl@0
    52
	 *
sl@0
    53
	 * Attention -- Improperly chosen values for these parameters will seriously
sl@0
    54
	 * impact the security of the derived key and as a result the security of 
sl@0
    55
	 * your application. 
sl@0
    56
	 *
sl@0
    57
	 * See the Cryptography api-guide documentation for more information and 
sl@0
    58
	 * recommended usage patterns.
sl@0
    59
	 * 
sl@0
    60
	 * @param aKey			Output Value. The key resulting from the operation.
sl@0
    61
	 * 						The length of the key will be equal to the length of
sl@0
    62
	 * 						the input descriptor. All data, from the first byte 
sl@0
    63
	 * 						to the set length, will be overwritten with the resulting
sl@0
    64
	 *						byte stream.
sl@0
    65
	 * @param aPasswd		Input Value. The password you wish to derive a key from.
sl@0
    66
	 * @param aSalt			Input Value. A <B><I>randomly</I></B> selected second
sl@0
    67
	 * 						input to the key derivation function to discourage certain
sl@0
    68
	 * 						attacks. PKCS5 recommends a minimum of 8 randomly chosen bytes.
sl@0
    69
	 * @param aIterations	Input Value. The number of times the internal hashing
sl@0
    70
	 * 						function should be run over the password and salt.
sl@0
    71
	 *						Minimum recommendation is KDefaultIterations.
sl@0
    72
	 */
sl@0
    73
	IMPORT_C static void DeriveKeyL(TDes8& aKey, const TDesC8& aPasswd, 
sl@0
    74
		const TDesC8& aSalt, TUint aIterations = KDefaultIterations);
sl@0
    75
private:
sl@0
    76
	/** 
sl@0
    77
	 * Internal iterative function that performs the actual hashing. 
sl@0
    78
	 */
sl@0
    79
	static void F(CMessageDigest& aDigest, TUint32* aAccumulator, TUint32* S,
sl@0
    80
	TUint32* Ui, TUint aHashBytes, const TUint32* aSalt, TUint aSaltBytes, 
sl@0
    81
	TUint c, TUint i);
sl@0
    82
	
sl@0
    83
	/** 
sl@0
    84
	 * XOR's the values of two equal length descriptors.  Internally, it
sl@0
    85
	 * operates on a word by word basis.  Data stored beyond the end of the
sl@0
    86
	 * descriptor, but before the end of the final word, will be xored as well.
sl@0
    87
	 */
sl@0
    88
	static inline void XORString(const TUint32* aOp1, TUint32* aOp2,
sl@0
    89
		TUint aLength);
sl@0
    90
	};
sl@0
    91
sl@0
    92
#endif