Skip to content

Commit

Permalink
Testing fake logins.
Browse files Browse the repository at this point in the history
  • Loading branch information
armfazh committed Jun 28, 2024
1 parent e924b7e commit 884f807
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 124 deletions.
6 changes: 6 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Oprf,
SuiteID,
deriveKeyPair,
generateKeyPair,
getKeySizes
} from '@cloudflare/voprf-ts'
import { CredentialResponse, KE1 } from './messages.js'
Expand Down Expand Up @@ -235,4 +236,9 @@ export class AKE3DH implements AKEFn {
)
return { private_key: keypair.privateKey, public_key: keypair.publicKey }
}

async generateDHKeyPair(): Promise<AKEKeyPair> {
const keypair = await generateKeyPair(this.suiteID)
return { private_key: keypair.privateKey, public_key: keypair.publicKey }
}
}
10 changes: 4 additions & 6 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,11 @@ export class RegistrationRecord extends Serializable {
return new RegistrationRecord(cfg, client_public_key, masking_key, envelope)
}

static async createFake(cfg: Config): Promise<RegistrationRecord> {
const seed = cfg.prng.random(cfg.constants.Nseed)
const { public_key: client_public_key } = await cfg.ake.deriveDHKeyPair(
new Uint8Array(seed)
)
static async createFakeRecord(cfg: Config): Promise<RegistrationRecord> {
const { public_key: client_public_key } = await cfg.ake.generateDHKeyPair()
const masking_key = new Uint8Array(cfg.prng.random(cfg.hash.Nh))
const envelope = Envelope.deserialize(cfg, new Array(Envelope.sizeSerialized(cfg)).fill(0))
const zero_envelope_bytes = new Array(Envelope.sizeSerialized(cfg))
const envelope = Envelope.deserialize(cfg, zero_envelope_bytes)

return new RegistrationRecord(cfg, client_public_key, masking_key, envelope)
}
Expand Down
6 changes: 1 addition & 5 deletions src/opaque_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ export class OpaqueClient implements RegistrationClient, AuthClient {
}
| Error
> {
if (
this.status !== OpaqueClient.States.REG_STARTED ||
typeof this.password === 'undefined' ||
typeof this.blind === 'undefined'
) {
if (this.status !== OpaqueClient.States.REG_STARTED || !this.password || !this.blind) {
return new Error('client not ready')
}
const te = new TextEncoder()
Expand Down
1 change: 1 addition & 0 deletions src/thecrypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export interface AKEFn {
readonly Nsk: number // Nsk: The size of AKE private keys.
readonly Npk: number // Npk: The size of AKE public keys.
deriveDHKeyPair(seed: Uint8Array): Promise<AKEKeyPair>
generateDHKeyPair(): Promise<AKEKeyPair>
}

export interface OPRFFn {
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function decode_vector_16(a: Uint8Array): {

export function checked_vector(a: Uint8Array, n: number, str = 'array'): Uint8Array {
if (a.length < n) {
throw new Error(`${str} has wrong length`)
throw new Error(`${str} has wrong length of ${a.length} expected ${n}`)
}
return a.slice(0, n)
}
Expand Down
21 changes: 21 additions & 0 deletions test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

export class KVStorage {
kvStorage: Map<string, Uint8Array>
default_key?: string

constructor() {
this.kvStorage = new Map<string, Uint8Array>()
Expand All @@ -22,6 +23,26 @@ export class KVStorage {
}
return false
}

set_default(k: string, v: Uint8Array): boolean {
const ok = this.store(k, v)
this.default_key = k
return ok
}

lookup_or_default(k: string): Uint8Array {
const err_msj = 'no default entry has been set'
if (!this.default_key) {
throw new Error(err_msj)
}

const v = this.kvStorage.get(k) ?? this.kvStorage.get(this.default_key)
if (!v) {
throw new Error(err_msj)
}

return v
}
}

export function fromHexString(x: string): string {
Expand Down
Binary file modified test/testdata/vectors_v16.json.gz
Binary file not shown.
Loading

0 comments on commit 884f807

Please sign in to comment.