edits
authorbdemsky <bdemsky@uci.edu>
Sun, 24 Jul 2016 07:37:27 +0000 (00:37 -0700)
committerbdemsky <bdemsky@uci.edu>
Sun, 24 Jul 2016 07:37:27 +0000 (00:37 -0700)
src/java/iotcloud/CloudComm.java
src/java/iotcloud/Table.java

index 84d7ae4..85f6b08 100644 (file)
@@ -3,23 +3,52 @@ import java.io.*;
 import java.net.*;
 import java.util.Arrays;
 import javax.crypto.*;
+import javax.crypto.spec.*;
+import java.security.SecureRandom;
 
 class CloudComm {
        String baseurl;
        Cipher encryptcipher;
        Cipher decryptcipher;
        Mac mac;
+       byte[] salt;
+       SecretKeySpec key;
+       static final int SALT_SIZE = 8;
 
+       
        CloudComm() {
        }
 
-       CloudComm(String _baseurl, Cipher _encrypt, Cipher _decrypt, Mac _mac) {
+       CloudComm(String _baseurl, String password) {
                this.baseurl=_baseurl;
-               this.encryptcipher = _encrypt;
-               this.decryptcipher = _decrypt;
-               this.mac = _mac;
+               initCloud(password);
        }
 
+       private void initKey(String password) {
+               try {
+                       salt=new byte[SALT_SIZE];
+                       SecureRandom random = new SecureRandom();
+                       random.nextBytes(salt);
+                       PBEKeySpec keyspec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128);
+                       SecretKey key = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(keyspec);
+                       key = new SecretKeySpec(key.getEncoded(), "AES");
+               } catch (Exception e) {
+                       e.printStackTrace();
+                       throw new Error("Failed generating key.");
+               }
+       }
+
+       private void initCloud(String password) {
+               try {
+                       initKey(password);
+                       mac = Mac.getInstance("HmacSHA256");
+                       mac.init(key);
+               } catch (Exception e) {
+                       e.printStackTrace();
+                       throw new Error("Failed To Initialize Ciphers");
+               }
+       }
+       
        private URL buildRequest(boolean isput, long sequencenumber, long maxentries) throws IOException {
                String reqstring=isput?"req=putslot":"req=getslot";
                String urlstr=baseurl+"?"+reqstring+"&seq="+sequencenumber;
@@ -37,10 +66,11 @@ class CloudComm {
                        URLConnection con=url.openConnection();
                        HttpURLConnection http = (HttpURLConnection) con;
                        http.setRequestMethod("POST");
-                       http.setFixedLengthStreamingMode(bytes.length);
+                       http.setFixedLengthStreamingMode(bytes.length + SALT_SIZE);
                        http.setDoOutput(true);
                        http.connect();
                        OutputStream os=http.getOutputStream();
+                       os.write(salt);
                        os.write(bytes);
                        System.out.println(http.getResponseMessage());
 
@@ -59,6 +89,13 @@ class CloudComm {
                }
        }
 
+       /*
+                       Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
+                       encryptCipher.init(Cipher.ENCRYPT_MODE, secret);
+                       Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
+                       decryptCipher.init(Cipher.DECRYPT_MODE, secret);
+       */
+       
        public Slot[] getSlots(long sequencenumber) {
                try {
                        URL url=buildRequest(false, sequencenumber, 0);
index c92aef9..1d27932 100644 (file)
@@ -2,8 +2,6 @@ package iotcloud;
 import java.util.HashMap;
 import java.util.Arrays;
 import java.util.Vector;
-import javax.crypto.spec.*;
-import javax.crypto.*;
 
 final public class Table {
        private int numslots;
@@ -11,7 +9,6 @@ final public class Table {
        private HashMap<Long, Pair<Long, Liveness> > lastmessagetable=new HashMap<Long, Pair<Long, Liveness> >();
        private SlotBuffer buffer;
        private CloudComm cloud;
-       private Mac hmac;
        private long sequencenumber;
        private long localmachineid;
        private TableStatus lastTableStatus;
@@ -23,7 +20,7 @@ final public class Table {
                buffer = new SlotBuffer();
                numslots = buffer.capacity();
                sequencenumber = 0;
-               initCloud(baseurl, password);
+               cloud=new CloudComm(baseurl, password);
        }
 
        public Table(CloudComm _cloud, long _localmachineid) {
@@ -34,32 +31,6 @@ final public class Table {
                cloud=_cloud;
        }
 
-       private void initCloud(String baseurl, String password) {
-               try {
-                       SecretKeySpec secret=getKey(password);
-                       Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
-                       encryptCipher.init(Cipher.ENCRYPT_MODE, secret);
-                       Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
-                       decryptCipher.init(Cipher.DECRYPT_MODE, secret);
-                       hmac = Mac.getInstance("HmacSHA256");
-                       hmac.init(secret);
-                       cloud=new CloudComm(baseurl, encryptCipher, decryptCipher, hmac);
-               } catch (Exception e) {
-                       throw new Error("Failed To Initialize Ciphers");
-               }
-       }
-
-       private SecretKeySpec getKey(String password) {
-               try {
-                       PBEKeySpec keyspec = new PBEKeySpec(password.toCharArray());
-                       SecretKey key = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(keyspec);
-                       SecretKeySpec secret = new SecretKeySpec(key.getEncoded(), "AES");
-                       return secret;
-               } catch (Exception e) {
-                       throw new Error("Failed generating key.");
-               }
-       }
-
        public void update() {
                Slot[] newslots=cloud.getSlots(sequencenumber+1);