~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/crypto/rsa/pkcs1v15.go

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2014-11-18 15:12:26 UTC
  • mfrom: (14.2.12 vivid-proposed)
  • Revision ID: package-import@ubuntu.com-20141118151226-zug7vn93mn3dtiz3
Tags: 2:1.3.2-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - Support co-installability with gccgo-go tool:
    - d/rules,golang-go.install: Rename bin/go -> bin/golang-go
    - d/golang-go.{postinst,prerm}: Install/remove /usr/bin/go using
      alternatives.
  - d/copyright: Amendments for full compiliance with copyright format.
  - d/control: Demote golang-go.tools to Suggests to support Ubuntu MIR.
  - dropped patches (now upstream):
    - d/p/issue27650045_40001_50001.diff
    - d/p/issue28050043_60001_70001.diff
    - d/p/issue54790044_100001_110001.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
        if err := checkPub(&priv.PublicKey); err != nil {
54
54
                return nil, err
55
55
        }
56
 
        valid, out, err := decryptPKCS1v15(rand, priv, ciphertext)
57
 
        if err == nil && valid == 0 {
58
 
                err = ErrDecryption
59
 
        }
60
 
 
 
56
        valid, out, index, err := decryptPKCS1v15(rand, priv, ciphertext)
 
57
        if err != nil {
 
58
                return
 
59
        }
 
60
        if valid == 0 {
 
61
                return nil, ErrDecryption
 
62
        }
 
63
        out = out[index:]
61
64
        return
62
65
}
63
66
 
80
83
        }
81
84
        k := (priv.N.BitLen() + 7) / 8
82
85
        if k-(len(key)+3+8) < 0 {
83
 
                err = ErrDecryption
84
 
                return
 
86
                return ErrDecryption
85
87
        }
86
88
 
87
 
        valid, msg, err := decryptPKCS1v15(rand, priv, ciphertext)
 
89
        valid, em, index, err := decryptPKCS1v15(rand, priv, ciphertext)
88
90
        if err != nil {
89
91
                return
90
92
        }
91
93
 
92
 
        valid &= subtle.ConstantTimeEq(int32(len(msg)), int32(len(key)))
93
 
        subtle.ConstantTimeCopy(valid, key, msg)
 
94
        if len(em) != k {
 
95
                // This should be impossible because decryptPKCS1v15 always
 
96
                // returns the full slice.
 
97
                return ErrDecryption
 
98
        }
 
99
 
 
100
        valid &= subtle.ConstantTimeEq(int32(len(em)-index), int32(len(key)))
 
101
        subtle.ConstantTimeCopy(valid, key, em[len(em)-len(key):])
94
102
        return
95
103
}
96
104
 
97
 
func decryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) (valid int, msg []byte, err error) {
 
105
// decryptPKCS1v15 decrypts ciphertext using priv and blinds the operation if
 
106
// rand is not nil. It returns one or zero in valid that indicates whether the
 
107
// plaintext was correctly structured. In either case, the plaintext is
 
108
// returned in em so that it may be read independently of whether it was valid
 
109
// in order to maintain constant memory access patterns. If the plaintext was
 
110
// valid then index contains the index of the original message in em.
 
111
func decryptPKCS1v15(rand io.Reader, priv *PrivateKey, ciphertext []byte) (valid int, em []byte, index int, err error) {
98
112
        k := (priv.N.BitLen() + 7) / 8
99
113
        if k < 11 {
100
114
                err = ErrDecryption
107
121
                return
108
122
        }
109
123
 
110
 
        em := leftPad(m.Bytes(), k)
 
124
        em = leftPad(m.Bytes(), k)
111
125
        firstByteIsZero := subtle.ConstantTimeByteEq(em[0], 0)
112
126
        secondByteIsTwo := subtle.ConstantTimeByteEq(em[1], 2)
113
127
 
115
129
        // octets, followed by a 0, followed by the message.
116
130
        //   lookingForIndex: 1 iff we are still looking for the zero.
117
131
        //   index: the offset of the first zero byte.
118
 
        var lookingForIndex, index int
119
 
        lookingForIndex = 1
 
132
        lookingForIndex := 1
120
133
 
121
134
        for i := 2; i < len(em); i++ {
122
135
                equals0 := subtle.ConstantTimeByteEq(em[i], 0)
129
142
        validPS := subtle.ConstantTimeLessOrEq(2+8, index)
130
143
 
131
144
        valid = firstByteIsZero & secondByteIsTwo & (^lookingForIndex & 1) & validPS
132
 
        msg = em[index+1:]
133
 
        return
 
145
        index = subtle.ConstantTimeSelect(valid, index+1, 0)
 
146
        return valid, em, index, nil
134
147
}
135
148
 
136
149
// nonZeroRandomBytes fills the given slice with non-zero random octets.
176
189
 
177
190
// SignPKCS1v15 calculates the signature of hashed using RSASSA-PKCS1-V1_5-SIGN from RSA PKCS#1 v1.5.
178
191
// Note that hashed must be the result of hashing the input message using the
179
 
// given hash function.
 
192
// given hash function. If hash is zero, hashed is signed directly. This isn't
 
193
// advisable except for interoperability.
180
194
func SignPKCS1v15(rand io.Reader, priv *PrivateKey, hash crypto.Hash, hashed []byte) (s []byte, err error) {
181
195
        hashLen, prefix, err := pkcs1v15HashInfo(hash, len(hashed))
182
196
        if err != nil {
212
226
// VerifyPKCS1v15 verifies an RSA PKCS#1 v1.5 signature.
213
227
// hashed is the result of hashing the input message using the given hash
214
228
// function and sig is the signature. A valid signature is indicated by
215
 
// returning a nil error.
 
229
// returning a nil error. If hash is zero then hashed is used directly. This
 
230
// isn't advisable except for interoperability.
216
231
func VerifyPKCS1v15(pub *PublicKey, hash crypto.Hash, hashed []byte, sig []byte) (err error) {
217
232
        hashLen, prefix, err := pkcs1v15HashInfo(hash, len(hashed))
218
233
        if err != nil {
249
264
}
250
265
 
251
266
func pkcs1v15HashInfo(hash crypto.Hash, inLen int) (hashLen int, prefix []byte, err error) {
 
267
        // Special case: crypto.Hash(0) is used to indicate that the data is
 
268
        // signed directly.
 
269
        if hash == 0 {
 
270
                return inLen, nil, nil
 
271
        }
 
272
 
252
273
        hashLen = hash.Size()
253
274
        if inLen != hashLen {
254
275
                return 0, nil, errors.New("crypto/rsa: input must be hashed message")