os/security/crypto/weakcrypto/source/pkcs12kdf/Pkcs12Pbe.java
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2006-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
* This program uses the Bouncy Castle APIs PKCS#12 KDF to generate encryption keys + ivs 
sl@0
    16
* and mac keys for use with compatibility testing.
sl@0
    17
*
sl@0
    18
*/
sl@0
    19
sl@0
    20
sl@0
    21
package com.symbian.security;
sl@0
    22
sl@0
    23
import java.math.BigInteger;
sl@0
    24
import java.security.SecureRandom;
sl@0
    25
import org.bouncycastle.crypto.PBEParametersGenerator;
sl@0
    26
import org.bouncycastle.crypto.digests.SHA1Digest;
sl@0
    27
import org.bouncycastle.crypto.generators.PKCS12ParametersGenerator;
sl@0
    28
import org.bouncycastle.crypto.CipherParameters;
sl@0
    29
import org.bouncycastle.crypto.params.KeyParameter;
sl@0
    30
import org.bouncycastle.crypto.params.ParametersWithIV;
sl@0
    31
sl@0
    32
public class Pkcs12Pbe {
sl@0
    33
	private PKCS12ParametersGenerator pgen;
sl@0
    34
sl@0
    35
	public Pkcs12Pbe() {
sl@0
    36
		pgen = new PKCS12ParametersGenerator(new SHA1Digest());
sl@0
    37
	}
sl@0
    38
sl@0
    39
	public static void main(String args[]) {
sl@0
    40
		try {
sl@0
    41
			if (args.length < 5) {
sl@0
    42
				usage();
sl@0
    43
				System.exit(-1);
sl@0
    44
				
sl@0
    45
			}
sl@0
    46
			int keyLength = Integer.parseInt(args[0]);
sl@0
    47
			int blockSize = Integer.parseInt(args[1]);
sl@0
    48
			int iterations = Integer.parseInt(args[2]);
sl@0
    49
			String salt = args[3];
sl@0
    50
			String password = args[4];		
sl@0
    51
			byte[] saltBytes = hexToByteArray(salt);
sl@0
    52
	
sl@0
    53
			Pkcs12Pbe pbe = new Pkcs12Pbe();
sl@0
    54
			pbe.getKey(keyLength, blockSize, iterations, password, saltBytes);			
sl@0
    55
		}
sl@0
    56
		catch (Exception e) {
sl@0
    57
			System.exit(-1);
sl@0
    58
		}
sl@0
    59
	}
sl@0
    60
	
sl@0
    61
	private static byte[] hexToByteArray(String hex) throws Exception {
sl@0
    62
		if (hex.length() % 2 != 0) {
sl@0
    63
			throw new Exception("hexToByteArray: odd number of nibbles");
sl@0
    64
		}
sl@0
    65
		StringBuffer hexBuffer = new StringBuffer(hex);
sl@0
    66
		
sl@0
    67
		byte[] byteBuffer = new byte[hexBuffer.length() / 2];
sl@0
    68
		for (int i = 0; i < hexBuffer.length(); i+=2) {
sl@0
    69
			try {
sl@0
    70
				byteBuffer[i / 2] = (byte) Integer.parseInt(hexBuffer.substring(i, i+2), 16);
sl@0
    71
			}
sl@0
    72
			catch (NumberFormatException e) {
sl@0
    73
				System.err.println("hexToByteArray: invalid hex string: " + hex);
sl@0
    74
				throw e;
sl@0
    75
			}
sl@0
    76
		}
sl@0
    77
		return byteBuffer;
sl@0
    78
	}
sl@0
    79
sl@0
    80
	private static void usage() {
sl@0
    81
		System.err
sl@0
    82
				.println("Usage: pkcs12pbe <key length> <block_size> <iterations> <salt> <password>\n");
sl@0
    83
	}
sl@0
    84
	
sl@0
    85
	private void getKey(int keyLen, int ivLen, int iterCount, String password,
sl@0
    86
			byte[] salt) {
sl@0
    87
		System.out.print("key len = " + keyLen + ", iter count = "
sl@0
    88
				+ iterCount + ", password = \"" + password + "\", salt = ");		
sl@0
    89
		printUnformattedByteArray(salt);
sl@0
    90
sl@0
    91
		char[] pwChars = password.toCharArray();
sl@0
    92
		byte[] pwBytes = PBEParametersGenerator.PKCS12PasswordToBytes(pwChars);
sl@0
    93
sl@0
    94
		pgen.init(pwBytes, salt, iterCount);
sl@0
    95
		CipherParameters cp = pgen.generateDerivedParameters(keyLen, ivLen);
sl@0
    96
sl@0
    97
		ParametersWithIV ivp = (ParametersWithIV) cp;
sl@0
    98
		KeyParameter kp = (KeyParameter) ivp.getParameters();
sl@0
    99
sl@0
   100
		System.out.print("key ");
sl@0
   101
		printUnformattedByteArray((kp.getKey()));
sl@0
   102
		System.out.print("iv ");
sl@0
   103
		printUnformattedByteArray(ivp.getIV());
sl@0
   104
sl@0
   105
		kp = (KeyParameter) pgen.generateDerivedMacParameters(160);
sl@0
   106
		System.out.print("160bit hmac key ");
sl@0
   107
		printUnformattedByteArray((kp.getKey()));
sl@0
   108
sl@0
   109
	}
sl@0
   110
sl@0
   111
	// unformatted hex strings that can be passed as arguments to openssl
sl@0
   112
	private void printUnformattedByteArray(byte[] a) {
sl@0
   113
		StringBuffer line = new StringBuffer();
sl@0
   114
		
sl@0
   115
		for (int i = 0; i < a.length; i++) {
sl@0
   116
			line.append(hexStr(a[i], 2));
sl@0
   117
		}
sl@0
   118
		System.out.println(line);
sl@0
   119
	}	
sl@0
   120
sl@0
   121
	private String hexStr(int val, int width) {
sl@0
   122
		StringBuffer result = new StringBuffer();
sl@0
   123
		while (--width >= 0) {
sl@0
   124
			int bitPos = 4 * width;
sl@0
   125
			int nybble = (val & (0xf << bitPos)) >> bitPos;
sl@0
   126
			result.append(Integer.toHexString(nybble));
sl@0
   127
		}
sl@0
   128
		return result.toString();
sl@0
   129
	}
sl@0
   130
}