summaryrefslogtreecommitdiffstats
path: root/test/ecdh_test.js
blob: 1a81bd18ab84bd682fc8afcc294579c8568ae62f (plain)
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
new sjcl.test.TestCase("ECDH test", function (cb) {
  if (!sjcl.ecc) {
    this.unimplemented();
    cb && cb();
    return;
  }
  
  try {  
    var keys = sjcl.ecc.elGamal.generateKeys(192,0),
        keyTag = keys.pub.kem(0),
        key2 = keys.sec.unkem(keyTag.tag);

    var serializedPubKey = keys.pub.serialize();
    var deserializedPubKey = sjcl.ecc.deserialize(serializedPubKey);
    var serializedSecKey = keys.sec.serialize();
    var deserializedSecKey = sjcl.ecc.deserialize(serializedSecKey);

    this.require(sjcl.bitArray.equal(keys.pub.get().x, 
	deserializedPubKey.get().x));
    this.require(sjcl.bitArray.equal(keys.pub.get().y, 
	deserializedPubKey.get().y));
    this.require(sjcl.bitArray.equal(deserializedSecKey.get(), keys.sec.get()));

    var ciphertext = sjcl.encrypt(deserializedPubKey, "hello world");
    var plaintext  = sjcl.decrypt(keys.sec, ciphertext);
    this.require(plaintext == "hello world");

    ciphertext = sjcl.encrypt(keys.pub, "hello world");
    plaintext  = sjcl.decrypt(deserializedSecKey, ciphertext);
    this.require(plaintext == "hello world");
        
    this.require(sjcl.bitArray.equal(keyTag.key, key2));
  } catch(e) {
    this.fail(e);
  }
  cb && cb();
});