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

Support for 25519 algos #4556

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ all: clean format tests build
build:
go build

build-debug:
go build -gcflags="all=-N -l"

## format: Applies Go formatting to code.
format:
find . -name '*.go' -exec gofmt -s -w {} +
Expand Down
11 changes: 11 additions & 0 deletions examples/webcrypto/generateKey/generateKey-ed25519.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default async function () {
const ed25519KeyPair = await crypto.subtle.generateKey(
{
name: "Ed25519",
},
true,
["sign", "verify"]
);

console.log("ed25519 key pair: " + JSON.stringify(ed25519KeyPair));
}
12 changes: 12 additions & 0 deletions examples/webcrypto/generateKey/generateKey-x25519.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default async function () {
const key = await crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "X25519",
},
true,
["deriveKey", "deriveBits"]
);

console.log(JSON.stringify(key));
}
33 changes: 33 additions & 0 deletions examples/webcrypto/import_export/export-ed25519-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export default async function () {
const generatedKeyPair = await crypto.subtle.generateKey(
{
name: "Ed25519",
},
true,
["sign", "verify"]
);

const exportedPrivateKey = await crypto.subtle.exportKey(
"pkcs8",
generatedKeyPair.privateKey
);
console.log("exported private key: " + printArrayBuffer(exportedPrivateKey));

const exportedRawPublicKey = await crypto.subtle.exportKey(
"raw",
generatedKeyPair.publicKey
);

const exportedSpkiPublicKey = await crypto.subtle.exportKey(
"spki",
generatedKeyPair.publicKey
);

console.log("exported public key: " + printArrayBuffer(exportedRawPublicKey));
console.log("exported spki public key: " + printArrayBuffer(exportedSpkiPublicKey));
}

const printArrayBuffer = (buffer) => {
let view = new Uint8Array(buffer);
return Array.from(view);
};
33 changes: 33 additions & 0 deletions examples/webcrypto/import_export/import-ed25519-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export default async function () {
const publicKey = await crypto.subtle.importKey(
"raw",
aliceRawPublicKeyData,
{ name: "Ed25519" },
true,
["verify"]
);

const privateKey = await crypto.subtle.importKey(
"pkcs8",
alicePkcs8PrivateKeyData,
{ name: "Ed25519" },
true,
["sign"]
);

const spkiPublicKey = await crypto.subtle.importKey(
"spki",
spkiPublicKeyData,
{ name: "Ed25519" },
true,
["verify"]
);

console.log("raw public key: ", JSON.stringify(publicKey));
console.log("pkcs8 private key: ", JSON.stringify(privateKey));
console.log("spki public key: ", JSON.stringify(spkiPublicKey));
}

const aliceRawPublicKeyData = new Uint8Array([20,143,11,228,219,143,240,246,228,95,189,140,34,196,138,241,105,163,220,110,81,16,167,243,77,251,70,100,130,131,153,43])
const alicePkcs8PrivateKeyData = new Uint8Array([48,46,2,1,0,48,5,6,3,43,101,112,4,34,4,32,235,89,226,177,105,103,230,133,229,2,157,78,107,14,0,197,81,149,209,139,6,37,80,98,219,50,0,38,144,234,156,194])
const spkiPublicKeyData = new Uint8Array([48,42,48,5,6,3,43,101,112,3,33,0,210,238,42,158,126,130,110,253,80,77,38,242,209,88,172,114,11,120,31,243,24,171,47,144,217,186,184,71,152,40,110,168])
43 changes: 43 additions & 0 deletions examples/webcrypto/import_export/import-export-jwk-ed25519.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
export default async function () {
const publicJwk = {
"kty":"OKP",
"crv":"Ed25519",
"x":"o7RbBVJW_6Ua3h5J3MCEGAeXRC6xHvtotIiAadK-xbM",
"key_ops":["verify"],
"ext":true
};

const privateJwk = {
"kty": "OKP",
crv: "Ed25519",
x: "o7RbBVJW_6Ua3h5J3MCEGAeXRC6xHvtotIiAadK-xbM",
d: "lHnUA3j3VmVOCYuF4nzEgbQ9QnaBNXXTLIK45adoyEmjtFsFUlb_pRreHkncwIQYB5dELrEe-2i0iIBp0r7Fsw",
key_ops: ["sign"],
ext: true
}

const publicKey = await crypto.subtle.importKey(
"jwk",
publicJwk,
{ name: "Ed25519" },
true,
["verify"]
);

const privateKey = await crypto.subtle.importKey(
"jwk",
privateJwk,
{ name: "Ed25519" },
true,
["sign"]
);

console.log("public key: " + JSON.stringify(publicKey));
console.log("private key: " + JSON.stringify(privateKey));

const exportedPublicJwk = await crypto.subtle.exportKey("jwk", publicKey);
console.log("exported public jwk: " + JSON.stringify(exportedPublicJwk));

const exportedPrivateJwk = await crypto.subtle.exportKey("jwk", privateKey);
console.log("exported private jwk: " + JSON.stringify(exportedPrivateJwk));
}
44 changes: 44 additions & 0 deletions examples/webcrypto/sign_verify/sign-verify-ed25519.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { crypto } from "k6/experimental/webcrypto";

