1 // Copyright 2017 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 //////////////////////////////////////////////////////////////////////////////// 16 17 package com.google.crypto.tink.subtle; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.assertArrayEquals; 21 import static org.junit.Assert.assertThrows; 22 23 import com.google.common.truth.Truth; 24 import java.security.GeneralSecurityException; 25 import java.security.InvalidKeyException; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 30 /** Unit tests for {@link ChaCha20} */ 31 @RunWith(JUnit4.class) 32 public class ChaCha20Test { createInstance(final byte[] key)33 public IndCpaCipher createInstance(final byte[] key) throws InvalidKeyException { 34 return new ChaCha20(key, 0 /* initialCounter */); 35 } 36 37 @Test testEncryptDecrypt()38 public void testEncryptDecrypt() throws Exception { 39 for (int i = 0; i < 64; i++) { 40 byte[] key = Random.randBytes(32); 41 IndCpaCipher cipher = createInstance(key); 42 for (int j = 0; j < 64; j++) { 43 byte[] expectedInput = Random.randBytes(new java.util.Random().nextInt(300)); 44 byte[] output = cipher.encrypt(expectedInput); 45 byte[] actualInput = cipher.decrypt(output); 46 assertArrayEquals( 47 String.format( 48 "\n\nMessage: %s\nKey: %s\nOutput: %s\nDecrypted Msg: %s\n", 49 Hex.encode(expectedInput), 50 Hex.encode(key), 51 Hex.encode(output), 52 Hex.encode(actualInput)), 53 expectedInput, 54 actualInput); 55 } 56 } 57 } 58 59 @Test testNewCipherThrowsIllegalArgExpWhenKeyLenIsLessThan32()60 public void testNewCipherThrowsIllegalArgExpWhenKeyLenIsLessThan32() throws Exception { 61 InvalidKeyException e = 62 assertThrows(InvalidKeyException.class, () -> createInstance(new byte[1])); 63 assertThat(e).hasMessageThat().containsMatch("The key length in bytes must be 32."); 64 } 65 66 @Test testNewCipherThrowsIllegalArgExpWhenKeyLenIsGreaterThan32()67 public void testNewCipherThrowsIllegalArgExpWhenKeyLenIsGreaterThan32() throws Exception { 68 InvalidKeyException e = 69 assertThrows(InvalidKeyException.class, () -> createInstance(new byte[33])); 70 assertThat(e).hasMessageThat().containsMatch("The key length in bytes must be 32."); 71 } 72 73 @Test testDecryptThrowsGeneralSecurityExpWhenCiphertextIsTooShort()74 public void testDecryptThrowsGeneralSecurityExpWhenCiphertextIsTooShort() throws Exception { 75 IndCpaCipher cipher = createInstance(Random.randBytes(32)); 76 GeneralSecurityException e = 77 assertThrows(GeneralSecurityException.class, () -> cipher.decrypt(new byte[2])); 78 assertThat(e).hasMessageThat().containsMatch("ciphertext too short"); 79 } 80 81 /** https://tools.ietf.org/html/rfc7539#section-2.4.2 */ 82 private static class Rfc7539TestVector { 83 public byte[] key; 84 public byte[] plaintext; 85 public byte[] nonce; 86 public byte[] ciphertext; 87 int initialCounter; 88 Rfc7539TestVector( String key, String plaintext, String nonce, String ciphertext, int initialCounter)89 public Rfc7539TestVector( 90 String key, String plaintext, String nonce, String ciphertext, int initialCounter) { 91 this.key = Hex.decode(key); 92 this.plaintext = Hex.decode(plaintext); 93 this.nonce = Hex.decode(nonce); 94 this.ciphertext = Hex.decode(ciphertext); 95 this.initialCounter = initialCounter; 96 } 97 } 98 99 final Rfc7539TestVector[] rfc7539TestVectors = { 100 new Rfc7539TestVector( 101 "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", 102 "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f66202739393a20496620" 103 + "4920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f722074686520667574" 104 + "7572652c2073756e73637265656e20776f756c642062652069742e", 105 "000000000000004a00000000", 106 "6e2e359a2568f98041ba0728dd0d6981e97e7aec1d4360c20a27afccfd9fae0bf91b65c5524733ab8f593dabcd" 107 + "62b3571639d624e65152ab8f530c359f0861d807ca0dbf500d6a6156a38e088a22b65e52bc514d16ccf8" 108 + "06818ce91ab77937365af90bbf74a35be6b40b8eedf2785e42874d", 109 1), 110 new Rfc7539TestVector( 111 "0000000000000000000000000000000000000000000000000000000000000000", 112 "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" 113 + "00000000000000000000000000000000000000", 114 "000000000000000000000000", 115 "76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8" 116 + "d84a376a43b8f41518a11cc387b669b2ee6586", 117 0), 118 new Rfc7539TestVector( 119 "0000000000000000000000000000000000000000000000000000000000000001", 120 "416e79207375626d697373696f6e20746f20746865204945544620696e74656e6465642062792074686520436f" 121 + "6e7472696275746f7220666f72207075626c69636174696f6e20617320616c6c206f722070617274206f" 122 + "6620616e204945544620496e7465726e65742d4472616674206f722052464320616e6420616e79207374" 123 + "6174656d656e74206d6164652077697468696e2074686520636f6e74657874206f6620616e2049455446" 124 + "20616374697669747920697320636f6e7369646572656420616e20224945544620436f6e747269627574" 125 + "696f6e222e20537563682073746174656d656e747320696e636c756465206f72616c2073746174656d65" 126 + "6e747320696e20494554462073657373696f6e732c2061732077656c6c206173207772697474656e2061" 127 + "6e6420656c656374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e792074" 128 + "696d65206f7220706c6163652c207768696368206172652061646472657373656420746f", 129 "000000000000000000000002", 130 "a3fbf07df3fa2fde4f376ca23e82737041605d9f4f4f57bd8cff2c1d4b7955ec2a97948bd3722915c8f3d337f7" 131 + "d370050e9e96d647b7c39f56e031ca5eb6250d4042e02785ececfa4b4bb5e8ead0440e20b6e8db09d881" 132 + "a7c6132f420e52795042bdfa7773d8a9051447b3291ce1411c680465552aa6c405b7764d5e87bea85ad0" 133 + "0f8449ed8f72d0d662ab052691ca66424bc86d2df80ea41f43abf937d3259dc4b2d0dfb48a6c9139ddd7" 134 + "f76966e928e635553ba76c5c879d7b35d49eb2e62b0871cdac638939e25e8a1e0ef9d5280fa8ca328b35" 135 + "1c3c765989cbcf3daa8b6ccc3aaf9f3979c92b3720fc88dc95ed84a1be059c6499b9fda236e7e818b04b" 136 + "0bc39c1e876b193bfe5569753f88128cc08aaa9b63d1a16f80ef2554d7189c411f5869ca52c5b83fa36f" 137 + "f216b9c1d30062bebcfd2dc5bce0911934fda79a86f6e698ced759c3ff9b6477338f3da4f9cd8514ea99" 138 + "82ccafb341b2384dd902f3d1ab7ac61dd29c6f21ba5b862f3730e37cfdc4fd806c22f221", 139 1), 140 new Rfc7539TestVector( 141 "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0", 142 "2754776173206272696c6c69672c20616e642074686520736c6974687920746f7665730a446964206779726520" 143 + "616e642067696d626c6520696e2074686520776162653a0a416c6c206d696d73792077657265207468652062" 144 + "6f726f676f7665732c0a416e6420746865206d6f6d65207261746873206f757467726162652e", 145 "000000000000000000000002", 146 "62e6347f95ed87a45ffae7426f27a1df5fb69110044c0d73118effa95b01e5cf166d3df2d721caf9b21e5fb14c" 147 + "616871fd84c54f9d65b283196c7fe4f60553ebf39c6402c42234e32a356b3e764312a61a5532055716ea" 148 + "d6962568f87d3f3f7704c6a8d1bcd1bf4d50d6154b6da731b187b58dfd728afa36757a797ac188d1", 149 42), 150 // Tests against the test vectors in Section 2.6.2 of RFC 7539. 151 // https://tools.ietf.org/html/rfc7539#section-2.6.2 152 new Rfc7539TestVector( 153 "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f", 154 "0000000000000000000000000000000000000000000000000000000000000000", 155 "000000000001020304050607", 156 "8ad5a08b905f81cc815040274ab29471a833b637e3fd0da508dbb8e2fdd1a646", 157 0), 158 new Rfc7539TestVector( 159 "0000000000000000000000000000000000000000000000000000000000000000", 160 "0000000000000000000000000000000000000000000000000000000000000000", 161 "000000000000000000000000", 162 "76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7", 163 0), 164 new Rfc7539TestVector( 165 "0000000000000000000000000000000000000000000000000000000000000001", 166 "0000000000000000000000000000000000000000000000000000000000000000", 167 "000000000000000000000002", 168 "ecfa254f845f647473d3cb140da9e87606cb33066c447b87bc2666dde3fbb739", 169 0), 170 new Rfc7539TestVector( 171 "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0", 172 "0000000000000000000000000000000000000000000000000000000000000000", 173 "000000000000000000000002", 174 "965e3bc6f9ec7ed9560808f4d229f94b137ff275ca9b3fcbdd59deaad23310ae", 175 0), 176 }; 177 178 @Test testWithRfc7539TestVectors()179 public void testWithRfc7539TestVectors() throws Exception { 180 for (Rfc7539TestVector test : rfc7539TestVectors) { 181 IndCpaCipher cipher = new ChaCha20(test.key, test.initialCounter); 182 byte[] out = cipher.decrypt(Bytes.concat(test.nonce, test.ciphertext)); 183 Truth.assertThat(out).isEqualTo(test.plaintext); 184 } 185 } 186 } 187