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";
3939import PKCS1 from "pkcs1" ;
4040import Mont from "mont" ;
4141import EC from "ec" ;
42+ import Curve from "curve" ;
43+ import BER from "ber" ;
4244
4345export 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 ) {
0 commit comments