Skip to main content
IoT
PSUT Engineering StudioPrincess Sumaya University for Technology
V21.6.5 · Student / connected
Cyber Defense StudioIoT Security and PrivacyExperimentsWorkspaces
Continue to CR29–CR34 — randomness and entropy →

V22.7 · Interactive algorithms, Python notebooks and WSL resources

Home / IoT Security and Privacy / EC10
IoTPSUT IoT Engineering StudioPrincess Sumaya University for Technology · Dr. Mohammad Alja’afreh
ESSENTIAL LABORATORY · EC10 · IoT SECURITY AND PRIVACY
IoT Security and Privacy

PyCryptodome — AES-CTR, hashing and hash-chain PRNG

Use a cryptography library to encrypt a byte stream, inspect SHA-256/SHA-512 digests and investigate deterministic hash-chain pseudo-randomness.

Design experiment & verify evidence →
EC10 · Laboratory mentorAsk about the current experiment, its algorithms or Python notebook.
Ask Dr. MohammadAdaptive practice
EC10 · Academic orientation

Scope and objectives

Scope of the experiment

Use a cryptography library to encrypt a byte stream, inspect SHA-256/SHA-512 digests and investigate deterministic hash-chain pseudo-randomness.

The focus of this experiment is to use cryptographic library APIs for AES-CTR and hashing. Students then trace nonce/counter uniqueness and deterministic hash-chain evolution, and verify recovery and distinguish classroom generator from secure OS randomness. The Concepts view explains the operation before the practical work; the Simulation view is used to examine reference and changed inputs, not to substitute for actual measurements.

Learning objectives

  • Use cryptographic library APIs for AES-CTR and hashing.
  • Trace nonce/counter uniqueness and deterministic hash-chain evolution.
  • Verify recovery and distinguish classroom generator from secure OS randomness.
  • Record the relevant inputs, intermediate states and final verification result before changing the reference configuration.
  • Compare the reference calculation or decision with an independent implementation or authorized observation; state any discrepancy and the applicable model boundary.

Expected learning outcomes

On completion, students can use cryptographic library APIs for AES-CTR and hashing, trace nonce/counter uniqueness and deterministic hash-chain evolution, and verify recovery and distinguish classroom generator from secure OS randomness.

Required deliverables

Submit the derivation or state trace for the named operation, the exact reference and changed configurations, the associated simulator or practical evidence, and a short validation statement addressing verify recovery and distinguish classroom generator from secure OS randomness. Separate predicted values from observations.

Prerequisite knowledge and related learning

Review the foundational terminology in Concepts and confirm readiness using the existing setup instructions. After completion, continue to Hash security — Preimages, collisions, HMAC and keyed generation.

Environment and experimental methodology

The included teaching workstation and the listed Python/Colab or WSL implementation. Historical and small-number constructions are instructional, not deployment recommendations.

Begin with a manual prediction, record the baseline, change one independent condition at a time, and compare the resulting trace or measurements with the original prediction. Record observations and uncertainty separately from reference values. Follow the experiment’s specific procedure, safety limits, and acceptance checks.

How to investigate PyCryptodome — AES-CTR, hashing and hash-chain PRNG

Use a cryptography library to encrypt a byte stream, inspect SHA-256/SHA-512 digests and investigate deterministic hash-chain pseudo-randomness.

Analytical investigation means collecting an observation, identifying the mechanism, using a calculation or state trace to explain it, and checking your conclusion against evidence. Controlled comparison means changing one independent variable, holding the remaining relevant conditions constant, and measuring a dependent variable against a baseline. Experimental validation means comparing a predicted result to a reproducible observation or assertion; agreement inside a model does not certify hardware. Evidence-based reasoning connects a specific observation to a theoretical rule and states uncertainty, not merely an opinion. Performance characterization records a named metric and its units across conditions; sensitivity analysis changes one parameter across several values. A threat assessment links an asset, threat, consequence and evidence to a mitigation.

Experiment-specific guided practice

