From 70f3abe603e5d2ff941c34cd4ab5abea61d02f9e Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Tue, 8 Dec 2020 18:42:28 +0700 Subject: [PATCH 1/6] Using Chinese Remainder Theorem --- src/rsa/rsa_internal.ts | 67 +++++++++++++++++++++++++++++------------ src/rsa/rsa_js.ts | 26 ++++------------ 2 files changed, 53 insertions(+), 40 deletions(-) diff --git a/src/rsa/rsa_internal.ts b/src/rsa/rsa_internal.ts index 093bfb1..6c1e1f3 100644 --- a/src/rsa/rsa_internal.ts +++ b/src/rsa/rsa_internal.ts @@ -4,6 +4,7 @@ import { i2osp, os2ip } from "./primitives.ts"; import { concat, random_bytes } from "./../helper.ts"; import { ber_decode, ber_simple } from "./basic_encoding_rule.ts"; import { RawBinary } from "../binary.ts"; +import { RSAKey } from "./rsa_key.ts"; /** * @param n public key modulus @@ -19,8 +20,25 @@ export function rsaep(n: bigint, e: bigint, m: bigint): bigint { * @param d private key exponent * @param c ciphertext representative */ -export function rsadp(n: bigint, d: bigint, c: bigint): bigint { - return power_mod(c, d, n); +export function rsadp(key: RSAKey, c: bigint): bigint { + if (!key.d) throw "Invalid RSA key"; + + if (key.dp && key.dq && key.qi && key.q && key.p) { + // Using the Chinese remainder algorithm + const m1 = power_mod(c, key.dp, key.p); + const m2 = power_mod(c, key.dq, key.q); + + let h = 0n; + if (m1 >= m2) { + h = (key.qi * (m1 - m2)) % key.p; + } else { + h = (key.qi * (m1 - m2 + key.p * (key.p / key.q))) % key.p; + } + + return (m2 + h * key.q) % (key.q * key.p); + } else { + return power_mod(c, key.d, key.n); + } } export function rsa_oaep_encrypt( @@ -37,17 +55,15 @@ export function rsa_oaep_encrypt( } export function rsa_oaep_decrypt( - bytes: number, - n: bigint, - d: bigint, + key: RSAKey, c: Uint8Array, algorithm: "sha1" | "sha256", ) { - const em = rsadp(n, d, os2ip(c)); + const em = rsadp(key, os2ip(c)); const m = eme_oaep_decode( new Uint8Array(0), - i2osp(em, bytes), - bytes, + i2osp(em, key.length), + key.length, algorithm, ); return m; @@ -65,13 +81,8 @@ export function rsa_pkcs1_encrypt( return i2osp(c, bytes); } -export function rsa_pkcs1_decrypt( - bytes: number, - n: bigint, - d: bigint, - c: Uint8Array, -) { - const em = i2osp(rsadp(n, d, os2ip(c)), bytes); +export function rsa_pkcs1_decrypt(key: RSAKey, c: Uint8Array) { + const em = i2osp(rsadp(key, os2ip(c)), key.length); if (em[0] !== 0) throw "Decryption error"; if (em[1] !== 0x02) throw "Decryption error"; @@ -87,13 +98,13 @@ export function rsa_pkcs1_decrypt( } export function rsa_pkcs1_verify( - bytes: number, - n: bigint, - d: bigint, + key: RSAKey, s: Uint8Array, m: Uint8Array, ): boolean { - let em = i2osp(rsadp(n, d, os2ip(s)), bytes); + if (!key.e) throw "Invalid RSA key"; + + let em = i2osp(rsaep(key.n, key.e, os2ip(s)), key.length); if (em[0] !== 0) throw "Decryption error"; if (em[1] !== 0x01) throw "Decryption error"; @@ -128,7 +139,23 @@ export function rsa_pkcs1_sign( message: Uint8Array, ): RawBinary { // deno-fmt-ignore - const oid = [0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00] + const oid = [ + 0x30, + 0x0d, + 0x06, + 0x09, + 0x60, + 0x86, + 0x48, + 0x01, + 0x65, + 0x03, + 0x04, + 0x02, + 0x01, + 0x05, + 0x00, + ]; const der = [ 0x30, message.length + 2 + oid.length, diff --git a/src/rsa/rsa_js.ts b/src/rsa/rsa_js.ts index 609b28e..2aa7848 100644 --- a/src/rsa/rsa_js.ts +++ b/src/rsa/rsa_js.ts @@ -16,13 +16,9 @@ export class PureRSA { if (!key.e) throw "Invalid RSA key"; if (options.padding === "oaep") { - return new RawBinary(rsa_oaep_encrypt( - key.length, - key.n, - key.e, - message, - options.hash, - )); + return new RawBinary( + rsa_oaep_encrypt(key.length, key.n, key.e, message, options.hash), + ); } else if (options.padding === "pkcs1") { return new RawBinary( rsa_pkcs1_encrypt(key.length, key.n, key.e, message), @@ -36,17 +32,9 @@ export class PureRSA { if (!key.d) throw "Invalid RSA key"; if (options.padding === "oaep") { - return new RawBinary(rsa_oaep_decrypt( - key.length, - key.n, - key.d, - ciper, - options.hash, - )); + return new RawBinary(rsa_oaep_decrypt(key, ciper, options.hash)); } else if (options.padding === "pkcs1") { - return new RawBinary( - rsa_pkcs1_decrypt(key.length, key.n, key.d, ciper), - ); + return new RawBinary(rsa_pkcs1_decrypt(key, ciper)); } throw "Invalid parameters"; @@ -61,9 +49,7 @@ export class PureRSA { if (!key.e) throw "Invalid RSA key"; return rsa_pkcs1_verify( - key.length, - key.n, - key.e, + key, signature, createHash(options.hash).update(message).digest(), ); From e222f6f4c8ea4bda191f97ae1615eb4b7d3aaa94 Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Tue, 8 Dec 2020 19:12:53 +0700 Subject: [PATCH 2/6] Add benchmark --- benchmark/rsa_decryption.ts | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 benchmark/rsa_decryption.ts diff --git a/benchmark/rsa_decryption.ts b/benchmark/rsa_decryption.ts new file mode 100644 index 0000000..28acb61 --- /dev/null +++ b/benchmark/rsa_decryption.ts @@ -0,0 +1,58 @@ +import { RSAKey } from "../src/rsa/rsa_key.ts"; +import { RSA } from "./../mod.ts"; + +async function benchmark( + callback: () => Promise, + time: number, +): Promise { + const begin = Date.now(); + for (let i = 0; i < time; i++) await callback(); + const duration = Date.now() - begin; + return ( + "Average time per operation: " + + (duration / time).toLocaleString(undefined, { + maximumFractionDigits: 3, + minimumFractionDigits: 3, + }) + + "ms" + ); +} + +const RSAWithoutCRT = new RSA( + new RSAKey({ + n: 18860626341786571281488823024986320858450594409825894653370543036132043241142573896042059032222842653925236802058387508300831766517210547599910735316414515960981281788934868325895562621846910598716519620622800765932541992515497122274302135166053720433521886017494719475895327062855822141735315456613329660043977602681873027347568355824008701177948209784908095092287571982104446342906805480285143028837509076748632094733769028381835899214426100089454654381383340292544225149266603150745375542089871264998183586873913491071542891188124185517417658034185267204900711933432911892469528812455677450346478816563776340692357n, + e: 65537n, + d: 675889233296421535959584534071325460876697008626327098961835826056666939623318583538247287416696388446697983317835135300723690022117970081796395234018582764098060974026823771341271141202058228435371968592820841644512299636165137501280011132877728106486853881053264668544941537620562475421678333757690909726581512579603702598150422421865490996288244127528474781858718770957987603681469839575489191876915053252047160475432784849252000744700939988583801783684487872676506769116621572018367654125765915215495461243190782913460732307866572233187464540482209385816659449697777403383908159076257721991916366849321145825505n, + length: 256, + }), +); + +const RSAWithCRT = new RSA( + new RSAKey({ + n: 18860626341786571281488823024986320858450594409825894653370543036132043241142573896042059032222842653925236802058387508300831766517210547599910735316414515960981281788934868325895562621846910598716519620622800765932541992515497122274302135166053720433521886017494719475895327062855822141735315456613329660043977602681873027347568355824008701177948209784908095092287571982104446342906805480285143028837509076748632094733769028381835899214426100089454654381383340292544225149266603150745375542089871264998183586873913491071542891188124185517417658034185267204900711933432911892469528812455677450346478816563776340692357n, + e: 65537n, + d: 675889233296421535959584534071325460876697008626327098961835826056666939623318583538247287416696388446697983317835135300723690022117970081796395234018582764098060974026823771341271141202058228435371968592820841644512299636165137501280011132877728106486853881053264668544941537620562475421678333757690909726581512579603702598150422421865490996288244127528474781858718770957987603681469839575489191876915053252047160475432784849252000744700939988583801783684487872676506769116621572018367654125765915215495461243190782913460732307866572233187464540482209385816659449697777403383908159076257721991916366849321145825505n, + p: 144012741627856144888308881334281149724189531338704225677876939563376606824918324031374859287124236226502971065990666108124651521772921507380585859955952240309460362784798071545709251190489395954672667468167440559860853869293126718717957973299492588051948353555478537074738121854154013245465025431728566135973n, + q: 130964983574331118238688281723343099404807798188351894743739498160286888632365582428908430826736465978859171109808627043780421532713284022169182243601628532694521039146449170161837408903716062502861857136295244082497345869098293182658740508141543051039696970285876309414793236229439985580214897811694398138209n, + dp: 101745242578283949839304176626026302615765501625718038014206601700771528284898122271399648057012472126577972233967069321732572665229256933255045035440751035763135842618089328298662278539176953652032498422434484837613519933106647440836151945919299110608989008748123460390840662802554879248234151815614020617785n, + dq: 35678301062546466652937128368533312430740473760697540759490440516864704054843143700287335764233224950585953598646920476061700200574682590472688401624478932858201910873563078628399913004363131970125205568021351112332081315087369371243559409682455859030208122243679686715774576798456162206832122091795348953409n, + qi: 290194643186456996857651131997752205887237030546449364256620415456526041634563025067464278896891532150660234750257681796597575941847755684751705687345792123358974007946764700824215929859573834912100325612311985272735079519163425874861676228607347917173236103665132111683625679969874392886475792638645717652n, + length: 256, + }), +); + +const ciper = await RSAWithCRT.encrypt("Hello World"); + +console.log( + "With CRT", + await benchmark(async () => { + RSAWithCRT.decrypt(ciper); + }, 1000), +); + +console.log( + "Without CRT", + await benchmark(async () => { + RSAWithoutCRT.decrypt(ciper); + }, 1000), +); From a2e50477e5af23177cb568f364990749db157a10 Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Tue, 8 Dec 2020 19:13:18 +0700 Subject: [PATCH 3/6] Slightly improve the speed of decrypt --- src/rsa/rsa_internal.ts | 4 ++-- tests/rsa/rsa.import_key.test.ts | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/rsa/rsa_internal.ts b/src/rsa/rsa_internal.ts index 6c1e1f3..193d849 100644 --- a/src/rsa/rsa_internal.ts +++ b/src/rsa/rsa_internal.ts @@ -25,8 +25,8 @@ export function rsadp(key: RSAKey, c: bigint): bigint { if (key.dp && key.dq && key.qi && key.q && key.p) { // Using the Chinese remainder algorithm - const m1 = power_mod(c, key.dp, key.p); - const m2 = power_mod(c, key.dq, key.q); + const m1 = power_mod(c % key.p, key.dp, key.p); + const m2 = power_mod(c % key.q, key.dq, key.q); let h = 0n; if (m1 >= m2) { diff --git a/tests/rsa/rsa.import_key.test.ts b/tests/rsa/rsa.import_key.test.ts index 6c6a6e8..cc9752d 100644 --- a/tests/rsa/rsa.import_key.test.ts +++ b/tests/rsa/rsa.import_key.test.ts @@ -6,7 +6,8 @@ Deno.test("RSA - Import JWK Public Key", () => { e: "AQAB", alg: "RS256", use: "sig", - n: "7NfiTQcshWgrEdKbHC2e1s92kK-YX7jS3JLFIBpT8f_j_b5y3dQdtFFS4vBoVNQkwep_34x_ihYlhA3QkwaTL2XMSiedjLnubFZBUjs7G0dgGIR3F8A06Bf5KT4g2x1dKVb0Lwwqg22XIfqaS88HdU5pDwcVmq4pVMaJQgUK-xFEC_sHdfqTV8Z0uBCr9Nik_7xz68FINDYyLhehnvwph9ui-8_WeDgU_h5xrG8H7oY28y2NCtBwXxIadB-K8pHxK2srM8wTCIivdyZS80P0jZMqyxPkt4fO33-GQWvelVmR0bS4Arb3Y4bXnoAMCEao3DTm0bgeNVz39274ippJSQ", + n: + "7NfiTQcshWgrEdKbHC2e1s92kK-YX7jS3JLFIBpT8f_j_b5y3dQdtFFS4vBoVNQkwep_34x_ihYlhA3QkwaTL2XMSiedjLnubFZBUjs7G0dgGIR3F8A06Bf5KT4g2x1dKVb0Lwwqg22XIfqaS88HdU5pDwcVmq4pVMaJQgUK-xFEC_sHdfqTV8Z0uBCr9Nik_7xz68FINDYyLhehnvwph9ui-8_WeDgU_h5xrG8H7oY28y2NCtBwXxIadB-K8pHxK2srM8wTCIivdyZS80P0jZMqyxPkt4fO33-GQWvelVmR0bS4Arb3Y4bXnoAMCEao3DTm0bgeNVz39274ippJSQ", kty: "RSA", kid: "0a7dc12664590c957ffaebf7b6718297b864ba91", }; From 99c369f1133a9d840dca9a07e35e8bbc79346817 Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Tue, 8 Dec 2020 19:21:58 +0700 Subject: [PATCH 4/6] Fixing fmt check --- benchmark/rsa_decryption.ts | 9 ++++++--- tests/rsa/rsa.import_key.test.ts | 3 +-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/benchmark/rsa_decryption.ts b/benchmark/rsa_decryption.ts index 28acb61..171094a 100644 --- a/benchmark/rsa_decryption.ts +++ b/benchmark/rsa_decryption.ts @@ -34,9 +34,12 @@ const RSAWithCRT = new RSA( d: 675889233296421535959584534071325460876697008626327098961835826056666939623318583538247287416696388446697983317835135300723690022117970081796395234018582764098060974026823771341271141202058228435371968592820841644512299636165137501280011132877728106486853881053264668544941537620562475421678333757690909726581512579603702598150422421865490996288244127528474781858718770957987603681469839575489191876915053252047160475432784849252000744700939988583801783684487872676506769116621572018367654125765915215495461243190782913460732307866572233187464540482209385816659449697777403383908159076257721991916366849321145825505n, p: 144012741627856144888308881334281149724189531338704225677876939563376606824918324031374859287124236226502971065990666108124651521772921507380585859955952240309460362784798071545709251190489395954672667468167440559860853869293126718717957973299492588051948353555478537074738121854154013245465025431728566135973n, q: 130964983574331118238688281723343099404807798188351894743739498160286888632365582428908430826736465978859171109808627043780421532713284022169182243601628532694521039146449170161837408903716062502861857136295244082497345869098293182658740508141543051039696970285876309414793236229439985580214897811694398138209n, - dp: 101745242578283949839304176626026302615765501625718038014206601700771528284898122271399648057012472126577972233967069321732572665229256933255045035440751035763135842618089328298662278539176953652032498422434484837613519933106647440836151945919299110608989008748123460390840662802554879248234151815614020617785n, - dq: 35678301062546466652937128368533312430740473760697540759490440516864704054843143700287335764233224950585953598646920476061700200574682590472688401624478932858201910873563078628399913004363131970125205568021351112332081315087369371243559409682455859030208122243679686715774576798456162206832122091795348953409n, - qi: 290194643186456996857651131997752205887237030546449364256620415456526041634563025067464278896891532150660234750257681796597575941847755684751705687345792123358974007946764700824215929859573834912100325612311985272735079519163425874861676228607347917173236103665132111683625679969874392886475792638645717652n, + dp: + 101745242578283949839304176626026302615765501625718038014206601700771528284898122271399648057012472126577972233967069321732572665229256933255045035440751035763135842618089328298662278539176953652032498422434484837613519933106647440836151945919299110608989008748123460390840662802554879248234151815614020617785n, + dq: + 35678301062546466652937128368533312430740473760697540759490440516864704054843143700287335764233224950585953598646920476061700200574682590472688401624478932858201910873563078628399913004363131970125205568021351112332081315087369371243559409682455859030208122243679686715774576798456162206832122091795348953409n, + qi: + 290194643186456996857651131997752205887237030546449364256620415456526041634563025067464278896891532150660234750257681796597575941847755684751705687345792123358974007946764700824215929859573834912100325612311985272735079519163425874861676228607347917173236103665132111683625679969874392886475792638645717652n, length: 256, }), ); diff --git a/tests/rsa/rsa.import_key.test.ts b/tests/rsa/rsa.import_key.test.ts index cc9752d..6c6a6e8 100644 --- a/tests/rsa/rsa.import_key.test.ts +++ b/tests/rsa/rsa.import_key.test.ts @@ -6,8 +6,7 @@ Deno.test("RSA - Import JWK Public Key", () => { e: "AQAB", alg: "RS256", use: "sig", - n: - "7NfiTQcshWgrEdKbHC2e1s92kK-YX7jS3JLFIBpT8f_j_b5y3dQdtFFS4vBoVNQkwep_34x_ihYlhA3QkwaTL2XMSiedjLnubFZBUjs7G0dgGIR3F8A06Bf5KT4g2x1dKVb0Lwwqg22XIfqaS88HdU5pDwcVmq4pVMaJQgUK-xFEC_sHdfqTV8Z0uBCr9Nik_7xz68FINDYyLhehnvwph9ui-8_WeDgU_h5xrG8H7oY28y2NCtBwXxIadB-K8pHxK2srM8wTCIivdyZS80P0jZMqyxPkt4fO33-GQWvelVmR0bS4Arb3Y4bXnoAMCEao3DTm0bgeNVz39274ippJSQ", + n: "7NfiTQcshWgrEdKbHC2e1s92kK-YX7jS3JLFIBpT8f_j_b5y3dQdtFFS4vBoVNQkwep_34x_ihYlhA3QkwaTL2XMSiedjLnubFZBUjs7G0dgGIR3F8A06Bf5KT4g2x1dKVb0Lwwqg22XIfqaS88HdU5pDwcVmq4pVMaJQgUK-xFEC_sHdfqTV8Z0uBCr9Nik_7xz68FINDYyLhehnvwph9ui-8_WeDgU_h5xrG8H7oY28y2NCtBwXxIadB-K8pHxK2srM8wTCIivdyZS80P0jZMqyxPkt4fO33-GQWvelVmR0bS4Arb3Y4bXnoAMCEao3DTm0bgeNVz39274ippJSQ", kty: "RSA", kid: "0a7dc12664590c957ffaebf7b6718297b864ba91", }; From d388e2f57b73ebfbaa85742d3b6026763136e1f1 Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Tue, 8 Dec 2020 19:43:50 +0700 Subject: [PATCH 5/6] Add auto fmt --- .vscode/settings.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.vscode/settings.json b/.vscode/settings.json index a08982f..dda860a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,4 +2,8 @@ "editor.tabSize": 2, "deno.enable": true, "files.eol": "\n", + "editor.formatOnSave": true, + "[typescript]": { + "editor.defaultFormatter": "denoland.vscode-deno" + } } From 5e7cd38d5ee3513cd4a05946590af1185a1028de Mon Sep 17 00:00:00 2001 From: "Visal .In" Date: Wed, 9 Dec 2020 19:20:20 +0700 Subject: [PATCH 6/6] Add more test case --- tests/rsa/cases/1024bit_1_ciper_oaep_sha1.txt | 2 + .../rsa/cases/1024bit_1_ciper_oaep_sha256.txt | 1 + tests/rsa/cases/1024bit_1_ciper_pkcs1.txt | Bin 0 -> 128 bytes tests/rsa/cases/1024bit_1_private.pem | 15 ++++++ tests/rsa/cases/1024bit_1_public.pub | 6 +++ tests/rsa/cases/1024bit_2_ciper_oaep_sha1.txt | 2 + .../rsa/cases/1024bit_2_ciper_oaep_sha256.txt | Bin 0 -> 128 bytes tests/rsa/cases/1024bit_2_ciper_pkcs1.txt | 2 + tests/rsa/cases/1024bit_2_private.pem | 15 ++++++ tests/rsa/cases/1024bit_2_public.pub | 6 +++ tests/rsa/cases/2048bit_1_ciper_oaep_sha1.txt | Bin 0 -> 256 bytes .../rsa/cases/2048bit_1_ciper_oaep_sha256.txt | Bin 0 -> 256 bytes tests/rsa/cases/2048bit_1_ciper_pkcs1.txt | Bin 0 -> 256 bytes tests/rsa/cases/2048bit_1_private.pem | 27 ++++++++++ tests/rsa/cases/2048bit_1_public.pub | 9 ++++ tests/rsa/cases/2048bit_2_ciper_oaep_sha1.txt | 2 + .../rsa/cases/2048bit_2_ciper_oaep_sha256.txt | 1 + tests/rsa/cases/2048bit_2_ciper_pkcs1.txt | 2 + tests/rsa/cases/2048bit_2_private.pem | 27 ++++++++++ tests/rsa/cases/2048bit_2_public.pub | 9 ++++ tests/rsa/cases/4096bit_1_ciper_oaep_sha1.txt | Bin 0 -> 512 bytes .../rsa/cases/4096bit_1_ciper_oaep_sha256.txt | Bin 0 -> 512 bytes tests/rsa/cases/4096bit_1_ciper_pkcs1.txt | Bin 0 -> 512 bytes tests/rsa/cases/4096bit_1_private.pem | 51 ++++++++++++++++++ tests/rsa/cases/4096bit_1_public.pub | 14 +++++ tests/rsa/cases/4096bit_2_ciper_oaep_sha1.txt | Bin 0 -> 512 bytes .../rsa/cases/4096bit_2_ciper_oaep_sha256.txt | Bin 0 -> 512 bytes tests/rsa/cases/4096bit_2_ciper_pkcs1.txt | Bin 0 -> 512 bytes tests/rsa/cases/4096bit_2_private.pem | 51 ++++++++++++++++++ tests/rsa/cases/4096bit_2_public.pub | 14 +++++ tests/rsa/cases/generate_test.sh | 20 +++++++ tests/rsa/cases/plain.txt | 1 + tests/rsa/ciper_oaep_sha1.txt | Bin 256 -> 0 bytes tests/rsa/ciper_pkcs1.txt | Bin 256 -> 0 bytes tests/rsa/decryption.test.ts | 38 +++++++++++++ tests/rsa/encryption.test.ts | 34 ++++++++++++ tests/rsa/rsa.test.ts | 46 ---------------- 37 files changed, 349 insertions(+), 46 deletions(-) create mode 100644 tests/rsa/cases/1024bit_1_ciper_oaep_sha1.txt create mode 100644 tests/rsa/cases/1024bit_1_ciper_oaep_sha256.txt create mode 100644 tests/rsa/cases/1024bit_1_ciper_pkcs1.txt create mode 100644 tests/rsa/cases/1024bit_1_private.pem create mode 100644 tests/rsa/cases/1024bit_1_public.pub create mode 100644 tests/rsa/cases/1024bit_2_ciper_oaep_sha1.txt create mode 100644 tests/rsa/cases/1024bit_2_ciper_oaep_sha256.txt create mode 100644 tests/rsa/cases/1024bit_2_ciper_pkcs1.txt create mode 100644 tests/rsa/cases/1024bit_2_private.pem create mode 100644 tests/rsa/cases/1024bit_2_public.pub create mode 100644 tests/rsa/cases/2048bit_1_ciper_oaep_sha1.txt create mode 100644 tests/rsa/cases/2048bit_1_ciper_oaep_sha256.txt create mode 100644 tests/rsa/cases/2048bit_1_ciper_pkcs1.txt create mode 100644 tests/rsa/cases/2048bit_1_private.pem create mode 100644 tests/rsa/cases/2048bit_1_public.pub create mode 100644 tests/rsa/cases/2048bit_2_ciper_oaep_sha1.txt create mode 100644 tests/rsa/cases/2048bit_2_ciper_oaep_sha256.txt create mode 100644 tests/rsa/cases/2048bit_2_ciper_pkcs1.txt create mode 100644 tests/rsa/cases/2048bit_2_private.pem create mode 100644 tests/rsa/cases/2048bit_2_public.pub create mode 100644 tests/rsa/cases/4096bit_1_ciper_oaep_sha1.txt create mode 100644 tests/rsa/cases/4096bit_1_ciper_oaep_sha256.txt create mode 100644 tests/rsa/cases/4096bit_1_ciper_pkcs1.txt create mode 100644 tests/rsa/cases/4096bit_1_private.pem create mode 100644 tests/rsa/cases/4096bit_1_public.pub create mode 100644 tests/rsa/cases/4096bit_2_ciper_oaep_sha1.txt create mode 100644 tests/rsa/cases/4096bit_2_ciper_oaep_sha256.txt create mode 100644 tests/rsa/cases/4096bit_2_ciper_pkcs1.txt create mode 100644 tests/rsa/cases/4096bit_2_private.pem create mode 100644 tests/rsa/cases/4096bit_2_public.pub create mode 100644 tests/rsa/cases/generate_test.sh create mode 100644 tests/rsa/cases/plain.txt delete mode 100644 tests/rsa/ciper_oaep_sha1.txt delete mode 100644 tests/rsa/ciper_pkcs1.txt create mode 100644 tests/rsa/decryption.test.ts create mode 100644 tests/rsa/encryption.test.ts delete mode 100644 tests/rsa/rsa.test.ts diff --git a/tests/rsa/cases/1024bit_1_ciper_oaep_sha1.txt b/tests/rsa/cases/1024bit_1_ciper_oaep_sha1.txt new file mode 100644 index 0000000..09ab8dc --- /dev/null +++ b/tests/rsa/cases/1024bit_1_ciper_oaep_sha1.txt @@ -0,0 +1,2 @@ +km8e5_qU!)ҡ[!  kuόM"Sȑ&I܍Ěb:} p !m +/> ?S`b ڟ4%B_ySu \ No newline at end of file diff --git a/tests/rsa/cases/1024bit_1_ciper_oaep_sha256.txt b/tests/rsa/cases/1024bit_1_ciper_oaep_sha256.txt new file mode 100644 index 0000000..326adb6 --- /dev/null +++ b/tests/rsa/cases/1024bit_1_ciper_oaep_sha256.txt @@ -0,0 +1 @@ +r=QG5 \SaA%\WzQzb" ؐV)z[nĘS[f",pWzgRސ&# qg \ FӇݵg \ No newline at end of file diff --git a/tests/rsa/cases/1024bit_1_ciper_pkcs1.txt b/tests/rsa/cases/1024bit_1_ciper_pkcs1.txt new file mode 100644 index 0000000000000000000000000000000000000000..4c861ab10e0062da49d8e07240fbb10c4812c0a3 GIT binary patch literal 128 zcmV-`0Du2kK4c&AD4_sVEw2t`d-iQMSFG7n+`Y6NLeGu!W5%8rGbuUgJqON9Rp&AAYdD%l*a id(*=5qa$|<@By!{NSll1yguqa!k|W`vXdbc4|' \ No newline at end of file diff --git a/tests/rsa/cases/1024bit_2_ciper_oaep_sha256.txt b/tests/rsa/cases/1024bit_2_ciper_oaep_sha256.txt new file mode 100644 index 0000000000000000000000000000000000000000..fa204dd4f35704e6552a2a506b9c30cd5215bcab GIT binary patch literal 128 zcmV-`0Du2d!~9X%5wu5r$qHKI3le~nOpdU|^z${IQD9#*JRZMdTL7U3K%~e-c^5dx zf06fn#}Q%hkW96d-%oRRB3{WgEZ-2K)b~mcejkvjzK0G;({fsEv*a8Ev?Z8$;v)MrG84deLSrXAc6Od0F@YH;QgaU}nK{=0 literal 0 HcmV?d00001 diff --git a/tests/rsa/cases/1024bit_2_ciper_pkcs1.txt b/tests/rsa/cases/1024bit_2_ciper_pkcs1.txt new file mode 100644 index 0000000..e56d2ec --- /dev/null +++ b/tests/rsa/cases/1024bit_2_ciper_pkcs1.txt @@ -0,0 +1,2 @@ +\G񛑆( +rdSϳ 0Im}U7#AeD!֬AaCoѫ!?&$g.ב~.?c35x„V$GpoBo7Q/%o \ No newline at end of file diff --git a/tests/rsa/cases/1024bit_2_private.pem b/tests/rsa/cases/1024bit_2_private.pem new file mode 100644 index 0000000..8a18128 --- /dev/null +++ b/tests/rsa/cases/1024bit_2_private.pem @@ -0,0 +1,15 @@ +-----BEGIN RSA PRIVATE KEY----- +MIICXAIBAAKBgQDQaYtcXuUK1GW796VloxTrr+Kf81XX85BxsPtfyKa+ywzRYGUr +I4PO14RjkXtQV1Qlr42EngNqJGTcFtum5sWxgb1ZfbAWuC4HDJrX/41hzgijaMT9 +i07twjt+UiQg9BJ6TguSxza5/IdJHFCR/f0S1fiMcr4KR9mxWF6ETpi1MQIDAQAB +AoGBALavfbhnejAnBFJi/vl7nXAHSgjZPStB1iDCeWZ5tShTRpYWADhzNivOoL7C +nKxNEHbETrGr7ORSqJJ/+uRvL2QXi8ybC/5SUaQCh2iDbMVazLWh38Rb8C+1Wx1u +rWmH0Yr6rm/X2LIAL6TUQVW9qyueKvnVye5+Mc1WPQRNRMiRAkEA6tv1yPMfyRC9 +CIj8KKAGQQ7lCyTrZZJVwkBnmHlMu5Jq/cO5QVY5GFZM6OmxRZL9T1gmccmfBP3F +dDpnt1ZbBQJBAOMsJbqOLSuIaGJ7ro9d8Akwjc/bFr4+/HJRAJru2msZ2fiNbYBd +2bbpo9i3PzKuiSbJgNBTC/+pVx0T0AoiAT0CQCeG+ajmclUOwiHrnDTiKetNLHNh +tVSU+mYFHOpmj+mJzQQEqus6LcXQ0ITqv/ErugJenanM+zyF0XOsWMRcflkCQGPk +e8qmzjIAT3ZO0Jl2QyCnWxR1UHDjnXpfKBb4xM6nlXN4LRhvLFcEMeoP2hhdKy4F +GUGB4xrOyeGdXrO19DkCQDeM5zf4fho3pIByfA6bj2Gg/ocJ3N+/oB7TzVR5TP5S +3o4N3U+rEVtcDm1+m555Ji7n0b3WC1wu3M0FncUSF8Y= +-----END RSA PRIVATE KEY----- diff --git a/tests/rsa/cases/1024bit_2_public.pub b/tests/rsa/cases/1024bit_2_public.pub new file mode 100644 index 0000000..e6eb91b --- /dev/null +++ b/tests/rsa/cases/1024bit_2_public.pub @@ -0,0 +1,6 @@ +-----BEGIN PUBLIC KEY----- +MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDQaYtcXuUK1GW796VloxTrr+Kf +81XX85BxsPtfyKa+ywzRYGUrI4PO14RjkXtQV1Qlr42EngNqJGTcFtum5sWxgb1Z +fbAWuC4HDJrX/41hzgijaMT9i07twjt+UiQg9BJ6TguSxza5/IdJHFCR/f0S1fiM +cr4KR9mxWF6ETpi1MQIDAQAB +-----END PUBLIC KEY----- diff --git a/tests/rsa/cases/2048bit_1_ciper_oaep_sha1.txt b/tests/rsa/cases/2048bit_1_ciper_oaep_sha1.txt new file mode 100644 index 0000000000000000000000000000000000000000..59c583865c759e6491e839f7373f24aa0ccbdb99 GIT binary patch literal 256 zcmV+b0ssDVV`6}3#o81WK>=?&0Wyh8Y9#u?Z%#$8Dxw*-+kolXY%F*h3nIUkQy}1) zw%vv=C#W`XawCaff1^$FHEX!AmMgU)X=Pp*{0|v;_*Omyp@odphzHuE|7jB243d%( zd9tKee&QlLkY|sW0uk%%3S1ZJL{_#gz+dCH8}z_D9^vR&?i>&vg)!EV(H@We-n$(! zhe|_-9bJLm<@!m?u+scqJIuSl#LV;A9Y269#|Ff4`8gz_$Xm*tTPcDB-EFV5=M zJD$Mn;`CRfMAo0#mMB1x}D7vh`3!es201&2qAw(p`)L z)K5?{n6%nMrW0O@0gj literal 0 HcmV?d00001 diff --git a/tests/rsa/cases/2048bit_1_ciper_pkcs1.txt b/tests/rsa/cases/2048bit_1_ciper_pkcs1.txt new file mode 100644 index 0000000000000000000000000000000000000000..f3a9c581191f9bb7bedce2fb4e61d63f0fa6507e GIT binary patch literal 256 zcmV+b0ssC?Ow#J()rjBzImSSuM{LODUpZRQiKWaWGoNEx=k{P@972;aYh!5?F>Rf` zApF%VyNAp`)vH+U1Bus*I6Qh6f(#Rl1#2g{m^#$y5idn5V{b+zCs;c&Pb9JX6CV^c z!d_de$k3VaT*X6KW8=F=H)r0%N2dU;HLF6EpunB`WA+Xv{jvTkKN+&VtVKcRsP=K+ zg5rzn=j&F`!ZsG~85{^rO:yt + eV~Q`]Hfε%*~7 WM:%b~rxjSJV^yy+/%]2Z&Y$(y5 ,|/ \ No newline at end of file diff --git a/tests/rsa/cases/2048bit_2_ciper_oaep_sha256.txt b/tests/rsa/cases/2048bit_2_ciper_oaep_sha256.txt new file mode 100644 index 0000000..196b52a --- /dev/null +++ b/tests/rsa/cases/2048bit_2_ciper_oaep_sha256.txt @@ -0,0 +1 @@ +J٭X[1ZsP0J1R9k{ 4W +7zk̰psr!?C*OD='&p8/:02:Bp^\<>CarK &n R\Q8aKniC9&4mXʮ՞0'.=`{oCACQ@zs `2#` ƔF=;Yr8/->T{bzZoR^rM?3oiM7eQ{4VrAw{oEOM4c9Mp;%)d7>PRTmqNrMSpHVqtAS)Cys*pqlgm zTH-e$qSm67Oll%?9_v;`UlVOv;G~&-08+I+Rt;EJ&znj~6{ZOa{P(%(J>{3#H-;JW21GCbqCsi*vi@(I)bbYkNHY9~her&KrC<7lAf1VfwV5cq~b-x7&ajM!02bTBpv9d^u`r}liIC*k)) z)5S-YAOZE8PDs5`=7=lV7n}&kwEV9WK;)E~r6t|_7*lyPkSl-@I);0oW7)WbFjRVt7cpP2WUepypNd{Te C6awV{ literal 0 HcmV?d00001 diff --git a/tests/rsa/cases/4096bit_1_ciper_oaep_sha256.txt b/tests/rsa/cases/4096bit_1_ciper_oaep_sha256.txt new file mode 100644 index 0000000000000000000000000000000000000000..b2c7cd363f1ba409d00274e6177407599bc8220d GIT binary patch literal 512 zcmV+b0{{JJNMiqOrB6y#hM}hjZEmDFD6A@{ZF{ZT7!$dq2IT0f6*!xXPlv-u!KyR9 z{#mg_RVYTIvEwLa%dNF`mC|QduGLR*WitK4hx7S*6|!p*p{vETh2|fQwkcd22(#>R zAV%9&7<^}lJ8OrO8lPEo0=iykRHrHDX-;C*uBP%A9o-Au54>=Mkwu|bxMT6ot6rGp zclFBP(rd2~3f+d%AYqOHX9eF+8n+-{0Apne>Wige)gOE!6=dpw}y^@Ww zFTuzl<6Q>xPg^TA6o{_joI+OO^QlUo{yvA;y8bfI zecWu6lt~qx>wX>v)aOTRyim+f5}fYEn<1M!H6T3-N`w;HEk=r-06>N4>!*PQSs+P# z!2b6nN46Zx5IkzYqq!^WBM4m+D=HyF8Y2k}pO{x9Uf=9<3%?^42$le@ zv^LWsZDC-!%0P@-V#mdvs$h9z*ls|)_Kjxw91H3Wkr;$j+HWP`wD~c}|LqYYu#=MoKBAA=z8lXJ zscWpCT&nxWH~p8JK&kBi=3@#~mr1eh70)84f7aiX8Z*kj<))U@o<%TxqIk4u&hnb8 z+`TPpjmP4sNss+YgoRn;8$%G-_SvR(vYVe+qPljuux==dbUsNj^-VknE_vpzD&i!` C>Hib} literal 0 HcmV?d00001 diff --git a/tests/rsa/cases/4096bit_1_private.pem b/tests/rsa/cases/4096bit_1_private.pem new file mode 100644 index 0000000..0a7deb8 --- /dev/null +++ b/tests/rsa/cases/4096bit_1_private.pem @@ -0,0 +1,51 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIJKQIBAAKCAgEAwR2+z04KUkauCxTpePQvplpK56csVuqe8hfsHzjasdGr/qpz +IBllGRoCDT4FFTtmnB0aHiCJWUiFK+khmSqVCX2cYtWwmmPXi4IPyaw0dIXmCmaf +Dl9K6wquc0GD/pPYapRFkhH0s10WueYghhL/n2VwJgpy0UzeTWWMAyQHfUH4mm1m +5geKQ27PTdS0dI1Yf30RwIrzPjk0i7usOB1Q96roiOVGTzGHLXcMtdwYQq87YedY +hl7CzNjQzJpjO0HeiL1BomvCzAbskWvOWFYS/Tx5CWgiBFtpCnt6EQKGK2VwfDam +gHUyhXonLwMH8dQVkO8PFTtJUHJGeqpM/v9XVyVQEtcDBFcb7lABAM05aqFuONvW +6EKYDyRRFNdBP+2SGZ+NvMz3DUHzeFbAdXXGOlB/qPWVx7+elSgnNYhuIgzm1MuE +bDxDg+6+LC3PO6RTyIFaos5Tk9xBYss/mDRkj2O8T1XZbKl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VVqoave9 +Ll/8+fld/+WdQmwFRDcUTe5MEkcm0D4HEVOuaKpsOmXCeYLhPMJLh9VzTH1B6QDN +i+r1DaS+TqMSYoeTeH5VGN3wawfy0vgbBCj95Ra8mGi+sHMxUGa5oM+TQnbjtg6Z +ag97rPs5vHC/gLWb2wsOi93TdRXEjnvBAoIBAQCT6aKeOvSuKmjIXwoQwE36UEdX +yaG+eG9mXXRH3brzNtkVVyryRDJxP41N9nUk0YDBbCEjv9UjDNw0/WH49f+QwuTr +oKz6WriYtjdXN6tpihcXCg0bffJEObSWXiqJgTYVZrOEk3xwiCLzl2ED94zWCLMO +xGiwS7W72whHHkS6F0wM5s+6JD3IbeNjC4j62687EsQXBG2yaVTGznZPw29lXdiZ +21O3UFVVIqqwdhvZHoJpDTth2bVlr8rqF2fqRkp6cZAlMlV0IZ+Pr+LX+LebnMkQ +6CHh8JYGTRg317853MO0age9OaCVBou1Rck8n3S1oIqr5122zZuSeQoF3EEf +-----END RSA PRIVATE KEY----- diff --git a/tests/rsa/cases/4096bit_1_public.pub b/tests/rsa/cases/4096bit_1_public.pub new file mode 100644 index 0000000..e9e74cc --- /dev/null +++ b/tests/rsa/cases/4096bit_1_public.pub @@ -0,0 +1,14 @@ +-----BEGIN PUBLIC KEY----- +MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAwR2+z04KUkauCxTpePQv +plpK56csVuqe8hfsHzjasdGr/qpzIBllGRoCDT4FFTtmnB0aHiCJWUiFK+khmSqV +CX2cYtWwmmPXi4IPyaw0dIXmCmafDl9K6wquc0GD/pPYapRFkhH0s10WueYghhL/ +n2VwJgpy0UzeTWWMAyQHfUH4mm1m5geKQ27PTdS0dI1Yf30RwIrzPjk0i7usOB1Q +96roiOVGTzGHLXcMtdwYQq87YedYhl7CzNjQzJpjO0HeiL1BomvCzAbskWvOWFYS +/Tx5CWgiBFtpCnt6EQKGK2VwfDamgHUyhXonLwMH8dQVkO8PFTtJUHJGeqpM/v9X +VyVQEtcDBFcb7lABAM05aqFuONvW6EKYDyRRFNdBP+2SGZ+NvMz3DUHzeFbAdXXG +OlB/qPWVx7+elSgnNYhuIgzm1MuEbDxDg+6+LC3PO6RTyIFaos5Tk9xBYss/mDRk +j2O8T1XZbKl5+UMxlKU0TBUdAu4Q2BkWTbACp+6UcTZ20RiCcfYjQph7ba7VqEDa +nJgePi/zGgQvWTmCJ/zFbcP6N064IPkX7wwALV6b8DGEyNfHI9K97cTm8+ecGdii +3VPusi8IaQfiR6nNFkhbtfcDDw14mI+E0rQAm+4HAkdB7XExvlL2aTrsFIM28Ynt +NA82hJKNzOI2VCUtd0Zs+R0CAwEAAQ== +-----END PUBLIC KEY----- diff --git a/tests/rsa/cases/4096bit_2_ciper_oaep_sha1.txt b/tests/rsa/cases/4096bit_2_ciper_oaep_sha1.txt new file mode 100644 index 0000000000000000000000000000000000000000..39b676ff735e264ff1f38f1b9e2b01472d0ce1d8 GIT binary patch literal 512 zcmV+b0{{IPEi4xOgj&lfj;1UO4Z=s(EDJ@c)#B>ZKBn}Av*F2R$7GR&Xk0L_WS|Fo zdS^;~nRM+KA^vrKOAJqGdV}PR;PeIrvB#QDJ%MIQd-0=)1V+u^NjbYX1Xc5IWa{EH zf?kepTcO{+tsi%PGM4DsM~MkIT`|OK9!6l2o! zU=$2uXsdrG2dzOL_QB2Zaq3wu*b3*7^y28KP5F6IqbtK&*KWV_!2bcy#}FB`BZB*< zhM!<5G)}D&rErALcU$yx{HLDpa696WWsuFM!(14;H4@8eW&J6cmT74D6U9rru?pZG zq>*a$0gH1d(BY723f*b>$HeIiBSvOAF_w~jr{R)a)eF*JDv|JhF|!>41+QDjg#iZ5 z>u6TFk$9+)+qv862^*vIpcqWJ`P43K<4zM~mawqLfCg zbLGcbVC{|jv69A-l7}!y8g2niOmBy*>v;c*>t%q=gPW2^WYdo;}h;}JC? C*89}} literal 0 HcmV?d00001 diff --git a/tests/rsa/cases/4096bit_2_ciper_oaep_sha256.txt b/tests/rsa/cases/4096bit_2_ciper_oaep_sha256.txt new file mode 100644 index 0000000000000000000000000000000000000000..737fc9e1f3aae35e482d7a36f00dbd88ca80b8a8 GIT binary patch literal 512 zcmV+b0{{I&-PGy?-Oj~!;;;+MByYZ=VP1kaRRiEf&Ckf5BNFk%PO{;dRAHu;;~@{a z@GRKflM;Z}56lDA)R%->fqdv?^0qLs=m)|R1y8+)IP;qc{kU^7g9ldyoC_0sZ2Q0u zQOZAUmbCf0sm?pki-W#&@{!FTCgZMtIY&bY5Pi>%U!m57Dw|DTpR4k6zg-K0A*KJt?NK_2DL zP-$oLCBJB3N+zw1$5Zj!K){+Iz=J;(fJ`zNQNs#4$eeI`r)E?y$g#)q9&W7LYA4SH zF&fT9u#l#R5{k&D*beyQ;Oi$F0#x-Mtwj!f^@sDqawy%n``!r#JVf>?)N&+?m1^SY z(|q#>LDs->7j3g40+liu%PHsKTe9*9-{Q!aa`z@^#Jp*XksaT!T#Gj-^MsQ C_yUgr literal 0 HcmV?d00001 diff --git a/tests/rsa/cases/4096bit_2_ciper_pkcs1.txt b/tests/rsa/cases/4096bit_2_ciper_pkcs1.txt new file mode 100644 index 0000000000000000000000000000000000000000..20239a2eb5670e52c81b9c572d2184e4fd245793 GIT binary patch literal 512 zcmV+b0{{JAgQBD{Nqoay#5i{u-fO5kPfLuz2F5g!av}9$1O&V_G9UMKQ?OVdOixu~ zTduG35t%2;kzDf6@VqZ-IJ$g?xb#;t=KbZF4d?ka|G2JI$5mDk4UrskZB1>KmQ-27 z+m##+0ITGIqfg0U!0xjHTcTv&&{L{$Cbs5z8V>Urmx}x&$m9Vp6I5pDtPN&^%nW>V zkVW9GHuWXCOkTGHikQXlkN%8mKq_ttOA-;>65AK@t{uC~$e}J^W2Z_(XXH=TRQo+~75BVH zOT;54)oZB2N;2x*>C_0e^>CNs5yqduI$%p`*r5zH#~8$SXi_yvEzK=?0}#m@T{*P> z40~lVZaOK%ve=iM@=QlTS7w{*R@sjtGgW@8eG^$Dw*e^T=;m(eLuPA zr)b-5BEiW15)M*3*7R6YrLz&wNGTB@&Er~$zqtdnKXkUmwjQ1??Ojc*P%%TU($gk$VOyNXs CF#?+a literal 0 HcmV?d00001 diff --git a/tests/rsa/cases/4096bit_2_private.pem b/tests/rsa/cases/4096bit_2_private.pem new file mode 100644 index 0000000..dd3d856 --- /dev/null +++ b/tests/rsa/cases/4096bit_2_private.pem @@ -0,0 +1,51 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIJKQIBAAKCAgEAyGsswKyN5ocGaHDCNbLL/Yrvw+u1VnftF1j5LBlbyoo9AfbN +Qj80G7jWnHUa5eMb03/n7BqZlHrtS0+Zze+DtK9TM/5xTBmPKg3QhSyZYor7DcxT +1kjn1LyJ7SeTANEhxzViaJjdPCQMZt6DNU5EAKUnbab6jcovnXy4VgPJfLNB223Q +QiB9qfa7NNYJ9YwxHgOCpyKmo73Wm0Ps06f6RcGh/GdURzv2l+dw2P5Ixz4jFXuX +QE0MQ2GkO3uyQuRfNbQof1NcjIvf5qggTtSQ7x2g6cJyeDdoijtnJYAupOWL9ojN +nsmjrgg4xAGoHJ9eHZn2+hKaIdUc7UaiZ4JtAvGN/DI98iITkrrJ57REPkktaNxX +Gjt3Oi7r4O+K+nlNvCWZlO08DM7uqEZsxUVGrSUEUyUCO5psue8dlkM1O9lrUo/+ +WOoiKaY1SbFbaNzyDmFSbmiyorS3FDr1pv9eXIQjPX+tpk/Dz/6ceYY5+dVw1SwD +KHT2aRG+OUPUvibDcAyHx2W5m3S8KQKCVCXEmNN0j30Vci7grQiif55F1vPcc924 +JFWINwJbo8XdrRiGOTamy5D8tC4deCtcHiWVAmasOFHPProjcjeCsubBkmViIaJM +jNiL70wJVStEwr2zMHzM1vuMwjEN2kDxPorec2pOqKHi2gYF2E/8ZPki4XkCAwEA +AQKCAgAYgSomoLJ05jt9V5QXVrYkWPF55f71BJLDkQheQPd9KyvMaq2sFOg9H3t2 +A8d3Ji6HzJLBxnrSsbkDxUbL7CYRWIJ2z/xwTou3m5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WXsKiyfAF88QC9f9QC2HNNus4s7z6/PupRzE +Qk9lODzRbibJpKV7yT9TgOkg2WcctvI+4nQh9UBS1Vx8Dr4Fq6b/33vViOShwMKK +oiH+H8nQudvzAqnp5looCSHGarcrtTP5hLZ9FWiG7QIvdgOq64zq/oZXpDDqIg/S +DZPgqGtPrA+xbQZ2wtcF4TVuMavQt4KDeWfJDpwIsqSmF8nDqwIISdn1GDtK +-----END RSA PRIVATE KEY----- diff --git a/tests/rsa/cases/4096bit_2_public.pub b/tests/rsa/cases/4096bit_2_public.pub new file mode 100644 index 0000000..83c9db9 --- /dev/null +++ b/tests/rsa/cases/4096bit_2_public.pub @@ -0,0 +1,14 @@ +-----BEGIN PUBLIC KEY----- +MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAyGsswKyN5ocGaHDCNbLL +/Yrvw+u1VnftF1j5LBlbyoo9AfbNQj80G7jWnHUa5eMb03/n7BqZlHrtS0+Zze+D +tK9TM/5xTBmPKg3QhSyZYor7DcxT1kjn1LyJ7SeTANEhxzViaJjdPCQMZt6DNU5E +AKUnbab6jcovnXy4VgPJfLNB223QQiB9qfa7NNYJ9YwxHgOCpyKmo73Wm0Ps06f6 +RcGh/GdURzv2l+dw2P5Ixz4jFXuXQE0MQ2GkO3uyQuRfNbQof1NcjIvf5qggTtSQ +7x2g6cJyeDdoijtnJYAupOWL9ojNnsmjrgg4xAGoHJ9eHZn2+hKaIdUc7UaiZ4Jt +AvGN/DI98iITkrrJ57REPkktaNxXGjt3Oi7r4O+K+nlNvCWZlO08DM7uqEZsxUVG +rSUEUyUCO5psue8dlkM1O9lrUo/+WOoiKaY1SbFbaNzyDmFSbmiyorS3FDr1pv9e +XIQjPX+tpk/Dz/6ceYY5+dVw1SwDKHT2aRG+OUPUvibDcAyHx2W5m3S8KQKCVCXE +mNN0j30Vci7grQiif55F1vPcc924JFWINwJbo8XdrRiGOTamy5D8tC4deCtcHiWV +AmasOFHPProjcjeCsubBkmViIaJMjNiL70wJVStEwr2zMHzM1vuMwjEN2kDxPore +c2pOqKHi2gYF2E/8ZPki4XkCAwEAAQ== +-----END PUBLIC KEY----- diff --git a/tests/rsa/cases/generate_test.sh b/tests/rsa/cases/generate_test.sh new file mode 100644 index 0000000..c64ebf1 --- /dev/null +++ b/tests/rsa/cases/generate_test.sh @@ -0,0 +1,20 @@ +#!/bin/bash +for BITS in 1024 2048 4096 +do + for CASES in 1 2 + do + PRIVATE_KEY="./${BITS}bit_${CASES}_private.pem" + PUBLIC_KEY="./${BITS}bit_${CASES}_public.pub" + openssl genrsa -out $PRIVATE_KEY $BITS + openssl rsa -in $PRIVATE_KEY -pubout > $PUBLIC_KEY + + CIPER="./${BITS}bit_${CASES}_ciper_oaep_sha256.txt" + openssl pkeyutl -in plain.txt -encrypt -inkey $PRIVATE_KEY -pkeyopt rsa_padding_mode:oaep -pkeyopt rsa_oaep_md:sha256 -pkeyopt rsa_mgf1_md:sha256 > $CIPER + + CIPER="./${BITS}bit_${CASES}_ciper_oaep_sha1.txt" + openssl pkeyutl -in plain.txt -encrypt -inkey $PRIVATE_KEY -pkeyopt rsa_padding_mode:oaep -pkeyopt rsa_oaep_md:sha1 -pkeyopt rsa_mgf1_md:sha1 > $CIPER + + CIPER="./${BITS}bit_${CASES}_ciper_pkcs1.txt" + openssl pkeyutl -in plain.txt -encrypt -inkey $PRIVATE_KEY -pkeyopt rsa_padding_mode:pkcs1 > $CIPER + done +done \ No newline at end of file diff --git a/tests/rsa/cases/plain.txt b/tests/rsa/cases/plain.txt new file mode 100644 index 0000000..21b385a --- /dev/null +++ b/tests/rsa/cases/plain.txt @@ -0,0 +1 @@ +Lorem Ipsum is simply dummy text of the printing. \ No newline at end of file diff --git a/tests/rsa/ciper_oaep_sha1.txt b/tests/rsa/ciper_oaep_sha1.txt deleted file mode 100644 index 932fe3918127ad08bc4f4ebe2f1a4307041e5c4f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 256 zcmV+b0ssC2(No#HRf*GWT2aZ{$GaJ|C!Ay}i|*7c;CpB~b5lWgKWqHdDfBSce|7`T zUVHYYzjFnsb#MT(D-Of^*uybF>N;x>7cHvP$xBBKyPEoAVbn}czc;;l!{ zR~b*xOJ8t{C(qn~O@_TSmQ0(hR#yfQ|6K)~?Q1ArEqt<}a1TjW?4-5sp7u(x;paT- zCpylw_IGhjmJVg{7sqd)kVi&uh$*1zZ-N!1T;}xDGF)m#N;FbTuYmu)|34xmjc%wt Gl*ge*8+?@j diff --git a/tests/rsa/ciper_pkcs1.txt b/tests/rsa/ciper_pkcs1.txt deleted file mode 100644 index 9d3a8567931a7944f6b6afaee3a24f36c40356ac..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 256 zcmV+b0ssDq!l|h78HK+~v!JD-9Pc|E)x>UA5Fo_;Ks&lve(yi=NFADIVlI~G5Dhsr zWuu?Gt<4h4-DeiuM;3Q~b6VlzfO$UWAE;$=QSPb7fW;beLf*IoxSwvwP;`oJr->^` zi?hIXOgW1{xzDdmFXQ2eJyCzGE&amcRDS?tjK}kT3keL)h(96O!8 { + for (const bits of [1024, 2048, 4096]) { + for (const n of [1, 2]) { + await test(bits, n, "oaep", "sha1"); + await test(bits, n, "oaep", "sha256"); + await test(bits, n, "pkcs1"); + } + } +}); diff --git a/tests/rsa/encryption.test.ts b/tests/rsa/encryption.test.ts new file mode 100644 index 0000000..47f8a42 --- /dev/null +++ b/tests/rsa/encryption.test.ts @@ -0,0 +1,34 @@ +import { assertEquals } from "https://deno.land/std@0.63.0/testing/asserts.ts"; +import { RSAKey } from "../../src/rsa/rsa_key.ts"; +import { RSA } from "./../../mod.ts"; + +async function test( + key: RSAKey, + plain: string, + alg: "pkcs1" | "oaep", + hash?: "sha1" | "sha256", +) { + const rsa = new RSA(key); + const ciper = (await rsa.encrypt(plain, { padding: alg, hash: hash })); + const text = (await rsa.decrypt(ciper, { padding: alg, hash: hash })); + + assertEquals( + text.toString(), + plain, + `Paddng: ${alg}, Bits: ${key.length * 8}, Hash: ${hash}`, + ); +} + +Deno.test("RSA - Encryption", async () => { + const plain = "Lorem Ipsum is simply dummy text of the printing."; + + for (const bits of [1024, 2048, 4096]) { + const key = RSA.importKey(Deno.readTextFileSync( + `./tests/rsa/cases/${bits}bit_1_private.pem`, + )); + + await test(key, plain, "pkcs1"); + await test(key, plain, "oaep", "sha1"); + await test(key, plain, "oaep", "sha256"); + } +}); diff --git a/tests/rsa/rsa.test.ts b/tests/rsa/rsa.test.ts deleted file mode 100644 index 1dd7a94..0000000 --- a/tests/rsa/rsa.test.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { assertEquals } from "https://deno.land/std@0.63.0/testing/asserts.ts"; -import { RSA } from "./../../mod.ts"; - -Deno.test("Decrypt RSA OAEP SHA1", async () => { - const privateKeyRaw = Deno.readTextFileSync("./tests/rsa/private.pem"); - const ciperText = Deno.readFileSync("./tests/rsa/ciper_oaep_sha1.txt"); - - const privateKey = RSA.parseKey(privateKeyRaw); - const plainText = (await new RSA(privateKey).decrypt(ciperText)).toString(); - - assertEquals(plainText, "Hello World from RSA"); -}); - -Deno.test("Encrypt RSA OAEP SHA1", async () => { - const privateKeyRaw = Deno.readTextFileSync("./tests/rsa/private.pem"); - const privateKey = RSA.parseKey(privateKeyRaw); - const rsa = new RSA(privateKey); - - const ciperText = await rsa.encrypt("Hello World"); - const plainText = (await rsa.decrypt(ciperText)).toString(); - - assertEquals(plainText, "Hello World"); -}); - -Deno.test("Decrypt RSA PKCS1 v1.5", async () => { - const privateKeyRaw = Deno.readTextFileSync("./tests/rsa/private.pem"); - const ciperText = Deno.readFileSync("./tests/rsa/ciper_pkcs1.txt"); - const privateKey = RSA.parseKey(privateKeyRaw); - const rsa = new RSA(privateKey); - - const plainText = (await rsa.decrypt(ciperText, { padding: "pkcs1" })) - .toString(); - assertEquals(plainText, "Hello World"); -}); - -Deno.test("Encrypt RSA PKCS1 v1.5", async () => { - const privateKeyRaw = Deno.readTextFileSync("./tests/rsa/private.pem"); - const privateKey = RSA.parseKey(privateKeyRaw); - const rsa = new RSA(privateKey); - - const ciperText = await rsa.encrypt("Hello World", { padding: "pkcs1" }); - const plainText = (await rsa.decrypt(ciperText, { padding: "pkcs1" })) - .toString(); - - assertEquals(plainText, "Hello World"); -});