aboutsummaryrefslogblamecommitdiffstats
path: root/crypto/ecies/ecies_test.go
blob: 943e4488ef7c7fbc0aa89daf441f488ea2adbcb4 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489








































































































































































































































































































































































































































































































                                                                                         
package ecies

import (
    "bytes"
    "crypto/elliptic"
    "crypto/rand"
    "crypto/sha256"
    "flag"
    "fmt"
    "io/ioutil"
    "testing"
)

var dumpEnc bool

func init() {
    flDump := flag.Bool("dump", false, "write encrypted test message to file")
    flag.Parse()
    dumpEnc = *flDump
}

// Ensure the KDF generates appropriately sized keys.
func TestKDF(t *testing.T) {
    msg := []byte("Hello, world")
    h := sha256.New()

    k, err := concatKDF(h, msg, nil, 64)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }
    if len(k) != 64 {
        fmt.Printf("KDF: generated key is the wrong size (%d instead of 64\n",
            len(k))
        t.FailNow()
    }
}

var skLen int
var ErrBadSharedKeys = fmt.Errorf("ecies: shared keys don't match")

// cmpParams compares a set of ECIES parameters. We assume, as per the
// docs, that AES is the only supported symmetric encryption algorithm.
func cmpParams(p1, p2 *ECIESParams) bool {
    if p1.hashAlgo != p2.hashAlgo {
        return false
    } else if p1.KeyLen != p2.KeyLen {
        return false
    } else if p1.BlockSize != p2.BlockSize {
        return false
    }
    return true
}

// cmpPublic returns true if the two public keys represent the same pojnt.
func cmpPublic(pub1, pub2 PublicKey) bool {
    if pub1.X == nil || pub1.Y == nil {
        fmt.Println(ErrInvalidPublicKey.Error())
        return false
    }
    if pub2.X == nil || pub2.Y == nil {
        fmt.Println(ErrInvalidPublicKey.Error())
        return false
    }
    pub1Out := elliptic.Marshal(pub1.Curve, pub1.X, pub1.Y)
    pub2Out := elliptic.Marshal(pub2.Curve, pub2.X, pub2.Y)

    return bytes.Equal(pub1Out, pub2Out)
}

// cmpPrivate returns true if the two private keys are the same.
func cmpPrivate(prv1, prv2 *PrivateKey) bool {
    if prv1 == nil || prv1.D == nil {
        return false
    } else if prv2 == nil || prv2.D == nil {
        return false
    } else if prv1.D.Cmp(prv2.D) != 0 {
        return false
    } else {
        return cmpPublic(prv1.PublicKey, prv2.PublicKey)
    }
}

// Validate the ECDH component.
func TestSharedKey(t *testing.T) {
    prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }
    skLen = MaxSharedKeyLength(&prv1.PublicKey) / 2

    prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    sk1, err := prv1.GenerateShared(&prv2.PublicKey, skLen, skLen)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    sk2, err := prv2.GenerateShared(&prv1.PublicKey, skLen, skLen)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    if !bytes.Equal(sk1, sk2) {
        fmt.Println(ErrBadSharedKeys.Error())
        t.FailNow()
    }
}

// Verify that the key generation code fails when too much key data is
// requested.
func TestTooBigSharedKey(t *testing.T) {
    prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    _, err = prv1.GenerateShared(&prv2.PublicKey, skLen*2, skLen*2)
    if err != ErrSharedKeyTooBig {
        fmt.Println("ecdh: shared key should be too large for curve")
        t.FailNow()
    }

    _, err = prv2.GenerateShared(&prv1.PublicKey, skLen*2, skLen*2)
    if err != ErrSharedKeyTooBig {
        fmt.Println("ecdh: shared key should be too large for curve")
        t.FailNow()
    }
}

