diff options
author | Sergio Giro <sgiro@google.com> | 2016-02-01 18:52:42 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2016-02-01 18:52:42 +0000 |
commit | 9218edabd1ef9852bc2f13115dcadc81b442dd6c (patch) | |
tree | 8229ff72c8cbb06f49dce3a8382930919fa6fc2b /bcprov/src/main/java/org/bouncycastle/pqc/jcajce/provider/mceliece/BCMcElieceCCA2PublicKey.java | |
parent | 9b30eb05e5be69d51881a0d1b31e503e97acd784 (diff) | |
parent | 397d32894b89b506dc318e0f83446187c9b76ebe (diff) | |
download | android_external_bouncycastle-9218edabd1ef9852bc2f13115dcadc81b442dd6c.tar.gz android_external_bouncycastle-9218edabd1ef9852bc2f13115dcadc81b442dd6c.tar.bz2 android_external_bouncycastle-9218edabd1ef9852bc2f13115dcadc81b442dd6c.zip |
Merge "Merge remote-tracking branch 'aosp/upstream-master' into merge-152-from-upstream"
Diffstat (limited to 'bcprov/src/main/java/org/bouncycastle/pqc/jcajce/provider/mceliece/BCMcElieceCCA2PublicKey.java')
-rw-r--r-- | bcprov/src/main/java/org/bouncycastle/pqc/jcajce/provider/mceliece/BCMcElieceCCA2PublicKey.java | 226 |
1 files changed, 226 insertions, 0 deletions
diff --git a/bcprov/src/main/java/org/bouncycastle/pqc/jcajce/provider/mceliece/BCMcElieceCCA2PublicKey.java b/bcprov/src/main/java/org/bouncycastle/pqc/jcajce/provider/mceliece/BCMcElieceCCA2PublicKey.java new file mode 100644 index 0000000..f606464 --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/pqc/jcajce/provider/mceliece/BCMcElieceCCA2PublicKey.java @@ -0,0 +1,226 @@ +package org.bouncycastle.pqc.jcajce.provider.mceliece; + + +import java.io.IOException; +import java.security.PublicKey; + +import org.bouncycastle.asn1.ASN1ObjectIdentifier; +import org.bouncycastle.asn1.ASN1Primitive; +import org.bouncycastle.asn1.DERNull; +import org.bouncycastle.asn1.x509.AlgorithmIdentifier; +import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; +import org.bouncycastle.crypto.CipherParameters; +import org.bouncycastle.pqc.asn1.McElieceCCA2PublicKey; +import org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2KeyPairGenerator; +import org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2Parameters; +import org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2PublicKeyParameters; +import org.bouncycastle.pqc.jcajce.spec.McElieceCCA2PublicKeySpec; +import org.bouncycastle.pqc.math.linearalgebra.GF2Matrix; + +/** + * This class implements a McEliece CCA2 public key and is usually instantiated + * by the {@link McElieceCCA2KeyPairGenerator} or {@link McElieceCCA2KeyFactorySpi}. + */ +public class BCMcElieceCCA2PublicKey + implements CipherParameters, PublicKey +{ + + /** + * + */ + private static final long serialVersionUID = 1L; + + // the OID of the algorithm + private String oid; + + // the length of the code + private int n; + + // the error correction capability of the code + private int t; + + // the generator matrix + private GF2Matrix g; + + private McElieceCCA2Parameters McElieceCCA2Params; + + /** + * Constructor (used by the {@link McElieceCCA2KeyPairGenerator}). + * + * @param n the length of the code + * @param t the error correction capability of the code + * @param g the generator matrix + */ + public BCMcElieceCCA2PublicKey(String oid, int n, int t, GF2Matrix g) + { + this.oid = oid; + this.n = n; + this.t = t; + this.g = g; + } + + /** + * Constructor (used by the {@link McElieceCCA2KeyFactorySpi}). + * + * @param keySpec a {@link McElieceCCA2PublicKeySpec} + */ + public BCMcElieceCCA2PublicKey(McElieceCCA2PublicKeySpec keySpec) + { + this(keySpec.getOIDString(), keySpec.getN(), keySpec.getT(), keySpec.getMatrixG()); + } + + public BCMcElieceCCA2PublicKey(McElieceCCA2PublicKeyParameters params) + { + this(params.getOIDString(), params.getN(), params.getT(), params.getMatrixG()); + this.McElieceCCA2Params = params.getParameters(); + } + + /** + * Return the name of the algorithm. + * + * @return "McEliece" + */ + public String getAlgorithm() + { + return "McEliece"; + } + + /** + * @return the length of the code + */ + public int getN() + { + return n; + } + + /** + * @return the dimension of the code + */ + public int getK() + { + return g.getNumRows(); + } + + /** + * @return the error correction capability of the code + */ + public int getT() + { + return t; + } + + /** + * @return the generator matrix + */ + public GF2Matrix getG() + { + return g; + } + + /** + * @return a human readable form of the key + */ + public String toString() + { + String result = "McEliecePublicKey:\n"; + result += " length of the code : " + n + "\n"; + result += " error correction capability: " + t + "\n"; + result += " generator matrix : " + g.toString(); + return result; + } + + /** + * Compare this key with another object. + * + * @param other the other object + * @return the result of the comparison + */ + public boolean equals(Object other) + { + if (other == null || !(other instanceof BCMcElieceCCA2PublicKey)) + { + return false; + } + + BCMcElieceCCA2PublicKey otherKey = (BCMcElieceCCA2PublicKey)other; + + return (n == otherKey.n) && (t == otherKey.t) && (g.equals(otherKey.g)); + } + + /** + * @return the hash code of this key + */ + public int hashCode() + { + return n + t + g.hashCode(); + } + + /** + * @return the OID of the algorithm + */ + public String getOIDString() + { + return oid; + } + + /** + * @return the OID to encode in the SubjectPublicKeyInfo structure + */ + protected ASN1ObjectIdentifier getOID() + { + return new ASN1ObjectIdentifier(McElieceCCA2KeyFactorySpi.OID); + } + + /** + * @return the algorithm parameters to encode in the SubjectPublicKeyInfo + * structure + */ + protected ASN1Primitive getAlgParams() + { + return null; // FIXME: needed at all? + } + + /** + * Return the keyData to encode in the SubjectPublicKeyInfo structure. + * <p> + * The ASN.1 definition of the key structure is + * <pre> + * McEliecePublicKey ::= SEQUENCE { + * n Integer -- length of the code + * t Integer -- error correcting capability + * matrixG OctetString -- generator matrix as octet string + * } + * </pre> + * </p> + * @return the keyData to encode in the SubjectPublicKeyInfo structure + */ + public byte[] getEncoded() + { + McElieceCCA2PublicKey key = new McElieceCCA2PublicKey(new ASN1ObjectIdentifier(oid), n, t, g); + AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(this.getOID(), DERNull.INSTANCE); + + try + { + SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo(algorithmIdentifier, key); + + return subjectPublicKeyInfo.getEncoded(); + } + catch (IOException e) + { + return null; + } + + } + + public String getFormat() + { + // TODO Auto-generated method stub + return null; + } + + public McElieceCCA2Parameters getMcElieceCCA2Parameters() + { + return McElieceCCA2Params; + } + +} |