• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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 
17 package com.google.crypto.tink.testing;
18 
19 import com.google.crypto.tink.subtle.Hex;
20 import com.google.errorprone.annotations.CanIgnoreReturnValue;
21 import javax.annotation.Nullable;
22 
23 /** Helper class that contains setup parameter values for an individual test vector. */
24 public final class HpkeTestSetup {
25   public final byte[] info; // info
26   public final byte[] senderEphemeralPublicKey; // pkEm
27   public final byte[] senderEphemeralPrivateKey; // skEm
28   @Nullable public final byte[] senderPublicKey; // pkSm
29   @Nullable public final byte[] senderPrivateKey; // skSm
30   public final byte[] recipientPublicKey; // pkRm
31   public final byte[] recipientPrivateKey; // skRm
32   public final byte[] encapsulatedKey; // enc
33   public final byte[] sharedSecret; // shared_secret
34   public final byte[] keyScheduleContext; // key_schedule_context
35   public final byte[] secret; // secret
36   public final byte[] key; // key
37   public final byte[] baseNonce; // base_nonce
38 
39   /** Builder class for {@link HpkeTestSetup}. */
40   public static final class Builder {
41     private String info; // info
42     private String senderEphemeralPublicKey; // pkEm
43     private String senderEphemeralPrivateKey; // skEm
44     private String senderPublicKey; // pkSm
45     private String senderPrivateKey; // skSm
46     private String recipientPublicKey; // pkRm
47     private String recipientPrivateKey; // skRm
48     private String encapsulatedKey; // enc
49     private String sharedSecret; // shared_secret
50     private String keyScheduleContext; // key_schedule_context
51     private String secret; // secret
52     private String key; // key
53     private String baseNonce; // base_nonce
54 
Builder()55     private Builder() {}
56 
57     @CanIgnoreReturnValue
setInfo(String info)58     public Builder setInfo(String info) {
59       this.info = info;
60       return this;
61     }
62 
63     @CanIgnoreReturnValue
setSenderPublicKey(String senderPublicKey)64     public Builder setSenderPublicKey(String senderPublicKey) {
65       this.senderPublicKey = senderPublicKey;
66       return this;
67     }
68 
69     @CanIgnoreReturnValue
setSenderPrivateKey(String senderPrivateKey)70     public Builder setSenderPrivateKey(String senderPrivateKey) {
71       this.senderPrivateKey = senderPrivateKey;
72       return this;
73     }
74 
75     @CanIgnoreReturnValue
setSenderEphemeralPublicKey(String senderEphemeralPublicKey)76     public Builder setSenderEphemeralPublicKey(String senderEphemeralPublicKey) {
77       this.senderEphemeralPublicKey = senderEphemeralPublicKey;
78       return this;
79     }
80 
81     @CanIgnoreReturnValue
setSenderEphemeralPrivateKey(String senderEphemeralPrivateKey)82     public Builder setSenderEphemeralPrivateKey(String senderEphemeralPrivateKey) {
83       this.senderEphemeralPrivateKey = senderEphemeralPrivateKey;
84       return this;
85     }
86 
87     @CanIgnoreReturnValue
setRecipientPublicKey(String recipientPublicKey)88     public Builder setRecipientPublicKey(String recipientPublicKey) {
89       this.recipientPublicKey = recipientPublicKey;
90       return this;
91     }
92 
93     @CanIgnoreReturnValue
setRecipientPrivateKey(String recipientPrivateKey)94     public Builder setRecipientPrivateKey(String recipientPrivateKey) {
95       this.recipientPrivateKey = recipientPrivateKey;
96       return this;
97     }
98 
99     @CanIgnoreReturnValue
setEncapsulatedKey(String encapsulatedKey)100     public Builder setEncapsulatedKey(String encapsulatedKey) {
101       this.encapsulatedKey = encapsulatedKey;
102       return this;
103     }
104 
105     @CanIgnoreReturnValue
setSharedSecret(String sharedSecret)106     public Builder setSharedSecret(String sharedSecret) {
107       this.sharedSecret = sharedSecret;
108       return this;
109     }
110 
111     @CanIgnoreReturnValue
setKeyScheduleContext(String keyScheduleContext)112     public Builder setKeyScheduleContext(String keyScheduleContext) {
113       this.keyScheduleContext = keyScheduleContext;
114       return this;
115     }
116 
117     @CanIgnoreReturnValue
setSecret(String secret)118     public Builder setSecret(String secret) {
119       this.secret = secret;
120       return this;
121     }
122 
123     @CanIgnoreReturnValue
setKey(String key)124     public Builder setKey(String key) {
125       this.key = key;
126       return this;
127     }
128 
129     @CanIgnoreReturnValue
setBaseNonce(String baseNonce)130     public Builder setBaseNonce(String baseNonce) {
131       this.baseNonce = baseNonce;
132       return this;
133     }
134 
build()135     public HpkeTestSetup build() {
136       if (info == null) {
137         throw new IllegalArgumentException("Info must be non-null.");
138       }
139       if (senderEphemeralPublicKey == null) {
140         throw new IllegalArgumentException("Sender ephemeral public key must be non-null.");
141       }
142       if (senderEphemeralPrivateKey == null) {
143         throw new IllegalArgumentException("Sender ephemeral private key must be non-null.");
144       }
145       if (recipientPublicKey == null) {
146         throw new IllegalArgumentException("Recipient public key must be non-null.");
147       }
148       if (recipientPrivateKey == null) {
149         throw new IllegalArgumentException("Recipient private key must be non-null.");
150       }
151       if (encapsulatedKey == null) {
152         throw new IllegalArgumentException("Encapsulated key must be non-null.");
153       }
154       if (sharedSecret == null) {
155         throw new IllegalArgumentException("Shared secret must be non-null.");
156       }
157       if (keyScheduleContext == null) {
158         throw new IllegalArgumentException("Key schedule context must be non-null.");
159       }
160       if (secret == null) {
161         throw new IllegalArgumentException("Secret must be non-null.");
162       }
163       if (key == null) {
164         throw new IllegalArgumentException("Key must be non-null.");
165       }
166       if (baseNonce == null) {
167         throw new IllegalArgumentException("Base nonce must be non-null.");
168       }
169 
170       return new HpkeTestSetup(
171           info,
172           senderEphemeralPublicKey,
173           senderEphemeralPrivateKey,
174           senderPublicKey,
175           senderPrivateKey,
176           recipientPublicKey,
177           recipientPrivateKey,
178           encapsulatedKey,
179           sharedSecret,
180           keyScheduleContext,
181           secret,
182           key,
183           baseNonce);
184     }
185   }
186 
HpkeTestSetup( String info, String senderEphemeralPublicKey, String senderEphemeralPrivateKey, String senderPublicKey, String senderPrivateKey, String recipientPublicKey, String recipientPrivateKey, String encapsulatedKey, String sharedSecret, String keyScheduleContext, String secret, String key, String baseNonce)187   private HpkeTestSetup(
188       String info,
189       String senderEphemeralPublicKey,
190       String senderEphemeralPrivateKey,
191       String senderPublicKey,
192       String senderPrivateKey,
193       String recipientPublicKey,
194       String recipientPrivateKey,
195       String encapsulatedKey,
196       String sharedSecret,
197       String keyScheduleContext,
198       String secret,
199       String key,
200       String baseNonce) {
201     this.info = Hex.decode(info);
202     this.senderEphemeralPublicKey = Hex.decode(senderEphemeralPublicKey);
203     this.senderEphemeralPrivateKey = Hex.decode(senderEphemeralPrivateKey);
204     // senderPublicKey is optional and might not be set.
205     if (senderPublicKey != null) {
206       this.senderPublicKey = Hex.decode(senderPublicKey);
207     } else {
208       this.senderPublicKey = null;
209     }
210     // senderPrivateKey is optional and might not be set.
211     if (senderPrivateKey != null) {
212       this.senderPrivateKey = Hex.decode(senderPrivateKey);
213     } else {
214       this.senderPrivateKey = null;
215     }
216     this.recipientPublicKey = Hex.decode(recipientPublicKey);
217     this.recipientPrivateKey = Hex.decode(recipientPrivateKey);
218     this.encapsulatedKey = Hex.decode(encapsulatedKey);
219     this.sharedSecret = Hex.decode(sharedSecret);
220     this.keyScheduleContext = Hex.decode(keyScheduleContext);
221     this.secret = Hex.decode(secret);
222     this.key = Hex.decode(key);
223     this.baseNonce = Hex.decode(baseNonce);
224   }
225 
builder()226   public static Builder builder() {
227     return new Builder();
228   }
229 
230   @Override
toString()231   public String toString() {
232     String s = "";
233     s += "info: " + Hex.encode(info) + "\n";
234     s += "pkEm: " + Hex.encode(senderPublicKey) + "\n";
235     s += "skEm: " + Hex.encode(senderPrivateKey) + "\n";
236     if (senderPublicKey != null) {
237       s += "pkSm: " + Hex.encode(senderPublicKey) + "\n";
238     }
239     if (senderPrivateKey != null) {
240       s += "skSm: " + Hex.encode(senderPrivateKey) + "\n";
241     }
242     s += "pkRm: " + Hex.encode(recipientPublicKey) + "\n";
243     s += "skRm: " + Hex.encode(recipientPrivateKey) + "\n";
244     s += "enc: " + Hex.encode(encapsulatedKey) + "\n";
245     s += "shared_secret: " + Hex.encode(sharedSecret) + "\n";
246     s += "key_schedule_context: " + Hex.encode(keyScheduleContext) + "\n";
247     s += "secret: " + Hex.encode(secret) + "\n";
248     s += "key: " + Hex.encode(key) + "\n";
249     s += "base_nonce: " + Hex.encode(baseNonce);
250     return s;
251   }
252 }
253