-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ecdsa support * Add new test cases * Rollback formatting for types file
- Loading branch information
Showing
11 changed files
with
308 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { webcrypto } from "one-webcrypto" | ||
import { NamedCurve, KeyType } from "../types" | ||
|
||
export const ALG = "ECDSA" | ||
export const DEFAULT_CURVE = "P-256" | ||
export const DEFAULT_HASH_ALG = "SHA-256" | ||
|
||
export const generateKeypair = async ( | ||
namedCurve: NamedCurve = DEFAULT_CURVE | ||
): Promise<CryptoKeyPair> => { | ||
return await webcrypto.subtle.generateKey( | ||
{ | ||
name: ALG, | ||
namedCurve, | ||
}, | ||
false, | ||
["sign", "verify"] | ||
) | ||
} | ||
|
||
export const exportKey = async (key: CryptoKey): Promise<Uint8Array> => { | ||
const buf = await webcrypto.subtle.exportKey("spki", key) | ||
return new Uint8Array(buf) | ||
} | ||
|
||
export const importKey = async ( | ||
key: Uint8Array, | ||
namedCurve: NamedCurve | ||
): Promise<CryptoKey> => { | ||
return await webcrypto.subtle.importKey( | ||
"spki", | ||
key.buffer, | ||
{ name: ALG, namedCurve }, | ||
true, | ||
["verify"] | ||
) | ||
} | ||
|
||
export const sign = async ( | ||
msg: Uint8Array, | ||
privateKey: CryptoKey | ||
): Promise<Uint8Array> => { | ||
const buf = await webcrypto.subtle.sign( | ||
{ name: ALG, hash: { name: DEFAULT_HASH_ALG } }, | ||
privateKey, | ||
msg.buffer | ||
) | ||
return new Uint8Array(buf) | ||
} | ||
|
||
export const verify = async ( | ||
msg: Uint8Array, | ||
sig: Uint8Array, | ||
pubKey: Uint8Array, | ||
namedCurve: NamedCurve | ||
): Promise<boolean> => { | ||
return await webcrypto.subtle.verify( | ||
{ name: ALG, hash: { name: DEFAULT_HASH_ALG } }, | ||
await importKey(pubKey, namedCurve), | ||
sig.buffer, | ||
msg.buffer | ||
) | ||
} | ||
|
||
export const toKeyType = (namedCurve: NamedCurve): KeyType => { | ||
switch (namedCurve) { | ||
case "P-256": | ||
return "p256" | ||
case "P-384": | ||
return "p384" | ||
case "P-521": | ||
return "p521" | ||
default: | ||
throw new Error(`Unsupported namedCurve: ${namedCurve}`) | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * as rsa from "./rsa" | ||
export * as ecdsa from "./ecdsa" |
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
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,56 @@ | ||
import { webcrypto } from "one-webcrypto" | ||
import * as uint8arrays from "uint8arrays" | ||
import * as ecdsa from "../crypto/ecdsa" | ||
import { | ||
AvailableCryptoKeyPair, | ||
Encodings, | ||
isAvailableCryptoKeyPair, | ||
NamedCurve, | ||
} from "../types" | ||
import BaseKeypair from "./base" | ||
|
||
export class EcdsaKeypair extends BaseKeypair { | ||
private keypair: AvailableCryptoKeyPair | ||
|
||
constructor( | ||
keypair: AvailableCryptoKeyPair, | ||
publicKey: Uint8Array, | ||
namedCurve: NamedCurve, | ||
exportable: boolean | ||
) { | ||
super(publicKey, ecdsa.toKeyType(namedCurve), exportable) | ||
this.keypair = keypair | ||
} | ||
|
||
static async create(params?: { | ||
namedCurve?: NamedCurve | ||
exportable?: boolean | ||
}): Promise<EcdsaKeypair> { | ||
const { namedCurve = "P-256", exportable = false } = params || {} | ||
const keypair = await ecdsa.generateKeypair(namedCurve) | ||
|
||
if (!isAvailableCryptoKeyPair(keypair)) { | ||
throw new Error(`Couldn't generate valid keypair`) | ||
} | ||
|
||
const publicKey = await ecdsa.exportKey(keypair.publicKey) | ||
return new EcdsaKeypair(keypair, publicKey, namedCurve, exportable) | ||
} | ||
|
||
async sign(msg: Uint8Array): Promise<Uint8Array> { | ||
return await ecdsa.sign(msg, this.keypair.privateKey) | ||
} | ||
|
||
async export(format: Encodings = "base64pad"): Promise<string> { | ||
if (!this.exportable) { | ||
throw new Error("Key is not exportable") | ||
} | ||
const arrayBuffer = await webcrypto.subtle.exportKey( | ||
"pkcs8", | ||
this.keypair.privateKey | ||
) | ||
return uint8arrays.toString(new Uint8Array(arrayBuffer), format) | ||
} | ||
} | ||
|
||
export default EcdsaKeypair |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./ed25519" | ||
export * from "./ecdsa" | ||
export * from "./rsa" | ||
export * from "./base" |
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
Oops, something went wrong.