Skip to content
This repository was archived by the owner on Apr 17, 2024. It is now read-only.

Commit 0fdbac7

Browse files
juergwcopybara-github
authored andcommitted
Add validation to HpkeUtil.intToByteArray.
- The algorithm implementation used here doesn't work for capacity larger than 4, so we shouldn't allow this. - The algorithm definition, see https://www.rfc-editor.org/rfc/rfc3447.html#section-4.1, requires that 0 <= value < 256^capacity. And add unit tests. PiperOrigin-RevId: 620041132
1 parent 6f0527e commit 0fdbac7

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

java_src/src/main/java/com/google/crypto/tink/hybrid/internal/HpkeUtil.java

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ public final class HpkeUtil {
6464
* @param value that should be represented as a byte array
6565
*/
6666
public static byte[] intToByteArray(int capacity, int value) {
67+
if ((capacity > 4) || (capacity < 0)) {
68+
throw new IllegalArgumentException("capacity must be between 0 and 4");
69+
}
70+
// Check that 0 <= value < 256^capacity.
71+
// For capacity == 4, all positive values are valid.
72+
if (value < 0 || (capacity < 4 && (value >= 1 << (8 * capacity)))) {
73+
throw new IllegalArgumentException("value too large");
74+
}
6775
final byte[] result = new byte[capacity];
6876
for (int i = 0; i < capacity; i++) {
6977
result[i] = (byte) ((value >> (8 * (capacity - i - 1))) & 0xFF);

java_src/src/test/java/com/google/crypto/tink/hybrid/internal/BUILD.bazel

+11
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,14 @@ java_test(
322322
"@maven//:junit_junit",
323323
],
324324
)
325+
326+
java_test(
327+
name = "HpkeUtilTest",
328+
size = "small",
329+
srcs = ["HpkeUtilTest.java"],
330+
deps = [
331+
"//src/main/java/com/google/crypto/tink/hybrid/internal:hpke_util",
332+
"@maven//:com_google_truth_truth",
333+
"@maven//:junit_junit",
334+
],
335+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2024 Google LLC
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+
package com.google.crypto.tink.hybrid.internal;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
import static org.junit.Assert.assertThrows;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.junit.runners.JUnit4;
24+
25+
@RunWith(JUnit4.class)
26+
public final class HpkeUtilTest {
27+
28+
@Test
29+
public void intToByteArray_works() {
30+
assertThat(HpkeUtil.intToByteArray(0, 0)).isEqualTo(new byte[] {});
31+
assertThat(HpkeUtil.intToByteArray(1, 42)).isEqualTo(new byte[] {(byte) 42});
32+
assertThat(HpkeUtil.intToByteArray(2, 0x0102)).isEqualTo(new byte[] {(byte) 01, (byte) 0x02});
33+
34+
assertThat(HpkeUtil.intToByteArray(1, 0xaa)).isEqualTo(new byte[] {(byte) 0xaa});
35+
assertThat(HpkeUtil.intToByteArray(1, 256 - 1)).isEqualTo(new byte[] {(byte) 0xff});
36+
assertThat(HpkeUtil.intToByteArray(2, 0xaabb)).isEqualTo(new byte[] {(byte) 0xaa, (byte) 0xbb});
37+
assertThat(HpkeUtil.intToByteArray(2, 256 * 256 - 1))
38+
.isEqualTo(new byte[] {(byte) 0xff, (byte) 0xff});
39+
assertThat(HpkeUtil.intToByteArray(3, 0xaabbcc))
40+
.isEqualTo(new byte[] {(byte) 0xaa, (byte) 0xbb, (byte) 0xcc});
41+
assertThat(HpkeUtil.intToByteArray(3, 256 * 256 * 256 - 1))
42+
.isEqualTo(new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff});
43+
assertThat(HpkeUtil.intToByteArray(4, 0x0abbccdd))
44+
.isEqualTo(new byte[] {(byte) 0x0a, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd});
45+
assertThat(HpkeUtil.intToByteArray(4, Integer.MAX_VALUE))
46+
.isEqualTo(new byte[] {(byte) 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff});
47+
}
48+
49+
@Test
50+
public void intToByteArray_failsWithInvalidCapacity() {
51+
assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(5, 0));
52+
assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(-1, 0));
53+
}
54+
55+
@Test
56+
public void intToByteArray_valueTooLong_fails() {
57+
assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(0, 1));
58+
assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(0, -1));
59+
assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(1, 256));
60+
assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(1, -1));
61+
assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(2, 256 * 256));
62+
assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(2, -1));
63+
assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(3, 256 * 256 * 256));
64+
assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(3, -1));
65+
assertThrows(IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(4, -1));
66+
assertThrows(
67+
IllegalArgumentException.class, () -> HpkeUtil.intToByteArray(4, Integer.MIN_VALUE));
68+
}
69+
}

0 commit comments

Comments
 (0)