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