# Key weakness finding report: JSON Schema (draft 2020-12), written as YAML.
# Phase 4 output. Criteria: taxonomy/classification.md. Procedure:
# taxonomy/decision-guide.md. Semantic rules that JSON Schema cannot express
# (disposition recomputation, class-specific requirements beyond field
# presence) are enforced by tools/check_reports.py.
# Extends the illustrative format in sources/.../03_badkeys_case_study.md.

$schema: https://json-schema.org/draft/2020-12/schema
$id: keyweakness/report-schema/0.1
title: Key weakness finding report
type: object
required: [case, subject, validity, findings, disposition]
additionalProperties: false

properties:
  kwc_version: {type: string, description: "Version of the classification the report follows, as in taxonomy/VERSION"}
  case:
    type: string
    description: Short identifier of the case.

  subject:
    type: object
    required: [secret_role, algorithm]
    additionalProperties: false
    properties:
      secret_role:
        enum: [long-term, ephemeral, nonce, seed, generator-state]
      algorithm: {type: string, description: "e.g. ECDSA, RSA, AES-128, DES, GHASH"}
      parameters: {type: string, description: "curve, modulus size, key size"}
      context: {type: string, description: "e.g. CA issuing key, TLS server key"}
      linked_reports:
        type: array
        items: {type: string}
        description: Cases for auxiliary secrets (auxiliary-secret rule).

  validity:
    type: object
    required: [status]
    additionalProperties: false
    properties:
      status: {enum: [valid, invalid]}
      rule: {type: string, description: Admissibility rule violated, if invalid.}

  findings:
    type: array
    items: {$ref: "#/$defs/finding"}

  descriptors:
    type: object
    additionalProperties: false
    properties:
      minimal_bit_length: {type: integer, description: "floor(log2 k) + 1, 0 for k = 0. A byte-string key is read as a big-endian unsigned integer. Context only."}
      encoding_width_bits: {type: integer}
      self_information_bits: {type: number}
      self_information_distribution: {type: string, description: "The distribution D under which self_information_bits is stated"}
      description_complexity: {type: string}
      generator_fingerprint: {type: string}
      accessibility:
        type: object
        additionalProperties: false
        properties:
          cost: {type: string, description: "Least known cost over declared and generic attacks, with its unit"}
          budget: {type: string, description: "A profile (commodity: 2^64 operations; well-resourced: 2^80; classification II.3) or a number with its unit"}
          feasible: {type: boolean, description: "cost is within budget (a cost equal to the budget is within it)"}
          objective: {$ref: "#/$defs/objective", description: "Objective the cost refers to; rec if omitted"}

  scan:
    type: object
    additionalProperties: false
    properties:
      tool: {type: string}
      checks_run: {type: array, items: {type: string}}
      clean: {type: boolean, description: "True only means the listed checks did not fire."}

  disposition:
    type: object
    required: [action, rule]
    additionalProperties: false
    properties:
      action: {enum: [reject-invalid, treat-as-compromised, reject-by-policy, investigate, none]}
      rule: {type: integer, minimum: 0, maximum: 6, description: Decision guide step 11 rule number.}
      note: {type: string}

