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