Skip to content

Commit

Permalink
Merge pull request #5 from caru-ini/fix/auth-random-fail
Browse files Browse the repository at this point in the history
ランダムに発生するログイン失敗のバグを修正
  • Loading branch information
solufa authored Jul 2, 2024
2 parents 1b31725 + 8183ee2 commit e0da1ef
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
7 changes: 0 additions & 7 deletions server/domain/user/service/srp/calcSessionKey.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import assert from 'assert';
import crypto from 'crypto';
import { BigInteger } from 'jsbn';
import { N, Nbytes } from './constants';
Expand All @@ -19,15 +18,9 @@ export const calculateSessionKey = (params: {
v: string;
}): Buffer => {
const Aint = new BigInteger(padHex(params.A), 16);
const Bint = new BigInteger(padHex(params.B), 16);
const bInt = new BigInteger(padHex(params.b), 16);
const vInt = new BigInteger(params.v, 16);

assert(Aint.compareTo(BigInteger.ZERO) > 0, 'A should be greater than 0');
assert(Aint.compareTo(N) < 0, 'A should be less than N');
assert(Bint.compareTo(BigInteger.ZERO) > 0, 'B should be greater than 0');
assert(Bint.compareTo(N) < 0, 'A should be less than N');

const scramblingParameter = calculateScramblingParameter(params.A, params.B);

// u = H(A,B) % N
Expand Down
15 changes: 15 additions & 0 deletions server/domain/user/service/srp/calcSrpA.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Buffer } from 'buffer';
import crypto from 'crypto';
import { N, g } from 'domain/user/service/srp/constants';
import { fromBuffer, toBuffer } from 'domain/user/service/srp/util';
import { BigInteger } from 'jsbn';

export const calculateSrpA = (): { a: Buffer; A: Buffer } => {
let a = Buffer.from([0]);
let AInt = BigInteger.ZERO;
while (AInt === BigInteger.ZERO) {
a = crypto.randomBytes(32);
AInt = g.modPow(fromBuffer(a), N);
}
return { a, A: toBuffer(AInt) };
};
13 changes: 10 additions & 3 deletions server/domain/user/service/srp/calcSrpB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ export const calculateSrpB = (
b: string;
B: string;
} => {
const b = crypto.randomBytes(32);
const bInt = fromBuffer(b);
let BInt = BigInteger.ZERO;
let b = Buffer.from([0]);
const vInt = new BigInteger(v, 16);

while (BInt === BigInteger.ZERO) {
b = crypto.randomBytes(32);
const bInt = fromBuffer(b);
BInt = multiplierParam.multiply(vInt).add(g.modPow(bInt, N)).mod(N);
}

// kv + g^b
const B = toBuffer(multiplierParam.multiply(vInt).add(g.modPow(bInt, N)).mod(N));
const B = toBuffer(BInt);
return { b: b.toString('hex'), B: B.toString('hex') };
};
2 changes: 1 addition & 1 deletion server/domain/user/service/srp/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const padBufferToHex = (buffer: Buffer): string => {

export const toBuffer = (bigInt: BigInteger): Buffer => {
const str = bigInt.toString(16);
return Buffer.from(str, 'hex');
return Buffer.from(str.padStart(str.length + (str.length % 2), '0'), 'hex');
};

export const toBufferWithLength = (bigInt: BigInteger, length: number): Buffer => {
Expand Down
8 changes: 3 additions & 5 deletions server/tests/api/changePassword.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import assert from 'assert';
import crypto from 'crypto';
import { calcClientSignature } from 'domain/user/service/srp/calcClientSignature';
import { N, g } from 'domain/user/service/srp/constants';
import { fromBuffer, toBuffer } from 'domain/user/service/srp/util';
import { calculateSrpA } from 'domain/user/service/srp/calcSrpA';
import { fromBuffer } from 'domain/user/service/srp/util';
import { DEFAULT_USER_POOL_CLIENT_ID } from 'service/envValues';
import { test } from 'vitest';
import {
Expand All @@ -15,8 +14,7 @@ import {

test('changePassword', async () => {
await createUserClient();
const a = crypto.randomBytes(32);
const A = toBuffer(g.modPow(fromBuffer(a), N));
const { a, A } = calculateSrpA();
const res1 = await noCookieClient.$post({
headers: { 'x-amz-target': 'AWSCognitoIdentityProviderService.InitiateAuth' },
body: {
Expand Down
8 changes: 3 additions & 5 deletions server/tests/api/signIn.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import assert from 'assert';
import crypto from 'crypto';
import { calcClientSignature } from 'domain/user/service/srp/calcClientSignature';
import { N, g } from 'domain/user/service/srp/constants';
import { fromBuffer, toBuffer } from 'domain/user/service/srp/util';
import { calculateSrpA } from 'domain/user/service/srp/calcSrpA';
import { fromBuffer } from 'domain/user/service/srp/util';
import { DEFAULT_USER_POOL_CLIENT_ID } from 'service/envValues';
import { expect, test } from 'vitest';
import {
Expand All @@ -16,8 +15,7 @@ import {
test('signIn', async () => {
await createUserClient();

const a = crypto.randomBytes(32);
const A = toBuffer(g.modPow(fromBuffer(a), N));
const { a, A } = calculateSrpA();
const res1 = await noCookieClient.$post({
headers: { 'x-amz-target': 'AWSCognitoIdentityProviderService.InitiateAuth' },
body: {
Expand Down

0 comments on commit e0da1ef

Please sign in to comment.