aboutsummaryrefslogtreecommitdiff
path: root/java_src/src/main/java/com/google/crypto/tink/hybrid/internal/HpkePrivateKeyManager.java
blob: 77cb832efaedab617b70ea62bb1041745a2b8ad1 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// 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 com.google.crypto.tink.HybridDecrypt;
import com.google.crypto.tink.KeyTemplate;
import com.google.crypto.tink.Registry;
import com.google.crypto.tink.internal.KeyTypeManager;
import com.google.crypto.tink.internal.PrimitiveFactory;
import com.google.crypto.tink.internal.PrivateKeyTypeManager;
import com.google.crypto.tink.proto.HpkeAead;
import com.google.crypto.tink.proto.HpkeKdf;
import com.google.crypto.tink.proto.HpkeKem;
import com.google.crypto.tink.proto.HpkeKeyFormat;
import com.google.crypto.tink.proto.HpkeParams;
import com.google.crypto.tink.proto.HpkePrivateKey;
import com.google.crypto.tink.proto.HpkePublicKey;
import com.google.crypto.tink.proto.KeyData.KeyMaterialType;
import com.google.crypto.tink.subtle.EllipticCurves;
import com.google.crypto.tink.subtle.Validators;
import com.google.crypto.tink.subtle.X25519;
import com.google.protobuf.ByteString;
import com.google.protobuf.ExtensionRegistryLite;
import com.google.protobuf.InvalidProtocolBufferException;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Key manager that generates new {@link HpkePrivateKey} keys and produces new instances of {@link
 * HpkeDecrypt} primitives.
 */
