annotate libgo/go/crypto/rsa/pkcs1v15.go @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 84e7813d76e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // Copyright 2009 The Go Authors. All rights reserved.
kono
parents:
diff changeset
2 // Use of this source code is governed by a BSD-style
kono
parents:
diff changeset
3 // license that can be found in the LICENSE file.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 package rsa
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 import (
kono
parents:
diff changeset
8 "crypto"
kono
parents:
diff changeset
9 "crypto/subtle"
kono
parents:
diff changeset
10 "errors"
kono
parents:
diff changeset
11 "io"
kono
parents:
diff changeset
12 "math/big"
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
13
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
14 "crypto/internal/randutil"
111
kono
parents:
diff changeset
15 )
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 // This file implements encryption and decryption using PKCS#1 v1.5 padding.
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19 // PKCS1v15DecrypterOpts is for passing options to PKCS#1 v1.5 decryption using
kono
parents:
diff changeset
20 // the crypto.Decrypter interface.
kono
parents:
diff changeset
21 type PKCS1v15DecryptOptions struct {
kono
parents:
diff changeset
22 // SessionKeyLen is the length of the session key that is being
kono
parents:
diff changeset
23 // decrypted. If not zero, then a padding error during decryption will
kono
parents:
diff changeset
24 // cause a random plaintext of this length to be returned rather than
kono
parents:
diff changeset
25 // an error. These alternatives happen in constant time.
kono
parents:
diff changeset
26 SessionKeyLen int
kono
parents:
diff changeset
27 }
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 // EncryptPKCS1v15 encrypts the given message with RSA and the padding
kono
parents:
diff changeset
30 // scheme from PKCS#1 v1.5. The message must be no longer than the
kono
parents:
diff changeset
31 // length of the public modulus minus 11 bytes.
kono
parents:
diff changeset
32 //
kono
parents:
diff changeset
33 // The rand parameter is used as a source of entropy to ensure that
kono
parents:
diff changeset
34 // encrypting the same message twice doesn't result in the same
kono
parents:
diff changeset
35 // ciphertext.
kono
parents:
diff changeset
36 //
kono
parents:
diff changeset
37 // WARNING: use of this function to encrypt plaintexts other than
kono
parents:
diff changeset
38 // session keys is dangerous. Use RSA OAEP in new protocols.
kono
parents:
diff changeset
39 func EncryptPKCS1v15(rand io.Reader, pub *PublicKey, msg []byte) ([]byte, error) {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
40 randutil.MaybeReadByte(rand)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
41
111
kono
parents:
diff changeset
42 if err := checkPub(pub); err != nil {
kono
parents:
diff changeset
43 return nil, err
kono
parents:
diff changeset
44 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
45 k := pub.Size()
111
kono
parents:
diff changeset
46 if len(msg) > k-11 {
kono
parents:
diff changeset
47 return nil, ErrMessageTooLong
kono
parents:
diff changeset
48 }
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 // EM = 0x00 || 0x02 || PS || 0x00 || M
kono
parents:
diff changeset
51 em := make([]byte, k)
kono
parents:
diff changeset
52 em[1] = 2
kono
parents:
diff changeset
53 ps, mm := em[2:len(em)-len(msg)-1], em[len(em)-len(msg):]
kono
parents:
diff changeset
54 err := nonZeroRandomBytes(ps, rand)
kono
parents:
diff changeset
55 if err != nil {
kono
parents:
diff changeset
56 return nil, err
kono
parents:
diff changeset
57 }
kono
parents:
diff changeset
58 em[len(em)-len(msg)-1] = 0
kono
parents:
diff changeset
59 copy(mm, msg)
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 m := new(big.Int).SetBytes(em)
kono
parents:
diff changeset
62 c := encrypt(new(big.Int), pub, m)
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 copyWithLeftPad(em, c.Bytes())
kono
parents:
diff changeset
65 return em, nil
kono
parents:
diff changeset
66 }
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 // DecryptPKCS1v15 decrypts a plaintext using RSA and the padding scheme from PKCS#1 v1.5.
kono
parents:
diff changeset
69 // If rand != nil, it uses RSA blinding to avoid timing side-channel attacks.
kono
parents:
diff changeset
70 //
kono
parents:
diff changeset
71 // Note that whether this function returns an error or not discloses secret
kono
parents:
diff changeset
72 // information. If an attacker can cause this function to run repeatedly and
kono
parents:
diff changeset
73 // learn whether each instance returned an error then they can decrypt and
kono
parents:
diff changeset
74 // forge signatures as if they had the private key. See
kono
parents:
diff changeset
75 // DecryptPKCS1v15SessionKey for a way of solving this problem.
kono
parents:
diff changeset
76 func DecryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) ([]byte, error) {
kono
parents:
diff changeset
77 if err := checkPub(&priv.PublicKey); err != nil {
kono
parents:
diff changeset
78 return nil, err
kono
parents:
diff changeset
79 }
kono
parents:
diff changeset
80 valid, out, index, err := decryptPKCS1v15(rand, priv, ciphertext)
kono
parents:
diff changeset
81 if err != nil {
kono
parents:
diff changeset
82 return nil, err
kono
parents:
diff changeset
83 }
kono
parents:
diff changeset
84 if valid == 0 {
kono
parents:
diff changeset
85 return nil, ErrDecryption
kono
parents:
diff changeset
86 }
kono
parents:
diff changeset
87 return out[index:], nil
kono
parents:
diff changeset
88 }
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 // DecryptPKCS1v15SessionKey decrypts a session key using RSA and the padding scheme from PKCS#1 v1.5.
kono
parents:
diff changeset
91 // If rand != nil, it uses RSA blinding to avoid timing side-channel attacks.
kono
parents:
diff changeset
92 // It returns an error if the ciphertext is the wrong length or if the
kono
parents:
diff changeset
93 // ciphertext is greater than the public modulus. Otherwise, no error is
kono
parents:
diff changeset
94 // returned. If the padding is valid, the resulting plaintext message is copied
kono
parents:
diff changeset
95 // into key. Otherwise, key is unchanged. These alternatives occur in constant
kono
parents:
diff changeset
96 // time. It is intended that the user of this function generate a random
kono
parents:
diff changeset
97 // session key beforehand and continue the protocol with the resulting value.
kono
parents:
diff changeset
98 // This will remove any possibility that an attacker can learn any information
kono
parents:
diff changeset
99 // about the plaintext.
kono
parents:
diff changeset
100 // See ``Chosen Ciphertext Attacks Against Protocols Based on the RSA
kono
parents:
diff changeset
101 // Encryption Standard PKCS #1'', Daniel Bleichenbacher, Advances in Cryptology
kono
parents:
diff changeset
102 // (Crypto '98).
kono
parents:
diff changeset
103 //
kono
parents:
diff changeset
104 // Note that if the session key is too small then it may be possible for an
kono
parents:
diff changeset
105 // attacker to brute-force it. If they can do that then they can learn whether
kono
parents:
diff changeset
106 // a random value was used (because it'll be different for the same ciphertext)
kono
parents:
diff changeset
107 // and thus whether the padding was correct. This defeats the point of this
kono
parents:
diff changeset
108 // function. Using at least a 16-byte key will protect against this attack.
kono
parents:
diff changeset
109 func DecryptPKCS1v15SessionKey(rand io.Reader, priv *PrivateKey, ciphertext []byte, key []byte) error {
kono
parents:
diff changeset
110 if err := checkPub(&priv.PublicKey); err != nil {
kono
parents:
diff changeset
111 return err
kono
parents:
diff changeset
112 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
113 k := priv.Size()
111
kono
parents:
diff changeset
114 if k-(len(key)+3+8) < 0 {
kono
parents:
diff changeset
115 return ErrDecryption
kono
parents:
diff changeset
116 }
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 valid, em, index, err := decryptPKCS1v15(rand, priv, ciphertext)
kono
parents:
diff changeset
119 if err != nil {
kono
parents:
diff changeset
120 return err
kono
parents:
diff changeset
121 }
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 if len(em) != k {
kono
parents:
diff changeset
124 // This should be impossible because decryptPKCS1v15 always
kono
parents:
diff changeset
125 // returns the full slice.
kono
parents:
diff changeset
126 return ErrDecryption
kono
parents:
diff changeset
127 }
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 valid &= subtle.ConstantTimeEq(int32(len(em)-index), int32(len(key)))
kono
parents:
diff changeset
130 subtle.ConstantTimeCopy(valid, key, em[len(em)-len(key):])
kono
parents:
diff changeset
131 return nil
kono
parents:
diff changeset
132 }
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 // decryptPKCS1v15 decrypts ciphertext using priv and blinds the operation if
kono
parents:
diff changeset
135 // rand is not nil. It returns one or zero in valid that indicates whether the
kono
parents:
diff changeset
136 // plaintext was correctly structured. In either case, the plaintext is
kono
parents:
diff changeset
137 // returned in em so that it may be read independently of whether it was valid
kono
parents:
diff changeset
138 // in order to maintain constant memory access patterns. If the plaintext was
kono
parents:
diff changeset
139 // valid then index contains the index of the original message in em.
kono
parents:
diff changeset
140 func decryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) (valid int, em []byte, index int, err error) {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
141 k := priv.Size()
111
kono
parents:
diff changeset
142 if k < 11 {
kono
parents:
diff changeset
143 err = ErrDecryption
kono
parents:
diff changeset
144 return
kono
parents:
diff changeset
145 }
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 c := new(big.Int).SetBytes(ciphertext)
kono
parents:
diff changeset
148 m, err := decrypt(rand, priv, c)
kono
parents:
diff changeset
149 if err != nil {
kono
parents:
diff changeset
150 return
kono
parents:
diff changeset
151 }
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 em = leftPad(m.Bytes(), k)
kono
parents:
diff changeset
154 firstByteIsZero := subtle.ConstantTimeByteEq(em[0], 0)
kono
parents:
diff changeset
155 secondByteIsTwo := subtle.ConstantTimeByteEq(em[1], 2)
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 // The remainder of the plaintext must be a string of non-zero random
kono
parents:
diff changeset
158 // octets, followed by a 0, followed by the message.
kono
parents:
diff changeset
159 // lookingForIndex: 1 iff we are still looking for the zero.
kono
parents:
diff changeset
160 // index: the offset of the first zero byte.
kono
parents:
diff changeset
161 lookingForIndex := 1
kono
parents:
diff changeset
162
kono
parents:
diff changeset
163 for i := 2; i < len(em); i++ {
kono
parents:
diff changeset
164 equals0 := subtle.ConstantTimeByteEq(em[i], 0)
kono
parents:
diff changeset
165 index = subtle.ConstantTimeSelect(lookingForIndex&equals0, i, index)
kono
parents:
diff changeset
166 lookingForIndex = subtle.ConstantTimeSelect(equals0, 0, lookingForIndex)
kono
parents:
diff changeset
167 }
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 // The PS padding must be at least 8 bytes long, and it starts two
kono
parents:
diff changeset
170 // bytes into em.
kono
parents:
diff changeset
171 validPS := subtle.ConstantTimeLessOrEq(2+8, index)
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 valid = firstByteIsZero & secondByteIsTwo & (^lookingForIndex & 1) & validPS
kono
parents:
diff changeset
174 index = subtle.ConstantTimeSelect(valid, index+1, 0)
kono
parents:
diff changeset
175 return valid, em, index, nil
kono
parents:
diff changeset
176 }
kono
parents:
diff changeset
177
kono
parents:
diff changeset
178 // nonZeroRandomBytes fills the given slice with non-zero random octets.
kono
parents:
diff changeset
179 func nonZeroRandomBytes(s []byte, rand io.Reader) (err error) {
kono
parents:
diff changeset
180 _, err = io.ReadFull(rand, s)
kono
parents:
diff changeset
181 if err != nil {
kono
parents:
diff changeset
182 return
kono
parents:
diff changeset
183 }
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 for i := 0; i < len(s); i++ {
kono
parents:
diff changeset
186 for s[i] == 0 {
kono
parents:
diff changeset
187 _, err = io.ReadFull(rand, s[i:i+1])
kono
parents:
diff changeset
188 if err != nil {
kono
parents:
diff changeset
189 return
kono
parents:
diff changeset
190 }
kono
parents:
diff changeset
191 // In tests, the PRNG may return all zeros so we do
kono
parents:
diff changeset
192 // this to break the loop.
kono
parents:
diff changeset
193 s[i] ^= 0x42
kono
parents:
diff changeset
194 }
kono
parents:
diff changeset
195 }
kono
parents:
diff changeset
196
kono
parents:
diff changeset
197 return
kono
parents:
diff changeset
198 }
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 // These are ASN1 DER structures:
kono
parents:
diff changeset
201 // DigestInfo ::= SEQUENCE {
kono
parents:
diff changeset
202 // digestAlgorithm AlgorithmIdentifier,
kono
parents:
diff changeset
203 // digest OCTET STRING
kono
parents:
diff changeset
204 // }
kono
parents:
diff changeset
205 // For performance, we don't use the generic ASN1 encoder. Rather, we
kono
parents:
diff changeset
206 // precompute a prefix of the digest value that makes a valid ASN1 DER string
kono
parents:
diff changeset
207 // with the correct contents.
kono
parents:
diff changeset
208 var hashPrefixes = map[crypto.Hash][]byte{
kono
parents:
diff changeset
209 crypto.MD5: {0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10},
kono
parents:
diff changeset
210 crypto.SHA1: {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14},
kono
parents:
diff changeset
211 crypto.SHA224: {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c},
kono
parents:
diff changeset
212 crypto.SHA256: {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20},
kono
parents:
diff changeset
213 crypto.SHA384: {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30},
kono
parents:
diff changeset
214 crypto.SHA512: {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40},
kono
parents:
diff changeset
215 crypto.MD5SHA1: {}, // A special TLS case which doesn't use an ASN1 prefix.
kono
parents:
diff changeset
216 crypto.RIPEMD160: {0x30, 0x20, 0x30, 0x08, 0x06, 0x06, 0x28, 0xcf, 0x06, 0x03, 0x00, 0x31, 0x04, 0x14},
kono
parents:
diff changeset
217 }
kono
parents:
diff changeset
218
kono
parents:
diff changeset
219 // SignPKCS1v15 calculates the signature of hashed using
kono
parents:
diff changeset
220 // RSASSA-PKCS1-V1_5-SIGN from RSA PKCS#1 v1.5. Note that hashed must
kono
parents:
diff changeset
221 // be the result of hashing the input message using the given hash
kono
parents:
diff changeset
222 // function. If hash is zero, hashed is signed directly. This isn't
kono
parents:
diff changeset
223 // advisable except for interoperability.
kono
parents:
diff changeset
224 //
kono
parents:
diff changeset
225 // If rand is not nil then RSA blinding will be used to avoid timing
kono
parents:
diff changeset
226 // side-channel attacks.
kono
parents:
diff changeset
227 //
kono
parents:
diff changeset
228 // This function is deterministic. Thus, if the set of possible
kono
parents:
diff changeset
229 // messages is small, an attacker may be able to build a map from
kono
parents:
diff changeset
230 // messages to signatures and identify the signed messages. As ever,
kono
parents:
diff changeset
231 // signatures provide authenticity, not confidentiality.
kono
parents:
diff changeset
232 func SignPKCS1v15(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte) ([]byte, error) {
kono
parents:
diff changeset
233 hashLen, prefix, err := pkcs1v15HashInfo(hash, len(hashed))
kono
parents:
diff changeset
234 if err != nil {
kono
parents:
diff changeset
235 return nil, err
kono
parents:
diff changeset
236 }
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 tLen := len(prefix) + hashLen
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
239 k := priv.Size()
111
kono
parents:
diff changeset
240 if k < tLen+11 {
kono
parents:
diff changeset
241 return nil, ErrMessageTooLong
kono
parents:
diff changeset
242 }
kono
parents:
diff changeset
243
kono
parents:
diff changeset
244 // EM = 0x00 || 0x01 || PS || 0x00 || T
kono
parents:
diff changeset
245 em := make([]byte, k)
kono
parents:
diff changeset
246 em[1] = 1
kono
parents:
diff changeset
247 for i := 2; i < k-tLen-1; i++ {
kono
parents:
diff changeset
248 em[i] = 0xff
kono
parents:
diff changeset
249 }
kono
parents:
diff changeset
250 copy(em[k-tLen:k-hashLen], prefix)
kono
parents:
diff changeset
251 copy(em[k-hashLen:k], hashed)
kono
parents:
diff changeset
252
kono
parents:
diff changeset
253 m := new(big.Int).SetBytes(em)
kono
parents:
diff changeset
254 c, err := decryptAndCheck(rand, priv, m)
kono
parents:
diff changeset
255 if err != nil {
kono
parents:
diff changeset
256 return nil, err
kono
parents:
diff changeset
257 }
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 copyWithLeftPad(em, c.Bytes())
kono
parents:
diff changeset
260 return em, nil
kono
parents:
diff changeset
261 }
kono
parents:
diff changeset
262
kono
parents:
diff changeset
263 // VerifyPKCS1v15 verifies an RSA PKCS#1 v1.5 signature.
kono
parents:
diff changeset
264 // hashed is the result of hashing the input message using the given hash
kono
parents:
diff changeset
265 // function and sig is the signature. A valid signature is indicated by
kono
parents:
diff changeset
266 // returning a nil error. If hash is zero then hashed is used directly. This
kono
parents:
diff changeset
267 // isn't advisable except for interoperability.
kono
parents:
diff changeset
268 func VerifyPKCS1v15(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte) error {
kono
parents:
diff changeset
269 hashLen, prefix, err := pkcs1v15HashInfo(hash, len(hashed))
kono
parents:
diff changeset
270 if err != nil {
kono
parents:
diff changeset
271 return err
kono
parents:
diff changeset
272 }
kono
parents:
diff changeset
273
kono
parents:
diff changeset
274 tLen := len(prefix) + hashLen
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
275 k := pub.Size()
111
kono
parents:
diff changeset
276 if k < tLen+11 {
kono
parents:
diff changeset
277 return ErrVerification
kono
parents:
diff changeset
278 }
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280 c := new(big.Int).SetBytes(sig)
kono
parents:
diff changeset
281 m := encrypt(new(big.Int), pub, c)
kono
parents:
diff changeset
282 em := leftPad(m.Bytes(), k)
kono
parents:
diff changeset
283 // EM = 0x00 || 0x01 || PS || 0x00 || T
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285 ok := subtle.ConstantTimeByteEq(em[0], 0)
kono
parents:
diff changeset
286 ok &= subtle.ConstantTimeByteEq(em[1], 1)
kono
parents:
diff changeset
287 ok &= subtle.ConstantTimeCompare(em[k-hashLen:k], hashed)
kono
parents:
diff changeset
288 ok &= subtle.ConstantTimeCompare(em[k-tLen:k-hashLen], prefix)
kono
parents:
diff changeset
289 ok &= subtle.ConstantTimeByteEq(em[k-tLen-1], 0)
kono
parents:
diff changeset
290
kono
parents:
diff changeset
291 for i := 2; i < k-tLen-1; i++ {
kono
parents:
diff changeset
292 ok &= subtle.ConstantTimeByteEq(em[i], 0xff)
kono
parents:
diff changeset
293 }
kono
parents:
diff changeset
294
kono
parents:
diff changeset
295 if ok != 1 {
kono
parents:
diff changeset
296 return ErrVerification
kono
parents:
diff changeset
297 }
kono
parents:
diff changeset
298
kono
parents:
diff changeset
299 return nil
kono
parents:
diff changeset
300 }
kono
parents:
diff changeset
301
kono
parents:
diff changeset
302 func pkcs1v15HashInfo(hash crypto.Hash, inLen int) (hashLen int, prefix []byte, err error) {
kono
parents:
diff changeset
303 // Special case: crypto.Hash(0) is used to indicate that the data is
kono
parents:
diff changeset
304 // signed directly.
kono
parents:
diff changeset
305 if hash == 0 {
kono
parents:
diff changeset
306 return inLen, nil, nil
kono
parents:
diff changeset
307 }
kono
parents:
diff changeset
308
kono
parents:
diff changeset
309 hashLen = hash.Size()
kono
parents:
diff changeset
310 if inLen != hashLen {
kono
parents:
diff changeset
311 return 0, nil, errors.New("crypto/rsa: input must be hashed message")
kono
parents:
diff changeset
312 }
kono
parents:
diff changeset
313 prefix, ok := hashPrefixes[hash]
kono
parents:
diff changeset
314 if !ok {
kono
parents:
diff changeset
315 return 0, nil, errors.New("crypto/rsa: unsupported hash function")
kono
parents:
diff changeset
316 }
kono
parents:
diff changeset
317 return
kono
parents:
diff changeset
318 }
kono
parents:
diff changeset
319
kono
parents:
diff changeset
320 // copyWithLeftPad copies src to the end of dest, padding with zero bytes as
kono
parents:
diff changeset
321 // needed.
kono
parents:
diff changeset
322 func copyWithLeftPad(dest, src []byte) {
kono
parents:
diff changeset
323 numPaddingBytes := len(dest) - len(src)
kono
parents:
diff changeset
324 for i := 0; i < numPaddingBytes; i++ {
kono
parents:
diff changeset
325 dest[i] = 0
kono
parents:
diff changeset
326 }
kono
parents:
diff changeset
327 copy(dest[numPaddingBytes:], src)
kono
parents:
diff changeset
328 }