1/60
Looks like no tags are added yet.
What is the main goal of cryptography?
To give the sender and receiver security like a secret pipe that only the two of them can use.
What is a random number generator and why does it matter?
It makes numbers that cannot be predicted better than by chance (P = 1/N). Random numbers are used as keys, so guessable numbers mean guessable keys.
Which Java class makes secure random numbers?
SecureRandom
What is symmetric encryption?
The same secret key encrypts and decrypts the data. Keeping the key private is critical. Example: AES.
What is asymmetric encryption?
A public/private key pair. Data encrypted with one key is decrypted with the other. Examples: RSA, DSA.
What does the Cipher class do and what does it protect?
Encrypts and decrypts data. Protects confidentiality.
What is a hash function?
Compresses input of any length into a short fixed-length output. Used to detect modification (integrity). Example: SHA-256.
Which Java class computes a hash?
MessageDigest
What is a MAC (Message Authentication Code)?
A digest computed with a secret key. Only someone with the key can verify the integrity of the data. Example: HmacSHA256.
Why is a plain hash not enough to stop an attacker?
The attacker can change the message and recompute the hash because it uses no secret. Without the key they cannot produce a valid MAC (soldiers example: 5K changed to 1K).
What is a digital signature?
Only the private key holder can create it; anyone with the public key can verify authenticity and integrity. Example: SHA256withDSA (Signature class).
What is a certificate?
A digital ID that contains a public key. A certificate chain links it to a trusted CA.
Where are keys and certificates stored in Java?
In a keystore (JKS, JCEKS, PKCS12).
Match the JCA classes to their jobs
SecureRandom = random numbers; Cipher = encrypt/decrypt; MessageDigest = hash; Mac = keyed hash; Signature = sign and verify.
What are the RSA steps?
1) N = p x q 2) m = (p-1)(q-1) 3) pick E with gcd(E, m) = 1 4) find D with E x D = 1 mod m 5) encrypt c = s^E mod N 6) decrypt s = c^D mod N
RSA exercise: p = 17, q = 19, E = 191, s = 25. Find N, m, D, c.
N = 323, m = 288, D = 95 (191 x 95 = 1 + 63 x 288), c = 25^191 mod 323 = 321. Check: 321^95 mod 323 = 25.
RSA slide example: p = 23, q = 29, E = 487. Find N, m, D.
N = 667, m = 616, D = 191. 25 encrypts to 169; 169 decrypts back to 25.
How does the SSL/TLS handshake (RSA suite) create the session key?
1) Server sends its certificate (public key) 2) Client verifies it with its TrustManager 3) Client makes random bytes with SecureRandom and encrypts them with the server's public key 4) Server decrypts with its private key 5) Both derive the symmetric key.
Name three common crypto errors
Insecure cryptosystems (DES, MD5); incorrect configurations; short keys; unsigned certificates; not verifying certificates.
Why not use the same key for a set of products?
If one key leaks, every product is compromised. Each product needs its own key.
What is wrong with srand(time(NULL)) then rand() to make a password?
rand() is not cryptographic and the seed is the time, which an attacker can guess, so the password is predictable. (Quiz 1 Q3)
Where should private keys not be stored?
In open locations like /tmp. Store them in a secure location.
What is taint analysis?
Tracks untrusted input from a source (e.g., request.getParameter) to a sink (e.g., executeQuery). Detects SQL injection.
Can taint analysis detect crypto API misuse? Why?
No. No untrusted input flows anywhere; the bug is how the crypto API is used (wrong algorithm, weak key, wrong call order).
What is wrong with Cipher.getInstance("DES/ECB/PKCS5Padding")?
DES has a short key and is breakable. ECB encrypts identical blocks identically, leaking patterns. Use AES with GCM.
What is wrong with Cipher.getInstance("AES") with no mode?
It defaults to ECB mode. Specify the mode, e.g., AES/GCM.
What is wrong with getRandomKey(192, 1873)?
The fixed seed 1873 makes the key predictable, effectively hardcoded.
What is wrong with the KeyDeriv code where algorithm = ""?
The algorithm is empty, so key derivation is undefined and cannot be verified. Set a strong algorithm explicitly.
Type vs typestate
Type: the set of operations ever permitted on an object. Typestate: the subset permitted in a particular context (state).
Typestate of a variable
Uninitialized -assign-> Initialized -read/update-> Initialized -finalize-> done
Typestate of KeyGenerator
0 -getInstance-> 1 -init-> 2 -generateKey-> 3, plus 1 -generateKey-> 3 (init is optional).
What is CrySL?
A language for writing rules that specify correct use of crypto APIs; a tool checks code against the rules.
Six parts of a CrySL rule
OBJECTS (variables), EVENTS (method calls), ORDER (allowed call sequence), CONSTRAINTS (allowed values), REQUIRES (predicates needed from other objects), ENSURES (predicate this object provides).
CrySL ORDER rule for KeyGenerator
GetInstance, Init?, GenKey (? means Init is optional)
Allowed AES key sizes in the CrySL rule
128, 192, 256
CrySL exercise: getInstance("AES"), init(64), generateKey(). Do ORDER and CONSTRAINTS hold?
ORDER holds. CONSTRAINTS fail: 64 is not in {128, 192, 256}. CrySL reports a misuse.
How do REQUIRES and ENSURES work together?
KeyGenerator ENSURES generatedKey; Cipher REQUIRES generatedKey, then ENSURES encrypted. A chain of promises across composed objects.
Allowed algorithms in the CrySL MessageDigest rule
SHA-256, SHA-384, SHA-512 (not MD5)
Limitation of static analysis for crypto misuse
isPrime(66) is always false so MD5 is never used, but the tool cannot evaluate it and reports MD5 anyway: a false positive.
Tools that detect crypto API misuse
Coverity, CrySL, CryptoGuard, SpotBugs
Study result on 2,324 Java projects
Only 642 used crypto securely; 1,682 were buggy.
What is an API?
Application Programming Interface: lets third-party systems use a system's capabilities.
Two API security issues and their fixes
Secure communication: SSL/TLS. Authorization: OAuth 2.0 (authorization code or client credentials).
What happens if an API uses plain http?
Anyone on the network can read or modify the data (e.g., name, card type, billing address).
What does curl -k do and why is it risky?
Skips certificate verification, so a man-in-the-middle can impersonate the server. Fix: --cacert ca.crt.
Why did curl reject certificate name foo for localhost?
The certificate subject name (CN) must match the host name.
What is mutual TLS?
Both server and client present certificates. Setting: server.ssl.client-auth: need.
Why use a token instead of giving an app your password?
Credentials give full, lasting access; a token is time-bounded and limited.
Four OAuth 2.0 roles
Resource owner (owns data), resource server (hosts data), client (app wanting access), authorization server (issues tokens).
Four OAuth 2.0 grant types
Client credentials; resource owner password; implicit; authorization code.
Which grant types are obsolete and why?
Password: the client sees the user's password. Implicit: no verification of client identity; token exposed in the URL.
When to use client credentials grant?
When the client owns the resources.
When to use authorization code grant?
When a user allows a client to use the user's resources.
Steps of the authorization code flow
1) Client redirects user to /authorize 2) User consents; server returns a code 3) Client sends code + client secret to /token 4) Server returns access token.
Three ways to send an access token
Authorization header (Bearer, best); query parameter; form body.
ApolloAuto: protect HD map updates
Vendor digitally signs maps; car verifies with vendor public key (integrity + authenticity).
ApolloAuto: stop fake CANBus commands
Add a MAC (HmacSHA256) with a per-vehicle secret key; spoofed messages fail the check.
ApolloAuto: keep car data private in transit
Use TLS, ideally mutual TLS.
ApolloAuto: phone app reading car status uses which grant?
Authorization code (user delegates access).
ApolloAuto: why a unique key per car?
One leaked key compromises only one car, not the whole fleet.