Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key...

21
CACHE-TIMING ATTACKS ON RSA KEY GENERATION Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019 Alejandro Cabrera Aldaya 1 , Cesar Pereida García 2 , Luis Manuel Alvarez Tapia 1 , Billy Bob Brumley 2 , {aldaya, lalvarezt89}@gmail.com, {billy.brumley, cesar.pereidagarcia}@tuni.fi 1 Universidad Tecnológica de la Habana (CUJAE), Habana, Cuba 2 Network and Information Security Group (NISEC), Tampere University, Tampere, Finland Aug 25-28, 2019

Transcript of Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key...

Page 1: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

CACHE-TIMING ATTACKS ON RSA KEY GENERATIONConference on Cryptographic Hardware and Embedded Systems (CHES) 2019

Alejandro Cabrera Aldaya1, Cesar Pereida García2,Luis Manuel Alvarez Tapia1, Billy Bob Brumley2,{aldaya, lalvarezt89}@gmail.com, {billy.brumley, cesar.pereidagarcia}@tuni.fi

1 Universidad Tecnológica de la Habana (CUJAE), Habana, Cuba2 Network and Information Security Group (NISEC), Tampere University, Tampere, FinlandAug 25-28, 2019

Page 2: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultContentsIntroductionSide-Channel Leakage FindingThe BN_FLG_CONSTTIMEA New MethodologyThe ToolLeakage AnalysisRSA Key GenerationBinary GCDThe AttackConclusionLessons Learned

2 / 21

Page 3: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultContentsIntroductionSide-Channel Leakage FindingThe BN_FLG_CONSTTIMEA New MethodologyThe ToolLeakage AnalysisRSA Key GenerationBinary GCDThe AttackConclusionLessons Learned

3 / 21

Page 4: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultIntroductionI What?: A single trace cache-timing attack against the binary ExtendedEuclidean (GCD) algorithm used during RSA key generation, leading tocomplete RSA private key recovery.I Why?: Because we can!.

I Cloud services (e.g. AWS, Azure) and automated certificate renewal (e.g. Let’sEncrypt) make RSA key generation a semi-predictable operation.I Micro-architecture attacks.I RSA key generation neglected.

I How?: We developed a new methodology to help us detect insecure codepaths in OpenSSL, then we combine FLUSH+RELOAD, signal processing andlattice techniques.4 / 21

Page 5: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultContentsIntroductionSide-Channel Leakage FindingThe BN_FLG_CONSTTIMEA New MethodologyThe ToolLeakage AnalysisRSA Key GenerationBinary GCDThe AttackConclusionLessons Learned

5 / 21

Page 6: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultOpenSSL and the BN_FLG_CONSTTIME

I OpenSSL relies on the BN_FLG_CONSTTIME to protect against timing-attacks.I The flag gives a lot of room for mistakes.I Several flaws involving the flag have been identified previously.

I CVE-2016-2178I CVE-2016-7056I CVE-2018-0734

I We have a record of well known side-channel vulnerable functions used inOpenSSL.

6 / 21

Page 7: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultA New MethodologyI Create a list of known side-channelvulnerable functions in a library (e.g.OpenSSL).I Use a debugger to automatically setbreakpoints at lines of code thatshould be unreachable.I Run several security-criticalcommands.I Generate a report if any of thebreakpoints is reached.I Investigate manually the root-cause.

7 / 21

Page 8: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultThe Tool1INFO: Parsing source code at: ./openssl-1.0.2k #2 ... in BN_is_prime_fasttest_ex (...) at bn_prime.c:329... #3 ... in BN_generate_prime_ex (...) at bn_prime.c:199INFO: Breakpoints file generated: triggers.gdb #4 ... in rsa_builtin_keygen (...) at rsa_gen.c:150... ...INFO: Monitor target command line INFO: Insecure code executed!TOOL: gdb --batch --command=triggers.gdb --args Breakpoint 2, BN_gcd (...) at bn_gcd.c:120

openssl-1.0.2k/apps/openssl genpkey -algorithm RSA 120 int ret = 0;-out private_key.pem -pkeyopt rsa_keygen_bits:2048 #0 BN_gcd (...) at bn_gcd.c:120

... #1 ... in rsa_builtin_keygen (...) at rsa_gen.c:154INFO: Setting breakpoints... ...Breakpoint 1 at ...: file bn_exp.c, line 418. INFO: Insecure code executed!Breakpoint 2 at ...: file bn_gcd.c, line 120. Breakpoint 3, BN_mod_inverse (...) at bn_gcd.c:238Breakpoint 3 at ...: file bn_gcd.c, line 238. 238 bn_check_top(a);... #0 BN_mod_inverse (...) at bn_gcd.c:238INFO: Insecure code executed! #1 ... in BN_MONT_CTX_set (...) at bn_mont.c:450Breakpoint 1, BN_mod_exp_mont (...) at bn_exp.c:418 #2 ... in BN_is_prime_fasttest_ex (...) at bn_prime.c:319418 bn_check_top(a); #3 ... in BN_generate_prime_ex (...) at bn_prime.c:199#0 BN_mod_exp_mont (...) at bn_exp.c:418 #4 ... in rsa_builtin_keygen (...) at rsa_gen.c:171#1 ... in witness (...) at bn_prime.c:356 ...

