• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 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.hybrid;
18 
19 import static com.google.crypto.tink.internal.TinkBugException.exceptionIsBug;
20 
21 import com.google.crypto.tink.aead.AesCtrHmacAeadParameters;
22 import com.google.crypto.tink.aead.AesGcmParameters;
23 
24 /**
25  * Pre-generated {@link Parameter} objects for {@link com.google.crypto.tink.HybridEncrypt} and
26  * {@link com.google.crypto.tink.HybridDecrypt} keys.
27  *
28  * <p>Note: if you want to keep dependencies small, consider inlining the constants here.
29  */
30 public final class PredefinedHybridParameters {
31   /**
32    * A {@link KeyTemplate} that generates new instances of {@link
33    * com.google.crypto.tink.proto.EciesAeadHkdfPrivateKey} with the following parameters:
34    *
35    * <ul>
36    *   <li>KEM: ECDH over NIST P-256
37    *   <li>DEM: AES128-GCM
38    *   <li>KDF: HKDF-HMAC-SHA256 with an empty salt
39    * </ul>
40    *
41    * <p>Unlike other key templates that use AES-GCM, the instances of {@link HybridDecrypt}
42    * generated by this key template has no limitation on Android KitKat (API level 19). They might
43    * not work in older versions though.
44    */
45   public static final EciesParameters ECIES_P256_HKDF_HMAC_SHA256_AES128_GCM =
46       exceptionIsBug(
47           () ->
48               EciesParameters.builder()
49                   .setCurveType(EciesParameters.CurveType.NIST_P256)
50                   .setHashType(EciesParameters.HashType.SHA256)
51                   .setNistCurvePointFormat(EciesParameters.PointFormat.UNCOMPRESSED)
52                   .setVariant(EciesParameters.Variant.TINK)
53                   .setDemParameters(
54                       AesGcmParameters.builder()
55                           .setIvSizeBytes(12)
56                           .setKeySizeBytes(16)
57                           .setTagSizeBytes(16)
58                           .setVariant(AesGcmParameters.Variant.NO_PREFIX)
59                           .build())
60                   .build());
61 
62   /**
63    * A {@link KeyTemplate} that generates new instances of {@link
64    * com.google.crypto.tink.proto.EciesAeadHkdfPrivateKey} with the following parameters:
65    *
66    * <ul>
67    *   <li>KEM: ECDH over NIST P-256
68    *   <li>DEM: AES128-GCM
69    *   <li>KDF: HKDF-HMAC-SHA256 with an empty salt
70    *   <li>EC Point Format: Compressed
71    *   <li>OutputPrefixType: RAW
72    * </ul>
73    *
74    * <p>Unlike other key templates that use AES-GCM, the instances of {@link HybridDecrypt}
75    * generated by this key template has no limitation on Android KitKat (API level 19). They might
76    * not work in older versions though.
77    */
78   public static final EciesParameters
79       ECIES_P256_HKDF_HMAC_SHA256_AES128_GCM_COMPRESSED_WITHOUT_PREFIX =
80           exceptionIsBug(
81               () ->
82                   EciesParameters.builder()
83                       .setCurveType(EciesParameters.CurveType.NIST_P256)
84                       .setHashType(EciesParameters.HashType.SHA256)
85                       .setNistCurvePointFormat(EciesParameters.PointFormat.COMPRESSED)
86                       .setVariant(EciesParameters.Variant.NO_PREFIX)
87                       .setDemParameters(
88                           AesGcmParameters.builder()
89                               .setIvSizeBytes(12)
90                               .setKeySizeBytes(16)
91                               .setTagSizeBytes(16)
92                               .setVariant(AesGcmParameters.Variant.NO_PREFIX)
93                               .build())
94                       .build());
95 
96   /**
97    * A {@link KeyTemplate} that generates new instances of {@link
98    * com.google.crypto.tink.proto.EciesAeadHkdfPrivateKey} with the following parameters:
99    *
100    * <ul>
101    *   <li>KEM: ECDH over NIST P-256
102    *   <li>DEM: AES128-CTR-HMAC-SHA256 with the following parameters
103    *       <ul>
104    *         <li>AES key size: 16 bytes
105    *         <li>AES CTR IV size: 16 bytes
106    *         <li>HMAC key size: 32 bytes
107    *         <li>HMAC tag size: 16 bytes
108    *       </ul>
109    *   <li>KDF: HKDF-HMAC-SHA256 with an empty salt
110    * </ul>
111    */
112   public static final EciesParameters ECIES_P256_HKDF_HMAC_SHA256_AES128_CTR_HMAC_SHA256 =
113       exceptionIsBug(
114           () ->
115               EciesParameters.builder()
116                   .setCurveType(EciesParameters.CurveType.NIST_P256)
117                   .setHashType(EciesParameters.HashType.SHA256)
118                   .setNistCurvePointFormat(EciesParameters.PointFormat.UNCOMPRESSED)
119                   .setVariant(EciesParameters.Variant.TINK)
120                   .setDemParameters(
121                       AesCtrHmacAeadParameters.builder()
122                           .setAesKeySizeBytes(16)
123                           .setHmacKeySizeBytes(32)
124                           .setTagSizeBytes(16)
125                           .setIvSizeBytes(16)
126                           .setHashType(AesCtrHmacAeadParameters.HashType.SHA256)
127                           .setVariant(AesCtrHmacAeadParameters.Variant.NO_PREFIX)
128                           .build())
129                   .build());
130 
PredefinedHybridParameters()131   private PredefinedHybridParameters() {}
132 }
133