aboutsummaryrefslogtreecommitdiff
path: root/src/crypto
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2020-04-07 13:37:32 +0530
committerFlorian Dold <florian.dold@gmail.com>2020-04-07 13:37:32 +0530
commitfb2e2f89935240666de66e4b2c11125cb3b2943d (patch)
tree7b7e148e6cce7bf7639a5e35102f5269f5920ab5 /src/crypto
parent1471aae8927c20d646cc2aa5ab0e20c1a7f2c0ca (diff)
more lint fixes
Diffstat (limited to 'src/crypto')
-rw-r--r--src/crypto/primitives/kdf.ts4
-rw-r--r--src/crypto/primitives/sha256.ts10
-rw-r--r--src/crypto/talerCrypto.ts6
-rw-r--r--src/crypto/workers/cryptoApi.ts50
-rw-r--r--src/crypto/workers/cryptoImplementation.ts31
-rw-r--r--src/crypto/workers/nodeThreadWorker.ts20
-rw-r--r--src/crypto/workers/synchronousWorker.ts12
7 files changed, 58 insertions, 75 deletions
diff --git a/src/crypto/primitives/kdf.ts b/src/crypto/primitives/kdf.ts
index 03deb3727..edc681bc1 100644
--- a/src/crypto/primitives/kdf.ts
+++ b/src/crypto/primitives/kdf.ts
@@ -51,11 +51,11 @@ export function hmac(
return digest(b2);
}
-export function hmacSha512(key: Uint8Array, message: Uint8Array) {
+export function hmacSha512(key: Uint8Array, message: Uint8Array): Uint8Array {
return hmac(sha512, 128, key, message);
}
-export function hmacSha256(key: Uint8Array, message: Uint8Array) {
+export function hmacSha256(key: Uint8Array, message: Uint8Array): Uint8Array {
return hmac(sha256, 64, key, message);
}
diff --git a/src/crypto/primitives/sha256.ts b/src/crypto/primitives/sha256.ts
index ed88b5ffd..97723dbfc 100644
--- a/src/crypto/primitives/sha256.ts
+++ b/src/crypto/primitives/sha256.ts
@@ -215,7 +215,7 @@ export class HashSha256 {
}
// Cleans internal buffers and re-initializes hash state.
- clean() {
+ clean(): void {
for (let i = 0; i < this.buffer.length; i++) {
this.buffer[i] = 0;
}
@@ -306,14 +306,14 @@ export class HashSha256 {
}
// Internal function for use in HMAC for optimization.
- _saveState(out: Uint32Array) {
+ _saveState(out: Uint32Array): void {
for (let i = 0; i < this.state.length; i++) {
out[i] = this.state[i];
}
}
// Internal function for use in HMAC for optimization.
- _restoreState(from: Uint32Array, bytesHashed: number) {
+ _restoreState(from: Uint32Array, bytesHashed: number): void {
for (let i = 0; i < this.state.length; i++) {
this.state[i] = from[i];
}
@@ -376,7 +376,7 @@ export class HMAC {
}
// Cleans HMAC state.
- clean() {
+ clean(): void {
for (let i = 0; i < this.istate.length; i++) {
this.ostate[i] = this.istate[i] = 0;
}
@@ -418,7 +418,7 @@ export function sha256(data: Uint8Array): Uint8Array {
}
// Returns HMAC-SHA256 of data under the key.
-export function hmacSha256(key: Uint8Array, data: Uint8Array) {
+export function hmacSha256(key: Uint8Array, data: Uint8Array): Uint8Array {
const h = new HMAC(key).update(data);
const digest = h.digest();
h.clean();
diff --git a/src/crypto/talerCrypto.ts b/src/crypto/talerCrypto.ts
index 3da4f4a47..457009a04 100644
--- a/src/crypto/talerCrypto.ts
+++ b/src/crypto/talerCrypto.ts
@@ -191,12 +191,12 @@ function kdfMod(
}
}
-export function stringToBytes(s: string) {
+export function stringToBytes(s: string): Uint8Array {
const te = new TextEncoder();
return te.encode(s);
}
-function loadBigInt(arr: Uint8Array) {
+function loadBigInt(arr: Uint8Array): bigint.BigInteger {
return bigint.fromArray(Array.from(arr), 256, false);
}
@@ -219,7 +219,7 @@ function rsaBlindingKeyDerive(
* @param r KDF result
* @param n RSA modulus of the public key
*/
-function rsaGcdValidate(r: bigint.BigInteger, n: bigint.BigInteger) {
+function rsaGcdValidate(r: bigint.BigInteger, n: bigint.BigInteger): void {
const t = bigint.gcd(r, n);
if (!t.equals(bigint.one)) {
throw Error("malicious RSA public key");
diff --git a/src/crypto/workers/cryptoApi.ts b/src/crypto/workers/cryptoApi.ts
index 24a43ff47..456bf309e 100644
--- a/src/crypto/workers/cryptoApi.ts
+++ b/src/crypto/workers/cryptoApi.ts
@@ -34,13 +34,7 @@ import {
import { CryptoWorker } from "./cryptoWorker";
-import {
- RecoupRequest,
- CoinDepositPermission,
- RecoupConfirmation,
- ExchangeSignKeyJson,
- EddsaPublicKeyString,
-} from "../../types/talerTypes";
+import { RecoupRequest, CoinDepositPermission } from "../../types/talerTypes";
import {
BenchmarkResult,
@@ -154,7 +148,7 @@ export class CryptoApi {
/**
* Terminate all worker threads.
*/
- terminateWorkers() {
+ terminateWorkers(): void {
for (const worker of this.workers) {
if (worker.w) {
CryptoApi.enableTracing && console.log("terminating worker");
@@ -172,7 +166,7 @@ export class CryptoApi {
}
}
- stop() {
+ stop(): void {
this.terminateWorkers();
this.stopped = true;
}
@@ -192,11 +186,14 @@ export class CryptoApi {
}
ws.currentWorkItem = work;
this.numBusy++;
+ let worker: CryptoWorker;
if (!ws.w) {
- const w = this.workerFactory.startWorker();
- w.onmessage = (m: MessageEvent) => this.handleWorkerMessage(ws, m);
- w.onerror = (e: ErrorEvent) => this.handleWorkerError(ws, e);
- ws.w = w;
+ worker = this.workerFactory.startWorker();
+ worker.onmessage = (m: MessageEvent) => this.handleWorkerMessage(ws, m);
+ worker.onerror = (e: ErrorEvent) => this.handleWorkerError(ws, e);
+ ws.w = worker;
+ } else {
+ worker = ws.w;
}
const msg: any = {
@@ -206,28 +203,28 @@ export class CryptoApi {
};
this.resetWorkerTimeout(ws);
work.startTime = timer.performanceNow();
- setTimeout(() => ws.w!.postMessage(msg), 0);
+ setTimeout(() => worker.postMessage(msg), 0);
}
- resetWorkerTimeout(ws: WorkerState) {
+ resetWorkerTimeout(ws: WorkerState): void {
if (ws.terminationTimerHandle !== null) {
ws.terminationTimerHandle.clear();
ws.terminationTimerHandle = null;
}
- const destroy = () => {
+ const destroy = (): void => {
// terminate worker if it's idle
if (ws.w && ws.currentWorkItem === null) {
- ws.w!.terminate();
+ ws.w.terminate();
ws.w = null;
}
};
ws.terminationTimerHandle = timer.after(15 * 1000, destroy);
}
- handleWorkerError(ws: WorkerState, e: ErrorEvent) {
+ handleWorkerError(ws: WorkerState, e: ErrorEvent): void {
if (ws.currentWorkItem) {
console.error(
- `error in worker during ${ws.currentWorkItem!.operation}`,
+ `error in worker during ${ws.currentWorkItem.operation}`,
e,
);
} else {
@@ -235,8 +232,10 @@ export class CryptoApi {
}
console.error(e.message);
try {
- ws.w!.terminate();
- ws.w = null;
+ if (ws.w) {
+ ws.w.terminate();
+ ws.w = null;
+ }
} catch (e) {
console.error(e);
}
@@ -248,19 +247,22 @@ export class CryptoApi {
this.findWork(ws);
}
- private findWork(ws: WorkerState) {
+ private findWork(ws: WorkerState): void {
// try to find more work for this worker
for (let i = 0; i < NUM_PRIO; i++) {
const q = this.workQueues[NUM_PRIO - i - 1];
if (q.length !== 0) {
- const work: WorkItem = q.shift()!;
+ const work: WorkItem | undefined = q.shift();
+ if (!work) {
+ continue;
+ }
this.wake(ws, work);
return;
}
}
}
- handleWorkerMessage(ws: WorkerState, msg: MessageEvent) {
+ handleWorkerMessage(ws: WorkerState, msg: MessageEvent): void {
const id = msg.data.id;
if (typeof id !== "number") {
console.error("rpc id must be number");
diff --git a/src/crypto/workers/cryptoImplementation.ts b/src/crypto/workers/cryptoImplementation.ts
index 0a3c217ab..96ad29bc0 100644
--- a/src/crypto/workers/cryptoImplementation.ts
+++ b/src/crypto/workers/cryptoImplementation.ts
@@ -36,13 +36,7 @@ import {
CoinSourceType,
} from "../../types/dbTypes";
-import {
- CoinDepositPermission,
- RecoupRequest,
- RecoupConfirmation,
- ExchangeSignKeyJson,
- EddsaPublicKeyString,
-} from "../../types/talerTypes";
+import { CoinDepositPermission, RecoupRequest } from "../../types/talerTypes";
import {
BenchmarkResult,
PlanchetCreationResult,
@@ -70,11 +64,7 @@ import {
} from "../talerCrypto";
import { randomBytes } from "../primitives/nacl-fast";
import { kdf } from "../primitives/kdf";
-import {
- Timestamp,
- getTimestampNow,
- timestampIsBetween,
-} from "../../util/time";
+import { Timestamp, getTimestampNow } from "../../util/time";
enum SignaturePurpose {
RESERVE_WITHDRAW = 1200,
@@ -144,24 +134,9 @@ function buildSigPS(purposeNum: number): SignaturePurposeBuilder {
return new SignaturePurposeBuilder(purposeNum);
}
-function checkSignKeyOkay(
- key: string,
- exchangeKeys: ExchangeSignKeyJson[],
-): boolean {
- const now = getTimestampNow();
- for (const k of exchangeKeys) {
- if (k.key == key) {
- return timestampIsBetween(now, k.stamp_start, k.stamp_end);
- }
- }
- return false;
-}
-
export class CryptoImplementation {
static enableTracing = false;
- constructor() {}
-
/**
* Create a pre-coin of the given denomination to be withdrawn from then given
* reserve.
@@ -535,7 +510,7 @@ export class CryptoImplementation {
let time_eddsa_create = 0;
for (let i = 0; i < repetitions; i++) {
const start = timer.performanceNow();
- const pair = createEddsaKeyPair();
+ createEddsaKeyPair();
time_eddsa_create += timer.performanceNow() - start;
}
diff --git a/src/crypto/workers/nodeThreadWorker.ts b/src/crypto/workers/nodeThreadWorker.ts
index d289c14b2..6c9dfc569 100644
--- a/src/crypto/workers/nodeThreadWorker.ts
+++ b/src/crypto/workers/nodeThreadWorker.ts
@@ -1,5 +1,3 @@
-import { CryptoWorkerFactory } from "./cryptoApi";
-
/*
This file is part of TALER
(C) 2016 GNUnet e.V.
@@ -16,8 +14,10 @@ import { CryptoWorkerFactory } from "./cryptoApi";
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
-// tslint:disable:no-var-requires
-
+/**
+ * Imports
+ */
+import { CryptoWorkerFactory } from "./cryptoApi";
import { CryptoWorker } from "./cryptoWorker";
import os from "os";
import { CryptoImplementation } from "./cryptoImplementation";
@@ -55,7 +55,7 @@ const workerCode = `
* This function is executed in the worker thread to handle
* a message.
*/
-export function handleWorkerMessage(msg: any) {
+export function handleWorkerMessage(msg: any): void {
const args = msg.args;
if (!Array.isArray(args)) {
console.error("args must be array");
@@ -72,7 +72,7 @@ export function handleWorkerMessage(msg: any) {
return;
}
- const handleRequest = async () => {
+ const handleRequest = async (): Promise<void> => {
const impl = new CryptoImplementation();
if (!(operation in impl)) {
@@ -82,6 +82,7 @@ export function handleWorkerMessage(msg: any) {
try {
const result = (impl as any)[operation](...args);
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
const worker_threads = require("worker_threads");
const p = worker_threads.parentPort;
worker_threads.parentPort?.postMessage;
@@ -101,7 +102,7 @@ export function handleWorkerMessage(msg: any) {
});
}
-export function handleWorkerError(e: Error) {
+export function handleWorkerError(e: Error): void {
console.log("got error from worker", e);
}
@@ -135,6 +136,7 @@ class NodeThreadCryptoWorker implements CryptoWorker {
private nodeWorker: import("worker_threads").Worker;
constructor() {
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
const worker_threads = require("worker_threads");
this.nodeWorker = new worker_threads.Worker(workerCode, { eval: true });
this.nodeWorker.on("error", (err: Error) => {
@@ -168,14 +170,14 @@ class NodeThreadCryptoWorker implements CryptoWorker {
/**
* Send a message to the worker thread.
*/
- postMessage(msg: any) {
+ postMessage(msg: any): void {
this.nodeWorker.postMessage(msg);
}
/**
* Forcibly terminate the worker thread.
*/
- terminate() {
+ terminate(): void {
this.nodeWorker.terminate();
}
}
diff --git a/src/crypto/workers/synchronousWorker.ts b/src/crypto/workers/synchronousWorker.ts
index f4bcf396c..2cc740975 100644
--- a/src/crypto/workers/synchronousWorker.ts
+++ b/src/crypto/workers/synchronousWorker.ts
@@ -70,13 +70,17 @@ export class SynchronousCryptoWorker {
}
}
- private dispatchMessage(msg: any) {
+ private dispatchMessage(msg: any): void {
if (this.onmessage) {
this.onmessage({ data: msg });
}
}
- private async handleRequest(operation: string, id: number, args: string[]) {
+ private async handleRequest(
+ operation: string,
+ id: number,
+ args: string[],
+ ): Promise<void> {
const impl = new CryptoImplementation();
if (!(operation in impl)) {
@@ -102,7 +106,7 @@ export class SynchronousCryptoWorker {
/**
* Send a message to the worker thread.
*/
- postMessage(msg: any) {
+ postMessage(msg: any): void {
const args = msg.args;
if (!Array.isArray(args)) {
console.error("args must be array");
@@ -127,7 +131,7 @@ export class SynchronousCryptoWorker {
/**
* Forcibly terminate the worker thread.
*/
- terminate() {
+ terminate(): void {
// This is a no-op.
}
}