Skip to content

Commit

Permalink
keys generation cpp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ftheirs committed Mar 12, 2024
1 parent 61a8aac commit 699b748
Show file tree
Hide file tree
Showing 5 changed files with 481 additions and 0 deletions.
96 changes: 96 additions & 0 deletions app/src/crypto_helper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*******************************************************************************
* (c) 2018 - 2024 Zondax AG
*
* 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
*
* http://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 "crypto_helper.h"
#include "keys_personalizations.h"
#include <string.h>
#include "zxformat.h"

#include "rslib.h"

#if defined (LEDGER_SPECIFIC)
#include "cx.h"
#else
#include "blake2.h"
#endif

static void swap_endian(uint8_t *data, int8_t len) {
for (int8_t i = 0; i < len / 2; i++) {
uint8_t t = data[len - i - 1];
data[len - i - 1] = data[i];
data[i] = t;
}
}

parser_error_t convertKey(const uint8_t spendingKey[KEY_LENGTH], const uint8_t modifier, uint8_t outputKey[KEY_LENGTH], bool reduceWideByte) {
#if 0
cx_blake2b_t ctx = {0};

uint8_t output[64] = {0};
cx_blake2b_init2_no_throw(&ctx, 512, NULL, 0,(uint8_t*) "Iron Fish Money ", 16);
cx_blake2b_update(&ctx, spendingKey, 32);
cx_blake2b_update(&ctx, &modifier, 1);
cx_blake2b_final(&ctx, output);

from_bytes_wide(output, ask);
swap_endian(ask, 32);

uint8_t outputStr[130] = {0};
array_to_hexstr(outputStr, sizeof(outputStr), ask, 32);
ZEMU_LOGF(200, "----- ASK: %s\n", outputStr)
array_to_hexstr(outputStr, sizeof(outputStr), output, 64);
ZEMU_LOGF(200, "----- HASH: %s\n", outputStr)
#endif

#if 1
blake2b_state state = {0};
uint8_t output[64] = {0};
blake2b_init_with_personalization(&state, BLAKE2B_OUTPUT_LEN, (const uint8_t*)EXPANDED_SPEND_BLAKE2_KEY, sizeof(EXPANDED_SPEND_BLAKE2_KEY));
blake2b_update(&state, spendingKey, KEY_LENGTH);
blake2b_update(&state, &modifier, 1);
blake2b_final(&state, output, sizeof(output));

if (reduceWideByte) {
from_bytes_wide(output, outputKey);
swap_endian(outputKey, KEY_LENGTH);
} else {
memcpy(outputKey, output, KEY_LENGTH);
}

#endif
return parser_ok;
}

parser_error_t generate_key(const uint8_t expandedKey[KEY_LENGTH], constant_key_t keyType, uint8_t output[KEY_LENGTH]) {
uint8_t tmpExpandedKey[KEY_LENGTH] = {0};
memcpy(tmpExpandedKey, expandedKey, KEY_LENGTH);
swap_endian(tmpExpandedKey, KEY_LENGTH);
scalar_multiplication(tmpExpandedKey, keyType, output);
return parser_ok;
}

parser_error_t computeIVK(const ak_t ak, const nk_t nk, ivk_t ivk) {
blake2s_state state;
blake2s_init_with_personalization(&state, 32, (const uint8_t*)CRH_IVK_PERSONALIZATION, sizeof(CRH_IVK_PERSONALIZATION));
blake2s_update(&state, ak, KEY_LENGTH);
blake2s_update(&state, nk, KEY_LENGTH);
blake2s_final(&state, ivk, KEY_LENGTH);
ivk[31] &= 0x07;
swap_endian(ivk, KEY_LENGTH);
// if ivk == [0; 32] {
// return Err(IronfishError::new(IronfishErrorKind::InvalidViewingKey));
// }
return parser_ok;
}
35 changes: 35 additions & 0 deletions app/src/crypto_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*******************************************************************************
* (c) 2018 - 2024 Zondax AG
*
* 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
*
* http://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.
********************************************************************************/
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include "parser_common.h"
#include "keys_def.h"


parser_error_t convertKey(const uint8_t spendingKey[32], const uint8_t modifier, uint8_t outputKey[32], bool reduceWideByte);
parser_error_t generate_key(const uint8_t expandedKey[32], constant_key_t keyType, uint8_t output[32]);
parser_error_t computeIVK(const ak_t ak, const nk_t nk, ivk_t ivk);

#ifdef __cplusplus
}
#endif
55 changes: 55 additions & 0 deletions app/src/keys_def.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*******************************************************************************
* (c) 2018 - 2024 Zondax AG
*
* 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
*
* http://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.
********************************************************************************/
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>
#include <stdint.h>
typedef struct {
uint8_t *ptr;
uint16_t len;
} bytes_t;


typedef enum {
SpendingKeyGenerator,
ProofGenerationKeyGenerator,
} constant_key_t;

#define KEY_LENGTH 32

typedef uint8_t spending_key_t[KEY_LENGTH];
typedef uint8_t ask_t[KEY_LENGTH];
typedef uint8_t nsk_t[KEY_LENGTH];

typedef uint8_t ak_t[KEY_LENGTH];
typedef uint8_t nk_t[KEY_LENGTH];

typedef uint8_t ivk_t[KEY_LENGTH];
typedef uint8_t ovk_t[KEY_LENGTH];

typedef struct {
bytes_t spendingKey;
bytes_t ask;
bytes_t nsk;
} keys_t;

#ifdef __cplusplus
}
#endif
38 changes: 38 additions & 0 deletions app/src/keys_personalizations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*******************************************************************************
* (c) 2018 - 2024 Zondax AG
*
* 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
*
* http://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.
********************************************************************************/
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>
#include <stdint.h>

const uint8_t MODIFIER_ASK = 0x00;
const uint8_t MODIFIER_NSK = 0x01;

const uint8_t BLAKE2B_OUTPUT_LEN = 64;

const char EXPANDED_SPEND_BLAKE2_KEY[16] = "Iron Fish Money ";
const char CRH_IVK_PERSONALIZATION[8] = "Zcashivk";


#ifdef __cplusplus
}
#endif


Loading

0 comments on commit 699b748

Please sign in to comment.