|
| 1 | +const fs = require('fs'); |
| 2 | +const crypto = require('crypto'); |
| 3 | + |
| 4 | +const filePath = './nrf52810_xxaa.bin'; |
| 5 | +const privateKeyFile = '../../private.key'; |
| 6 | +const algo = 'sha256'; |
| 7 | +const curve = 'p256'; |
| 8 | + |
| 9 | + |
| 10 | +const initPacket_header = new Buffer.from([0x12 ,0x8a ,0x01 ,0x0a ,0x44 ,0x08 ,0x01 ,0x12 ,0x40]); |
| 11 | +let initPacket = new Buffer.from([0x08 ,0x01 ,0x10 ,0x34 ,0x1a ,0x02 ,0x83 ,0x02 ,0x20 ,0x00 ,0x28 ,0x00 ,0x30 ,0x00 ,0x38 ,0x84 ,0x8c ,0x03 ,0x42 ,0x24 ,0x08 ,0x03 ,0x12 ,0x20]); |
| 12 | + |
| 13 | +if (process.argv.length < 2) { |
| 14 | + console.log("base64 adv key expected"); |
| 15 | + exit(); |
| 16 | +} |
| 17 | + |
| 18 | +const file = fs.readFileSync(filePath); |
| 19 | +const str = file.toString("hex"); |
| 20 | +let keyBuffer = new Buffer.from(process.argv[2], 'base64'); |
| 21 | +let offlineBuffer = new Buffer.from("OFFLINEFINDINGPUBLICKEYHERE!", 'ascii'); |
| 22 | +let newStr = str.replace(offlineBuffer.toString("hex"), keyBuffer.toString("hex")); |
| 23 | +let buffer = Buffer.from( newStr, "hex" ); |
| 24 | +fs.writeFileSync(filePath + ".patched", buffer); |
| 25 | + |
| 26 | +fs.readFile(filePath +".patched", (err, data) => { |
| 27 | + if (err) { |
| 28 | + console.error(err); |
| 29 | + return; |
| 30 | + } |
| 31 | + const hash = crypto.createHash('sha256'); |
| 32 | + hash.update(data); |
| 33 | + const hashBuffer = hash.digest(); |
| 34 | + |
| 35 | + // Añadimos SHA256 en little endian |
| 36 | + initPacket = Buffer.concat([initPacket, hashBuffer.reverse()]); |
| 37 | + |
| 38 | + // Añadimos data fijo |
| 39 | + initPacket = Buffer.concat([initPacket, new Buffer.from([0x48, 0x00, 0x52, 0x04, 0x08, 0x01, 0x12, 0x00])]); |
| 40 | + |
| 41 | + // Firmamos lo que tenemos con ecdsa |
| 42 | + fs.readFile('../../private.key', 'utf8', (err, sk_pem) => { |
| 43 | + /* |
| 44 | + if (err) throw err; |
| 45 | + const EC = require('elliptic').ec; |
| 46 | + const ec = new EC('p256'); |
| 47 | +
|
| 48 | + console.log("initPacket to be signed: ", initPacket); |
| 49 | +
|
| 50 | + const sk = ec.keyFromPrivate(sk_pem, 'hex'); |
| 51 | + const hash = crypto.createHash('sha256'); |
| 52 | + hash.update(initPacket); |
| 53 | + const sha256packet = hash.digest(); |
| 54 | + console.log("initPacket", initPacket.length, initPacket); |
| 55 | + console.log("sha256packet", sha256packet); |
| 56 | + const sig = sk.sign(sha256packet); |
| 57 | + const sigBuffer1 = Buffer.from(sig.toDER().slice(0,32)).reverse(); |
| 58 | + const sigBuffer2 = Buffer.from(sig.toDER().slice(32,64)).reverse(); |
| 59 | + console.log("sig", sig.toDER()); |
| 60 | + console.log("sigBuffer1", sigBuffer1); |
| 61 | + console.log("sigBuffer2", sigBuffer2); |
| 62 | + console.log("sigBufferR", Buffer.from(sig.r.toArray())); |
| 63 | + console.log("sigBufferS", Buffer.from(sig.s.toArray())); |
| 64 | + initPacket = Buffer.concat([initPacket, Buffer.from([0x10, 0x00, 0x1a, 0x40]), Buffer.from(sig.r.toArray()).reverse(), Buffer.from(sig.s.toArray()).reverse()]); |
| 65 | + //initPacket = Buffer.concat([initPacket, sigBuffer1, sigBuffer2]); |
| 66 | + */ |
| 67 | + |
| 68 | + //fs.writeFileSync("initPacket.bin", initPacket); |
| 69 | + const sign = crypto.createSign('SHA256'); |
| 70 | + sign.update(initPacket); |
| 71 | + sign.end(); |
| 72 | + |
| 73 | + |
| 74 | + const signature = sign.sign({ key: sk_pem, dsaEncoding: 'ieee-p1363', curve: 'p256' }); |
| 75 | + console.log(signature); |
| 76 | + //fs.writeFileSync("sign.bin", signature); |
| 77 | + let r = signature.subarray(0,32); |
| 78 | + let s = signature.subarray(32,64); |
| 79 | + console.log(r,s); |
| 80 | + initPacket = Buffer.concat([initPacket, Buffer.from([0x10, 0x00, 0x1a, 0x40]), Buffer.from(r).reverse(), Buffer.from(s).reverse()]); |
| 81 | + |
| 82 | + /* |
| 83 | + let r = signature.subarray(0, 32); |
| 84 | + let s = signature.subarray(32, 64); |
| 85 | + console.log("sigBufferR", r.reverse()); |
| 86 | + console.log("sigBufferR", s.reverse()); |
| 87 | + */ |
| 88 | + |
| 89 | + /* |
| 90 | + const elliptic = require("elliptic").ec; |
| 91 | + const ec = new elliptic("p256"); |
| 92 | + const key = ec.keyFromPrivate(sk_pem); |
| 93 | + const signature = key.sign(initPacket); |
| 94 | + let r = new Buffer.from(signature.r.toArray()); |
| 95 | + let s = new Buffer.from(signature.s.toArray()); |
| 96 | + console.log(r, s); |
| 97 | +
|
| 98 | + initPacket = Buffer.concat([initPacket, r.reverse(), s.reverse()]); |
| 99 | +
|
| 100 | + */ |
| 101 | + |
| 102 | + |
| 103 | + // Finalizamos packet |
| 104 | + initPacket = Buffer.concat([initPacket_header, initPacket]); |
| 105 | + |
| 106 | + fs.writeFile('my.dat', initPacket, (err) => { |
| 107 | + if (err) throw err; |
| 108 | + console.log('The buffer was written to the file!'); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + //const hashArray = Array.from(hashBuffer); |
| 113 | + //const littleEndian = hashArray.reverse().map((val) => ('0' + val.toString(16)).slice(-2)).join(''); |
| 114 | + //console.log(`The SHA-256 hash of ${filePath} in little-endian format is: ${littleEndian}`); |
| 115 | + |
| 116 | +}); |
0 commit comments