本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

647 lines
19KB

  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package rsa implements RSA encryption as specified in PKCS#1.
  5. //
  6. // RSA is a single, fundamental operation that is used in this package to
  7. // implement either public-key encryption or public-key signatures.
  8. //
  9. // The original specification for encryption and signatures with RSA is PKCS#1
  10. // and the terms "RSA encryption" and "RSA signatures" by default refer to
  11. // PKCS#1 version 1.5. However, that specification has flaws and new designs
  12. // should use version two, usually called by just OAEP and PSS, where
  13. // possible.
  14. //
  15. // Two sets of interfaces are included in this package. When a more abstract
  16. // interface isn't neccessary, there are functions for encrypting/decrypting
  17. // with v1.5/OAEP and signing/verifying with v1.5/PSS. If one needs to abstract
  18. // over the public-key primitive, the PrivateKey struct implements the
  19. // Decrypter and Signer interfaces from the crypto package.
  20. package rsa
  21. import (
  22. "crypto"
  23. "crypto/rand"
  24. "crypto/subtle"
  25. "errors"
  26. "hash"
  27. "io"
  28. "math/big"
  29. )
  30. var bigZero = big.NewInt(0)
  31. var bigOne = big.NewInt(1)
  32. // A PublicKey represents the public part of an RSA key.
  33. type PublicKey struct {
  34. N *big.Int // modulus
  35. E int64 // public exponent
  36. }
  37. // OAEPOptions is an interface for passing options to OAEP decryption using the
  38. // crypto.Decrypter interface.
  39. type OAEPOptions struct {
  40. // Hash is the hash function that will be used when generating the mask.
  41. Hash crypto.Hash
  42. // Label is an arbitrary byte string that must be equal to the value
  43. // used when encrypting.
  44. Label []byte
  45. }
  46. var (
  47. errPublicModulus = errors.New("crypto/rsa: missing public modulus")
  48. errPublicExponentSmall = errors.New("crypto/rsa: public exponent too small")
  49. errPublicExponentLarge = errors.New("crypto/rsa: public exponent too large")
  50. )
  51. // checkPub sanity checks the public key before we use it.
  52. // We require pub.E to fit into a 32-bit integer so that we
  53. // do not have different behavior depending on whether
  54. // int is 32 or 64 bits. See also
  55. // http://www.imperialviolet.org/2012/03/16/rsae.html.
  56. func checkPub(pub *PublicKey) error {
  57. if pub.N == nil {
  58. return errPublicModulus
  59. }
  60. if pub.E < 2 {
  61. return errPublicExponentSmall
  62. }
  63. if pub.E > 1<<63-1 {
  64. return errPublicExponentLarge
  65. }
  66. return nil
  67. }
  68. // A PrivateKey represents an RSA key
  69. type PrivateKey struct {
  70. PublicKey // public part.
  71. D *big.Int // private exponent
  72. Primes []*big.Int // prime factors of N, has >= 2 elements.
  73. // Precomputed contains precomputed values that speed up private
  74. // operations, if available.
  75. Precomputed PrecomputedValues
  76. }
  77. // Public returns the public key corresponding to priv.
  78. func (priv *PrivateKey) Public() crypto.PublicKey {
  79. return &priv.PublicKey
  80. }
  81. // Sign signs msg with priv, reading randomness from rand. If opts is a
  82. // *PSSOptions then the PSS algorithm will be used, otherwise PKCS#1 v1.5 will
  83. // be used. This method is intended to support keys where the private part is
  84. // kept in, for example, a hardware module. Common uses should use the Sign*
  85. // functions in this package.
  86. func (priv *PrivateKey) Sign(rand io.Reader, msg []byte, opts crypto.SignerOpts) ([]byte, error) {
  87. if pssOpts, ok := opts.(*PSSOptions); ok {
  88. return SignPSS(rand, priv, pssOpts.Hash, msg, pssOpts)
  89. }
  90. return SignPKCS1v15(rand, priv, opts.HashFunc(), msg)
  91. }
  92. // Decrypt decrypts ciphertext with priv. If opts is nil or of type
  93. // *PKCS1v15DecryptOptions then PKCS#1 v1.5 decryption is performed. Otherwise
  94. // opts must have type *OAEPOptions and OAEP decryption is done.
  95. func (priv *PrivateKey) Decrypt(rand io.Reader, ciphertext []byte, opts crypto.DecrypterOpts) (plaintext []byte, err error) {
  96. if opts == nil {
  97. return DecryptPKCS1v15(rand, priv, ciphertext)
  98. }
  99. switch opts := opts.(type) {
  100. case *OAEPOptions:
  101. return DecryptOAEP(opts.Hash.New(), rand, priv, ciphertext, opts.Label)
  102. case *PKCS1v15DecryptOptions:
  103. if l := opts.SessionKeyLen; l > 0 {
  104. plaintext = make([]byte, l)
  105. if _, err := io.ReadFull(rand, plaintext); err != nil {
  106. return nil, err
  107. }
  108. if err := DecryptPKCS1v15SessionKey(rand, priv, ciphertext, plaintext); err != nil {
  109. return nil, err
  110. }
  111. return plaintext, nil
  112. } else {
  113. return DecryptPKCS1v15(rand, priv, ciphertext)
  114. }
  115. default:
  116. return nil, errors.New("crypto/rsa: invalid options for Decrypt")
  117. }
  118. }
  119. type PrecomputedValues struct {
  120. Dp, Dq *big.Int // D mod (P-1) (or mod Q-1)
  121. Qinv *big.Int // Q^-1 mod P
  122. // CRTValues is used for the 3rd and subsequent primes. Due to a
  123. // historical accident, the CRT for the first two primes is handled
  124. // differently in PKCS#1 and interoperability is sufficiently
  125. // important that we mirror this.
  126. CRTValues []CRTValue
  127. }
  128. // CRTValue contains the precomputed Chinese remainder theorem values.
  129. type CRTValue struct {
  130. Exp *big.Int // D mod (prime-1).
  131. Coeff *big.Int // R·Coeff ≡ 1 mod Prime.
  132. R *big.Int // product of primes prior to this (inc p and q).
  133. }
  134. // Validate performs basic sanity checks on the key.
  135. // It returns nil if the key is valid, or else an error describing a problem.
  136. func (priv *PrivateKey) Validate() error {
  137. if err := checkPub(&priv.PublicKey); err != nil {
  138. return err
  139. }
  140. // Check that Πprimes == n.
  141. modulus := new(big.Int).Set(bigOne)
  142. for _, prime := range priv.Primes {
  143. // Any primes ≤ 1 will cause divide-by-zero panics later.
  144. if prime.Cmp(bigOne) <= 0 {
  145. return errors.New("crypto/rsa: invalid prime value")
  146. }
  147. modulus.Mul(modulus, prime)
  148. }
  149. if modulus.Cmp(priv.N) != 0 {
  150. return errors.New("crypto/rsa: invalid modulus")
  151. }
  152. // Check that de ≡ 1 mod p-1, for each prime.
  153. // This implies that e is coprime to each p-1 as e has a multiplicative
  154. // inverse. Therefore e is coprime to lcm(p-1,q-1,r-1,...) =
  155. // exponent(ℤ/nℤ). It also implies that a^de ≡ a mod p as a^(p-1) ≡ 1
  156. // mod p. Thus a^de ≡ a mod n for all a coprime to n, as required.
  157. congruence := new(big.Int)
  158. de := new(big.Int).SetInt64(int64(priv.E))
  159. de.Mul(de, priv.D)
  160. for _, prime := range priv.Primes {
  161. pminus1 := new(big.Int).Sub(prime, bigOne)
  162. congruence.Mod(de, pminus1)
  163. if congruence.Cmp(bigOne) != 0 {
  164. return errors.New("crypto/rsa: invalid exponents")
  165. }
  166. }
  167. return nil
  168. }
  169. // GenerateKey generates an RSA keypair of the given bit size using the
  170. // random source random (for example, crypto/rand.Reader).
  171. func GenerateKey(random io.Reader, bits int) (priv *PrivateKey, err error) {
  172. return GenerateMultiPrimeKey(random, 2, bits)
  173. }
  174. // GenerateMultiPrimeKey generates a multi-prime RSA keypair of the given bit
  175. // size and the given random source, as suggested in [1]. Although the public
  176. // keys are compatible (actually, indistinguishable) from the 2-prime case,
  177. // the private keys are not. Thus it may not be possible to export multi-prime
  178. // private keys in certain formats or to subsequently import them into other
  179. // code.
  180. //
  181. // Table 1 in [2] suggests maximum numbers of primes for a given size.
  182. //
  183. // [1] US patent 4405829 (1972, expired)
  184. // [2] http://www.cacr.math.uwaterloo.ca/techreports/2006/cacr2006-16.pdf
  185. func GenerateMultiPrimeKey(random io.Reader, nprimes int, bits int) (priv *PrivateKey, err error) {
  186. priv = new(PrivateKey)
  187. priv.E = 65537
  188. if nprimes < 2 {
  189. return nil, errors.New("crypto/rsa: GenerateMultiPrimeKey: nprimes must be >= 2")
  190. }
  191. primes := make([]*big.Int, nprimes)
  192. NextSetOfPrimes:
  193. for {
  194. todo := bits
  195. // crypto/rand should set the top two bits in each prime.
  196. // Thus each prime has the form
  197. // p_i = 2^bitlen(p_i) × 0.11... (in base 2).
  198. // And the product is:
  199. // P = 2^todo × α
  200. // where α is the product of nprimes numbers of the form 0.11...
  201. //
  202. // If α < 1/2 (which can happen for nprimes > 2), we need to
  203. // shift todo to compensate for lost bits: the mean value of 0.11...
  204. // is 7/8, so todo + shift - nprimes * log2(7/8) ~= bits - 1/2
  205. // will give good results.
  206. if nprimes >= 7 {
  207. todo += (nprimes - 2) / 5
  208. }
  209. for i := 0; i < nprimes; i++ {
  210. primes[i], err = rand.Prime(random, todo/(nprimes-i))
  211. if err != nil {
  212. return nil, err
  213. }
  214. todo -= primes[i].BitLen()
  215. }
  216. // Make sure that primes is pairwise unequal.
  217. for i, prime := range primes {
  218. for j := 0; j < i; j++ {
  219. if prime.Cmp(primes[j]) == 0 {
  220. continue NextSetOfPrimes
  221. }
  222. }
  223. }
  224. n := new(big.Int).Set(bigOne)
  225. totient := new(big.Int).Set(bigOne)
  226. pminus1 := new(big.Int)
  227. for _, prime := range primes {
  228. n.Mul(n, prime)
  229. pminus1.Sub(prime, bigOne)
  230. totient.Mul(totient, pminus1)
  231. }
  232. if n.BitLen() != bits {
  233. // This should never happen for nprimes == 2 because
  234. // crypto/rand should set the top two bits in each prime.
  235. // For nprimes > 2 we hope it does not happen often.
  236. continue NextSetOfPrimes
  237. }
  238. g := new(big.Int)
  239. priv.D = new(big.Int)
  240. y := new(big.Int)
  241. e := big.NewInt(int64(priv.E))
  242. g.GCD(priv.D, y, e, totient)
  243. if g.Cmp(bigOne) == 0 {
  244. if priv.D.Sign() < 0 {
  245. priv.D.Add(priv.D, totient)
  246. }
  247. priv.Primes = primes
  248. priv.N = n
  249. break
  250. }
  251. }
  252. priv.Precompute()
  253. return
  254. }
  255. // incCounter increments a four byte, big-endian counter.
  256. func incCounter(c *[4]byte) {
  257. if c[3]++; c[3] != 0 {
  258. return
  259. }
  260. if c[2]++; c[2] != 0 {
  261. return
  262. }
  263. if c[1]++; c[1] != 0 {
  264. return
  265. }
  266. c[0]++
  267. }
  268. // mgf1XOR XORs the bytes in out with a mask generated using the MGF1 function
  269. // specified in PKCS#1 v2.1.
  270. func mgf1XOR(out []byte, hash hash.Hash, seed []byte) {
  271. var counter [4]byte
  272. var digest []byte
  273. done := 0
  274. for done < len(out) {
  275. hash.Write(seed)
  276. hash.Write(counter[0:4])
  277. digest = hash.Sum(digest[:0])
  278. hash.Reset()
  279. for i := 0; i < len(digest) && done < len(out); i++ {
  280. out[done] ^= digest[i]
  281. done++
  282. }
  283. incCounter(&counter)
  284. }
  285. }
  286. // ErrMessageTooLong is returned when attempting to encrypt a message which is
  287. // too large for the size of the public key.
  288. var ErrMessageTooLong = errors.New("crypto/rsa: message too long for RSA public key size")
  289. func encrypt(c *big.Int, pub *PublicKey, m *big.Int) *big.Int {
  290. e := big.NewInt(int64(pub.E))
  291. c.Exp(m, e, pub.N)
  292. return c
  293. }
  294. // EncryptOAEP encrypts the given message with RSA-OAEP.
  295. //
  296. // OAEP is parameterised by a hash function that is used as a random oracle.
  297. // Encryption and decryption of a given message must use the same hash function
  298. // and sha256.New() is a reasonable choice.
  299. //
  300. // The random parameter is used as a source of entropy to ensure that
  301. // encrypting the same message twice doesn't result in the same ciphertext.
  302. //
  303. // The label parameter may contain arbitrary data that will not be encrypted,
  304. // but which gives important context to the message. For example, if a given
  305. // public key is used to decrypt two types of messages then distinct label
  306. // values could be used to ensure that a ciphertext for one purpose cannot be
  307. // used for another by an attacker. If not required it can be empty.
  308. //
  309. // The message must be no longer than the length of the public modulus less
  310. // twice the hash length plus 2.
  311. func EncryptOAEP(hash hash.Hash, random io.Reader, pub *PublicKey, msg []byte, label []byte) (out []byte, err error) {
  312. if err := checkPub(pub); err != nil {
  313. return nil, err
  314. }
  315. hash.Reset()
  316. k := (pub.N.BitLen() + 7) / 8
  317. if len(msg) > k-2*hash.Size()-2 {
  318. err = ErrMessageTooLong
  319. return
  320. }
  321. hash.Write(label)
  322. lHash := hash.Sum(nil)
  323. hash.Reset()
  324. em := make([]byte, k)
  325. seed := em[1 : 1+hash.Size()]
  326. db := em[1+hash.Size():]
  327. copy(db[0:hash.Size()], lHash)
  328. db[len(db)-len(msg)-1] = 1
  329. copy(db[len(db)-len(msg):], msg)
  330. _, err = io.ReadFull(random, seed)
  331. if err != nil {
  332. return
  333. }
  334. mgf1XOR(db, hash, seed)
  335. mgf1XOR(seed, hash, db)
  336. m := new(big.Int)
  337. m.SetBytes(em)
  338. c := encrypt(new(big.Int), pub, m)
  339. out = c.Bytes()
  340. if len(out) < k {
  341. // If the output is too small, we need to left-pad with zeros.
  342. t := make([]byte, k)
  343. copy(t[k-len(out):], out)
  344. out = t
  345. }
  346. return
  347. }
  348. // ErrDecryption represents a failure to decrypt a message.
  349. // It is deliberately vague to avoid adaptive attacks.
  350. var ErrDecryption = errors.New("crypto/rsa: decryption error")
  351. // ErrVerification represents a failure to verify a signature.
  352. // It is deliberately vague to avoid adaptive attacks.
  353. var ErrVerification = errors.New("crypto/rsa: verification error")
  354. // modInverse returns ia, the inverse of a in the multiplicative group of prime
  355. // order n. It requires that a be a member of the group (i.e. less than n).
  356. func modInverse(a, n *big.Int) (ia *big.Int, ok bool) {
  357. g := new(big.Int)
  358. x := new(big.Int)
  359. y := new(big.Int)
  360. g.GCD(x, y, a, n)
  361. if g.Cmp(bigOne) != 0 {
  362. // In this case, a and n aren't coprime and we cannot calculate
  363. // the inverse. This happens because the values of n are nearly
  364. // prime (being the product of two primes) rather than truly
  365. // prime.
  366. return
  367. }
  368. if x.Cmp(bigOne) < 0 {
  369. // 0 is not the multiplicative inverse of any element so, if x
  370. // < 1, then x is negative.
  371. x.Add(x, n)
  372. }
  373. return x, true
  374. }
  375. // Precompute performs some calculations that speed up private key operations
  376. // in the future.
  377. func (priv *PrivateKey) Precompute() {
  378. if priv.Precomputed.Dp != nil {
  379. return
  380. }
  381. priv.Precomputed.Dp = new(big.Int).Sub(priv.Primes[0], bigOne)
  382. priv.Precomputed.Dp.Mod(priv.D, priv.Precomputed.Dp)
  383. priv.Precomputed.Dq = new(big.Int).Sub(priv.Primes[1], bigOne)
  384. priv.Precomputed.Dq.Mod(priv.D, priv.Precomputed.Dq)
  385. priv.Precomputed.Qinv = new(big.Int).ModInverse(priv.Primes[1], priv.Primes[0])
  386. r := new(big.Int).Mul(priv.Primes[0], priv.Primes[1])
  387. priv.Precomputed.CRTValues = make([]CRTValue, len(priv.Primes)-2)
  388. for i := 2; i < len(priv.Primes); i++ {
  389. prime := priv.Primes[i]
  390. values := &priv.Precomputed.CRTValues[i-2]
  391. values.Exp = new(big.Int).Sub(prime, bigOne)
  392. values.Exp.Mod(priv.D, values.Exp)
  393. values.R = new(big.Int).Set(r)
  394. values.Coeff = new(big.Int).ModInverse(r, prime)
  395. r.Mul(r, prime)
  396. }
  397. }
  398. // decrypt performs an RSA decryption, resulting in a plaintext integer. If a
  399. // random source is given, RSA blinding is used.
  400. func decrypt(random io.Reader, priv *PrivateKey, c *big.Int) (m *big.Int, err error) {
  401. // TODO(agl): can we get away with reusing blinds?
  402. if c.Cmp(priv.N) > 0 {
  403. err = ErrDecryption
  404. return
  405. }
  406. var ir *big.Int
  407. if random != nil {
  408. // Blinding enabled. Blinding involves multiplying c by r^e.
  409. // Then the decryption operation performs (m^e * r^e)^d mod n
  410. // which equals mr mod n. The factor of r can then be removed
  411. // by multiplying by the multiplicative inverse of r.
  412. var r *big.Int
  413. for {
  414. r, err = rand.Int(random, priv.N)
  415. if err != nil {
  416. return
  417. }
  418. if r.Cmp(bigZero) == 0 {
  419. r = bigOne
  420. }
  421. var ok bool
  422. ir, ok = modInverse(r, priv.N)
  423. if ok {
  424. break
  425. }
  426. }
  427. bigE := big.NewInt(int64(priv.E))
  428. rpowe := new(big.Int).Exp(r, bigE, priv.N)
  429. cCopy := new(big.Int).Set(c)
  430. cCopy.Mul(cCopy, rpowe)
  431. cCopy.Mod(cCopy, priv.N)
  432. c = cCopy
  433. }
  434. if priv.Precomputed.Dp == nil {
  435. m = new(big.Int).Exp(c, priv.D, priv.N)
  436. } else {
  437. // We have the precalculated values needed for the CRT.
  438. m = new(big.Int).Exp(c, priv.Precomputed.Dp, priv.Primes[0])
  439. m2 := new(big.Int).Exp(c, priv.Precomputed.Dq, priv.Primes[1])
  440. m.Sub(m, m2)
  441. if m.Sign() < 0 {
  442. m.Add(m, priv.Primes[0])
  443. }
  444. m.Mul(m, priv.Precomputed.Qinv)
  445. m.Mod(m, priv.Primes[0])
  446. m.Mul(m, priv.Primes[1])
  447. m.Add(m, m2)
  448. for i, values := range priv.Precomputed.CRTValues {
  449. prime := priv.Primes[2+i]
  450. m2.Exp(c, values.Exp, prime)
  451. m2.Sub(m2, m)
  452. m2.Mul(m2, values.Coeff)
  453. m2.Mod(m2, prime)
  454. if m2.Sign() < 0 {
  455. m2.Add(m2, prime)
  456. }
  457. m2.Mul(m2, values.R)
  458. m.Add(m, m2)
  459. }
  460. }
  461. if ir != nil {
  462. // Unblind.
  463. m.Mul(m, ir)
  464. m.Mod(m, priv.N)
  465. }
  466. return
  467. }
  468. func decryptAndCheck(random io.Reader, priv *PrivateKey, c *big.Int) (m *big.Int, err error) {
  469. m, err = decrypt(random, priv, c)
  470. if err != nil {
  471. return nil, err
  472. }
  473. // In order to defend against errors in the CRT computation, m^e is
  474. // calculated, which should match the original ciphertext.
  475. check := encrypt(new(big.Int), &priv.PublicKey, m)
  476. if c.Cmp(check) != 0 {
  477. return nil, errors.New("rsa: internal error")
  478. }
  479. return m, nil
  480. }
  481. // DecryptOAEP decrypts ciphertext using RSA-OAEP.
  482. // OAEP is parameterised by a hash function that is used as a random oracle.
  483. // Encryption and decryption of a given message must use the same hash function
  484. // and sha256.New() is a reasonable choice.
  485. //
  486. // The random parameter, if not nil, is used to blind the private-key operation
  487. // and avoid timing side-channel attacks. Blinding is purely internal to this
  488. // function – the random data need not match that used when encrypting.
  489. //
  490. // The label parameter must match the value given when encrypting. See
  491. // EncryptOAEP for details.
  492. func DecryptOAEP(hash hash.Hash, random io.Reader, priv *PrivateKey, ciphertext []byte, label []byte) (msg []byte, err error) {
  493. if err := checkPub(&priv.PublicKey); err != nil {
  494. return nil, err
  495. }
  496. k := (priv.N.BitLen() + 7) / 8
  497. if len(ciphertext) > k ||
  498. k < hash.Size()*2+2 {
  499. err = ErrDecryption
  500. return
  501. }
  502. c := new(big.Int).SetBytes(ciphertext)
  503. m, err := decrypt(random, priv, c)
  504. if err != nil {
  505. return
  506. }
  507. hash.Write(label)
  508. lHash := hash.Sum(nil)
  509. hash.Reset()
  510. // Converting the plaintext number to bytes will strip any
  511. // leading zeros so we may have to left pad. We do this unconditionally
  512. // to avoid leaking timing information. (Although we still probably
  513. // leak the number of leading zeros. It's not clear that we can do
  514. // anything about this.)
  515. em := leftPad(m.Bytes(), k)
  516. firstByteIsZero := subtle.ConstantTimeByteEq(em[0], 0)
  517. seed := em[1 : hash.Size()+1]
  518. db := em[hash.Size()+1:]
  519. mgf1XOR(seed, hash, db)
  520. mgf1XOR(db, hash, seed)
  521. lHash2 := db[0:hash.Size()]
  522. // We have to validate the plaintext in constant time in order to avoid
  523. // attacks like: J. Manger. A Chosen Ciphertext Attack on RSA Optimal
  524. // Asymmetric Encryption Padding (OAEP) as Standardized in PKCS #1
  525. // v2.0. In J. Kilian, editor, Advances in Cryptology.
  526. lHash2Good := subtle.ConstantTimeCompare(lHash, lHash2)
  527. // The remainder of the plaintext must be zero or more 0x00, followed
  528. // by 0x01, followed by the message.
  529. // lookingForIndex: 1 iff we are still looking for the 0x01
  530. // index: the offset of the first 0x01 byte
  531. // invalid: 1 iff we saw a non-zero byte before the 0x01.
  532. var lookingForIndex, index, invalid int
  533. lookingForIndex = 1
  534. rest := db[hash.Size():]
  535. for i := 0; i < len(rest); i++ {
  536. equals0 := subtle.ConstantTimeByteEq(rest[i], 0)
  537. equals1 := subtle.ConstantTimeByteEq(rest[i], 1)
  538. index = subtle.ConstantTimeSelect(lookingForIndex&equals1, i, index)
  539. lookingForIndex = subtle.ConstantTimeSelect(equals1, 0, lookingForIndex)
  540. invalid = subtle.ConstantTimeSelect(lookingForIndex&^equals0, 1, invalid)
  541. }
  542. if firstByteIsZero&lHash2Good&^invalid&^lookingForIndex != 1 {
  543. err = ErrDecryption
  544. return
  545. }
  546. msg = rest[index+1:]
  547. return
  548. }
  549. // leftPad returns a new slice of length size. The contents of input are right
  550. // aligned in the new slice.
  551. func leftPad(input []byte, size int) (out []byte) {
  552. n := len(input)
  553. if n > size {
  554. n = size
  555. }
  556. out = make([]byte, size)
  557. copy(out[len(out)-n:], input)
  558. return
  559. }
上海开阖软件有限公司 沪ICP备12045867号-1