Skip to content

Commit

Permalink
fix(sdk): calculate the length of ecdsa binding
Browse files Browse the repository at this point in the history
  • Loading branch information
sujankota committed Aug 21, 2024
1 parent 6d01eff commit d5e93d7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
38 changes: 37 additions & 1 deletion lib/src/nanotdf/models/Policy/AbstractPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ abstract class AbstractPolicy implements PolicyInterface {
static readonly BODY_BYTE_MAX_LEN = 257;
static readonly BINDING_BYTE_MIN_LEN = 8;
static readonly BINDING_BYTE_MAX_LEN = 132;
static readonly SIZE_OF_LENGTH_FIELD = 1; // 1 byte for each length field (R and S)
static readonly GMAC_BINDING_LEN = 8;

readonly type: PolicyType;
readonly binding: Uint8Array;
Expand All @@ -18,7 +20,7 @@ abstract class AbstractPolicy implements PolicyInterface {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
buff: Uint8Array,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
bindingLength: number,
useECDSABinding: boolean,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type?: PolicyType
): { policy: PolicyInterface; offset: number } {
Expand All @@ -43,6 +45,40 @@ abstract class AbstractPolicy implements PolicyInterface {
toBuffer(): Uint8Array | never {
throw new Error('toBuffer() was not implemented');
}

/**
* Parses an ECDSA binding from a given buffer.
*
* @param {Uint8Array} buff - The buffer containing the ECDSA binding.
* @returns {{ bindingLength: number; binding: Uint8Array }} - An object containing the binding length and the binding subarray.
*/
static parseECDSABinding(buff: Uint8Array): { bindingLength: number; binding: Uint8Array } {
const lengthOfR = buff[0];
const lengthOfS = buff[this.SIZE_OF_LENGTH_FIELD + lengthOfR];

const bindingLength = this.SIZE_OF_LENGTH_FIELD + lengthOfR + this.SIZE_OF_LENGTH_FIELD + lengthOfS;
const binding = buff.subarray(0, bindingLength);

return { bindingLength, binding };
}

/**
* Parses a binding from a given buffer based on the specified binding type.
*
* @param {Uint8Array} buff - The buffer containing the binding.
* @param {boolean} useEcdsaBinding - Flag indicating whether to use ECDSA binding.
* @param {number} offset - The starting offset in the buffer.
* @returns {{ binding: Uint8Array; newOffset: number }} - An object containing the binding and the new offset.
*/
static parseBinding(buff: Uint8Array, useEcdsaBinding: boolean, offset: number): { binding: Uint8Array; newOffset: number } {
if (useEcdsaBinding) {
const ecdsaBinding = this.parseECDSABinding(buff.subarray(offset));
return { binding: ecdsaBinding.binding, newOffset: offset + ecdsaBinding.bindingLength };
} else {
const binding = buff.subarray(offset, offset + this.GMAC_BINDING_LEN);
return { binding, newOffset: offset + this.GMAC_BINDING_LEN };
}
}
}

export default AbstractPolicy;
6 changes: 3 additions & 3 deletions lib/src/nanotdf/models/Policy/EmbeddedPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EmbeddedPolicy extends AbstractPolicy implements EmbeddedPolicyInterface {

static override parse(
buff: Uint8Array,
bindingLength: number,
useEcdsaBinding: boolean,
type: PolicyTypes
): { offset: number; policy: EmbeddedPolicy } {
let offset = 0;
Expand All @@ -32,8 +32,8 @@ class EmbeddedPolicy extends AbstractPolicy implements EmbeddedPolicyInterface {
const content = buff.subarray(offset, offset + length);
offset += length;

const binding = buff.subarray(offset, offset + bindingLength);
offset += bindingLength;
const { binding, newOffset: bindingOffset } = this.parseBinding(buff, useEcdsaBinding, offset);
offset = bindingOffset;

return {
policy: new EmbeddedPolicy(type, binding, content),
Expand Down
6 changes: 2 additions & 4 deletions lib/src/nanotdf/models/Policy/PolicyFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import AbstractPolicy from './AbstractPolicy.js';
import EmbeddedPolicy from './EmbeddedPolicy.js';
import RemotePolicy from './RemotePolicy.js';
import PolicyTypeEnum from '../../enum/PolicyTypeEnum.js';
import { lengthOfBinding } from '../../helpers/calculateByCipher.js';
import { InvalidPolicyTypeError } from '../../../errors.js';
import CurveNameEnum from '../../enum/CurveNameEnum.js';

Expand All @@ -12,15 +11,14 @@ function parse(
curve: CurveNameEnum
): { policy: AbstractPolicy; offset: number } | never {
const type = buff[AbstractPolicy.TYPE_BYTE_OFF];
const bindingLength = lengthOfBinding(useEcdsaBinding, curve);
let policy: AbstractPolicy;
let offset: number;

// Check if remote policy
if (type === PolicyTypeEnum.Remote) {
({ policy, offset } = RemotePolicy.parse(
buff.subarray(AbstractPolicy.TYPE_BYTE_LEN),
bindingLength
useEcdsaBinding,
));
} else if (
[
Expand All @@ -32,7 +30,7 @@ function parse(
) {
({ policy, offset } = EmbeddedPolicy.parse(
buff.subarray(AbstractPolicy.TYPE_BYTE_LEN),
bindingLength,
useEcdsaBinding,
type
));
} else {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/nanotdf/models/Policy/RemotePolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class RemotePolicy extends AbstractPolicy implements RemotePolicyInterface {

static override parse(
buff: Uint8Array,
bindingLength: number
useEcdsaBinding: boolean
): { offset: number; policy: RemotePolicy } {
let offset = 0;
const resource = new ResourceLocator(buff);
offset += resource.offset;

const binding = buff.subarray(offset, offset + bindingLength);
offset += bindingLength;
const { binding, newOffset: bindingOffset } = this.parseBinding(buff, useEcdsaBinding, offset);
offset = bindingOffset;

return {
policy: new RemotePolicy(PolicyTypeEnum.Remote, binding, resource),
Expand Down

0 comments on commit d5e93d7

Please sign in to comment.