1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcrypto/source/pkcs12kdf/Pkcs12Pbe.java Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,130 @@
1.4 +/*
1.5 +* Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* This program uses the Bouncy Castle APIs PKCS#12 KDF to generate encryption keys + ivs
1.19 +* and mac keys for use with compatibility testing.
1.20 +*
1.21 +*/
1.22 +
1.23 +
1.24 +package com.symbian.security;
1.25 +
1.26 +import java.math.BigInteger;
1.27 +import java.security.SecureRandom;
1.28 +import org.bouncycastle.crypto.PBEParametersGenerator;
1.29 +import org.bouncycastle.crypto.digests.SHA1Digest;
1.30 +import org.bouncycastle.crypto.generators.PKCS12ParametersGenerator;
1.31 +import org.bouncycastle.crypto.CipherParameters;
1.32 +import org.bouncycastle.crypto.params.KeyParameter;
1.33 +import org.bouncycastle.crypto.params.ParametersWithIV;
1.34 +
1.35 +public class Pkcs12Pbe {
1.36 + private PKCS12ParametersGenerator pgen;
1.37 +
1.38 + public Pkcs12Pbe() {
1.39 + pgen = new PKCS12ParametersGenerator(new SHA1Digest());
1.40 + }
1.41 +
1.42 + public static void main(String args[]) {
1.43 + try {
1.44 + if (args.length < 5) {
1.45 + usage();
1.46 + System.exit(-1);
1.47 +
1.48 + }
1.49 + int keyLength = Integer.parseInt(args[0]);
1.50 + int blockSize = Integer.parseInt(args[1]);
1.51 + int iterations = Integer.parseInt(args[2]);
1.52 + String salt = args[3];
1.53 + String password = args[4];
1.54 + byte[] saltBytes = hexToByteArray(salt);
1.55 +
1.56 + Pkcs12Pbe pbe = new Pkcs12Pbe();
1.57 + pbe.getKey(keyLength, blockSize, iterations, password, saltBytes);
1.58 + }
1.59 + catch (Exception e) {
1.60 + System.exit(-1);
1.61 + }
1.62 + }
1.63 +
1.64 + private static byte[] hexToByteArray(String hex) throws Exception {
1.65 + if (hex.length() % 2 != 0) {
1.66 + throw new Exception("hexToByteArray: odd number of nibbles");
1.67 + }
1.68 + StringBuffer hexBuffer = new StringBuffer(hex);
1.69 +
1.70 + byte[] byteBuffer = new byte[hexBuffer.length() / 2];
1.71 + for (int i = 0; i < hexBuffer.length(); i+=2) {
1.72 + try {
1.73 + byteBuffer[i / 2] = (byte) Integer.parseInt(hexBuffer.substring(i, i+2), 16);
1.74 + }
1.75 + catch (NumberFormatException e) {
1.76 + System.err.println("hexToByteArray: invalid hex string: " + hex);
1.77 + throw e;
1.78 + }
1.79 + }
1.80 + return byteBuffer;
1.81 + }
1.82 +
1.83 + private static void usage() {
1.84 + System.err
1.85 + .println("Usage: pkcs12pbe <key length> <block_size> <iterations> <salt> <password>\n");
1.86 + }
1.87 +
1.88 + private void getKey(int keyLen, int ivLen, int iterCount, String password,
1.89 + byte[] salt) {
1.90 + System.out.print("key len = " + keyLen + ", iter count = "
1.91 + + iterCount + ", password = \"" + password + "\", salt = ");
1.92 + printUnformattedByteArray(salt);
1.93 +
1.94 + char[] pwChars = password.toCharArray();
1.95 + byte[] pwBytes = PBEParametersGenerator.PKCS12PasswordToBytes(pwChars);
1.96 +
1.97 + pgen.init(pwBytes, salt, iterCount);
1.98 + CipherParameters cp = pgen.generateDerivedParameters(keyLen, ivLen);
1.99 +
1.100 + ParametersWithIV ivp = (ParametersWithIV) cp;
1.101 + KeyParameter kp = (KeyParameter) ivp.getParameters();
1.102 +
1.103 + System.out.print("key ");
1.104 + printUnformattedByteArray((kp.getKey()));
1.105 + System.out.print("iv ");
1.106 + printUnformattedByteArray(ivp.getIV());
1.107 +
1.108 + kp = (KeyParameter) pgen.generateDerivedMacParameters(160);
1.109 + System.out.print("160bit hmac key ");
1.110 + printUnformattedByteArray((kp.getKey()));
1.111 +
1.112 + }
1.113 +
1.114 + // unformatted hex strings that can be passed as arguments to openssl
1.115 + private void printUnformattedByteArray(byte[] a) {
1.116 + StringBuffer line = new StringBuffer();
1.117 +
1.118 + for (int i = 0; i < a.length; i++) {
1.119 + line.append(hexStr(a[i], 2));
1.120 + }
1.121 + System.out.println(line);
1.122 + }
1.123 +
1.124 + private String hexStr(int val, int width) {
1.125 + StringBuffer result = new StringBuffer();
1.126 + while (--width >= 0) {
1.127 + int bitPos = 4 * width;
1.128 + int nybble = (val & (0xf << bitPos)) >> bitPos;
1.129 + result.append(Integer.toHexString(nybble));
1.130 + }
1.131 + return result.toString();
1.132 + }
1.133 +}