package cologne.eck.peafactory.crypto.kdf;
import cologne.eck.all_peas.data.PeaProperties;
import cologne.eck.peafactory.crypto.CipherStuff;
import cologne.eck.peafactory.crypto.HashStuff;
import cologne.eck.tools.Comparator;
/*
* Peafactory - Production of Password Encryption Archives
* Copyright (C) 2015 Axel von dem Bruch
*
* This library 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 library 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.
* See: http://www.gnu.org/licenses/gpl-2.0.html
* You should have received a copy of the GNU General Public License
* along with this library.
*/
/**
* parent class of all used key derivation functions
*/
public abstract class KeyDerivation {
private static KeyDerivation kdf = null;
private static int tCost = 0;
private static int mCost = 0;
private static int arg3 = 0;
private static int arg4 = 0;
private static int arg5 = 0;
private static int arg6 = 0;
private final static int SALT_SIZE = 32;// at most 129 byte for fixed initialized salt
private static byte[] salt;
private static byte[] extraValues = "peafactory_extra".getBytes(); // 16 byte
private static String versionString = "";
/**
* Derive the key. All parameters are set in child classes
*
* @param pswMaterial material build from the password
*
* @return the derived key
*/
public abstract byte[] deriveKey( byte[] pswMaterial);
/**
* Get the name of the key derivation scheme
*
* @return the name of the key derivation scheme
*/
public abstract String getName();
/**
* Get the required memory for the key derivation function.
* Returns the required memory in MiB.
*
* @return memory requirement in MiB
*/
public abstract int getMemoryRequirement();
/**
* Get a key of required length (expand or extract) using HKDF;
* this is only used for Bcrypt and Scrypt
*
* @param keyMaterial the material from key derivation
* @return the key
*/
public final static byte[] adjustKeyMaterial(byte[] keyMaterial){
if (PeaProperties.getWorkingMode().equals("-t")){
Comparator.checkNullVector(keyMaterial);
}
//Help.printBytes("key", HashStuff.generateHKDFBytes(keyMaterial, null, CipherStuff.getKeySize()));
return HashStuff.generateHKDFBytes(keyMaterial, salt, CipherStuff.getKeySize());
}
/**
* Print informations about the used key derivation function
*
* @param print if true, informations are printed
*/
public final static void printInfos(boolean print) {
if (print == true) {
System.out.print("Key derivation: " + kdf.getName() );
if (! versionString.equals("")){
System.out.print(", " + versionString);
}
System.out.print(", time parameter: " + tCost
+ ", memory parameter: " + mCost
);
if (arg3 != 0) {
System.out.print(", parameter 3: " + arg3);
}
if (arg4 != 0) {
System.out.print(", parameter 4: " + arg4);
}
if (arg5 != 0) {
System.out.print(", parameter 5: " + arg5);
}
if (arg6 != 0) {
System.out.print(", parameter 6: " + arg6);
}
System.out.println("");
}
}
//=============================================
// Getter & Setter
/**
* @return the tCost
*/
public static int gettCost() {
return tCost;
}
/**
* @param tCost the tCost to set
*/
public static void settCost(int _tCost) {
KeyDerivation.tCost = _tCost;
}
/**
* @return the mCost
*/
public static int getmCost() {
return mCost;
}
/**
* @param mCost the mCost to set
*/
public static void setmCost(int _mCost) {
KeyDerivation.mCost = _mCost;
}
/**
* @return the arg3
*/
public static int getArg3() {
return arg3;
}
/**
* @param arg3 the arg3 to set
*/
public static void setArg3(int _arg3) {
KeyDerivation.arg3 = _arg3;
}
/**
* @return the arg4
*/
public static int getArg4() {
return arg4;
}
/**
* @param arg4 the arg4 to set
*/
public static void setArg4(int _arg4) {
KeyDerivation.arg4 = _arg4;
}
/**
* @return the arg5
*/
public static int getArg5() {
return arg5;
}
/**
* @param arg5 the arg5 to set
*/
public static void setArg5(int _arg5) {
KeyDerivation.arg5 = _arg5;
}
/**
* @return the arg6
*/
public static int getArg6() {
return arg6;
}
/**
* @param arg6 the arg6 to set
*/
public static void setArg6(int _arg6) {
KeyDerivation.arg6 = _arg6;
}
/**
* @return the salt
*/
public static byte[] getSalt() {
return salt;
}
/**
* @param salt the salt to set
*/
public static void setSalt(byte[] _salt) {
salt = new byte[SALT_SIZE];
System.arraycopy(_salt, 0, KeyDerivation.salt, 0, SALT_SIZE);
//Help.printBytes("KeyDerivation set Salt ", salt);
}
/**
* @return the extraValues
*/
public static byte[] getExtraValues() {
return extraValues;
}
/**
* @param extraValues the extraValues to set
*/
public static void setExtraValues(byte[] _extraValues) {
KeyDerivation.extraValues = _extraValues;
}
/**
* @return the kdf
*/
public static KeyDerivation getKdf() {
return kdf;
}
/**
* @param kdf the kdf to set
*/
public static void setKdf(KeyDerivation kdf) {
KeyDerivation.kdf = kdf;
}
/**
* @return the versionString
*/
public static String getVersionString() {
return versionString;
}
/**
* @param versionString the versionString to set
*/
public static void setVersionString(String versionString) {
KeyDerivation.versionString = versionString;
}
public static int getSaltSize(){
return SALT_SIZE;
}
}