@road-labs/ocmf-crypto-noble 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "@road-labs/ocmf-crypto-noble",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "main": "build/cjs/index.js",
5
5
  "module": "build/es2022/index.js",
6
6
  "types": "build/types/index.d.ts",
7
+ "files": [
8
+ "build/**/*"
9
+ ],
7
10
  "keywords": [],
8
11
  "author": "",
9
12
  "license": "MIT",
@@ -21,7 +24,7 @@
21
24
  "dependencies": {
22
25
  "@noble/curves": "^1.9.2",
23
26
  "@noble/hashes": "^1.8.0",
24
- "@road-labs/ocmf-crypto": "0.0.1"
27
+ "@road-labs/ocmf-crypto": "0.0.3"
25
28
  },
26
29
  "scripts": {
27
30
  "clear": "rimraf build",
package/jest.config.js DELETED
@@ -1,11 +0,0 @@
1
- const { createDefaultPreset } = require('ts-jest');
2
-
3
- const tsJestTransformCfg = createDefaultPreset().transform;
4
-
5
- /** @type {import("jest").Config} **/
6
- module.exports = {
7
- testEnvironment: 'node',
8
- transform: {
9
- ...tsJestTransformCfg,
10
- },
11
- };
package/src/crypto.ts DELETED
@@ -1,46 +0,0 @@
1
- import {
2
- CryptoAdapter,
3
- Hash,
4
- PrivateKeyFormat,
5
- PublicKeyFormat,
6
- SignatureFormat,
7
- } from '@road-labs/ocmf-crypto';
8
- import { EcPrivateKey } from './private-key';
9
- import { EcPublicKey } from './public-key';
10
- import sign from './sign';
11
- import verify from './verify';
12
-
13
- export class Crypto implements CryptoAdapter {
14
- async decodeEcPrivateKey(
15
- value: Uint8Array,
16
- format: PrivateKeyFormat
17
- ): Promise<EcPrivateKey> {
18
- return EcPrivateKey.fromEncoded(value, format);
19
- }
20
-
21
- async decodeEcPublicKey(
22
- value: Uint8Array,
23
- format: PublicKeyFormat
24
- ): Promise<EcPublicKey> {
25
- return EcPublicKey.fromEncoded(value, format);
26
- }
27
-
28
- async sign(
29
- data: Uint8Array,
30
- privateKey: EcPrivateKey,
31
- hash: Hash,
32
- format: SignatureFormat
33
- ): Promise<Uint8Array> {
34
- return sign(data, privateKey, hash, format);
35
- }
36
-
37
- async verify(
38
- signature: Uint8Array,
39
- data: Uint8Array,
40
- publicKey: EcPublicKey,
41
- hash: Hash,
42
- format: SignatureFormat
43
- ): Promise<boolean> {
44
- return verify(signature, data, publicKey, hash, format);
45
- }
46
- }
package/src/curve.ts DELETED
@@ -1,101 +0,0 @@
1
- import { secp256k1 } from '@noble/curves/secp256k1';
2
- import { secp256r1, secp384r1 } from '@noble/curves/nist';
3
- import { Field } from '@noble/curves/abstract/modular';
4
- import { sha256, sha384 } from '@noble/hashes/sha2';
5
- import { Curve } from '@road-labs/ocmf-crypto';
6
- import { CurveFn, weierstrass } from '@noble/curves/abstract/weierstrass';
7
-
8
- const secp192r1 = weierstrass({
9
- a: BigInt('0xfffffffffffffffffffffffffffffffefffffffffffffffc'),
10
- b: BigInt('0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1'),
11
- Fp: Field(BigInt('0xfffffffffffffffffffffffffffffffeffffffffffffffff')),
12
- n: BigInt('0xffffffffffffffffffffffff99def836146bc9b1b4d22831'),
13
- Gx: BigInt('0x188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012'),
14
- Gy: BigInt('0x07192b95ffc8da78631011ed6b24cdd573f977a11e794811'),
15
- h: BigInt(1),
16
- lowS: false,
17
- hash: sha256,
18
- });
19
-
20
- const secp192k1 = weierstrass({
21
- a: BigInt(0),
22
- b: BigInt(3),
23
- Fp: Field(BigInt('0xfffffffffffffffffffffffffffffffffffffffeffffee37')),
24
- n: BigInt('0xfffffffffffffffffffffffe26f2fc170f69466a74defd8d'),
25
- Gx: BigInt('0xdb4ff10ec057e9ae26b07d0280b7f4341da5d1b1eae06c7d'),
26
- Gy: BigInt('0x9b2f2f6d9c5628a7844163d015be86344082aa88d95e2f9d'),
27
- h: BigInt(1),
28
- lowS: false,
29
- hash: sha256,
30
- });
31
-
32
- const brainpool256r1 = weierstrass({
33
- a: BigInt(
34
- '0x7d5a0975fc2c3057eef67530417affe7fb8055c126dc5c6ce94a4b44f330b5d9'
35
- ),
36
- b: BigInt(
37
- '0x26dc5c6ce94a4b44f330b5d9bbd77cbf958416295cf7e1ce6bccdc18ff8c07b6'
38
- ),
39
- Fp: Field(
40
- BigInt('0xa9fb57dba1eea9bc3e660a909d838d726e3bf623d52620282013481d1f6e5377')
41
- ),
42
- n: BigInt(
43
- '0xa9fb57dba1eea9bc3e660a909d838d718c397aa3b561a6f7901e0e82974856a7'
44
- ),
45
- Gx: BigInt(
46
- '0x8bd2aeb9cb7e57cb2c4b482ffc81b7afb9de27e1e3bd23c23a4453bd9ace3262'
47
- ),
48
- Gy: BigInt(
49
- '0x547ef835c3dac4fd97f8461a14611dc9c27745132ded8e545c1d54c72f046997'
50
- ),
51
- h: BigInt(1),
52
- lowS: false,
53
- hash: sha256,
54
- });
55
-
56
- const brainpool384r1 = weierstrass({
57
- a: BigInt(
58
- '0x7bc382c63d8c150c3c72080ace05afa0c2bea28e4fb22787139165efba91f90f8aa5814a503ad4eb04a8c7dd22ce2826'
59
- ),
60
- b: BigInt(
61
- '0x4a8c7dd22ce28268b39b55416f0447c2fb77de107dcd2a62e880ea53eeb62d57cb4390295dbc9943ab78696fa504c11'
62
- ),
63
- Fp: Field(
64
- BigInt(
65
- '0x8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b412b1da197fb71123acd3a729901d1a71874700133107ec53'
66
- )
67
- ),
68
- n: BigInt(
69
- '0x8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b31f166e6cac0425a7cf3ab6af6b7fc3103b883202e9046565'
70
- ),
71
- Gx: BigInt(
72
- '0x1d1c64f068cf45ffa2a63a81b7c13f6b8847a3e77ef14fe3db7fcafe0cbd10e8e826e03436d646aaef87b2e247d4af1e'
73
- ),
74
- Gy: BigInt(
75
- '0x8abe1d7520f9c2a45cb1eb8e95cfd55262b70b29feec5864e19c054ff99129280e4646217791811142820341263c5315'
76
- ),
77
- h: BigInt(1),
78
- lowS: false,
79
- hash: sha384,
80
- });
81
-
82
- export function resolveCurveFn(curve: Curve): CurveFn {
83
- switch (curve) {
84
- case 'secp192r1':
85
- return secp192r1;
86
- case 'secp192k1':
87
- return secp192k1;
88
- case 'secp256k1':
89
- return secp256k1;
90
- case 'secp256r1':
91
- return secp256r1;
92
- case 'secp384r1':
93
- return secp384r1;
94
- case 'brainpool256r1':
95
- return brainpool256r1;
96
- case 'brainpool384r1':
97
- return brainpool384r1;
98
- default:
99
- throw new Error(`Failed to resolve curve: ${curve}`);
100
- }
101
- }
package/src/index.ts DELETED
@@ -1,6 +0,0 @@
1
- export * from './crypto';
2
- export * from './curve';
3
- export * from './private-key';
4
- export * from './public-key';
5
- export { default as sign } from './sign';
6
- export { default as verify } from './verify';
@@ -1,60 +0,0 @@
1
- import { EcPublicKey } from './public-key';
2
- import {
3
- Curve,
4
- decodePkcs8PrivateKeyInfo,
5
- oidToCurve,
6
- PrivateKeyFormat,
7
- UnsupportedCurveError,
8
- } from '@road-labs/ocmf-crypto';
9
-
10
- export class EcPrivateKey {
11
- private constructor(
12
- private readonly value: Uint8Array,
13
- private readonly curve: Curve,
14
- private readonly publicKey: EcPublicKey | null
15
- ) {}
16
-
17
- public getValue(): Uint8Array {
18
- return this.value;
19
- }
20
-
21
- public getCurve(): Curve {
22
- return this.curve;
23
- }
24
-
25
- public getPublicKey(): EcPublicKey | null {
26
- return this.publicKey;
27
- }
28
-
29
- /**
30
- * @param value - Encoded private key value
31
- * @param format - Encoding format
32
- */
33
- static fromEncoded(
34
- value: Uint8Array,
35
- format: PrivateKeyFormat
36
- ): EcPrivateKey {
37
- if (format !== 'pkcs8-der') {
38
- throw new Error(`Unsupported format: ${format}`);
39
- }
40
-
41
- const keyInfo = decodePkcs8PrivateKeyInfo(value);
42
-
43
- const namedCurve = keyInfo?.privateKey?.parameters?.namedCurve;
44
- if (!namedCurve) {
45
- throw new Error(`Named curve not specified`);
46
- }
47
-
48
- const curve = oidToCurve.get(namedCurve);
49
- if (!curve) {
50
- throw new UnsupportedCurveError(`Unknown curve: oid=${namedCurve}`);
51
- }
52
-
53
- let publicKey: EcPublicKey | null = null;
54
- if (keyInfo.privateKey.publicKey) {
55
- publicKey = new EcPublicKey(keyInfo.privateKey.publicKey, curve);
56
- }
57
-
58
- return new EcPrivateKey(keyInfo.privateKey.privateKey, curve, publicKey);
59
- }
60
- }
package/src/public-key.ts DELETED
@@ -1,67 +0,0 @@
1
- import {
2
- Curve,
3
- curveToOid,
4
- decodePkixSubjectPublicKeyInfo,
5
- encodePkixSubjectPublicKeyInfo,
6
- oidEllipticCurveKey,
7
- oidToCurve,
8
- PublicKeyFormat,
9
- UnsupportedCurveError,
10
- UnsupportedPublicKeyFormatError,
11
- } from '@road-labs/ocmf-crypto';
12
-
13
- export class EcPublicKey {
14
- constructor(
15
- private readonly value: Uint8Array,
16
- private readonly curve: Curve
17
- ) {}
18
-
19
- public getValue(): Uint8Array {
20
- return this.value;
21
- }
22
-
23
- public getCurve(): Curve {
24
- return this.curve;
25
- }
26
-
27
- static fromEncoded(value: Uint8Array, format: PublicKeyFormat): EcPublicKey {
28
- if (format !== 'spki-der') {
29
- throw new UnsupportedPublicKeyFormatError(`Unknown format: ${format}`);
30
- }
31
-
32
- const keyInfo = decodePkixSubjectPublicKeyInfo(value);
33
-
34
- const namedCurve = keyInfo.algorithm.parameters?.namedCurve;
35
- if (!namedCurve) {
36
- throw new Error(`Named curve not specified`);
37
- }
38
-
39
- const curve = oidToCurve.get(namedCurve);
40
- if (!curve) {
41
- throw new UnsupportedCurveError(`Unknown curve: oid=${namedCurve}`);
42
- }
43
-
44
- return new EcPublicKey(keyInfo.subjectPublicKey, curve);
45
- }
46
-
47
- public encode(format: PublicKeyFormat): Uint8Array {
48
- if (format !== 'spki-der') {
49
- throw new UnsupportedPublicKeyFormatError(`Unknown format: ${format}`);
50
- }
51
-
52
- const namedCurve = curveToOid.get(this.curve);
53
- if (!namedCurve) {
54
- throw new Error(`Failed to map curve to OID: ${this.curve}`);
55
- }
56
-
57
- return encodePkixSubjectPublicKeyInfo({
58
- algorithm: {
59
- algorithm: oidEllipticCurveKey,
60
- parameters: {
61
- namedCurve,
62
- },
63
- },
64
- subjectPublicKey: this.value,
65
- });
66
- }
67
- }
package/src/sign.ts DELETED
@@ -1,36 +0,0 @@
1
- import { EcPrivateKey } from './private-key';
2
- import { sha256 } from '@noble/hashes/sha2';
3
- import { resolveCurveFn } from './curve';
4
- import {
5
- Hash,
6
- SignatureFormat,
7
- UnsupportedHashError,
8
- UnsupportedSignatureFormatError,
9
- } from '@road-labs/ocmf-crypto';
10
-
11
- /**
12
- * @param data - Data to be signed
13
- * @param privateKey - Private key to use for signing
14
- * @param hash - Hash to apply
15
- * @param format - Signature format
16
- * @return X.509 ECDSASigValue ASN.1 type DER encoded
17
- */
18
- export default function sign(
19
- data: Uint8Array,
20
- privateKey: EcPrivateKey,
21
- hash: Hash,
22
- format: SignatureFormat
23
- ): Uint8Array {
24
- if (hash !== 'SHA-256') {
25
- throw new UnsupportedHashError(`Invalid hash: ${hash}`);
26
- }
27
- if (format !== 'sigvalue-der') {
28
- throw new UnsupportedSignatureFormatError(
29
- `Invalid signature format: ${format}`
30
- );
31
- }
32
-
33
- return resolveCurveFn(privateKey.getCurve())
34
- .sign(sha256(data), privateKey.getValue())
35
- .toDERRawBytes();
36
- }
package/src/verify.ts DELETED
@@ -1,40 +0,0 @@
1
- import { EcPublicKey } from './public-key';
2
- import { sha256 } from '@noble/hashes/sha2';
3
- import { resolveCurveFn } from './curve';
4
- import {
5
- Hash,
6
- SignatureFormat,
7
- UnsupportedHashError,
8
- UnsupportedSignatureFormatError,
9
- } from '@road-labs/ocmf-crypto';
10
-
11
- /**
12
- * @param signature - X.509 ECDSASigValue ASN.1 type DER encoded
13
- * @param data - Raw value
14
- * @param key - The public key to verify against
15
- * @param hash - Hash to apply
16
- * @param format - Signature format
17
- */
18
- export default function verify(
19
- signature: Uint8Array,
20
- data: Uint8Array,
21
- key: EcPublicKey,
22
- hash: Hash,
23
- format: SignatureFormat
24
- ): boolean {
25
- if (hash !== 'SHA-256') {
26
- throw new UnsupportedHashError(`Invalid hash: ${hash}`);
27
- }
28
- if (format !== 'sigvalue-der') {
29
- throw new UnsupportedSignatureFormatError(
30
- `Invalid signature format: ${format}`
31
- );
32
- }
33
-
34
- return resolveCurveFn(key.getCurve()).verify(
35
- signature,
36
- sha256(data),
37
- key.getValue(),
38
- { format: 'der' }
39
- );
40
- }
@@ -1,40 +0,0 @@
1
- import { describe, expect, it } from '@jest/globals';
2
- import { Curve } from '@road-labs/ocmf-crypto';
3
- import { buildPrivateKeyTestCases } from 'test-commons';
4
- import { EcPrivateKey } from '../src';
5
-
6
- const curves: Curve[] = [
7
- 'brainpool256r1',
8
- 'brainpool384r1',
9
- 'secp192r1',
10
- 'secp192k1',
11
- 'secp256k1',
12
- 'secp256r1',
13
- 'secp384r1',
14
- ];
15
-
16
- describe('EcPrivateKey', () => {
17
- describe('fromEncoded', () => {
18
- it.each(buildPrivateKeyTestCases(curves))(
19
- 'supports $name',
20
- ({ curve, pkcs8 }) => {
21
- const privateKey = EcPrivateKey.fromEncoded(pkcs8, 'pkcs8-der');
22
- expect(privateKey.getCurve()).toEqual(curve);
23
- }
24
- );
25
- });
26
-
27
- describe('getPublicKey', () => {
28
- it.each(buildPrivateKeyTestCases(curves))(
29
- 'can derive a public key for $name',
30
- ({ curve, pkcs8, spki }) => {
31
- const publicKey = EcPrivateKey.fromEncoded(
32
- pkcs8,
33
- 'pkcs8-der'
34
- ).getPublicKey();
35
- expect(publicKey?.getCurve()).toEqual(curve);
36
- expect(publicKey?.encode('spki-der')).toEqual(spki);
37
- }
38
- );
39
- });
40
- });
@@ -1,39 +0,0 @@
1
- import { describe, expect, it } from '@jest/globals';
2
- import { Curve } from '@road-labs/ocmf-crypto';
3
- import { buildPublicKeyTestCases, isValidPublicKey } from 'test-commons';
4
- import { EcPublicKey } from '../src';
5
-
6
- const curves: Curve[] = [
7
- 'brainpool256r1',
8
- 'brainpool384r1',
9
- 'secp192r1',
10
- 'secp192k1',
11
- 'secp256k1',
12
- 'secp256r1',
13
- 'secp384r1',
14
- ];
15
-
16
- describe('EcPublicKey', () => {
17
- describe('fromEncoded', () => {
18
- it.each(buildPublicKeyTestCases(curves))(
19
- 'supports $name',
20
- ({ curve, spki }) => {
21
- const publicKey = EcPublicKey.fromEncoded(spki, 'spki-der');
22
- expect(publicKey.getCurve()).toEqual(curve);
23
- }
24
- );
25
- });
26
-
27
- describe('encode', () => {
28
- it.each(buildPublicKeyTestCases(curves))(
29
- 'encodes $name',
30
- ({ spki, curve }) => {
31
- const publicKey = EcPublicKey.fromEncoded(spki, 'spki-der').encode(
32
- 'spki-der'
33
- );
34
- expect(publicKey).toEqual(spki);
35
- expect(isValidPublicKey(publicKey, curve)).toEqual(true);
36
- }
37
- );
38
- });
39
- });
package/test/sign.spec.ts DELETED
@@ -1,26 +0,0 @@
1
- import { describe, expect, it } from '@jest/globals';
2
- import { buildSignTestCases, isValidSignature } from 'test-commons';
3
- import { EcPrivateKey, sign } from '../src';
4
- import { Curve } from '@road-labs/ocmf-crypto';
5
-
6
- const format = 'sigvalue-der';
7
- const curves: Curve[] = [
8
- 'brainpool256r1',
9
- 'brainpool384r1',
10
- 'secp192r1',
11
- 'secp192k1',
12
- 'secp256k1',
13
- 'secp256r1',
14
- 'secp384r1',
15
- ];
16
-
17
- describe('verify', () => {
18
- it.each(buildSignTestCases(curves))(
19
- '$name',
20
- ({ curve, data, hash, pkcs8 }) => {
21
- const privateKey = EcPrivateKey.fromEncoded(pkcs8, 'pkcs8-der');
22
- const signature = sign(data, privateKey, hash, format);
23
- expect(isValidSignature(signature, curve)).toEqual(true);
24
- }
25
- );
26
- });
@@ -1,26 +0,0 @@
1
- import { describe, expect, it } from '@jest/globals';
2
- import { buildVerifyTestCases } from 'test-commons';
3
- import { EcPublicKey, verify } from '../src';
4
- import { Curve } from '@road-labs/ocmf-crypto';
5
-
6
- const format = 'sigvalue-der';
7
- const curves: Curve[] = [
8
- 'brainpool256r1',
9
- 'brainpool384r1',
10
- 'secp192r1',
11
- 'secp192k1',
12
- 'secp256k1',
13
- 'secp256r1',
14
- 'secp384r1',
15
- ];
16
-
17
- describe('verify', () => {
18
- it.each(buildVerifyTestCases(curves))(
19
- '$name',
20
- ({ curve, signature, data, hash, spki, expected }) => {
21
- const publicKey = EcPublicKey.fromEncoded(spki, 'spki-der');
22
- const actual = verify(signature, data, publicKey, hash, format);
23
- expect(actual).toEqual(expected);
24
- }
25
- );
26
- });
package/tsconfig.json DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "include": ["src/**/*"]
4
- }