Skip to content

Commit 1e650a2

Browse files
committed
blast in TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256; fix error/warning with ec.h and txBigInt
1 parent 6656376 commit 1e650a2

File tree

14 files changed

+262
-221
lines changed

14 files changed

+262
-221
lines changed

modules/crypt/arith/ecp.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016-2017 Moddable Tech, Inc.
2+
* Copyright (c) 2016-2021 Moddable Tech, Inc.
33
*
44
* This file is part of the Moddable SDK Runtime.
55
*
@@ -74,6 +74,14 @@ export default class ECPoint {
7474
return new ECPoint(a[0], a[1], a[2]);
7575
}
7676
};
77+
static fromOctetString(os) {
78+
if (os[0] != 0x04)
79+
throw new Error("unsupported format");
80+
let flen = (os.length - 1) / 2;
81+
let x = BigInt.fromArrayBuffer(os.slice(1, 1 + flen).buffer);
82+
let y = BigInt.fromArrayBuffer(os.slice(1 + flen, os.length).buffer);
83+
return new ECPoint(x, y);
84+
}
7785
};
7886

7987
Object.freeze(ECPoint.prototype);

modules/crypt/arith/xsBigIntEx.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016-2017 Moddable Tech, Inc.
2+
* Copyright (c) 2016-2021 Moddable Tech, Inc.
33
*
44
* This file is part of the Moddable SDK Runtime.
55
*
@@ -36,7 +36,9 @@
3636
*/
3737

3838
#ifndef __XSALL__
39-
typedef void txBigInt;
39+
typedef struct {
40+
txU4 opaque;
41+
} txBigInt;
4042
typedef xsBooleanValue txBoolean;
4143

4244
extern void fxBigInt_setBigInt(xsSlot *slot, txBigInt *a);

modules/crypt/etc/ber.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
/*
2-
* Copyright (c) 2016-2020 Moddable Tech, Inc.
2+
* Copyright (c) 2016-2021 Moddable Tech, Inc.
33
*
44
* This file is part of the Moddable SDK Runtime.
5-
*
5+
*
66
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as published by
88
* the Free Software Foundation, either version 3 of the License, or
99
* (at your option) any later version.
10-
*
10+
*
1111
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
1212
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1313
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414
* GNU Lesser General Public License for more details.
15-
*
15+
*
1616
* You should have received a copy of the GNU Lesser General Public License
1717
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
1818
*
19-
* This file incorporates work covered by the following copyright and
20-
* permission notice:
19+
* This file incorporates work covered by the following copyright and
20+
* permission notice:
2121
*
2222
* Copyright (C) 2010-2016 Marvell International Ltd.
2323
* Copyright (C) 2002-2010 Kinoma, Inc.
@@ -108,16 +108,13 @@ export default class BER {
108108
return this.getChunk(this.getLength());
109109
};
110110
getObjectIdentifier() {
111-
if (this.getTag() != 0x06)
111+
if (this.getTag() !== 0x06)
112112
throw new Error("BER: not an object identifier");
113113
return this._getObjectIdentifier(this.getLength())
114114
}
115115
_getObjectIdentifier(len) {
116-
let oid = [];
117116
let i = this.#a[this.#i++];
118-
let rem = i % 40;
119-
oid.push((i - rem) / 40);
120-
oid.push(rem);
117+
let oid = [Math.idiv(i, 40), Math.irem(i, 40)];
121118
--len;
122119
while (len > 0) {
123120
let v = 0;

modules/crypt/etc/dsa.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
/*
2-
* Copyright (c) 2016-2017 Moddable Tech, Inc.
2+
* Copyright (c) 2016-2021 Moddable Tech, Inc.
33
*
44
* This file is part of the Moddable SDK Runtime.
5-
*
5+
*
66
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as published by
88
* the Free Software Foundation, either version 3 of the License, or
99
* (at your option) any later version.
10-
*
10+
*
1111
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
1212
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1313
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414
* GNU Lesser General Public License for more details.
15-
*
15+
*
1616
* You should have received a copy of the GNU Lesser General Public License
1717
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
1818
*
19-
* This file incorporates work covered by the following copyright and
20-
* permission notice:
19+
* This file incorporates work covered by the following copyright and
20+
* permission notice:
2121
*
2222
* Copyright (C) 2010-2016 Marvell International Ltd.
2323
* Copyright (C) 2002-2010 Kinoma, Inc.
@@ -36,7 +36,8 @@
3636
*/
3737

3838
import Crypt from "crypt";
39-
import Mont from "mont"
39+
import Mont from "mont";
40+
import BER from "ber";
4041

