Skip to content

Encrypting, decrypting createKeyWithSeed do not work, unless createKey is called once before it #6

@transducer

Description

@transducer

Encrypting and decrypting does not work with key created by createKeyWithSeed unless another key created with createKey is created before or after it.

The createKeyWithSeed generates a key pair object that does not work with the encrypt and decrypt methods.

Test:

it('should be able to encrypt and decrypt with key from createKeyWithSeed', function () {

    const seed = new Uint8Array([11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18,
    11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18,
    11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18
    ])

    const keyPair = NTRU.createKeyWithSeed(seed)

    const plaintext = new Uint8Array([104, 101, 108, 108, 111, 0]) // "hello"
    let encrypted = NTRU.encrypt(plaintext, keyPair.public)
    let decrypted = NTRU.decrypt(encrypted, keyPair.private) // same as plaintext

    assert.deepEqual(plaintext, decrypted)
});

Result:

AssertionError: expected <Buffer 68 65 6c 6c 6f 00;> to deeply equal <Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... >
+ expected - actual

 [
 -  104
 -  101
 -  108
 -  108
 -  111
    0
 +  0
 ....
]

But it does work when calling NTRU.createKey() before it:

The test succeeds when creating a normal key after createKeyWithSeed:

it('should be able to encrypt and decrypt with key from createKeyWithSeed', function () {

    const seed = new Uint8Array([11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18,
    11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18,
    11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18, 11, 12, 13, 14, 15, 16, 17, 18
    ])

    const dummyKeyPair = NTRU.createKey(); // <== Added this
    const keyPair = NTRU.createKeyWithSeed(seed)

    const plaintext = new Uint8Array([104, 101, 108, 108, 111, 0]) // "hello"
    let encrypted = NTRU.encrypt(plaintext, keyPair.public)
    let decrypted = NTRU.decrypt(encrypted, keyPair.private) // same as plaintext

    assert.deepEqual(plaintext, decrypted)
});

Why is this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions