Skip to content

Commit 1387048

Browse files
init project
0 parents  commit 1387048

23 files changed

+2443
-0
lines changed

Client/client.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const getMsg2 = require('./msg2').getMsg2
2+
const request = require('request');
3+
const bigInt = require("big-integer");
4+
//var url="http://127.0.0.0:12345";
5+
const url="http://localhost:12345";
6+
const sigurl = "https://api.trustedservices.intel.com/sgx/dev/attestation/v3/sigrl/000013d6"
7+
var msg0 = {
8+
"type": "msg0",
9+
"name" : "yaoz",
10+
"age" : 44,
11+
};
12+
13+
const send = function(data) {
14+
return new Promise(function(resolve, reject) {
15+
request({
16+
url: url,
17+
method: "POST",
18+
json: true,
19+
headers: {
20+
"content-type": "application/json",
21+
//"Ocp-Apim-Subscription-Key": "e2e08166ca0f41ef88af2797f007c7cd",
22+
},
23+
body: data
24+
}, function(error, response, body) {
25+
if (!error && response.statusCode == 200) {
26+
console.log("successful:",body) // 请求成功的处理逻辑
27+
resolve(body)
28+
} else {
29+
console.log("failed:",error) // 请求成功的处理逻辑
30+
reject(error)
31+
}
32+
});
33+
})
34+
};
35+
36+
main()
37+
38+
async function main() {
39+
const msg1 = await send(msg0)
40+
console.log("msg1",msg1)
41+
const msg2 = getMsg2({
42+
X : msg1.gax,
43+
Y : msg1.gay,
44+
})
45+
console.log("msg2",msg2)
46+
const msg3 = send(msg2)
47+
//assemble(msg3)
48+
}

Client/ecdsa.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const asn1 = require('asn1');
2+
const BN = require('bn');
3+
const crypto = require('crypto');
4+
const EcdsaDerSig = asn1.define('ECPrivateKey', function() { return this.seq().obj( this.key('r').int(), this.key('s').int() ); });
5+
function asn1SigSigToConcatSig(asn1SigBuffer) { const rsSig = EcdsaDerSig.decode(asn1SigBuffer, 'der'); return Buffer.concat([ rsSig.r.toArrayLike(Buffer, 'be', 32), rsSig.s.toArrayLike(Buffer, 'be', 32) ]); }
6+
7+
function concatSigToAsn1SigSig(concatSigBuffer) { const r = new BN(concatSigBuffer.slice(0, 32).toString('hex'), 16, 'be'); const s = new BN(concatSigBuffer.slice(32).toString('hex'), 16, 'be'); return EcdsaDerSig.encode({r, s}, 'der'); }
8+
9+
function ecdsaSign(hashBuffer, key) { const sign = crypto.createSign('sha256'); sign.update(asBuffer(hashBuffer)); const asn1SigBuffer = sign.sign(key, 'buffer'); return asn1SigSigToConcatSig(asn1SigBuffer); }
10+
11+
function ecdsaVerify(data, signature, key) { const verify = crypto.createVerify('SHA256'); verify.update(data); const asn1sig = concatSigToAsn1Sig(signature); return verify.verify(key, new Buffer(asn1sig, 'hex')); }
12+
13+
module.exports = {
14+
ecdsaSign: ecdsaSign,
15+
}

