Skip to content

Commit

Permalink
Merge pull request #30 from grafana/feature/generateKey-symmetric
Browse files Browse the repository at this point in the history
[1/3] Implement `generateKey` with support for AES algorithms
  • Loading branch information
oleiade authored Mar 28, 2023
2 parents cacea29 + 86e837f commit 13857ba
Show file tree
Hide file tree
Showing 18 changed files with 1,086 additions and 332 deletions.
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
# xk6-webcrypto

This is a **work in progress** project implementation of the [WebCrypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) for k6.
This is a **work in progress** project implementation of the [WebCrypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API) specification for k6.

## Current state

The current state of the project is that it is an experimental module of the WebCrypto API specification. While we consider it ready for production use, it is still missing some features and is not yet fully compliant with the specification.

### Supported APIs and algorithms

#### Crypto

| API | Supported | Notes |
| :------------------------- | :-------- | :----------- |
| `crypto.getRandomValues()` || **complete** |
| `crypto.randomUUID()` || **complete** |

#### SubtleCrypto

| API | Supported | Notes |
| :---------------------------------------------------------------------------- | :-------- | :------------------------------------------------------- |
| `crypto.subtle.digest(algorithm)` || **complete** |
| `crypto.subtle.generateKey(algorithm, extractable, keyUsages)` || **limited to** AES-CBC, AES-GCM, and AES-CTR algorithms. |
| `crypto.subtle.importKey(format, keyData, algorithm, extractable, keyUsages)` || **limited to** AES-CBC, AES-GCM, and AES-CTR algorithms. |
| `crypto.subtle.exportKey(format, key)` || **limited to** AES-CBC, AES-GCM, and AES-CTR algorithms. |
| `crypto.subtle.encrypt(algorithm, key, data)` || **limited to** AES-CBC, AES-GCM, and AES-CTR algorithms. |
| `crypto.subtle.decrypt(algorithm, key, data)` || **limited to** AES-CBC, AES-GCM, and AES-CTR algorithms. |
| `crypto.subtle.deriveBits()` || |
| `crypto.subtle.deriveKey()` || |
| `crypto.subtle.sign()` || |
| `crypto.subtle.verify()` || |
| `crypto.subtle.wrapKey()` || |
| `crypto.subtle.unwrapKey()` || |


### APIs and algorithms with limited support

- **AES-KW**: in the current state of things, this module does not support the AES-KW (JSON Key Wrap) algorithm. The reason for it is that the Go standard library does not support it. We are looking into alternatives, but for now, this is a limitation of the module.
- **AES-GCM**: although the algorithm is supported, and can already be used, it is not fully compliant with the specification. The reason for this is that the Go standard library only supports a 12-byte nonce/iv, while the specification allows for a wider range of sizes. We do not expect to address this limitation unless the Go standard library adds support for it.

## Contributing

Contributions are welcome!

### Practices

Contributors will likely notice that the codebase is annotated with comments of the form `// {some number}.`. Those comments are used to track the progress of the implementation of the specification. The numbers are the section numbers of the specification. For example, the comment `// 8.` in the `SubtleCrypto.GenerateKey` function refers to the [step 8 of the `generateKey` algorithm from the specification](https://www.w3.org/TR/WebCryptoAPI/#SubtleCrypto-method-generateKey).

Following this convention allows us to document why certain operations are made in a certain way, and to track the progress of the implementation. We do not always add them, but we try to do so when it makes sense, and encourage contributors to do the same.
17 changes: 17 additions & 0 deletions examples/generateKey/generateKey-aes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { crypto } from "k6/x/webcrypto";

export default async function () {
const key = await crypto.subtle.generateKey(
{
name: "AES-CBC",
length: 256
},
true,
[
"encrypt",
"decrypt",
]
);

console.log(JSON.stringify(key))
}
30 changes: 16 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ module github.com/grafana/xk6-webcrypto
go 1.19

require (
github.com/dop251/goja v0.0.0-20220815083517-0c74f9139fd6
github.com/dop251/goja v0.0.0-20230128084908-78b980256d04
github.com/google/uuid v1.1.2
github.com/stretchr/testify v1.8.0
go.k6.io/k6 v0.40.0
go.k6.io/k6 v0.43.1
gopkg.in/guregu/null.v3 v3.3.0
)

Expand All @@ -17,27 +18,28 @@ require (
github.com/go-sourcemap/sourcemap v2.1.4-0.20211119122758-180fcef48034+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/klauspost/compress v1.15.7 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mccutchen/go-httpbin v1.1.2-0.20190116014521-c5cb2f4802fa // indirect
github.com/mstoykov/atlas v0.0.0-20220808085829-90340e9998bd // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.20.2 // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/afero v1.1.2 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be // indirect
golang.org/x/net v0.0.0-20221002022538-bcab6841153b // indirect
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
google.golang.org/genproto v0.0.0-20210604141403-392c879c8b08 // indirect
google.golang.org/grpc v1.47.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af // indirect
google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21 // indirect
google.golang.org/grpc v1.49.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 13857ba

Please sign in to comment.