Skip to content

Commit ed4f01a

Browse files
committed
first poc tool to generate dfu package working
1 parent e3f1908 commit ed4f01a

4 files changed

Lines changed: 141 additions & 0 deletions

File tree

apps/firmware/dfu_images/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Formato de fichero DFU
2+
3+
Primeros 9 bytes => 12 8a 01 0a 44 08 01 12 40
4+
[
5+
siguiente 24 bytes => 08 01 10 34 1a 02 83 02 20 00 28 00 30 00 38 84 8c 03 42 24 08 03 12 20
6+
Byte 34 => 32 bytes sha256 bin file - little endian
7+
byte 65 => 48 00 52 04 08 01 12 00
8+
] <-- esto se firma con ecds y se pone a partir del byte 77
9+
byte 73 => 10 00 1a 40
10+
byte 77 =>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
cd ../build
3+
make
4+
cd ../dfu_images
5+
cp ../../firmware/build/_build/nrf52810_xxaa.hex .
6+
nrfutil settings generate --family NRF52810 --application nrf52810_xxaa.hex --application-version 1 --bootloader-version 1 --bl-settings-version 2 bl_settings.hex
7+
mergehex -m bl_settings.hex secure_bootloader_moko.hex nrf52810_xxaa.hex ../../../nRF5_SDK_17.0.2_d674dde/components/softdevice/s112/hex/s112_nrf52_7.2.0_softdevice.hex --output full_dfu.hex
8+
nrfjprog -f nrf52 --recover; while [ $? != 0 ]; do nrfjprog -f nrf52 --recover; done;
9+
nrfjprog -f nrf52 --program full_dfu.hex --reset
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
cd ../build
3+
make
4+
cd ../dfu_images
5+
cp ../../firmware/build/_build/nrf52810_xxaa.hex .
6+
nrfutil pkg generate --hw-version 52 --sd-req 0x0100 --application-version 1 --application nrf52810_xxaa.hex --sd-id 0x103 --key-file private.key app.zip
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

Comments
 (0)