├── README.md └── src ├── yao ├── CryptChain.java ├── Main.java ├── Transfer.java ├── Utils.java └── gate │ ├── AndGate.java │ ├── Gate.java │ ├── GenericGate.java │ ├── OrGate.java │ ├── Wire.java │ └── XorGate.java └── yao4 └── gate ├── AndGate.java ├── Gate.java ├── GenericGate.java ├── OrGate.java ├── Wire.java └── XorGate.java /README.md: -------------------------------------------------------------------------------- 1 | Introduction 2 | ============ 3 | 4 | The classic approach of secure function evaluation (SFE) also deals with encrypted programs but is often based on Yao’s Garbled Circuits. The concept of garbled circuits is an alternative to homomorphically encrypted function representations and is currently considered the most effective way to hide program functionality. In contrast to FHE encryted data, garbled circuits encrypt the function tables of boolean gates. The input values are used as row and column keys to decrypt the output value of the gates. Garbled circuits only support one pass directed, acyclic circuits. 5 | 6 | The Java-based sample implementation uses AES to encrypt the function tables and supports delegation only, i.e. the untrusted party is not allowed to input it’s own data which the first party encrypts during an oblivious transfer protocol. 7 | 8 | 9 | License 10 | ======= 11 | 12 | The source code is published under a MIT license: 13 | 14 | Copyright © 2011 15 | 16 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 17 | 18 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /src/yao/CryptChain.java: -------------------------------------------------------------------------------- 1 | package yao; 2 | 3 | import java.security.KeyPair; 4 | 5 | import yao.gate.Gate; 6 | import yao.gate.Wire; 7 | import yao.gate.XorGate; 8 | 9 | public class CryptChain 10 | { 11 | 12 | 13 | public static void main(String[] args) throws Exception 14 | { 15 | KeyPair kp=Utils.genRSAkeypair(); 16 | 17 | Wire a1=new Wire(); 18 | Wire a2=new Wire(); 19 | Wire ra=new Wire(); 20 | Gate g1=new XorGate(a1,a2,ra); 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/yao/Main.java: -------------------------------------------------------------------------------- 1 | package yao; 2 | 3 | import yao.gate.AndGate; 4 | import yao.gate.Gate; 5 | import yao.gate.Wire; 6 | import yao.gate.XorGate; 7 | 8 | public class Main 9 | { 10 | public static void main(String [] args) throws Exception 11 | { 12 | new Main()._main(); 13 | } 14 | 15 | void _main() throws Exception 16 | { 17 | /* PHASE A: CONSTRUCTION (on trusted resource) 18 | * ===========================================*/ 19 | 20 | /* (a1 XOR a2) XOR (b1 AND b2) 21 | * 22 | * ra=a1 XOR a2 23 | * rb=b1 AND b2 24 | * r =ra XOR rb 25 | * 26 | */ 27 | 28 | Wire a1=new Wire(); 29 | Wire a2=new Wire(); 30 | Wire b1=new Wire(); 31 | Wire b2=new Wire(); 32 | Wire ra=new Wire(); 33 | Wire rb=new Wire(); 34 | Wire r=new Wire(3); 35 | 36 | Gate g1=new XorGate(a1,a2,ra); 37 | Gate g2=new AndGate(b1,b2,rb); 38 | Gate g3=new XorGate(ra,rb,r); 39 | 40 | /* ship the luts and input wires 41 | * to the untrusted evaluator 42 | * 43 | * we want him to execute (1 XOR 0) XOR (1 AND 1) 44 | */ 45 | byte[][] lut_g1=g1.getLut(); 46 | byte[][] lut_g2=g2.getLut(); 47 | byte[][] lut_g3=g3.getLut(); 48 | byte[] in_a1=a1.getValue1(); 49 | byte[] in_a2=a2.getValue0(); 50 | byte[] in_b1=b1.getValue1(); 51 | byte[] in_b2=b2.getValue1(); 52 | 53 | /* PHASE T: TRANSPORT-FORMAT 54 | * =========================*/ 55 | 56 | Utils.printLut(g1, "g1"); 57 | Utils.printLut(g2, "g2"); 58 | Utils.printLut(g3, "g3"); 59 | System.out.println("in_a1:"+Utils.getHex(in_a1)); 60 | System.out.println("in_a2:"+Utils.getHex(in_a2)); 61 | System.out.println("in_b1:"+Utils.getHex(in_b1)); 62 | System.out.println("in_b2:"+Utils.getHex(in_b2)); 63 | 64 | 65 | /* PHASE B: EVALUATE (on untrusted resource) 66 | * =========================================*/ 67 | 68 | //notice that the evaluator instantiates untyped gates 69 | Gate gate1=new Gate(lut_g1); 70 | Gate gate2=new Gate(lut_g2); 71 | Gate gate3=new Gate(lut_g3); 72 | 73 | byte[] r1=gate1.operate(in_a2, in_a1); 74 | byte[] r2=gate2.operate(in_b2, in_b1); 75 | byte[] r3=gate3.operate(r2, r1); 76 | System.out.println("encrypted result="+Utils.getHex(r3)); 77 | 78 | /* PHASE C: RESULT DECRYPTION (on trustes resource) 79 | * ================================================*/ 80 | 81 | if(Utils.arraysAreEqual(r3,r.getValue0())) 82 | System.out.println("result is 0"); 83 | // else if(Utils.arraysAreEqual(r3,r.getValue0())) 84 | // System.out.println("result is 0"); 85 | else if(Utils.arraysAreEqual(r3,r.getValue1())) 86 | System.out.println("result is 1"); 87 | // else if(Utils.arraysAreEqual(r3,r.getValue1())) 88 | // System.out.println("result is 1"); 89 | else 90 | System.out.println("result is undefined"); 91 | 92 | // KeyPair kp=Utils.genRSAkeypair(); 93 | // 94 | // byte[] secret=new byte[]{0x12,0x34,0x56,0x78}; 95 | // 96 | // byte[] cipher=Utils.RSAencrypt(secret, kp.getPublic()); 97 | // 98 | // System.out.println("RSA encrypted="+Utils.getHex(cipher)); 99 | // 100 | // secret=Utils.RSAdecrypt(cipher, kp.getPrivate()); 101 | // 102 | // System.out.println("RSA decrypted="+Utils.getHex(secret)); 103 | // 104 | // new Transfer().exchange(); 105 | 106 | 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/yao/Transfer.java: -------------------------------------------------------------------------------- 1 | package yao; 2 | 3 | import java.math.BigInteger; 4 | import java.security.KeyFactory; 5 | import java.security.KeyPair; 6 | import java.security.PrivateKey; 7 | import java.security.PublicKey; 8 | import java.security.spec.RSAPrivateKeySpec; 9 | import java.security.spec.RSAPublicKeySpec; 10 | import java.util.Random; 11 | 12 | public class Transfer 13 | { 14 | Random rand=new Random(); 15 | 16 | public void exchange() throws Exception 17 | { 18 | KeyPair keypair=Utils.genRSAkeypair(); 19 | KeyFactory fact = KeyFactory.getInstance("RSA"); 20 | 21 | PrivateKey Alice_privatekey=keypair.getPrivate(); 22 | RSAPrivateKeySpec Alice_priv=fact.getKeySpec(Alice_privatekey,RSAPrivateKeySpec.class); 23 | BigInteger Alice_N=Alice_priv.getModulus(); 24 | BigInteger Alice_d=Alice_priv.getPrivateExponent(); 25 | System.out.println("Alice_d="+Alice_d.toString(10)); 26 | BigInteger Alice_m0=new BigInteger("12345",10); 27 | BigInteger Alice_m1=new BigInteger("67890",10); 28 | BigInteger Alice_x0=new BigInteger(1024,rand); 29 | BigInteger Alice_x1=new BigInteger(1024,rand); 30 | 31 | PublicKey Bob_publickey=keypair.getPublic(); 32 | RSAPublicKeySpec Bob_pub = fact.getKeySpec(Bob_publickey,RSAPublicKeySpec.class); 33 | BigInteger Bob_N=Bob_pub.getModulus(); 34 | BigInteger Bob_e=Bob_pub.getPublicExponent(); 35 | 36 | System.out.println("Bob_e="+Bob_e.toString(10)); 37 | BigInteger Bob_x0=Alice_x0; 38 | BigInteger Bob_x1=Alice_x1; 39 | 40 | BigInteger Bob_secretx=Bob_x1; //wir wollen m1 haben 41 | BigInteger Bob_k=new BigInteger(1024,rand); 42 | BigInteger Bob_v=Bob_secretx.add(Bob_k.pow(Bob_e.intValue())).mod(Bob_N); 43 | 44 | BigInteger Alice_v=Bob_v; 45 | 46 | 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/yao/Utils.java: -------------------------------------------------------------------------------- 1 | package yao; 2 | 3 | import java.security.KeyPair; 4 | import java.security.KeyPairGenerator; 5 | import java.security.MessageDigest; 6 | import java.security.PrivateKey; 7 | import java.security.PublicKey; 8 | 9 | import javax.crypto.Cipher; 10 | import javax.crypto.KeyGenerator; 11 | import javax.crypto.SecretKey; 12 | import javax.crypto.spec.SecretKeySpec; 13 | 14 | import yao.gate.Gate; 15 | 16 | public class Utils 17 | { 18 | public static void swap(byte[] a,byte[] b) 19 | { 20 | byte[] h; 21 | h=a; 22 | a=b; 23 | b=h; 24 | } 25 | 26 | public static void printLut(Gate gate,String title) 27 | { 28 | System.out.println(title); 29 | for(int i=0;i<4;i++) 30 | { 31 | System.out.println(getHex(gate.getLutEntry(i))); 32 | } 33 | System.out.println(); 34 | } 35 | 36 | public static String getHex(byte[] b) 37 | { 38 | String result=""; 39 | for(int j=0;j