forked from asmcrypto/asmcrypto.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
179 lines (156 loc) · 7.85 KB
/
test.html
File metadata and controls
179 lines (156 loc) · 7.85 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>asmCrypto test suite</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.12.0.css">
<script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
<script src="asmcrypto.js"></script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script>
test( "dummy test", function () {
ok( true, "Passed!" );
});
///////////////////////////////////////////////////////////////////////////////
module("Core API");
var sha256_vectors = [
[ 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', '' ],
[ 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', 'abc' ],
[ 'f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650', 'message digest' ],
[ 'f30ceb2bb2829e79e4ca9753d35a8ecc00262d164cc077080295381cbd643f0d', 'secure hash algorithm' ],
[ '6819d915c73f4d1e77e4e1b52d1fa0f9cf9beaead3939f15874bd988e2a23630', 'SHA256 is considered to be safe' ],
[ '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1', 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' ],
[ 'f08a78cbbaee082b052ae0708f32fa1e50c5c421aa772ba5dbb406a2ea6be342', 'For this sample, this 63-byte string will be used as input data' ],
[ 'ab64eff7e88e2e46165e29f2bce41826bd4c7b3552f6b382a9e7d3af47c245f8', 'This is exactly 64 bytes long, not counting the terminating byte' ]
];
test( "asmCrypto.SHA256.hex", function () {
for ( var i = 0; i < sha256_vectors.length; ++i ) {
equal(
asmCrypto.SHA256.hex( sha256_vectors[i][1] ),
sha256_vectors[i][0],
"vector " + i
);
}
});
///////////////////////////////////////////////////////////////////////////////
var hmac_sha256_vectors = [
[ 'b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad', '', '' ],
[ 'f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8', 'key', 'The quick brown fox jumps over the lazy dog' ],
[ 'b54d57e9b21940b6496b58d5ac120eda9f1637788b5df058928637f2eca40cd9', 'MyVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryLoooooooooongPassword', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.' ]
];
test( "asmCrypto.HMAC_SHA256.hex", function () {
for ( var i = 0; i < hmac_sha256_vectors.length; ++i ) {
equal(
asmCrypto.HMAC_SHA256.hex( hmac_sha256_vectors[i][2], hmac_sha256_vectors[i][1] ),
hmac_sha256_vectors[i][0],
"vector " + i
);
}
});
///////////////////////////////////////////////////////////////////////////////
var pbkdf2_hmac_sha256_vectors = [
[ '120fb6cffcf8b32c43e7225256c4f837a86548c92ccc35480805987cb70be17b', 'password', 'salt', 1, 32 ],
[ 'ae4d0c95af6b46d32d0adff928f06dd02a303f8ef3c251dfd6e2d85a95474c43', 'password', 'salt', 2, 32 ],
[ 'c5e478d59288c841aa530db6845c4c8d962893a001ce4e11a4963873aa98134a', 'password', 'salt', 4096, 32 ],
[ '348c89dbcbd32b2f32d814b8116e84cf2b17347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9', 'passwordPASSWORDpassword', 'saltSALTsaltSALTsaltSALTsaltSALTsalt', 4096, 40 ],
[ '89b69d0516f829893c696226650a8687', "pass\0word", "sa\0lt", 4096, 16 ],
[ 'cdc8b1780ca68aba97f1f729c9d281719702eb4b308d7d87409817e60188be0d', 'MyVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryLoooooooooongPassword', 'MyVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryVeeeeeeeeeeeeeryLoooooooooongPassword', 4096, 32 ]
];
test( "asmCrypto.PBKDF2_HMAC_SHA256.hex", function () {
for ( var i = 0; i < pbkdf2_hmac_sha256_vectors.length; ++i ) {
equal(
// got
asmCrypto.PBKDF2_HMAC_SHA256.hex(
pbkdf2_hmac_sha256_vectors[i][1], // password
pbkdf2_hmac_sha256_vectors[i][2], // salt
pbkdf2_hmac_sha256_vectors[i][3], // count
pbkdf2_hmac_sha256_vectors[i][4] // dklen
),
// expect
pbkdf2_hmac_sha256_vectors[i][0],
// comment
"asmCrypto.PBKDF2_HMAC_SHA256.hex('"
+pbkdf2_hmac_sha256_vectors[i][1]+"', '"
+pbkdf2_hmac_sha256_vectors[i][2]+"', '"
+pbkdf2_hmac_sha256_vectors[i][3]+"', '"
+pbkdf2_hmac_sha256_vectors[i][4]
+"') is equal to '"+pbkdf2_hmac_sha256_vectors[i][0]+"'"
);
}
});
///////////////////////////////////////////////////////////////////////////////
var cbc_aes_vectors = [
[ // key
[ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c ],
// iv
[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f ],
// clear text
[ 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 ],
// cipher text
[ 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 ]
],
];
test( "asmCrypto.AES_CBC.encrypt / asmCrypto.AES_CBC.decrypt", function () {
for ( var i = 0; i < cbc_aes_vectors.length; ++i ) {
var key = new Uint8Array( cbc_aes_vectors[i][0] ),
iv = new Uint8Array( cbc_aes_vectors[i][1] ),
clear = new Uint8Array( cbc_aes_vectors[i][2] ),
cipher = new Uint8Array( cbc_aes_vectors[i][3] );
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_CBC.encrypt( clear, key, false, iv ) ),
asmCrypto.bytes_to_hex(cipher),
"encrypt vector " + i
);
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_CBC.decrypt( cipher, key, false, iv ) ),
asmCrypto.bytes_to_hex(clear),
"encrypt vector " + i
);
}
});
var ccm_aes_vectors = [
[
// key
[ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf ],
// nonce
[ 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5 ],
// adata
[ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 ],
// input message
[ 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e ],
// encrypted message
[ 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2, 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0 ]
]
];
test( "asmCrypto.AES_CCM.encrypt / asmCrypto.AES_CCM.decrypt", function () {
for ( var i = 0; i < ccm_aes_vectors.length; ++i ) {
var key = new Uint8Array( ccm_aes_vectors[i][0] ),
nonce = new Uint8Array( ccm_aes_vectors[i][1] ),
adata = new Uint8Array( ccm_aes_vectors[i][2] ),
clear = new Uint8Array( ccm_aes_vectors[i][3] ),
cipher = new Uint8Array( ccm_aes_vectors[i][4] );
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_CCM.encrypt( clear, key, nonce, adata, 8 ) ),
asmCrypto.bytes_to_hex(cipher),
"encrypt vector " + i
);
equal(
asmCrypto.bytes_to_hex( asmCrypto.AES_CCM.decrypt( cipher, key, nonce, adata, 8 ) ),
asmCrypto.bytes_to_hex(clear),
"encrypt vector " + i
);
}
});
</script>
</body>