title: Arduino to Android secure bluetooth connection
body: <p>I'm using an Arduino Uno and a nRF8001 board from Adafruit to connect to an Android phone over bluetooth. I will be using it to lock and unlock a lock and I need to make sure only verified devices are able to initiate the locking and unlocking. I've searched around a bunch and I'm having trouble finding a clear example of what I should do to verify the connecting device. Currently I have the Arduino hooked up to the lock and whenever the Android phone connects, it is allowed to lock and unlock. </p>
  
  <p>The process I had in mind is as follows.</p>
  
  <ol>
  <li>Android tries to connect to the Arduino</li>
  <li>Arduino sees the request and sends a random string to the Android device</li>
  <li>The Android encrypts the string with a shared secret key and sends it back to the Arduino</li>
  <li>The Arduino decrypts the encrypted string and verifies it matches the original it sent if it does it it goes ahead and connects/continues with locking or unlocking.</li>
  </ol>
  
  <p>[EDIT]
  I've done a lot more research and work and from advice from security.stackexchange I decided to use AES to do the encryption. I am using <a href="http://forum.arduino.cc/index.php?topic=88890.0" rel="nofollow">this Arduino library</a> and <a href="https://github.com/tozny/java-aes-crypto" rel="nofollow">this Android library</a> I'm a little confused on what configuration I should use on the two libraries I am using from programming an Arduino and Android.</p>
  
  <p>My Arduino code I set up to test encrypting an decrypting:</p>
  
  <pre><code>#include &lt;AES.h&gt;
  
  AES aes ;
  
  byte key[] = 
  {
    0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  } ;
  
  byte plain[] =
  {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  } ;
  
  byte my_iv[] = 
  {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  } ;
  
  char PassString[] = "This is hard to believe but true";
  char Message[] = "We, the Fairies, blithe and antic Of dimensions not gigantic, Th";
  
  byte cipher [4*N_BLOCK] ;
  byte check [4*N_BLOCK] ;
  
  int bits = 128;
  int blocks = 4;
  
  void loop () 
  {}
  
  
  void setup ()
  {
    Serial.begin (9600) ;
    Serial.println ("Starting encryption...") ;
  
    byte iv [N_BLOCK] ;
  
    // Pass the key into the byte array
    for (int i = 0; i &lt; 32; i++) {
      key[i] = PassString[i];
    }
  
    print_value ("KEY = ", key, 256) ;
  
    for (int i = 0; i &lt; 64; i++) {
      plain[i] = Message[i];
    }
  
    // Set Key
    byte succ = aes.set_key (key, bits) ;
  
    // Encrypt
    for (byte i = 0 ; i &lt; 16 ; i++)
      iv[i] = my_iv[i] ;
    succ = aes.cbc_encrypt (plain, cipher, blocks, iv) ;
  
    // Decrypt
    for (byte i = 0 ; i &lt; 16 ; i++)
      iv[i] = my_iv[i] ;
    succ = aes.cbc_decrypt (cipher, check, blocks, iv) ;
  
    // Prints the plain, ciper, decrypted, and IV
    for (byte ph = 0 ; ph &lt; (blocks == 1 ? 3 : 4) ; ph++)
    {
      for (byte i = 0 ; i &lt; (ph &lt; 3 ? blocks*N_BLOCK : N_BLOCK) ; i++)
      {
        byte val = ph == 0 ? plain[i] : ph == 1 ? cipher[i] : ph == 2 ? check[i] : iv[i] ;
        Serial.print (val&gt;&gt;4, HEX) ; Serial.print (val&amp;15, HEX) ; Serial.print (" ") ;
      }
      Serial.println () ;
    }  
  
  }
  
  char * hex = "0123456789abcdef" ;
  void print_value (char * str, byte * a, int bits)
  {
    Serial.print (str) ;
    bits &gt;&gt;= 3 ; //bits goes from decimal 128 to decimal 16
    for (int i = 0 ; i &lt; bits ; i++)
      {
        // of ex. 0xb9 prints b then 9
        byte b = a[i] ;
        Serial.print (hex [b &gt;&gt; 4]) ;
        Serial.print (hex [b &amp; 15]) ;
      }
    Serial.println () ;
  }
  </code></pre>
  
  <p>My Android program is almost verbatim from the link above. </p>
  
  <p>I can get both of them to encrypt and decrypt their own messages, but their set ups seem very different and haven't been able to encrypt each others. The Android code seems to have quite a bit more involved like creating keys and salts. Both libraries are pretty versatile and I'm not sure how to make them encrypt the same way. </p>
  
  <p>I have a few questions that hopefully will help me get this solved:</p>
  
  <ol>
  <li>How do the "blocks" in the Arduino code relate to the Android
  implementation?</li>
  <li>From what I've read, to decrypt the cipher text on the Arduino from
  Android I will need to send, the cipher text, the IV, and the PBE
  iteration count. Is that correct and everything?</li>
  <li>Android code takes my key and uses a bunch of SecretKeys functions
  on it to I think randomize it and make it more secure. If I'm having
  to store the key on the Arduino, do I need to even bother with this?</li>
  <li><p>Where is the PBE iteration count in the Arduino code? I don't really
  see anything in the library. Do I need to implement that myself? I
  saw in the "test_vectors" example of the library a section that had
  the following bit of code. Is this the iteration?</p>
  
  <pre><code>for (int j = 0 ; j &lt; 1000 ; j++)
  {
      succ = aes.encrypt (plain, cipher) ;
      aes.copy_n_bytes (plain, cipher, 16) ;
  }
  </code></pre></li>
  </ol>