@road-labs/ocmf-crypto 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",
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",
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/asn1.ts DELETED
@@ -1,159 +0,0 @@
1
- import { AsnConvert, AsnParser } from '@peculiar/asn1-schema';
2
- import { PrivateKeyInfo } from '@peculiar/asn1-pkcs8';
3
- import {
4
- ECDSASigValue,
5
- ECParameters,
6
- ECPrivateKey,
7
- id_ecPublicKey,
8
- id_secp192r1,
9
- id_secp256r1,
10
- id_secp384r1,
11
- } from '@peculiar/asn1-ecc';
12
- import { AlgorithmIdentifier, SubjectPublicKeyInfo } from '@peculiar/asn1-x509';
13
- import { Curve } from './types';
14
-
15
- export const oidEllipticCurveKey = id_ecPublicKey;
16
-
17
- const id_brainpool256r1 = '1.3.36.3.3.2.8.1.1.7';
18
- const id_brainpool384r1 = '1.3.36.3.3.2.8.1.1.11';
19
- const id_secp192k1 = '1.3.132.0.31';
20
- const id_secp256k1 = '1.3.132.0.10';
21
-
22
- export const oidToCurve = new Map<string, Curve>([
23
- [id_brainpool256r1, 'brainpool256r1'],
24
- [id_brainpool384r1, 'brainpool384r1'],
25
- [id_secp192k1, 'secp192k1'],
26
- [id_secp192r1, 'secp192r1'],
27
- [id_secp256k1, 'secp256k1'],
28
- [id_secp256r1, 'secp256r1'],
29
- [id_secp384r1, 'secp384r1'],
30
- ]);
31
-
32
- export const curveToOid = new Map<Curve, string>([
33
- ['brainpool256r1', id_brainpool256r1],
34
- ['brainpool384r1', id_brainpool384r1],
35
- ['secp192k1', id_secp192k1],
36
- ['secp192r1', id_secp192r1],
37
- ['secp256k1', id_secp256k1],
38
- ['secp256r1', id_secp256r1],
39
- ['secp384r1', id_secp384r1],
40
- ]);
41
-
42
- export interface Pkcs8PrivateKeyInfo {
43
- version: number;
44
- privateKeyAlgorithm: {
45
- algorithm: string;
46
- };
47
- privateKey: {
48
- version: number;
49
- privateKey: Uint8Array;
50
- parameters?: {
51
- namedCurve?: string;
52
- };
53
- publicKey?: Uint8Array;
54
- };
55
- }
56
-
57
- export interface PkixSubjectPublicKeyInfo {
58
- algorithm: {
59
- algorithm: string;
60
- parameters?: {
61
- namedCurve?: string;
62
- };
63
- };
64
- subjectPublicKey: Uint8Array;
65
- }
66
-
67
- export interface PkixEcdsaSigValue {
68
- r: Uint8Array;
69
- s: Uint8Array;
70
- }
71
-
72
- export function decodePkcs8PrivateKeyInfo(
73
- value: Uint8Array
74
- ): Pkcs8PrivateKeyInfo {
75
- const privateKeyInfo = AsnParser.parse(value, PrivateKeyInfo);
76
- if (privateKeyInfo?.privateKeyAlgorithm?.algorithm !== oidEllipticCurveKey) {
77
- throw new Error(
78
- `Unexpected private key algorithm: ${privateKeyInfo?.privateKeyAlgorithm?.algorithm}`
79
- );
80
- }
81
-
82
- const privateKey = AsnParser.parse(
83
- privateKeyInfo.privateKey.buffer,
84
- ECPrivateKey
85
- );
86
-
87
- return {
88
- version: privateKeyInfo.version,
89
- privateKeyAlgorithm: privateKeyInfo.privateKeyAlgorithm,
90
- privateKey: {
91
- version: privateKey.version,
92
- parameters: privateKey.parameters,
93
- privateKey: new Uint8Array(privateKey.privateKey.buffer),
94
- publicKey: privateKey.publicKey
95
- ? new Uint8Array(privateKey.publicKey)
96
- : undefined,
97
- },
98
- };
99
- }
100
-
101
- export function encodePkixSubjectPublicKeyInfo(
102
- keyInfo: PkixSubjectPublicKeyInfo
103
- ): Uint8Array {
104
- const asn1 = new SubjectPublicKeyInfo({
105
- algorithm: new AlgorithmIdentifier({
106
- algorithm: keyInfo.algorithm.algorithm,
107
- parameters: AsnConvert.serialize(
108
- new ECParameters(keyInfo.algorithm.parameters)
109
- ),
110
- }),
111
- subjectPublicKey: keyInfo.subjectPublicKey,
112
- });
113
- return new Uint8Array(AsnConvert.serialize(asn1));
114
- }
115
-
116
- export function decodePkixSubjectPublicKeyInfo(
117
- value: Uint8Array
118
- ): PkixSubjectPublicKeyInfo {
119
- const subjectPublicKeyInfo = AsnParser.parse(value, SubjectPublicKeyInfo);
120
- if (subjectPublicKeyInfo?.algorithm?.algorithm !== oidEllipticCurveKey) {
121
- throw new Error(
122
- `Unexpected public key algorithm: ${subjectPublicKeyInfo?.algorithm?.algorithm}`
123
- );
124
- }
125
-
126
- let parameters: PkixSubjectPublicKeyInfo['algorithm']['parameters'];
127
- if (subjectPublicKeyInfo.algorithm.parameters) {
128
- parameters = AsnParser.parse(
129
- subjectPublicKeyInfo.algorithm.parameters,
130
- ECParameters
131
- );
132
- }
133
-
134
- return {
135
- algorithm: {
136
- algorithm: subjectPublicKeyInfo.algorithm.algorithm,
137
- parameters,
138
- },
139
- subjectPublicKey: new Uint8Array(subjectPublicKeyInfo.subjectPublicKey),
140
- };
141
- }
142
-
143
- export function encodePkixEcdsaSigValue(
144
- sigValue: PkixEcdsaSigValue
145
- ): Uint8Array {
146
- const asn1 = new ECDSASigValue({
147
- r: sigValue.r,
148
- s: sigValue.s,
149
- });
150
- return new Uint8Array(AsnConvert.serialize(asn1));
151
- }
152
-
153
- export function decodePkixEcdsaSigValue(value: Uint8Array): PkixEcdsaSigValue {
154
- const subjectPublicKeyInfo = AsnParser.parse(value, ECDSASigValue);
155
- return {
156
- r: new Uint8Array(subjectPublicKeyInfo.r),
157
- s: new Uint8Array(subjectPublicKeyInfo.s),
158
- };
159
- }
package/src/crypto.ts DELETED
@@ -1,101 +0,0 @@
1
- import {
2
- Curve,
3
- Hash,
4
- PrivateKeyFormat,
5
- PublicKeyFormat,
6
- SignatureFormat,
7
- } from './types';
8
-
9
- export class UnsupportedCurveError extends Error {}
10
- export class UnsupportedHashError extends Error {}
11
- export class UnsupportedPrivateKeyFormatError extends Error {}
12
- export class UnsupportedPublicKeyFormatError extends Error {}
13
- export class UnsupportedSignatureFormatError extends Error {}
14
-
15
- export interface CryptoAdapter {
16
- /**
17
- * @param value - Encoded private key value
18
- * @param format - Encoding format
19
- * @return A private key instance
20
- * @throws UnsupportedCurveError
21
- * @throws UnsupportedPrivateKeyFormatError
22
- * @throws Error
23
- */
24
- decodeEcPrivateKey(
25
- value: Uint8Array,
26
- format: PrivateKeyFormat
27
- ): EcPrivateKey | Promise<EcPrivateKey>;
28
-
29
- /**
30
- * @param value - Encoded public key value
31
- * @param format - Encoding format
32
- * @return A public key instance
33
- * @throws UnsupportedCurveError
34
- * @throws UnsupportedPublicKeyFormatError
35
- * @throws Error
36
- */
37
- decodeEcPublicKey(
38
- value: Uint8Array,
39
- format: PublicKeyFormat
40
- ): EcPublicKey | Promise<EcPublicKey>;
41
-
42
- /**
43
- * @param data - Data to be signed
44
- * @param privateKey - Private key to use for signing
45
- * @param hash - Hash to apply
46
- * @param format - Format to return the signature in
47
- * @return Signature encoded in the specified format
48
- * @throws UnsupportedHashError
49
- * @throws UnsupportedSignatureFormatError
50
- * @throws Error
51
- */
52
- sign(
53
- data: Uint8Array,
54
- privateKey: EcPrivateKey,
55
- hash: Hash,
56
- format: SignatureFormat
57
- ): Uint8Array | Promise<Uint8Array>;
58
-
59
- /**
60
- * @param signature - X.509 ECDSASigValue ASN.1 type DER encoded
61
- * @param data - Raw value
62
- * @param key - The public key to verify against
63
- * @param hash - Hash to apply
64
- * @param format - Format the signature is encoded in
65
- * @return true if the signature is valid
66
- * @throws UnsupportedSignatureFormatError
67
- * @throws Error
68
- */
69
- verify(
70
- signature: Uint8Array,
71
- data: Uint8Array,
72
- key: EcPublicKey,
73
- hash: Hash,
74
- format: SignatureFormat
75
- ): boolean | Promise<boolean>;
76
- }
77
-
78
- export interface EcPublicKey {
79
- /**
80
- * @return The curve of the public key
81
- */
82
- getCurve(): Curve;
83
-
84
- /**
85
- * @param format - Format to encode with
86
- * @return Encoded public key
87
- */
88
- encode(format: PublicKeyFormat): Uint8Array | Promise<Uint8Array>;
89
- }
90
-
91
- export interface EcPrivateKey {
92
- /**
93
- * @return The curve of the private key
94
- */
95
- getCurve(): Curve;
96
-
97
- /**
98
- * @return The public key, if available
99
- */
100
- getPublicKey(): EcPublicKey | null;
101
- }
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './types';
2
- export * from './asn1';
3
- export * from './crypto';
package/src/types.ts DELETED
@@ -1,13 +0,0 @@
1
- export type Curve =
2
- | 'brainpool256r1'
3
- | 'brainpool384r1'
4
- | 'secp192k1'
5
- | 'secp192r1'
6
- | 'secp256k1'
7
- | 'secp256r1'
8
- | 'secp384r1';
9
-
10
- export type PrivateKeyFormat = 'pkcs8-der'; // PKCS#8 PrivateKeyInfo ASN.1 type DER encoded
11
- export type PublicKeyFormat = 'spki-der'; // X.509 SubjectPublicKeyInfo ASN.1 type DER encoded
12
- export type SignatureFormat = 'sigvalue-der'; // X.509 ECDSASigValue ASN.1 type DER encoded
13
- export type Hash = 'SHA-256';
package/tsconfig.json DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "include": ["src/**/*"]
4
- }