1 /* Any copyright is dedicated to the Public Domain.
2    http://creativecommons.org/publicdomain/zero/1.0/ */
3 
4 package org.mozilla.gecko.sync.crypto.test;
5 
6 import org.junit.Test;
7 import org.junit.runner.RunWith;
8 import org.mozilla.apache.commons.codec.binary.Base64;
9 import org.mozilla.gecko.background.testhelpers.TestRunner;
10 import org.mozilla.gecko.sync.Utils;
11 import org.mozilla.gecko.sync.crypto.HKDF;
12 import org.mozilla.gecko.sync.crypto.KeyBundle;
13 
14 import java.util.Arrays;
15 
16 import static org.junit.Assert.assertTrue;
17 import static org.junit.Assert.fail;
18 
19 /*
20  * This class tests the HKDF.java class.
21  * The tests are the 3 HMAC-based test cases
22  * from the RFC 5869 specification.
23  */
24 @RunWith(TestRunner.class)
25 public class TestHKDF {
26   @Test
testCase1()27   public void testCase1() {
28     String IKM  = "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b";
29     String salt = "000102030405060708090a0b0c";
30     String info = "f0f1f2f3f4f5f6f7f8f9";
31     int L       = 42;
32     String PRK  = "077709362c2e32df0ddc3f0dc47bba63" +
33                   "90b6c73bb50f9c3122ec844ad7c2b3e5";
34     String OKM  = "3cb25f25faacd57a90434f64d0362f2a" +
35                   "2d2d0a90cf1a5a4c5db02d56ecc4c5bf" +
36                   "34007208d5b887185865";
37 
38     assertTrue(doStep1(IKM, salt, PRK));
39     assertTrue(doStep2(PRK, info, L, OKM));
40   }
41 
42   @Test
testCase2()43   public void testCase2() {
44     String IKM  = "000102030405060708090a0b0c0d0e0f" +
45                   "101112131415161718191a1b1c1d1e1f" +
46                   "202122232425262728292a2b2c2d2e2f" +
47                   "303132333435363738393a3b3c3d3e3f" +
48                   "404142434445464748494a4b4c4d4e4f";
49     String salt = "606162636465666768696a6b6c6d6e6f" +
50                   "707172737475767778797a7b7c7d7e7f" +
51                   "808182838485868788898a8b8c8d8e8f" +
52                   "909192939495969798999a9b9c9d9e9f" +
53                   "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf";
54     String info = "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" +
55                   "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf" +
56                   "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf" +
57                   "e0e1e2e3e4e5e6e7e8e9eaebecedeeef" +
58                   "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
59     int L       = 82;
60     String PRK  = "06a6b88c5853361a06104c9ceb35b45c" +
61                   "ef760014904671014a193f40c15fc244";
62     String OKM  = "b11e398dc80327a1c8e7f78c596a4934" +
63                   "4f012eda2d4efad8a050cc4c19afa97c" +
64                   "59045a99cac7827271cb41c65e590e09" +
65                   "da3275600c2f09b8367793a9aca3db71" +
66                   "cc30c58179ec3e87c14c01d5c1f3434f" +
67                   "1d87";
68 
69     assertTrue(doStep1(IKM, salt, PRK));
70     assertTrue(doStep2(PRK, info, L, OKM));
71   }
72 
73   @Test
testCase3()74   public void testCase3() {
75     String IKM  = "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b";
76     String salt = "";
77     String info = "";
78     int L       = 42;
79     String PRK  = "19ef24a32c717b167f33a91d6f648bdf" +
80                   "96596776afdb6377ac434c1c293ccb04";
81     String OKM  = "8da4e775a563c18f715f802a063c5a31" +
82                   "b8a11f5c5ee1879ec3454e5f3c738d2d" +
83                   "9d201395faa4b61a96c8";
84 
85     assertTrue(doStep1(IKM, salt, PRK));
86     assertTrue(doStep2(PRK, info, L, OKM));
87   }
88 
89   /*
90    * Tests the code for getting the keys necessary to
91    * decrypt the crypto keys bundle for Mozilla Sync.
92    *
93    * This operation is just a tailored version of the
94    * standard to get only the 2 keys we need.
95    */
96   @Test
testGetCryptoKeysBundleKeys()97   public void testGetCryptoKeysBundleKeys() {
98     String username              = "smqvooxj664hmrkrv6bw4r4vkegjhkns";
99     String friendlyBase32SyncKey = "gbh7teqqcgyzd65svjgibd7tqy";
100     String base64EncryptionKey   = "069EnS3EtDK4y1tZ1AyKX+U7WEsWRp9bRIKLdW/7aoE=";
101     String base64HmacKey         = "LF2YCS1QCgSNCf0BCQvQ06SGH8jqJDi9dKj0O+b0fwI=";
102 
103     KeyBundle bundle = null;
104     try {
105       bundle = new KeyBundle(username, friendlyBase32SyncKey);
106     } catch (Exception e) {
107       fail("Unexpected exception " + e);
108     }
109 
110     byte[] expectedEncryptionKey = Base64.decodeBase64(base64EncryptionKey);
111     byte[] expectedHMACKey       = Base64.decodeBase64(base64HmacKey);
112     assertTrue(Arrays.equals(bundle.getEncryptionKey(), expectedEncryptionKey));
113     assertTrue(Arrays.equals(bundle.getHMACKey(), expectedHMACKey));
114   }
115 
116   /*
117    * Helper to do step 1 of RFC 5869.
118    */
doStep1(String IKM, String salt, String PRK)119   private boolean doStep1(String IKM, String salt, String PRK) {
120     try {
121       byte[] prkResult = HKDF.hkdfExtract(Utils.hex2Byte(salt), Utils.hex2Byte(IKM));
122       byte[] prkExpect = Utils.hex2Byte(PRK);
123       return Arrays.equals(prkResult, prkExpect);
124     } catch (Exception e) {
125       fail("Unexpected exception " + e);
126     }
127     return false;
128   }
129 
130   /*
131    * Helper to do step 2 of RFC 5869.
132    */
doStep2(String PRK, String info, int L, String OKM)133   private boolean doStep2(String PRK, String info, int L, String OKM) {
134     try {
135       byte[] okmResult = HKDF.hkdfExpand(Utils.hex2Byte(PRK), Utils.hex2Byte(info), L);
136       byte[] okmExpect = Utils.hex2Byte(OKM);
137       return Arrays.equals(okmResult, okmExpect);
138     } catch (Exception e) {
139       fail("Unexpected exception " + e);
140     }
141     return false;
142   }
143 }
144