use of com.github.zhenwei.pkix.operator.MacCalculator in project LinLong-Java by zhenwei1108.
the class PKMACBuilder method genCalculator.
private MacCalculator genCalculator(final PBMParameter params, char[] password) throws CRMFException {
// From RFC 4211
//
// 1. Generate a random salt value S
//
// 2. Append the salt to the pw. K = pw || salt.
//
// 3. Hash the value of K. K = HASH(K)
//
// 4. Iter = Iter - 1. If Iter is greater than zero. Goto step 3.
//
// 5. Compute an HMAC as documented in [HMAC].
//
// MAC = HASH( K XOR opad, HASH( K XOR ipad, data) )
//
// Where opad and ipad are defined in [HMAC].
byte[] pw = Strings.toUTF8ByteArray(password);
byte[] salt = params.getSalt().getOctets();
byte[] K = new byte[pw.length + salt.length];
System.arraycopy(pw, 0, K, 0, pw.length);
System.arraycopy(salt, 0, K, pw.length, salt.length);
calculator.setup(params.getOwf(), params.getMac());
int iter = params.getIterationCount().intValueExact();
do {
K = calculator.calculateDigest(K);
} while (--iter > 0);
final byte[] key = K;
return new MacCalculator() {
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
public AlgorithmIdentifier getAlgorithmIdentifier() {
return new AlgorithmIdentifier(CMPObjectIdentifiers.passwordBasedMac, params);
}
public GenericKey getKey() {
return new GenericKey(getAlgorithmIdentifier(), key);
}
public OutputStream getOutputStream() {
return bOut;
}
public byte[] getMac() {
try {
return calculator.calculateMac(key, bOut.toByteArray());
} catch (CRMFException e) {
throw new RuntimeOperatorException("exception calculating mac: " + e.getMessage(), e);
}
}
};
}
use of com.github.zhenwei.pkix.operator.MacCalculator in project LinLong-Java by zhenwei1108.
the class PKMACValueVerifier method isValid.
public boolean isValid(PKMACValue value, char[] password, SubjectPublicKeyInfo keyInfo) throws CRMFException {
builder.setParameters(PBMParameter.getInstance(value.getAlgId().getParameters()));
MacCalculator calculator = builder.build(password);
OutputStream macOut = calculator.getOutputStream();
try {
macOut.write(keyInfo.getEncoded(ASN1Encoding.DER));
macOut.close();
} catch (IOException e) {
throw new CRMFException("exception encoding mac input: " + e.getMessage(), e);
}
return Arrays.constantTimeAreEqual(calculator.getMac(), value.getValue().getBytes());
}
use of com.github.zhenwei.pkix.operator.MacCalculator in project LinLong-Java by zhenwei1108.
the class PKMACValueGenerator method generate.
public PKMACValue generate(char[] password, SubjectPublicKeyInfo keyInfo) throws CRMFException {
MacCalculator calculator = builder.build(password);
OutputStream macOut = calculator.getOutputStream();
try {
macOut.write(keyInfo.getEncoded(ASN1Encoding.DER));
macOut.close();
} catch (IOException e) {
throw new CRMFException("exception encoding mac input: " + e.getMessage(), e);
}
return new PKMACValue(calculator.getAlgorithmIdentifier(), new DERBitString(calculator.getMac()));
}
use of com.github.zhenwei.pkix.operator.MacCalculator in project LinLong-Java by zhenwei1108.
the class ProtectedPKIMessage method verify.
/**
* Verify a message with password based MAC protection.
*
* @param pkMacBuilder MAC builder that can be used to construct the appropriate MacCalculator
* @param password the MAC password
* @return true if the passed in password and MAC builder verify the message, false otherwise.
* @throws CMPException if algorithm not MAC based, or an exception is thrown verifying the MAC.
*/
public boolean verify(PKMACBuilder pkMacBuilder, char[] password) throws CMPException {
if (!CMPObjectIdentifiers.passwordBasedMac.equals(pkiMessage.getHeader().getProtectionAlg().getAlgorithm())) {
throw new CMPException("protection algorithm not mac based");
}
try {
pkMacBuilder.setParameters(PBMParameter.getInstance(pkiMessage.getHeader().getProtectionAlg().getParameters()));
MacCalculator calculator = pkMacBuilder.build(password);
OutputStream macOut = calculator.getOutputStream();
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(pkiMessage.getHeader());
v.add(pkiMessage.getBody());
macOut.write(new DERSequence(v).getEncoded(ASN1Encoding.DER));
macOut.close();
return Arrays.areEqual(calculator.getMac(), pkiMessage.getProtection().getBytes());
} catch (Exception e) {
throw new CMPException("unable to verify MAC: " + e.getMessage(), e);
}
}
use of com.github.zhenwei.pkix.operator.MacCalculator in project LinLong-Java by zhenwei1108.
the class JcePKCS12MacCalculatorBuilder method build.
public MacCalculator build(final char[] password) throws OperatorCreationException {
if (random == null) {
random = new SecureRandom();
}
try {
final Mac mac = helper.createMac(algorithm.getId());
saltLength = mac.getMacLength();
final byte[] salt = new byte[saltLength];
random.nextBytes(salt);
PBEParameterSpec defParams = new PBEParameterSpec(salt, iterationCount);
final SecretKey key = new PKCS12Key(password);
mac.init(key, defParams);
return new MacCalculator() {
public AlgorithmIdentifier getAlgorithmIdentifier() {
return new AlgorithmIdentifier(algorithm, new PKCS12PBEParams(salt, iterationCount));
}
public OutputStream getOutputStream() {
return new MacOutputStream(mac);
}
public byte[] getMac() {
return mac.doFinal();
}
public GenericKey getKey() {
return new GenericKey(getAlgorithmIdentifier(), key.getEncoded());
}
};
} catch (Exception e) {
throw new OperatorCreationException("unable to create MAC calculator: " + e.getMessage(), e);
}
}
Aggregations