summaryrefslogtreecommitdiffstats
path: root/third_party/js/PKI.js/src/CryptoEngine/CryptoEngineInterface.ts
blob: 8aab69c6045cea090694978958627eb3b393b1f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import type * as asn1js from "asn1js";
import type { AlgorithmIdentifier } from "../AlgorithmIdentifier";
import type { EncryptedContentInfo } from "../EncryptedContentInfo";
import type { PublicKeyInfo } from "../PublicKeyInfo";

export type CryptoEngineAlgorithmOperation = "sign" | "encrypt" | "generateKey" | "importKey" | "exportKey" | "verify";

/**
 * Algorithm parameters
 */
export interface CryptoEngineAlgorithmParams {
  /**
   * Algorithm
   */
  algorithm: Algorithm | object;
  /**
   * Key usages
   */
  usages: KeyUsage[];
}

export interface CryptoEngineSignatureParams {
  signatureAlgorithm: AlgorithmIdentifier;
  parameters: CryptoEngineAlgorithmParams;
}

export interface CryptoEngineSignWithPrivateKeyParams {
  algorithm: Algorithm;
}

/**
 * Public key parameters
 */
export interface CryptoEnginePublicKeyParams {
  /**
   * Algorithm
   */
  algorithm: CryptoEngineAlgorithmParams;
}


export type ContentEncryptionAesCbcParams = AesCbcParams & AesDerivedKeyParams;
export type ContentEncryptionAesGcmParams = AesGcmParams & AesDerivedKeyParams;
export type ContentEncryptionAlgorithm = ContentEncryptionAesCbcParams | ContentEncryptionAesGcmParams;

export interface CryptoEngineEncryptParams {
  password: ArrayBuffer;
  contentEncryptionAlgorithm: ContentEncryptionAlgorithm;
  hmacHashAlgorithm: string;
  iterationCount: number;
  contentToEncrypt: ArrayBuffer;
  contentType: string;
}

export interface CryptoEngineDecryptParams {
  password: ArrayBuffer;
  encryptedContentInfo: EncryptedContentInfo;
}

export interface CryptoEngineStampDataWithPasswordParams {
  password: ArrayBuffer;
  hashAlgorithm: string;
  salt: ArrayBuffer;
  iterationCount: number;
  contentToStamp: ArrayBuffer;
}

export interface CryptoEngineVerifyDataStampedWithPasswordParams {
  password: ArrayBuffer;
  hashAlgorithm: string;
  salt: ArrayBuffer;
  iterationCount: number;
  contentToVerify: ArrayBuffer;
  signatureToVerify: ArrayBuffer;
}

export interface ICryptoEngine extends SubtleCrypto {
  name: string;
  crypto: Crypto;
  subtle: SubtleCrypto;

  getRandomValues<T extends ArrayBufferView | null>(array: T): T;

  /**
   * Get OID for each specific algorithm
   * @param algorithm WebCrypto Algorithm
   * @param safety If `true` throws exception on unknown algorithm. Default is `false`
   * @param target Name of the target
   * @throws Throws {@link Error} exception if unknown WebCrypto algorithm
   */
  getOIDByAlgorithm(algorithm: Algorithm, safety?: boolean, target?: string): string;
  /**
   * Get default algorithm parameters for each kind of operation
   * @param algorithmName Algorithm name to get common parameters for
   * @param operation Kind of operation: "sign", "encrypt", "generateKey", "importKey", "exportKey", "verify"
   */
  // TODO Use safety
  getAlgorithmParameters(algorithmName: string, operation: CryptoEngineAlgorithmOperation): CryptoEngineAlgorithmParams;

  /**
   * Gets WebCrypto algorithm by wel-known OID
   * @param oid algorithm identifier
   * @param safety if `true` throws exception on unknown algorithm identifier
   * @param target name of the target
   * @returns Returns WebCrypto algorithm or an empty object
   */
  getAlgorithmByOID<T extends Algorithm = Algorithm>(oid: string, safety?: boolean, target?: string): T | object;
  /**
   * Gets WebCrypto algorithm by wel-known OID
   * @param oid algorithm identifier
   * @param safety if `true` throws exception on unknown algorithm identifier
   * @param target name of the target
   * @returns Returns WebCrypto algorithm
   * @throws Throws {@link Error} exception if unknown algorithm identifier
   */
  getAlgorithmByOID<T extends Algorithm = Algorithm>(oid: string, safety: true, target?: string): T;

  /**
   * Getting hash algorithm by signature algorithm
   * @param signatureAlgorithm Signature algorithm
   */
  // TODO use safety
  getHashAlgorithm(signatureAlgorithm: AlgorithmIdentifier): string;

  /**
   * Get signature parameters by analyzing private key algorithm
   * @param privateKey The private key user would like to use
   * @param hashAlgorithm Hash algorithm user would like to use. Default is SHA-1
   */
  getSignatureParameters(privateKey: CryptoKey, hashAlgorithm?: string): Promise<CryptoEngineSignatureParams>;

  /**
   * Sign data with pre-defined private key
   * @param data Data to be signed
   * @param privateKey Private key to use
   * @param parameters Parameters for used algorithm
   */
  signWithPrivateKey(data: BufferSource, privateKey: CryptoKey, parameters: CryptoEngineSignWithPrivateKeyParams): Promise<ArrayBuffer>;

  /**
   * Verify data with the public key
   * @param data Data to be verified
   * @param signature Signature value
   * @param publicKeyInfo Public key information
   * @param signatureAlgorithm Signature algorithm
   * @param shaAlgorithm Hash algorithm
   */
  verifyWithPublicKey(data: BufferSource, signature: asn1js.BitString | asn1js.OctetString, publicKeyInfo: PublicKeyInfo, signatureAlgorithm: AlgorithmIdentifier, shaAlgorithm?: string): Promise<boolean>;

  getPublicKey(publicKeyInfo: PublicKeyInfo, signatureAlgorithm: AlgorithmIdentifier, parameters?: CryptoEnginePublicKeyParams): Promise<CryptoKey>;

  /**
   * Specialized function encrypting "EncryptedContentInfo" object using parameters
   * @param parameters
   */
  encryptEncryptedContentInfo(parameters: CryptoEngineEncryptParams): Promise<EncryptedContentInfo>;

  /**
   * Decrypt data stored in "EncryptedContentInfo" object using parameters
   * @param parameters
   */
  decryptEncryptedContentInfo(parameters: CryptoEngineDecryptParams): Promise<ArrayBuffer>;

  /**
   * Stamping (signing) data using algorithm similar to HMAC
   * @param parameters
   */
  stampDataWithPassword(parameters: CryptoEngineStampDataWithPasswordParams): Promise<ArrayBuffer>;
  verifyDataStampedWithPassword(parameters: CryptoEngineVerifyDataStampedWithPasswordParams): Promise<boolean>;
}

export interface CryptoEngineParameters {
  name?: string;
  crypto: Crypto;
  /**
   * @deprecated
   */
  subtle?: SubtleCrypto;
}

export interface CryptoEngineConstructor {
  new(params: CryptoEngineParameters): ICryptoEngine;
}