Objective: Use a cryptography library to encrypt a byte stream, inspect SHA-256/SHA-512 digests and investigate deterministic hash-chain pseudo-randomness.

  1. Complete the analytical preparation below before programming. Show the intermediate mathematical calculations.
  2. PyCryptodome CTR file format — complete its numbered steps and record the requested evidence.
  3. SHA-256 and SHA-512 — complete its numbered steps and record the requested evidence.
  4. Hash-chain digit generation and timing — complete its numbered steps and record the requested evidence.

Reference check: SHA-256("I like cats") = 446b43a682a92a039e4081a30be1c39c7d46b3675d6e209c200a252c410167d9. Correct CTR decryption recovers all bytes; seed stream matches the reference.

Model boundary: Fictional educational inputs. Browser algorithms and reference programs; no hardware or live-network measurements. Supplied starter notebooks may be incomplete; trace playback is recorded execution, not arbitrary Python running in browser.

Independent task and submission evidence

2. PyCryptodome CTR file format — complete its numbered steps and record the requested evidence. Record the named input, prediction, actual output and one limitation; restore the original condition and repeat.

Before execution write the independent variable and its levels, dependent observation with units where applicable, baseline, controlled variables, measurement method and expected result. For a state machine use state/event/output rather than inventing a numeric metric. Record failures and repeat the baseline. A weak conclusion is “it works”; a supported conclusion names the changed setting, observed value/state, corresponding rule and one limitation.

TrialChanged settingFixed settingsPredicted resultActual observation / unitsEvidence / uncertainty
A: baselineReferenceRecordPredictRecordRecord
B: one changeRecordSame as APredictRecordRecord
A: repeatRestoreSame as APredictRecordRecord

Completion: submit the filled table, relevant simulator screenshot or terminal/notebook output, reasoning with units/assumptions, and one limitation. Preserve the independent assessment questions; this guide does not fill their answers.

Scientific concepts and engineering principles

Scientific architecture · EC10 · PyCryptodome — AES-CTR, hashing and hash-chain PRNG

CTR, digest and hash-chain generation — Nonce + counter / distinct counter blocks; AES encrypt / keystream; XOR / ciphertext, inverse with same stream; Digest / hash message bytes; Chain / hash previous digest, not secure PRNG by itself
Scientific concept map: Nonce + counter — Unique counter blocks; AES — Encrypt counter; XOR — Plaintext to ciphertext; Hash — SHA-256 / SHA-512; Hash chain — Repeated digest generation.

CTR encryption

Counter mode encrypts a unique nonce/counter input to obtain a keystream, then XORs it with plaintext. Decryption uses the same keystream; store the nonce alongside ciphertext. Never repeat a nonce/counter sequence under the same key. CTR by itself does not authenticate data.

Digest properties

SHA-256 and SHA-512 map byte strings to 32 and 64 bytes respectively. Hexadecimal representations use twice as many characters. Compare the exact digest and Hamming distance after changing one input character; similar messages should not imply similar digests.

Iterated hash generation

Hash a seed, retain binary digest as the next state and collect the desired hexadecimal output. Compare one-digit extraction against full-digest extraction with equal output counts. A frequency histogram can detect some bias but cannot establish cryptographic unpredictability; a public seed makes this generator predictable.

Prerequisites: EC09 and earlier foundations as applicable.

EC10 engineering diagram
Algorithm structure and data flow. Use the simulation to inspect the corresponding numerical transformations.

Engineering diagrams

CTR uses encrypted counter blocks as a keystream.
CTR uses encrypted counter blocks as a keystream.

Scientific foundations and worked examples

CTR keystream construction

For AES-CTR, encrypt a distinct 128-bit nonce/counter block with the key and XOR the resulting 16-byte keystream with data. A partial final block uses only the needed keystream bytes; no padding is required. The receiver reconstructs the same counter sequence. The nonce is not secret, but it must not be reused with the same key. The laboratory stores nonce bytes before ciphertext; the decoder must know their length.

SHA-256 and SHA-512 digest experiment

