diff options
Diffstat (limited to 'bcprov/src/main/java/org/bouncycastle/pqc/crypto/mceliece/McEliecePKCSDigestCipher.java')
-rw-r--r-- | bcprov/src/main/java/org/bouncycastle/pqc/crypto/mceliece/McEliecePKCSDigestCipher.java | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/bcprov/src/main/java/org/bouncycastle/pqc/crypto/mceliece/McEliecePKCSDigestCipher.java b/bcprov/src/main/java/org/bouncycastle/pqc/crypto/mceliece/McEliecePKCSDigestCipher.java new file mode 100644 index 0000000..d8e6ba2 --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/pqc/crypto/mceliece/McEliecePKCSDigestCipher.java @@ -0,0 +1,128 @@ +package org.bouncycastle.pqc.crypto.mceliece; + + +import org.bouncycastle.crypto.CipherParameters; +import org.bouncycastle.crypto.Digest; +import org.bouncycastle.crypto.params.AsymmetricKeyParameter; +import org.bouncycastle.crypto.params.ParametersWithRandom; +import org.bouncycastle.pqc.crypto.MessageEncryptor; + +// TODO should implement some interface? +public class McEliecePKCSDigestCipher +{ + + private final Digest messDigest; + + private final MessageEncryptor mcElieceCipher; + + private boolean forEncrypting; + + + public McEliecePKCSDigestCipher(MessageEncryptor mcElieceCipher, Digest messDigest) + { + this.mcElieceCipher = mcElieceCipher; + this.messDigest = messDigest; + } + + + public void init(boolean forEncrypting, + CipherParameters param) + { + + this.forEncrypting = forEncrypting; + AsymmetricKeyParameter k; + + if (param instanceof ParametersWithRandom) + { + k = (AsymmetricKeyParameter)((ParametersWithRandom)param).getParameters(); + } + else + { + k = (AsymmetricKeyParameter)param; + } + + if (forEncrypting && k.isPrivate()) + { + throw new IllegalArgumentException("Encrypting Requires Public Key."); + } + + if (!forEncrypting && !k.isPrivate()) + { + throw new IllegalArgumentException("Decrypting Requires Private Key."); + } + + reset(); + + mcElieceCipher.init(forEncrypting, param); + } + + + public byte[] messageEncrypt() + { + if (!forEncrypting) + { + throw new IllegalStateException("McEliecePKCSDigestCipher not initialised for encrypting."); + } + + byte[] hash = new byte[messDigest.getDigestSize()]; + messDigest.doFinal(hash, 0); + byte[] enc = null; + + try + { + enc = mcElieceCipher.messageEncrypt(hash); + } + catch (Exception e) + { + e.printStackTrace(); + } + + + return enc; + } + + + public byte[] messageDecrypt(byte[] ciphertext) + { + byte[] output = null; + if (forEncrypting) + { + throw new IllegalStateException("McEliecePKCSDigestCipher not initialised for decrypting."); + } + + + try + { + output = mcElieceCipher.messageDecrypt(ciphertext); + } + catch (Exception e) + { + e.printStackTrace(); + } + + + return output; + } + + + public void update(byte b) + { + messDigest.update(b); + + } + + public void update(byte[] in, int off, int len) + { + messDigest.update(in, off, len); + + } + + + public void reset() + { + messDigest.reset(); + + } + + +} |