org.bouncycastle.pqc.crypto.util.Utils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcprov-jdk15on Show documentation
Show all versions of bcprov-jdk15on Show documentation
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms. This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5 and up.
The newest version!
package org.bouncycastle.pqc.crypto.util;
import java.util.HashMap;
import java.util.Map;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.DERNull;
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.crypto.digests.SHAKEDigest;
import org.bouncycastle.pqc.asn1.PQCObjectIdentifiers;
import org.bouncycastle.pqc.asn1.SPHINCS256KeyParams;
import org.bouncycastle.pqc.crypto.qtesla.QTESLASecurityCategory;
import org.bouncycastle.pqc.crypto.sphincs.SPHINCSKeyParameters;
import org.bouncycastle.pqc.crypto.xmss.XMSSKeyParameters;
import org.bouncycastle.util.Integers;
class Utils
{
static final AlgorithmIdentifier AlgID_qTESLA_p_I = new AlgorithmIdentifier(PQCObjectIdentifiers.qTESLA_p_I);
static final AlgorithmIdentifier AlgID_qTESLA_p_III = new AlgorithmIdentifier(PQCObjectIdentifiers.qTESLA_p_III);
static final AlgorithmIdentifier SPHINCS_SHA3_256 = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha3_256);
static final AlgorithmIdentifier SPHINCS_SHA512_256 = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha512_256);
static final AlgorithmIdentifier XMSS_SHA256 = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256);
static final AlgorithmIdentifier XMSS_SHA512 = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha512);
static final AlgorithmIdentifier XMSS_SHAKE128 = new AlgorithmIdentifier(NISTObjectIdentifiers.id_shake128);
static final AlgorithmIdentifier XMSS_SHAKE256 = new AlgorithmIdentifier(NISTObjectIdentifiers.id_shake256);
static final Map categories = new HashMap();
static
{
categories.put(PQCObjectIdentifiers.qTESLA_p_I, Integers.valueOf(QTESLASecurityCategory.PROVABLY_SECURE_I));
categories.put(PQCObjectIdentifiers.qTESLA_p_III, Integers.valueOf(QTESLASecurityCategory.PROVABLY_SECURE_III));
}
static int qTeslaLookupSecurityCategory(AlgorithmIdentifier algorithm)
{
return ((Integer)categories.get(algorithm.getAlgorithm())).intValue();
}
static AlgorithmIdentifier qTeslaLookupAlgID(int securityCategory)
{
switch (securityCategory)
{
case QTESLASecurityCategory.PROVABLY_SECURE_I:
return AlgID_qTESLA_p_I;
case QTESLASecurityCategory.PROVABLY_SECURE_III:
return AlgID_qTESLA_p_III;
default:
throw new IllegalArgumentException("unknown security category: " + securityCategory);
}
}
static AlgorithmIdentifier sphincs256LookupTreeAlgID(String treeDigest)
{
if (treeDigest.equals(SPHINCSKeyParameters.SHA3_256))
{
return SPHINCS_SHA3_256;
}
else if (treeDigest.equals(SPHINCSKeyParameters.SHA512_256))
{
return SPHINCS_SHA512_256;
}
else
{
throw new IllegalArgumentException("unknown tree digest: " + treeDigest);
}
}
static AlgorithmIdentifier xmssLookupTreeAlgID(String treeDigest)
{
if (treeDigest.equals(XMSSKeyParameters.SHA_256))
{
return XMSS_SHA256;
}
else if (treeDigest.equals(XMSSKeyParameters.SHA_512))
{
return XMSS_SHA512;
}
else if (treeDigest.equals(XMSSKeyParameters.SHAKE128))
{
return XMSS_SHAKE128;
}
else if (treeDigest.equals(XMSSKeyParameters.SHAKE256))
{
return XMSS_SHAKE256;
}
else
{
throw new IllegalArgumentException("unknown tree digest: " + treeDigest);
}
}
static String sphincs256LookupTreeAlgName(SPHINCS256KeyParams keyParams)
{
AlgorithmIdentifier treeDigest = keyParams.getTreeDigest();
if (treeDigest.getAlgorithm().equals(SPHINCS_SHA3_256.getAlgorithm()))
{
return SPHINCSKeyParameters.SHA3_256;
}
else if (treeDigest.getAlgorithm().equals(SPHINCS_SHA512_256.getAlgorithm()))
{
return SPHINCSKeyParameters.SHA512_256;
}
else
{
throw new IllegalArgumentException("unknown tree digest: " + treeDigest.getAlgorithm());
}
}
static Digest getDigest(ASN1ObjectIdentifier oid)
{
if (oid.equals(NISTObjectIdentifiers.id_sha256))
{
return new SHA256Digest();
}
if (oid.equals(NISTObjectIdentifiers.id_sha512))
{
return new SHA512Digest();
}
if (oid.equals(NISTObjectIdentifiers.id_shake128))
{
return new SHAKEDigest(128);
}
if (oid.equals(NISTObjectIdentifiers.id_shake256))
{
return new SHAKEDigest(256);
}
throw new IllegalArgumentException("unrecognized digest OID: " + oid);
}
public static AlgorithmIdentifier getAlgorithmIdentifier(String digestName)
{
if (digestName.equals("SHA-1"))
{
return new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1, DERNull.INSTANCE);
}
if (digestName.equals("SHA-224"))
{
return new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha224);
}
if (digestName.equals("SHA-256"))
{
return new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256);
}
if (digestName.equals("SHA-384"))
{
return new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha384);
}
if (digestName.equals("SHA-512"))
{
return new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha512);
}
throw new IllegalArgumentException("unrecognised digest algorithm: " + digestName);
}
public static String getDigestName(ASN1ObjectIdentifier digestOid)
{
if (digestOid.equals(OIWObjectIdentifiers.idSHA1))
{
return "SHA-1";
}
if (digestOid.equals(NISTObjectIdentifiers.id_sha224))
{
return "SHA-224";
}
if (digestOid.equals(NISTObjectIdentifiers.id_sha256))
{
return "SHA-256";
}
if (digestOid.equals(NISTObjectIdentifiers.id_sha384))
{
return "SHA-384";
}
if (digestOid.equals(NISTObjectIdentifiers.id_sha512))
{
return "SHA-512";
}
throw new IllegalArgumentException("unrecognised digest algorithm: " + digestOid);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy