2014-11-15 01:01:21 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +00:00
|
|
|
* This file is part of the Trezor project, https://trezor.io/
|
2014-11-15 01:01:21 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Pavol Rusnak <stick@satoshilabs.com>
|
|
|
|
*
|
|
|
|
* This library is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __CRYPTO_H__
|
|
|
|
#define __CRYPTO_H__
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
#include <bip32.h>
|
|
|
|
#include <ecdsa.h>
|
|
|
|
#include <pb.h>
|
|
|
|
#include <sha2.h>
|
2014-11-15 01:01:21 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
2017-10-30 21:13:09 +00:00
|
|
|
#include "coins.h"
|
2017-12-09 14:38:37 +00:00
|
|
|
#include "hasher.h"
|
2018-07-10 14:58:00 +00:00
|
|
|
#include "messages-bitcoin.pb.h"
|
|
|
|
#include "messages-crypto.pb.h"
|
2014-11-15 01:01:21 +00:00
|
|
|
|
2022-01-25 21:50:43 +00:00
|
|
|
#define PATH_HARDENED 0x80000000
|
|
|
|
#define PATH_UNHARDEN_MASK 0x7fffffff
|
|
|
|
#define PATH_MAX_ACCOUNT 100
|
|
|
|
#define PATH_MAX_CHANGE 1
|
|
|
|
#define PATH_MAX_ADDRESS_INDEX 1000000
|
2022-06-03 12:50:11 +00:00
|
|
|
#define PATH_SLIP25_PURPOSE (PATH_HARDENED | 10025)
|
2022-01-25 21:50:43 +00:00
|
|
|
|
2022-12-23 16:07:26 +00:00
|
|
|
// The number of bip32 levels used in a wallet (chain and address)
|
|
|
|
#define BIP32_WALLET_DEPTH 2
|
|
|
|
|
2017-11-08 00:13:21 +00:00
|
|
|
#define ser_length_size(len) ((len) < 253 ? 1 : (len) < 0x10000 ? 3 : 5)
|
|
|
|
|
2022-12-30 22:20:33 +00:00
|
|
|
typedef enum {
|
|
|
|
SCHEMA_NONE,
|
|
|
|
SCHEMA_SLIP25_TAPROOT,
|
|
|
|
SCHEMA_SLIP25_TAPROOT_EXTERNAL
|
|
|
|
} PathSchema;
|
|
|
|
|
2022-12-15 11:42:36 +00:00
|
|
|
typedef struct {
|
|
|
|
uint8_t data[64];
|
|
|
|
} Slip21Node;
|
|
|
|
|
2014-11-15 01:01:21 +00:00
|
|
|
uint32_t ser_length(uint32_t len, uint8_t *out);
|
|
|
|
|
2017-12-09 14:38:37 +00:00
|
|
|
uint32_t ser_length_hash(Hasher *hasher, uint32_t len);
|
2014-12-21 18:39:20 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
int sshMessageSign(HDNode *node, const uint8_t *message, size_t message_len,
|
|
|
|
uint8_t *signature);
|
2015-06-03 19:21:16 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
int gpgMessageSign(HDNode *node, const uint8_t *message, size_t message_len,
|
|
|
|
uint8_t *signature);
|
2016-04-15 19:04:45 +00:00
|
|
|
|
2019-08-19 16:11:08 +00:00
|
|
|
int signifyMessageSign(HDNode *node, const uint8_t *message, size_t message_len,
|
|
|
|
uint8_t *signature);
|
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
int cryptoMessageSign(const CoinInfo *coin, HDNode *node,
|
2021-11-05 17:01:46 +00:00
|
|
|
InputScriptType script_type, bool no_script_type,
|
|
|
|
const uint8_t *message, size_t message_len,
|
|
|
|
uint8_t *signature);
|
2014-11-15 01:01:21 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
int cryptoMessageVerify(const CoinInfo *coin, const uint8_t *message,
|
|
|
|
size_t message_len, const char *address,
|
|
|
|
const uint8_t *signature);
|
2014-11-15 01:01:21 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
const HDNode *cryptoMultisigPubkey(const CoinInfo *coin,
|
|
|
|
const MultisigRedeemScriptType *multisig,
|
|
|
|
uint32_t index);
|
2019-02-04 00:26:26 +00:00
|
|
|
|
|
|
|
uint32_t cryptoMultisigPubkeyCount(const MultisigRedeemScriptType *multisig);
|
2014-12-13 19:33:49 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
int cryptoMultisigPubkeyIndex(const CoinInfo *coin,
|
|
|
|
const MultisigRedeemScriptType *multisig,
|
|
|
|
const uint8_t *pubkey);
|
2014-12-13 19:33:49 +00:00
|
|
|
|
2019-03-29 16:10:31 +00:00
|
|
|
int cryptoMultisigFingerprint(const MultisigRedeemScriptType *multisig,
|
|
|
|
uint8_t *hash);
|
2014-12-10 17:04:51 +00:00
|
|
|
|
2015-02-20 19:22:05 +00:00
|
|
|
int cryptoIdentityFingerprint(const IdentityType *identity, uint8_t *hash);
|
|
|
|
|
2021-06-29 11:38:59 +00:00
|
|
|
bool coin_path_check(const CoinInfo *coin, InputScriptType script_type,
|
|
|
|
uint32_t address_n_count, const uint32_t *address_n,
|
2022-12-30 22:20:33 +00:00
|
|
|
bool has_multisig, PathSchema unlock, bool full_check);
|
2020-08-04 16:51:19 +00:00
|
|
|
|
2022-12-15 09:58:25 +00:00
|
|
|
bool is_multisig_input_script_type(InputScriptType script_type);
|
|
|
|
bool is_multisig_output_script_type(OutputScriptType script_type);
|
|
|
|
bool is_internal_input_script_type(InputScriptType script_type);
|
|
|
|
bool is_change_output_script_type(OutputScriptType script_type);
|
|
|
|
bool is_segwit_input_script_type(InputScriptType script_type);
|
|
|
|
bool is_segwit_output_script_type(OutputScriptType script_type);
|
|
|
|
bool change_output_to_input_script_type(OutputScriptType output_script_type,
|
|
|
|
InputScriptType *input_script_type);
|
|
|
|
|
2022-12-15 11:42:36 +00:00
|
|
|
void slip21_from_seed(const uint8_t *seed, int seed_len, Slip21Node *out);
|
|
|
|
void slip21_derive_path(Slip21Node *inout, const uint8_t *label,
|
|
|
|
size_t label_len);
|
|
|
|
const uint8_t *slip21_key(const Slip21Node *node);
|
|
|
|
|
2014-11-15 01:01:21 +00:00
|
|
|
#endif
|