1/* 2 * Copyright (c) 2025-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import type { AsyncCallback, Callback } from './@ohos.base'; 17 18declare namespace cryptoFramework { 19 enum Result { 20 INVALID_PARAMS = 401, 21 NOT_SUPPORT = 801, 22 ERR_OUT_OF_MEMORY = 17620001, 23 ERR_RUNTIME_ERROR = 17620002, 24 ERR_CRYPTO_OPERATION = 17630001 25 } 26 27 interface DataBlob { 28 data: Uint8Array; 29 } 30 31 interface ParamsSpec { 32 algName: string; 33 } 34 35 interface IvParamsSpec extends ParamsSpec { 36 iv: DataBlob; 37 } 38 39 interface GcmParamsSpec extends ParamsSpec { 40 iv: DataBlob; 41 aad: DataBlob; 42 authTag: DataBlob; 43 } 44 45 interface CcmParamsSpec extends ParamsSpec { 46 iv: DataBlob; 47 aad: DataBlob; 48 authTag: DataBlob; 49 } 50 51 enum CryptoMode { 52 ENCRYPT_MODE = 0, 53 DECRYPT_MODE = 1 54 } 55 56 interface KeyEncodingConfig { 57 password: string; 58 cipherName: string; 59 } 60 61 62 interface Key { 63 getEncoded(): DataBlob; 64 readonly format: string; 65 readonly algName: string; 66 } 67 68 interface SymKey extends Key { 69 clearMem(): void; 70 } 71 72 interface PriKey extends Key { 73 clearMem(): void; 74 getAsyKeySpec(itemType: AsyKeySpecItem): bigint | string | number; 75 getEncodedDer(format: string): DataBlob; 76 getEncodedPem(format: string): string; 77 getEncodedPem(format: string, config: KeyEncodingConfig): string; 78 } 79 80 interface PubKey extends Key { 81 getAsyKeySpec(itemType: AsyKeySpecItem): bigint | string | number; 82 getEncodedDer(format: string): DataBlob; 83 getEncodedPem(format: string): string; 84 } 85 86 interface KeyPair { 87 readonly priKey: PriKey; 88 readonly pubKey: PubKey; 89 } 90 91 interface Random { 92 generateRandom(len: number, callback: AsyncCallback<DataBlob>): void; 93 generateRandom(len: number): Promise<DataBlob>; 94 generateRandomSync(len: number): DataBlob; 95 setSeed(seed: DataBlob): void; 96 readonly algName: string; 97 } 98 function createRandom(): Random; 99 100 interface AsyKeyGenerator { 101 generateKeyPair(callback: AsyncCallback<KeyPair>): void; 102 generateKeyPair(): Promise<KeyPair>; 103 generateKeyPairSync(): KeyPair; 104 convertKey(pubKey: DataBlob, priKey: DataBlob, callback: AsyncCallback<KeyPair>): void; 105 convertKey(pubKey: DataBlob | null, priKey: DataBlob | null, callback: AsyncCallback<KeyPair>): void; 106 convertKey(pubKey: DataBlob, priKey: DataBlob): Promise<KeyPair>; 107 convertKey(pubKey: DataBlob | null, priKey: DataBlob | null): Promise<KeyPair>; 108 convertKeySync(pubKey: DataBlob | null, priKey: DataBlob | null): KeyPair; 109 convertPemKey(pubKey: string | null, priKey: string | null): Promise<KeyPair>; 110 convertPemKey(pubKey: string | null, priKey: string | null, password: string): Promise<KeyPair>; 111 convertPemKeySync(pubKey: string | null, priKey: string | null): KeyPair; 112 convertPemKeySync(pubKey: string | null, priKey: string | null, password: string): KeyPair; 113 readonly algName: string; 114 } 115 116 interface SymKeyGenerator { 117 generateSymKey(callback: AsyncCallback<SymKey>): void; 118 generateSymKey(): Promise<SymKey>; 119 generateSymKeySync(): SymKey; 120 convertKey(key: DataBlob, callback: AsyncCallback<SymKey>): void; 121 convertKey(key: DataBlob): Promise<SymKey>; 122 convertKeySync(key: DataBlob): SymKey; 123 readonly algName: string; 124 } 125 126 function createAsyKeyGenerator(algName: string): AsyKeyGenerator; 127 function createSymKeyGenerator(algName: string): SymKeyGenerator; 128 129 interface MacSpec { 130 algName: string; 131 } 132 133 interface HmacSpec extends MacSpec { 134 mdName: string; 135 } 136 137 interface CmacSpec extends MacSpec { 138 cipherName: string; 139 } 140 141 interface Mac { 142 init(key: SymKey, callback: AsyncCallback<void>): void; 143 init(key: SymKey): Promise<void>; 144 initSync(key: SymKey): void; 145 update(input: DataBlob, callback: AsyncCallback<void>): void; 146 update(input: DataBlob): Promise<void>; 147 updateSync(input: DataBlob): void; 148 doFinal(callback: AsyncCallback<DataBlob>): void; 149 doFinal(): Promise<DataBlob>; 150 doFinalSync(): DataBlob; 151 getMacLength(): number; 152 readonly algName: string; 153 } 154 function createMac(algName: string): Mac; 155 function createMac(macSpec: MacSpec): Mac; 156 157 interface Md { 158 update(input: DataBlob, callback: AsyncCallback<void>): void; 159 update(input: DataBlob): Promise<void>; 160 updateSync(input: DataBlob): void; 161 digest(callback: AsyncCallback<DataBlob>): void; 162 digest(): Promise<DataBlob>; 163 digestSync(): DataBlob; 164 getMdLength(): number; 165 readonly algName: string; 166 } 167 function createMd(algName: string): Md; 168 169 enum CipherSpecItem { 170 OAEP_MD_NAME_STR = 100, 171 OAEP_MGF_NAME_STR = 101, 172 OAEP_MGF1_MD_STR = 102, 173 OAEP_MGF1_PSRC_UINT8ARR = 103, 174 SM2_MD_NAME_STR = 104 175 } 176 177 enum SignSpecItem { 178 PSS_MD_NAME_STR = 100, 179 PSS_MGF_NAME_STR = 101, 180 PSS_MGF1_MD_STR = 102, 181 PSS_SALT_LEN_NUM = 103, 182 PSS_TRAILER_FIELD_NUM = 104, 183 SM2_USER_ID_UINT8ARR = 105 184 } 185 186 interface Cipher { 187 init(opMode: CryptoMode, key: Key, params: ParamsSpec, callback: AsyncCallback<void>): void; 188 init(opMode: CryptoMode, key: Key, params: ParamsSpec | null, callback: AsyncCallback<void>): void; 189 init(opMode: CryptoMode, key: Key, params: ParamsSpec): Promise<void>; 190 init(opMode: CryptoMode, key: Key, params: ParamsSpec | null): Promise<void>; 191 initSync(opMode: CryptoMode, key: Key, params: ParamsSpec | null): void; 192 update(data: DataBlob, callback: AsyncCallback<DataBlob>): void; 193 update(data: DataBlob): Promise<DataBlob>; 194 updateSync(data: DataBlob): DataBlob; 195 doFinal(data: DataBlob, callback: AsyncCallback<DataBlob>): void; 196 doFinal(data: DataBlob | null, callback: AsyncCallback<DataBlob>): void; 197 doFinal(data: DataBlob): Promise<DataBlob>; 198 doFinal(data: DataBlob | null): Promise<DataBlob>; 199 doFinalSync(data: DataBlob | null): DataBlob; 200 setCipherSpec(itemType: CipherSpecItem, itemValue: Uint8Array): void; 201 getCipherSpec(itemType: CipherSpecItem): string | Uint8Array; 202 readonly algName: string; 203 } 204 function createCipher(transformation: string): Cipher; 205 206 interface Sign { 207 init(priKey: PriKey, callback: AsyncCallback<void>): void; 208 init(priKey: PriKey): Promise<void>; 209 initSync(priKey: PriKey): void; 210 update(data: DataBlob, callback: AsyncCallback<void>): void; 211 update(data: DataBlob): Promise<void>; 212 updateSync(data: DataBlob): void; 213 sign(data: DataBlob, callback: AsyncCallback<DataBlob>): void; 214 sign(data: DataBlob | null, callback: AsyncCallback<DataBlob>): void; 215 sign(data: DataBlob): Promise<DataBlob>; 216 sign(data: DataBlob | null): Promise<DataBlob>; 217 signSync(data: DataBlob | null): DataBlob; 218 setSignSpec(itemType: SignSpecItem, itemValue: number): void; 219 setSignSpec(itemType: SignSpecItem, itemValue: number | Uint8Array): void; 220 getSignSpec(itemType: SignSpecItem): string | number; 221 readonly algName: string; 222 } 223 224 interface Verify { 225 init(pubKey: PubKey, callback: AsyncCallback<void>): void; 226 init(pubKey: PubKey): Promise<void>; 227 initSync(pubKey: PubKey): void; 228 update(data: DataBlob, callback: AsyncCallback<void>): void; 229 update(data: DataBlob): Promise<void>; 230 updateSync(data: DataBlob): void; 231 verify(data: DataBlob, signatureData: DataBlob, callback: AsyncCallback<boolean>): void; 232 verify(data: DataBlob | null, signatureData: DataBlob, callback: AsyncCallback<boolean>): void; 233 verify(data: DataBlob, signatureData: DataBlob): Promise<boolean>; 234 verify(data: DataBlob | null, signatureData: DataBlob): Promise<boolean>; 235 verifySync(data: DataBlob | null, signatureData: DataBlob): boolean; 236 recover(signatureData: DataBlob): Promise<DataBlob | null>; 237 recoverSync(signatureData: DataBlob): DataBlob | null; 238 setVerifySpec(itemType: SignSpecItem, itemValue: number): void; 239 setVerifySpec(itemType: SignSpecItem, itemValue: number | Uint8Array): void; 240 getVerifySpec(itemType: SignSpecItem): string | number; 241 readonly algName: string; 242 } 243 function createSign(algName: string): Sign; 244 function createVerify(algName: string): Verify; 245 246 interface KeyAgreement { 247 generateSecret(priKey: PriKey, pubKey: PubKey, callback: AsyncCallback<DataBlob>): void; 248 generateSecret(priKey: PriKey, pubKey: PubKey): Promise<DataBlob>; 249 generateSecretSync(priKey: PriKey, pubKey: PubKey): DataBlob; 250 readonly algName: string; 251 } 252 function createKeyAgreement(algName: string): KeyAgreement; 253 254 enum AsyKeySpecItem { 255 DSA_P_BN = 101, 256 DSA_Q_BN = 102, 257 DSA_G_BN = 103, 258 DSA_SK_BN = 104, 259 DSA_PK_BN = 105, 260 ECC_FP_P_BN = 201, 261 ECC_A_BN = 202, 262 ECC_B_BN = 203, 263 ECC_G_X_BN = 204, 264 ECC_G_Y_BN = 205, 265 ECC_N_BN = 206, 266 ECC_H_NUM = 207, 267 ECC_SK_BN = 208, 268 ECC_PK_X_BN = 209, 269 ECC_PK_Y_BN = 210, 270 ECC_FIELD_TYPE_STR = 211, 271 ECC_FIELD_SIZE_NUM = 212, 272 ECC_CURVE_NAME_STR = 213, 273 RSA_N_BN = 301, 274 RSA_SK_BN = 302, 275 RSA_PK_BN = 303, 276 DH_P_BN = 401, 277 DH_G_BN = 402, 278 DH_L_NUM = 403, 279 DH_SK_BN = 404, 280 DH_PK_BN = 405, 281 ED25519_SK_BN = 501, 282 ED25519_PK_BN = 502, 283 X25519_SK_BN = 601, 284 X25519_PK_BN = 602 285 } 286 287 enum AsyKeySpecType { 288 COMMON_PARAMS_SPEC = 0, 289 PRIVATE_KEY_SPEC = 1, 290 PUBLIC_KEY_SPEC = 2, 291 KEY_PAIR_SPEC = 3 292 } 293 294 interface AsyKeySpec { 295 algName: string; 296 specType: AsyKeySpecType; 297 } 298 299 interface DSACommonParamsSpec extends AsyKeySpec { 300 p: bigint; 301 q: bigint; 302 g: bigint; 303 } 304 305 interface DSAPubKeySpec extends AsyKeySpec { 306 params: DSACommonParamsSpec; 307 pk: bigint; 308 } 309 310 interface DSAKeyPairSpec extends AsyKeySpec { 311 params: DSACommonParamsSpec; 312 sk: bigint; 313 pk: bigint; 314 } 315 316 interface ECField { 317 fieldType: string; 318 } 319 320 interface ECFieldFp extends ECField { 321 p: bigint; 322 } 323 324 interface Point { 325 x: bigint; 326 y: bigint; 327 } 328 329 interface ECCCommonParamsSpec extends AsyKeySpec { 330 field: ECField; 331 a: bigint; 332 b: bigint; 333 g: Point; 334 n: bigint; 335 h: number; 336 } 337 338 interface ECCPriKeySpec extends AsyKeySpec { 339 params: ECCCommonParamsSpec; 340 sk: bigint; 341 } 342 343 interface ECCPubKeySpec extends AsyKeySpec { 344 params: ECCCommonParamsSpec; 345 pk: Point; 346 } 347 348 interface ECCKeyPairSpec extends AsyKeySpec { 349 params: ECCCommonParamsSpec; 350 sk: bigint; 351 pk: Point; 352 } 353 354 class ECCKeyUtil { 355 static genECCCommonParamsSpec(curveName: string): ECCCommonParamsSpec; 356 static convertPoint(curveName: string, encodedPoint: Uint8Array): Point; 357 static getEncodedPoint(curveName: string, point: Point, format: string): Uint8Array; 358 } 359 360 interface DHCommonParamsSpec extends AsyKeySpec { 361 p: bigint; 362 g: bigint; 363 l: number; 364 } 365 366 interface DHPriKeySpec extends AsyKeySpec { 367 params: DHCommonParamsSpec; 368 sk: bigint; 369 } 370 371 interface DHPubKeySpec extends AsyKeySpec { 372 params: DHCommonParamsSpec; 373 pk: bigint; 374 } 375 376 interface DHKeyPairSpec extends AsyKeySpec { 377 params: DHCommonParamsSpec; 378 sk: bigint; 379 pk: bigint; 380 } 381 382 class DHKeyUtil { 383 static genDHCommonParamsSpec(pLen: number, skLen?: number): DHCommonParamsSpec; 384 } 385 386 interface ED25519PriKeySpec extends AsyKeySpec { 387 sk: bigint; 388 } 389 390 interface ED25519PubKeySpec extends AsyKeySpec { 391 pk: bigint; 392 } 393 394 interface ED25519KeyPairSpec extends AsyKeySpec { 395 sk: bigint; 396 pk: bigint; 397 } 398 399 interface X25519PriKeySpec extends AsyKeySpec { 400 sk: bigint; 401 } 402 403 interface X25519PubKeySpec extends AsyKeySpec { 404 pk: bigint; 405 } 406 407 interface X25519KeyPairSpec extends AsyKeySpec { 408 sk: bigint; 409 pk: bigint; 410 } 411 412 interface RSACommonParamsSpec extends AsyKeySpec { 413 n: bigint; 414 } 415 416 interface RSAPubKeySpec extends AsyKeySpec { 417 params: RSACommonParamsSpec; 418 pk: bigint; 419 } 420 421 interface RSAKeyPairSpec extends AsyKeySpec { 422 params: RSACommonParamsSpec; 423 sk: bigint; 424 pk: bigint; 425 } 426 427 interface AsyKeyGeneratorBySpec { 428 generateKeyPair(callback: AsyncCallback<KeyPair>): void; 429 generateKeyPair(): Promise<KeyPair>; 430 generateKeyPairSync(): KeyPair; 431 generatePriKey(callback: AsyncCallback<PriKey>): void; 432 generatePriKey(): Promise<PriKey>; 433 generatePriKeySync(): PriKey; 434 generatePubKey(callback: AsyncCallback<PubKey>): void; 435 generatePubKey(): Promise<PubKey>; 436 generatePubKeySync(): PubKey; 437 readonly algName: string; 438 } 439 function createAsyKeyGeneratorBySpec(asyKeySpec: AsyKeySpec): AsyKeyGeneratorBySpec; 440 441 interface KdfSpec { 442 algName: string; 443 } 444 445 interface PBKDF2Spec extends KdfSpec { 446 password: string | Uint8Array; 447 salt: Uint8Array; 448 iterations: number; 449 keySize: number; 450 } 451 452 interface HKDFSpec extends KdfSpec { 453 key: string | Uint8Array; 454 salt: Uint8Array; 455 info: Uint8Array; 456 keySize: number; 457 } 458 459 interface ScryptSpec extends KdfSpec { 460 passphrase: string | Uint8Array; 461 salt: Uint8Array; 462 n: number; 463 r: number; 464 p: number; 465 maxMemory: number; 466 keySize: number; 467 } 468 469 interface Kdf { 470 generateSecret(params: KdfSpec, callback: AsyncCallback<DataBlob>): void; 471 generateSecret(params: KdfSpec): Promise<DataBlob>; 472 generateSecretSync(params: KdfSpec): DataBlob; 473 readonly algName: string; 474 } 475 function createKdf(algName: string): Kdf; 476 477 interface SM2CipherTextSpec { 478 xCoordinate: bigint; 479 yCoordinate: bigint; 480 cipherTextData: Uint8Array; 481 hashData: Uint8Array; 482 } 483 484 class SM2CryptoUtil { 485 static genCipherTextBySpec(spec: SM2CipherTextSpec, mode?: string): DataBlob; 486 static getCipherTextSpec(cipherText: DataBlob, mode?: string): SM2CipherTextSpec; 487 } 488} 489 490export default cryptoFramework; 491