A digest is deterministic and has fixed width independent of message length. SHA-256 produces 32 bytes (64 hex digits), SHA-512 produces 64 bytes (128 hex digits). Compare the exact UTF-8 strings I like cats and I like cars. The expected difference is a statistical avalanche property, not a guaranteed exact half of bits for each pair. Measure bitwise Hamming distance, not merely the count of changed hex symbols.

Hash-chain generation and performance

Let \(s_{0}\) be the byte encoding of the seed, then \(s_{i+1}\)=H(\(s_{i}\)). The next state is the binary digest, not its printed hexadecimal text. The final hex digit of each digest gives one output symbol; consuming all 64 SHA-256 hex digits reduces the number of hash calls for one million digits from 1,000,000 to 15,625. SHA-512 needs ceil(1,000,000/128)=7,813 calls. Compare elapsed time under identical conditions; fewer calls does not guarantee proportional speedup. Uniform-looking histograms do not establish unpredictability, and public state allows future outputs to be computed.

Engineering principles · additional worked teaching example

CTR uses an encrypted nonce/counter as a keystream; plaintext XOR stream gives ciphertext and XOR again recovers. If a teaching keystream byte is A5 and message 3C, ciphertext99. Reusing stream exposes XOR of messages; hash-chain output alone is not a complete secure generator.

From theory to experiment

Predict the reference result before operating the workbench; check the actual intermediate state, units and constraints. The existing simulation, implementation and procedure below are distinct sources of evidence.

Worked engineering reasoning and boundary conditions

AES-CTR produces a keystream from nonce/counter blocks; XOR makes encryption and decryption symmetric. Hash chains iterate a digest but a public deterministic chain is not a secure RNG.

Use the diagram to follow the calculation or state progression. Keep the teaching configuration separate from the independent exercise values; compare observations with the actual model and implementation.

Concept animation · operation pathway

Explore the teaching example independently of the simulator. Each step reveals an operation, data relationship or calculated state. The Simulation view has separate editable inputs.

PyCryptodome — AES-CTR, hashing and hash-chain PRNG: operation pathwayCounter blocknonce concatenatedwith counterAES encryptionkeystream KXOR35 XOR C2 = F7RecoverF7 XOR C2 = 35Read the active operation, then inspect the accumulated state.

Step 1 of 4 · Counter block

Counter block

Counter block
nonce concatenated with counter

Teaching example only; inspect live calculations or real practical evidence separately.

Interactive flashcards

Self-rated study progress is stored locally, not used for official grades.

Browse all explanations
Why must CTR counters not repeat under a key?

Keystream reuse reveals plaintext relationships.

Does CTR alone authenticate?

No.

What does SHA-256 output?

A 256-bit digest.

Why not use a simple hash chain as a production RNG?

It lacks vetted entropy and state-security design.

How does CTR generate a keystream?

Encrypt unique counter blocks under the key.

How does CTR encrypt plaintext?

XOR plaintext with keystream bytes.

Why must the nonce/counter never repeat under a key?

Repetition reuses keystream and exposes relationships between messages.

Does CTR need block padding?

No, it can use a partial final keystream block.

Does CTR provide authentication by itself?

No.

How long is SHA-256 output?

256 bits, or 32 bytes.

How long is SHA-512 output?

512 bits, or 64 bytes.

What changes when a message character changes?

Its digest typically changes in many bit positions.

How does a hash chain generate another state?

Hash the preceding state according to the specified encoding.

Why control message length when benchmarking?

To compare equivalent workloads.

Two AES-CTR blocks reuse the same counter under one key. What is the risk?

The keystream repeats. XORing ciphertexts reveals the XOR of the plaintexts at repeated positions.

Can a SHA-256 digest decrypt its original input?

No. A digest is a fixed-length one-way fingerprint, not encryption.

How should SHA-256 and SHA-512 timing be compared?

Use the same input bytes, environment and repetitions, recording elapsed time and message length.

Shared research workbenches · network designer & advanced studies
Concurrent-device simulation & Monte Carlo research
Distributed digital twin · topology, protocol & battery research