Garbler.java 937 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package com.oblivm.backend.gc.regular;
  2. import java.nio.ByteBuffer;
  3. import java.security.MessageDigest;
  4. import com.oblivm.backend.gc.GCSignal;
  5. final class Garbler {
  6. private MessageDigest sha1 = null;
  7. Garbler() {
  8. try {
  9. sha1 = MessageDigest.getInstance("SHA-1");
  10. } catch (Exception e) {
  11. e.printStackTrace();
  12. System.exit(1);
  13. }
  14. }
  15. public void enc(GCSignal lb0, GCSignal lb1, long k, GCSignal m, GCSignal ret) {
  16. getPadding(lb0, lb1, k, ret);
  17. GCSignal.xor(ret, m, ret);
  18. }
  19. public void dec(GCSignal lb0, GCSignal lb1, long k, GCSignal c, GCSignal ret) {
  20. getPadding(lb0, lb1, k, ret);
  21. GCSignal.xor(ret, c, ret);
  22. }
  23. ByteBuffer buffer = ByteBuffer.allocate(GCSignal.len * 2 + 8);
  24. private void getPadding(GCSignal lb0, GCSignal lb1, long k, GCSignal ret) {
  25. buffer.clear();
  26. sha1.update((buffer.put(lb0.bytes).put(lb1.bytes).putLong(k)));
  27. System.arraycopy(sha1.digest(), 0, ret.bytes, 0, GCSignal.len);
  28. }
  29. }