1 /* 2 * Copyright (C) 2021 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 16 #ifndef ALG_DEFS_H 17 #define ALG_DEFS_H 18 19 #include "hc_types.h" 20 #include "string_util.h" 21 22 #define SHA256_LEN 32 23 #define HMAC_LEN 32 24 #define SIGNATURE_LEN 64 25 #define AE_TAG_LEN 16 26 #define BIG_PRIME_LEN_384 384 27 #define BIG_PRIME_LEN_256 256 28 29 typedef enum { 30 PAIR_TYPE_BIND = 0, 31 PAIR_TYPE_CLONE = 1, 32 PAIR_TYPE_END 33 } PairType; // range: 0 ~ 2^8-1 34 35 typedef struct { 36 Uint8Buff authId; 37 int32_t userType; 38 int32_t pairType; 39 } ExtraInfo; 40 41 typedef enum { 42 ED25519 = 0, 43 X25519 = 1, 44 P256 = 2, 45 AES = 3, 46 } Algorithm; 47 48 typedef enum { 49 KEY_PURPOSE_MAC = 0, 50 KEY_PURPOSE_DERIVE = 1, 51 KEY_PURPOSE_SIGN_VERIFY = 2, 52 KEY_PURPOSE_KEY_AGREE = 3 53 } KeyPurpose; 54 55 typedef enum { 56 CURVE_NONE, 57 CURVE_256, 58 CURVE_25519, 59 } CurveType; 60 61 typedef struct { 62 uint8_t *nonce; 63 uint32_t nonceLen; 64 uint8_t *aad; 65 uint32_t aadLen; 66 } GcmParam; 67 68 typedef struct { 69 uint8_t *key; 70 uint32_t keyLen; 71 bool isAlias; 72 } KeyBuff; 73 74 typedef int32_t (*InitAlgFunc)(void); 75 76 typedef int32_t (*Sha256Func)(const Uint8Buff *message, Uint8Buff *hash); 77 78 typedef int32_t (*GenerateRandomFunc)(Uint8Buff *rand); 79 80 typedef int32_t (*ComputeHmacFunc)(const Uint8Buff *key, const Uint8Buff *message, Uint8Buff *outHmac, bool isAlias); 81 82 typedef int32_t (*ComputeHkdfFunc)(const Uint8Buff *baseKey, const Uint8Buff *salt, const Uint8Buff *keyInfo, 83 Uint8Buff *outHkdf, bool isAlias); 84 85 typedef int32_t (*ImportSymmetricKeyFunc)(const Uint8Buff *keyAlias, const Uint8Buff *authToken, KeyPurpose purpose, 86 const ExtraInfo *exInfo); 87 88 typedef int32_t (*CheckKeyExistFunc)(const Uint8Buff *keyAlias); 89 typedef int32_t (*DeleteKeyFunc)(const Uint8Buff *keyAlias); 90 91 typedef int32_t (*AesGcmEncryptFunc)(const Uint8Buff *key, const Uint8Buff *plain, 92 const GcmParam *encryptInfo, bool isAlias, Uint8Buff *outCipher); 93 typedef int32_t (*AesGcmDecryptFunc)(const Uint8Buff *key, const Uint8Buff *cipher, 94 const GcmParam *decryptInfo, bool isAlias, Uint8Buff *outPlain); 95 96 typedef int32_t (*GetTrustAuthIdListFunc)(const Uint8Buff *ownerAuthId, int32_t trustUserType, 97 Uint8Buff *outAuthIdList, uint32_t *outCount); 98 99 typedef int32_t (*HashToPointFunc)(const Uint8Buff *hash, Algorithm algo, Uint8Buff *outEcPoint); 100 101 typedef int32_t (*AgreeSharedSecretWithStorageFunc)(const KeyBuff *priKey, const KeyBuff *pubKey, Algorithm algo, 102 uint32_t sharedKeyLen, const Uint8Buff *sharedKeyAlias); 103 104 typedef int32_t (*AgreeSharedSecretFunc)(const KeyBuff *priKey, const KeyBuff *pubKey, Algorithm algo, 105 Uint8Buff *sharedKey); 106 107 typedef int32_t (*BigNumExpModFunc)(const Uint8Buff *base, const Uint8Buff *exp, const char *bigNumHex, 108 Uint8Buff *outNum); 109 110 typedef int32_t (*GenerateKeyPairWithStorageFunc)(const Uint8Buff *keyAlias, uint32_t keyLen, Algorithm algo, 111 KeyPurpose purpose, const ExtraInfo *exInfo); 112 113 typedef int32_t (*GenerateKeyPairFunc)(Algorithm algo, Uint8Buff *outPriKey, Uint8Buff *outPubKey); 114 115 typedef int32_t (*ExportPublicKeyFunc)(const Uint8Buff *keyAlias, Uint8Buff *outPubKey); 116 117 typedef int32_t (*SignFunc)(const Uint8Buff *keyAlias, const Uint8Buff *message, Algorithm algo, 118 Uint8Buff *outSignature, bool isAlias); 119 120 typedef int32_t (*VerifyFunc)(const Uint8Buff *key, const Uint8Buff *message, Algorithm algo, 121 const Uint8Buff *signature, bool isAlias); 122 123 typedef int32_t (*ImportPublicKeyFunc)(const Uint8Buff *keyAlias, const Uint8Buff *pubKey, Algorithm algo, 124 const ExtraInfo *exInfo); 125 126 typedef bool (*CheckEcPublicKeyFunc)(const Uint8Buff *pubKey, Algorithm algo); 127 128 typedef bool (*CheckDlPublicKeyFunc)(const Uint8Buff *key, const char *primeHex); 129 130 typedef int32_t (*BigNumCompareFunc)(const Uint8Buff *x, const Uint8Buff *y); 131 132 typedef struct { 133 InitAlgFunc initAlg; 134 Sha256Func sha256; 135 GenerateRandomFunc generateRandom; 136 ComputeHmacFunc computeHmac; 137 ComputeHkdfFunc computeHkdf; 138 ImportSymmetricKeyFunc importSymmetricKey; 139 CheckKeyExistFunc checkKeyExist; 140 DeleteKeyFunc deleteKey; 141 AesGcmEncryptFunc aesGcmEncrypt; 142 AesGcmDecryptFunc aesGcmDecrypt; 143 HashToPointFunc hashToPoint; 144 AgreeSharedSecretWithStorageFunc agreeSharedSecretWithStorage; 145 AgreeSharedSecretFunc agreeSharedSecret; 146 BigNumExpModFunc bigNumExpMod; 147 GenerateKeyPairWithStorageFunc generateKeyPairWithStorage; 148 GenerateKeyPairFunc generateKeyPair; 149 ExportPublicKeyFunc exportPublicKey; 150 SignFunc sign; 151 VerifyFunc verify; 152 ImportPublicKeyFunc importPublicKey; 153 CheckDlPublicKeyFunc checkDlPublicKey; 154 CheckEcPublicKeyFunc checkEcPublicKey; 155 BigNumCompareFunc bigNumCompare; 156 } AlgLoader; 157 158 #endif