|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/** |
| 4 | + * Test dependencies |
| 5 | + */ |
| 6 | +const chai = require('chai') |
| 7 | + |
| 8 | +/** |
| 9 | + * Assertions |
| 10 | + */ |
| 11 | +chai.should() |
| 12 | +let expect = chai.expect |
| 13 | + |
| 14 | +/** |
| 15 | + * Code under test |
| 16 | + */ |
| 17 | +const ECDSA = require('../../src/algorithms/ECDSA') |
| 18 | +const {TextEncoder} = require('@sinonjs/text-encoding') |
| 19 | +const crypto = require('isomorphic-webcrypto') |
| 20 | +const base64url = require('base64url') |
| 21 | +const { getPublicKey, getPrivateKey } = require('../keys/ES256') |
| 22 | +const { should } = require('chai') |
| 23 | + |
| 24 | +/** |
| 25 | + * Reused test constants |
| 26 | + */ |
| 27 | +const alg = { name: 'ECDSA', hash: { name: 'SHA-256' }, namedCurve: "P-256" } |
| 28 | + |
| 29 | +const data = 'signed with Chrome generated webcrypto key' |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests |
| 33 | + */ |
| 34 | +describe('ECDSA', () => { |
| 35 | + |
| 36 | + /** |
| 37 | + * constructor |
| 38 | + */ |
| 39 | + describe('constructor', () => { |
| 40 | + it('should set params', () => { |
| 41 | + let alg = { name: 'ECDSA' } |
| 42 | + let ecdsa = new ECDSA(alg) |
| 43 | + ecdsa.params.should.equal(alg) |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + /** |
| 48 | + * sign |
| 49 | + */ |
| 50 | + describe('sign', () => { |
| 51 | + let importedEcdsaPrivateKey, importedEcdsaPublicKey |
| 52 | + |
| 53 | + before(async () => { |
| 54 | + importedEcdsaPrivateKey = await getPrivateKey() |
| 55 | + importedEcdsaPublicKey = await getPublicKey() |
| 56 | + }) |
| 57 | + |
| 58 | + it('should return a promise', () => { |
| 59 | + let ecdsa = new ECDSA(alg) |
| 60 | + return ecdsa.sign(importedEcdsaPrivateKey, data).should.be.instanceof(Promise) |
| 61 | + }) |
| 62 | + |
| 63 | + it('should reject an insufficient key length') |
| 64 | + |
| 65 | + it('should resolve a base64url encoded value and verification should pass', async () => { |
| 66 | + const ecdsa = new ECDSA(alg) |
| 67 | + const signature = await ecdsa.sign(importedEcdsaPrivateKey, data) |
| 68 | + |
| 69 | + // this will fail if signature is anything but base64 string |
| 70 | + expect(base64url(base64url.toBuffer(signature))).to.equal(signature) |
| 71 | + |
| 72 | + // ECDSA is non-deterministic. Therefore we test that the generated signature gets verified with crypto library |
| 73 | + const verified = await crypto.subtle.verify( |
| 74 | + { |
| 75 | + name: 'ECDSA', |
| 76 | + hash: { name: 'SHA-256' }, |
| 77 | + namedCurve: "P-256" |
| 78 | + }, |
| 79 | + importedEcdsaPublicKey, |
| 80 | + base64url.toBuffer(signature), |
| 81 | + new TextEncoder().encode(data) |
| 82 | + ) |
| 83 | + |
| 84 | + verified.should.eql(true) |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + /** |
| 89 | + * verify |
| 90 | + */ |
| 91 | + describe('verify', () => { |
| 92 | + let importedEcdsaPublicKey, signature |
| 93 | + |
| 94 | + before(async () => { |
| 95 | + /** |
| 96 | + * This signature was produced in Chromium |
| 97 | + * deails can be found in comments of ../keys/ES256.js |
| 98 | + */ |
| 99 | + signature = 'AUNkOnr//z999flIoTMebaf5EQC56WVQizK3GXW/u4EOQBvs9CtvfgWi0pQ3bi0k8p357ajtNvN/dJ1Vr8gbYg==' |
| 100 | + |
| 101 | + importedEcdsaPublicKey = await getPublicKey() |
| 102 | + }) |
| 103 | + |
| 104 | + it('should return a promise', () => { |
| 105 | + let ecdsa = new ECDSA(alg) |
| 106 | + ecdsa.verify(importedEcdsaPublicKey, signature, data).should.be.instanceof(Promise) |
| 107 | + }) |
| 108 | + |
| 109 | + it('should resolve to true', async () => { |
| 110 | + const ecdsa = new ECDSA(alg) |
| 111 | + const verified = await ecdsa.verify(importedEcdsaPublicKey, signature, data) |
| 112 | + expect(verified).to.equal(true) |
| 113 | + }) |
| 114 | + }) |
| 115 | + |
| 116 | + /** |
| 117 | + * importKey |
| 118 | + */ |
| 119 | + describe('importKey', () => { |
| 120 | + let exampleJwkPublicKey |
| 121 | + |
| 122 | + before(() => { |
| 123 | + exampleJwkPublicKey = { |
| 124 | + crv: 'P-256', |
| 125 | + kty: 'EC', |
| 126 | + x: '-c0_z0ly3xRDR0XQuvIirfgal59hq7BzF9ObdUXrgmI', |
| 127 | + y: '_7QdnDYKrkrkYaCqZko0ebDQ1L1RpHLtzg8YwdT79n8', |
| 128 | + alg: 'ES256' |
| 129 | + } |
| 130 | + }) |
| 131 | + |
| 132 | + it('should successfully import public jwk key', async () => { |
| 133 | + const ecdsa = new ECDSA(alg) |
| 134 | + const key = await ecdsa.importKey(exampleJwkPublicKey) |
| 135 | + key.cryptoKey.constructor.name.should.equal('CryptoKey') |
| 136 | + }) |
| 137 | + }) |
| 138 | +}) |
| 139 | + |
0 commit comments