Skip to content

Commit

Permalink
Fix: fix fromBytes static method, #1
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Apr 20, 2024
1 parent b672768 commit dc14a2c
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 23 deletions.
26 changes: 13 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/aesgcm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import { AesGcmKey } from './aesgcm'

describe('AesGcmKey Examples', () => {
it('AlgorithmA128GCM', async () => {
const key = AesGcmKey.fromSecret(
let key = AesGcmKey.fromSecret(
hexToBytes('849B57219DAE48DE646D07DBB533566E')
)
assert.equal(key.alg, iana.AlgorithmA128GCM)

const keyBytes = key.toBytes()
key = AesGcmKey.fromBytes(keyBytes)

const ciphertext = await key.encrypt(
utf8ToBytes('This is the content.'),
hexToBytes('02D1F7E6F26C43D4868D87CE'),
Expand Down
8 changes: 6 additions & 2 deletions src/aesgcm.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
// (c) 2023-present, LDC Labs. All rights reserved.
// See the file LICENSE for licensing terms.

import { gcm } from '@noble/ciphers/webcrypto/aes'
import { gcm } from '@noble/ciphers/webcrypto'
import * as iana from './iana'
import { RawMap, assertBytes } from './map'
import { Key, type Encryptor } from './key'
import { randomBytes } from './utils'
import { randomBytes, decodeCBOR } from './utils'

// TODO: more checks
// AesGcmKey implements content encryption algorithm AES-GCM for COSE as defined in RFC9053.
// https://datatracker.ietf.org/doc/html/rfc9053#name-aes-gcm.
export class AesGcmKey extends Key implements Encryptor {
static fromBytes(data: Uint8Array): AesGcmKey {
return new AesGcmKey(decodeCBOR(data))
}

static generate<T>(alg: number, kid?: T): AesGcmKey {
return AesGcmKey.fromSecret(randomBytes(getKeySize(alg)), kid)
}
Expand Down
5 changes: 4 additions & 1 deletion src/chacha20poly1305.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ describe('ChaCha20Poly1305Key Examples', () => {
// https://github.com/cose-wg/Examples/tree/master/chacha-poly-examples
// https://github.com/cose-wg/Examples/pull/104
it('Examples', async () => {
const key = ChaCha20Poly1305Key.fromSecret(
let key = ChaCha20Poly1305Key.fromSecret(
hexToBytes(
'0F1E2D3C4B5A69788796A5B4C3D2E1F01F2E3D4C5B6A798897A6B5C4D3E2F100'
)
)
assert.equal(key.alg, iana.AlgorithmChaCha20Poly1305)

const keyBytes = key.toBytes()
key = ChaCha20Poly1305Key.fromBytes(keyBytes)

const ciphertext = await key.encrypt(
utf8ToBytes('This is the content.'),
hexToBytes('26682306D4FB28CA01B43B80'),
Expand Down
6 changes: 5 additions & 1 deletion src/chacha20poly1305.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import { chacha20poly1305 } from '@noble/ciphers/chacha'
import * as iana from './iana'
import { RawMap, assertBytes } from './map'
import { Key, type Encryptor } from './key'
import { randomBytes } from './utils'
import { randomBytes, decodeCBOR } from './utils'

// TODO: more checks
// ChaCha20Poly1305Key implements content encryption algorithm ChaCha20/Poly1305 for COSE as defined in RFC9053.
// https://datatracker.ietf.org/doc/html/rfc9053#name-chacha20-and-poly1305.
export class ChaCha20Poly1305Key extends Key implements Encryptor {
static fromBytes(data: Uint8Array): ChaCha20Poly1305Key {
return new ChaCha20Poly1305Key(decodeCBOR(data))
}

static generate<T>(kid?: T): ChaCha20Poly1305Key {
return ChaCha20Poly1305Key.fromSecret(randomBytes(32), kid)
}
Expand Down
5 changes: 4 additions & 1 deletion src/ecdsa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ describe('ECDSAKey Examples', () => {
iana.AlgorithmES384,
iana.AlgorithmES512,
]) {
const key = ECDSAKey.generate(alg)
let key = ECDSAKey.generate(alg)
assert.equal(key.kty, iana.KeyTypeEC2)
assert.equal(key.alg, alg)
assert.equal(key.getInt(iana.EC2KeyParameterCrv), getCrv(alg))

const keyBytes = key.toBytes()
key = ECDSAKey.fromBytes(keyBytes)

const sig = key.sign(utf8ToBytes('This is the content.'))
assert.equal(key.verify(utf8ToBytes('This is the content.'), sig), true)
assert.equal(key.verify(utf8ToBytes('This is the content'), sig), false)
Expand Down
5 changes: 5 additions & 0 deletions src/ecdsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import { CurveFn } from '@noble/curves/abstract/weierstrass'
import * as iana from './iana'
import { RawMap, assertBytes } from './map'
import { Key, type Signer, Verifier } from './key'
import { decodeCBOR } from './utils'

// TODO: more checks
// ECDSAKey implements signature algorithm ECDSA for COSE as defined in RFC9053.
// https://datatracker.ietf.org/doc/html/rfc9053#name-ecdsa.
export class ECDSAKey extends Key implements Signer, Verifier {
static fromBytes(data: Uint8Array): ECDSAKey {
return new ECDSAKey(decodeCBOR(data))
}

static generate<T>(alg: number, kid?: T): ECDSAKey {
const curve = getCurve(alg)
return ECDSAKey.fromSecret(curve.utils.randomPrivateKey(), kid)
Expand Down
5 changes: 4 additions & 1 deletion src/ed25519.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import { Ed25519Key } from './ed25519'

describe('Ed25519Key Examples', () => {
it('Signer and Verifier', () => {
const key = Ed25519Key.generate()
let key = Ed25519Key.generate()
assert.equal(key.kty, iana.KeyTypeOKP)
assert.equal(key.alg, iana.AlgorithmEdDSA)
assert.equal(key.getInt(iana.OKPKeyParameterCrv), iana.EllipticCurveEd25519)

const keyBytes = key.toBytes()
key = Ed25519Key.fromBytes(keyBytes)

const sig = key.sign(utf8ToBytes('This is the content.'))
assert.equal(key.verify(utf8ToBytes('This is the content.'), sig), true)
assert.equal(key.verify(utf8ToBytes('This is the content'), sig), false)
Expand Down
6 changes: 5 additions & 1 deletion src/ed25519.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import { ed25519 } from '@noble/curves/ed25519'
import * as iana from './iana'
import { RawMap, assertBytes } from './map'
import { Key, type Signer, Verifier } from './key'
import { randomBytes } from './utils'
import { randomBytes, decodeCBOR } from './utils'

// TODO: more checks
// Ed25519Key implements signature algorithm Ed25519 for COSE as defined in RFC9053.
// https://datatracker.ietf.org/doc/html/rfc9053#name-edwards-curve-digital-signa.
export class Ed25519Key extends Key implements Signer, Verifier {
static fromBytes(data: Uint8Array): Ed25519Key {
return new Ed25519Key(decodeCBOR(data))
}

static generate<T>(kid?: T): Ed25519Key {
return Ed25519Key.fromSecret(randomBytes(32), kid)
}
Expand Down
4 changes: 3 additions & 1 deletion src/hmac.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ describe('HMACKey Examples', () => {
]

for (const [alg, secret, message, tag] of cases) {
const key = HMACKey.fromSecret(hexToBytes(secret), alg)
let key = HMACKey.fromSecret(hexToBytes(secret), alg)
const keyBytes = key.toBytes()
key = HMACKey.fromBytes(keyBytes)
assert.equal(bytesToHex(key.mac(hexToBytes(message))).toUpperCase(), tag)
}
})
Expand Down
7 changes: 6 additions & 1 deletion src/hmac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
import * as iana from './iana'
import { RawMap, assertBytes } from './map'
import { Key, type MACer } from './key'
import { randomBytes } from './utils'
import { randomBytes, decodeCBOR } from './utils'
import { hmac, getHash } from './hash'


// TODO: more checks
// HMACKey implements message authentication code algorithm HMAC for COSE as defined in RFC9053.
// https://datatracker.ietf.org/doc/html/rfc9053#name-hash-based-message-authenti.
export class HMACKey extends Key implements MACer {
static fromBytes(data: Uint8Array): HMACKey {
return new HMACKey(decodeCBOR(data))
}

static generate<T>(alg: number, kid?: T): HMACKey {
return HMACKey.fromSecret(randomBytes(getKeySize(alg)), alg, kid)
}
Expand Down

0 comments on commit dc14a2c

Please sign in to comment.