• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 CIPHER_H
17 #define CIPHER_H
18 
19 #include <stdint.h>
20 #include <stdio.h>
21 
22 #ifdef __cplusplus
23 #if __cplusplus
24 extern "C" {
25 #endif
26 #endif /* __cplusplus */
27 
28 #define KEY_LEN              32
29 #define AES_BLOCK_SIZE       16
30 #define ERROR_CODE_GENERAL   -1
31 #define ERROR_SUCCESS         0
32 
33 typedef enum {
34     CIPHER_AES_ECB,
35     CIPHER_AES_CBC
36 } CipherAesMode;
37 
38 typedef struct {
39     char *text;
40     char *key;
41     int32_t action;
42     int32_t textLen;
43     int32_t keyLen;
44 } CryptData;
45 
46 typedef struct {
47     char *transformation;
48     char *ivBuf;
49     int32_t ivOffset;
50     int32_t ivLen;
51 } AesIvMode;
52 
53 typedef struct {
54     CryptData data;
55     CipherAesMode mode;
56     AesIvMode iv;
57 } AesCryptContext;
58 
59 typedef struct {
60     char *data;
61     size_t length;
62 } RsaData;
63 
64 typedef struct {
65     char *trans;
66     char *action;
67     char *key;
68     size_t keyLen;
69 } RsaKeyData;
70 
71 int32_t InitAesCryptData(const char *action, const char *text, const char *key,
72     const AesIvMode *iv, AesCryptContext *aesCryptCxt);
73 int32_t AesCrypt(AesCryptContext *aesCryptCxt);
74 void DeinitAesCryptData(AesCryptContext *aesCryptCxt);
75 int32_t RsaCrypt(RsaKeyData *key, RsaData *inData, RsaData *outData);
76 
77 #ifdef __cplusplus
78 #if __cplusplus
79 }
80 #endif
81 #endif
82 
83 #endif // CIPHER_H