1[Gri+19] expanded our methodology and tooling into a CI tool called TriggerFlow.https://gitlab.com/nisec/triggerflow

8 / 21

Page 9: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultContentsIntroductionSide-Channel Leakage FindingThe BN_FLG_CONSTTIMEA New MethodologyThe ToolLeakage AnalysisRSA Key GenerationBinary GCDThe AttackConclusionLessons Learned

9 / 21

Page 10: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultRSA Key GenerationAlgorithm 1: OpenSSL RSA key generationInput: Key size n and public exponent e.Output: Public and private key pair.

1 begin2 while gcd(p− 1, e) 6= 1 do3 p← rand n/2-bit prime /* Generate p */

4 while gcd(q− 1, e) 6= 1 do5 q← rand n/2-bit prime /* Generate q */

6 d← e−1 mod (p− 1)(q− 1) /* Priv exp */7 dp← d mod (p− 1)8 dq← d mod (q− 1) /* CRT parameters */

9 iq← q−1 mod p10 return (N, e), (d, p, q, dp, dq, iq)

10 / 21

Page 11: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultBinary GCDAlgorithm 2: Binary GCDInput: Integers a and b such that 0 < a < b.Output: Greatest common divisor of a and b.

1 begin2 u← a, v ← b, i← 03 while even(u) and even(v) do4 u← u/2, v ← v/2, i← i + 15 while u 6= 0 do6 while even(u) do7 u← u/2 /* u-loop */

8 while even(v) do9 v ← v/2 /* v-loop */

10 if u ≥ v then11 u← u− v /* sub-step */

12 else13 v ← v − u

14 return v · 2i

Coppersmith bound512

e = 2 - 1 e = 2 +1

1023NIST

256

768 1007

16

0

11 / 21

Page 12: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultContentsIntroductionSide-Channel Leakage FindingThe BN_FLG_CONSTTIMEA New MethodologyThe ToolLeakage AnalysisRSA Key GenerationBinary GCDThe AttackConclusionLessons Learned

12 / 21

Page 13: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultMemory Hierarchy

13 / 21

Page 14: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultFLUSH+RELOAD and Performance Degradation

1) Victim executes its own process, filling the cache

3) Victim may or may not execute its own processagain while the attacker waits

4) Attacker reloads the data and measures the loadingtime

2) Attacker flushes victim's data from the cache andwaits

5) Attacker traces the victim's process execution andinfers information about the victim 14 / 21

Page 15: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultAttack ScenarioCore 0 Core 1 Core 2 Core 3

L1 L1 L1 L1 L1 L1 L1 L1

L2 L2 L2 L2

Victim AttackerFlush+Reload

AttackerPerf. Degradation

Shared LLCOpenSSL

15 / 21

Page 16: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultThe Attack 1/2I OpenSSL 1.0.2k.I FLUSH+RELOAD[YF14].I Templating.I Pearsoncorrelation.I Low-pass filter.I Horizontalanalysis.I Sequence of ops.

Tra

ce

Lat

ency

(fi

lter

ed)

Tem

pla

te

Lat

ency

(fi

lter

ed)-0.4

-0.2

0

0.2

0.4

0 50000 100000 150000 200000 250000

Co

rrela

tio

n

100

200

300

284400 284500 284600 284700 284800 284900 285000 285100 285200 285300

Tra

ce,

Lat

ency

Time (samples)

subtraction probeshift probe

LLLSLLSLSLLLLLSL. . .LS 16 / 21

Page 17: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultThe Attack 2/2I Use expand-and-prune [HMM10] error correction algorithm.I Obtain a ranked list of partial prime factors.I Formulate lattice problems for the candidates.I Run in a cluster for 4 hours.I Recover private keys with a 27% success rate.

17 / 21

Page 18: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultContentsIntroductionSide-Channel Leakage FindingThe BN_FLG_CONSTTIMEA New MethodologyThe ToolLeakage AnalysisRSA Key GenerationBinary GCDThe AttackConclusionLessons Learned

18 / 21

Page 19: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultTL;DNLI We developed a simple methodology and tool to track existing flaws leadingto insecure code paths in crypto libraries.I We discovered three new flaws affecting OpenSSL during RSA key generation.I We performed a cache-timing attack on the GCD algorithm, allowing us tofully recover RSA keys with a success rate of 27%.I Our general strategy was:

I FLUSH+RELOAD and performance degradation.I Signal processing.I Error correction algorithm.I Lattice problem solving.

19 / 21

Page 20: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

defaultResponsible DisclosureWe reported our findings to OpenSSL security team, and they confirmed affectedversions2 1.1.0-1.1.0h and 1.0.2-1.0.2o.OpenSSL assigned CVE-2018-0737 based on our work and adopted the proposedpatches.I Lesson 1: Secure by default. These and similar flaws can be prevented witha secure-by-default approach.

I Adopt constant-time algorithms by default, e.g. [BY19]I Lesson 2: Knowledge transfer. The engineers and cryptographers mustwork side-by-side to ensure that academic results permeate over real-worldproducts.2OpenSSL 1.1.1 did not exist at the time of disclosure.

20 / 21

Page 21: Cache-Timing Attacks on RSA Key Generation - Conference on ... · Cache-Timing Attacks on RSA Key Generation - Conference on Cryptographic Hardware and Embedded Systems (CHES) 2019

default

Thank you for listening.Questions?

21 / 21