-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.init1.js
261 lines (235 loc) · 7.8 KB
/
RSA.init1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* RSA.init1.js
* An implementation of RSA public-key cryptography
* Methods for RSA public key operation.
*
* See RSA.readme.txt for further information.
*
*
* ACKNOWLEDGMENT
*
* This library is originally written by Tom Wu
*
* Copyright (c) 2005 Tom Wu
* All Rights Reserved.
* http://www-cs-students.stanford.edu/~tjw/jsbn/
*
* MODIFICATION
*
* Some modifications are applied by Atsushi Oka
*
* Atsushi Oka
* http://oka.nu/
*
* - Packaged
* - Added Object-Oriented Interface.
* - Added Asynchronous Execution Feauture.
*/
function initRSA1( packages ) {
__unit( "RSA.init1.js" );
__uses( "packages.js" );
// __uses( "SecureRandom.js" );
__uses( "BigInteger.init1.js" );
__uses( "isarray.js" );
/////////////////////////////////////////////////////////////////////
// import
/////////////////////////////////////////////////////////////////////
var BigInteger = __import( packages, "titaniumcore.crypto.BigInteger" );
// var SecureRandom = __import( packages, "titaniumcore.crypto.SecureRandom" );
/////////////////////////////////////
// implementation
/////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// class RSA
/////////////////////////////////////////////////////////////////////
var none = function(m){
return m;
};
// "empty" RSA key constructor
var RSA = function () {
this.n = null;
this.e = 0;
this.d = null;
this.p = null;
this.q = null;
this.dmp1 = null;
this.dmq1 = null;
this.coeff = null;
this.ksize = 0;
this.tolerantlyGenerate=true;
this.preprocessPublic = none;
this.preprocessPrivate = none;
this.keyFormat = RSA.defaultKeyFormat;
this.messageFormat = RSA.defaultMessageFormat;
}
RSA.prototype.className = "RSA";
RSA.prototype.toString = function (r) {
if ( r == null ) r = 16;
var rsaKey =this;
var result=""
+ "n=" + ( rsaKey.n == null ? "" : rsaKey.n .toString(r) + "(" + this.n.bitLength() + ")" ) + "\n"
+ "e=" + ( rsaKey.e == null ? "" : rsaKey.e .toString(r) ) +"\n"
+ "d=" + ( rsaKey.d == null ? "" : rsaKey.d .toString(r) + "(" + this.d.bitLength() + ")" ) + "\n"
+ "p=" + ( rsaKey.p == null ? "" : rsaKey.p .toString(r) ) +"\n"
+ "q=" + ( rsaKey.q == null ? "" : rsaKey.q .toString(r) ) +"\n"
+ "dmp1=" + ( rsaKey.dmp1 == null ? "" : rsaKey.dmp1 .toString(r) ) +"\n"
+ "dmq1=" + ( rsaKey.dmq1 == null ? "" : rsaKey.dmq1 .toString(r) ) +"\n"
+ "coeff="+ ( rsaKey.coeff == null ? "" : rsaKey.coeff.toString(r) ) +"\n"
return result;
};
RSA.defaultKeyFormat= null;
RSA.installKeyFormat= function( keyFormat ){
RSA.defaultKeyFormat = keyFormat;
};
RSA.defaultMessageFormat= null;
RSA.installMessageFormat= function( messageFormat ){
RSA.defaultMessageFormat = messageFormat;
};
// (private)
function makeGetterSetter(name){
return function() {
if ( arguments.length == 0 ) {
return this[name];
} else {
var value = arguments[0];
var type = typeof value;
if ( value == null ) {
throw "parameter " + name + " cannot be null.";
} else if ( type == "object" && value.className == "BigInteger" ) {
this[name] = value;
} else if ( type == "object" && value.isArray ) {
this[name] = new BigInteger(value);
} else if ( type == "string" ) {
this[name] = new BigInteger( value, 16 );
} else if ( type == "number" ) {
this[name] = new BigInteger( value );
} else {
throw "parameter " + name + " must be a BigInteger object or a hex string or a number object.";
}
return this;
}
};
}
// (private)
function makeGetterSetter2(name){
return function() {
if ( arguments.length == 0 ) {
return this[name];
} else {
var value = arguments[0];
var type = typeof value;
if ( value == null ) {
throw "parameter " + name + " cannot be null.";
} else if ( type == "object" && value.className == "BigInteger" ) {
this[name] = value.intValue();
} else if ( type == "object" && value.isArray ) {
this[name] = new BigInteger(value).intValue();
} else if ( type == "string" ) {
this[name] = parseInt( value, 16 );
} else if ( type == "number" ) {
this[name] = value;
} else {
throw "parameter " + name + " must be a BigInteger object or a hex string or a number object.";
}
return this;
}
};
}
RSA.prototype._n = makeGetterSetter( "n" );
RSA.prototype._e = makeGetterSetter2( "e" );
RSA.prototype._d = makeGetterSetter( "d" );
RSA.prototype._p = makeGetterSetter( "p" );
RSA.prototype._q = makeGetterSetter( "q" );
RSA.prototype._dmp1 = makeGetterSetter( "dmp1" );
RSA.prototype._dmq1 = makeGetterSetter( "dmq1" );
RSA.prototype._coeff = makeGetterSetter( "coeff" );
RSA.prototype._ksize = makeGetterSetter2( "ksize" );
// // Set the public key fields N and e from hex strings
// RSA.prototype.setPublic = function (N,E) {
// if(N != null && E != null && N.length > 0 && E.length > 0) {
// this.n = new BigInteger(N,16);
// this.e = parseInt(E,16);
// }
// else
// alert("Invalid public key");
// };
//
// // Perform raw public operation on "x": return x^e (mod n)
// RSA.prototype.doPublic = function (x) {
// return x.modPowInt(this.e, this.n);
// };
/*
* publicKey(n,e)
* Set a public key to the object. Returns current value as an array
* when no parameter is specified. The type of each parameters are
* automatically converted to proper type.
*/
RSA.prototype.publicKey = function (n,e,ksize) {
if ( arguments.length == 0 ) {
return { n:this.n, e:this.e, ksize:this.ksize };
} else {
this._n(n);
this._e(e);
this._ksize( ksize );
}
};
/*
* processPublic(m)
* encrypt( when it is used as cipher ) / decrypt ( when it is used as
* signing ) message. Parameter m specifies a BigInteger object to
* encrypt/decrypt. Returns an encrypted/decrypted BigInteger value.
*/
RSA.prototype.processPublic = function (m) {
m = this.preprocessPublic(m);
return m.modPowInt(this.e, this.n);
};
RSA.prototype.publicEncrypt = function(m) {
if ( this.messageFormat==null ) {
throw "Error. No message format is installed.";
}
return this.messageFormat.publicEncrypt( this, m );
};
RSA.prototype.publicDecrypt = function(m) {
if ( this.messageFormat==null ) {
throw "Error. No message format is installed.";
}
return this.messageFormat.publicDecrypt( this, m );
};
RSA.prototype.publicEncryptMaxSize = function() {
if ( this.messageFormat==null ) {
throw "Error. No message format is installed.";
}
return this.messageFormat.publicEncryptMaxSize( this );
};
/*
* Set a public key as a binary representation string.
*/
RSA.prototype.publicKeyBytes = function() {
if ( this.keyFormat==null ) {
throw "Error. No key format is installed.";
}
if ( arguments.length==0 ) {
if ( this.ksize == null || this.ksize == 0 ) {
throw "key size is not set.";
}
return this.keyFormat.encodePublicKey( this.n, this.e, this.ksize );
} else {
var key = this.keyFormat.decodePublicKey( arguments[0] );
this.publicKey( key.n, key.e, key.ksize );
return this;
}
};
/////////////////////////////////////////////////////////////////////
RSA.log = function(message) {
// trace(message);
};
RSA.err = function(message) {
trace(message);
};
/////////////////////////////////////////////////////////////////////
// export
/////////////////////////////////////////////////////////////////////
__export( packages, "titaniumcore.crypto.RSA" ,RSA );
};
initRSA1( this );
// vim:ts=8 sw=4:noexpandtab: