-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use WebCrypto for RSA and fallback to JS if not available.
- Loading branch information
Showing
9 changed files
with
242 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export interface RSAKey { | ||
n: bigint; | ||
e?: bigint; | ||
d?: bigint; | ||
p?: bigint; | ||
q?: bigint; | ||
dp?: bigint; | ||
dq?: bigint; | ||
qi?: bigint; | ||
length: number; | ||
} | ||
|
||
export interface RSAOption { | ||
hash: "sha1" | "sha256"; | ||
padding: "oaep" | "pkcs1"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export abstract class RSABase { | ||
public abstract async encrypt(m: Uint8Array): Promise<Uint8Array>; | ||
public abstract async decrypt(m: Uint8Array): Promise<Uint8Array>; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
rsa_oaep_encrypt, | ||
rsa_pkcs1_encrypt, | ||
rsa_oaep_decrypt, | ||
rsa_pkcs1_decrypt, | ||
} from "./rsa_internal.ts"; | ||
import { RawBinary } from "./../binary.ts"; | ||
import { RSAKey, RSAOption } from "./common.ts"; | ||
import { RSABase } from "./rsa_base.ts"; | ||
|
||
export class PureRSA implements RSABase { | ||
key: RSAKey; | ||
options: RSAOption; | ||
|
||
constructor(key: RSAKey, options: RSAOption) { | ||
this.key = key; | ||
this.options = options; | ||
} | ||
|
||
async encrypt(message: Uint8Array) { | ||
if (!this.key.e) throw "Invalid RSA key"; | ||
|
||
if (this.options.padding === "oaep") { | ||
return new RawBinary(rsa_oaep_encrypt( | ||
this.key.length, | ||
this.key.n, | ||
this.key.e, | ||
message, | ||
this.options.hash, | ||
)); | ||
} else if (this.options.padding === "pkcs1") { | ||
return new RawBinary( | ||
rsa_pkcs1_encrypt(this.key.length, this.key.n, this.key.e, message), | ||
); | ||
} | ||
|
||
throw "Invalid parameters"; | ||
} | ||
|
||
async decrypt(ciper: Uint8Array) { | ||
if (!this.key.d) throw "Invalid RSA key"; | ||
|
||
if (this.options.padding === "oaep") { | ||
return new RawBinary(rsa_oaep_decrypt( | ||
this.key.length, | ||
this.key.n, | ||
this.key.d, | ||
ciper, | ||
this.options.hash, | ||
)); | ||
} else if (this.options.padding === "pkcs1") { | ||
return new RawBinary( | ||
rsa_pkcs1_decrypt(this.key.length, this.key.n, this.key.d, ciper), | ||
); | ||
} | ||
|
||
throw "Invalid parameters"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { RSABase } from "./rsa_base.ts"; | ||
import { RSAKey, RSAOption } from "./common.ts"; | ||
|
||
function big_base64(m: bigint) { | ||
const bytes = []; | ||
|
||
while (m > 0n) { | ||
bytes.push(Number(m & 255n)); | ||
m = m >> 8n; | ||
} | ||
|
||
bytes.reverse(); | ||
let a = btoa(String.fromCharCode.apply(null, bytes)).replace(/=/g, ""); | ||
a = a.replace(/\+/g, "-"); | ||
a = a.replace(/\//g, "_"); | ||
return a; | ||
} | ||
|
||
export class WebCryptoRSA implements RSABase { | ||
key: RSAKey; | ||
options: RSAOption; | ||
encryptedKey: any = null; | ||
decryptedKey: any = null; | ||
|
||
constructor(key: RSAKey, options: RSAOption) { | ||
this.key = key; | ||
this.options = options; | ||
} | ||
|
||
protected getHashFunctionName() { | ||
if (this.options.hash === "sha1") return "SHA-1"; | ||
if (this.options.hash === "sha256") return "SHA-256"; | ||
return ""; | ||
} | ||
|
||
protected async loadKeyForDecrypt() { | ||
if (!this.key.e) return null; | ||
if (!this.key.d) return null; | ||
|
||
if (this.decryptedKey === null) { | ||
const jwk = { | ||
kty: "RSA", | ||
n: big_base64(this.key.n), | ||
d: big_base64(this.key.d), | ||
e: big_base64(this.key.e), | ||
p: this.key.p ? big_base64(this.key.p) : undefined, | ||
q: this.key.q ? big_base64(this.key.q) : undefined, | ||
dp: this.key.dp ? big_base64(this.key.dp) : undefined, | ||
dq: this.key.dq ? big_base64(this.key.dq) : undefined, | ||
qi: this.key.qi ? big_base64(this.key.qi) : undefined, | ||
ext: true, | ||
}; | ||
|
||
// @ts-ignore | ||
this.decryptedKey = await crypto.subtle.importKey( | ||
"jwk", | ||
jwk, | ||
{ | ||
name: "RSA-OAEP", | ||
hash: { name: this.getHashFunctionName() }, | ||
}, | ||
false, | ||
["decrypt"], | ||
); | ||
} | ||
|
||
return this.decryptedKey; | ||
} | ||
|
||
protected async loadKeyForEncrypt() { | ||
if (!this.key.e) return null; | ||
|
||
if (this.encryptedKey === null) { | ||
const jwk = { | ||
kty: "RSA", | ||
e: big_base64(this.key.e), | ||
n: big_base64(this.key.n), | ||
ext: true, | ||
}; | ||
|
||
// @ts-ignore | ||
this.encryptedKey = await crypto.subtle.importKey( | ||
"jwk", | ||
jwk, | ||
{ | ||
name: "RSA-OAEP", | ||
hash: { name: this.getHashFunctionName() }, | ||
}, | ||
false, | ||
["encrypt"], | ||
); | ||
} | ||
|
||
return this.encryptedKey; | ||
} | ||
|
||
async encrypt(m: Uint8Array) { | ||
// @ts-ignore | ||
return await crypto.subtle.encrypt( | ||
{ name: "RSA-OAEP" }, | ||
await this.loadKeyForEncrypt(), | ||
m, | ||
); | ||
} | ||
|
||
async decrypt(m: Uint8Array) { | ||
// @ts-ignore | ||
return await crypto.subtle.decrypt( | ||
{ name: "RSA-OAEP" }, | ||
await this.loadKeyForDecrypt(), | ||
m, | ||
); | ||
} | ||
} |
Oops, something went wrong.