NPOTSender.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (C) 2013 by Yan Huang <yhuang@cs.umd.edu>
  2. package com.oblivm.backend.ot;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.math.BigInteger;
  10. import java.security.NoSuchAlgorithmException;
  11. import java.security.SecureRandom;
  12. import java.security.Security;
  13. import com.oblivm.backend.flexsc.Flag;
  14. import com.oblivm.backend.gc.GCSignal;
  15. import com.oblivm.backend.network.Network;
  16. import com.oblivm.backend.rand.ISAACProvider;
  17. public class NPOTSender extends OTSender {
  18. static SecureRandom rnd;
  19. static {
  20. Security.addProvider(new ISAACProvider());
  21. try {
  22. rnd = SecureRandom.getInstance("ISAACRandom");
  23. } catch (NoSuchAlgorithmException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. private static final int certainty = 80;
  28. private final static int qLength = 160; // 512;
  29. private final static int pLength = 1024; // 15360;
  30. private BigInteger p, q, g, C, r;
  31. private BigInteger Cr, gr;
  32. Cipher cipher;
  33. public NPOTSender(int msgBitLength, Network channel) throws Exception {
  34. super(msgBitLength, channel);
  35. cipher = new Cipher();
  36. initialize();
  37. }
  38. public void send(GCSignal[][] msgPairs) throws IOException {
  39. step1(msgPairs);
  40. }
  41. private void initialize() throws Exception {
  42. File keyfile = new File("NPOTKey");
  43. if (keyfile.exists()) {
  44. FileInputStream fin = new FileInputStream(keyfile);
  45. ObjectInputStream fois = new ObjectInputStream(fin);
  46. C = (BigInteger) fois.readObject();
  47. p = (BigInteger) fois.readObject();
  48. q = (BigInteger) fois.readObject();
  49. g = (BigInteger) fois.readObject();
  50. gr = (BigInteger) fois.readObject();
  51. r = (BigInteger) fois.readObject();
  52. fois.close();
  53. Flag.sw.startOTIO();
  54. channel.writeBI(C);
  55. channel.writeBI(p);
  56. channel.writeBI(q);
  57. channel.writeBI(g);
  58. channel.writeBI(gr);
  59. channel.writeInt(msgBitLength);
  60. channel.flush();
  61. Flag.sw.stopOTIO();
  62. Cr = C.modPow(r, p);
  63. } else {
  64. BigInteger pdq;
  65. q = new BigInteger(qLength, certainty, rnd);
  66. do {
  67. pdq = new BigInteger(pLength - qLength, rnd);
  68. pdq = pdq.clearBit(0);
  69. p = q.multiply(pdq).add(BigInteger.ONE);
  70. } while (!p.isProbablePrime(certainty));
  71. do {
  72. g = new BigInteger(pLength - 1, rnd);
  73. } while ((g.modPow(pdq, p)).equals(BigInteger.ONE) || (g.modPow(q, p)).equals(BigInteger.ONE));
  74. r = (new BigInteger(qLength, rnd)).mod(q);
  75. gr = g.modPow(r, p);
  76. C = (new BigInteger(qLength, rnd)).mod(q);
  77. Flag.sw.startOTIO();
  78. channel.writeBI(C);
  79. channel.writeBI(p);
  80. channel.writeBI(q);
  81. channel.writeBI(g);
  82. channel.writeBI(gr);
  83. channel.writeInt(msgBitLength);
  84. channel.flush();
  85. Flag.sw.stopOTIO();
  86. Cr = C.modPow(r, p);
  87. FileOutputStream fout = new FileOutputStream(keyfile);
  88. ObjectOutputStream foos = new ObjectOutputStream(fout);
  89. foos.writeObject(C);
  90. foos.writeObject(p);
  91. foos.writeObject(q);
  92. foos.writeObject(g);
  93. foos.writeObject(gr);
  94. foos.writeObject(r);
  95. foos.flush();
  96. foos.close();
  97. }
  98. }
  99. GCSignal[][] m = new GCSignal[1][2];
  100. @Override
  101. public void send(GCSignal[] msgPair) throws IOException {
  102. m[0][0] = msgPair[0];
  103. m[0][1] = msgPair[1];
  104. send(m);
  105. }
  106. private void step1(GCSignal[][] msgPairs) throws IOException {
  107. BigInteger[] pk0 = new BigInteger[msgPairs.length];
  108. Flag.sw.startOTIO();
  109. for (int i = 0; i < pk0.length; i++)
  110. pk0[i] = channel.readBI();
  111. Flag.sw.stopOTIO();
  112. BigInteger[] pk1 = new BigInteger[msgPairs.length];
  113. BigInteger[][] msg = new BigInteger[msgPairs.length][2];
  114. for (int i = 0; i < msgPairs.length; i++) {
  115. pk0[i] = pk0[i].modPow(r, p);
  116. pk1[i] = Cr.multiply(pk0[i].modInverse(p)).mod(p);
  117. msg[i][0] = cipher.encrypt(pk0[i].toByteArray(), new BigInteger(msgPairs[i][0].bytes), msgBitLength);
  118. msg[i][1] = cipher.encrypt(pk1[i].toByteArray(), new BigInteger(msgPairs[i][1].bytes), msgBitLength);
  119. }
  120. Flag.sw.startOTIO();
  121. for (int i = 0; i < msg.length; i++) {
  122. channel.writeBI(msg[i][0]);
  123. channel.writeBI(msg[i][1]);
  124. }
  125. channel.flush();
  126. Flag.sw.stopOTIO();
  127. }
  128. }