aboutsummaryrefslogtreecommitdiff
path: root/java_src/src/test/java/com/google/crypto/tink/hybrid/internal/AesGcmHpkeAeadTest.java
blob: 8438aef0165c166425769bd9d1b6c7f05f178c4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////

package com.google.crypto.tink.hybrid.internal;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;

import com.google.common.io.Files;
import com.google.common.truth.Expect;
import com.google.crypto.tink.testing.HpkeTestEncryption;
import com.google.crypto.tink.testing.HpkeTestId;
import com.google.crypto.tink.testing.HpkeTestSetup;
import com.google.crypto.tink.testing.HpkeTestUtil;
import com.google.crypto.tink.testing.HpkeTestVector;
import com.google.crypto.tink.testing.TestUtil;
import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.util.List;
import java.util.Map;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link AesGcmHpkeAead}. */
@RunWith(JUnit4.class)
public final class AesGcmHpkeAeadTest {
  private static Map<HpkeTestId, HpkeTestVector> testVectors;

  @Rule public final Expect expect = Expect.create();

  @BeforeClass
  public static void setUpTestVectors() throws IOException {
    String path = "testdata/testvectors/hpke_boringssl.json";
    if (TestUtil.isAndroid()) {
      path = "/sdcard/googletest/test_runfiles/google3/" + path;  // Special prefix for Android.
    }
    testVectors = HpkeTestUtil.parseTestVectors(Files.newReader(new File(path), UTF_8));
  }

  private HpkeTestVector getTestVector(byte[] mode, byte[] kemId, byte[] kdfId, byte[] aeadId) {
    HpkeTestId testId = new HpkeTestId(mode, kemId, kdfId, aeadId);
    return testVectors.get(testId);
  }

  private void testSealAndOpen(
      AesGcmHpkeAead aead, byte[] mode, byte[] kemId, byte[] kdfId, byte[] aeadId)
      throws GeneralSecurityException {
    HpkeTestVector testVector = getTestVector(mode, kemId, kdfId, aeadId);
    HpkeTestSetup testSetup = testVector.getTestSetup();
    List<HpkeTestEncryption> encryptions = testVector.getEncryptions();
    for (HpkeTestEncryption encryption : encryptions) {
      byte[] ciphertext =
          aead.seal(
              testSetup.key, encryption.nonce, encryption.plaintext, encryption.associatedData);
      byte[] plaintext =
          aead.open(
              testSetup.key, encryption.nonce, encryption.ciphertext, encryption.associatedData);
      expect.that(ciphertext).isEqualTo(encryption.ciphertext);
      expect.that(plaintext).isEqualTo(encryption.plaintext);
    }
  }

  @Test
  public void sealAndOpen_aes128Gcm() throws GeneralSecurityException {
    AesGcmHpkeAead aead = new AesGcmHpkeAead(16);
    testSealAndOpen(
        aead,
        HpkeUtil.BASE_MODE,
        HpkeUtil.X25519_HKDF_SHA256_KEM_ID,
        HpkeUtil.HKDF_SHA256_KDF_ID,
        HpkeUtil.AES_128_GCM_AEAD_ID);
  }

  @Test
  public void sealAndOpen_aes256Gcm() throws GeneralSecurityException {
    AesGcmHpkeAead aead = new AesGcmHpkeAead(32);
    testSealAndOpen(
        aead,
        HpkeUtil.BASE_MODE,
        HpkeUtil.X25519_HKDF_SHA256_KEM_ID,
        HpkeUtil.HKDF_SHA256_KDF_ID,
        HpkeUtil.AES_256_GCM_AEAD_ID);
  }

  @Test
  public void create_invalidKeyLength() {
    assertThrows(InvalidAlgorithmParameterException.class, () -> new AesGcmHpkeAead(24));
  }

  @Test
  public void seal_wrongKeyLength() throws GeneralSecurityException {
    AesGcmHpkeAead aead = new AesGcmHpkeAead(32);
    HpkeTestVector testVector =
        getTestVector(
            HpkeUtil.BASE_MODE,
            HpkeUtil.X25519_HKDF_SHA256_KEM_ID,
            HpkeUtil.HKDF_SHA256_KDF_ID,
            HpkeUtil.AES_128_GCM_AEAD_ID); // 16-byte keys incompatible with above 'aead'.
    HpkeTestSetup setup = testVector.getTestSetup();
    HpkeTestEncryption encryption = testVector.getEncryptions().get(0);
    assertThrows(
        InvalidAlgorithmParameterException.class,
        () ->
            aead.seal(
                setup.key, encryption.nonce, encryption.plaintext, encryption.associatedData));
  }

  @Test
  public void open_wrongKeyLength() throws GeneralSecurityException {
    AesGcmHpkeAead aead = new AesGcmHpkeAead(16);
    HpkeTestVector testVector =
        getTestVector(
            HpkeUtil.BASE_MODE,
            HpkeUtil.X25519_HKDF_SHA256_KEM_ID,
            HpkeUtil.HKDF_SHA256_KDF_ID,
            HpkeUtil.AES_256_GCM_AEAD_ID); // 32-byte keys incompatible with above 'aead'.
    HpkeTestSetup setup = testVector.getTestSetup();
    HpkeTestEncryption encryption = testVector.getEncryptions().get(0);
    assertThrows(
        InvalidAlgorithmParameterException.class,
        () ->
            aead.open(
                setup.key, encryption.nonce, encryption.ciphertext, encryption.associatedData));
  }
}