OpenCyber Academy
Bachelor's · Chapter 8 · English
Course code: OCA-100Chapter 08: OCA-100-C008

Chapter 8 — Programming II

Open, free learning for anyone who wants to protect people and technology. No account is required to read this text or its worked answers.

Lesson map

Lesson 08 published. Map, example, EX022.

flowchart TD
  N0["Learning outcomes"]
  N1["8.1 High-level APIs"]
  N2["8.2 Structuring for safety"]
  N3["8.3 Negative tests"]
  N4["Synthesis"]
  N0 --> N1 --> N2 --> N3 --> N4
  N4 --> EX["Worked example"]
  EX --> Q["Exercise EX022"]

Logical drawing

flowchart LR
  N0["Learning outcomes"]
  N1["8.1 High-level APIs"]
  N2["8.2 Structuring for safety"]
  N3["8.3 Negative tests"]
  N4["Synthesis"]
  N0 --> N1 --> N2 --> N3 --> N4 --> Q["EX022"]
[Learning outcomes]
    |
    v
[8.1 High-level APIs]
    |
    v
[8.2 Structuring for safety]
    |
    v
[8.3 Negative tests]
    |
    v
[Synthesis]
    |
    v
[Worked example] --> [Exercise EX022]

Learning outcomes

You will (1) use high-level cryptographic APIs safely; (2) structure code to avoid misuse; (3) write negative tests.

8.1 High-level APIs

Priya learns that cryptographic libraries offer low-level primitives and high-level recipes. Low-level APIs require choosing modes, nonces and padding, where mistakes are easy. High-level APIs, such as an authenticated-encryption function that generates nonces and returns a single token, make safe use the default. For Tidewell's export tool, she uses a maintained library's authenticated-encryption recipe rather than assembling AES and a mode herself. The course rule is explicit: students learn how primitives work, but production code uses vetted high-level APIs. Teammates agree to follow it.

8.2 Structuring for safety

Priya isolates cryptography in one module with a small interface: protect(data) and unprotect(token). Keys are loaded from the consortium's secret store, never from source code. Errors in unprotect raise a single generic exception, so callers cannot learn why a token failed. She writes type hints marking secret values and avoids logging them. Code review focuses on this module, which is small enough to read carefully. The structure means future changes of algorithm affect one file rather than the whole application.

8.3 Negative tests

Priya writes tests that should fail: tampered tokens, truncated tokens, tokens protected with a different key and empty input. Each must be rejected. She also tests that two protections of the same data produce different tokens, confirming that nonces are fresh. A teammate's earlier version passed all positive tests while returning data even for tampered tokens, because it skipped tag verification. Negative tests caught it. She adds them to continuous integration so they run on every change. Each test has a one-line comment explaining its purpose.

Worked example

Priya uses a high-level authenticated-encryption recipe in a small protect/unprotect module, loads keys from a secret store and catches a skipped tag check with negative tests.

Synthetic case. The export tool is invented.

Errors and limits

High-level APIs still need correct key management. Tests prove only what they check.

Exercises

Synthesis

Safe cryptographic code relies on high-level APIs, a small isolated module and negative tests that prove rejection.

Continue studying — chapter 08 of 40

← Chapter 07 (draft) · Course chapter index · Chapter 09 (draft) →

Worked answers

EX022. They make secure choices about nonces, modes and padding by default, so fewer mistakes are possible.

EX023. So that neither callers nor attackers learn which check failed.

EX024. That the tokens differ, which shows a fresh nonce is used each time.