Debian Codegraph @

h2c/h2c.go

raw symbols SCIP
1 // Copyright (c) 2021 Oasis Labs Inc. All rights reserved.
2 // Copyright (c) 2021 Yawning Angel. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // Package h2c implements Hashing to Elliptic Curves as specified in RFC 9380.
32 package h2c
33
34 import (
35 "crypto"
36 _ "crypto/sha512"
37 "fmt"
38
39 "filippo.io/edwards25519"
40 "filippo.io/edwards25519/field"
41 "golang.org/x/crypto/sha3"
42
43 "gitlab.com/yawning/edwards25519-extra/elligator2"
44 "gitlab.com/yawning/edwards25519-extra/internal/montgomery"
45 )
46
47 const (
48 ell = 48 // L = ceil((ceil(log2(2^255-19)) + k) / 8)
49 kay = 128 // k = target security level in bits
50
51 encodeToCurveSize = ell
52 hashToCurveSize = ell * 2
53 )
54
55 // Edwards25519_XMD_SHA512_ELL2_RO implements the edwards25519_XMD:SHA-512_ELL2_RO_
56 // suite.
57 func Edwards25519_XMD_SHA512_ELL2_RO(domainSeparator, message []byte) (*edwards25519.Point, error) {
58 return Edwards25519_XMD_ELL2_RO(crypto.SHA512, domainSeparator, message)
59 }
60
61 // Edwards25519_XMD_SHA512_ELL2_NU implements the edwards25519_XMD:SHA-512_ELL2_NU_
62 // suite.
63 func Edwards25519_XMD_SHA512_ELL2_NU(domainSeparator, message []byte) (*edwards25519.Point, error) {
64 return Edwards25519_XMD_ELL2_NU(crypto.SHA512, domainSeparator, message)
65 }
66
67 // Curve25519_XMD_SHA512_ELL2_RO implements the curve25519_XMD:SHA-512_ELL2_RO_
68 // suite.
69 func Curve25519_XMD_SHA512_ELL2_RO(domainSeparator, message []byte) (*field.Element, *field.Element, error) {
70 return Curve25519_XMD_ELL2_RO(crypto.SHA512, domainSeparator, message)
71 }
72
73 // Curve25519_XMD_SHA512_ELL2_NU implements the curve25519_XMD:SHA-512_ELL2_NU_
74 // suite.
75 func Curve25519_XMD_SHA512_ELL2_NU(domainSeparator, message []byte) (*field.Element, *field.Element, error) {
76 return Curve25519_XMD_ELL2_NU(crypto.SHA512, domainSeparator, message)
77 }
78
79 // Edwards25519_XMD_ELL2_RO implements a generic edwards25519 random oracle suite
80 // using `expand_message_xmd`.
81 func Edwards25519_XMD_ELL2_RO(hFunc crypto.Hash, domainSeparator, message []byte) (*edwards25519.Point, error) {
82 var uniformBytes [hashToCurveSize]byte
83 if err := ExpandMessageXMD(uniformBytes[:], hFunc, domainSeparator, message); err != nil {
84 return nil, fmt.Errorf("h2c: failed to expand message: %w", err)
85 }
86 return hashToCurveEdwards(&uniformBytes), nil
87 }
88
89 // Edwards25519_XMD_ELL2_NU implements a generic edwards25519 nonuniform suite
90 // using `expand_messsage_xmd`.
91 func Edwards25519_XMD_ELL2_NU(hFunc crypto.Hash, domainSeparator, message []byte) (*edwards25519.Point, error) {
92 var uniformBytes [encodeToCurveSize]byte
93 if err := ExpandMessageXMD(uniformBytes[:], hFunc, domainSeparator, message); err != nil {
94 return nil, fmt.Errorf("h2c: failed to expand message: %w", err)
95 }
96 return encodeToCurveEdwards(&uniformBytes), nil
97 }
98
99 // Edwards25519_XOF_ELL2_RO implements a generic edwards25519 random oracle suite
100 // using `expand_message_xof`.
101 func Edwards25519_XOF_ELL2_RO(xofFunc sha3.ShakeHash, domainSeparator, message []byte) (*edwards25519.Point, error) {
102 var uniformBytes [hashToCurveSize]byte
103 if err := ExpandMessageXOF(uniformBytes[:], xofFunc, domainSeparator, message); err != nil {
104 return nil, fmt.Errorf("h2c: failed to expand message: %w", err)
105 }
106 return hashToCurveEdwards(&uniformBytes), nil
107 }
108
109 // Edwards25519_XOF_ELL2_NU implements a generic edwards25519 nonuniform suite
110 // using `expand_messsage_xof`.
111 func Edwards25519_XOF_ELL2_NU(xofFunc sha3.ShakeHash, domainSeparator, message []byte) (*edwards25519.Point, error) {
112 var uniformBytes [encodeToCurveSize]byte
113 if err := ExpandMessageXOF(uniformBytes[:], xofFunc, domainSeparator, message); err != nil {
114 return nil, fmt.Errorf("h2c: failed to expand message: %w", err)
115 }
116 return encodeToCurveEdwards(&uniformBytes), nil
117 }
118
119 // Curve25519_XMD_ELL2_RO implements a generic curve25519 random oracle suite
120 // using `expand_message_xmd`, returning the u and v-coordinates.
121 func Curve25519_XMD_ELL2_RO(hFunc crypto.Hash, domainSeparator, message []byte) (*field.Element, *field.Element, error) {
122 var uniformBytes [hashToCurveSize]byte
123 if err := ExpandMessageXMD(uniformBytes[:], hFunc, domainSeparator, message); err != nil {
124 return nil, nil, fmt.Errorf("h2c: failed to expand message: %w", err)
125 }
126 u, v := hashToCurveMontgomery(&uniformBytes)
127 return u, v, nil
128 }
129
130 // Curve25519_XMD_ELL2_NU implements a generic curve25519 nonuniform suite
131 // using `expand_messsage_xmd`, returning the u and v-coordiantes.
132 func Curve25519_XMD_ELL2_NU(hFunc crypto.Hash, domainSeparator, message []byte) (*field.Element, *field.Element, error) {
133 var uniformBytes [encodeToCurveSize]byte
134 if err := ExpandMessageXMD(uniformBytes[:], hFunc, domainSeparator, message); err != nil {
135 return nil, nil, fmt.Errorf("h2c: failed to expand message: %w", err)
136 }
137 u, v := encodeToCurveMontgomery(&uniformBytes)
138 return u, v, nil
139 }
140
141 // Curve25519_XOF_ELL2_RO implements a generic curve25519 random oracle suite
142 // using `expand_message_xof`, returning the u and v-coordinates.
143 func Curve25519_XOF_ELL2_RO(xofFunc sha3.ShakeHash, domainSeparator, message []byte) (*field.Element, *field.Element, error) {
144 var uniformBytes [hashToCurveSize]byte
145 if err := ExpandMessageXOF(uniformBytes[:], xofFunc, domainSeparator, message); err != nil {
146 return nil, nil, fmt.Errorf("h2c: failed to expand message: %w", err)
147 }
148 u, v := hashToCurveMontgomery(&uniformBytes)
149 return u, v, nil
150 }
151
152 // Curve5519_XOF_ELL2_NU implements a generic curve25519 nonuniform suite
153 // using `expand_messsage_xof`, returning the u and v-coordinates.
154 func Curve25519_XOF_ELL2_NU(xofFunc sha3.ShakeHash, domainSeparator, message []byte) (*field.Element, *field.Element, error) {
155 var uniformBytes [encodeToCurveSize]byte
156 if err := ExpandMessageXOF(uniformBytes[:], xofFunc, domainSeparator, message); err != nil {
157 return nil, nil, fmt.Errorf("h2c: failed to expand message: %w", err)
158 }
159 u, v := encodeToCurveMontgomery(&uniformBytes)
160 return u, v, nil
161 }
162
163 func hashToCurveEdwards(uniformBytes *[hashToCurveSize]byte) *edwards25519.Point {
164 fe0 := uniformToField25519(uniformBytes[:ell])
165 fe1 := uniformToField25519(uniformBytes[ell:])
166
167 Q0 := elligator2.EdwardsFlavor(fe0)
168 Q1 := elligator2.EdwardsFlavor(fe1)
169
170 p := new(edwards25519.Point).Add(Q0, Q1)
171 return p.MultByCofactor(p)
172 }
173
174 func encodeToCurveEdwards(uniformBytes *[encodeToCurveSize]byte) *edwards25519.Point {
175 fe := uniformToField25519(uniformBytes[:])
176
177 Q := elligator2.EdwardsFlavor(fe)
178
179 return new(edwards25519.Point).MultByCofactor(Q)
180 }
181
182 func hashToCurveMontgomery(uniformBytes *[hashToCurveSize]byte) (*field.Element, *field.Element) {
183 p := hashToCurveEdwards(uniformBytes)
184 return montgomery.FromEdwardsPoint(p)
185 }
186
187 func encodeToCurveMontgomery(uniformBytes *[encodeToCurveSize]byte) (*field.Element, *field.Element) {
188 p := encodeToCurveEdwards(uniformBytes)
189 return montgomery.FromEdwardsPoint(p)
190 }
191
192 func uniformToField25519(b []byte) *field.Element {
193 if len(b) != ell {
194 panic("h2c: invalid uniform bytes length")
195 }
196
197 // Unlike curve25519-voi, edwards25519 implements a 512-bit reduction
198 // so zero-extend the big-endian input.
199 bExtended := make([]byte, 64-ell, 64)
200 bExtended = append(bExtended, b...)
201
202 // The wide-reduction routine wants little-endian, so do the byte-swap.
203 bLE := reversedByteSlice(bExtended)
204
205 fe, err := new(field.Element).SetWideBytes(bLE)
206 if err != nil {
207 panic("h2c: failed to decode wide field element: " + err.Error())
208 }
209
210 return fe
211 }
212
213 func reversedByteSlice(b []byte) []byte {
214 bLen := len(b)
215 if bLen == 0 {
216 return []byte{}
217 }
218
219 out := make([]byte, bLen)
220 for i, j := bLen-1, 0; i >= 0; i, j = i-1, j+1 {
221 out[j] = b[i]
222 }
223
224 return out
225 }