Client/msg2.js

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
//const {
2+
// SPID,
3+
// SIGRL,
4+
// SIZE_SIGRL,
5+
// AES_CMAC_KDF_ID,
6+
// SAMPLE_QUOTE_LINKABLE_SIGNATURE
7+
//} = require("./ecConstants");
8+
const SPID = "FEF23C7E73A379823CE71FF289CFBC07";
9+
const SIGRL = 0;
10+
const SIZE_SIGRL = 0;
11+
const AES_CMAC_KDF_ID = 0x0001;
12+
const SAMPLE_QUOTE_LINKABLE_SIGNATURE = 1;
13+
const {
14+
switchEndian,
15+
toHex,
16+
hexStringToArray,
17+
buf2hexString,
18+
hexString2Buffer,
19+
} = require("./utils");
20+
21+
const crypto = require("crypto")
22+
//const ec = crypto.createECDH('secp256k1')
23+
const aesCmac = require("node-aes-cmac").aesCmac;
24+
const EC = require('elliptic').ec
25+
const ec = new EC('p256');
26+
//const ec = new EC('secp256k1');
27+
const ecUtils = require('eckey-utils')
28+
const eccrypto = require("eccrypto")
29+
const bigInt = require("big-integer");
30+
31+
32+
function handleEcdhParam(decArray) {
33+
const hexStrArray = decArray.map(num => {
34+
const hex = num.toString(16);
35+
return (hex.length < 2) ? '0' + hex : hex;
36+
});
37+
const hexString = hexStrArray.join("");
38+
const switchedHexString = switchEndian(hexString);
39+
const decimalString = bigInt(switchedHexString, 16).toString();
40+
return decimalString;
41+
}
42+
43+
44+
function getMsg2(ecPublicKey) {
45+
//const GAX = hexStringToArray(ecPublicKey.X,2)
46+
//const GAY = hexStringToArray(ecPublicKey.Y,2)
47+
const gax_o = "8f6405f2bc7b7d3c66eb9dfcdaeb1ab19867d528b85426fab9f4459d3f6a715e"
48+
const gay_o = "60bb87cc172220380294e0ddbb8e92ea2ba63a3eb99ebd6c659a07c8d39977ed"
49+
const gax = switchEndian(gax_o)
50+
const gay = switchEndian(gay_o)
51+
const GAX = hexStringToArray(gax,2)
52+
const GAY = hexStringToArray(gay,2)
53+
54+
/* Generate msg2 */
55+
const gbx = "68d4d7c82d4dd8a72de568da4989fc95076745a13a612164c30a208958ef0485"
56+
const gby = "55edd100abc57249157f174c78cd4863a5daa161b812a0f3e19eb0a36fce06e7"
57+
//const gbx = switchEndian("38b9fc97433e342ada6d1aacf8b5eec04515ca4ef4ef4602fdb436facf0a4b7d")
58+
//const gby = switchEndian("a1bb2339bd6b39965d08d4b31bd9d2a875cf165e6f00895779691cfcc9208a1a")
59+
const GBX = hexStringToArray(gbx,2)
60+
const GBY = hexStringToArray(gby,2)
61+
const pubKey = {
62+
x: gbx,
63+
y: gby,
64+
}
65+
//const signPriKey = "90e76cbb2d52a1ce3b66de11439c87ec1f866a3b65b6aeeaad573453d1038c01"
66+
const signPriKey = "18C03D1533457ADEAAEB6653B6A861FEC879C4311DE663BCEA1522DBB6CE790"
67+
//const signPriKey = switchEndian("18C03D1533457ADEAAEB6653B6A861FEC879C4311DE663BCEA1522DBB6CE790")
68+
const priKey = "85DDC3B7C45F40F7DD97C543A61524B6C6E34975C68C0AA981F7363447BB0DD4"
69+
const signKey = ec.keyFromPrivate(signPriKey)
70+
const key = ec.keyFromPrivate(priKey)
71+
//const key_pub = ec.keyFromPublic(pubKey)
72+
console.log("=====signpubkey",signKey.getPublic().getX().toString(16))
73+
console.log("=====signpubkey",signKey.getPublic().getY().toString(16))
74+
//console.log("=====pubkey",key.getPublic())
75+
//const key = ec.genKeyPair()
76+
const MY_PRIVATE_KEY = key.getPrivate()
77+
const MY_PUBLIC_KEY = key.getPublic()
78+
//const GBX = hexStringToArray(MY_PUBLIC_KEY.getX().toString(16),2)
79+
//const GBY = hexStringToArray(MY_PUBLIC_KEY.getY().toString(16),2)
80+
//console.log("pubkey:",MY_PUBLIC_KEY.getX())
81+
82+
// Get server public key
83+
const serverPubKey = {
84+
//x: ecPublicKey.X,
85+
//y: ecPublicKey.Y
86+
x: gax,
87+
y: gay,
88+
}
89+
const serverKey = ec.keyFromPublic(serverPubKey,'hex')
90+
//console.log("server key",serverKey)
91+
92+
// derive kdk
93+
const sharedKey = switchEndian(toHex(key.derive(serverKey.getPublic())))
94+
//console.log("sharedKey",key.derive(serverKey.getPublic()))
95+
console.log("sharedKey",sharedKey)
96+
const iv = Buffer.alloc(16, 0)
97+
//const cipher = crypto.createCipheriv('aes-128-cbc', iv, iv)
98+
//cipher.update(Buffer.from(hexStringToArray(sharedKey,2)), 'hex', 'hex')
99+
//cipher.update(sharedKey, 'hex', 'hex')
100+
const kdk = aesCmac(iv, hexString2Buffer(sharedKey))
101+
//const kdk_hex = cipher.final('hex')
102+
//const kdk = Buffer.from(hexStringToArray(kdk_hex, 2))
103+
//const kdk = Buffer.from(hexStringToArray(cipher.final('hex'), 2))
104+
console.log("======kdk",kdk)
105+
// derive smk
106+
//const message = 0x01+'S'+'M'+'K'+0x00+0x80+0x00
107+
const message = [0x01,'S'.charCodeAt(0),'M'.charCodeAt(0),'K'.charCodeAt(0),0x00,0x80,0x00]
108+
const smk = aesCmac(hexString2Buffer(kdk), Buffer.from(message))
109+
console.log("======smk",smk)
110+
//console.log("message",message)
111+
//const cipher2 = crypto.createCipheriv('aes-128-cbc', kdk, iv)
112+
//cipher2.update(Buffer.from(message), 'utf8', 'hex')
113+
//const smk = hexStringToArray(cipher2.final('hex'), 2)
114+
115+
/**
116+
* @desc get signature: sign publck keys with my private key
117+
*/
118+
const GBA = gbx+gby+gax_o+gay_o
119+
console.log("=====GBA",GBA)
120+
const digest = crypto.createHash('sha256')
121+
.update(hexString2Buffer(GBA))
122+
.digest()
123+
console.log("===== digest",digest)
124+
console.log("===== digest",buf2hexString(digest))
125+
126+
const rs = require('jsrsasign')
127+
const KJUREC = new rs.KJUR.crypto.ECDSA({'curve': 'prime256v1'})
128+
const KJURSIG = KJUREC.signHex(buf2hexString(digest), signPriKey)
129+
console.log("===== KJURSIG:", KJURSIG)
130+
131+
const sign_t = crypto.createSign('SHA256')
132+
sign_t.write(GBA)
133+
sign_t.end()
134+
//const ttt = '-----BEGIN EC PRIVATE KEY-----\n' +
135+
// 'MD4CAQEEIBjAPRUzRXrequtmU7aoYf7IecQxHeZjvOoVItu2znkAoAoGCCqGSM49\n' +
136+
// ' AwEHoQsDCQAAC+wAAAAMAA==\n' +
137+
// ' -----END EC PRIVATE KEY-----'
138+
//const tmp3 = sign_t.sign(ttt, 'hex')
139+
//const ttt = crypto.createPrivateKey(signPriKey)
140+
//const tmp3 = sign_t.sign(ttt, 'hex')
141+
const my_keypair = crypto.generateKeyPairSync('ec', {
142+
namedCurve:'P-256',
143+
//namedCurve:'prime256v1',
144+
privateKeyEncoding : {
145+
type: 'pkcs8',
146+
format: 'pem'
147+
}
148+
})
149+
const pems = ecUtils.generatePem({
150+
curveName: 'prime256v1',
151+
privateKey: hexString2Buffer(toHex(signKey.getPrivate())),
152+
publicKey: hexString2Buffer(toHex(signKey.getPublic()))
153+
})
154+
//console.log("===== tmp3:", my_keypair.privateKey)
155+
//console.log("===== tmp3:", pems.privateKey)
156+
//console.log("===== tmp3:", tmp3)
157+
158+
//const sig = signKey.sign(Array.from(digest))
159+
const sig = signKey.sign(Array.from(digest))
160+
const SigSPX = toHex(sig.r)
161+
const SigSPY = toHex(sig.s)
162+
//console.log("SigSPX", sig.r.toString(16))
163+
//console.log("SigSPX", buf2hexString(sig.toDER()))
164+
console.log("===== SigSPX", bigInt(sig.r).toString(16))
165+
console.log("===== SigSPY", sig.s.toString(16))
166+
console.log("===== SigSPY", bigInt('387a059f2330aff862bcf7cd572f67f596ad77ee6b4f0bd1e0e06de9d6f90d93', 16).toString(16))
167+
//console.log("===== elliptic",sig.r)
168+
//console.log("===== elliptic",sig.s)
169+
//const sig = signKey.sign(Buffer.from(hexStringToArray(digest,2)))
170+
//const sig = signKey.sign(Buffer.from(hexStringToArray(GBA,2)))
171+
//const sign_x = key.sign(GBAY);
172+
//const sign_y = key.sign(GBAY);
173+
//const SigSPX = sign_x.toDER();
174+
//const SigSPY = sign_y.toDER();
175+
176+
eccrypto.sign(hexString2Buffer(signPriKey), digest).then(function(sig){
177+
console.log("===== tmp:", buf2hexString(sig))
178+
})
179+
//const tmp2 = ec.sign(digest, hexString2Buffer(signPriKey), {canonical:true})
180+
const tmp2 = ec.sign(Array.from(digest), signPriKey)
181+
console.log("===== tmp2",toHex(tmp2.r))
182+
console.log("===== tmp2",toHex(tmp2.s))
183+
// derive CMACsmk
184+
const QUOTE_TYPE = [0x00,0x01]
185+
const KDF_ID = [0x00,0x01]
186+
const SPID_ARRY = hexStringToArray(SPID, 2)
187+
const A = GBX.concat(GBY).concat(SPID_ARRY).concat(QUOTE_TYPE).concat(KDF_ID).concat(SigSPX).concat(SigSPY)
188+
189+
//const cipher3 = crypto.createCipheriv('aes-128-cbc', Buffer.from(smk), iv)
190+
//cipher3.update(toHex(A), 'utf8', 'hex')
191+
//const CMACsmk = cipher3.final('hex')
192+
const CMACsmk = aesCmac(Buffer.from(hexStringToArray(smk,2)), Buffer.from(A))
193+
console.log("=====CMACsmk", CMACsmk)
194+
console.log("SigSPX", toHex(MY_PUBLIC_KEY.getX()))
195+
196+
/**
197+
* @desc get smac
198+
*/
199+
//const GBX = toHex(MY_PUBLIC_KEY.X);
200+
//const GBY = toHex(MY_PUBLIC_KEY.Y);
201+
//const sMyPublicKey = switchEndian(bigInt(MY_PUBLIC_KEY.X).toString(16), 2) + switchEndian(bigInt(MY_PUBLIC_KEY.Y).toString(16), 2);
202+
203+
//const smac = aesCmac(SHORT_KEY, sMyPublicKey);
204+
return {
205+
type: "msg2",
206+
gbx: switchEndian(MY_PUBLIC_KEY.getX().toString(16)),
207+
gby: switchEndian(MY_PUBLIC_KEY.getY().toString(16)),
208+
//gbx: MY_PUBLIC_KEY.getX().toString(16),
209+
//gby: MY_PUBLIC_KEY.getY().toString(16),
210+
quoteType: buf2hexString(switchEndian(QUOTE_TYPE)),
211+
spid: SPID,
212+
kdfId: buf2hexString(switchEndian(KDF_ID)),
213+
SigSPX: buf2hexString(switchEndian(SigSPX)),
214+
SigSPY: buf2hexString(switchEndian(SigSPY)),
215+
CMACsmk: CMACsmk,
216+
sizeSigrl: SIZE_SIGRL,
217+
sigrl: SIGRL
218+
}
219+
}
220+
221+
module.exports = {
222+
getMsg2: getMsg2,
223+
}

0 commit comments

Comments
 (0)