1/* 2 * Copyright (C) 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 AES_COMMON_H 17#define AES_COMMON_H 18 19#include "algorithm_parameter.h" 20#include "blob.h" 21#include "cipher.h" 22#include "sym_key.h" 23 24#ifdef __cplusplus 25extern "C" { 26#endif 27 28static const int32_t FILE_BLOCK_SIZE = 1024; 29static const int32_t RAND_MAX_NUM = 100; 30static const bool IS_DEBUG = false; 31static constexpr int32_t CIPHER_TEXT_LEN = 128; 32static constexpr int32_t KEY_MATERIAL_LEN = 16; 33static constexpr int32_t AES_IV_LEN = 16; // iv for CBC|CTR|OFB|CFB mode 34static constexpr int32_t GCM_IV_LEN = 12; // GCM 35static constexpr int32_t GCM_AAD_LEN = 8; 36static constexpr int32_t GCM_TAG_LEN = 16; 37static constexpr int32_t GCM_IV_LONG_LEN = 16; 38static constexpr int32_t GCM_IV_SHORT_LEN = 9; 39static constexpr int32_t GCM_AAD_LONG_LEN = 2049; 40static constexpr int32_t GCM_AAD_SHORT_LEN = 1; 41static constexpr int32_t CCM_IV_LEN = 7; // CCM 42static constexpr int32_t CCM_AAD_LEN = 8; 43static constexpr int32_t CCM_TAG_LEN = 12; 44static constexpr int32_t PLAINTEXT_LEN = 13; 45static constexpr int32_t AES_KEY_SIZE = 128; 46 47void PrintfHex(const char *tag, uint8_t *in, int inLen); 48int32_t GenerateSymKey(const char *algoName, HcfSymKey **key); 49int32_t ConvertSymKey(const char *algoName, HcfSymKey **key); 50 51/* just rand data fill file for test */ 52int32_t GeneratorFile(const char *fileName, int32_t genFileSize); 53int32_t CompareFileContent(void); 54int32_t AesMultiBlockEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params); 55int32_t AesMultiBlockDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params); 56 57// use ECB, test abnormal input 58int32_t AesEncryptWithInput(HcfCipher *cipher, HcfSymKey *key, HcfBlob *input, 59 uint8_t *cipherText, int *cipherTextLen); 60int32_t AesEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params, 61 uint8_t *cipherText, int *cipherTextLen); 62int32_t AesDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params, 63 uint8_t *cipherText, int cipherTextLen); 64int32_t AesNoUpdateEncWithInput(HcfCipher *cipher, HcfSymKey *key, HcfBlob *input, 65 uint8_t *cipherText, int *cipherTextLen); 66 67// test encrypt and decrypt with null plain text 68int32_t AesDecryptEmptyMsg(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params, 69 uint8_t *cipherText, int cipherTextLen); 70int32_t AesNoUpdateEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params, 71 uint8_t *cipherText, int *cipherTextLen); 72int32_t AesNoUpdateDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params, 73 uint8_t *cipherText, int cipherTextLen); 74 75#ifdef __cplusplus 76} 77#endif 78#endif