use of com.github.zhenwei.core.crypto.Digest in project LinLong-Java by zhenwei1108.
the class BcECContentVerifierProviderBuilder method createSigner.
protected Signer createSigner(AlgorithmIdentifier sigAlgId) throws OperatorCreationException {
AlgorithmIdentifier digAlg = digestAlgorithmFinder.find(sigAlgId);
Digest dig = digestProvider.get(digAlg);
return new DSADigestSigner(new ECDSASigner(), dig);
}
use of com.github.zhenwei.core.crypto.Digest in project LinLong-Java by zhenwei1108.
the class PSSSignatureSpi method engineSetParameter.
protected void engineSetParameter(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException {
if (params == null) {
if (originalSpec != null) {
params = originalSpec;
} else {
// Java 11 bug
return;
}
}
if (!isInitState) {
throw new ProviderException("cannot call setParameter in the middle of update");
}
if (params instanceof PSSParameterSpec) {
PSSParameterSpec newParamSpec = (PSSParameterSpec) params;
if (originalSpec != null) {
if (!DigestFactory.isSameDigest(originalSpec.getDigestAlgorithm(), newParamSpec.getDigestAlgorithm())) {
throw new InvalidAlgorithmParameterException("parameter must be using " + originalSpec.getDigestAlgorithm());
}
}
Digest mgfDigest;
if (newParamSpec.getMGFAlgorithm().equalsIgnoreCase("MGF1") || newParamSpec.getMGFAlgorithm().equals(PKCSObjectIdentifiers.id_mgf1.getId())) {
if (!(newParamSpec.getMGFParameters() instanceof MGF1ParameterSpec)) {
throw new InvalidAlgorithmParameterException("unknown MGF parameters");
}
MGF1ParameterSpec mgfParams = (MGF1ParameterSpec) newParamSpec.getMGFParameters();
if (!DigestFactory.isSameDigest(mgfParams.getDigestAlgorithm(), newParamSpec.getDigestAlgorithm())) {
throw new InvalidAlgorithmParameterException("digest algorithm for MGF should be the same as for PSS parameters.");
}
mgfDigest = DigestFactory.getDigest(mgfParams.getDigestAlgorithm());
} else if (newParamSpec.getMGFAlgorithm().equals("SHAKE128") || newParamSpec.getMGFAlgorithm().equals("SHAKE256")) {
mgfDigest = DigestFactory.getDigest(newParamSpec.getMGFAlgorithm());
} else {
throw new InvalidAlgorithmParameterException("unknown mask generation function specified");
}
if (mgfDigest == null) {
throw new InvalidAlgorithmParameterException("no match on MGF algorithm: " + newParamSpec.getMGFAlgorithm());
}
this.engineParams = null;
this.paramSpec = newParamSpec;
this.mgfDigest = mgfDigest;
this.saltLength = paramSpec.getSaltLength();
this.trailer = getTrailer(paramSpec.getTrailerField());
setupContentDigest();
if (key != null) {
pss = new com.github.zhenwei.core.crypto.signers.PSSSigner(signer, contentDigest, mgfDigest, saltLength, trailer);
if (key.isPrivate()) {
pss.init(true, key);
} else {
pss.init(false, key);
}
}
} else {
throw new InvalidAlgorithmParameterException("Only PSSParameterSpec supported");
}
}
use of com.github.zhenwei.core.crypto.Digest in project LinLong-Java by zhenwei1108.
the class Mac method hmac.
public byte[] hmac(DigestAlgEnum digestAlgEnum, Key key, byte[] source) {
Digest digest;
switch(digestAlgEnum) {
case SHA1:
digest = new SHA1Digest();
break;
case SHA224:
digest = new SHA224Digest();
break;
case SHA256:
digest = new SHA256Digest();
break;
case SHA384:
digest = new SHA384Digest();
break;
case SHA512:
digest = new SHA512Digest();
break;
// default sm3 digest
default:
digest = new SM3Digest();
break;
}
HMac hMac = new HMac(digest);
hMac.init(new KeyParameter(key.getEncoded()));
hMac.update(source, 0, source.length);
byte[] result = new byte[hMac.getMacSize()];
hMac.doFinal(result, 0);
return result;
}
use of com.github.zhenwei.core.crypto.Digest in project LinLong-Java by zhenwei1108.
the class LM_OTS method lm_ots_validate_signature_calculate.
public static byte[] lm_ots_validate_signature_calculate(LMSContext context) {
LMOtsPublicKey publicKey = context.getPublicKey();
LMOtsParameters parameter = publicKey.getParameter();
Object sig = context.getSignature();
LMOtsSignature signature;
if (sig instanceof LMSSignature) {
signature = ((LMSSignature) sig).getOtsSignature();
} else {
signature = (LMOtsSignature) sig;
}
int n = parameter.getN();
int w = parameter.getW();
int p = parameter.getP();
byte[] Q = context.getQ();
int cs = cksm(Q, n, parameter);
Q[n] = (byte) ((cs >>> 8) & 0xFF);
Q[n + 1] = (byte) cs;
byte[] I = publicKey.getI();
int q = publicKey.getQ();
Digest finalContext = DigestUtil.getDigest(parameter.getDigestOID());
LmsUtils.byteArray(I, finalContext);
LmsUtils.u32str(q, finalContext);
LmsUtils.u16str(D_PBLC, finalContext);
byte[] tmp = Composer.compose().bytes(I).u32str(q).padUntil(0, ITER_PREV + n).build();
int max_digit = (1 << w) - 1;
byte[] y = signature.getY();
Digest ctx = DigestUtil.getDigest(parameter.getDigestOID());
for (int i = 0; i < p; i++) {
Pack.shortToBigEndian((short) i, tmp, ITER_K);
System.arraycopy(y, i * n, tmp, ITER_PREV, n);
int a = coef(Q, i, w);
for (int j = a; j < max_digit; j++) {
tmp[ITER_J] = (byte) j;
ctx.update(tmp, 0, ITER_PREV + n);
ctx.doFinal(tmp, ITER_PREV);
}
finalContext.update(tmp, ITER_PREV, n);
}
byte[] K = new byte[n];
finalContext.doFinal(K, 0);
return K;
}
use of com.github.zhenwei.core.crypto.Digest in project LinLong-Java by zhenwei1108.
the class BcDigestCalculatorProvider method get.
public DigestCalculator get(final AlgorithmIdentifier algorithm) throws OperatorCreationException {
Digest dig = digestProvider.get(algorithm);
final DigestOutputStream stream = new DigestOutputStream(dig);
return new DigestCalculator() {
public AlgorithmIdentifier getAlgorithmIdentifier() {
return algorithm;
}
public OutputStream getOutputStream() {
return stream;
}
public byte[] getDigest() {
return stream.getDigest();
}
};
}
Aggregations