1 /* 2 * Copyright (c) 2023-2024 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 DLP_CRYPT_H 17 #define DLP_CRYPT_H 18 19 #include <stdint.h> 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 enum DlpKeyDigest { 26 DLP_DIGEST_NONE = 0, 27 DLP_DIGEST_SHA256 = 12, 28 DLP_DIGEST_SHA384 = 13, 29 DLP_DIGEST_SHA512 = 14, 30 }; 31 32 struct DlpOpensslAesCtx { 33 uint32_t mode; 34 uint32_t padding; 35 void* append; 36 }; 37 38 enum DLP_DIGEST_LEN { 39 SHA256_LEN = 32, 40 SHA384_LEN = 48, 41 SHA512_LEN = 64, 42 }; 43 44 #define OPENSSL_CTX_PADDING_NONE (0) /* set chipher padding none */ 45 #define OPENSSL_CTX_PADDING_ENABLE (1) /* set chipher padding enable */ 46 47 #define DLP_BITS_PER_BYTE (8) 48 #define DLP_KEY_BYTES(keySize) (((keySize) + DLP_BITS_PER_BYTE - 1) / DLP_BITS_PER_BYTE) 49 50 #define DLP_OPENSSL_ERROR_LEN 128 51 52 #define DLP_OPENSSL_SUCCESS 1 /* openssl return 1: success */ 53 54 #define BIT_NUM_OF_UINT8 8 55 56 #define DLP_RANDOM_MAX_SIZE 1024 57 58 enum DlpKeySize { 59 DLP_AES_KEY_SIZE_128 = 128, 60 DLP_AES_KEY_SIZE_192 = 192, 61 DLP_AES_KEY_SIZE_256 = 256, 62 }; 63 64 struct DlpBlob { 65 uint32_t size = 0; 66 uint8_t* data = nullptr; 67 }; 68 69 struct DlpCipherParam { 70 struct DlpBlob iv; 71 }; 72 73 struct DlpUsageSpec { 74 uint32_t mode; 75 struct DlpCipherParam* algParam; 76 }; 77 78 enum DlpCipherMode { 79 DLP_MODE_CTR = 1, 80 }; 81 82 enum DlpKeyPadding { 83 DLP_PADDING_NONE = 0, 84 DLP_PADDING_OAEP = 1, 85 DLP_PADDING_PSS = 2, 86 DLP_PADDING_PKCS1_V1_5 = 3, 87 DLP_PADDING_PKCS5 = 4, 88 DLP_PADDING_PKCS7 = 5, 89 }; 90 91 #define SELF_FREE_PTR(PTR, FREE_FUNC) \ 92 { \ 93 if ((PTR) != NULL) { \ 94 FREE_FUNC(PTR); \ 95 (PTR) = NULL; \ 96 } \ 97 } 98 99 #define DLP_FREE_PTR(p) SELF_FREE_PTR(p, free) 100 101 int32_t DlpOpensslGenerateRandom(uint32_t keySize, struct DlpBlob* key); 102 103 int32_t DlpOpensslGenerateRandomKey(uint32_t keySize, struct DlpBlob* key); 104 105 int32_t DlpOpensslAesEncrypt(const struct DlpBlob* key, const struct DlpUsageSpec* usageSpec, 106 const struct DlpBlob* message, struct DlpBlob* cipherText); 107 108 int32_t DlpOpensslAesDecrypt(const struct DlpBlob* key, const struct DlpUsageSpec* usageSpec, 109 const struct DlpBlob* message, struct DlpBlob* plainText); 110 111 int32_t DlpOpensslAesEncryptInit(void** cryptoCtx, const struct DlpBlob* key, const struct DlpUsageSpec* usageSpec); 112 113 int32_t DlpOpensslAesEncryptUpdate(void* cryptoCtx, const struct DlpBlob* message, struct DlpBlob* cipherText); 114 115 int32_t DlpOpensslAesEncryptFinal(void** cryptoCtx, const struct DlpBlob* message, struct DlpBlob* cipherText); 116 117 int32_t DlpOpensslAesDecryptInit(void** cryptoCtx, const struct DlpBlob* key, const struct DlpUsageSpec* usageSpec); 118 119 int32_t DlpOpensslAesDecryptUpdate(void* cryptoCtx, const struct DlpBlob* message, struct DlpBlob* plainText); 120 121 int32_t DlpOpensslAesDecryptFinal(void** cryptoCtx, const struct DlpBlob* message, struct DlpBlob* plainText); 122 123 void DlpOpensslAesHalFreeCtx(void** cryptoCtx); 124 125 int32_t DlpOpensslHash(uint32_t alg, const struct DlpBlob* msg, struct DlpBlob* hash); 126 127 int32_t DlpOpensslHashInit(void** cryptoCtx, uint32_t alg); 128 129 int32_t DlpOpensslHashUpdate(void* cryptoCtx, const struct DlpBlob* msg); 130 131 int32_t DlpOpensslHashFinal(void** cryptoCtx, const struct DlpBlob* msg, struct DlpBlob* hash); 132 133 int32_t DlpOpensslHashFreeCtx(void** cryptoCtx); 134 135 int32_t DlpCtrModeIncreaeIvCounter(struct DlpBlob& iv, uint32_t count); 136 137 int32_t DlpHmacEncode(const DlpBlob& key, int32_t fd, DlpBlob& out); 138 #ifdef __cplusplus 139 } 140 #endif 141 142 #endif 143