export default async function () {
const keyPair = await crypto.subtle.generateKey(
{
name: "Ed25519",
},
true,
["sign", "verify"]
);

const data = string2ArrayBuffer("Hello World");

const alg = { name: "Ed25519" };

// makes a signature of the encoded data with the provided key
const signature = await crypto.subtle.sign(alg, keyPair.privateKey, data);

console.log("signature: ", printArrayBuffer(signature));

//Verifies the signature of the encoded data with the provided key
const verified = await crypto.subtle.verify(
alg,
otherKeyPair.publicKey,
signature,
data
);

console.log("verified: ", verified);
}

const string2ArrayBuffer = (str) => {
let buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
let bufView = new Uint16Array(buf);
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
};

const printArrayBuffer = (buffer) => {
let view = new Uint8Array(buffer);
return Array.from(view);
};
16 changes: 12 additions & 4 deletions internal/js/modules/k6/webcrypto/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@

// ECDH represents the ECDH algorithm.
ECDH = "ECDH"

// Ed25519 represents the Ed25519 algorithm.
Ed25519 = "ED25519" // TODO: This should be "Ed25519"

// X25519 represents the X25519 algorithm.
X25519 = "X25519"
)

// HashAlgorithmIdentifier represents the name of a hash algorithm.
Expand Down Expand Up @@ -187,16 +193,18 @@
isHashAlgorithm(algorithmName) ||
algorithmName == HMAC ||
isEllipticCurve(algorithmName) ||
isRSAAlgorithm(algorithmName)
isRSAAlgorithm(algorithmName) ||
algorithmName == Ed25519
case OperationIdentifierExportKey, OperationIdentifierImportKey:
return isAesAlgorithm(algorithmName) ||
algorithmName == HMAC ||
isEllipticCurve(algorithmName) ||
isRSAAlgorithm(algorithmName)
isRSAAlgorithm(algorithmName) ||
algorithmName == Ed25519
case OperationIdentifierEncrypt, OperationIdentifierDecrypt:
return isAesAlgorithm(algorithmName) || algorithmName == RSAOaep
case OperationIdentifierSign, OperationIdentifierVerify:
return algorithmName == HMAC || algorithmName == ECDSA || algorithmName == RSAPss || algorithmName == RSASsaPkcs1v15
return algorithmName == HMAC || algorithmName == ECDSA || algorithmName == RSAPss || algorithmName == RSASsaPkcs1v15 || algorithmName == Ed25519

Check failure on line 207 in internal/js/modules/k6/webcrypto/algorithm.go

View workflow job for this annotation

GitHub Actions / lint

The line is 146 characters long, which exceeds the maximum of 120 characters. (lll)
default:
return false
}
Expand All @@ -221,5 +229,5 @@
}

func isEllipticCurve(algorithmName string) bool {
return algorithmName == ECDH || algorithmName == ECDSA
return algorithmName == ECDH || algorithmName == ECDSA || algorithmName == X25519
}
Loading
Loading