Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
1cf656e
Initial typescript project setup
lovasoa May 29, 2021
4955eb3
Convert sign.js to typescript
lovasoa May 29, 2021
12f3392
Convert common and mac to typescript
lovasoa May 29, 2021
02c08d6
port index and encrypt to typescript
lovasoa May 29, 2021
5d0e3f6
Apply npx semistandard --fix
lovasoa May 29, 2021
546f23f
Fix module imports and coverage
lovasoa May 30, 2021
59b51d7
Use sourcemaps in coverage reports
lovasoa May 30, 2021
1d4d63a
Update cbor
lovasoa May 30, 2021
d9433fb
fix a type error
lovasoa May 30, 2021
82fc4ec
fix typo
lovasoa May 30, 2021
f04e39a
fix grammar in error message in tests
lovasoa May 30, 2021
757a28b
remove some code duplication
lovasoa May 30, 2021
fabd894
replace elliptic with the standard webcrypto API
lovasoa May 30, 2021
0dfcddc
Take a cryptoKey directly in verify and start adapting tests
lovasoa May 31, 2021
e75bc30
fix more tests
lovasoa May 31, 2021
d52ca75
fix RSA signature
lovasoa May 31, 2021
4b8c5ef
simplify tests
lovasoa May 31, 2021
740a80a
Remove some duplication
lovasoa May 31, 2021
78d16a9
refactor more tests
lovasoa May 31, 2021
51d4a07
removve unused imports
lovasoa May 31, 2021
1f1af3c
Implement MAC with webcrypto
lovasoa Jun 1, 2021
c02da3b
npm ignore
lovasoa Jun 1, 2021
ba6a743
remove dependency to aes-cbc-mac
lovasoa Jun 1, 2021
a4db2db
v0.5.3
lovasoa Jun 1, 2021
7158509
depend on cbor-web instead of cbor
lovasoa Jun 25, 2021
ec6d567
Allow extracting the plaintext even when the signature is incorrect
lovasoa Jun 26, 2021
eec562d
Delete xxx.js
lovasoa Jul 7, 2021
d06e963
update dependencies
lovasoa Aug 5, 2021
e591350
add the ability to fetch the public key dynamically when decoding a m…
lovasoa Aug 5, 2021
0ef493a
make more types public
lovasoa Aug 5, 2021
810af30
reexport cbor
lovasoa Aug 5, 2021
df055ad
add support for RSA-PSS
lovasoa Aug 24, 2021
98b78d8
pass the algorithm name to the verifier function
lovasoa Aug 24, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ npm-debug.log
coverage/
tmp/
.vscode/
build/*
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!package.json
!build/*
22 changes: 22 additions & 0 deletions .nycrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "@istanbuljs/nyc-config-typescript",
"include": [
"lib/**/*.ts"
],
"exclude": [
"**/*.d.ts"
],
"extension": [
".ts"
],
"require": [

],
"reporter": [
"html",
"text"
],
"sourceMap": true,
"instrument": true,
"all": true
}
118 changes: 71 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,91 +1,115 @@
[![Build Status](https://travis-ci.com/erdtman/cose-js.svg?branch=master)](https://travis-ci.com/erdtman/cose-js)
[![Coverage Status](https://coveralls.io/repos/github/erdtman/cose-js/badge.svg?branch=master)](https://coveralls.io/github/erdtman/cose-js?branch=master)
# cose-js
JavaScript implementation of [COSE](https://tools.ietf.org/html/rfc8152), [RFC8152](https://tools.ietf.org/html/rfc8152)
## MAC

# cosette

Typescript implementation of [COSE](https://tools.ietf.org/html/rfc8152), [RFC8152](https://tools.ietf.org/html/rfc8152)

This is a fork of cose-js with the objective of providing a well-typed implementation of cose that works in NodeJS and the browser.

It depends on isomorphic-webcrypto, so on the browser, it uses the fast and secure WebCrypto cryptographic API.

## Current state

Working with isomorphic-webcrypto :
- Create and verify ECDSA signatures
- Create and verify MAC and AES-CCM signatures

Still not ported to isomorphic-webcrypto :
- Encryption and Decryption

## Sign

```js
const cose = require('cose-js');
const cose = require('cosette');
const crypto = cose.sign.webcrypto;

const plaintext = 'Important message!';
const headers = {
'p': {'alg': 'SHA-256_64'},
'u': {'kid': 'our-secret'}
};
const recipent = {
'key': Buffer.from('231f4c4d4d3051fdc2ec0a3851d5b383', 'hex')
};
async function sign() {
const plaintext = 'Important message!';
const headers = {
'p': {'alg': 'ES256'},
'u': {'kid': '11'}
};
const signer = {
// See https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey
'key': await crypto.subtle.importKey("jwk", {
"kty":"EC",
"crv":"P-256",
"x":"usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8",
"y":"IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4",
"d":"V8kgd2ZBRuh2dgyVINBUqpPDr7BOMGcF22CQMIUHtNM"
}, { name: "ECDSA", namedCurve: "P-256" }, true, [usage]);
};

cose.mac.create(
headers,
plaintext,
recipent)
.then((buf) => {
console.log('MACed message: ' + buf.toString('hex'));
}).catch((error) => {
console.log(error);
});
const buf = await cose.sign.create(headers, plaintext, signer);
console.log('Signed message: ' + buf.toString('hex'));
}

sign()
```
## Verify MAC

## Verify Signature
```js
const cose = require('cose-js');

const key = Buffer.from('231f4c4d4d3051fdc2ec0a3851d5b383', 'hex');
const COSEMessage = Buffer.from('d18443a10104a1044a6f75722d73656372657472496d706f7274616e74206d65737361676521488894981d4aa5d614', 'hex');
cose.mac.read(
const verifier = {
'key': {
'x': Buffer.from('143329cce7868e416927599cf65a34f3ce2ffda55a7eca69ed8919a394d42f0f', 'hex'),
'y': Buffer.from('60f7f1a780d8a783bfb7a2dd6b2796e8128dbbcef9d3d168db9529971a36e7b9', 'hex')
}
};
const COSEMessage = Buffer.from('d28443a10126a10442313172496d706f7274616e74206d6573736167652158404c2b6b66dfedc4cfef0f221cf7ac7f95087a4c4245fef0063a0fd4014b670f642d31e26d38345bb4efcdc7ded3083ab4fe71b62a23f766d83785f044b20534f9', 'hex');

cose.sign.verify(
COSEMessage,
key)
verifier)
.then((buf) => {
console.log('Verified message: ' + buf.toString('utf8'));
}).catch((error) => {
console.log(error);
});
```
## Sign


## MAC
```js
const cose = require('cose-js');

const plaintext = 'Important message!';
const headers = {
'p': {'alg': 'ES256'},
'u': {'kid': '11'}
'p': {'alg': 'SHA-256_64'},
'u': {'kid': 'our-secret'}
};
const signer = {
'key': {
'd': Buffer.from('6c1382765aec5358f117733d281c1c7bdc39884d04a45a1e6c67c858bc206c19', 'hex')
}
const recipent = {
'key': Buffer.from('231f4c4d4d3051fdc2ec0a3851d5b383', 'hex')
};

cose.sign.create(
cose.mac.create(
headers,
plaintext,
signer)
recipent)
.then((buf) => {
console.log('Signed message: ' + buf.toString('hex'));
console.log('MACed message: ' + buf.toString('hex'));
}).catch((error) => {
console.log(error);
});
```
## Verify Signature
## Verify MAC
```js
const cose = require('cose-js');

const verifier = {
'key': {
'x': Buffer.from('143329cce7868e416927599cf65a34f3ce2ffda55a7eca69ed8919a394d42f0f', 'hex'),
'y': Buffer.from('60f7f1a780d8a783bfb7a2dd6b2796e8128dbbcef9d3d168db9529971a36e7b9', 'hex')
}
};
const COSEMessage = Buffer.from('d28443a10126a10442313172496d706f7274616e74206d6573736167652158404c2b6b66dfedc4cfef0f221cf7ac7f95087a4c4245fef0063a0fd4014b670f642d31e26d38345bb4efcdc7ded3083ab4fe71b62a23f766d83785f044b20534f9', 'hex');

cose.sign.verify(
const key = Buffer.from('231f4c4d4d3051fdc2ec0a3851d5b383', 'hex');
const COSEMessage = Buffer.from('d18443a10104a1044a6f75722d73656372657472496d706f7274616e74206d65737361676521488894981d4aa5d614', 'hex');
cose.mac.read(
COSEMessage,
verifier)
key)
.then((buf) => {
console.log('Verified message: ' + buf.toString('utf8'));
}).catch((error) => {
console.log(error);
});
```

## Encrypt
```js
const cose = require('cose-js');
Expand Down
6 changes: 3 additions & 3 deletions examples/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const cose = require('../');

const plaintext = 'Secret message!';
const headers = {
'p': { 'alg': 'A128GCM' },
'u': { 'kid': 'our-secret' }
p: { alg: 'A128GCM' },
u: { kid: 'our-secret' }
};
const recipient = {
'key': Buffer.from('231f4c4d4d3051fdc2ec0a3851d5b383', 'hex')
key: Buffer.from('231f4c4d4d3051fdc2ec0a3851d5b383', 'hex')
};

cose.encrypt.create(
Expand Down
6 changes: 3 additions & 3 deletions examples/mac.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const cose = require('../');

const plaintext = 'Important message!';
const headers = {
'p': { 'alg': 'SHA-256_64' },
'u': { 'kid': 'our-secret' }
p: { alg: 'SHA-256_64' },
u: { kid: 'our-secret' }
};
const recipent = {
'key': Buffer.from('231f4c4d4d3051fdc2ec0a3851d5b383', 'hex')
key: Buffer.from('231f4c4d4d3051fdc2ec0a3851d5b383', 'hex')
};

cose.mac.create(
Expand Down
8 changes: 4 additions & 4 deletions examples/sign-rs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const cose = require('../lib');
async function sample () {
const plaintext = 'Important message!';
const headers = {
'p': { 'alg': 'RS256' },
'u': { 'kid': '11' }
p: { alg: 'RS256' },
u: { kid: '11' }
};
const keys = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
Expand All @@ -19,14 +19,14 @@ async function sample () {
}
});
const signer = {
'key': keys.privateKey
key: keys.privateKey
};

const msg = await cose.sign.create(headers, plaintext, signer);
console.log('Signed message: ' + msg.toString('hex'));

const verifier = {
'key': keys.publicKey
key: keys.publicKey
};

const plaintext2 = await cose.sign.verify(msg, verifier);
Expand Down
14 changes: 7 additions & 7 deletions examples/sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ const cose = require('../');

const plaintext = 'Important message!';
const headers = {
'p': { 'alg': 'ES256' },
'u': { 'kid': '11' }
p: { alg: 'ES256' },
u: { kid: '11' }
};
const signer = {
'key': {
'd': Buffer.from('6c1382765aec5358f117733d281c1c7bdc39884d04a45a1e6c67c858bc206c19', 'hex')
key: {
d: Buffer.from('6c1382765aec5358f117733d281c1c7bdc39884d04a45a1e6c67c858bc206c19', 'hex')
}
};

Expand All @@ -22,9 +22,9 @@ cose.sign.create(
});

const verifier = {
'key': {
'x': Buffer.from('143329cce7868e416927599cf65a34f3ce2ffda55a7eca69ed8919a394d42f0f', 'hex'),
'y': Buffer.from('60f7f1a780d8a783bfb7a2dd6b2796e8128dbbcef9d3d168db9529971a36e7b9', 'hex')
key: {
x: Buffer.from('143329cce7868e416927599cf65a34f3ce2ffda55a7eca69ed8919a394d42f0f', 'hex'),
y: Buffer.from('60f7f1a780d8a783bfb7a2dd6b2796e8128dbbcef9d3d168db9529971a36e7b9', 'hex')
}
};
const COSEMessage = Buffer.from('d28443a10126a10442313172496d706f7274616e74206d6573736167652158404c2b6b66dfedc4cfef0f221cf7ac7f95087a4c4245fef0063a0fd4014b670f642d31e26d38345bb4efcdc7ded3083ab4fe71b62a23f766d83785f044b20534f9', 'hex');
Expand Down
Loading