~ubuntu-branches/ubuntu/utopic/golang/utopic

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
        if err != nil {
22
22
                t.Errorf("failed to generate key")
23
23
        }
 
24
        if bits := priv.N.BitLen(); bits != size {
 
25
                t.Errorf("key too short (%d vs %d)", bits, size)
 
26
        }
24
27
        testKeyBasics(t, priv)
25
28
}
26
29
 
27
30
func Test3PrimeKeyGeneration(t *testing.T) {
 
31
        size := 768
28
32
        if testing.Short() {
29
 
                return
 
33
                size = 256
30
34
        }
31
35
 
32
 
        size := 768
33
36
        priv, err := GenerateMultiPrimeKey(rand.Reader, 3, size)
34
37
        if err != nil {
35
38
                t.Errorf("failed to generate key")
38
41
}
39
42
 
40
43
func Test4PrimeKeyGeneration(t *testing.T) {
 
44
        size := 768
41
45
        if testing.Short() {
42
 
                return
 
46
                size = 256
43
47
        }
44
48
 
45
 
        size := 768
46
49
        priv, err := GenerateMultiPrimeKey(rand.Reader, 4, size)
47
50
        if err != nil {
48
51
                t.Errorf("failed to generate key")
50
53
        testKeyBasics(t, priv)
51
54
}
52
55
 
 
56
func TestNPrimeKeyGeneration(t *testing.T) {
 
57
        primeSize := 64
 
58
        maxN := 24
 
59
        if testing.Short() {
 
60
                primeSize = 16
 
61
                maxN = 16
 
62
        }
 
63
        // Test that generation of N-prime keys works for N > 4.
 
64
        for n := 5; n < maxN; n++ {
 
65
                priv, err := GenerateMultiPrimeKey(rand.Reader, n, 64+n*primeSize)
 
66
                if err == nil {
 
67
                        testKeyBasics(t, priv)
 
68
                } else {
 
69
                        t.Errorf("failed to generate %d-prime key", n)
 
70
                }
 
71
        }
 
72
}
 
73
 
 
74
func TestGnuTLSKey(t *testing.T) {
 
75
        // This is a key generated by `certtool --generate-privkey --bits 128`.
 
76
        // It's such that de ≢ 1 mod φ(n), but is congruent mod the order of
 
77
        // the group.
 
78
        priv := &PrivateKey{
 
79
                PublicKey: PublicKey{
 
80
                        N: fromBase10("290684273230919398108010081414538931343"),
 
81
                        E: 65537,
 
82
                },
 
83
                D: fromBase10("31877380284581499213530787347443987241"),
 
84
                Primes: []*big.Int{
 
85
                        fromBase10("16775196964030542637"),
 
86
                        fromBase10("17328218193455850539"),
 
87
                },
 
88
        }
 
89
        testKeyBasics(t, priv)
 
90
}
 
91
 
53
92
func testKeyBasics(t *testing.T, priv *PrivateKey) {
54
93
        if err := priv.Validate(); err != nil {
55
94
                t.Errorf("Validate() failed: %s", err)
56
95
        }
 
96
        if priv.D.Cmp(priv.N) > 0 {
 
97
                t.Errorf("private exponent too large")
 
98
        }
57
99
 
58
100
        pub := &priv.PublicKey
59
101
        m := big.NewInt(42)
98
140
        }
99
141
        priv.Precompute()
100
142
 
101
 
        c := fromBase10("1000")
 
143
        c := fromBase10("8472002792838218989464636159316973636630013835787202418124758118372358261975764365740026024610403138425986214991379012696600761514742817632790916315594342398720903716529235119816755589383377471752116975374952783629225022962092351886861518911824745188989071172097120352727368980275252089141512321893536744324822590480751098257559766328893767334861211872318961900897793874075248286439689249972315699410830094164386544311554704755110361048571142336148077772023880664786019636334369759624917224888206329520528064315309519262325023881707530002540634660750469137117568199824615333883758410040459705787022909848740188613313")
102
144
 
103
145
        b.StartTimer()
104
146
 
123
165
        }
124
166
        priv.Precompute()
125
167
 
126
 
        c := fromBase10("1000")
 
168
        c := fromBase10("8472002792838218989464636159316973636630013835787202418124758118372358261975764365740026024610403138425986214991379012696600761514742817632790916315594342398720903716529235119816755589383377471752116975374952783629225022962092351886861518911824745188989071172097120352727368980275252089141512321893536744324822590480751098257559766328893767334861211872318961900897793874075248286439689249972315699410830094164386544311554704755110361048571142336148077772023880664786019636334369759624917224888206329520528064315309519262325023881707530002540634660750469137117568199824615333883758410040459705787022909848740188613313")
127
169
 
128
170
        b.StartTimer()
129
171
 
158
200
                        if err != nil {
159
201
                                t.Errorf("#%d,%d error: %s", i, j, err)
160
202
                        }
161
 
                        if bytes.Compare(out, message.out) != 0 {
 
203
                        if !bytes.Equal(out, message.out) {
162
204
                                t.Errorf("#%d,%d bad result: %x (want %x)", i, j, out, message.out)
163
205
                        }
164
206
                }
182
224
                        out, err := DecryptOAEP(sha1, nil, private, message.out, nil)
183
225
                        if err != nil {
184
226
                                t.Errorf("#%d,%d error: %s", i, j, err)
185
 
                        } else if bytes.Compare(out, message.in) != 0 {
 
227
                        } else if !bytes.Equal(out, message.in) {
186
228
                                t.Errorf("#%d,%d bad result: %#v (want %#v)", i, j, out, message.in)
187
229
                        }
188
230
 
190
232
                        out, err = DecryptOAEP(sha1, random, private, message.out, nil)
191
233
                        if err != nil {
192
234
                                t.Errorf("#%d,%d (blind) error: %s", i, j, err)
193
 
                        } else if bytes.Compare(out, message.in) != 0 {
 
235
                        } else if !bytes.Equal(out, message.in) {
194
236
                                t.Errorf("#%d,%d (blind) bad result: %#v (want %#v)", i, j, out, message.in)
195
237
                        }
196
238
                }