// Ensure a public key can be successfully marshalled and unmarshalled, and
// that the decoded key is the same as the original.
func TestMarshalPublic(t *testing.T) {
    prv, err := GenerateKey(rand.Reader, DefaultCurve, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    out, err := MarshalPublic(&prv.PublicKey)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    pub, err := UnmarshalPublic(out)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    if !cmpPublic(prv.PublicKey, *pub) {
        fmt.Println("ecies: failed to unmarshal public key")
        t.FailNow()
    }
}

// Ensure that a private key can be encoded into DER format, and that
// the resulting key is properly parsed back into a public key.
func TestMarshalPrivate(t *testing.T) {
    prv, err := GenerateKey(rand.Reader, DefaultCurve, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    out, err := MarshalPrivate(prv)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    if dumpEnc {
        ioutil.WriteFile("test.out", out, 0644)
    }

    prv2, err := UnmarshalPrivate(out)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    if !cmpPrivate(prv, prv2) {
        fmt.Println("ecdh: private key import failed")
        t.FailNow()
    }
}

// Ensure that a private key can be successfully encoded to PEM format, and
// the resulting key is properly parsed back in.
func TestPrivatePEM(t *testing.T) {
    prv, err := GenerateKey(rand.Reader, DefaultCurve, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    out, err := ExportPrivatePEM(prv)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    if dumpEnc {
        ioutil.WriteFile("test.key", out, 0644)
    }

    prv2, err := ImportPrivatePEM(out)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    } else if !cmpPrivate(prv, prv2) {
        fmt.Println("ecdh: import from PEM failed")
        t.FailNow()
    }
}

// Ensure that a public key can be successfully encoded to PEM format, and
// the resulting key is properly parsed back in.
func TestPublicPEM(t *testing.T) {
    prv, err := GenerateKey(rand.Reader, DefaultCurve, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    out, err := ExportPublicPEM(&prv.PublicKey)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    if dumpEnc {
        ioutil.WriteFile("test.pem", out, 0644)
    }

    pub2, err := ImportPublicPEM(out)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    } else if !cmpPublic(prv.PublicKey, *pub2) {
        fmt.Println("ecdh: import from PEM failed")
        t.FailNow()
    }
}

// Benchmark the generation of P256 keys.
func BenchmarkGenerateKeyP256(b *testing.B) {
    for i := 0; i < b.N; i++ {
        if _, err := GenerateKey(rand.Reader, elliptic.P256(), nil); err != nil {
            fmt.Println(err.Error())
            b.FailNow()
        }
    }
}

// Benchmark the generation of P256 shared keys.
func BenchmarkGenSharedKeyP256(b *testing.B) {
    prv, err := GenerateKey(rand.Reader, elliptic.P256(), nil)
    if err != nil {
        fmt.Println(err.Error())
        b.FailNow()
    }

    for i := 0; i < b.N; i++ {
        _, err := prv.GenerateShared(&prv.PublicKey, skLen, skLen)
        if err != nil {
            fmt.Println(err.Error())
            b.FailNow()
        }
    }
}

// Verify that an encrypted message can be successfully decrypted.
func TestEncryptDecrypt(t *testing.T) {
    prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    message := []byte("Hello, world.")
    ct, err := Encrypt(rand.Reader, &prv2.PublicKey, message, nil, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    pt, err := prv2.Decrypt(rand.Reader, ct, nil, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    if !bytes.Equal(pt, message) {
        fmt.Println("ecies: plaintext doesn't match message")
        t.FailNow()
    }

    _, err = prv1.Decrypt(rand.Reader, ct, nil, nil)
    if err == nil {
        fmt.Println("ecies: encryption should not have succeeded")
        t.FailNow()
    }
}

// TestMarshalEncryption validates the encode/decode produces a valid
// ECIES encryption key.
func TestMarshalEncryption(t *testing.T) {
    prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    out, err := MarshalPrivate(prv1)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    prv2, err := UnmarshalPrivate(out)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    message := []byte("Hello, world.")
    ct, err := Encrypt(rand.Reader, &prv2.PublicKey, message, nil, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    pt, err := prv2.Decrypt(rand.Reader, ct, nil, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    if !bytes.Equal(pt, message) {
        fmt.Println("ecies: plaintext doesn't match message")
        t.FailNow()
    }

    _, err = prv1.Decrypt(rand.Reader, ct, nil, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

}

type testCase struct {
    Curve    elliptic.Curve
    Name     string
    Expected bool
}

var testCases = []testCase{
    testCase{
        Curve:    elliptic.P224(),
        Name:     "P224",
        Expected: false,
    },
    testCase{
        Curve:    elliptic.P256(),
        Name:     "P256",
        Expected: true,
    },
    testCase{
        Curve:    elliptic.P384(),
        Name:     "P384",
        Expected: true,
    },
    testCase{
        Curve:    elliptic.P521(),
        Name:     "P521",
        Expected: true,
    },
}

// Test parameter selection for each curve, and that P224 fails automatic
// parameter selection (see README for a discussion of P224). Ensures that
// selecting a set of parameters automatically for the given curve works.
func TestParamSelection(t *testing.T) {
    for _, c := range testCases {
        testParamSelection(t, c)
    }
}

func testParamSelection(t *testing.T, c testCase) {
    params := ParamsFromCurve(c.Curve)
    if params == nil && c.Expected {
        fmt.Printf("%s (%s)\n", ErrInvalidParams.Error(), c.Name)
        t.FailNow()
    } else if params != nil && !c.Expected {
        fmt.Printf("ecies: parameters should be invalid (%s)\n",
            c.Name)
        t.FailNow()
    }

    prv1, err := GenerateKey(rand.Reader, DefaultCurve, nil)
    if err != nil {
        fmt.Printf("%s (%s)\n", err.Error(), c.Name)
        t.FailNow()
    }

    prv2, err := GenerateKey(rand.Reader, DefaultCurve, nil)
    if err != nil {
        fmt.Printf("%s (%s)\n", err.Error(), c.Name)
        t.FailNow()
    }

    message := []byte("Hello, world.")
    ct, err := Encrypt(rand.Reader, &prv2.PublicKey, message, nil, nil)
    if err != nil {
        fmt.Printf("%s (%s)\n", err.Error(), c.Name)
        t.FailNow()
    }

    pt, err := prv2.Decrypt(rand.Reader, ct, nil, nil)
    if err != nil {
        fmt.Printf("%s (%s)\n", err.Error(), c.Name)
        t.FailNow()
    }

    if !bytes.Equal(pt, message) {
        fmt.Printf("ecies: plaintext doesn't match message (%s)\n",
            c.Name)
        t.FailNow()
    }

    _, err = prv1.Decrypt(rand.Reader, ct, nil, nil)
    if err == nil {
        fmt.Printf("ecies: encryption should not have succeeded (%s)\n",
            c.Name)
        t.FailNow()
    }

}

// Ensure that the basic public key validation in the decryption operation
// works.
func TestBasicKeyValidation(t *testing.T) {
    badBytes := []byte{0, 1, 5, 6, 7, 8, 9}

    prv, err := GenerateKey(rand.Reader, DefaultCurve, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    message := []byte("Hello, world.")
    ct, err := Encrypt(rand.Reader, &prv.PublicKey, message, nil, nil)
    if err != nil {
        fmt.Println(err.Error())
        t.FailNow()
    }

    for _, b := range badBytes {
        ct[0] = b
        _, err := prv.Decrypt(rand.Reader, ct, nil, nil)
        if err != ErrInvalidPublicKey {
            fmt.Println("ecies: validated an invalid key")
            t.FailNow()
        }
    }
}