xref: /aosp_15_r20/external/tink/go/hybrid/internal/hpke/kem.go (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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
17package hpke
18
19// kem is a package-internal interface for the Hybrid Public Key Encryption
20// (HPKE) key encapsulation mechanism (KEM).
21//
22// The HPKE RFC is available at
23// https://www.rfc-editor.org/rfc/rfc9180.html.
24type kem interface {
25	// encapsulate generates and encapsulates a shared secret using
26	// recipientPubKey. It returns the raw shared secret and encapsulated key.
27	// The HPKE RFC refers to this function as Encap(). It is used by the sender.
28	encapsulate(recipientPubKey []byte) ([]byte, []byte, error)
29
30	// decapsulate extracts the shared secret from encapsulatedKey using
31	// recipientPrivKey. It returns the raw shared secret. The HPKE RFC refers
32	// to this function as Decap(). It is used by the recipient.
33	decapsulate(encapsulatedKey, recipientPrivKey []byte) ([]byte, error)
34
35	// id returns the HPKE KEM algorithm identifier for the underlying KEM
36	// implementation.
37	//
38	// https://www.rfc-editor.org/rfc/rfc9180.html#section-7.1
39	id() uint16
40
41	// encapsulatedKeyLength returns the length of the encapsulated key,
42	// corresponding to Nenc in the following table.
43	//
44	// https://www.rfc-editor.org/rfc/rfc9180.html#section-7.1
45	encapsulatedKeyLength() int
46}
47