This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
/
utils.go
106 lines (96 loc) · 3.91 KB
/
utils.go
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
// Copyright 2020 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 verification provides verification utilities.
//
// This is provided as reference to application authors wishing to calculate
// the exposure key HMAC as part of their exposure notifications mobile app.
//
// This protocol is detailed at
// https://developers.google.com/android/exposure-notifications/verification-system
//
// Although exported, this package is non intended for general consumption.
// It is a shared dependency between multiple exposure notifications projects.
// We cannot guarantee that there won't be breaking changes in the future.
package verification
import (
"crypto/hmac"
"crypto/sha256"
"fmt"
"sort"
"strings"
verifyapi "github.com/google/exposure-notifications-server/pkg/api/v1"
"github.com/google/exposure-notifications-server/pkg/api/v1alpha1"
)
// CalculateExpsureKeyHMACv1Alpha1 is a convenience method for anyone still on v1alpha1.
// Deprecated: use CalculateExposureKeyHMAC instead
// Preserved for clients on v1alpha1, will be removed in v0.3 release.
func CalculateExpsureKeyHMACv1Alpha1(legacyKeys []v1alpha1.ExposureKey, secret []byte) ([]byte, error) {
keys := make([]verifyapi.ExposureKey, len(legacyKeys))
for i, k := range legacyKeys {
keys[i] = verifyapi.ExposureKey{
Key: k.Key,
IntervalNumber: k.IntervalNumber,
IntervalCount: k.IntervalCount,
TransmissionRisk: k.TransmissionRisk,
}
}
return CalculateExposureKeyHMAC(keys, secret)
}
// CalculateAllAllowedExposureKeyHMAC calculates the main HMAC and the optional HMAC. The optional HMAC
// is only valid if the transmission risks are all zero.
func CalculateAllAllowedExposureKeyHMAC(keys []verifyapi.ExposureKey, secret []byte) ([][]byte, error) {
if len(keys) == 0 {
return nil, fmt.Errorf("cannot calculate hmac on empty exposure keys")
}
// Sort by the key
sort.Slice(keys, func(i int, j int) bool {
return strings.Compare(keys[i].Key, keys[j].Key) <= 0
})
// Build the cleartext.
perKeyText := make([]string, 0, len(keys))
altPerKeyText := make([]string, 0, len(keys))
calculateAlt := true
for _, ek := range keys {
perKeyText = append(perKeyText,
fmt.Sprintf("%s.%d.%d.%d", ek.Key, ek.IntervalNumber, ek.IntervalCount, ek.TransmissionRisk))
altPerKeyText = append(altPerKeyText,
fmt.Sprintf("%s.%d.%d", ek.Key, ek.IntervalNumber, ek.IntervalCount))
// The alt HMAC is only valid of all transmission risk are "omitted" (set to zero).
calculateAlt = calculateAlt && ek.TransmissionRisk == 0
}
cleartext := strings.Join(perKeyText, ",")
mac := hmac.New(sha256.New, secret)
if _, err := mac.Write([]byte(cleartext)); err != nil {
return nil, fmt.Errorf("failed to write hmac: %w", err)
}
results := [][]byte{mac.Sum(nil)}
if calculateAlt {
altCleartext := strings.Join(altPerKeyText, ",")
mac := hmac.New(sha256.New, secret)
if _, err := mac.Write([]byte(altCleartext)); err != nil {
return nil, fmt.Errorf("failed to write hmac: %w", err)
}
results = append(results, mac.Sum(nil))
}
return results, nil
}
// CalculateExposureKeyHMAC will calculate the verification protocol HMAC value.
// Input keys are already to be base64 encoded. They will be sorted if necessary.
func CalculateExposureKeyHMAC(keys []verifyapi.ExposureKey, secret []byte) ([]byte, error) {
results, err := CalculateAllAllowedExposureKeyHMAC(keys, secret)
if err != nil {
return nil, err
}
return results[0], nil
}