4142
export default class DSA {
4243
constructor(key, priv) {
@@ -61,10 +62,15 @@ export default class DSA {
6162
sig.s = s;
6263
return sig;
6364
};
64-
sign(H) {
65-
var sig = this._sign(H);
66-
var os = new ArrayBuffer();
67-
return os.concat(Crypt.PKCS1.I2OSP(sig.r, 20), Crypt.PKCS1.I2OSP(sig.s, 20));
65+
sign(H, asn1) {
66+
if (asn1) {
67+
return BER.encode([0x30, [0x02, sig.r], [0x02, sig.s]]);
68+
}
69+
else {
70+
var sig = this._sign(H);
71+
var os = new ArrayBuffer();
72+
return os.concat(Crypt.PKCS1.I2OSP(sig.r, 20), Crypt.PKCS1.I2OSP(sig.s, 20));
73+
}
6874
};
6975
_verify(H, r, s) {
7076
// w = 1/s mod q
@@ -82,10 +88,19 @@ export default class DSA {
8288
var v = q.mod(p.exp2(g, u1, y, u2));
8389
return this.comp(v, r) == 0;
8490
};
85-
verify(H, sig) {
86-
// "20" is specified in the xmldsig-core spec.
87-
var r = Crypt.PKCS1.OS2IP(sig.slice(0, 20));
88-
var s = Crypt.PKCS1.OS2IP(sig.slice(20, 40));
91+
verify(H, sig, asn1) {
92+
var r, s;
93+
if (asn1) {
94+
let ber = new BER(sig);
95+
let seq = new BER(ber.getSequence());
96+
r = seq.getInteger();
97+
s = seq.getInteger();
98+
}
99+
else {
100+
// "20" is specified in the xmldsig-core spec.
101+
r = Crypt.PKCS1.OS2IP(sig.slice(0, 20));
102+
s = Crypt.PKCS1.OS2IP(sig.slice(20, 40));
103+
}
89104
return(this._verify(H, r, s));
90105
};
91106
};

modules/crypt/etc/ecdsa.js

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
/*
2-
* Copyright (c) 2016-2017 Moddable Tech, Inc.
2+
* Copyright (c) 2016-2021 Moddable Tech, Inc.
33
*
44
* This file is part of the Moddable SDK Runtime.
5-
*
5+
*
66
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as published by
88
* the Free Software Foundation, either version 3 of the License, or
99
* (at your option) any later version.
10-
*
10+
*
1111
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
1212
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1313
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414
* GNU Lesser General Public License for more details.
15-
*
15+
*
1616
* You should have received a copy of the GNU Lesser General Public License
1717
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
1818
*
19-
* This file incorporates work covered by the following copyright and
20-
* permission notice:
19+
* This file incorporates work covered by the following copyright and
20+
* permission notice:
2121
*
2222
* Copyright (C) 2010-2016 Marvell International Ltd.
2323
* Copyright (C) 2002-2010 Kinoma, Inc.
@@ -39,15 +39,17 @@ import RNG from "rng";
3939
import PKCS1 from "pkcs1";
4040
import Mont from "mont";
4141
import EC from "ec";
42+
import Curve from "curve";
43+
import BER from "ber";
4244

4345
export default class ECDSA {
44-
constructor(key, priv) {
45-
this.u = priv ? key.du: key.Qu;
46-
this.G = key.G;
47-
this.orderSize = (BigInt.bitLength(key.n) + 7) >>> 3;
48-
this.n = new Mont({m: key.n});
49-
this.ec = new EC(key.a, key.b, key.p);
50-
this.k = key.k; // just for the debugging purpose
46+
constructor(key, curve, priv) {
47+
this.u = key;
48+
this.G = curve.G;
49+
this.orderSize = curve.orderSize;
50+
this.n = new Mont({m: curve.n});
51+
this.ec = curve.ec;
52+
this.k = curve.k; // just for a debugging purpose
5153
};
5254
_sign(H) {
5355
// (r, s) = (k*G, (e + du*r) / k)
@@ -69,11 +71,16 @@ export default class ECDSA {
6971
sig.s = s;
7072
return sig;
7173
};
72-
sign(H) {
73-
var sig = this._sign(H);
74-
var os = new ArrayBuffer();
75-
var l = this.orderSize;
76-
return os.concat(PKCS1.I2OSP(sig.r, l), PKCS1.I2OSP(sig.s, l));
74+
sign(H, asn1) {
75+
if (asn1) {
76+
return BER.encode([0x30, [0x02, sig.r], [0x02, sig.s]]);
77+
}
78+
else {
79+
var sig = this._sign(H);
80+
var os = new ArrayBuffer();
81+
var l = this.orderSize;
82+
return os.concat(PKCS1.I2OSP(sig.r, l), PKCS1.I2OSP(sig.s, l));
83+
}
7784
};
7885
_verify(H, r, s) {
7986
// u1 = e / s
@@ -90,13 +97,22 @@ export default class ECDSA {
9097
var u2 = n.mul(r, s_inv);
9198
// var R = ec.add(ec.mul(G, u1), ec.mul(Qu, u2));
9299
var R = ec.mul2(G, u1, Qu, u2);
93-
return R.X === r;
100+
return R.X == r;
94101

95102
};
96-
verify(H, sig) {
97-
var l = this.orderSize;
98-
var r = PKCS1.OS2IP(sig.slice(0, l));
99-
var s = PKCS1.OS2IP(sig.slice(l, l*2));
103+
verify(H, sig, asn1) {
104+
var r, s;
105+
if (asn1) {
106+
let ber = new BER(sig);
107+
let seq = new BER(ber.getSequence());
108+
r = seq.getInteger();
109+
s = seq.getInteger();
110+
}
111+
else {
112+
let l = this.orderSize;
113+
r = PKCS1.OS2IP(sig.slice(0, l));
114+
s = PKCS1.OS2IP(sig.slice(l, l*2));
115+
}
100116
return this._verify(H, r, s);
101117
};
102118
static randint(max) {

modules/crypt/etc/hmac.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
/*
2-
* Copyright (c) 2016-2017 Moddable Tech, Inc.
2+
* Copyright (c) 2016-2021 Moddable Tech, Inc.
33
*
44
* This file is part of the Moddable SDK Runtime.
5-
*
5+
*
66
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as published by
88
* the Free Software Foundation, either version 3 of the License, or
99
* (at your option) any later version.
10-
*
10+
*
1111
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
1212
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1313
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414
* GNU Lesser General Public License for more details.
15-
*
15+
*
1616
* You should have received a copy of the GNU Lesser General Public License
1717
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
1818
*
19-
* This file incorporates work covered by the following copyright and
20-
* permission notice:
19+
* This file incorporates work covered by the following copyright and
20+
* permission notice:
2121
*
2222
* Copyright (C) 2010-2016 Marvell International Ltd.
2323
* Copyright (C) 2002-2010 Kinoma, Inc.
@@ -44,30 +44,30 @@ export default class HMAC {
4444
this.hashLen = bitlen >> 3; // in byte
4545
};
4646
init(key) {
47-
var arr = new Uint8Array(key);
48-
var h = this.h;
49-
if (arr.length > h.blockSize) {
47+
const h = this.h;
48+
if (key.byteLength > h.blockSize) {
5049
// truncate the key
5150
h.reset();
5251
h.write(key);
53-
arr = new Uint8Array(h.close());
52+
key = new Uint8Array(h.close());
5453
}
55-
var n = h.blockSize;
56-
var l = arr.length;
57-
this.ipad = new Uint8Array(n);
58-
this.opad = new Uint8Array(n);
59-
if (l > n)
60-
l = n;
61-
var i = 0;
62-
for (; i < l; i++) {
63-
var c = arr[i];
64-
this.ipad[i] = c ^ 0x36;
65-
this.opad[i] = c ^ 0x5c;
54+
else {
55+
key = new Uint8Array(key);
6656
}
67-
for (; i < n; i++) {
68-
this.ipad[i] = 0x36;
69-
this.opad[i] = 0x5c;
57+
58+
this.ipad = new Uint8Array(h.blockSize);
59+
this.opad = new Uint8Array(h.blockSize);
60+
61+
let l = key.length;
62+
if (l > h.blockSize)
63+
l = h.blockSize;
64+
let i = 0;
65+
for (; i < l; i++) {
66+
this.ipad[i] = key[i] ^ 0x36;
67+
this.opad[i] = key[i] ^ 0x5c;
7068
}
69+
this.ipad.fill(0x36, i);
70+
this.opad.fill(0x5c, i);
7171
h.reset();
7272
h.write(this.ipad.buffer);
7373
};

modules/crypt/etc/pkcs8.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
/*
2-
* Copyright (c) 2016-2017 Moddable Tech, Inc.
2+
* Copyright (c) 2016-2021 Moddable Tech, Inc.
33
*
44
* This file is part of the Moddable SDK Runtime.
5-
*
5+
*
66
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as published by
88
* the Free Software Foundation, either version 3 of the License, or
99
* (at your option) any later version.
10-
*
10+
*
1111
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
1212
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1313
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414
* GNU Lesser General Public License for more details.
15-
*
15+
*
1616
* You should have received a copy of the GNU Lesser General Public License
1717
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
1818
*
19-
* This file incorporates work covered by the following copyright and
20-
* permission notice:
19+
* This file incorporates work covered by the following copyright and
20+
* permission notice:
2121
*
2222
* Copyright (C) 2010-2016 Marvell International Ltd.
2323
* Copyright (C) 2002-2010 Kinoma, Inc.

modules/crypt/etc/x509.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016-2017 Moddable Tech, Inc.
2+
* Copyright (c) 2016-2021 Moddable Tech, Inc.
33
*
44
* This file is part of the Moddable SDK Runtime.
55
*

0 commit comments

Comments
 (0)