$defs:
  evidence:
    enum: [unknown, hypothesized, indicated, credible, demonstrated]

  objective:
    enum: [rec, dist, forge, conf, other]

  finding:
    type: object
    required: [class, evidence]
    additionalProperties: false
    properties:
      class: {enum: [A1, A2, B1, B2, C1, C2, C3]}
      evidence: {$ref: "#/$defs/evidence"}
      objective: {$ref: "#/$defs/objective", description: "For A1, A2 and C1 findings. The strongest objective the stated setting of use supports"}
      sources: {type: array, items: {type: string, pattern: "^R[0-9]+$"}}
      detection:
        type: object
        additionalProperties: false
        properties:
          check: {type: string, description: "Free text; 'unnamed' if the case names none"}
          tool: {type: string, description: "Free text; 'unnamed' if the case names none"}
      note: {type: string}

      # Group A
      route: {enum: [structural, degeneracy]}
      attack:
        type: object
        additionalProperties: false
        properties:
          subset: {type: string}
          algorithm: {type: string}
          relation: {type: string, description: "Phi, for route degeneracy: a text statement of the relation on maps. Not the C1 object of the same name, which sits on the finding"}
          setting: {enum: [secret-key, known-key, chosen-key]}
          use: {type: string, description: "Setting of use that supports the objective: mode or protocol, what the adversary can query, who chooses inputs (classification I.3)"}
          queries: {type: string, description: "q, the oracle queries the attack needs"}
          advantage: {type: string, description: "epsilon, the success probability or advantage reached"}
          cost: {type: string}
          rho: {type: string, description: "Structural advantage. A recovery measure: write 'not applicable' for another objective (classification I.3)"}
          subset_advantage: {type: string, description: "beta: generic cost over the baseline (classification I.2)"}
          prevalence_ratio: {type: string, description: "kappa, for route degeneracy: prevalence of the relation against the ideal counterpart"}
          excess_prevalence: {type: string, description: "delta, for route degeneracy: prevalence in the construction minus prevalence in the ideal counterpart"}
          precomputation: {type: string, description: "Precomputation, memory and number of targets, if any"}
          mass: {type: string}
          listing_feasible: {type: boolean, description: "The class can be enumerated within budget and candidates tested: recovery by listing, rho = 1. Counts for rule 2 on a finding at credible or above (classification I.3)"}
          class_size: {type: string, description: "Number of keys in the class, for listing_feasible"}
          sigma: {type: string, description: "log2 of rho, for a concrete Route 1 attack; compared with sigma_min (default 16 bits)"}
          feasible: {type: boolean, description: "Works against this key, in its actual setting of use, within budget and with success above the generic bound. Counts for rule 2 only on a finding at credible or above"}

      # B1
      strategy:
        type: object
        required: [name, budget]
        additionalProperties: false
        properties:
          name: {type: string}
          budget: {type: string, description: "Number of guesses allowed, with unit, e.g. '2^8 guesses'"}
          recovered: {type: boolean, description: "The strategy was run using only information available to an adversary and found this key. Counts for rule 2 only with evidence demonstrated"}

      # B2
      generation:
        type: object
        required: [shortfall]
        additionalProperties: false
        properties:
          mechanism: {type: string}
          lambda_required: {type: integer}
          shortfall: {type: boolean, description: "Must be true for B2; without a shortfall the bias is a generator_fingerprint descriptor (classification I.4)."}
          root_cause: {type: string}
          cause_evidence: {$ref: "#/$defs/evidence"}

      # C1
      relation:
        type: object
        description: "The C1 relation to data outside this instance. Not attack.relation, which is the group A relation Phi"
        required: [subtype]
        additionalProperties: false
        properties:
          subtype: {enum: [cross-key, auxiliary-secret, cross-execution, related-key, side-channel]}
          observations: {type: string}
          via: {type: string, description: "Class of the auxiliary secret, e.g. 'nonce B2'"}
          feasible: {type: boolean, description: "As attack.feasible"}

      # C2
      compromise:
        type: object
        required: [mode, party, history]
        additionalProperties: false
        properties:
          mode: {enum: [disclosed, recovered, shared, substituted, misused]}
          party: {enum: [public, detector, third-party, unknown], description: "public: published. detector: the party whose check or scan produced this report. third-party: another identified outsider"}
          history: {enum: [known, unknown, excluded], description: "Unauthorized use in the past. known: it occurred. unknown: not established either way. excluded: evidence covering the whole exposure rules it out"}

      # C3
      policy:
        type: object
        required: [name, version, rule, mandates_rejection]
        additionalProperties: false
        properties:
          name: {type: string}
          version: {type: string}
          rule: {type: string}
          mandates_rejection: {type: boolean}

    allOf:
      - if: {properties: {class: {enum: [A1, A2]}}}
        then: {required: [objective, route, attack]}
      - if: {properties: {route: {const: degeneracy}}, required: [route]}
        then: {properties: {attack: {required: [relation, setting, prevalence_ratio, excess_prevalence]}}}
      - if: {properties: {route: {const: structural}}, required: [route]}
        then: {properties: {attack: {required: [subset, algorithm, cost]}}}
      - if: {properties: {class: {const: B1}}}
        then: {required: [strategy]}
      - if: {properties: {class: {const: B2}}}
        then: {required: [generation]}
      - if: {properties: {class: {const: C1}}}
        then: {required: [objective, relation]}
      - if: {properties: {class: {const: C2}}}
        then: {required: [compromise]}
      - if: {properties: {class: {const: C3}}}
        then: {required: [policy]}
