From 90b67536563443d0454fe08158300db902339986 Mon Sep 17 00:00:00 2001 From: Torben Ewert Date: Mon, 20 Apr 2020 09:50:40 +0200 Subject: [PATCH] TASK: Reorganize function folder structure --- .../Functions/ReCrypt_Functions_MD.bs.js | 10 ++ .../Functions/ReCrypt_Functions_SHA1.bs.js | 41 ++++++ .../Functions/ReCrypt_Functions_SHA2.bs.js | 86 ++++++++++++ lib/js/src/ReCrypt/ReCrypt_Functions.bs.js | 125 +----------------- lib/js/src/ReCrypt/ReCrypt_MD5.bs.js | 4 +- lib/js/src/ReCrypt/ReCrypt_Sha1.bs.js | 8 +- lib/js/src/ReCrypt/ReCrypt_Sha224.bs.js | 8 +- lib/js/src/ReCrypt/ReCrypt_Sha256.bs.js | 8 +- lib/js/src/ReCrypt/ReCrypt_Sha384.bs.js | 8 +- lib/js/src/ReCrypt/ReCrypt_Sha512.bs.js | 8 +- src/ReCrypt/Functions/ReCrypt_Functions_MD.re | 3 + .../Functions/ReCrypt_Functions_SHA1.re | 27 ++++ .../Functions/ReCrypt_Functions_SHA2.re | 77 +++++++++++ src/ReCrypt/ReCrypt_Functions.re | 122 +---------------- src/ReCrypt/ReCrypt_Functions.rei | 24 ---- src/ReCrypt/ReCrypt_MD5.re | 2 +- src/ReCrypt/ReCrypt_Sha1.re | 8 +- src/ReCrypt/ReCrypt_Sha224.re | 12 +- src/ReCrypt/ReCrypt_Sha256.re | 12 +- src/ReCrypt/ReCrypt_Sha384.re | 12 +- src/ReCrypt/ReCrypt_Sha512.re | 12 +- 21 files changed, 304 insertions(+), 313 deletions(-) create mode 100644 lib/js/src/ReCrypt/Functions/ReCrypt_Functions_MD.bs.js create mode 100644 lib/js/src/ReCrypt/Functions/ReCrypt_Functions_SHA1.bs.js create mode 100644 lib/js/src/ReCrypt/Functions/ReCrypt_Functions_SHA2.bs.js create mode 100644 src/ReCrypt/Functions/ReCrypt_Functions_MD.re create mode 100644 src/ReCrypt/Functions/ReCrypt_Functions_SHA1.re create mode 100644 src/ReCrypt/Functions/ReCrypt_Functions_SHA2.re delete mode 100644 src/ReCrypt/ReCrypt_Functions.rei diff --git a/lib/js/src/ReCrypt/Functions/ReCrypt_Functions_MD.bs.js b/lib/js/src/ReCrypt/Functions/ReCrypt_Functions_MD.bs.js new file mode 100644 index 0000000..9341273 --- /dev/null +++ b/lib/js/src/ReCrypt/Functions/ReCrypt_Functions_MD.bs.js @@ -0,0 +1,10 @@ +// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE +'use strict'; + + +function rotl(x, n) { + return (x << n) | (x >>> (64 - n | 0)) | 0; +} + +exports.rotl = rotl; +/* No side effect */ diff --git a/lib/js/src/ReCrypt/Functions/ReCrypt_Functions_SHA1.bs.js b/lib/js/src/ReCrypt/Functions/ReCrypt_Functions_SHA1.bs.js new file mode 100644 index 0000000..019a988 --- /dev/null +++ b/lib/js/src/ReCrypt/Functions/ReCrypt_Functions_SHA1.bs.js @@ -0,0 +1,41 @@ +// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE +'use strict'; + +var Int32 = require("bs-platform/lib/js/int32.js"); + +function rotl(x, n) { + return (x << n) | (x >>> (64 - n | 0)) | 0; +} + +function change(x, y, z) { + return x & y ^ Int32.lognot(x) & z; +} + +function majority(x, y, z) { + return x & y ^ x & z ^ y & z; +} + +function parity(x, y, z) { + return x ^ y ^ z; +} + +function f(t, x, y, z) { + if (t >= 0 && t <= 19) { + return change(x, y, z); + } else if (t >= 20 && t <= 39) { + return parity(x, y, z); + } else if (t >= 40 && t <= 59) { + return majority(x, y, z); + } else if (t >= 60 && t <= 79) { + return parity(x, y, z); + } else { + return 0; + } +} + +exports.rotl = rotl; +exports.change = change; +exports.majority = majority; +exports.parity = parity; +exports.f = f; +/* No side effect */ diff --git a/lib/js/src/ReCrypt/Functions/ReCrypt_Functions_SHA2.bs.js b/lib/js/src/ReCrypt/Functions/ReCrypt_Functions_SHA2.bs.js new file mode 100644 index 0000000..a727a19 --- /dev/null +++ b/lib/js/src/ReCrypt/Functions/ReCrypt_Functions_SHA2.bs.js @@ -0,0 +1,86 @@ +// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE +'use strict'; + +var Int32 = require("bs-platform/lib/js/int32.js"); +var Int64 = require("bs-platform/lib/js/int64.js"); +var Caml_int64 = require("bs-platform/lib/js/caml_int64.js"); + +function rotr(x, n) { + return (x >>> n) | 0 | (x << (64 - n | 0)); +} + +function change(x, y, z) { + return x & y ^ Int32.lognot(x) & z; +} + +function majority(x, y, z) { + return x & y ^ x & z ^ y & z; +} + +function sum0(x) { + return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22); +} + +function sum1(x) { + return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25); +} + +function sigma0(x) { + return rotr(x, 7) ^ rotr(x, 18) ^ (x >>> 3); +} + +function sigma1(x) { + return rotr(x, 17) ^ rotr(x, 19) ^ (x >>> 10); +} + +var Int32$1 = { + rotr: rotr, + change: change, + majority: majority, + sum0: sum0, + sum1: sum1, + sigma0: sigma0, + sigma1: sigma1 +}; + +function rotr$1(x, n) { + return Caml_int64.or_(Caml_int64.lsr_(x, n), Caml_int64.lsl_(x, 64 - n | 0)); +} + +function change$1(x, y, z) { + return Caml_int64.xor(Caml_int64.and_(x, y), Caml_int64.and_(Int64.lognot(x), z)); +} + +function majority$1(x, y, z) { + return Caml_int64.xor(Caml_int64.xor(Caml_int64.and_(x, y), Caml_int64.and_(x, z)), Caml_int64.and_(y, z)); +} + +function sum0$1(x) { + return Caml_int64.xor(Caml_int64.xor(rotr$1(x, 28), rotr$1(x, 34)), rotr$1(x, 39)); +} + +function sum1$1(x) { + return Caml_int64.xor(Caml_int64.xor(rotr$1(x, 14), rotr$1(x, 18)), rotr$1(x, 41)); +} + +function sigma0$1(x) { + return Caml_int64.xor(Caml_int64.xor(rotr$1(x, 1), rotr$1(x, 8)), Caml_int64.lsr_(x, 7)); +} + +function sigma1$1(x) { + return Caml_int64.xor(Caml_int64.xor(rotr$1(x, 19), rotr$1(x, 61)), Caml_int64.lsr_(x, 6)); +} + +var Int64$1 = { + rotr: rotr$1, + change: change$1, + majority: majority$1, + sum0: sum0$1, + sum1: sum1$1, + sigma0: sigma0$1, + sigma1: sigma1$1 +}; + +exports.Int32 = Int32$1; +exports.Int64 = Int64$1; +/* No side effect */ diff --git a/lib/js/src/ReCrypt/ReCrypt_Functions.bs.js b/lib/js/src/ReCrypt/ReCrypt_Functions.bs.js index 9e87a42..930a686 100644 --- a/lib/js/src/ReCrypt/ReCrypt_Functions.bs.js +++ b/lib/js/src/ReCrypt/ReCrypt_Functions.bs.js @@ -1,127 +1,14 @@ // Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE 'use strict'; -var Int32 = require("bs-platform/lib/js/int32.js"); -var Int64 = require("bs-platform/lib/js/int64.js"); -var Caml_int64 = require("bs-platform/lib/js/caml_int64.js"); -function rotl(x, n) { - return (x << n) | (x >>> (64 - n | 0)) | 0; -} +var MD = /* alias */0; -var MD5 = { - rotl: rotl -}; +var SHA1 = /* alias */0; -function rotl$1(x, n) { - return (x << n) | (x >>> (64 - n | 0)) | 0; -} +var SHA2 = /* alias */0; -function parity(x, y, z) { - return x ^ y ^ z; -} - -function f(t, x, y, z) { - if (t >= 0 && t <= 19) { - var x$1 = x; - var y$1 = y; - var z$1 = z; - return x$1 & y$1 ^ Int32.lognot(x$1) & z$1; - } else if (t >= 20 && t <= 39) { - return parity(x, y, z); - } else if (t >= 40 && t <= 59) { - var x$2 = x; - var y$2 = y; - var z$2 = z; - return x$2 & y$2 ^ x$2 & z$2 ^ y$2 & z$2; - } else if (t >= 60 && t <= 79) { - return parity(x, y, z); - } else { - return 0; - } -} - -function rotr(x, n) { - return (x >>> n) | 0 | (x << (64 - n | 0)); -} - -function change(x, y, z) { - return x & y ^ Int32.lognot(x) & z; -} - -function majority(x, y, z) { - return x & y ^ x & z ^ y & z; -} - -function sum0(x) { - return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22); -} - -function sum1(x) { - return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25); -} - -function sigma0(x) { - return rotr(x, 7) ^ rotr(x, 18) ^ (x >>> 3); -} - -function sigma1(x) { - return rotr(x, 17) ^ rotr(x, 19) ^ (x >>> 10); -} - -function rotr$1(x, n) { - return Caml_int64.or_(Caml_int64.lsr_(x, n), Caml_int64.lsl_(x, 64 - n | 0)); -} - -function change$1(x, y, z) { - return Caml_int64.xor(Caml_int64.and_(x, y), Caml_int64.and_(Int64.lognot(x), z)); -} - -function majority$1(x, y, z) { - return Caml_int64.xor(Caml_int64.xor(Caml_int64.and_(x, y), Caml_int64.and_(x, z)), Caml_int64.and_(y, z)); -} - -function sum0$1(x) { - return Caml_int64.xor(Caml_int64.xor(rotr$1(x, 28), rotr$1(x, 34)), rotr$1(x, 39)); -} - -function sum1$1(x) { - return Caml_int64.xor(Caml_int64.xor(rotr$1(x, 14), rotr$1(x, 18)), rotr$1(x, 41)); -} - -function sigma0$1(x) { - return Caml_int64.xor(Caml_int64.xor(rotr$1(x, 1), rotr$1(x, 8)), Caml_int64.lsr_(x, 7)); -} - -function sigma1$1(x) { - return Caml_int64.xor(Caml_int64.xor(rotr$1(x, 19), rotr$1(x, 61)), Caml_int64.lsr_(x, 6)); -} - -var Sha1 = { - f: f, - rotl: rotl$1 -}; - -var Sha224_256 = { - change: change, - majority: majority, - sum0: sum0, - sum1: sum1, - sigma0: sigma0, - sigma1: sigma1 -}; - -var Sha384_512 = { - change: change$1, - majority: majority$1, - sum0: sum0$1, - sum1: sum1$1, - sigma0: sigma0$1, - sigma1: sigma1$1 -}; - -exports.MD5 = MD5; -exports.Sha1 = Sha1; -exports.Sha224_256 = Sha224_256; -exports.Sha384_512 = Sha384_512; +exports.MD = MD; +exports.SHA1 = SHA1; +exports.SHA2 = SHA2; /* No side effect */ diff --git a/lib/js/src/ReCrypt/ReCrypt_MD5.bs.js b/lib/js/src/ReCrypt/ReCrypt_MD5.bs.js index d4329c0..44b3e43 100644 --- a/lib/js/src/ReCrypt/ReCrypt_MD5.bs.js +++ b/lib/js/src/ReCrypt/ReCrypt_MD5.bs.js @@ -8,7 +8,7 @@ var Caml_bytes = require("bs-platform/lib/js/caml_bytes.js"); var Caml_int32 = require("bs-platform/lib/js/caml_int32.js"); var Pervasives = require("bs-platform/lib/js/pervasives.js"); var ReCrypt_Utils = require("./ReCrypt_Utils.bs.js"); -var ReCrypt_Functions = require("./ReCrypt_Functions.bs.js"); +var ReCrypt_Functions_MD = require("./Functions/ReCrypt_Functions_MD.bs.js"); var constants = [ -680876936, @@ -154,7 +154,7 @@ function make(message) { 32 <= j$1 && j$1 <= 47 ? (Caml_int32.imul(3, j$1) + 5 | 0) % 16 : Caml_int32.imul(7, j$1) % 16 ) ); - var temp = b + ReCrypt_Functions.MD5.rotl(((a + f | 0) + Caml_array.caml_array_get(w, g) | 0) + Caml_array.caml_array_get(constants, j$1) | 0, Caml_array.caml_array_get(s, ((j$1 >>> 4) << 2) | j$1 & 3)) | 0; + var temp = b + ReCrypt_Functions_MD.rotl(((a + f | 0) + Caml_array.caml_array_get(w, g) | 0) + Caml_array.caml_array_get(constants, j$1) | 0, Caml_array.caml_array_get(s, ((j$1 >>> 4) << 2) | j$1 & 3)) | 0; a = d; d = c; c = b; diff --git a/lib/js/src/ReCrypt/ReCrypt_Sha1.bs.js b/lib/js/src/ReCrypt/ReCrypt_Sha1.bs.js index 4c3f33b..2c1ff84 100644 --- a/lib/js/src/ReCrypt/ReCrypt_Sha1.bs.js +++ b/lib/js/src/ReCrypt/ReCrypt_Sha1.bs.js @@ -6,8 +6,8 @@ var Bytes = require("bs-platform/lib/js/bytes.js"); var Int32 = require("bs-platform/lib/js/int32.js"); var Caml_array = require("bs-platform/lib/js/caml_array.js"); var ReCrypt_Utils = require("./ReCrypt_Utils.bs.js"); -var ReCrypt_Functions = require("./ReCrypt_Functions.bs.js"); var ReCrypt_Preprocess = require("./ReCrypt_Preprocess.bs.js"); +var ReCrypt_Functions_SHA1 = require("./Functions/ReCrypt_Functions_SHA1.bs.js"); function constants(t) { if (t >= 0 && t <= 19) { @@ -48,7 +48,7 @@ function make(message) { Caml_array.caml_array_set(w, t, Caml_array.caml_array_get(w, t) | (message$1[((i << 6) + (t << 2) | 0) + 2 | 0] << 8) & 65280 | message$1[((i << 6) + (t << 2) | 0) + 3 | 0] & 255); } for(var t$1 = 16; t$1 <= 79; ++t$1){ - Caml_array.caml_array_set(w, t$1, ReCrypt_Functions.Sha1.rotl(Caml_array.caml_array_get(w, t$1 - 3 | 0) ^ Caml_array.caml_array_get(w, t$1 - 8 | 0) ^ Caml_array.caml_array_get(w, t$1 - 14 | 0) ^ Caml_array.caml_array_get(w, t$1 - 16 | 0), 1)); + Caml_array.caml_array_set(w, t$1, ReCrypt_Functions_SHA1.rotl(Caml_array.caml_array_get(w, t$1 - 3 | 0) ^ Caml_array.caml_array_get(w, t$1 - 8 | 0) ^ Caml_array.caml_array_get(w, t$1 - 14 | 0) ^ Caml_array.caml_array_get(w, t$1 - 16 | 0), 1)); } var originalA = a; var originalB = b; @@ -56,10 +56,10 @@ function make(message) { var originalD = d; var originalE = e; for(var t$2 = 0; t$2 <= 79; ++t$2){ - var temp = (((ReCrypt_Functions.Sha1.rotl(a, 5) + ReCrypt_Functions.Sha1.f(t$2, b, c, d) | 0) + e | 0) + constants(t$2) | 0) + Caml_array.caml_array_get(w, t$2) | 0; + var temp = (((ReCrypt_Functions_SHA1.rotl(a, 5) + ReCrypt_Functions_SHA1.f(t$2, b, c, d) | 0) + e | 0) + constants(t$2) | 0) + Caml_array.caml_array_get(w, t$2) | 0; e = d; d = c; - c = ReCrypt_Functions.Sha1.rotl(b, 30); + c = ReCrypt_Functions_SHA1.rotl(b, 30); b = a; a = temp; } diff --git a/lib/js/src/ReCrypt/ReCrypt_Sha224.bs.js b/lib/js/src/ReCrypt/ReCrypt_Sha224.bs.js index b321dcc..4c01190 100644 --- a/lib/js/src/ReCrypt/ReCrypt_Sha224.bs.js +++ b/lib/js/src/ReCrypt/ReCrypt_Sha224.bs.js @@ -6,8 +6,8 @@ var Bytes = require("bs-platform/lib/js/bytes.js"); var Int32 = require("bs-platform/lib/js/int32.js"); var Caml_array = require("bs-platform/lib/js/caml_array.js"); var ReCrypt_Utils = require("./ReCrypt_Utils.bs.js"); -var ReCrypt_Functions = require("./ReCrypt_Functions.bs.js"); var ReCrypt_Preprocess = require("./ReCrypt_Preprocess.bs.js"); +var ReCrypt_Functions_SHA2 = require("./Functions/ReCrypt_Functions_SHA2.bs.js"); var constants = [ 1116352408, @@ -107,7 +107,7 @@ function make(message) { Caml_array.caml_array_set(w, t, Caml_array.caml_array_get(w, t) | (message$1[((i << 6) + (t << 2) | 0) + 2 | 0] << 8) & 65280 | message$1[((i << 6) + (t << 2) | 0) + 3 | 0] & 255); } for(var t$1 = 16; t$1 <= 63; ++t$1){ - Caml_array.caml_array_set(w, t$1, ((ReCrypt_Functions.Sha224_256.sigma1(Caml_array.caml_array_get(w, t$1 - 2 | 0)) + Caml_array.caml_array_get(w, t$1 - 7 | 0) | 0) + ReCrypt_Functions.Sha224_256.sigma0(Caml_array.caml_array_get(w, t$1 - 15 | 0)) | 0) + Caml_array.caml_array_get(w, t$1 - 16 | 0) | 0); + Caml_array.caml_array_set(w, t$1, ((ReCrypt_Functions_SHA2.Int32.sigma1(Caml_array.caml_array_get(w, t$1 - 2 | 0)) + Caml_array.caml_array_get(w, t$1 - 7 | 0) | 0) + ReCrypt_Functions_SHA2.Int32.sigma0(Caml_array.caml_array_get(w, t$1 - 15 | 0)) | 0) + Caml_array.caml_array_get(w, t$1 - 16 | 0) | 0); } var originalA = a; var originalB = b; @@ -118,8 +118,8 @@ function make(message) { var originalG = g; var originalH = h; for(var t$2 = 0; t$2 <= 63; ++t$2){ - var temp1 = (((h + ReCrypt_Functions.Sha224_256.sum1(e) | 0) + ReCrypt_Functions.Sha224_256.change(e, f, g) | 0) + Caml_array.caml_array_get(constants, t$2) | 0) + Caml_array.caml_array_get(w, t$2) | 0; - var temp2 = ReCrypt_Functions.Sha224_256.sum0(a) + ReCrypt_Functions.Sha224_256.majority(a, b, c) | 0; + var temp1 = (((h + ReCrypt_Functions_SHA2.Int32.sum1(e) | 0) + ReCrypt_Functions_SHA2.Int32.change(e, f, g) | 0) + Caml_array.caml_array_get(constants, t$2) | 0) + Caml_array.caml_array_get(w, t$2) | 0; + var temp2 = ReCrypt_Functions_SHA2.Int32.sum0(a) + ReCrypt_Functions_SHA2.Int32.majority(a, b, c) | 0; h = g; g = f; f = e; diff --git a/lib/js/src/ReCrypt/ReCrypt_Sha256.bs.js b/lib/js/src/ReCrypt/ReCrypt_Sha256.bs.js index 754e3c7..1e84f67 100644 --- a/lib/js/src/ReCrypt/ReCrypt_Sha256.bs.js +++ b/lib/js/src/ReCrypt/ReCrypt_Sha256.bs.js @@ -6,8 +6,8 @@ var Bytes = require("bs-platform/lib/js/bytes.js"); var Int32 = require("bs-platform/lib/js/int32.js"); var Caml_array = require("bs-platform/lib/js/caml_array.js"); var ReCrypt_Utils = require("./ReCrypt_Utils.bs.js"); -var ReCrypt_Functions = require("./ReCrypt_Functions.bs.js"); var ReCrypt_Preprocess = require("./ReCrypt_Preprocess.bs.js"); +var ReCrypt_Functions_SHA2 = require("./Functions/ReCrypt_Functions_SHA2.bs.js"); var constants = [ 1116352408, @@ -107,7 +107,7 @@ function make(message) { Caml_array.caml_array_set(w, t, Caml_array.caml_array_get(w, t) | (message$1[((i << 6) + (t << 2) | 0) + 2 | 0] << 8) & 65280 | message$1[((i << 6) + (t << 2) | 0) + 3 | 0] & 255); } for(var t$1 = 16; t$1 <= 63; ++t$1){ - Caml_array.caml_array_set(w, t$1, ((ReCrypt_Functions.Sha224_256.sigma1(Caml_array.caml_array_get(w, t$1 - 2 | 0)) + Caml_array.caml_array_get(w, t$1 - 7 | 0) | 0) + ReCrypt_Functions.Sha224_256.sigma0(Caml_array.caml_array_get(w, t$1 - 15 | 0)) | 0) + Caml_array.caml_array_get(w, t$1 - 16 | 0) | 0); + Caml_array.caml_array_set(w, t$1, ((ReCrypt_Functions_SHA2.Int32.sigma1(Caml_array.caml_array_get(w, t$1 - 2 | 0)) + Caml_array.caml_array_get(w, t$1 - 7 | 0) | 0) + ReCrypt_Functions_SHA2.Int32.sigma0(Caml_array.caml_array_get(w, t$1 - 15 | 0)) | 0) + Caml_array.caml_array_get(w, t$1 - 16 | 0) | 0); } var originalA = a; var originalB = b; @@ -118,8 +118,8 @@ function make(message) { var originalG = g; var originalH = h; for(var t$2 = 0; t$2 <= 63; ++t$2){ - var temp1 = (((h + ReCrypt_Functions.Sha224_256.sum1(e) | 0) + ReCrypt_Functions.Sha224_256.change(e, f, g) | 0) + Caml_array.caml_array_get(constants, t$2) | 0) + Caml_array.caml_array_get(w, t$2) | 0; - var temp2 = ReCrypt_Functions.Sha224_256.sum0(a) + ReCrypt_Functions.Sha224_256.majority(a, b, c) | 0; + var temp1 = (((h + ReCrypt_Functions_SHA2.Int32.sum1(e) | 0) + ReCrypt_Functions_SHA2.Int32.change(e, f, g) | 0) + Caml_array.caml_array_get(constants, t$2) | 0) + Caml_array.caml_array_get(w, t$2) | 0; + var temp2 = ReCrypt_Functions_SHA2.Int32.sum0(a) + ReCrypt_Functions_SHA2.Int32.majority(a, b, c) | 0; h = g; g = f; f = e; diff --git a/lib/js/src/ReCrypt/ReCrypt_Sha384.bs.js b/lib/js/src/ReCrypt/ReCrypt_Sha384.bs.js index 9fd49ff..1616bdd 100644 --- a/lib/js/src/ReCrypt/ReCrypt_Sha384.bs.js +++ b/lib/js/src/ReCrypt/ReCrypt_Sha384.bs.js @@ -8,8 +8,8 @@ var Caml_array = require("bs-platform/lib/js/caml_array.js"); var Caml_bytes = require("bs-platform/lib/js/caml_bytes.js"); var Caml_int64 = require("bs-platform/lib/js/caml_int64.js"); var ReCrypt_Utils = require("./ReCrypt_Utils.bs.js"); -var ReCrypt_Functions = require("./ReCrypt_Functions.bs.js"); var ReCrypt_Preprocess = require("./ReCrypt_Preprocess.bs.js"); +var ReCrypt_Functions_SHA2 = require("./Functions/ReCrypt_Functions_SHA2.bs.js"); var constants = [ /* int64 */[ @@ -402,7 +402,7 @@ function make(message) { } } for(var t$1 = 16; t$1 <= 79; ++t$1){ - Caml_array.caml_array_set(w, t$1, Caml_int64.add(Caml_int64.add(Caml_int64.add(ReCrypt_Functions.Sha384_512.sigma1(Caml_array.caml_array_get(w, t$1 - 2 | 0)), Caml_array.caml_array_get(w, t$1 - 7 | 0)), ReCrypt_Functions.Sha384_512.sigma0(Caml_array.caml_array_get(w, t$1 - 15 | 0))), Caml_array.caml_array_get(w, t$1 - 16 | 0))); + Caml_array.caml_array_set(w, t$1, Caml_int64.add(Caml_int64.add(Caml_int64.add(ReCrypt_Functions_SHA2.Int64.sigma1(Caml_array.caml_array_get(w, t$1 - 2 | 0)), Caml_array.caml_array_get(w, t$1 - 7 | 0)), ReCrypt_Functions_SHA2.Int64.sigma0(Caml_array.caml_array_get(w, t$1 - 15 | 0))), Caml_array.caml_array_get(w, t$1 - 16 | 0))); } var originalA = a; var originalB = b; @@ -413,8 +413,8 @@ function make(message) { var originalG = g; var originalH = h; for(var t$2 = 0; t$2 <= 79; ++t$2){ - var temp1 = Caml_int64.add(Caml_int64.add(Caml_int64.add(Caml_int64.add(h, ReCrypt_Functions.Sha384_512.sum1(e)), ReCrypt_Functions.Sha384_512.change(e, f, g)), Caml_array.caml_array_get(constants, t$2)), Caml_array.caml_array_get(w, t$2)); - var temp2 = Caml_int64.add(ReCrypt_Functions.Sha384_512.sum0(a), ReCrypt_Functions.Sha384_512.majority(a, b, c)); + var temp1 = Caml_int64.add(Caml_int64.add(Caml_int64.add(Caml_int64.add(h, ReCrypt_Functions_SHA2.Int64.sum1(e)), ReCrypt_Functions_SHA2.Int64.change(e, f, g)), Caml_array.caml_array_get(constants, t$2)), Caml_array.caml_array_get(w, t$2)); + var temp2 = Caml_int64.add(ReCrypt_Functions_SHA2.Int64.sum0(a), ReCrypt_Functions_SHA2.Int64.majority(a, b, c)); h = g; g = f; f = e; diff --git a/lib/js/src/ReCrypt/ReCrypt_Sha512.bs.js b/lib/js/src/ReCrypt/ReCrypt_Sha512.bs.js index 808dd64..1369938 100644 --- a/lib/js/src/ReCrypt/ReCrypt_Sha512.bs.js +++ b/lib/js/src/ReCrypt/ReCrypt_Sha512.bs.js @@ -8,8 +8,8 @@ var Caml_array = require("bs-platform/lib/js/caml_array.js"); var Caml_bytes = require("bs-platform/lib/js/caml_bytes.js"); var Caml_int64 = require("bs-platform/lib/js/caml_int64.js"); var ReCrypt_Utils = require("./ReCrypt_Utils.bs.js"); -var ReCrypt_Functions = require("./ReCrypt_Functions.bs.js"); var ReCrypt_Preprocess = require("./ReCrypt_Preprocess.bs.js"); +var ReCrypt_Functions_SHA2 = require("./Functions/ReCrypt_Functions_SHA2.bs.js"); var constants = [ /* int64 */[ @@ -402,7 +402,7 @@ function make(message) { } } for(var t$1 = 16; t$1 <= 79; ++t$1){ - Caml_array.caml_array_set(w, t$1, Caml_int64.add(Caml_int64.add(Caml_int64.add(ReCrypt_Functions.Sha384_512.sigma1(Caml_array.caml_array_get(w, t$1 - 2 | 0)), Caml_array.caml_array_get(w, t$1 - 7 | 0)), ReCrypt_Functions.Sha384_512.sigma0(Caml_array.caml_array_get(w, t$1 - 15 | 0))), Caml_array.caml_array_get(w, t$1 - 16 | 0))); + Caml_array.caml_array_set(w, t$1, Caml_int64.add(Caml_int64.add(Caml_int64.add(ReCrypt_Functions_SHA2.Int64.sigma1(Caml_array.caml_array_get(w, t$1 - 2 | 0)), Caml_array.caml_array_get(w, t$1 - 7 | 0)), ReCrypt_Functions_SHA2.Int64.sigma0(Caml_array.caml_array_get(w, t$1 - 15 | 0))), Caml_array.caml_array_get(w, t$1 - 16 | 0))); } var originalA = a; var originalB = b; @@ -413,8 +413,8 @@ function make(message) { var originalG = g; var originalH = h; for(var t$2 = 0; t$2 <= 79; ++t$2){ - var temp1 = Caml_int64.add(Caml_int64.add(Caml_int64.add(Caml_int64.add(h, ReCrypt_Functions.Sha384_512.sum1(e)), ReCrypt_Functions.Sha384_512.change(e, f, g)), Caml_array.caml_array_get(constants, t$2)), Caml_array.caml_array_get(w, t$2)); - var temp2 = Caml_int64.add(ReCrypt_Functions.Sha384_512.sum0(a), ReCrypt_Functions.Sha384_512.majority(a, b, c)); + var temp1 = Caml_int64.add(Caml_int64.add(Caml_int64.add(Caml_int64.add(h, ReCrypt_Functions_SHA2.Int64.sum1(e)), ReCrypt_Functions_SHA2.Int64.change(e, f, g)), Caml_array.caml_array_get(constants, t$2)), Caml_array.caml_array_get(w, t$2)); + var temp2 = Caml_int64.add(ReCrypt_Functions_SHA2.Int64.sum0(a), ReCrypt_Functions_SHA2.Int64.majority(a, b, c)); h = g; g = f; f = e; diff --git a/src/ReCrypt/Functions/ReCrypt_Functions_MD.re b/src/ReCrypt/Functions/ReCrypt_Functions_MD.re new file mode 100644 index 0000000..525f72c --- /dev/null +++ b/src/ReCrypt/Functions/ReCrypt_Functions_MD.re @@ -0,0 +1,3 @@ +let rotl = (x, n) => { + x lsl n lor x lsr (64 - n); +}; \ No newline at end of file diff --git a/src/ReCrypt/Functions/ReCrypt_Functions_SHA1.re b/src/ReCrypt/Functions/ReCrypt_Functions_SHA1.re new file mode 100644 index 0000000..1b32800 --- /dev/null +++ b/src/ReCrypt/Functions/ReCrypt_Functions_SHA1.re @@ -0,0 +1,27 @@ +let rotl = (x, n) => { + Int32.logor(Int32.shift_left(x, n), Int32.shift_right_logical(x, 64 - n)); +}; + +let change = (x, y, z) => { + Int32.logand(x, y)->Int32.logxor(Int32.logand(Int32.lognot(x), z)); +}; + +let majority = (x, y, z) => { + Int32.logand(x, y) + ->Int32.logxor(Int32.logand(x, z)) + ->Int32.logxor(Int32.logand(y, z)); +}; + +let parity = (x, y, z) => { + x->Int32.logxor(y)->Int32.logxor(z); +}; + +let f = (t, x, y, z) => { + switch (t) { + | t when t >= 0 && t <= 19 => change(x, y, z) + | t when t >= 20 && t <= 39 => parity(x, y, z) + | t when t >= 40 && t <= 59 => majority(x, y, z) + | t when t >= 60 && t <= 79 => parity(x, y, z) + | _ => 0x00l + }; +}; \ No newline at end of file diff --git a/src/ReCrypt/Functions/ReCrypt_Functions_SHA2.re b/src/ReCrypt/Functions/ReCrypt_Functions_SHA2.re new file mode 100644 index 0000000..bfc7739 --- /dev/null +++ b/src/ReCrypt/Functions/ReCrypt_Functions_SHA2.re @@ -0,0 +1,77 @@ +module Int32 = { + let rotr = (x, n) => { + Int32.logor( + Int32.shift_right_logical(x, n), + Int32.shift_left(x, 64 - n), + ); + }; + + let change = (x, y, z) => { + Int32.logand(x, y)->Int32.logxor(Int32.logand(Int32.lognot(x), z)); + }; + + let majority = (x, y, z) => { + Int32.logand(x, y) + ->Int32.logxor(Int32.logand(x, z)) + ->Int32.logxor(Int32.logand(y, z)); + }; + + let sum0 = x => { + rotr(x, 2)->Int32.logxor(rotr(x, 13))->Int32.logxor(rotr(x, 22)); + }; + + let sum1 = x => { + rotr(x, 6)->Int32.logxor(rotr(x, 11))->Int32.logxor(rotr(x, 25)); + }; + + let sigma0 = x => { + rotr(x, 7) + ->Int32.logxor(rotr(x, 18)) + ->Int32.logxor(Int32.shift_right_logical(x, 3)); + }; + + let sigma1 = x => { + rotr(x, 17) + ->Int32.logxor(rotr(x, 19)) + ->Int32.logxor(Int32.shift_right_logical(x, 10)); + }; +}; + +module Int64 = { + let rotr = (x, n) => { + Int64.logor( + Int64.shift_right_logical(x, n), + Int64.shift_left(x, 64 - n), + ); + }; + + let change = (x, y, z) => { + Int64.logand(x, y)->Int64.logxor(Int64.logand(Int64.lognot(x), z)); + }; + + let majority = (x, y, z) => { + Int64.logand(x, y) + ->Int64.logxor(Int64.logand(x, z)) + ->Int64.logxor(Int64.logand(y, z)); + }; + + let sum0 = x => { + rotr(x, 28)->Int64.logxor(rotr(x, 34))->Int64.logxor(rotr(x, 39)); + }; + + let sum1 = x => { + rotr(x, 14)->Int64.logxor(rotr(x, 18))->Int64.logxor(rotr(x, 41)); + }; + + let sigma0 = x => { + rotr(x, 1) + ->Int64.logxor(rotr(x, 8)) + ->Int64.logxor(Int64.shift_right_logical(x, 7)); + }; + + let sigma1 = x => { + rotr(x, 19) + ->Int64.logxor(rotr(x, 61)) + ->Int64.logxor(Int64.shift_right_logical(x, 6)); + }; +}; \ No newline at end of file diff --git a/src/ReCrypt/ReCrypt_Functions.re b/src/ReCrypt/ReCrypt_Functions.re index 4f17c7a..775a854 100644 --- a/src/ReCrypt/ReCrypt_Functions.re +++ b/src/ReCrypt/ReCrypt_Functions.re @@ -1,119 +1,3 @@ -/** - * Logical functions [ยง4.1] - */ -module MD5 = { - let rotl = (x, n) => { - x lsl n lor x lsr (64 - n); - }; -}; - -module Sha1 = { - let rotl = (x, n) => { - Int32.logor( - Int32.shift_left(x, n), - Int32.shift_right_logical(x, 64 - n), - ); - }; - - let change = (x, y, z) => { - Int32.logand(x, y)->Int32.logxor(Int32.logand(Int32.lognot(x), z)); - }; - - let majority = (x, y, z) => { - Int32.logand(x, y) - ->Int32.logxor(Int32.logand(x, z)) - ->Int32.logxor(Int32.logand(y, z)); - }; - - let parity = (x, y, z) => { - x->Int32.logxor(y)->Int32.logxor(z); - }; - - let f = (t, x, y, z) => { - switch (t) { - | t when t >= 0 && t <= 19 => change(x, y, z) - | t when t >= 20 && t <= 39 => parity(x, y, z) - | t when t >= 40 && t <= 59 => majority(x, y, z) - | t when t >= 60 && t <= 79 => parity(x, y, z) - | _ => 0x00l - }; - }; -}; - -module Sha224_256 = { - let rotr = (x, n) => { - Int32.logor( - Int32.shift_right_logical(x, n), - Int32.shift_left(x, 64 - n), - ); - }; - - let change = (x, y, z) => { - Int32.logand(x, y)->Int32.logxor(Int32.logand(Int32.lognot(x), z)); - }; - - let majority = (x, y, z) => { - Int32.logand(x, y) - ->Int32.logxor(Int32.logand(x, z)) - ->Int32.logxor(Int32.logand(y, z)); - }; - - let sum0 = x => { - rotr(x, 2)->Int32.logxor(rotr(x, 13))->Int32.logxor(rotr(x, 22)); - }; - - let sum1 = x => { - rotr(x, 6)->Int32.logxor(rotr(x, 11))->Int32.logxor(rotr(x, 25)); - }; - - let sigma0 = x => { - rotr(x, 7) - ->Int32.logxor(rotr(x, 18)) - ->Int32.logxor(Int32.shift_right_logical(x, 3)); - }; - - let sigma1 = x => { - rotr(x, 17) - ->Int32.logxor(rotr(x, 19)) - ->Int32.logxor(Int32.shift_right_logical(x, 10)); - }; -}; - -module Sha384_512 = { - let rotr = (x, n) => { - Int64.logor( - Int64.shift_right_logical(x, n), - Int64.shift_left(x, 64 - n), - ); - }; - - let change = (x, y, z) => { - Int64.logand(x, y)->Int64.logxor(Int64.logand(Int64.lognot(x), z)); - }; - - let majority = (x, y, z) => { - Int64.logand(x, y) - ->Int64.logxor(Int64.logand(x, z)) - ->Int64.logxor(Int64.logand(y, z)); - }; - - let sum0 = x => { - rotr(x, 28)->Int64.logxor(rotr(x, 34))->Int64.logxor(rotr(x, 39)); - }; - - let sum1 = x => { - rotr(x, 14)->Int64.logxor(rotr(x, 18))->Int64.logxor(rotr(x, 41)); - }; - - let sigma0 = x => { - rotr(x, 1) - ->Int64.logxor(rotr(x, 8)) - ->Int64.logxor(Int64.shift_right_logical(x, 7)); - }; - - let sigma1 = x => { - rotr(x, 19) - ->Int64.logxor(rotr(x, 61)) - ->Int64.logxor(Int64.shift_right_logical(x, 6)); - }; -}; \ No newline at end of file +module MD = ReCrypt_Functions_MD; +module SHA1 = ReCrypt_Functions_SHA1; +module SHA2 = ReCrypt_Functions_SHA2; \ No newline at end of file diff --git a/src/ReCrypt/ReCrypt_Functions.rei b/src/ReCrypt/ReCrypt_Functions.rei deleted file mode 100644 index e417c14..0000000 --- a/src/ReCrypt/ReCrypt_Functions.rei +++ /dev/null @@ -1,24 +0,0 @@ -module MD5: {let rotl: (int, int) => int;}; - -module Sha1: { - let f: (int, int32, int32, int32) => int32; - let rotl: (int32, int) => int32; -}; - -module Sha224_256: { - let change: (int32, int32, int32) => int32; - let majority: (int32, int32, int32) => int32; - let sum0: int32 => int32; - let sum1: int32 => int32; - let sigma0: int32 => int32; - let sigma1: int32 => int32; -}; - -module Sha384_512: { - let change: (int64, int64, int64) => int64; - let majority: (int64, int64, int64) => int64; - let sum0: int64 => int64; - let sum1: int64 => int64; - let sigma0: int64 => int64; - let sigma1: int64 => int64; -}; \ No newline at end of file diff --git a/src/ReCrypt/ReCrypt_MD5.re b/src/ReCrypt/ReCrypt_MD5.re index 3bde5fa..58d56c4 100644 --- a/src/ReCrypt/ReCrypt_MD5.re +++ b/src/ReCrypt/ReCrypt_MD5.re @@ -141,7 +141,7 @@ let processMessage = (message: Bytes.t) => { let temp = b^ - + ReCrypt_Functions.MD5.rotl( + + ReCrypt_Functions.MD.rotl( a^ + f + w[g] + constants[j], s[(j lsr 4) lsl 2 lor (j land 3)], ); diff --git a/src/ReCrypt/ReCrypt_Sha1.re b/src/ReCrypt/ReCrypt_Sha1.re index 65c3377..c189b3d 100644 --- a/src/ReCrypt/ReCrypt_Sha1.re +++ b/src/ReCrypt/ReCrypt_Sha1.re @@ -65,7 +65,7 @@ let processMessage = (message: Bytes.t) => { for (t in 16 to 79) { w[t] = - ReCrypt_Functions.Sha1.rotl( + ReCrypt_Functions.SHA1.rotl( w[t - 3] ->Int32.logxor(w[t - 8]) ->Int32.logxor(w[t - 14]) @@ -82,15 +82,15 @@ let processMessage = (message: Bytes.t) => { for (t in 0 to 79) { let temp = - ReCrypt_Functions.Sha1.rotl(a^, 5) - ->Int32.add(ReCrypt_Functions.Sha1.f(t, b^, c^, d^)) + ReCrypt_Functions.SHA1.rotl(a^, 5) + ->Int32.add(ReCrypt_Functions.SHA1.f(t, b^, c^, d^)) ->Int32.add(e^) ->Int32.add(constants(t)) ->Int32.add(w[t]); e := d^; d := c^; - c := ReCrypt_Functions.Sha1.rotl(b^, 30); + c := ReCrypt_Functions.SHA1.rotl(b^, 30); b := a^; a := temp; }; diff --git a/src/ReCrypt/ReCrypt_Sha224.re b/src/ReCrypt/ReCrypt_Sha224.re index 8bc8221..e9494ad 100644 --- a/src/ReCrypt/ReCrypt_Sha224.re +++ b/src/ReCrypt/ReCrypt_Sha224.re @@ -128,9 +128,9 @@ let processMessage = (message: Bytes.t) => { for (t in 16 to 63) { w[t] = - ReCrypt_Functions.Sha224_256.sigma1(w[t - 2]) + ReCrypt_Functions.SHA2.Int32.sigma1(w[t - 2]) ->Int32.add(w[t - 7]) - ->Int32.add(ReCrypt_Functions.Sha224_256.sigma0(w[t - 15])) + ->Int32.add(ReCrypt_Functions.SHA2.Int32.sigma0(w[t - 15])) ->Int32.add(w[t - 16]); }; @@ -146,15 +146,15 @@ let processMessage = (message: Bytes.t) => { for (t in 0 to 63) { let temp1 = (h^) - ->Int32.add(ReCrypt_Functions.Sha224_256.sum1(e^)) - ->Int32.add(ReCrypt_Functions.Sha224_256.change(e^, f^, g^)) + ->Int32.add(ReCrypt_Functions.SHA2.Int32.sum1(e^)) + ->Int32.add(ReCrypt_Functions.SHA2.Int32.change(e^, f^, g^)) ->Int32.add(constants[t]) ->Int32.add(w[t]); let temp2 = Int32.add( - ReCrypt_Functions.Sha224_256.sum0(a^), - ReCrypt_Functions.Sha224_256.majority(a^, b^, c^), + ReCrypt_Functions.SHA2.Int32.sum0(a^), + ReCrypt_Functions.SHA2.Int32.majority(a^, b^, c^), ); h := g^; diff --git a/src/ReCrypt/ReCrypt_Sha256.re b/src/ReCrypt/ReCrypt_Sha256.re index a4b532b..dd97336 100644 --- a/src/ReCrypt/ReCrypt_Sha256.re +++ b/src/ReCrypt/ReCrypt_Sha256.re @@ -127,9 +127,9 @@ let processMessage = (message: Bytes.t) => { for (t in 16 to 63) { w[t] = - ReCrypt_Functions.Sha224_256.sigma1(w[t - 2]) + ReCrypt_Functions.SHA2.Int32.sigma1(w[t - 2]) ->Int32.add(w[t - 7]) - ->Int32.add(ReCrypt_Functions.Sha224_256.sigma0(w[t - 15])) + ->Int32.add(ReCrypt_Functions.SHA2.Int32.sigma0(w[t - 15])) ->Int32.add(w[t - 16]); }; @@ -145,15 +145,15 @@ let processMessage = (message: Bytes.t) => { for (t in 0 to 63) { let temp1 = (h^) - ->Int32.add(ReCrypt_Functions.Sha224_256.sum1(e^)) - ->Int32.add(ReCrypt_Functions.Sha224_256.change(e^, f^, g^)) + ->Int32.add(ReCrypt_Functions.SHA2.Int32.sum1(e^)) + ->Int32.add(ReCrypt_Functions.SHA2.Int32.change(e^, f^, g^)) ->Int32.add(constants[t]) ->Int32.add(w[t]); let temp2 = Int32.add( - ReCrypt_Functions.Sha224_256.sum0(a^), - ReCrypt_Functions.Sha224_256.majority(a^, b^, c^), + ReCrypt_Functions.SHA2.Int32.sum0(a^), + ReCrypt_Functions.SHA2.Int32.majority(a^, b^, c^), ); h := g^; diff --git a/src/ReCrypt/ReCrypt_Sha384.re b/src/ReCrypt/ReCrypt_Sha384.re index e0dd31a..2e51c24 100644 --- a/src/ReCrypt/ReCrypt_Sha384.re +++ b/src/ReCrypt/ReCrypt_Sha384.re @@ -123,9 +123,9 @@ let processMessage = (message: Bytes.t) => { for (t in 16 to 79) { w[t] = - ReCrypt_Functions.Sha384_512.sigma1(w[t - 2]) + ReCrypt_Functions.SHA2.Int64.sigma1(w[t - 2]) ->Int64.add(w[t - 7]) - ->Int64.add(ReCrypt_Functions.Sha384_512.sigma0(w[t - 15])) + ->Int64.add(ReCrypt_Functions.SHA2.Int64.sigma0(w[t - 15])) ->Int64.add(w[t - 16]); }; @@ -141,15 +141,15 @@ let processMessage = (message: Bytes.t) => { for (t in 0 to 79) { let temp1 = (h^) - ->Int64.add(ReCrypt_Functions.Sha384_512.sum1(e^)) - ->Int64.add(ReCrypt_Functions.Sha384_512.change(e^, f^, g^)) + ->Int64.add(ReCrypt_Functions.SHA2.Int64.sum1(e^)) + ->Int64.add(ReCrypt_Functions.SHA2.Int64.change(e^, f^, g^)) ->Int64.add(constants[t]) ->Int64.add(w[t]); let temp2 = Int64.add( - ReCrypt_Functions.Sha384_512.sum0(a^), - ReCrypt_Functions.Sha384_512.majority(a^, b^, c^), + ReCrypt_Functions.SHA2.Int64.sum0(a^), + ReCrypt_Functions.SHA2.Int64.majority(a^, b^, c^), ); h := g^; diff --git a/src/ReCrypt/ReCrypt_Sha512.re b/src/ReCrypt/ReCrypt_Sha512.re index 4e35fb6..1e82c81 100644 --- a/src/ReCrypt/ReCrypt_Sha512.re +++ b/src/ReCrypt/ReCrypt_Sha512.re @@ -123,9 +123,9 @@ let processMessage = (message: Bytes.t) => { for (t in 16 to 79) { w[t] = - ReCrypt_Functions.Sha384_512.sigma1(w[t - 2]) + ReCrypt_Functions.SHA2.Int64.sigma1(w[t - 2]) ->Int64.add(w[t - 7]) - ->Int64.add(ReCrypt_Functions.Sha384_512.sigma0(w[t - 15])) + ->Int64.add(ReCrypt_Functions.SHA2.Int64.sigma0(w[t - 15])) ->Int64.add(w[t - 16]); }; @@ -141,15 +141,15 @@ let processMessage = (message: Bytes.t) => { for (t in 0 to 79) { let temp1 = (h^) - ->Int64.add(ReCrypt_Functions.Sha384_512.sum1(e^)) - ->Int64.add(ReCrypt_Functions.Sha384_512.change(e^, f^, g^)) + ->Int64.add(ReCrypt_Functions.SHA2.Int64.sum1(e^)) + ->Int64.add(ReCrypt_Functions.SHA2.Int64.change(e^, f^, g^)) ->Int64.add(constants[t]) ->Int64.add(w[t]); let temp2 = Int64.add( - ReCrypt_Functions.Sha384_512.sum0(a^), - ReCrypt_Functions.Sha384_512.majority(a^, b^, c^), + ReCrypt_Functions.SHA2.Int64.sum0(a^), + ReCrypt_Functions.SHA2.Int64.majority(a^, b^, c^), ); h := g^; -- 2.51.2