diff options
author | David Hook <dgh@cryptoworkshop.com> | 2013-05-31 11:07:45 +0400 |
---|---|---|
committer | David Hook <dgh@cryptoworkshop.com> | 2013-05-31 11:07:45 +0400 |
commit | 2b976f5364cfdbc37d3086019d93483c983eb80b (patch) | |
tree | cb846af3fd1d43f9c2562a1fb2d06b997ad8f229 /core/src/main/java/org/bouncycastle/pqc/crypto/mceliece/McEliecePointchevalDigestCipher.java | |
parent | 5f714bd92fbd780d22406f4bc3681be005f6f04a (diff) |
initial reshuffle
Diffstat (limited to 'core/src/main/java/org/bouncycastle/pqc/crypto/mceliece/McEliecePointchevalDigestCipher.java')
-rw-r--r-- | core/src/main/java/org/bouncycastle/pqc/crypto/mceliece/McEliecePointchevalDigestCipher.java | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/core/src/main/java/org/bouncycastle/pqc/crypto/mceliece/McEliecePointchevalDigestCipher.java b/core/src/main/java/org/bouncycastle/pqc/crypto/mceliece/McEliecePointchevalDigestCipher.java new file mode 100644 index 00000000..8a1ed62a --- /dev/null +++ b/core/src/main/java/org/bouncycastle/pqc/crypto/mceliece/McEliecePointchevalDigestCipher.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 McEliecePointchevalDigestCipher +{ + + private final Digest messDigest; + + private final MessageEncryptor mcElieceCCA2Cipher; + + private boolean forEncrypting; + + + public McEliecePointchevalDigestCipher(MessageEncryptor mcElieceCCA2Cipher, Digest messDigest) + { + this.mcElieceCCA2Cipher = mcElieceCCA2Cipher; + 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(); + + mcElieceCCA2Cipher.init(forEncrypting, param); + } + + + public byte[] messageEncrypt() + { + if (!forEncrypting) + { + throw new IllegalStateException("McEliecePointchevalDigestCipher not initialised for encrypting."); + } + + byte[] hash = new byte[messDigest.getDigestSize()]; + messDigest.doFinal(hash, 0); + byte[] enc = null; + + try + { + enc = mcElieceCCA2Cipher.messageEncrypt(hash); + } + catch (Exception e) + { + e.printStackTrace(); + } + + + return enc; + } + + + public byte[] messageDecrypt(byte[] ciphertext) + { + byte[] output = null; + if (forEncrypting) + { + throw new IllegalStateException("McEliecePointchevalDigestCipher not initialised for decrypting."); + } + + + try + { + output = mcElieceCCA2Cipher.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(); + + } + + +} |