• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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.assertEquals;
21 import static org.junit.Assert.assertThrows;
22 
23 import com.google.crypto.tink.subtle.Enums.HashType;
24 import java.math.BigInteger;
25 import java.nio.ByteBuffer;
26 import java.security.GeneralSecurityException;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.JUnit4;
30 
31 /** Unit tests for SubtleUtil. */
32 @RunWith(JUnit4.class)
33 public final class SubtleUtilTest {
34   @Test
testToEcdsaAlgo()35   public void testToEcdsaAlgo() throws Exception {
36     assertEquals("SHA256withECDSA", SubtleUtil.toEcdsaAlgo(HashType.SHA256));
37     assertEquals("SHA512withECDSA", SubtleUtil.toEcdsaAlgo(HashType.SHA512));
38     assertThrows(GeneralSecurityException.class, () -> SubtleUtil.toEcdsaAlgo(HashType.SHA1));
39   }
40 
41   @Test
testToRsaSsaPkcs1Algo()42   public void testToRsaSsaPkcs1Algo() throws Exception {
43     assertEquals("SHA256withRSA", SubtleUtil.toRsaSsaPkcs1Algo(HashType.SHA256));
44     assertEquals("SHA512withRSA", SubtleUtil.toRsaSsaPkcs1Algo(HashType.SHA512));
45     assertThrows(GeneralSecurityException.class, () -> SubtleUtil.toRsaSsaPkcs1Algo(HashType.SHA1));
46   }
47 
48   @Test
testPutAsUnsigedInt_smallNumber()49   public void testPutAsUnsigedInt_smallNumber() throws Exception {
50     ByteBuffer buffer = ByteBuffer.allocate(4);
51     SubtleUtil.putAsUnsigedInt(buffer, 0x1122EEFFL);
52     assertThat(buffer.array()).isEqualTo(new byte[] {0x11, 0x22, (byte) 0xEE, (byte) 0xFF});
53   }
54 
55   @Test
testPutAsUnsigedInt_largeNumber()56   public void testPutAsUnsigedInt_largeNumber() throws Exception {
57     ByteBuffer buffer = ByteBuffer.allocate(4);
58     SubtleUtil.putAsUnsigedInt(buffer, 0xFFEEDDCCL);
59     assertThat(buffer.array())
60         .isEqualTo(new byte[] {(byte) 0xFF, (byte) 0xEE, (byte) 0xDD, (byte) 0xCC});
61   }
62 
63   @Test
testPutAsUnsigedInt_max()64   public void testPutAsUnsigedInt_max() throws Exception {
65     ByteBuffer buffer = ByteBuffer.allocate(4);
66     SubtleUtil.putAsUnsigedInt(buffer, 0xFFFFFFFFL);
67     assertThat(buffer.array())
68         .isEqualTo(new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF});
69   }
70 
71   @Test
testPutAsUnsigedInt_tooLargeNumber_throws()72   public void testPutAsUnsigedInt_tooLargeNumber_throws() throws Exception {
73     ByteBuffer buffer = ByteBuffer.allocate(4);
74     assertThrows(
75         GeneralSecurityException.class, () -> SubtleUtil.putAsUnsigedInt(buffer, 0xFFFFFFFFL + 1L));
76   }
77 
78   @Test
testPutAsUnsigedInt_minusOne_throws()79   public void testPutAsUnsigedInt_minusOne_throws() throws Exception {
80     ByteBuffer buffer = ByteBuffer.allocate(4);
81     assertThrows(GeneralSecurityException.class, () -> SubtleUtil.putAsUnsigedInt(buffer, -1));
82   }
83 
84   @Test
bytes2Integer()85   public void bytes2Integer() throws Exception {
86     assertThat(SubtleUtil.bytes2Integer(new byte[] {(byte) 0x00}))
87         .isEqualTo(BigInteger.ZERO);
88     assertThat(SubtleUtil.bytes2Integer(new byte[] {(byte) 0x01}))
89         .isEqualTo(BigInteger.ONE);
90     assertThat(SubtleUtil.bytes2Integer(new byte[] {(byte) 0x7F}))
91         .isEqualTo(BigInteger.valueOf(127));
92     // The input should be interpreted as an unsigned integers. So 0x80 is 128.
93     assertThat(SubtleUtil.bytes2Integer(new byte[] {(byte) 0x80}))
94         .isEqualTo(BigInteger.valueOf(128));
95     assertThat(SubtleUtil.bytes2Integer(new byte[] {(byte) 0xFF}))
96         .isEqualTo(BigInteger.valueOf(255));
97     assertThat(SubtleUtil.bytes2Integer(new byte[] {(byte) 0x01, (byte) 0x00}))
98         .isEqualTo(BigInteger.valueOf(256));
99     assertThat(SubtleUtil.bytes2Integer(new byte[] {(byte) 0x01, (byte) 0x02}))
100         .isEqualTo(BigInteger.valueOf(258));
101     // leading zeros are ignored
102     assertThat(SubtleUtil.bytes2Integer(
103                    new byte[] {(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x02}))
104         .isEqualTo(BigInteger.valueOf(258));
105     // the empty array is decoded to 0.
106     assertThat(
107         SubtleUtil.bytes2Integer(new byte[] {})).isEqualTo(BigInteger.ZERO);
108   }
109 
110   @Test
integer2Bytes_success()111   public void integer2Bytes_success() throws Exception {
112     assertThat(SubtleUtil.integer2Bytes(BigInteger.ZERO, /*intendedLength=*/ 0))
113         .isEqualTo(new byte[] {});
114     assertThat(SubtleUtil.integer2Bytes(BigInteger.ZERO, /*intendedLength=*/ 1))
115         .isEqualTo(new byte[] {(byte) 0x00});
116     assertThat(SubtleUtil.integer2Bytes(BigInteger.ZERO, /*intendedLength=*/ 2))
117         .isEqualTo(new byte[] {(byte) 0x00, (byte) 0x00});
118     assertThat(SubtleUtil.integer2Bytes(BigInteger.ONE, /*intendedLength=*/ 1))
119         .isEqualTo(new byte[] {(byte) 0x01});
120     assertThat(SubtleUtil.integer2Bytes(BigInteger.ONE, /*intendedLength=*/ 2))
121         .isEqualTo(new byte[] {(byte) 0x00, (byte) 0x01});
122     assertThat(SubtleUtil.integer2Bytes(BigInteger.valueOf(127), /*intendedLength=*/ 1))
123         .isEqualTo(new byte[] {(byte) 0x7F});
124     assertThat(SubtleUtil.integer2Bytes(BigInteger.valueOf(127), /*intendedLength=*/ 2))
125         .isEqualTo(new byte[] {(byte) 0x00, (byte) 0x7F});
126     assertThat(SubtleUtil.integer2Bytes(BigInteger.valueOf(127), /*intendedLength=*/ 3))
127         .isEqualTo(new byte[] {(byte) 0x00, (byte) 0x00, (byte) 0x7F});
128     assertThat(SubtleUtil.integer2Bytes(BigInteger.valueOf(128), /*intendedLength=*/ 1))
129         .isEqualTo(new byte[] {(byte) 0x80});
130     assertThat(SubtleUtil.integer2Bytes(BigInteger.valueOf(128), /*intendedLength=*/ 2))
131         .isEqualTo(new byte[] {(byte) 0x00, (byte) 0x80});
132     assertThat(SubtleUtil.integer2Bytes(BigInteger.valueOf(128), /*intendedLength=*/ 3))
133         .isEqualTo(new byte[] {(byte) 0x00, (byte) 0x00, (byte) 0x80});
134     assertThat(SubtleUtil.integer2Bytes(BigInteger.valueOf(255), /*intendedLength=*/ 1))
135         .isEqualTo(new byte[] {(byte) 0xFF});
136     assertThat(SubtleUtil.integer2Bytes(BigInteger.valueOf(255), /*intendedLength=*/ 2))
137         .isEqualTo(new byte[] {(byte) 0x00, (byte) 0xFF});
138     assertThat(SubtleUtil.integer2Bytes(BigInteger.valueOf(255), /*intendedLength=*/ 3))
139         .isEqualTo(new byte[] {(byte) 0x00, (byte) 0x00, (byte) 0xFF});
140     assertThat(SubtleUtil.integer2Bytes(BigInteger.valueOf(256), /*intendedLength=*/ 2))
141         .isEqualTo(new byte[] {(byte) 0x01, (byte) 0x00});
142     assertThat(SubtleUtil.integer2Bytes(BigInteger.valueOf(258), /*intendedLength=*/ 2))
143         .isEqualTo(new byte[] {(byte) 0x01, (byte) 0x02});
144     assertThat(SubtleUtil.integer2Bytes(BigInteger.valueOf(258), /*intendedLength=*/ 4))
145         .isEqualTo(new byte[] {(byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x02});
146   }
147 
148   @Test
integer2Bytes_failWhenIntegerIsNegative()149   public void integer2Bytes_failWhenIntegerIsNegative() throws Exception {
150     assertThrows(
151         IllegalArgumentException.class,
152         () -> SubtleUtil.integer2Bytes(BigInteger.valueOf(-1), /*intendedLength=*/ 2));
153     assertThrows(
154         IllegalArgumentException.class,
155         () -> SubtleUtil.integer2Bytes(BigInteger.valueOf(-42), /*intendedLength=*/ 2));
156   }
157 
158   @Test
integer2Bytes_failWhenIntegerIsLargerThanIntendedLength()159   public void integer2Bytes_failWhenIntegerIsLargerThanIntendedLength() throws Exception {
160     assertThrows(
161         GeneralSecurityException.class,
162         () -> SubtleUtil.integer2Bytes(BigInteger.ONE, /* intendedLength= */ 0));
163     assertThrows(
164         GeneralSecurityException.class,
165         () -> SubtleUtil.integer2Bytes(BigInteger.valueOf(256), /*intendedLength=*/ 1));
166     assertThrows(
167         GeneralSecurityException.class,
168         () -> SubtleUtil.integer2Bytes(BigInteger.valueOf(256 * 256), /*intendedLength=*/ 2));
169   }
170 
171 }
172