• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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.subtle;
18 
19 import java.security.GeneralSecurityException;
20 import java.security.interfaces.ECPrivateKey;
21 import java.security.interfaces.ECPublicKey;
22 
23 /**
24  * HKDF-based KEM (key encapsulation mechanism) for ECIES recipient.
25  *
26  * @since 1.0.0
27  */
28 public final class EciesHkdfRecipientKem {
29   private ECPrivateKey recipientPrivateKey;
30 
EciesHkdfRecipientKem(final ECPrivateKey recipientPrivateKey)31   public EciesHkdfRecipientKem(final ECPrivateKey recipientPrivateKey) {
32     this.recipientPrivateKey = recipientPrivateKey;
33   }
34 
generateKey( byte[] kemBytes, String hmacAlgo, final byte[] hkdfSalt, final byte[] hkdfInfo, int keySizeInBytes, EllipticCurves.PointFormatType pointFormat)35   public byte[] generateKey(
36       byte[] kemBytes,
37       String hmacAlgo,
38       final byte[] hkdfSalt,
39       final byte[] hkdfInfo,
40       int keySizeInBytes,
41       EllipticCurves.PointFormatType pointFormat)
42       throws GeneralSecurityException {
43     ECPublicKey ephemeralPublicKey = EllipticCurves.getEcPublicKey(
44         recipientPrivateKey.getParams(), pointFormat, kemBytes);
45     byte[] sharedSecret = EllipticCurves.computeSharedSecret(
46         recipientPrivateKey, ephemeralPublicKey);
47     return Hkdf.computeEciesHkdfSymmetricKey(
48         kemBytes, sharedSecret, hmacAlgo, hkdfSalt, hkdfInfo, keySizeInBytes);
49   }
50 }
51