| 1 | // Copyright (c) 2021 Oasis Labs Inc. All rights reserved. |
| 2 | // |
| 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are |
| 5 | // met: |
| 6 | // |
| 7 | // 1. Redistributions of source code must retain the above copyright |
| 8 | // notice, this list of conditions and the following disclaimer. |
| 9 | // |
| 10 | // 2. Redistributions in binary form must reproduce the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer in the |
| 12 | // documentation and/or other materials provided with the distribution. |
| 13 | // |
| 14 | // 3. Neither the name of the copyright holder nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 19 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 20 | // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 21 | // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 24 | // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 25 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 26 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | package h2c |
| 31 | |
| 32 | import ( |
| 33 | "crypto" |
| 34 | "fmt" |
| 35 | "io" |
| 36 | "math" |
| 37 | |
| 38 | "golang.org/x/crypto/sha3" |
| 39 | ) |
| 40 | |
| 41 | var oversizeDST = []byte("H2C-OVERSIZE-DST-") |
| 42 | |
| 43 | // ExpandMessageXMD implements expand_message_xmd, overwriting out with |
| 44 | // uniformly random data generated by the provided hash function, domain |
| 45 | // separation tag, and message. |
| 46 | func ExpandMessageXMD(out []byte, hFunc crypto.Hash, domainSeparator, message []byte) error { |
| 47 | lenInBytes := len(out) |
| 48 | bInBytes := hFunc.Size() |
| 49 | |
| 50 | h := hFunc.New() |
| 51 | rInBytes := h.BlockSize() |
| 52 | |
| 53 | // 0. Ensure parameters are sensible. |
| 54 | if bInBytes < 2*kay/8 { |
| 55 | return fmt.Errorf("h2c: b_in_bytes insufficiently large: %d", bInBytes) |
| 56 | } |
| 57 | if lenInBytes == 0 || lenInBytes > math.MaxUint16 { |
| 58 | return fmt.Errorf("h2c: len_in_bytes out of range: %d", lenInBytes) |
| 59 | } |
| 60 | |
| 61 | // 5.3.3 Using DSTs longer than 255 bytes. |
| 62 | DST := domainSeparator |
| 63 | lenDST := len(domainSeparator) |
| 64 | if lenDST > math.MaxUint8 { |
| 65 | // DST = H("H2C-OVERSIZE-DST-" || a_very_long_DST) |
| 66 | _, _ = h.Write(oversizeDST) |
| 67 | _, _ = h.Write(DST) |
| 68 | |
| 69 | DST = h.Sum(nil) |
| 70 | lenDST = len(DST) |
| 71 | |
| 72 | h.Reset() |
| 73 | } |
| 74 | |
| 75 | // 1. ell = ceil(len_in_bytes / b_in_bytes) |
| 76 | ell := (lenInBytes + bInBytes - 1) / bInBytes |
| 77 | |
| 78 | // 2. ABORT if ell > 255 |
| 79 | if ell > 255 { |
| 80 | return fmt.Errorf("h2c: ell out of range: %d", ell) |
| 81 | } |
| 82 | |
| 83 | // 7. b_0 = H(msg_prime) |
| 84 | _, _ = h.Write(make([]byte, rInBytes)) // Z_pad (I2OSP(0, r_in_bytes)) |
| 85 | _, _ = h.Write(message) // msg |
| 86 | _, _ = h.Write([]byte{byte(lenInBytes >> 8), byte(lenInBytes), 0}) // l_i_b_str || I2OSP(0, 1) |
| 87 | _, _ = h.Write(DST) // DST |
| 88 | _, _ = h.Write([]byte{byte(lenDST)}) // I2OSP(len(DST), 1) |
| 89 | b0 := h.Sum(nil) |
| 90 | |
| 91 | // 8. b_1 = H(b_0 || I2OSP(1, 1) || DST_prime) |
| 92 | h.Reset() |
| 93 | _, _ = h.Write(b0) // b_0 |
| 94 | _, _ = h.Write([]byte{1}) // I2OSP(1, 1) |
| 95 | _, _ = h.Write(DST) // DST |
| 96 | _, _ = h.Write([]byte{byte(lenDST)}) // I2OSP(len(DST), 1) |
| 97 | b1 := h.Sum(nil) |
| 98 | |
| 99 | // We attempt to be somewhat more efficient about the remaining steps |
| 100 | // by: |
| 101 | // * Seeing if we can service the request just with b_1 (len_in_bytes <= b_in_bytes) |
| 102 | // * Allocating a b_in_bytes sized temporary buffer to store: |
| 103 | // * b_(i - 1) (Initialized to b_1) |
| 104 | // * strxor(b_0, b_(i - 1)) |
| 105 | // * b_i, which becomes b_(i - 1) |
| 106 | |
| 107 | // Special case: if len_in_bytes <= b_in_bytes, we can return output |
| 108 | // from b_1 and terminate. |
| 109 | if lenInBytes <= bInBytes { |
| 110 | copy(out, b1[:lenInBytes]) |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | // Reuse a temporary buffer to hold both the xored portion of the hash |
| 115 | // input and b_(i - 1). |
| 116 | xorBuf := make([]byte, 0, bInBytes) |
| 117 | xorBuf = append(xorBuf, b1...) |
| 118 | |
| 119 | // Append b_1 to the output, since we know we need all of it. |
| 120 | copy(out, b1) // 11. uniform_bytes = b_1 || ... |
| 121 | outOff := len(b1) |
| 122 | |
| 123 | // 9. for i in (2, ..., ell): |
| 124 | for i, wanted := 2, lenInBytes-bInBytes; wanted > 0; i++ { |
| 125 | // 10. b_i = H(strxor(b_0, b_(i - 1)) || I2OSP(i, 1) || DST_prime) |
| 126 | for i, v := range b0 { |
| 127 | xorBuf[i] ^= v |
| 128 | } |
| 129 | |
| 130 | h.Reset() |
| 131 | _, _ = h.Write(xorBuf) // strxor(b_0, b_(i - 1)) |
| 132 | _, _ = h.Write([]byte{byte(i)}) // I2OSP(i, 1) |
| 133 | _, _ = h.Write(DST) // DST |
| 134 | _, _ = h.Write([]byte{byte(lenDST)}) // I2OSP(len(DST), 1) |
| 135 | h.Sum(xorBuf[:0]) // xorBuf = b_i |
| 136 | |
| 137 | // Append up to b_in_bytes from b_i (this handles the substr) |
| 138 | toAppend := wanted |
| 139 | if wanted > bInBytes { |
| 140 | toAppend = bInBytes |
| 141 | } |
| 142 | |
| 143 | copy(out[outOff:], xorBuf[:toAppend]) |
| 144 | outOff += toAppend |
| 145 | wanted -= toAppend |
| 146 | } |
| 147 | |
| 148 | return nil |
| 149 | } |
| 150 | |
| 151 | func newXOF(xofFunc sha3.ShakeHash) sha3.ShakeHash { |
| 152 | xof := xofFunc.Clone() |
| 153 | xof.Reset() |
| 154 | |
| 155 | return xof |
| 156 | } |
| 157 | |
| 158 | // ExpandMessageXOF implements expand_message_xof, overwriting out with |
| 159 | // uniformly random data generated by the provided extensible-output |
| 160 | // function, domain separation tag, and message. |
| 161 | // |
| 162 | // Note: This needs to use the Clone() method of the XOF to instantiate |
| 163 | // a new instance of the XOF. At present there are 3 different XOF |
| 164 | // interfaces in the x/crypto package, all mutually incompatible due |
| 165 | // to the return type of Clone(). Complain to the x/crypto developers, |
| 166 | // not me. |
| 167 | func ExpandMessageXOF(out []byte, xofFunc sha3.ShakeHash, domainSeparator, message []byte) error { |
| 168 | lenInBytes := len(out) |
| 169 | |
| 170 | // 0. Ensure parameters are sensible. |
| 171 | if lenInBytes == 0 || lenInBytes > math.MaxUint16 { |
| 172 | return fmt.Errorf("h2c: len_in_bytes out of range: %d", lenInBytes) |
| 173 | } |
| 174 | |
| 175 | // Get a fresh instance of the XOF to work with. |
| 176 | xof := newXOF(xofFunc) |
| 177 | |
| 178 | // Feed input into the XOF. Since we have an XOF, we can feed the |
| 179 | // inputs into the XOF one-by-one instead of allocating a temporary |
| 180 | // buffer. |
| 181 | |
| 182 | // 2. msg_prime = msg || I2OSP(len_in_bytes, 2) || DST_prime (appended next) |
| 183 | _, _ = xof.Write(message) // msg |
| 184 | _, _ = xof.Write([]byte{byte(lenInBytes >> 8), byte(lenInBytes)}) // I2OSP(len_in_bytes, 2) |
| 185 | |
| 186 | // 1. DST_prime = DST || I2OSP(len(DST), 1) |
| 187 | DST := domainSeparator |
| 188 | lenDST := len(domainSeparator) |
| 189 | if lenDST > math.MaxUint8 { |
| 190 | newDST := make([]byte, 2*kay/8) |
| 191 | |
| 192 | dstXOF := newXOF(xofFunc) |
| 193 | _, _ = dstXOF.Write(oversizeDST) |
| 194 | _, _ = dstXOF.Write(DST) |
| 195 | if _, err := io.ReadFull(dstXOF, newDST); err != nil { |
| 196 | return fmt.Errorf("h2c: failed to read shortened DST: %w", err) |
| 197 | } |
| 198 | |
| 199 | DST = newDST |
| 200 | lenDST = len(DST) |
| 201 | } |
| 202 | _, _ = xof.Write(DST) // DST |
| 203 | _, _ = xof.Write([]byte{byte(lenDST)}) // I2OSP(len(DST), 1) |
| 204 | |
| 205 | // 3. uniform_bytes = H(msg_prime, len_in_bytes) |
| 206 | if _, err := io.ReadFull(xof, out); err != nil { |
| 207 | return fmt.Errorf("h2c: failed to read XOF output: %w", err) |
| 208 | } |
| 209 | |
| 210 | return nil |
| 211 | } |