|
| 1 | +#include "shared_context.h" |
| 2 | +#include "apdu_constants.h" |
| 3 | + |
| 4 | +#include "ui_flow.h" |
| 5 | +#include "feature_performPrivacyOperation.h" |
| 6 | + |
| 7 | +#define P2_PUBLIC_ENCRYPTION_KEY 0x00 |
| 8 | +#define P2_SHARED_SECRET 0x01 |
| 9 | + |
| 10 | +void decodeScalar(const uint8_t *scalarIn, uint8_t *scalarOut) { |
| 11 | + for (uint8_t i = 0; i < 32; i++) { |
| 12 | + switch (i) { |
| 13 | + case 0: |
| 14 | + scalarOut[0] = (scalarIn[31] & 0x7f) | 0x40; |
| 15 | + break; |
| 16 | + case 31: |
| 17 | + scalarOut[31] = scalarIn[0] & 0xf8; |
| 18 | + break; |
| 19 | + default: |
| 20 | + scalarOut[i] = scalarIn[31 - i]; |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +void handlePerformPrivacyOperation(uint8_t p1, |
| 26 | + uint8_t p2, |
| 27 | + uint8_t *dataBuffer, |
| 28 | + uint16_t dataLength, |
| 29 | + unsigned int *flags, |
| 30 | + unsigned int *tx) { |
| 31 | + UNUSED(dataLength); |
| 32 | + uint8_t privateKeyData[INT256_LENGTH]; |
| 33 | + uint8_t privateKeyDataSwapped[INT256_LENGTH]; |
| 34 | + uint32_t bip32Path[MAX_BIP32_PATH]; |
| 35 | + uint8_t bip32PathLength = *(dataBuffer++); |
| 36 | + cx_err_t status = CX_OK; |
| 37 | + if (p2 == P2_PUBLIC_ENCRYPTION_KEY) { |
| 38 | + if (dataLength < 1 + 4 * bip32PathLength) { |
| 39 | + THROW(0x6700); |
| 40 | + } |
| 41 | + } else if (p2 == P2_SHARED_SECRET) { |
| 42 | + if (dataLength < 1 + 4 * bip32PathLength + 32) { |
| 43 | + THROW(0x6700); |
| 44 | + } |
| 45 | + } else { |
| 46 | + THROW(0x6B00); |
| 47 | + } |
| 48 | + cx_ecfp_private_key_t privateKey; |
| 49 | + if ((bip32PathLength < 0x01) || (bip32PathLength > MAX_BIP32_PATH)) { |
| 50 | + PRINTF("Invalid path\n"); |
| 51 | + THROW(0x6a80); |
| 52 | + } |
| 53 | + if ((p1 != P1_CONFIRM) && (p1 != P1_NON_CONFIRM)) { |
| 54 | + THROW(0x6B00); |
| 55 | + } |
| 56 | + for (uint8_t i = 0; i < bip32PathLength; i++) { |
| 57 | + bip32Path[i] = U4BE(dataBuffer, 0); |
| 58 | + dataBuffer += 4; |
| 59 | + } |
| 60 | + os_perso_derive_node_bip32( |
| 61 | + CX_CURVE_256K1, |
| 62 | + bip32Path, |
| 63 | + bip32PathLength, |
| 64 | + privateKeyData, |
| 65 | + (tmpCtx.publicKeyContext.getChaincode ? tmpCtx.publicKeyContext.chainCode : NULL)); |
| 66 | + cx_ecfp_init_private_key(CX_CURVE_256K1, privateKeyData, 32, &privateKey); |
| 67 | + cx_ecfp_generate_pair(CX_CURVE_256K1, &tmpCtx.publicKeyContext.publicKey, &privateKey, 1); |
| 68 | + getEthAddressStringFromKey(&tmpCtx.publicKeyContext.publicKey, |
| 69 | + tmpCtx.publicKeyContext.address, |
| 70 | + &global_sha3, |
| 71 | + chainConfig->chainId); |
| 72 | + if (p2 == P2_PUBLIC_ENCRYPTION_KEY) { |
| 73 | + decodeScalar(privateKeyData, privateKeyDataSwapped); |
| 74 | + cx_ecfp_init_private_key(CX_CURVE_Curve25519, privateKeyDataSwapped, 32, &privateKey); |
| 75 | + cx_ecfp_generate_pair(CX_CURVE_Curve25519, |
| 76 | + &tmpCtx.publicKeyContext.publicKey, |
| 77 | + &privateKey, |
| 78 | + 1); |
| 79 | + explicit_bzero(privateKeyDataSwapped, sizeof(privateKeyDataSwapped)); |
| 80 | + } else { |
| 81 | + memmove(tmpCtx.publicKeyContext.publicKey.W + 1, dataBuffer, 32); |
| 82 | + status = cx_x25519(tmpCtx.publicKeyContext.publicKey.W + 1, privateKeyData, 32); |
| 83 | + } |
| 84 | + explicit_bzero(&privateKey, sizeof(privateKey)); |
| 85 | + explicit_bzero(privateKeyData, sizeof(privateKeyData)); |
| 86 | + |
| 87 | + if (status != CX_OK) { |
| 88 | + THROW(0x6A80); |
| 89 | + } |
| 90 | + |
| 91 | +#ifndef NO_CONSENT |
| 92 | + if (p1 == P1_NON_CONFIRM) |
| 93 | +#endif // NO_CONSENT |
| 94 | + { |
| 95 | + *tx = set_result_perform_privacy_operation(); |
| 96 | + THROW(0x9000); |
| 97 | + } |
| 98 | +#ifndef NO_CONSENT |
| 99 | + else { |
| 100 | + snprintf(strings.common.fullAddress, |
| 101 | + sizeof(strings.common.fullAddress), |
| 102 | + "0x%.*s", |
| 103 | + 40, |
| 104 | + tmpCtx.publicKeyContext.address); |
| 105 | + for (uint8_t i = 0; i < 32; i++) { |
| 106 | + privateKeyData[i] = tmpCtx.publicKeyContext.publicKey.W[32 - i]; |
| 107 | + } |
| 108 | + snprintf(strings.common.fullAmount, |
| 109 | + sizeof(strings.common.fullAmount) - 1, |
| 110 | + "%.*H", |
| 111 | + 32, |
| 112 | + privateKeyData); |
| 113 | + if (p2 == P2_PUBLIC_ENCRYPTION_KEY) { |
| 114 | + ux_flow_init(0, ux_display_privacy_public_key_flow, NULL); |
| 115 | + } else { |
| 116 | + ux_flow_init(0, ux_display_privacy_shared_secret_flow, NULL); |
| 117 | + } |
| 118 | + |
| 119 | + *flags |= IO_ASYNCH_REPLY; |
| 120 | + } |
| 121 | +#endif // NO_CONSENT |
| 122 | +} |
0 commit comments