Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/2 ecdsa implementation #30

Merged
merged 4 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

An implementation of the W3C Web Cryptography API specification (https://www.w3.org/TR/WebCryptoAPI/) for Go using Go's standard `crypto` library.

> [!IMPORTANT]
> Whilst we try to ensure that we don't commit breaking changes until we release our first stable version, there
> may be times where decisions made during early development no longer make sense and therefore require
> breaking changes. Please be mindful of this when updating your version of this library until we hit v1.0.0.

## Contents

- [Background](#background)
- [Implementation status](#implementation-status)
- [Getting started](#getting-started)
- [Algorithms](#algorithms)
- [ECDSA](#ecdsa)
- [HMAC](#hmac)
- [RSA-OAEP](#rsa-oaep)
- [SHA](#sha)
Expand All @@ -29,6 +35,7 @@ This library is still in active development and all algorithms are not yet suppo

| Algorithm | encrypt | decrypt | sign | verify | digest | generateKey | deriveKey | deriveBits | importKey | exportKey | wrapKey | unwrapKey |
| :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: | :--: |
| [ECDSA](#ecdsa) |||:white_check_mark:|:white_check_mark:||:white_check_mark:|||:white_check_mark:|:white_check_mark:|||
| [HMAC](#hmac) |||:white_check_mark:|:white_check_mark:||:white_check_mark:|||:white_check_mark:|:white_check_mark:|||
| [RSA-OAEP](#rsa-oaep) |:white_check_mark:|:white_check_mark:||||:white_check_mark:|||:white_check_mark:|:white_check_mark:|||
| [SHA](#sha) |||||:white_check_mark:||||||||
Expand All @@ -39,6 +46,52 @@ This library is still in active development and all algorithms are not yet suppo

## Algorithms

### ECDSA

The **ECDSA** algorithm is the implementation of operations described in [§23](https://www.w3.org/TR/WebCryptoAPI/#ecdsa) of the W3C specification.

```go
package main

import (
"github.com/armortal/webcrypto-go"
"github.com/armortal/webcrypto-go/algorithms/ecdsa"
)

func main() {
// generate a new ECDSA key
key, err := webcrypto.Subtle().GenerateKey(
&ecdsa.Algorithm{
NamedCurve: "P-256",
}, true, webcrypto.Sign, webcrypto.Verify)
if err != nil {
panic(err)
}

ckp := key.(webcrypto.CryptoKeyPair)

// sign some data with the private key
sig, err := webcrypto.Subtle().Sign(&ecdsa.Algorithm{
Hash: "SHA-256",
}, ckp.PrivateKey(), []byte("test"))
if err != nil {
panic(err)
}

// verify the signature with the public key
ok, err := webcrypto.Subtle().Verify(&ecdsa.Algorithm{
Hash: "SHA-256",
}, ckp.PublicKey(), sig, []byte("test"))
if err != nil {
panic(err)
}

if !ok {
// didn't verify - do something
}
}
```

### HMAC

The **HMAC** algorithm is the implementation of operations described in [§29](https://www.w3.org/TR/WebCryptoAPI/#hmac) of the W3C specification.
Expand All @@ -54,6 +107,12 @@ import (
func main() {
// Generate a new key. A *hmac.CryptoKey is returned which implements webcrypto.CryptoKey
key, err := webcrypto.Subtle().GenerateKey(
&Algorithm{
Name: "ECDSA",
Params: ecdsa.KeyGenParams{

}
}
&hmac.Algorithm{
KeyGenParams: &hmac.KeyGenParams{
Hash: "SHA-256",
Expand Down
6 changes: 6 additions & 0 deletions algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package webcrypto

import "fmt"

var algorithms = map[string]func() SubtleCrypto{}

// Algorithm implements the Algorithm dictionary type as specified at
Expand All @@ -33,6 +35,10 @@ type KeyAlgorithm interface {
// RegisterAlgorithm will register SubtleCrypto implementations referenced by the algorithm
// name provided. When fn gets called, it should return a NEW instance of the implementation.
func RegisterAlgorithm(name string, fn func() SubtleCrypto) {
_, ok := algorithms[name]
if ok {
panic(fmt.Sprintf("%s algorithm already registered", name))
}
algorithms[name] = fn
}

Expand Down
Loading
Loading