• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 "buffer.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif // __cplusplus
26 
27 #define ED25519_FIX_SIGN_BUFFER_SIZE 64
28 #define AES256_BLOCK_SIZE 16
29 #define AES256_KEY_SIZE 32
30 #define SECRET_SIZE 32
31 #define HKDF_SALT_SIZE 32
32 #define HKDF_KEY_SIZE 32
33 #define AES_GCM_VI_SIZE 12
34 #define AES_GCM_TAG_SIZE 16
35 #define CIPHER_INFO_MAX_SIZE 1024
36 #define SHA256_DIGEST_SIZE 32
37 
38 typedef struct {
39     Buffer *pubKey;
40     Buffer *priKey;
41 } KeyPair;
42 
43 bool IsEd25519KeyPairValid(const KeyPair *keyPair);
44 void DestoryKeyPair(KeyPair *keyPair);
45 KeyPair *GenerateEd25519KeyPair(void);
46 int32_t Ed25519Sign(const KeyPair *keyPair, const Buffer *data, Buffer **sign);
47 int32_t Ed25519Verify(const Buffer *pubKey, const Buffer *data, const Buffer *sign);
48 
49 Buffer *Aes256GcmEncryptNoPadding(const Buffer *plaintext, const Buffer *key);
50 Buffer *Aes256GcmDecryptNoPadding(const Buffer *cipherInfo, const Buffer *key);
51 Buffer *DeriveDeviceKey(const Buffer *pinData, const Buffer *secret);
52 Buffer *Hkdf(const Buffer *salt, const Buffer *rootKey);
53 Buffer *Sha256Adaptor(const Buffer *data);
54 
55 int32_t HmacSha256(const Buffer *hmacKey, const Buffer *data, Buffer **hmac);
56 int32_t HmacSha512(const Buffer *hmacKey, const Buffer *data, Buffer **hmac);
57 
58 int32_t SecureRandom(uint8_t *buffer, uint32_t size);
59 
60 #ifdef __cplusplus
61 }
62 #endif // __cplusplus
63 #endif // ADAPTOR_ALGORITHM_H
64 
65