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 enum DlpKeySize { 57 DLP_AES_KEY_SIZE_128 = 128, 58 DLP_AES_KEY_SIZE_192 = 192, 59 DLP_AES_KEY_SIZE_256 = 256, 60 }; 61 62 struct DlpBlob { 63 uint32_t size = 0; 64 uint8_t* data = nullptr; 65 }; 66 67 struct DlpCipherParam { 68 struct DlpBlob iv; 69 }; 70 71 struct DlpUsageSpec { 72 uint32_t mode; 73 struct DlpCipherParam* algParam; 74 }; 75 76 enum DlpCipherMode { 77 DLP_MODE_CTR = 1, 78 }; 79 80 enum DlpKeyPadding { 81 DLP_PADDING_NONE = 0, 82 DLP_PADDING_OAEP = 1, 83 DLP_PADDING_PSS = 2, 84 DLP_PADDING_PKCS1_V1_5 = 3, 85 DLP_PADDING_PKCS5 = 4, 86 DLP_PADDING_PKCS7 = 5, 87 }; 88 89 #define SELF_FREE_PTR(PTR, FREE_FUNC) \ 90 { \ 91 if ((PTR) != NULL) { \ 92 FREE_FUNC(PTR); \ 93 (PTR) = NULL; \ 94 } \ 95 } 96 97 #define DLP_FREE_PTR(p) SELF_FREE_PTR(p, free) 98 99 int32_t DlpOpensslGenerateRandomKey(uint32_t keySize, struct DlpBlob* key); 100 101 int32_t DlpOpensslAesEncrypt(const struct DlpBlob* key, const struct DlpUsageSpec* usageSpec, 102 const struct DlpBlob* message, struct DlpBlob* cipherText); 103 104 int32_t DlpOpensslAesDecrypt(const struct DlpBlob* key, const struct DlpUsageSpec* usageSpec, 105 const struct DlpBlob* message, struct DlpBlob* plainText); 106 107 int32_t DlpOpensslAesEncryptInit(void** cryptoCtx, const struct DlpBlob* key, const struct DlpUsageSpec* usageSpec); 108 109 int32_t DlpOpensslAesEncryptUpdate(void* cryptoCtx, const struct DlpBlob* message, struct DlpBlob* cipherText); 110 111 int32_t DlpOpensslAesEncryptFinal(void** cryptoCtx, const struct DlpBlob* message, struct DlpBlob* cipherText); 112 113 int32_t DlpOpensslAesDecryptInit(void** cryptoCtx, const struct DlpBlob* key, const struct DlpUsageSpec* usageSpec); 114 115 int32_t DlpOpensslAesDecryptUpdate(void* cryptoCtx, const struct DlpBlob* message, struct DlpBlob* plainText); 116 117 int32_t DlpOpensslAesDecryptFinal(void** cryptoCtx, const struct DlpBlob* message, struct DlpBlob* plainText); 118 119 void DlpOpensslAesHalFreeCtx(void** cryptoCtx); 120 121 int32_t DlpOpensslHash(uint32_t alg, const struct DlpBlob* msg, struct DlpBlob* hash); 122 123 int32_t DlpOpensslHashInit(void** cryptoCtx, uint32_t alg); 124 125 int32_t DlpOpensslHashUpdate(void* cryptoCtx, const struct DlpBlob* msg); 126 127 int32_t DlpOpensslHashFinal(void** cryptoCtx, const struct DlpBlob* msg, struct DlpBlob* hash); 128 129 int32_t DlpOpensslHashFreeCtx(void** cryptoCtx); 130 131 int32_t DlpCtrModeIncreaeIvCounter(struct DlpBlob& iv, uint32_t count); 132 133 int32_t DlpHmacEncode(const DlpBlob& key, int32_t fd, DlpBlob& out); 134 #ifdef __cplusplus 135 } 136 #endif 137 138 #endif 139