1// Copyright 2022 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 19import ( 20 "fmt" 21 22 pb "github.com/google/tink/go/proto/hpke_go_proto" 23) 24 25// newPrimitivesFromProto constructs new KEM, KDF, AEADs from HpkeParams. 26func newPrimitivesFromProto(params *pb.HpkeParams) (kem, kdf, aead, error) { 27 kemID, err := kemIDFromProto(params.GetKem()) 28 if err != nil { 29 return nil, nil, nil, fmt.Errorf("kemIDFromProto(%d): %v", params.GetKem(), err) 30 } 31 kem, err := newKEM(kemID) 32 if err != nil { 33 return nil, nil, nil, fmt.Errorf("newKEM(%d): %v", kemID, err) 34 } 35 36 kdfID, err := kdfIDFromProto(params.GetKdf()) 37 if err != nil { 38 return nil, nil, nil, fmt.Errorf("kdfIDFromProto(%d): %v", params.GetKdf(), err) 39 } 40 kdf, err := newKDF(kdfID) 41 if err != nil { 42 return nil, nil, nil, fmt.Errorf("newKDF(%d): %v", kdfID, err) 43 } 44 45 aeadID, err := aeadIDFromProto(params.GetAead()) 46 if err != nil { 47 return nil, nil, nil, fmt.Errorf("aeadIDFromProto(%d): %v", params.GetAead(), err) 48 } 49 aead, err := newAEAD(aeadID) 50 if err != nil { 51 return nil, nil, nil, fmt.Errorf("newAEAD(%d): %v", aeadID, err) 52 } 53 54 return kem, kdf, aead, nil 55} 56 57// newKEM constructs a HPKE KEM using kemID, which are specified at 58// https://www.rfc-editor.org/rfc/rfc9180.html#section-7.1. 59func newKEM(kemID uint16) (kem, error) { 60 if kemID == x25519HKDFSHA256 { 61 return newX25519KEM(sha256) 62 } 63 return nil, fmt.Errorf("KEM ID %d is not supported", kemID) 64} 65 66// kemIDFromProto returns the KEM ID from the HpkeKem enum value. KEM IDs are 67// specified at 68// https://www.rfc-editor.org/rfc/rfc9180.html#section-7.1. 69func kemIDFromProto(enum pb.HpkeKem) (uint16, error) { 70 if enum == pb.HpkeKem_DHKEM_X25519_HKDF_SHA256 { 71 return x25519HKDFSHA256, nil 72 } 73 return 0, fmt.Errorf("HpkeKem enum value %d is not supported", enum) 74} 75 76// newKDF constructs a HPKE KDF using kdfID, which are specified at 77// https://www.rfc-editor.org/rfc/rfc9180.html#section-7.2. 78func newKDF(kdfID uint16) (kdf, error) { 79 if kdfID == hkdfSHA256 { 80 return newHKDFKDF(sha256) 81 } 82 return nil, fmt.Errorf("KDF ID %d is not supported", kdfID) 83} 84 85// kdfIDFromProto returns the KDF ID from the HpkeKdf enum value. KDF IDs are 86// specified at 87// https://www.rfc-editor.org/rfc/rfc9180.html#section-7.2. 88func kdfIDFromProto(enum pb.HpkeKdf) (uint16, error) { 89 if enum == pb.HpkeKdf_HKDF_SHA256 { 90 return hkdfSHA256, nil 91 } 92 return 0, fmt.Errorf("HpkeKdf enum value %d is not supported", enum) 93} 94 95// newAEAD constructs a HPKE AEAD using aeadID, which are specified at 96// https://www.rfc-editor.org/rfc/rfc9180.html#section-7.3. 97func newAEAD(aeadID uint16) (aead, error) { 98 switch aeadID { 99 case aes128GCM: 100 return newAESGCMAEAD(16) 101 case aes256GCM: 102 return newAESGCMAEAD(32) 103 case chaCha20Poly1305: 104 return &chaCha20Poly1305AEAD{}, nil 105 default: 106 return nil, fmt.Errorf("AEAD ID %d is not supported", aeadID) 107 } 108} 109 110// aeadIDFromProto returns the AEAD ID from the HpkeAead enum value. AEAD IDs 111// are specified at 112// https://www.rfc-editor.org/rfc/rfc9180.html#section-7.3. 113func aeadIDFromProto(enum pb.HpkeAead) (uint16, error) { 114 switch enum { 115 case pb.HpkeAead_AES_128_GCM: 116 return aes128GCM, nil 117 case pb.HpkeAead_AES_256_GCM: 118 return aes256GCM, nil 119 case pb.HpkeAead_CHACHA20_POLY1305: 120 return chaCha20Poly1305, nil 121 default: 122 return 0, fmt.Errorf("HpkeAead enum value %d is not supported", enum) 123 } 124} 125