does not pass tests yet

This commit is contained in:
Bart Butler 2015-04-20 14:51:56 -07:00 committed by Tankred Hase
parent 1c6e777a3d
commit 5711028449
12 changed files with 1569 additions and 1077 deletions

View File

@ -3,7 +3,8 @@
* @module crypto/hash * @module crypto/hash
*/ */
var sha = require('./sha.js'), var sha = require('./sha.js'),
forge_sha256 = require('./forge_sha256.js'); forge_sha256 = require('./forge_sha256.js'),
util = require('../../util.js');
module.exports = { module.exports = {
/** @see module:crypto/hash/md5 */ /** @see module:crypto/hash/md5 */
@ -24,8 +25,8 @@ module.exports = {
/** /**
* Create a hash on the specified data using the specified algorithm * Create a hash on the specified data using the specified algorithm
* @param {module:enums.hash} algo Hash algorithm type (see {@link http://tools.ietf.org/html/rfc4880#section-9.4|RFC 4880 9.4}) * @param {module:enums.hash} algo Hash algorithm type (see {@link http://tools.ietf.org/html/rfc4880#section-9.4|RFC 4880 9.4})
* @param {String} data Data to be hashed * @param {Uint8Array} data Data to be hashed
* @return {String} hash value * @return {Uint8Array} hash value
*/ */
digest: function(algo, data) { digest: function(algo, data) {
switch (algo) { switch (algo) {
@ -40,9 +41,10 @@ module.exports = {
return this.ripemd(data); return this.ripemd(data);
case 8: case 8:
// - SHA256 [FIPS180] // - SHA256 [FIPS180]
//return this.sha256(data);
var sha256 = forge_sha256.create(); var sha256 = forge_sha256.create();
sha256.update(data); sha256.update(util.Uint8Array2str(data));
return sha256.digest().getBytes(); return util.str2Uint8Array(sha256.digest().getBytes());
case 9: case 9:
// - SHA384 [FIPS180] // - SHA384 [FIPS180]
return this.sha384(data); return this.sha384(data);

View File

@ -24,8 +24,8 @@ var util = require('../../util.js');
* @param {String} entree string to hash * @param {String} entree string to hash
*/ */
module.exports = function (entree) { module.exports = function (entree) {
var hex = md5(entree); var hex = md5(util.Uint8Array2str(entree));
var bin = util.hex2bin(hex); var bin = util.str2Uint8Array(util.hex2bin(hex));
return bin; return bin;
}; };

View File

@ -20,10 +20,16 @@
/* Modified by Recurity Labs GmbH /* Modified by Recurity Labs GmbH
*/ */
/* Modified by ProtonTech AG
*/
/** /**
* @requires util
* @module crypto/hash/ripe-md * @module crypto/hash/ripe-md
*/ */
var util = require('../../util.js');
var RMDsize = 160; var RMDsize = 160;
var X = []; var X = [];
@ -284,14 +290,14 @@ function RMD(message) {
function RMDstring(message) { function RMDstring(message) {
var hashcode = RMD(message); var hashcode = RMD(util.Uint8Array2str(message));
var retString = ""; var retString = "";
for (var i = 0; i < RMDsize / 8; i++) { for (var i = 0; i < RMDsize / 8; i++) {
retString += String.fromCharCode(hashcode[i]); retString += String.fromCharCode(hashcode[i]);
} }
return retString; return util.str2Uint8Array(retString);
} }
module.exports = RMDstring; module.exports = RMDstring;

File diff suppressed because it is too large Load Diff

View File

@ -135,7 +135,7 @@ module.exports = {
encode: function(algo, M, emLen) { encode: function(algo, M, emLen) {
var i; var i;
// Apply the hash function to the message M to produce a hash value H // Apply the hash function to the message M to produce a hash value H
var H = hash.digest(algo, M); var H = util.Uint8Array2str(hash.digest(algo, util.str2Uint8Array(M)));
if (H.length !== hash.getHashByteLength(algo)) { if (H.length !== hash.getHashByteLength(algo)) {
throw new Error('Invalid hash length'); throw new Error('Invalid hash length');
} }

View File

@ -88,7 +88,7 @@ module.exports = {
var n = keyIntegers[0].toBigInteger(); var n = keyIntegers[0].toBigInteger();
m = pkcs1.emsa.encode(hash_algo, m = pkcs1.emsa.encode(hash_algo,
data, keyIntegers[0].byteLength()); data, keyIntegers[0].byteLength());
return rsa.sign(m, d, n).toMPI(); return util.str2Uint8Array(rsa.sign(m, d, n).toMPI());
case 17: case 17:
// DSA (Digital Signature Algorithm) [FIPS186] [HAC] // DSA (Digital Signature Algorithm) [FIPS186] [HAC]
@ -102,7 +102,7 @@ module.exports = {
m = data; m = data;
var result = dsa.sign(hash_algo, m, g, p, q, x); var result = dsa.sign(hash_algo, m, g, p, q, x);
return result[0].toString() + result[1].toString(); return util.str2Uint8Array(result[0].toString() + result[1].toString());
case 16: case 16:
// Elgamal (Encrypt-Only) [ELGAMAL] [HAC] // Elgamal (Encrypt-Only) [ELGAMAL] [HAC]
throw new Error('Signing with Elgamal is not defined in the OpenPGP standard.'); throw new Error('Signing with Elgamal is not defined in the OpenPGP standard.');

View File

@ -191,13 +191,13 @@ PublicKey.prototype.getFingerprint = function () {
var toHash = ''; var toHash = '';
if (this.version == 4) { if (this.version == 4) {
toHash = this.writeOld(); toHash = this.writeOld();
this.fingerprint = crypto.hash.sha1(util.Uint8Array2str(toHash)); this.fingerprint = util.Uint8Array2str(crypto.hash.sha1(toHash));
} else if (this.version == 3) { } else if (this.version == 3) {
var mpicount = crypto.getPublicMpiCount(this.algorithm); var mpicount = crypto.getPublicMpiCount(this.algorithm);
for (var i = 0; i < mpicount; i++) { for (var i = 0; i < mpicount; i++) {
toHash += this.mpi[i].toBytes(); toHash += this.mpi[i].toBytes();
} }
this.fingerprint = crypto.hash.md5(toHash); this.fingerprint = util.Uint8Array2str(crypto.hash.md5(util.str2Uint8Array(toHash)));
} }
this.fingerprint = util.hexstrdump(this.fingerprint); this.fingerprint = util.hexstrdump(this.fingerprint);
return this.fingerprint; return this.fingerprint;

View File

@ -68,7 +68,7 @@ function get_hash_fn(hash) {
return crypto.hash.sha1; return crypto.hash.sha1;
else else
return function(c) { return function(c) {
return util.Uint8Array2str(util.writeNumber(util.calc_checksum(util.str2Uint8Array(c)), 2)); return util.writeNumber(util.calc_checksum(c), 2);
}; };
} }
@ -81,7 +81,11 @@ function parse_cleartext_mpi(hash_algorithm, cleartext, algorithm) {
var hashtext = util.Uint8Array2str(cleartext.subarray(cleartext.length - hashlen, cleartext.length)); var hashtext = util.Uint8Array2str(cleartext.subarray(cleartext.length - hashlen, cleartext.length));
cleartext = cleartext.subarray(0, cleartext.length - hashlen); cleartext = cleartext.subarray(0, cleartext.length - hashlen);
var hash = hashfn(util.Uint8Array2str(cleartext)); var hash = util.Uint8Array2str(hashfn(cleartext));
console.log(hash);
console.log(hashtext);
console.log(hash_algorithm);
if (hash != hashtext) if (hash != hashtext)
return new Error("Hash mismatch."); return new Error("Hash mismatch.");
@ -109,9 +113,9 @@ function write_cleartext_mpi(hash_algorithm, algorithm, mpi) {
var bytes = util.concatUint8Array(arr); var bytes = util.concatUint8Array(arr);
var hash = get_hash_fn(hash_algorithm)(util.Uint8Array2str(bytes)); var hash = get_hash_fn(hash_algorithm)(bytes);
return util.concatUint8Array([bytes, util.str2Uint8Array(hash)]); return util.concatUint8Array([bytes, hash]);
} }
@ -220,16 +224,6 @@ SecretKey.prototype.decrypt = function (passphrase) {
symmetric, symmetric,
key; key;
if(!Uint8Array.prototype.isPrototypeOf(this.encrypted)) {
if(Uint8Array.prototype.isPrototypeOf(this.encrypted.message)) {
throw new Error('this.encrypted.message is a typed array!');
}
if(this.encrypted === null) {
throw new Error('this.encrypted is null!');
}
throw new Error(Object.prototype.toString.call(this.encrypted));
}
var s2k_usage = this.encrypted[i++]; var s2k_usage = this.encrypted[i++];
// - [Optional] If string-to-key usage octet was 255 or 254, a one- // - [Optional] If string-to-key usage octet was 255 or 254, a one-
@ -248,7 +242,7 @@ SecretKey.prototype.decrypt = function (passphrase) {
} else { } else {
symmetric = s2k_usage; symmetric = s2k_usage;
symmetric = enums.read(enums.symmetric, symmetric); symmetric = enums.read(enums.symmetric, symmetric);
key = util.str2Uint8Array(crypto.hash.md5(passphrase)); key = crypto.hash.md5(passphrase);
} }

View File

@ -224,14 +224,14 @@ Signature.prototype.sign = function (key, data) {
var trailer = this.calculateTrailer(); var trailer = this.calculateTrailer();
var toHash = util.Uint8Array2str(util.concatUint8Array([this.toSign(signatureType, data), this.signatureData, trailer])); var toHash = util.concatUint8Array([this.toSign(signatureType, data), this.signatureData, trailer]);
var hash = crypto.hash.digest(hashAlgorithm, toHash); var hash = crypto.hash.digest(hashAlgorithm, toHash);
this.signedHashValue = util.str2Uint8Array(hash.substr(0, 2)); this.signedHashValue = hash.subarray(0, 2);
this.signature = util.str2Uint8Array(crypto.signature.sign(hashAlgorithm, this.signature = crypto.signature.sign(hashAlgorithm,
publicKeyAlgorithm, key.mpi, util.str2Uint8Array(toHash))); publicKeyAlgorithm, key.mpi, toHash);
}; };
/** /**
@ -287,7 +287,7 @@ Signature.prototype.write_all_sub_packets = function () {
// 2 octets of value length // 2 octets of value length
bytes.push(util.writeNumber(value.length, 2)); bytes.push(util.writeNumber(value.length, 2));
bytes.push(util.str2Uint8Array(name + value)); bytes.push(util.str2Uint8Array(name + value));
bytes = concatUint8Array(bytes); bytes = util.concatUint8Array(bytes);
arr.push(write_sub_packet(sub.notation_data, bytes)); arr.push(write_sub_packet(sub.notation_data, bytes));
} }
} }

View File

@ -87,7 +87,7 @@ SymEncryptedIntegrityProtected.prototype.encrypt = function (sessionKeyAlgorithm
// This could probably be cleaned up to use less memory // This could probably be cleaned up to use less memory
var tohash = util.concatUint8Array([bytes, mdc]); var tohash = util.concatUint8Array([bytes, mdc]);
var hash = util.str2Uint8Array(crypto.hash.sha1(util.Uint8Array2str(util.concatUint8Array([prefix, tohash])))); var hash = crypto.hash.sha1(util.concatUint8Array([prefix, tohash]));
tohash = util.concatUint8Array([tohash, hash]); tohash = util.concatUint8Array([tohash, hash]);
@ -111,7 +111,7 @@ SymEncryptedIntegrityProtected.prototype.decrypt = function (sessionKeyAlgorithm
// there must be a modification detection code packet as the // there must be a modification detection code packet as the
// last packet and everything gets hashed except the hash itself // last packet and everything gets hashed except the hash itself
this.hash = crypto.hash.sha1(util.Uint8Array2str(util.concatUint8Array([crypto.cfb.mdc(sessionKeyAlgorithm, key, this.encrypted), this.hash = util.Uint8Array2str(crypto.hash.sha1(util.concatUint8Array([crypto.cfb.mdc(sessionKeyAlgorithm, key, this.encrypted),
decrypted.subarray(0, decrypted.length - 20)]))); decrypted.subarray(0, decrypted.length - 20)])));
var mdc = util.Uint8Array2str(decrypted.subarray(decrypted.length - 20, decrypted.length)); var mdc = util.Uint8Array2str(decrypted.subarray(decrypted.length - 20, decrypted.length));

View File

@ -142,33 +142,33 @@ S2K.prototype.write = function () {
* hashAlgorithm hash length * hashAlgorithm hash length
*/ */
S2K.prototype.produce_key = function (passphrase, numBytes) { S2K.prototype.produce_key = function (passphrase, numBytes) {
passphrase = util.encode_utf8(passphrase); passphrase = util.str2Uint8Array(util.encode_utf8(passphrase));
function round(prefix, s2k) { function round(prefix, s2k) {
var algorithm = enums.write(enums.hash, s2k.algorithm); var algorithm = enums.write(enums.hash, s2k.algorithm);
switch (s2k.type) { switch (s2k.type) {
case 'simple': case 'simple':
return util.str2Uint8Array(crypto.hash.digest(algorithm, prefix + passphrase)); return crypto.hash.digest(algorithm, util.concatUint8Array([prefix,passphrase]));
case 'salted': case 'salted':
return util.str2Uint8Array(crypto.hash.digest(algorithm, return crypto.hash.digest(algorithm,
prefix + util.Uint8Array2str(s2k.salt) + passphrase)); util.concatUint8Array([prefix, s2k.salt, passphrase]));
case 'iterated': case 'iterated':
var isp = [], var isp = [],
count = s2k.get_count(), count = s2k.get_count(),
data = util.Uint8Array2str(s2k.salt) + passphrase; data = util.concatUint8Array([s2k.salt,passphrase]);
while (isp.length * data.length < count) while (isp.length * data.length < count)
isp.push(data); isp.push(data);
isp = isp.join(''); isp = util.concatUint8Array(isp);
if (isp.length > count) if (isp.length > count)
isp = isp.substr(0, count); isp = isp.subarray(0, count);
return util.str2Uint8Array(crypto.hash.digest(algorithm, prefix + isp)); return crypto.hash.digest(algorithm, util.concatUint8Array([prefix,isp]));
case 'gnu': case 'gnu':
throw new Error("GNU s2k type not supported."); throw new Error("GNU s2k type not supported.");
@ -179,14 +179,19 @@ S2K.prototype.produce_key = function (passphrase, numBytes) {
} }
var arr = [], var arr = [],
i = 0,
rlength = 0, rlength = 0,
prefix = ''; prefix = new Uint8Array(numBytes);
for(var i = 0; i<numBytes; i++) {
prefix[i] = 0;
}
while (rlength <= numBytes) { while (rlength <= numBytes) {
var result = round(prefix, this); var result = round(prefix.subarray(0,i), this);
arr.push(result); arr.push(result);
rlength += result.length; rlength += result.length;
prefix += String.fromCharCode(0); i++;
} }
return util.concatUint8Array(arr).subarray(0, numBytes); return util.concatUint8Array(arr).subarray(0, numBytes);

View File

@ -190,7 +190,7 @@ module.exports = {
// Uncomment for debugging // Uncomment for debugging
if(!(typeof str === 'string') && !String.prototype.isPrototypeOf(str)) { if(!(typeof str === 'string') && !String.prototype.isPrototypeOf(str)) {
throw new Error('Data must be in the form of a string'); throw new Error('str2Uint8Array: Data must be in the form of a string');
} }
var result = new Uint8Array(str.length); var result = new Uint8Array(str.length);
@ -211,7 +211,7 @@ module.exports = {
// Uncomment for debugging // Uncomment for debugging
if(!Uint8Array.prototype.isPrototypeOf(bin)) { if(!Uint8Array.prototype.isPrototypeOf(bin)) {
throw new Error('Data must be in the form of a Uint8Array'); throw new Error('Uint8Array2str: Data must be in the form of a Uint8Array');
} }
var result = []; var result = [];
@ -234,7 +234,9 @@ module.exports = {
// Uncomment for debugging // Uncomment for debugging
if(!Uint8Array.prototype.isPrototypeOf(element)) { if(!Uint8Array.prototype.isPrototypeOf(element)) {
throw new Error('Data must be in the form of a Uint8Array'); var err = new Error('here');
console.log(err.stack);
throw new Error('concatUint8Array: Data must be in the form of a Uint8Array');
} }
totalLength += element.length; totalLength += element.length;