-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nicolas Burtey
committed
Sep 17, 2023
1 parent
1eb6936
commit 76e37a8
Showing
9 changed files
with
195 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
- rate limits | ||
- passing IP to backend | ||
- proper link of .gql (instead of copy) | ||
- Tilt setup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const aesjs = require("aes-js") | ||
|
||
// used for encryption | ||
export const decodePToUidCtr = ( | ||
decryptedP: Uint8Array, | ||
): { uid: string; uidRaw: Uint8Array; ctr: number; ctrRawInverseBytes: Uint8Array } => { | ||
if (decryptedP[0] !== 0xc7) { | ||
throw new Error("data not starting with 0xC7") | ||
} | ||
|
||
const uidRaw = decryptedP.slice(1, 8) | ||
const uid = aesjs.utils.hex.fromBytes(uidRaw) | ||
|
||
const ctrRawInverseBytes = decryptedP.slice(8, 11) | ||
const ctr = | ||
(ctrRawInverseBytes[2] << 16) | (ctrRawInverseBytes[1] << 8) | ctrRawInverseBytes[0] | ||
|
||
return { | ||
uid, | ||
uidRaw, | ||
ctr, | ||
ctrRawInverseBytes, | ||
} | ||
} | ||
|
||
// only used to simulate the cold card | ||
export const encodeUidCtrToP = (uid: Buffer, ctr: Buffer): Uint8Array => { | ||
return new Uint8Array([ | ||
0xc7, | ||
uid[0], | ||
uid[1], | ||
uid[2], | ||
uid[3], | ||
uid[4], | ||
uid[5], | ||
uid[6], | ||
ctr[0], | ||
ctr[1], | ||
ctr[2], | ||
|
||
// those value can be random, but they are set as is so that | ||
// tests pass for the simulation of the encryption fo the coldcard | ||
2, | ||
63, | ||
181, | ||
243, | ||
74, | ||
]) | ||
} | ||
|
||
// used for signature | ||
export const createSV2 = (uid: Uint8Array, ctr: Uint8Array) => { | ||
const sv2 = Buffer.from([ | ||
0x3c, | ||
0xc3, | ||
0x00, | ||
0x01, | ||
0x00, | ||
0x80, | ||
uid[0], | ||
uid[1], | ||
uid[2], | ||
uid[3], | ||
uid[4], | ||
uid[5], | ||
uid[6], | ||
ctr[0], | ||
ctr[1], | ||
ctr[2], | ||
]) | ||
|
||
return sv2 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { aesEncrypt, getSunMAC } from "@/app/crypto/aes" | ||
import { createSV2, encodeUidCtrToP } from "@/app/crypto/decoder" | ||
|
||
const aesjs = require("aes-js") | ||
|
||
const uidInit = "04996c6a926980" | ||
const ctrInit = "030000" | ||
|
||
export const main = (k1: string, k2: string) => { | ||
// "0c3b25d92b38ae443229dd59ad34b85d" | ||
// k2: b45775776cb224c75bcde7ca3704e933 | ||
|
||
const uid = Buffer.from(uidInit, "hex") | ||
const ctr = Buffer.from(ctrInit, "hex") | ||
|
||
const p1 = encodeUidCtrToP(uid, ctr) | ||
const p2 = Buffer.from(p1) | ||
|
||
// encrypt P | ||
const encryptP = aesEncrypt(Buffer.from(k1, "hex"), p2) | ||
if (encryptP instanceof Error) { | ||
throw encryptP | ||
} | ||
|
||
const cv2 = createSV2(uid, ctr) | ||
const cmac = getSunMAC(Buffer.from(k2, "hex"), cv2) | ||
|
||
return JSON.stringify({ | ||
c: cmac.toString("hex"), | ||
p: aesjs.utils.hex.fromBytes(encryptP), | ||
}) | ||
} | ||
|
||
console.log(main(process.argv[2], process.argv[3])) |
This file was deleted.
Oops, something went wrong.