forked from FiloSottile/age
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kyber1024.go
155 lines (131 loc) · 4.44 KB
/
kyber1024.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2019 The age Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package age
import (
"crypto/rand"
"errors"
"fmt"
"io"
"strings"
"filippo.io/age/internal/bech32"
"filippo.io/age/internal/format"
"github.com/cloudflare/circl/kem/kyber/kyber1024"
"golang.org/x/crypto/sha3"
)
const KyberLabel = "age-encryption.org/v3/Kyber1024"
// Kyber1024Recipient is the standard age public key. Messages encrypted to this
// recipient can be decrypted with the corresponding Kyber1024Identity.
//
// This recipient is anonymous, in the sense that an attacker can't tell from
// the message alone if it is encrypted to a certain recipient.
type Kyber1024Recipient struct {
theirPublicKey []byte
}
var _ Recipient = &Kyber1024Recipient{}
// ParseKyber1024Recipient returns a new Kyber1024Recipient from a raw string without any encoding
func ParseKyber1024Recipient(s string) (*Kyber1024Recipient, error) {
t, k, err := bech32.Decode(s)
if err != nil {
return nil, fmt.Errorf("malformed recipient %q: %v", s, err)
}
if t != "age" {
return nil, fmt.Errorf("malformed recipient %q: invalid type %q", s, t)
}
return &Kyber1024Recipient{theirPublicKey: k}, nil
}
func (r *Kyber1024Recipient) Wrap(fileKey []byte) ([]*Stanza, error) {
//sharedKey<-encapsulate(pk) as wrappingKey
var p kyber1024.PublicKey
p.Unpack(r.theirPublicKey)
ct := make([]byte, kyber1024.CiphertextSize)
wrappingKey := make([]byte, kyber1024.SharedKeySize)
p.EncapsulateTo(ct, wrappingKey, nil)
wrappedKey, err := aeadEncrypt(wrappingKey, fileKey)
if err != nil {
return nil, err
}
//Due to the size of kyber.encapsulation,it's more pleasing to put wrappedKey to where ourPublicKey was
l := &Stanza{
Type: "Kyber1024",
Args: []string{format.EncodeToString(wrappedKey)},
Body: ct,
}
return []*Stanza{l}, nil
}
// String returns the Bech32 public key encoding of r.
func (r *Kyber1024Recipient) String() string {
s, _ := bech32.Encode("age", r.theirPublicKey)
return s
}
// Kyber1024Identity is the key seed bind to a certain Kyber1024.(pk,sk) key pair, which can decapsulate messages
// encrypted to the corresponding Kyber1024Recipient.
type Kyber1024Identity struct {
ks []byte
}
var _ Identity = &Kyber1024Identity{}
// GenerateKyber1024Identity randomly generates a new Kyber1024Identity.
func GenerateKyber1024Identity() (*Kyber1024Identity, error) {
var i Kyber1024Identity
var seed [kyber1024.KeySeedSize]byte
_, err := io.ReadFull(rand.Reader, seed[:])
if err != nil {
return nil, err
}
h := sha3.NewShake256()
h.Write(seed[:])
h.Read(seed[:])
i.ks = seed[:]
return &i, err
}
// ParseKyber1024Identity returns a new Kyber1024Identity from a Kyber1024 private key
// encoding with the "AGE-SECRET-KEY-1" prefix.
func ParseKyber1024Identity(s string) (*Kyber1024Identity, error) {
t, k, err := bech32.Decode(s)
if err != nil {
return nil, fmt.Errorf("malformed secret key: %v", err)
}
if t != "AGE-SECRET-KEY-" {
return nil, fmt.Errorf("malformed secret key: unknown type %q", t)
}
return &Kyber1024Identity{ks: k}, nil
}
func (i *Kyber1024Identity) Unwrap(stanzas []*Stanza) ([]byte, error) {
return multiUnwrap(i.unwrap, stanzas)
}
func (i *Kyber1024Identity) unwrap(block *Stanza) ([]byte, error) {
if block.Type != "Kyber1024" {
return nil, ErrIncorrectIdentity
}
if len(block.Args) != 1 {
return nil, errors.New("invalid Kyber1024 recipient block")
}
wrappedKey, err := format.DecodeString(block.Args[0])
if err != nil {
return nil, fmt.Errorf("failed to parse Kyber1024 wrappedKey: %v", err)
}
wrappingKey := make([]byte, kyber1024.SharedKeySize)
_, sk := kyber1024.NewKeyFromSeed(i.ks[:])
sk.DecapsulateTo(wrappingKey, block.Body)
fileKey, err := aeadDecrypt(wrappingKey, fileKeySize, wrappedKey)
if err == errIncorrectCiphertextSize {
return nil, errors.New("invalid Kyber1024 recipient block: incorrect file key size")
} else if err != nil {
return nil, ErrIncorrectIdentity
}
return fileKey, nil
}
// Recipient returns the public Kyber1024Recipient value corresponding to i.
func (i *Kyber1024Identity) Recipient() *Kyber1024Recipient {
var r Kyber1024Recipient
pk, _ := kyber1024.NewKeyFromSeed(i.ks)
buf := make([]byte, kyber1024.PublicKeySize)
pk.Pack(buf)
r.theirPublicKey = buf
return &r
}
// String returns the seed of private key
func (i *Kyber1024Identity) String() string {
s, _ := bech32.Encode("AGE-SECRET-KEY-", i.ks)
return strings.ToUpper(s)
}