1 /*
2  * Copyright (C) 2022-2023 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 ADAPTOR_ALGORITHM_H
17 #define ADAPTOR_ALGORITHM_H
18 
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include "securec.h"
22 #include "buffer.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #define ED25519_FIX_SIGN_BUFFER_SIZE 64
29 #define SHA256_DIGEST_SIZE 32
30 #define AES_GCM_TAG_SIZE 16
31 #define AES_GCM_IV_SIZE 12
32 
33 #define USER_AUTH_DISTRIBUTE_DEVICE_KEY "USER_AUTH_DISTRIBUTED_DEVICE_KEY"
34 #define USER_AUTH_DISTRIBUTE_DEVICE_KEY_SIZE 32
35 
36 typedef struct {
37     Buffer *pubKey;
38     Buffer *priKey;
39 } KeyPair;
40 
41 typedef struct {
42     Buffer *key;
43     Buffer *iv;
44     Buffer *aad;
45 } AesGcmParam;
46 
47 bool IsEd25519KeyPairValid(const KeyPair *keyPair);
48 void DestoryKeyPair(KeyPair *keyPair);
49 KeyPair *GenerateEd25519KeyPair(void);
50 int32_t Ed25519Sign(const KeyPair *keyPair, const Buffer *data, Buffer **sign);
51 int32_t Ed25519Verify(const Buffer *pubKey, const Buffer *data, const Buffer *sign);
52 
53 int32_t HmacSha256(const Buffer *hmacKey, const Buffer *data, Buffer **hmac);
54 int32_t SecureRandom(uint8_t *buffer, uint32_t size);
55 
56 int32_t AesGcmEncrypt(const Buffer *plaintext, const AesGcmParam *aesGcmParam, Buffer **ciphertext, Buffer **tag);
57 int32_t AesGcmDecrypt(const Buffer *ciphertext, const AesGcmParam *aesGcmParam, const Buffer *tag, Buffer **plaintext);
58 int32_t GetDistributeKey(const Buffer *peerUdid, const Buffer *salt, Buffer **key);
59 
60 #ifdef __cplusplus
61 }
62 #endif
63 
64 #endif
65 
66