|
| 1 | +package org.bouncycastle.operator.bc; |
| 2 | + |
| 3 | +import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
| 4 | +import org.bouncycastle.crypto.CipherParameters; |
| 5 | +import org.bouncycastle.crypto.CryptoException; |
| 6 | +import org.bouncycastle.crypto.DataLengthException; |
| 7 | +import org.bouncycastle.crypto.Digest; |
| 8 | +import org.bouncycastle.crypto.Signer; |
| 9 | +import org.bouncycastle.operator.OperatorCreationException; |
| 10 | +import org.bouncycastle.pqc.crypto.MessageSigner; |
| 11 | +import org.bouncycastle.pqc.crypto.lms.HSSSigner; |
| 12 | + |
| 13 | +public class BcHssLmsContentSignerBuilder |
| 14 | + extends BcContentSignerBuilder |
| 15 | +{ |
| 16 | + public BcHssLmsContentSignerBuilder(AlgorithmIdentifier sigAlgId, AlgorithmIdentifier digAlgId) |
| 17 | + { |
| 18 | + super(sigAlgId, digAlgId); |
| 19 | + } |
| 20 | + |
| 21 | + protected Signer createSigner(AlgorithmIdentifier sigAlgId, AlgorithmIdentifier digAlgId) |
| 22 | + throws OperatorCreationException |
| 23 | + { |
| 24 | + Digest dig = digestProvider.get(digAlgId); |
| 25 | + |
| 26 | + return new HssSigner(dig); |
| 27 | + } |
| 28 | + |
| 29 | + private static class HssSigner |
| 30 | + implements Signer |
| 31 | + { |
| 32 | + private final MessageSigner hss = new HSSSigner(); |
| 33 | + private final Digest digest; |
| 34 | + |
| 35 | + public HssSigner(Digest digest) |
| 36 | + { |
| 37 | + this.digest = digest; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void init(boolean forSigning, CipherParameters param) |
| 42 | + { |
| 43 | + hss.init(forSigning, param); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void update(byte b) |
| 48 | + { |
| 49 | + digest.update(b); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void update(byte[] in, int off, int len) |
| 54 | + { |
| 55 | + digest.update(in, off, len); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public byte[] generateSignature() |
| 60 | + throws CryptoException, DataLengthException |
| 61 | + { |
| 62 | + byte[] hash = new byte[digest.getDigestSize()]; |
| 63 | + digest.doFinal(hash, 0); |
| 64 | + return hss.generateSignature(hash); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public boolean verifySignature(byte[] signature) |
| 69 | + { |
| 70 | + byte[] hash = new byte[digest.getDigestSize()]; |
| 71 | + digest.doFinal(hash, 0); |
| 72 | + return hss.verifySignature(hash, signature); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void reset() |
| 77 | + { |
| 78 | + digest.reset(); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments