Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation. #1

Merged
merged 12 commits into from
Nov 8, 2024
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand All @@ -28,7 +28,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand All @@ -44,7 +44,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand All @@ -60,7 +60,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
module.exports = function(config) {

Expand Down
22 changes: 5 additions & 17 deletions lib/canonize.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
/*!
* Copyright (c) 2023-2024 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
import * as rdfCanonize from 'rdf-canonize';
import jsonld from 'jsonld';

export async function canonize(input, options) {
// convert to RDF dataset and do canonicalization
options = {
algorithm: 'RDFC-1.0',
format: 'application/n-quads',
base: null,
safe: true,
...options
};
const opts = {...options, produceGeneralizedRdf: false};
delete opts.format;
opts.produceGeneralizedRdf = false;
const dataset = await jsonld.toRDF(input, opts);
return rdfCanonize.canonize(dataset, options);
import canonicalize from 'canonicalize';

export async function canonize(input) {
return canonicalize(input);
}
2 changes: 1 addition & 1 deletion lib/createVerifier.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright (c) 2023 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
import * as Ed25519Multikey from '@digitalbazaar/ed25519-multikey';

Expand Down
102 changes: 95 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,102 @@
/*!
* Copyright (c) 2023 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
import {canonize} from './canonize.js';
import {createVerifier} from './createVerifier.js';
import {name} from './name.js';
import {requiredAlgorithm} from './requiredAlgorithm.js';
import {sha} from './sha.js';

export const cryptosuite = {
canonize,
createVerifier,
name,
requiredAlgorithm,
};
export function createSignCryptosuite() {
return {
name,
canonize,
requiredAlgorithm,
createVerifier: _throwSignUsageError,
createVerifyData: _createVerifyDataFn(
_modifySignProofOptionsAndDocument
),
};
}

export function createVerifyCryptosuite() {
return {
name,
canonize,
requiredAlgorithm,
createVerifier,
createVerifyData: _createVerifyDataFn(
_modifyVerifyProofOptionsAndDocument
),
};
}

function _createVerifyDataFn(modifyProofOptionsAndDocument) {
return async function({cryptosuite, document, proof} = {}) {
if(cryptosuite?.name !== name) {
throw new TypeError(`"cryptosuite.name" must be "${name}".`);
}
// determine digest algorithm from key algorithm
modifyProofOptionsAndDocument({proof, document});

// await both jcs proof hash and jcs document hash
const [proofHash, docHash] = await Promise.all([
// canonize and hash proof
_canonizeProof(proof, {cryptosuite}).then(
jcsProofOptions => sha({string: jcsProofOptions})),
// canonize and hash document
cryptosuite.canonize(document).then(
jcsDocument => sha({string: jcsDocument}))
]);

// concatenate hash of jcs proof options and hash of c14n document
return _concat(proofHash, docHash);
};
}

function _modifyVerifyProofOptionsAndDocument({proof, document}) {
// 4) If proofOptions.@context exists:
if(proof['@context']) {
let proofContext = proof['@context'];
proofContext = Array.isArray(proofContext) ? proofContext : [proofContext];
let docContext = document['@context'];
docContext = Array.isArray(docContext) ? docContext : [docContext];

// 4.1) Check that the securedDocument.@context starts with all values
// contained in the proofOptions.@context in the same order. Otherwise, set
// verified to false and skip to the last step.
for(let i = 0; i < proofContext.length; i++) {
if(proofContext[i] !== docContext[i]) {
throw new Error('document.@context does not start with proof.@context');
}
}
// 4.2) Set unsecuredDocument.@context equal to proofOptions.@context.
document['@context'] = proof['@context'];
}
}

function _modifySignProofOptionsAndDocument({proof, document}) {
// 2) If unsecuredDocument.@context is present, set proof.@context to
// unsecuredDocument.@context.
if(document['@context']) {
proof['@context'] = document['@context'];
}
}

async function _canonizeProof(proofOptions, {cryptosuite}) {
const proof = {...proofOptions};
// `proofValue` must not be included in the proof options
delete proof.proofValue;
return cryptosuite.canonize(proof);
}

function _concat(b1, b2) {
const rval = new Uint8Array(b1.length + b2.length);
rval.set(b1, 0);
rval.set(b2, b1.length);
return rval;
}

function _throwSignUsageError() {
throw new Error('This cryptosuite must only be used with "sign".');
}
4 changes: 2 additions & 2 deletions lib/name.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*!
* Copyright (c) 2023 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
export const name = 'eddsa-rdfc-2022';
export const name = 'eddsa-jcs-2022';
2 changes: 1 addition & 1 deletion lib/requiredAlgorithm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*!
* Copyright (c) 2023 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
export const requiredAlgorithm = 'Ed25519';
21 changes: 21 additions & 0 deletions lib/sha-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
/* eslint-env browser */
const crypto = self && (self.crypto || self.msCrypto);

const REQUIRED_HASH_ALGORITHM = 'SHA-256';

/**
* Hashes a string of data using SHA-256 or SHA-384.
gannan08 marked this conversation as resolved.
Show resolved Hide resolved
*
* @param {object} options - The options to use.
* @param {string} options.string - The string to hash.
*
* @returns {Uint8Array} The hash digest.
*/
export async function sha({string}) {
const algorithm = REQUIRED_HASH_ALGORITHM;
const bytes = new TextEncoder().encode(string);
return new Uint8Array(await crypto.subtle.digest(algorithm, bytes));
}
19 changes: 19 additions & 0 deletions lib/sha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
import crypto from 'node:crypto';

const REQUIRED_HASH_ALGORITHM = 'SHA-256';

/**
* Hashes a string of data using SHA-256 or SHA-384.
gannan08 marked this conversation as resolved.
Show resolved Hide resolved
*
* @param {object} options - The options to use.
* @param {string} options.string - The string to hash.
*
* @returns {Uint8Array} The hash digest.
*/
export async function sha({string}) {
const algorithm = REQUIRED_HASH_ALGORITHM;
return new Uint8Array(crypto.createHash(algorithm).update(string).digest());
}
43 changes: 23 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,40 @@
"files": [
"lib/**/*.js"
],
"browser": {
"crypto": false,
"./lib/sha.js": "./lib/sha-browser.js"
},
"dependencies": {
"@digitalbazaar/ed25519-multikey": "^1.0.0",
"jsonld": "^8.1.0",
"rdf-canonize": "^4.0.1"
"@digitalbazaar/ed25519-multikey": "^1.3.0",
"canonicalize": "^2.0.0"
},
"devDependencies": {
"@digitalbazaar/data-integrity": "^2.0.0",
"@digitalbazaar/data-integrity-context": "^2.0.0",
"@digitalbazaar/ed25519-multikey": "^1.0.0",
"@digitalbazaar/data-integrity": "^2.5.0",
"@digitalbazaar/data-integrity-context": "^2.0.1",
"@digitalbazaar/ed25519-multikey": "^1.3.0",
"@digitalbazaar/ed25519-verification-key-2018": "^4.0.0",
"@digitalbazaar/ed25519-verification-key-2020": "^4.1.0",
"@digitalbazaar/ed25519-verification-key-2020": "^4.2.0",
"@digitalbazaar/multikey-context": "^2.0.1",
"@digitalbazaar/security-document-loader": "^3.0.0",
"c8": "^7.11.3",
"chai": "^4.3.6",
"c8": "^10.1.2",
"chai": "^4.5.0",
"cross-env": "^7.0.3",
"eslint": "^8.53.0",
"eslint-config-digitalbazaar": "^5.0.1",
"eslint-plugin-jsdoc": "^46.8.2",
"eslint-plugin-unicorn": "^49.0.0",
"jsonld-signatures": "^11.2.1",
"karma": "^6.3.20",
"eslint": "^8.57.1",
"eslint-config-digitalbazaar": "^5.2.0",
"eslint-plugin-jsdoc": "^50.4.3",
"eslint-plugin-unicorn": "^56.0.0",
"jsonld-signatures": "^11.3.2",
"karma": "^6.4.4",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^3.1.1",
"karma-chrome-launcher": "^3.2.0",
"karma-mocha": "^2.0.1",
"karma-mocha-reporter": "^2.2.5",
"karma-sourcemap-loader": "^0.3.8",
"karma-webpack": "^5.0.0",
"mocha": "^10.0.0",
"karma-sourcemap-loader": "^0.4.0",
"karma-webpack": "^5.0.1",
"mocha": "^10.8.2",
"mocha-lcov-reporter": "^1.3.0",
"webpack": "^5.73.0"
"webpack": "^5.96.1"
},
"engines": {
"node": ">=18"
Expand Down
2 changes: 1 addition & 1 deletion test/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright (c) 2023 Digital Bazaar, Inc. All rights reserved.
* Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved.
*/
module.exports = {
globals: {
Expand Down
Loading
Loading