Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
erdtman committed Nov 2, 2021
1 parent 1c42abe commit 59868e3
Show file tree
Hide file tree
Showing 22 changed files with 642 additions and 618 deletions.
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
92 changes: 46 additions & 46 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
'use strict';

const AlgToTags = {
'PS512': -39,
'PS384': -38,
'PS256': -37,
'RS512': -259,
'RS384': -258,
'RS256': -257,
PS512: -39,
PS384: -38,
PS256: -37,
RS512: -259,
RS384: -258,
RS256: -257,
'ECDH-SS-512': -28,
'ECDH-SS': -27,
'ECDH-ES-512': -26,
'ECDH-ES': -25,
'ES256': -7,
'ES384': -35,
'ES512': -36,
'direct': -6,
'A128GCM': 1,
'A192GCM': 2,
'A256GCM': 3,
ES256: -7,
ES384: -35,
ES512: -36,
direct: -6,
A128GCM: 1,
A192GCM: 2,
A256GCM: 3,
'SHA-256_64': 4,
'SHA-256-64': 4,
'HS256/64': 4,
'SHA-256': 5,
'HS256': 5,
HS256: 5,
'SHA-384': 6,
'HS384': 6,
HS384: 6,
'SHA-512': 7,
'HS512': 7,
HS512: 7,
'AES-CCM-16-64-128': 10,
'AES-CCM-16-128/64': 10,
'AES-CCM-16-64-256': 11,
Expand All @@ -52,10 +52,10 @@ const AlgToTags = {
};

const Translators = {
'kid': (value) => {
kid: (value) => {
return Buffer.from(value, 'utf8');
},
'alg': (value) => {
alg: (value) => {
if (!(AlgToTags[value])) {
throw new Error('Unknown \'alg\' parameter, ' + value);
}
Expand All @@ -64,18 +64,18 @@ const Translators = {
};

const HeaderParameters = {
'partyUNonce': -22,
'static_key_id': -3,
'static_key': -2,
'ephemeral_key': -1,
'alg': 1,
'crit': 2,
'content_type': 3,
'ctyp': 3, // one could question this but it makes testing easier
'kid': 4,
'IV': 5,
'Partial_IV': 6,
'counter_signature': 7
partyUNonce: -22,
static_key_id: -3,
static_key: -2,
ephemeral_key: -1,
alg: 1,
crit: 2,
content_type: 3,
ctyp: 3, // one could question this but it makes testing easier
kid: 4,
IV: 5,
Partial_IV: 6,
counter_signature: 7
};

exports.EMPTY_BUFFER = Buffer.alloc(0);
Expand All @@ -98,39 +98,39 @@ exports.TranslateHeaders = function (header) {
};

const KeyParameters = {
'crv': -1,
'k': -1,
'x': -2,
'y': -3,
'd': -4,
'kty': 1
crv: -1,
k: -1,
x: -2,
y: -3,
d: -4,
kty: 1
};

const KeyTypes = {
'OKP': 1,
'EC2': 2,
'RSA': 3,
'Symmetric': 4
OKP: 1,
EC2: 2,
RSA: 3,
Symmetric: 4
};

const KeyCrv = {
'P-256': 1,
'P-384': 2,
'P-521': 3,
'X25519': 4,
'X448': 5,
'Ed25519': 6,
'Ed448': 7
X25519: 4,
X448: 5,
Ed25519: 6,
Ed448: 7
};

const KeyTranslators = {
'kty': (value) => {
kty: (value) => {
if (!(KeyTypes[value])) {
throw new Error('Unknown \'kty\' parameter, ' + value);
}
return KeyTypes[value];
},
'crv': (value) => {
crv: (value) => {
if (!(KeyCrv[value])) {
throw new Error('Unknown \'crv\' parameter, ' + value);
}
Expand Down
18 changes: 9 additions & 9 deletions lib/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ const TagToAlg = {
};

const COSEAlgToNodeAlg = {
'A128GCM': 'aes-128-gcm',
'A192GCM': 'aes-192-gcm',
'A256GCM': 'aes-256-gcm',
A128GCM: 'aes-128-gcm',
A192GCM: 'aes-192-gcm',
A256GCM: 'aes-256-gcm',

'AES-CCM-16-64-128': 'aes-128-ccm',
'AES-CCM-16-64-256': 'aes-256-ccm',
Expand Down Expand Up @@ -192,7 +192,7 @@ exports.create = function (headers, payload, recipients, options) {

let iv;
if (options.contextIv) {
let partialIv = randomSource(2);
const partialIv = randomSource(2);
iv = common.xor(partialIv, options.contextIv);
u.set(common.HeaderParameters.Partial_IV, partialIv);
} else {
Expand Down Expand Up @@ -231,10 +231,10 @@ exports.create = function (headers, payload, recipients, options) {
]);

const generatedKey = common.TranslateKey({
'crv': recipients[0].key.crv,
'x': senderPublicKey.slice(1, keyLength[recipients[0].key.crv] + 1), // TODO slice based on key length
'y': senderPublicKey.slice(keyLength[recipients[0].key.crv] + 1),
'kty': 'EC2' // TODO use real value
crv: recipients[0].key.crv,
x: senderPublicKey.slice(1, keyLength[recipients[0].key.crv] + 1), // TODO slice based on key length
y: senderPublicKey.slice(keyLength[recipients[0].key.crv] + 1),
kty: 'EC2' // TODO use real value
});
const rp = cbor.encode(common.TranslateHeaders(recipients[0].p));
const ikm = generated.computeSecret(recipientPublicKey);
Expand Down Expand Up @@ -285,7 +285,7 @@ exports.create = function (headers, payload, recipients, options) {
} else {
let iv;
if (options.contextIv) {
let partialIv = randomSource(2);
const partialIv = randomSource(2);
iv = common.xor(partialIv, options.contextIv);
u.set(common.HeaderParameters.Partial_IV, partialIv);
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/mac.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const AlgFromTags = {
const COSEAlgToNodeAlg = {
'SHA-256_64': 'sha256',
'SHA-256': 'sha256',
'HS256': 'sha256',
HS256: 'sha256',
'SHA-384': 'sha384',
'SHA-512': 'sha512',
'AES-MAC-128/64': 'aes-cbc-mac-64',
Expand Down
Loading

0 comments on commit 59868e3

Please sign in to comment.