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 #include "ed25519_key.h" 17 18 #include <stddef.h> 19 20 #include "adaptor_algorithm.h" 21 #include "adaptor_log.h" 22 #include "buffer.h" 23 #include "defines.h" 24 25 static KeyPair *g_keyPair = NULL; 26 GenerateKeyPair(void)27ResultCode GenerateKeyPair(void) 28 { 29 DestoryKeyPair(g_keyPair); 30 g_keyPair = GenerateEd25519KeyPair(); 31 if (!IsEd25519KeyPairValid(g_keyPair)) { 32 LOG_ERROR("GenerateEd25519Keypair fail"); 33 return RESULT_GENERAL_ERROR; 34 } 35 return RESULT_SUCCESS; 36 } 37 GetPriKey(void)38const Buffer *GetPriKey(void) 39 { 40 if (!IsEd25519KeyPairValid(g_keyPair)) { 41 LOG_ERROR("g_keyPair is invalid"); 42 return NULL; 43 } 44 return g_keyPair->priKey; 45 } 46 GetPubKey(void)47const Buffer *GetPubKey(void) 48 { 49 if (!IsEd25519KeyPairValid(g_keyPair)) { 50 LOG_ERROR("g_keyPair is invalid"); 51 return NULL; 52 } 53 return g_keyPair->pubKey; 54 } 55 DestoryEd25519KeyPair(void)56void DestoryEd25519KeyPair(void) 57 { 58 DestoryKeyPair(g_keyPair); 59 g_keyPair = NULL; 60 } 61 ExecutorMsgSign(const Buffer * data)62Buffer *ExecutorMsgSign(const Buffer *data) 63 { 64 if (!IsEd25519KeyPairValid(g_keyPair)) { 65 return NULL; 66 } 67 Buffer *signatrue = NULL; 68 int32_t ret = Ed25519Sign(g_keyPair, data, &signatrue); 69 if (ret != RESULT_SUCCESS) { 70 LOG_ERROR("sign failed"); 71 return NULL; 72 } 73 return signatrue; 74 }