1 package civitas.crypto.signedciphertext;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.stereotype.Controller;
5
6 import civitas.crypto.CryptoBase;
7 import civitas.crypto.messagedigest.CryptoHash;
8 import civitas.crypto.msg.CryptMessage;
9 import civitas.crypto.parameters.ElGamalParameters;
10 import civitas.crypto.publickey.ElGamalPublicKey;
11 import civitas.crypto.reencryptfactor.ElGamalReencryptFactor;
12 import civitas.crypto.reencryptfactor.GenerateElGamalReencryptFactor;
13 import civitas.util.CivitasBigInteger;
14
15 @Controller
16 public class SignAndEncrypt {
17
18 @Autowired
19 CryptoBase cryptoBase;
20
21 @Autowired
22 CryptoHash cryptoHash;
23
24 @Autowired
25 GenerateElGamalReencryptFactor generateElGamalReencryptFactor;
26
27 public ElGamalSignedCiphertext apply(
28 final ElGamalPublicKey key,
29 final CryptMessage msg,
30 final ElGamalReencryptFactor r,
31 final byte[] additionalEnv) {
32 ElGamalParameters ps = key.params;
33 CivitasBigInteger m = msg.m();
34 CivitasBigInteger rr = r.r();
35 CivitasBigInteger s = cryptoBase.generateRandomElement(ps.q);
36 CivitasBigInteger a = ps.g.modPow(rr, ps.p);
37 CivitasBigInteger b = m.modMultiply(key.y.modPow(rr, ps.p), ps.p);
38 CivitasBigInteger c =
39 cryptoHash.apply(ps.g.modPow(s, ps.p), a, b, additionalEnv).mod(ps.q);
40 CivitasBigInteger d = s.modAdd(c.modMultiply(rr, ps.q), ps.q);
41 return new ElGamalSignedCiphertext(a, b, c, d);
42 }
43 }