Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyoshiaki committed Dec 12, 2023
1 parent 9f784b9 commit 440703a
Show file tree
Hide file tree
Showing 153 changed files with 877 additions and 783 deletions.
6 changes: 3 additions & 3 deletions packages/common/src/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function random32() {
export function bufferXor(a: Buffer, b: Buffer): Buffer {
if (a.length !== b.length) {
throw new TypeError(
"[webrtc-stun] You can not XOR buffers which length are different"
"[webrtc-stun] You can not XOR buffers which length are different",
);
}

Expand Down Expand Up @@ -71,7 +71,7 @@ export class BitWriter2 {
*/
constructor(
/**Max 32bit */
private bitLength: number
private bitLength: number,
) {
if (bitLength > 32) {
throw new Error();
Expand Down Expand Up @@ -116,7 +116,7 @@ export function paddingByte(bits: number) {
export function paddingBits(bits: number, expectLength: number) {
const dec = bits.toString(2);
return [...[...Array(expectLength - dec.length)].map(() => "0"), ...dec].join(
""
"",
);
}

Expand Down
14 changes: 7 additions & 7 deletions packages/common/src/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ export type InterfaceAddresses = {

export const interfaceAddress = (
type: SocketType,
interfaceAddresses: InterfaceAddresses | undefined
interfaceAddresses: InterfaceAddresses | undefined,
) => (interfaceAddresses ? interfaceAddresses[type] : undefined);

export async function randomPort(
protocol: SocketType = "udp4",
interfaceAddresses?: InterfaceAddresses
interfaceAddresses?: InterfaceAddresses,
) {
const socket = createSocket(protocol);

setImmediate(() =>
socket.bind({
port: 0,
address: interfaceAddress(protocol, interfaceAddresses),
})
}),
);

await new Promise<void>((r) => {
Expand All @@ -35,18 +35,18 @@ export async function randomPort(
export async function randomPorts(
num: number,
protocol: SocketType = "udp4",
interfaceAddresses?: InterfaceAddresses
interfaceAddresses?: InterfaceAddresses,
) {
return Promise.all(
[...Array(num)].map(() => randomPort(protocol, interfaceAddresses))
[...Array(num)].map(() => randomPort(protocol, interfaceAddresses)),
);
}

export async function findPort(
min: number,
max: number,
protocol: SocketType = "udp4",
interfaceAddresses?: InterfaceAddresses
interfaceAddresses?: InterfaceAddresses,
) {
let port: number | undefined;

Expand All @@ -57,7 +57,7 @@ export async function findPort(
socket.bind({
port: i,
address: interfaceAddress(protocol, interfaceAddresses),
})
}),
);

const err = await new Promise<Error | void>((r) => {
Expand Down
12 changes: 6 additions & 6 deletions packages/dtls/src/cipher/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ export const SignatureAlgorithm = {
ecdsa_3: 3,
} as const;
export type SignatureAlgorithms =
typeof SignatureAlgorithm[keyof typeof SignatureAlgorithm];
(typeof SignatureAlgorithm)[keyof typeof SignatureAlgorithm];

export const HashAlgorithm = {
sha256_4: 4,
} as const;
export type HashAlgorithms = typeof HashAlgorithm[keyof typeof HashAlgorithm];
export type HashAlgorithms = (typeof HashAlgorithm)[keyof typeof HashAlgorithm];

export type SignatureHash = {
hash: HashAlgorithms;
Expand All @@ -19,24 +19,24 @@ export const CipherSuite = {
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256_49195: 0xc02b, //49195,
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256_49199: 0xc02f, //49199
} as const;
export type CipherSuites = typeof CipherSuite[keyof typeof CipherSuite];
export type CipherSuites = (typeof CipherSuite)[keyof typeof CipherSuite];
export const CipherSuiteList: CipherSuites[] = Object.values(CipherSuite);

export const NamedCurveAlgorithm = {
x25519_29: 29,
secp256r1_23: 23,
} as const;
export type NamedCurveAlgorithms =
typeof NamedCurveAlgorithm[keyof typeof NamedCurveAlgorithm];
(typeof NamedCurveAlgorithm)[keyof typeof NamedCurveAlgorithm];
export const NamedCurveAlgorithmList: NamedCurveAlgorithms[] =
Object.values(NamedCurveAlgorithm);

export const CurveType = { named_curve_3: 3 } as const;
export type CurveTypes = typeof CurveType[keyof typeof CurveType];
export type CurveTypes = (typeof CurveType)[keyof typeof CurveType];

export const SignatureScheme = {
rsa_pkcs1_sha256: 0x0401,
ecdsa_secp256r1_sha256: 0x0403,
} as const;
export type SignatureSchemes =
typeof SignatureScheme[keyof typeof SignatureScheme];
(typeof SignatureScheme)[keyof typeof SignatureScheme];
22 changes: 11 additions & 11 deletions packages/dtls/src/cipher/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function createCipher(cipher: number) {
"TLS_RSA_WITH_AES_128_GCM_SHA256",
"aes-128-gcm",
RSA_KEY_EXCHANGE,
AEAD_AES_128_GCM
AEAD_AES_128_GCM,
);
case cipherSuites.TLS_RSA_WITH_AES_256_GCM_SHA384:
return createAEADCipher(
Expand All @@ -75,15 +75,15 @@ export function createCipher(cipher: number) {
"aes-256-gcm",
RSA_KEY_EXCHANGE,
AEAD_AES_256_GCM,
"sha384"
"sha384",
);
case cipherSuites.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
return createAEADCipher(
cipherSuites.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
"aes-128-gcm",
ECDHE_RSA_KEY_EXCHANGE,
AEAD_AES_128_GCM
AEAD_AES_128_GCM,
);
case cipherSuites.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
return createAEADCipher(
Expand All @@ -92,15 +92,15 @@ export function createCipher(cipher: number) {
"aes-256-gcm",
ECDHE_RSA_KEY_EXCHANGE,
AEAD_AES_256_GCM,
"sha384"
"sha384",
);
case cipherSuites.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
return createAEADCipher(
cipherSuites.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
"aes-128-gcm",
ECDHE_ECDSA_KEY_EXCHANGE,
AEAD_AES_128_GCM
AEAD_AES_128_GCM,
);
case cipherSuites.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
return createAEADCipher(
Expand All @@ -109,7 +109,7 @@ export function createCipher(cipher: number) {
"aes-256-gcm",
ECDHE_ECDSA_KEY_EXCHANGE,
AEAD_AES_256_GCM,
"sha384"
"sha384",
);
case cipherSuites.TLS_PSK_WITH_AES_128_GCM_SHA256:
return createAEADCipher(
Expand All @@ -118,7 +118,7 @@ export function createCipher(cipher: number) {
"aes-128-gcm",
PSK_KEY_EXCHANGE,
AEAD_AES_128_GCM,
"sha256"
"sha256",
);
case cipherSuites.TLS_PSK_WITH_AES_256_GCM_SHA384:
return createAEADCipher(
Expand All @@ -127,7 +127,7 @@ export function createCipher(cipher: number) {
"aes-256-gcm",
PSK_KEY_EXCHANGE,
AEAD_AES_256_GCM,
"sha384"
"sha384",
);
case cipherSuites.TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256:
return createAEADCipher(
Expand All @@ -136,7 +136,7 @@ export function createCipher(cipher: number) {
"aes-128-gcm",
ECDHE_PSK_KEY_EXCHANGE,
AEAD_AES_128_GCM,
"sha256"
"sha256",
);
case cipherSuites.TLS_ECDHE_PSK_WITH_AES_256_GCM_SHA384:
return createAEADCipher(
Expand All @@ -145,7 +145,7 @@ export function createCipher(cipher: number) {
"aes-256-gcm",
ECDHE_PSK_KEY_EXCHANGE,
AEAD_AES_256_GCM,
"sha384"
"sha384",
);
default:
break;
Expand All @@ -169,7 +169,7 @@ export function createAEADCipher(
block: string,
kx: KeyExchange,
constants: { K_LEN: number; N_MAX: number },
hash = "sha256"
hash = "sha256",
) {
const cipher = new AEADCipher();

Expand Down
2 changes: 1 addition & 1 deletion packages/dtls/src/cipher/namedCurve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface NamedCurveKeyPair {
}

export function generateKeyPair(
namedCurve: NamedCurveAlgorithms
namedCurve: NamedCurveAlgorithms,
): NamedCurveKeyPair {
switch (namedCurve) {
case NamedCurveAlgorithm.secp256r1_23: {
Expand Down
20 changes: 10 additions & 10 deletions packages/dtls/src/cipher/prf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { NamedCurveAlgorithm, NamedCurveAlgorithms } from "./const";
export function prfPreMasterSecret(
publicKey: Buffer,
privateKey: Buffer,
curve: NamedCurveAlgorithms
curve: NamedCurveAlgorithms,
) {
switch (curve) {
case NamedCurveAlgorithm.secp256r1_23:
Expand All @@ -35,7 +35,7 @@ export function prfPHash(
secret: Buffer,
seed: Buffer,
requestedLegth: number,
algorithm = "sha256"
algorithm = "sha256",
) {
const totalLength = requestedLegth;
const bufs: Buffer[] = [];
Expand All @@ -55,7 +55,7 @@ export function prfPHash(
export function prfMasterSecret(
preMasterSecret: Buffer,
clientRandom: Buffer,
serverRandom: Buffer
serverRandom: Buffer,
) {
const seed = Buffer.concat([
Buffer.from("master secret"),
Expand All @@ -67,14 +67,14 @@ export function prfMasterSecret(

export function prfExtendedMasterSecret(
preMasterSecret: Buffer,
handshakes: Buffer
handshakes: Buffer,
) {
const sessionHash = hash("sha256", handshakes);
const label = "extended master secret";
return prfPHash(
preMasterSecret,
Buffer.concat([Buffer.from(label), sessionHash]),
48
48,
);
}

Expand All @@ -84,7 +84,7 @@ export function exportKeyingMaterial(
masterSecret: Buffer,
localRandom: Buffer,
remoteRandom: Buffer,
isClient: boolean
isClient: boolean,
) {
const clientRandom = isClient ? localRandom : remoteRandom;
const serverRandom = isClient ? remoteRandom : localRandom;
Expand All @@ -100,13 +100,13 @@ export function prfVerifyData(
masterSecret: Buffer,
handshakes: Buffer,
label: string,
size = 12
size = 12,
) {
const bytes = hash("sha256", handshakes);
return prfPHash(
masterSecret,
Buffer.concat([Buffer.from(label), bytes]),
size
size,
);
}

Expand All @@ -125,7 +125,7 @@ export function prfEncryptionKeys(
prfKeyLen: number,
prfIvLen: number,
prfNonceLen: number,
algorithm = "sha256"
algorithm = "sha256",
) {
const size = prfKeyLen * 2 + prfIvLen * 2;
const secret = masterSecret;
Expand All @@ -134,7 +134,7 @@ export function prfEncryptionKeys(
secret,
Buffer.concat([Buffer.from("key expansion"), seed]),
size,
algorithm
algorithm,
);
const stream = createDecode(keyBlock);

Expand Down
2 changes: 1 addition & 1 deletion packages/dtls/src/cipher/suites/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const SessionType = {
CLIENT: 1,
SERVER: 2,
} as const;
export type SessionTypes = typeof SessionType[keyof typeof SessionType];
export type SessionTypes = (typeof SessionType)[keyof typeof SessionType];

export default abstract class AbstractCipher {
id = 0;
Expand Down
10 changes: 5 additions & 5 deletions packages/dtls/src/cipher/suites/aead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const AEADAdditionalData = {
};

const err = debug(
"werift-dtls : packages/dtls/src/cipher/suites/aead.ts : err"
"werift-dtls : packages/dtls/src/cipher/suites/aead.ts : err",
);

/**
Expand Down Expand Up @@ -59,7 +59,7 @@ export default class AEADCipher extends Cipher {
this.keyLength,
this.ivLength,
this.nonceLength,
this.hashAlgorithm
this.hashAlgorithm,
);

this.clientWriteKey = keys.clientWriteKey;
Expand Down Expand Up @@ -98,7 +98,7 @@ export default class AEADCipher extends Cipher {
iv,
{
authTagLength: this.authTagLength,
}
},
);

cipher.setAAD(additionalBuffer, {
Expand Down Expand Up @@ -145,7 +145,7 @@ export default class AEADCipher extends Cipher {
iv,
{
authTagLength: this.authTagLength,
}
},
);

decipher.setAuthTag(authTag);
Expand All @@ -166,7 +166,7 @@ export default class AEADCipher extends Cipher {
type,
dumpBuffer(data),
header,
this.summary
this.summary,
);
throw error;
}
Expand Down
Loading

0 comments on commit 440703a

Please sign in to comment.