public final class HpkePrivateKeyManager
    extends PrivateKeyTypeManager<HpkePrivateKey, HpkePublicKey> {
  public HpkePrivateKeyManager() {
    super(
        HpkePrivateKey.class,
        HpkePublicKey.class,
        new PrimitiveFactory<HybridDecrypt, HpkePrivateKey>(HybridDecrypt.class) {
          @Override
          public HybridDecrypt getPrimitive(HpkePrivateKey recipientPrivateKey)
              throws GeneralSecurityException {
            return HpkeDecrypt.createHpkeDecrypt(recipientPrivateKey);
          }
        });
  }

  /**
   * Registers an {@link HpkePrivateKeyManager} and an {@link HpkePublicKeyManager} with the
   * registry, so that HpkePrivateKey and HpkePublicKey key types can be used with Tink.
   */
  public static void registerPair(boolean newKeyAllowed) throws GeneralSecurityException {
    Registry.registerAsymmetricKeyManagers(
        new HpkePrivateKeyManager(), new HpkePublicKeyManager(), newKeyAllowed);
  }

  @Override
  public String getKeyType() {
    return "type.googleapis.com/google.crypto.tink.HpkePrivateKey";
  }

  @Override
  public int getVersion() {
    return 0;
  }

  @Override
  public HpkePublicKey getPublicKey(HpkePrivateKey key) {
    return key.getPublicKey();
  }

  @Override
  public KeyMaterialType keyMaterialType() {
    return KeyMaterialType.ASYMMETRIC_PRIVATE;
  }

  @Override
  public HpkePrivateKey parseKey(ByteString byteString) throws InvalidProtocolBufferException {
    return HpkePrivateKey.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry());
  }

  @Override
  public void validateKey(HpkePrivateKey key) throws GeneralSecurityException {
    if (key.getPrivateKey().isEmpty()) {
      throw new GeneralSecurityException("Private key is empty.");
    }
    if (!key.hasPublicKey()) {
      throw new GeneralSecurityException("Missing public key.");
    }
    Validators.validateVersion(key.getVersion(), getVersion());
    HpkeUtil.validateParams(key.getPublicKey().getParams());
  }

  @Override
  public KeyTypeManager.KeyFactory<HpkeKeyFormat, HpkePrivateKey> keyFactory() {
    return new KeyTypeManager.KeyFactory<HpkeKeyFormat, HpkePrivateKey>(HpkeKeyFormat.class) {
      @Override
      public void validateKeyFormat(HpkeKeyFormat keyFormat) throws GeneralSecurityException {
        HpkeUtil.validateParams(keyFormat.getParams());
      }

      @Override
      public HpkeKeyFormat parseKeyFormat(ByteString byteString)
          throws InvalidProtocolBufferException {
        return HpkeKeyFormat.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry());
      }

      @Override
      public HpkePrivateKey createKey(HpkeKeyFormat keyFormat) throws GeneralSecurityException {
        byte[] privateKeyBytes;
        byte[] publicKeyBytes;

        switch (keyFormat.getParams().getKem()) {
          case DHKEM_X25519_HKDF_SHA256:
            privateKeyBytes = X25519.generatePrivateKey();
            publicKeyBytes = X25519.publicFromPrivate(privateKeyBytes);
            break;
          case DHKEM_P256_HKDF_SHA256:
          case DHKEM_P384_HKDF_SHA384:
          case DHKEM_P521_HKDF_SHA512:
            EllipticCurves.CurveType curveType =
                HpkeUtil.nistHpkeKemToCurve(keyFormat.getParams().getKem());
            KeyPair keyPair = EllipticCurves.generateKeyPair(curveType);
            publicKeyBytes =
                EllipticCurves.pointEncode(
                    curveType,
                    EllipticCurves.PointFormatType.UNCOMPRESSED,
                    ((ECPublicKey) keyPair.getPublic()).getW());
            privateKeyBytes = ((ECPrivateKey) keyPair.getPrivate()).getS().toByteArray();
            break;
          default:
            throw new GeneralSecurityException("Invalid KEM");
        }

        HpkePublicKey publicKey =
            HpkePublicKey.newBuilder()
                .setVersion(getVersion())
                .setParams(keyFormat.getParams())
                .setPublicKey(ByteString.copyFrom(publicKeyBytes))
                .build();

        return HpkePrivateKey.newBuilder()
            .setVersion(getVersion())
            .setPublicKey(publicKey)
            .setPrivateKey(ByteString.copyFrom(privateKeyBytes))
            .build();
      }

      @Override
      public Map<String, KeyFactory.KeyFormat<HpkeKeyFormat>> keyFormats() {
        Map<String, KeyFactory.KeyFormat<HpkeKeyFormat>> result = new HashMap<>();
        result.put(
            "DHKEM_X25519_HKDF_SHA256_HKDF_SHA256_AES_128_GCM",
            createKeyFormat(
                HpkeKem.DHKEM_X25519_HKDF_SHA256,
                HpkeKdf.HKDF_SHA256,
                HpkeAead.AES_128_GCM,
                KeyTemplate.OutputPrefixType.TINK));
        result.put(
            "DHKEM_X25519_HKDF_SHA256_HKDF_SHA256_AES_128_GCM_RAW",
            createKeyFormat(
                HpkeKem.DHKEM_X25519_HKDF_SHA256,
                HpkeKdf.HKDF_SHA256,
                HpkeAead.AES_128_GCM,
                KeyTemplate.OutputPrefixType.RAW));
        result.put(
            "DHKEM_X25519_HKDF_SHA256_HKDF_SHA256_AES_256_GCM",
            createKeyFormat(
                HpkeKem.DHKEM_X25519_HKDF_SHA256,
                HpkeKdf.HKDF_SHA256,
                HpkeAead.AES_256_GCM,
                KeyTemplate.OutputPrefixType.TINK));
        result.put(
            "DHKEM_X25519_HKDF_SHA256_HKDF_SHA256_AES_256_GCM_RAW",
            createKeyFormat(
                HpkeKem.DHKEM_X25519_HKDF_SHA256,
                HpkeKdf.HKDF_SHA256,
                HpkeAead.AES_256_GCM,
                KeyTemplate.OutputPrefixType.RAW));
        result.put(
            "DHKEM_X25519_HKDF_SHA256_HKDF_SHA256_CHACHA20_POLY1305",
            createKeyFormat(
                HpkeKem.DHKEM_X25519_HKDF_SHA256,
                HpkeKdf.HKDF_SHA256,
                HpkeAead.CHACHA20_POLY1305,
                KeyTemplate.OutputPrefixType.TINK));
        result.put(
            "DHKEM_X25519_HKDF_SHA256_HKDF_SHA256_CHACHA20_POLY1305_RAW",
            createKeyFormat(
                HpkeKem.DHKEM_X25519_HKDF_SHA256,
                HpkeKdf.HKDF_SHA256,
                HpkeAead.CHACHA20_POLY1305,
                KeyTemplate.OutputPrefixType.RAW));
        result.put(
            "DHKEM_P256_HKDF_SHA256_HKDF_SHA256_AES_128_GCM",
            createKeyFormat(
                HpkeKem.DHKEM_P256_HKDF_SHA256,
                HpkeKdf.HKDF_SHA256,
                HpkeAead.AES_128_GCM,
                KeyTemplate.OutputPrefixType.TINK));
        result.put(
            "DHKEM_P256_HKDF_SHA256_HKDF_SHA256_AES_128_GCM_RAW",
            createKeyFormat(
                HpkeKem.DHKEM_P256_HKDF_SHA256,
                HpkeKdf.HKDF_SHA256,
                HpkeAead.AES_128_GCM,
                KeyTemplate.OutputPrefixType.RAW));
        result.put(
            "DHKEM_P256_HKDF_SHA256_HKDF_SHA256_AES_256_GCM",
            createKeyFormat(
                HpkeKem.DHKEM_P256_HKDF_SHA256,
                HpkeKdf.HKDF_SHA256,
                HpkeAead.AES_256_GCM,
                KeyTemplate.OutputPrefixType.TINK));
        result.put(
            "DHKEM_P256_HKDF_SHA256_HKDF_SHA256_AES_256_GCM_RAW",
            createKeyFormat(
                HpkeKem.DHKEM_P256_HKDF_SHA256,
                HpkeKdf.HKDF_SHA256,
                HpkeAead.AES_256_GCM,
                KeyTemplate.OutputPrefixType.RAW));
        result.put(
            "DHKEM_P384_HKDF_SHA384_HKDF_SHA384_AES_128_GCM",
            createKeyFormat(
                HpkeKem.DHKEM_P384_HKDF_SHA384,
                HpkeKdf.HKDF_SHA384,
                HpkeAead.AES_128_GCM,
                KeyTemplate.OutputPrefixType.TINK));
        result.put(
            "DHKEM_P384_HKDF_SHA384_HKDF_SHA384_AES_128_GCM_RAW",
            createKeyFormat(
                HpkeKem.DHKEM_P384_HKDF_SHA384,
                HpkeKdf.HKDF_SHA384,
                HpkeAead.AES_128_GCM,
                KeyTemplate.OutputPrefixType.RAW));
        result.put(
            "DHKEM_P384_HKDF_SHA384_HKDF_SHA384_AES_256_GCM",
            createKeyFormat(
                HpkeKem.DHKEM_P384_HKDF_SHA384,
                HpkeKdf.HKDF_SHA384,
                HpkeAead.AES_256_GCM,
                KeyTemplate.OutputPrefixType.TINK));
        result.put(
            "DHKEM_P384_HKDF_SHA384_HKDF_SHA384_AES_256_GCM_RAW",
            createKeyFormat(
                HpkeKem.DHKEM_P384_HKDF_SHA384,
                HpkeKdf.HKDF_SHA384,
                HpkeAead.AES_256_GCM,
                KeyTemplate.OutputPrefixType.RAW));
        result.put(
            "DHKEM_P521_HKDF_SHA512_HKDF_SHA512_AES_128_GCM",
            createKeyFormat(
                HpkeKem.DHKEM_P521_HKDF_SHA512,
                HpkeKdf.HKDF_SHA512,
                HpkeAead.AES_128_GCM,
                KeyTemplate.OutputPrefixType.TINK));
        result.put(
            "DHKEM_P521_HKDF_SHA512_HKDF_SHA512_AES_128_GCM_RAW",
            createKeyFormat(
                HpkeKem.DHKEM_P521_HKDF_SHA512,
                HpkeKdf.HKDF_SHA512,
                HpkeAead.AES_128_GCM,
                KeyTemplate.OutputPrefixType.RAW));
        result.put(
            "DHKEM_P521_HKDF_SHA512_HKDF_SHA512_AES_256_GCM",
            createKeyFormat(
                HpkeKem.DHKEM_P521_HKDF_SHA512,
                HpkeKdf.HKDF_SHA512,
                HpkeAead.AES_256_GCM,
                KeyTemplate.OutputPrefixType.TINK));
        result.put(
            "DHKEM_P521_HKDF_SHA512_HKDF_SHA512_AES_256_GCM_RAW",
            createKeyFormat(
                HpkeKem.DHKEM_P521_HKDF_SHA512,
                HpkeKdf.HKDF_SHA512,
                HpkeAead.AES_256_GCM,
                KeyTemplate.OutputPrefixType.RAW));
        return Collections.unmodifiableMap(result);
      }
    };
  }

  private static KeyFactory.KeyFormat<HpkeKeyFormat> createKeyFormat(
      HpkeKem kem, HpkeKdf kdf, HpkeAead aead, KeyTemplate.OutputPrefixType prefixType) {
    HpkeParams params = HpkeParams.newBuilder().setKem(kem).setKdf(kdf).setAead(aead).build();
    return new KeyFactory.KeyFormat<>(
        HpkeKeyFormat.newBuilder().setParams(params).build(), prefixType);
  }
}