merging the embedded/main into nearby SDK

This commit is contained in:
Shweta
2023-06-05 23:19:07 +00:00
parent b7f7e7aeb9
commit ed2552b88b
36 changed files with 4593 additions and 206 deletions
@@ -0,0 +1,6 @@
all:
gcc -DuECC_ENABLE_VLI_API=1 -DuECC_VLI_NATIVE_LITTLE_ENDIAN=1 -DuECC_SUPPORTS_secp160r1=1 -c -o uECC.o uECC.c
ar rcs libmicro-ecc.a uECC.o
cp libmicro-ecc.a ../../../out/gLinux/third_party/micro-ecc
clean:
rm -f libmicro-ecc.a
@@ -0,0 +1,69 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <nearby.h>
#include <uECC.h>
#include <uECC_vli.h>
#include "nearby_fp_library.h"
#include "nearby_platform_se.h"
#include "nearby_trace.h"
nearby_platform_status nearby_platform_GetSecp160r1PublicKey(
const uint8_t private_key[32], uint8_t output[20],
uint8_t *hashed_flag_field) {
uECC_Curve curve = uECC_secp160r1();
uint8_t public_key[40];
uint8_t priv[48];
memset(priv, 0, 48);
memset(public_key, 0, 40);
memset(output, 0, 20);
for (int i = 0; i < 32; i++) {
priv[i] = private_key[31 - i];
}
uECC_vli_mmod((uECC_word_t *)priv, (uECC_word_t *)priv, uECC_curve_n(curve),
uECC_curve_num_n_words(curve));
// r should be aligned to the curves size: zeros should be added as most
// significant bits if its representation is shorter than 160/256 bits, or the
// most significant bits should be truncated if its representation is larger
// than 160/256 bits.
// make sure big endian format is passed as SHA256 input
for (int i = 0; i < 20; i++) {
output[i] = priv[19 - i];
}
if (nearby_fp_Sha256(public_key, output, 20) != kNearbyStatusOK) {
NEARBY_TRACE(ERROR, "failed to generate hashed field");
*hashed_flag_field = 0;
} else {
// the least significant byte of SHA256(r)
*hashed_flag_field = public_key[31];
}
memset(public_key, 0, 40);
int ret = uECC_compute_public_key(priv, public_key, curve);
memset(output, 0, 20);
if (ret == 1) {
NEARBY_TRACE(INFO, "public key generated");
for (int i = 0; i < 20; i++) {
output[i] = public_key[19 - i];
}
return kNearbyStatusOK;
}
NEARBY_TRACE(ERROR, "failed to generate public key: %d", ret);
return kNearbyStatusError;
}