gdc-common-utils-ts 1.0.1
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/PUBLISHING.md +33 -0
- package/__tests__/AesManager.test.ts +53 -0
- package/__tests__/CryptographyService.test.ts +194 -0
- package/__tests__/bundle.test.ts +29 -0
- package/__tests__/content.test.ts +72 -0
- package/__tests__/crypto-encode-decode.test.ts +52 -0
- package/__tests__/crypto-hmac.test.ts +21 -0
- package/__tests__/did-generateServiceId.errors.test.ts +8 -0
- package/__tests__/did-generateServiceId.test.ts +18 -0
- package/__tests__/models-clinical-sections.test.ts +32 -0
- package/__tests__/models-multibase58.test.ts +33 -0
- package/__tests__/multibase58.errors.test.ts +7 -0
- package/__tests__/multibase58.test.ts +28 -0
- package/__tests__/multibasehash.test.ts +25 -0
- package/__tests__/utils-actor.test.ts +22 -0
- package/__tests__/utils-base-convert.test.ts +57 -0
- package/__tests__/utils-baseN.test.ts +40 -0
- package/__tests__/utils-did-extra.test.ts +33 -0
- package/__tests__/utils-format-converter.test.ts +87 -0
- package/__tests__/utils-jwt.test.ts +57 -0
- package/__tests__/utils-manager-error.test.ts +11 -0
- package/__tests__/utils-normalize.test.ts +15 -0
- package/__tests__/utils-object-convert.test.ts +38 -0
- package/__tests__/utils-string-convert.test.ts +20 -0
- package/__tests__/utils-string-utils.test.ts +25 -0
- package/__tests__/utils-url.test.ts +21 -0
- package/babel.config.cjs +5 -0
- package/jest.config.ts +46 -0
- package/package.json +36 -0
- package/src/AesManager.ts +82 -0
- package/src/CryptographyService.ts +461 -0
- package/src/JweManager.ts.txt +365 -0
- package/src/KmsService.txt +493 -0
- package/src/constants/Schemas.ts +61 -0
- package/src/constants/index.ts +1 -0
- package/src/constants/schemaorg.ts +193 -0
- package/src/cryptoDecode.ts +104 -0
- package/src/cryptoEncode.ts +36 -0
- package/src/cryptography.abstract.ts +29 -0
- package/src/hmac.ts +15 -0
- package/src/index.ts +3 -0
- package/src/interfaces/Cryptography.types.ts +131 -0
- package/src/interfaces/ICryptoHelper.ts +33 -0
- package/src/interfaces/ICryptography.ts +177 -0
- package/src/interfaces/IWallet.ts +62 -0
- package/src/interfaces/MlDsa.ts +25 -0
- package/src/interfaces/MlKem.ts +18 -0
- package/src/models/aes.ts +93 -0
- package/src/models/auth.ts +38 -0
- package/src/models/bundle.ts +152 -0
- package/src/models/bundle.txt +93 -0
- package/src/models/clinical-sections.en.ts +82 -0
- package/src/models/clinical-sections.ts +64 -0
- package/src/models/comm.ts +63 -0
- package/src/models/confidential-job.ts +100 -0
- package/src/models/confidential-message.ts +137 -0
- package/src/models/confidential-storage.ts +170 -0
- package/src/models/consent-rule.ts +141 -0
- package/src/models/crypto.ts +43 -0
- package/src/models/device-license.ts +161 -0
- package/src/models/did.ts +81 -0
- package/src/models/index.ts +31 -0
- package/src/models/indexing.ts +20 -0
- package/src/models/issue.ts +85 -0
- package/src/models/jsonapi.ts +19 -0
- package/src/models/jwe.ts +132 -0
- package/src/models/jwk.ts +50 -0
- package/src/models/jws.ts +42 -0
- package/src/models/jwt.ts +15 -0
- package/src/models/multibase58.ts +46 -0
- package/src/models/oidc4ida.common.model.ts +39 -0
- package/src/models/oidc4ida.document.model.ts +61 -0
- package/src/models/oidc4ida.electronicRecord.model.ts +86 -0
- package/src/models/oidc4ida.evidence.model.ts +69 -0
- package/src/models/openid-device.ts +146 -0
- package/src/models/operation-outcome.ts +34 -0
- package/src/models/params.ts +142 -0
- package/src/models/resource-document.ts +21 -0
- package/src/models/response.ts +5 -0
- package/src/models/urlPath.ts +76 -0
- package/src/models/verifiable-credential.ts +52 -0
- package/src/types/noble-hashes.d.ts +4 -0
- package/src/utils/actor.ts +52 -0
- package/src/utils/base-convert.ts +77 -0
- package/src/utils/baseN.ts +203 -0
- package/src/utils/bundle.ts +30 -0
- package/src/utils/content.ts +66 -0
- package/src/utils/did.ts +155 -0
- package/src/utils/format-converter.ts +119 -0
- package/src/utils/index.ts +13 -0
- package/src/utils/jwt.ts +165 -0
- package/src/utils/manager-error.ts +27 -0
- package/src/utils/multibase58.ts +46 -0
- package/src/utils/multibasehash.ts +28 -0
- package/src/utils/normalize.ts +43 -0
- package/src/utils/object-convert.ts +57 -0
- package/src/utils/string-convert.ts +71 -0
- package/src/utils/string-utils.ts +70 -0
- package/src/utils/url.ts +46 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// Copyright 2025 Antifraud Services Inc. under the Apache License, Version 2.0.
|
|
2
|
+
// File: crypto-ts/interfaces/ICryptography.ts
|
|
3
|
+
|
|
4
|
+
import { JweObject } from '../models/jwe';
|
|
5
|
+
import { ProtectedDataAES } from '../models/aes';
|
|
6
|
+
import { MlkemPublicJwk, MldsaPublicJwk, PublicJwk, MlkemPrivateJwk, MldsaAlg, MlkemCurve } from './Cryptography.types';
|
|
7
|
+
import { DataCompactJWT, JwtCompactParts } from '../models/jwt';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Defines the class for the low-level, stateless cryptography utility (the "Engine").
|
|
11
|
+
*/
|
|
12
|
+
export interface ICryptography {
|
|
13
|
+
|
|
14
|
+
// --- Key Generation ---
|
|
15
|
+
/**
|
|
16
|
+
* Generates a ML-KEM (Kyber) key pair.
|
|
17
|
+
* @param seedBytes Optional 64-byte seed for deterministic key generation.
|
|
18
|
+
* @param crv The desired security level. Defaults to 'ML-KEM-768'.
|
|
19
|
+
*/
|
|
20
|
+
generateKeyPairMlKem(seedBytes?: Uint8Array, crv?: MlkemCurve): Promise<{ publicJWKey: MlkemPublicJwk & { kid: string }; secretKeyBytes: Uint8Array }>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Generates a ML-DSA (Dilithium) key pair.
|
|
24
|
+
* @param seedBytes Optional 32-byte seed for deterministic key generation.
|
|
25
|
+
* @param alg The desired security level. Defaults to 'ML-DSA-44'.
|
|
26
|
+
*/
|
|
27
|
+
generateKeyPairMlDsa(seedBytes?: Uint8Array, alg?: MldsaAlg): Promise<{ publicJWKey: MldsaPublicJwk & { kid: string }; secretKeyBytes: Uint8Array }>;
|
|
28
|
+
|
|
29
|
+
// --- Low-Level Primitives (Symmetric AES) ---
|
|
30
|
+
/**
|
|
31
|
+
* Encrypts a plaintext string using AES-GCM (a symmetric algorithm) and returns the components.
|
|
32
|
+
* This is the core symmetric encryption primitive.
|
|
33
|
+
* @param plaintext The stringified data to encrypt.
|
|
34
|
+
* @param cekBytes The 32-byte Content Encryption Key (Symmetric Key).
|
|
35
|
+
* @param aad The base64url-encoded 'JWE Protected Header', which serves as the 'Additional Authenticated Data' (AAD) for integrity verification.
|
|
36
|
+
* @returns A promise resolving to the JWE-compatible encrypted components (ciphertext, iv, tag).
|
|
37
|
+
*/
|
|
38
|
+
encrypt(plaintext: string, cekBytes: Uint8Array, aad: string): Promise<ProtectedDataAES>
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Decrypts JWE-compatible encrypted components back to a plaintext string.
|
|
42
|
+
* @param encryptedData The object containing the base64url-encoded ciphertext, iv, and tag.
|
|
43
|
+
* @param cekBytes The 32-byte Content Encryption Key.
|
|
44
|
+
* @param aad The base64url-encoded 'JWE Protected Header', which serves as the 'Additional Authenticated Data' (AAD) for integrity verification.
|
|
45
|
+
* @returns A promise resolving to the decrypted plaintext string.
|
|
46
|
+
*/
|
|
47
|
+
decrypt(
|
|
48
|
+
encryptedData: ProtectedDataAES,
|
|
49
|
+
cekBytes: Uint8Array,
|
|
50
|
+
aad: string
|
|
51
|
+
): Promise<string>
|
|
52
|
+
|
|
53
|
+
// --- Post-Quantum Computing ---
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Generates and protects (encapsulates) a symmetric shared key (32 bytes)
|
|
57
|
+
* @param cekSeedBytes
|
|
58
|
+
* @param secretKeyBytes
|
|
59
|
+
* @param recipientPublicKeyBytes
|
|
60
|
+
*/
|
|
61
|
+
encapsulate(cekSeedBytes: Uint8Array, secretKeyBytes: Uint8Array, recipientPublicKeyBytes: Uint8Array): Promise<{ encapsulatedCekBytes: Uint8Array; derivedCekBytes: Uint8Array; }>
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns the unprotected shared symmetric key
|
|
65
|
+
* @param encapsulatedBytes
|
|
66
|
+
* @param secretKeyBytes
|
|
67
|
+
*/
|
|
68
|
+
decapsulate(encapsulatedBytes: Uint8Array, secretKeyBytes: Uint8Array): Promise<Uint8Array>;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Signs a byte array using a specified ML-DSA algorithm.
|
|
72
|
+
* @param payloadBytes The raw data to sign.
|
|
73
|
+
* @param secretKeyBytes The private signing key.
|
|
74
|
+
* @param alg The ML-DSA algorithm to use (e.g., 'ML-DSA-44').
|
|
75
|
+
* @returns A promise resolving to the raw signature bytes.
|
|
76
|
+
*/
|
|
77
|
+
signBytes(payloadBytes: Uint8Array, secretKeyBytes: Uint8Array, alg: MldsaAlg): Promise<Uint8Array>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Verifies a signature against a byte array and a public key.
|
|
81
|
+
* The algorithm is inferred from the `alg` property of the publicJWKey.
|
|
82
|
+
* @param signatureBytes The raw signature to verify.
|
|
83
|
+
* @param dataBytes The original data that was signed.
|
|
84
|
+
* @param publicJWKey The public key to use for verification.
|
|
85
|
+
* @returns A promise resolving to true if the signature is valid, false otherwise.
|
|
86
|
+
*/
|
|
87
|
+
verifyBytes(signatureBytes: Uint8Array, dataBytes: Uint8Array, publicJWKey: PublicJwk): Promise<boolean>;
|
|
88
|
+
|
|
89
|
+
// --- High-Level Workflows ---
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Encrypts a payload into a JWE Object, suitable for JSON General Serialization.
|
|
93
|
+
* This method keeps protected and per-recipient headers separate, making it ideal
|
|
94
|
+
* for multi-recipient scenarios or storing as a structured object (e.g., ConfidentialStorage).
|
|
95
|
+
* @param payload The JSON object to encrypt.
|
|
96
|
+
* @param protectedHeader The main protected header (JWE Protected Header). Used as AAD.
|
|
97
|
+
* @param secretJWKey The sender's private ML-KEM key.
|
|
98
|
+
* @param recipientsJWKeys An array of public ML-KEM keys for the recipients.
|
|
99
|
+
* @returns A Promise resolving to a JweObject.
|
|
100
|
+
*/
|
|
101
|
+
encryptJwe(payload: object, protectedHeader: object, secretJWKey: MlkemPrivateJwk, recipientsJWKeys: MlkemPublicJwk[]): Promise<JweObject>;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Encrypts a payload directly into a JWE Compact Serialization string.
|
|
105
|
+
* This method is optimized for single-recipient JWEs. It merges the protected and
|
|
106
|
+
* recipient headers *before* encryption to form the correct AAD for the compact format.
|
|
107
|
+
* @param payload The JSON object to encrypt or a nested JWS string (compact representation).
|
|
108
|
+
* @param protectedHeader The main protected header (e.g., specifying `enc`).
|
|
109
|
+
* @param secretJWKey The sender's private ML-KEM key.
|
|
110
|
+
* @param recipientJWKey The single recipient's public ML-KEM key.
|
|
111
|
+
* @returns A Promise resolving to the JWE as a compact string.
|
|
112
|
+
*/
|
|
113
|
+
encryptJweToCompact(payload: object | string, protectedHeader: object, secretJWKey: MlkemPrivateJwk, recipientJWKey: MlkemPublicJwk): Promise<string>;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Decrypts a JWE (in Compact or JSON format) and returns the decrypted bytes and protected header.
|
|
117
|
+
* This method identifies the correct recipient using the `kid` from the provided private JWK.
|
|
118
|
+
* @param jwe The JWE object or Compact JWE string.
|
|
119
|
+
* @param secretKeyJwk The private key of the recipient, containing the `kid` to find the
|
|
120
|
+
* correct recipient and the `dBytes` for the decapsulation operation.
|
|
121
|
+
* @returns A promise resolving to an object containing the decrypted bytes and the decoded protected header.
|
|
122
|
+
*/
|
|
123
|
+
decryptJwe(
|
|
124
|
+
jwe: JweObject | string,
|
|
125
|
+
secretKeyJwk: MlkemPrivateJwk
|
|
126
|
+
): Promise<{ decryptedBytes: Uint8Array, protectedHeader: object }>;
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @param jwe The JWE object or Compact/JSON JWE string.
|
|
131
|
+
* @returns An array of strings, where each string is a recipient's `kid`. Returns an empty array if no kids are found.
|
|
132
|
+
*/
|
|
133
|
+
getRecipientKidsFromJwe(jwe: JweObject | string): string[];
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Creates a JWS using the payload and header objects, and the signer's private key bytes.
|
|
137
|
+
*/
|
|
138
|
+
signDataJws(payload: object, protectedHeader: object, secretJWKey: Uint8Array): Promise<JwtCompactParts>;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Verifies the signature of a JWS Object against the signer's public key (JWK).
|
|
142
|
+
*/
|
|
143
|
+
verifyJws(jws: JwtCompactParts | string, publicJWKey: PublicJwk): Promise<boolean>;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Verifies a detached JWS signature against the original payload.
|
|
147
|
+
* @param payloadBytes The original, unencoded byte stream that was signed.
|
|
148
|
+
* @param detachedJws The JWS in detached format ('header..signature').
|
|
149
|
+
* @param publicJWKey The signer's public key (JWK) to use for verification.
|
|
150
|
+
* @returns A boolean indicating if the signature is valid.
|
|
151
|
+
*/
|
|
152
|
+
verifyDetachedJws(payloadBytes: Uint8Array, detachedJws: string, publicJWKey: PublicJwk): Promise<boolean>;
|
|
153
|
+
|
|
154
|
+
// --- Formatting & Parsing Utilities (Note: from cryptoEncode, cryptoDecode, jwt utils ... ---
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Converts a JWS Object (with decoded headers and payload) into Compact Serialization format.
|
|
158
|
+
* @param jws The JWS Object to convert.
|
|
159
|
+
* @returns The JWS in Compact Serialization format (three base64url strings joined by dots).
|
|
160
|
+
*/
|
|
161
|
+
jwsToCompact(jws: DataCompactJWT): string;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Parses a JWS in Compact Serialization format into a JWS Object with decoded headers and payload.
|
|
165
|
+
* @param jwsString The compact JWS string.
|
|
166
|
+
* @returns A JWS Object with JSON objects for the header and payload.
|
|
167
|
+
*/
|
|
168
|
+
parseCompactJws(jwsString: string): DataCompactJWT;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Parses a JWE in Compact Serialization format into a JWE Object.
|
|
172
|
+
* @param jweString The compact JWE string.
|
|
173
|
+
* @returns A JWE Object.
|
|
174
|
+
*/
|
|
175
|
+
parseCompactJwe(jweString: string): JweObject;
|
|
176
|
+
}
|
|
177
|
+
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Copyright 2025 Antifraud Services Inc. under the Apache License, Version 2.0.
|
|
2
|
+
// File: crypto-ts/interfaces/IWallet.ts
|
|
3
|
+
|
|
4
|
+
import { JwkSet } from '../models/jwk';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @interface IWallet
|
|
8
|
+
* Defines the contract for a client-side Wallet, acting as the "frontend KmsService".
|
|
9
|
+
* It provides high-level cryptographic capabilities without exposing low-level primitives.
|
|
10
|
+
* @sdk
|
|
11
|
+
*/
|
|
12
|
+
export interface IWallet {
|
|
13
|
+
/**
|
|
14
|
+
* Provisions a new, full set of cryptographic keys for a given entity identifier.
|
|
15
|
+
* This is the primary method for creating a new cryptographic identity.
|
|
16
|
+
* @param entityId The unique identifier for the key set (e.g., a profile ID).
|
|
17
|
+
* @returns A Promise that resolves to the public parts of the generated keys in a JWKSet format.
|
|
18
|
+
*/
|
|
19
|
+
provisionKeys(entityId: string): Promise<JwkSet>;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Creates a cryptographic digest (hash) of a string.
|
|
23
|
+
* @param data The string to hash.
|
|
24
|
+
* @param algorithm The digest algorithm to use.
|
|
25
|
+
* @returns A Promise that resolves to the hex-encoded hash string.
|
|
26
|
+
*/
|
|
27
|
+
digest(data: string, algorithm: any): Promise<string>;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Encrypts a document for secure, local storage (at-rest).
|
|
31
|
+
* @param doc The document to protect, which must have a `.content` property.
|
|
32
|
+
* @param entityId The ID of the entity whose keys should be used for encryption.
|
|
33
|
+
* @returns A Promise that resolves to the protected document, where `.content` is replaced by `.jwe`.
|
|
34
|
+
*/
|
|
35
|
+
protectConfidentialData(doc: any, entityId: string): Promise<any>;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Decrypts a document from secure storage.
|
|
39
|
+
* @param doc The protected document containing the `.jwe` property.
|
|
40
|
+
* @param entityId The ID of the entity whose keys should be used for decryption.
|
|
41
|
+
* @returns A Promise that resolves to the document with the decrypted `.content`.
|
|
42
|
+
*/
|
|
43
|
+
unprotectConfidentialData(doc: any, entityId: string): Promise<any>;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* (Optional) Packs a DIDComm message into a secure format (JWE/JARM) for a recipient.
|
|
47
|
+
* This is required for FAPI-compliant flows.
|
|
48
|
+
* @param content The DIDComm message content to pack.
|
|
49
|
+
* @param recipientDid The DID of the recipient.
|
|
50
|
+
* @returns A Promise that resolves to the packed, secure message string.
|
|
51
|
+
*/
|
|
52
|
+
packForRecipient?(content: any, recipientDid: string): Promise<string>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* (Optional) Unpacks a secure message (JWE/JARM) received from a server.
|
|
56
|
+
* This is the counterpart to `packForRecipient`.
|
|
57
|
+
* @param packedMessage The secure message string (e.g., a compact JWE).
|
|
58
|
+
* @returns A Promise that resolves to an object containing the plaintext `content` and any cryptographic `meta` data.
|
|
59
|
+
*/
|
|
60
|
+
unpack?(packedMessage: string): Promise<{ content: any, meta: any }>;
|
|
61
|
+
}
|
|
62
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Copyright 2025 Antifraud Services Inc. under the Apache License, Version 2.0.
|
|
2
|
+
// File: crypto-ts/interfaces/MlDsa.ts
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* level 2: privateKeySize = 2528, publicKeySize = 1312, signatureSize = 2420
|
|
6
|
+
* level 3: privateKeySize = 4000, publicKeySize = 1952, signatureSize = 3293
|
|
7
|
+
* level 5: privateKeySize = 4864, publicKeySize = 2592, signatureSize = 4595
|
|
8
|
+
* @see https://openquantumsafe.org/liboqs/algorithms/sig/dilithium.html
|
|
9
|
+
*/
|
|
10
|
+
;
|
|
11
|
+
|
|
12
|
+
// Size of a packed public key: 32 + PolyT1Size*K
|
|
13
|
+
export const MlDsaPubKeySizeLevel2 = 1312;
|
|
14
|
+
export const MlDsaPubKeySizeLevel3 = 1952;
|
|
15
|
+
export const MlDsaPubKeySizeLevel5 = 2592;
|
|
16
|
+
|
|
17
|
+
// Size of a packed private key : 32 + 32 + 32 + polyLeqEtaSize*(l+k) + PolyT0Size*K
|
|
18
|
+
export const MlDsaPrivKeySizeLevel2 = 2528;
|
|
19
|
+
export const MlDsaPrivKeySizeLevel3 = 4000;
|
|
20
|
+
export const MlDsaPrivKeySizeLevel5 = 4864;
|
|
21
|
+
|
|
22
|
+
// Size of a packed signature: l*polyLeGamma1Size + omega + k + 32
|
|
23
|
+
export const MlDsaSignatureSizeLevel2 = 2420;
|
|
24
|
+
export const MlDsaSignatureSizeLevel3 = 3293;
|
|
25
|
+
export const MlDsaSignatureSizeLevel5 = 4595;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Copyright 2025 Antifraud Services Inc. under the Apache License, Version 2.0.
|
|
2
|
+
// File: crypto-ts/interfaces/MlKem.ts
|
|
3
|
+
|
|
4
|
+
export const paramsSymBytes = 32;
|
|
5
|
+
|
|
6
|
+
export const paramsPolyBytes = 384;
|
|
7
|
+
export const paramsPolyvecBytesK512 = 2 * paramsPolyBytes;
|
|
8
|
+
export const paramsPolyvecBytesK768 = 3 * paramsPolyBytes;
|
|
9
|
+
export const paramsPolyvecBytesK1024 = 4 * paramsPolyBytes;
|
|
10
|
+
|
|
11
|
+
/** Kyber512PKBytes is a constant representing the byte length of public keys in Kyber-512 */
|
|
12
|
+
export const Kyber512PKBytes = paramsPolyvecBytesK512 + paramsSymBytes;
|
|
13
|
+
|
|
14
|
+
/** Kyber768PKBytes is a constant representing the byte length of public keys in Kyber-768 */
|
|
15
|
+
export const Kyber768PKBytes = paramsPolyvecBytesK768 + paramsSymBytes;
|
|
16
|
+
|
|
17
|
+
/** Kyber1024PKBytes is a constant representing the byte length of public keys in Kyber-1024 */
|
|
18
|
+
export const Kyber1024PKBytes = paramsPolyvecBytesK1024 + paramsSymBytes;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Copyright 2025 Antifraud Services Inc. under the Apache License, Version 2.0.
|
|
2
|
+
// File: crypto-ts/models/aes.ts
|
|
3
|
+
|
|
4
|
+
export const WORD_BYTES=4;
|
|
5
|
+
export const WORD_BITS=WORD_BYTES*8; // sjcl BitArray are words of 32 bits.
|
|
6
|
+
export const AES_GCM_256_KEY_SIZE_BITS=256; // key size is 32 bytes = 256 bits (by default it is 128 bits).
|
|
7
|
+
export const AES_GCM_TAG_SIZE_BITS=128; // tag size is 16 bytes = 128 bits (by default it is 64 bits).
|
|
8
|
+
export const AES_GCM_NONCE_SIZE_BITS=128; // NIST 800-38D 8.2.2 (RGB Construction of IV) allows to be 16 bytes (128 bits).
|
|
9
|
+
export const AES_GCM_JWA_ENC='A256GCM' // AES GCM using 256-bit key, see https://datatracker.ietf.org/doc/html/rfc7518#section-5.1
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Represents the raw, binary components of a payload encrypted with AES-GCM.
|
|
13
|
+
* This interface is used for in-memory cryptographic operations before any
|
|
14
|
+
* Base64Url encoding is applied. Its string-based counterpart for transport
|
|
15
|
+
* is `ProtectedDataAES`.
|
|
16
|
+
*/
|
|
17
|
+
export interface AesGcmComponents {
|
|
18
|
+
/**
|
|
19
|
+
* The encrypted content as a raw byte array.
|
|
20
|
+
*/
|
|
21
|
+
ciphertext: Uint8Array;
|
|
22
|
+
/**
|
|
23
|
+
* The Initialization Vector (IV) as a raw byte array.
|
|
24
|
+
*/
|
|
25
|
+
iv: Uint8Array;
|
|
26
|
+
/**
|
|
27
|
+
* The Authentication Tag generated by the GCM mode.
|
|
28
|
+
*/
|
|
29
|
+
authTag: Uint8Array;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Represents the components of an AES-GCM encrypted payload, with each
|
|
34
|
+
* component encoded as a Base64Url string. This is suitable for use in
|
|
35
|
+
* transport structures like a JWE. Its byte-based counterpart for in-memory
|
|
36
|
+
* operations is `AesGcmComponents`.
|
|
37
|
+
*
|
|
38
|
+
* - ciphertext: base64url encoded bytes of the plaintext
|
|
39
|
+
* - tag: base64url encoded
|
|
40
|
+
* - iv: base64url encoded (it is like a nonce)
|
|
41
|
+
*/
|
|
42
|
+
export interface ProtectedDataAES {
|
|
43
|
+
ciphertext: string;
|
|
44
|
+
tag: string;
|
|
45
|
+
iv: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
/** Use it instead of the SjclCipherDecryptParams to avoid problems with encryption / decryption
|
|
50
|
+
* iv: required random bytes created for Initialization Vector (nonce) created when doing the encryption, base64 encoded (but not Base64url nor BitArray).
|
|
51
|
+
* adata: AAD (Additional Authenticated Data) base64 encoded (e.g.: JWE protected headers), but not base64url nor BitArray. It can be and empty string "".
|
|
52
|
+
* ct: ciphertext and tag combined and then base64 encoded, but not base64url nor BitArray.
|
|
53
|
+
* ts: tagsize is only required if a value other than the sjcl default value (64 bits) is defined in the encryption options (to know what size authentication tag is part of the cipher text)
|
|
54
|
+
* mode: "ccm", "gcm" (the default is "ccm").
|
|
55
|
+
* ks: keysize is only required if a value other than the sjcl default value (128 bits) is defined in the encryption options (to know what size key to generate with Pbkdf2)
|
|
56
|
+
* iter: iterations for Pbkdf2
|
|
57
|
+
* v: scjl version
|
|
58
|
+
* cipher: "aes"
|
|
59
|
+
*/
|
|
60
|
+
export interface DecryptionDataWithParametersSJCL {
|
|
61
|
+
iv: string; // required random bytes for Initialization Vector (nonce) base64 encoded, but not Base64url nor BitArray.
|
|
62
|
+
adata: string; // required AAD (Additional Authenticated Data) base64 encoded (e.g.: JWE protected headers), but not base64url nor BitArray. It can be and empty string "".
|
|
63
|
+
ct: string; // ciphertext and tag combined and then base64 encoded, but not base64url nor BitArray.
|
|
64
|
+
ts?: number; // tagsize is only required if a value other than the sjcl default value (64 bits) is defined in the encryption options (to know what size authentication tag is part of the cipher text)
|
|
65
|
+
mode?: string; // "ccm", "gcm" (the default is "ccm").
|
|
66
|
+
ks?: number; // keysize is only required if a value other than the sjcl default value (128 bits) is defined in the encryption options (to know what size key to generate with Pbkdf2)
|
|
67
|
+
iter?: number; // iterations for Pbkdf2
|
|
68
|
+
v?: number; // scjl version (optional, 1 is the default)
|
|
69
|
+
cipher?:string; // "aes"
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/** Use it instead of the SjclCipherEncryptParams to avoid problems with encryption / decryption
|
|
73
|
+
* iv: required BitArray containing the random bytes for the Initialization Vector (nonce).
|
|
74
|
+
* adata: required BitArray containing the AAD (Additional Authenticated Data), e.g.: JWE protected headers.
|
|
75
|
+
* ts: tagsize is only required if a value other than the sjcl default value (64 bits) is used (to know what size authentication tag is part of the cipher text).
|
|
76
|
+
* mode: "ccm", "gcm" (the default is "ccm").
|
|
77
|
+
* ks: keysize is only required if a value other than the sjcl default value (128 bits) is used (to know what size key to generate with Pbkdf2)
|
|
78
|
+
* iter: iterations for Pbkdf2
|
|
79
|
+
* salt: BitArray, a 64 bits salt it is created automatically if not provided when generating the key from a password (KDF).
|
|
80
|
+
* v: scjl version
|
|
81
|
+
* cipher: "aes"
|
|
82
|
+
*/
|
|
83
|
+
export interface EncryptionParametersSJCL {
|
|
84
|
+
iv: any; // required sjcl.BitArray containing the random bytes for the Initialization Vector (nonce).
|
|
85
|
+
adata: any; // required sjcl.BitArray containing the AAD (Additional Authenticated Data), e.g.: JWE protected headers.
|
|
86
|
+
ts?: number; // tagsize is only required if a value other than the sjcl default value (64 bits) is used (to know what size authentication tag is part of the cipher text).
|
|
87
|
+
mode?: string; // "ccm", "gcm" (the default is "ccm").
|
|
88
|
+
ks?: number; // keysize is only required if a value other than the sjcl default value (128 bits) is used (to know what size key to generate with Pbkdf2)
|
|
89
|
+
iter?: number; // iterations for Pbkdf2
|
|
90
|
+
salt?: any; // sjcl.BitArray, a 64 bits salt it is created automatically if not provided when generating the key from a password (KDF).
|
|
91
|
+
v?: number; // scjl version (optional, 1 is the default)
|
|
92
|
+
cipher?: string; // "aes"
|
|
93
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Copyright 2025 Antifraud Services Inc. under the Apache License, Version 2.0.
|
|
2
|
+
// File: src/models/auth.ts
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Defines the structure of the claims object decoded from a bearer access token.
|
|
6
|
+
* This object is typically attached to the request object by an authentication middleware.
|
|
7
|
+
*/
|
|
8
|
+
export interface IAccessTokenClaims {
|
|
9
|
+
/**
|
|
10
|
+
* Issuer of the token (e.g., the DID of the issuing tenant).
|
|
11
|
+
* This is the source of truth for identifying the tenant.
|
|
12
|
+
*/
|
|
13
|
+
iss: string;
|
|
14
|
+
/**
|
|
15
|
+
* Subject of the token (e.g., the DID of the employee).
|
|
16
|
+
*/
|
|
17
|
+
sub: string;
|
|
18
|
+
/**
|
|
19
|
+
* Audience for which the token is intended.
|
|
20
|
+
*/
|
|
21
|
+
aud: string;
|
|
22
|
+
/**
|
|
23
|
+
* Expiration time (Unix timestamp).
|
|
24
|
+
*/
|
|
25
|
+
exp: number;
|
|
26
|
+
/**
|
|
27
|
+
* Issued at time (Unix timestamp).
|
|
28
|
+
*/
|
|
29
|
+
iat: number;
|
|
30
|
+
/**
|
|
31
|
+
* The scope of permissions granted by the token.
|
|
32
|
+
*/
|
|
33
|
+
scope: string;
|
|
34
|
+
/**
|
|
35
|
+
* Client ID - The client that requested the token.
|
|
36
|
+
*/
|
|
37
|
+
client_id: string;
|
|
38
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/models/bundle.ts
|
|
3
|
+
* @copyright 2025 Antifraud Services Inc. under the Apache License, Version 2.0.
|
|
4
|
+
*
|
|
5
|
+
* @summary
|
|
6
|
+
* This file defines the canonical data structures for "Bundles" and their "Entries"
|
|
7
|
+
* within this system, following a hybrid FHIR / JSON:API pattern. It is the single
|
|
8
|
+
* source of truth for the shape of request and response bodies.
|
|
9
|
+
*
|
|
10
|
+
* @architecture
|
|
11
|
+
* The models herein are critical for maintaining architectural consistency.
|
|
12
|
+
*
|
|
13
|
+
* 1. **`BundleJsonApi`**: The top-level container for all batch operations. Corresponds
|
|
14
|
+
* to the `body` of a DIDComm message or a standard API request.
|
|
15
|
+
*
|
|
16
|
+
* 2. **`BundleEntry`**: The core component of a Bundle. Represents a single unit of work,
|
|
17
|
+
* such as registering one organization or creating one employee. It has a strict
|
|
18
|
+
* structure:
|
|
19
|
+
* - `type`: A string that defines the business action (e.g., 'Organization-registration-offer-v1.0').
|
|
20
|
+
* - `meta`: A **TOP-LEVEL** property containing the original `claims` for the operation.
|
|
21
|
+
* This is crucial for both processing and error reporting.
|
|
22
|
+
* - `resource`: A FHIR-like resource object that is the subject of the action. It
|
|
23
|
+
* contains the structured data derived from the claims.
|
|
24
|
+
* - `request`/`response`: Contextual objects indicating the operation's details or result.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { OperationOutcome } from "./operation-outcome";
|
|
28
|
+
|
|
29
|
+
// ===================================================================================
|
|
30
|
+
// BUNDLE ENTRY COMPONENTS
|
|
31
|
+
// ===================================================================================
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Defines the `request` property for an entry in a request Bundle.
|
|
35
|
+
* This indicates the intended action for the entry.
|
|
36
|
+
*/
|
|
37
|
+
export interface BundleRequest {
|
|
38
|
+
method: 'POST' | 'PUT' | 'DELETE' | 'GET';
|
|
39
|
+
url: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Defines the `response` property for an entry in a response Bundle.
|
|
44
|
+
* This indicates the outcome of the action for the entry.
|
|
45
|
+
*/
|
|
46
|
+
export interface BundleResponse {
|
|
47
|
+
/** The HTTP status code as a string (e.g., "201", "404"). */
|
|
48
|
+
status: string;
|
|
49
|
+
[key: string]: any;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Defines the `meta` property that holds the original, unprocessed claims for a BundleEntry.
|
|
54
|
+
* This ensures that the complete context of a request is preserved through the entire
|
|
55
|
+
* asynchronous workflow and is available for error reporting.
|
|
56
|
+
*/
|
|
57
|
+
export interface BundleEntryMeta {
|
|
58
|
+
/** The original, flattened claims record for this specific entry. */
|
|
59
|
+
claims?: Record<string, any>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// ===================================================================================
|
|
63
|
+
// BUNDLE ENTRY TYPES
|
|
64
|
+
// ===================================================================================
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @deprecated Use `BundleEntry` instead. This will be removed in a future version.
|
|
68
|
+
* Represents a single entry in an INCOMING request Bundle.
|
|
69
|
+
*/
|
|
70
|
+
export interface BundleEntryRequest {
|
|
71
|
+
id?: string;
|
|
72
|
+
type: string;
|
|
73
|
+
request: BundleRequest;
|
|
74
|
+
resource?: Record<string, any>;
|
|
75
|
+
meta?: BundleEntryMeta;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @deprecated Use `BundleEntry` instead. This will be removed in a future version.
|
|
80
|
+
* Represents a single successful entry in an OUTGOING response Bundle.
|
|
81
|
+
*/
|
|
82
|
+
export interface BundleEntryResponse {
|
|
83
|
+
id?: string;
|
|
84
|
+
type: string;
|
|
85
|
+
response: BundleResponse;
|
|
86
|
+
resource?: Record<string, any>;
|
|
87
|
+
meta?: BundleEntryMeta;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Represents a single error entry in a response Bundle.
|
|
92
|
+
*
|
|
93
|
+
* @architecture
|
|
94
|
+
* CRITICAL: When an error occurs for a specific request entry, the corresponding error
|
|
95
|
+
* entry in the response MUST include the original, unprocessed `meta` object from that
|
|
96
|
+
* request entry. This allows the client to correlate the exact input that caused the failure.
|
|
97
|
+
*/
|
|
98
|
+
export interface ErrorEntry {
|
|
99
|
+
/** An optional unique identifier for this entry. */
|
|
100
|
+
id?: string;
|
|
101
|
+
/** The `type` of the original request entry that failed. */
|
|
102
|
+
type: string;
|
|
103
|
+
/** The original, unprocessed `meta` object from the request entry that failed. */
|
|
104
|
+
meta?: BundleEntryMeta;
|
|
105
|
+
/** The details of the error that occurred. */
|
|
106
|
+
response: {
|
|
107
|
+
/** The HTTP status code reflecting the error (e.g., "400", "500"). */
|
|
108
|
+
status: string;
|
|
109
|
+
/** A FHIR OperationOutcome resource providing detailed error diagnostics. */
|
|
110
|
+
outcome: OperationOutcome;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Represents a single, canonical entry within a `BundleJsonApi`.
|
|
116
|
+
* This structure is used for both requests and successful responses.
|
|
117
|
+
*
|
|
118
|
+
* @property {string} type - A string identifying the business action of the entry.
|
|
119
|
+
* @property {BundleEntryMeta} meta - **(TOP-LEVEL PROPERTY)** Contains the original, unprocessed claims.
|
|
120
|
+
* @property {object} resource - The primary FHIR-like resource that is the subject of the action.
|
|
121
|
+
* @property {BundleRequest} [request] - Details of the requested operation (for request bundles).
|
|
122
|
+
* @property {BundleResponse} [response] - Details of the operation outcome (for response bundles).
|
|
123
|
+
*/
|
|
124
|
+
export type BundleEntry = {
|
|
125
|
+
id?: string;
|
|
126
|
+
type: string;
|
|
127
|
+
meta?: BundleEntryMeta;
|
|
128
|
+
resource?: Record<string, any>;
|
|
129
|
+
request?: BundleRequest;
|
|
130
|
+
response?: BundleResponse;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ===================================================================================
|
|
134
|
+
// BUNDLE DEFINITION
|
|
135
|
+
// ===================================================================================
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Represents the canonical Bundle structure used as the `body` of a request or response.
|
|
139
|
+
* The generic type `T` allows for specifying whether the `data` array contains request
|
|
140
|
+
* entries or response entries, providing strong type safety.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* const requestBundle: BundleJsonApi<BundleEntry> = { ... };
|
|
144
|
+
* const responseBundle: BundleJsonApi<BundleEntry | ErrorEntry> = { ... };
|
|
145
|
+
*/
|
|
146
|
+
export interface BundleJsonApi<T = BundleEntry | ErrorEntry> {
|
|
147
|
+
data: T[];
|
|
148
|
+
resourceType: 'Bundle';
|
|
149
|
+
total?: number;
|
|
150
|
+
type: string;
|
|
151
|
+
}
|
|
152
|
+
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// src/models/bundle.ts
|
|
2
|
+
// Copyright 2025 Antifraud Services Inc. under the Apache License, Version 2.0.
|
|
3
|
+
|
|
4
|
+
import { OperationOutcome } from "./fhir/operation-outcome";
|
|
5
|
+
|
|
6
|
+
// ===================================================================================
|
|
7
|
+
// BUNDLE ENTRY COMPONENTS
|
|
8
|
+
// ===================================================================================
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Defines the `request` property for an entry in a request Bundle.
|
|
12
|
+
*/
|
|
13
|
+
export interface BundleRequest {
|
|
14
|
+
method: 'POST' | 'PUT' | 'DELETE' | 'GET';
|
|
15
|
+
url: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Defines the `response` property for an entry in a response Bundle.
|
|
20
|
+
*/
|
|
21
|
+
export interface BundleResponse {
|
|
22
|
+
status: string; // e.g., "201"
|
|
23
|
+
[key: string]: any;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Defines the `meta` property that can hold contextual information.
|
|
28
|
+
*/
|
|
29
|
+
export interface BundleEntryMeta {
|
|
30
|
+
claims?: Record<string, any>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ===================================================================================
|
|
34
|
+
// BUNDLE ENTRY TYPES
|
|
35
|
+
// ===================================================================================
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Represents a single entry in an INCOMING request Bundle.
|
|
39
|
+
*/
|
|
40
|
+
export interface BundleEntryRequest {
|
|
41
|
+
id?: string;
|
|
42
|
+
type: string;
|
|
43
|
+
request: BundleRequest;
|
|
44
|
+
resource?: Record<string, any>;
|
|
45
|
+
meta?: BundleEntryMeta;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Represents a single successful entry in an OUTGOING response Bundle.
|
|
50
|
+
*/
|
|
51
|
+
export interface BundleEntryResponse {
|
|
52
|
+
id?: string;
|
|
53
|
+
type: string;
|
|
54
|
+
response: BundleResponse;
|
|
55
|
+
resource?: Record<string, any>;
|
|
56
|
+
meta?: BundleEntryMeta;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Represents a single error entry in an OUTGOING response Bundle.
|
|
61
|
+
*/
|
|
62
|
+
export interface ErrorEntry {
|
|
63
|
+
id?: string;
|
|
64
|
+
type: string;
|
|
65
|
+
meta?: BundleEntryMeta; // Preserves original context
|
|
66
|
+
response: {
|
|
67
|
+
status: string;
|
|
68
|
+
outcome: OperationOutcome;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** A union type for backward compatibility where the distinction is not yet needed. */
|
|
73
|
+
|
|
74
|
+
export type BundleEntry = BundleEntryRequest | BundleEntryResponse | ErrorEntry
|
|
75
|
+
|
|
76
|
+
// ===================================================================================
|
|
77
|
+
// BUNDLE DEFINITION
|
|
78
|
+
// ===================================================================================
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Represents the canonical Bundle structure.
|
|
82
|
+
* The generic type `T` allows us to specify whether the `data` array
|
|
83
|
+
* contains request entries or response entries, providing strong type safety.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* const requestBundle: Bundle<BundleEntryRequest> = { ... };
|
|
87
|
+
* const responseBundle: Bundle<BundleEntryResponse | ErrorEntry> = { ... };
|
|
88
|
+
*/
|
|
89
|
+
export interface Bundle<T = BundleEntryRequest | BundleEntryResponse | ErrorEntry> {
|
|
90
|
+
type: string;
|
|
91
|
+
total?: number;
|
|
92
|
+
data: T[];
|
|
93
|
+
}
|