This article follows DPA Attack on AES with the ChipWhisperer, in which we recovered the AES key by splitting the traces into two groups according to a predicted bit of the S-box output, then comparing their means.
DPA works, but it wastes information: it only looks at one bit out of the eight the chip is processing. The other seven also contribute to power consumption, but the attack treats them as noise. As a result, it takes several thousand traces for the spike of the correct hypothesis to stand out. This is what CPA (Correlation Power Analysis) fixes. That is the mechanism we will detail and implement in the following sections.
In statistics, correlation measures how closely two series of values vary together. It is expressed as a coefficient between -1 and 1:
The coefficient is computed with Pearson’s formula. There is no need to code it yourself: it is already available in NumPy with the np.corrcoef function. For reference, here it is:
Σ (xᵢ - mean(x)) × (yᵢ - mean(y))
r = ───────────────────────────────────────────
√Σ (xᵢ - mean(x))² × √Σ (yᵢ - mean(y))²To illustrate it, we take a noise-free reference affine function, 2x + 1, and correlate it with three series of measurements: the same affine function with noise, a decreasing affine function with noise, and a cloud of random points.
Illustrated example in Python:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
N = 100
x = np.random.uniform(0, 10, N)
reference = 2 * x + 1 # reference affine function, no noise
y_pos = 2 * x + 1 + np.random.normal(0, 2, N) # increasing affine + noise
y_neg = -2 * x + 1 + np.random.normal(0, 2, N) # decreasing affine + noise
y_rand = np.random.uniform(-20, 20, N) # random cloud
series = [("Positive correlation", y_pos),
("Inverse correlation", y_neg),
("No correlation", y_rand)]
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
for ax, (title, y) in zip(axes, series):
r = np.corrcoef(reference, y)[0, 1] # Pearson correlation coefficient
ax.scatter(x, y, s=10, label="measurements")
ax.plot(np.sort(x), np.sort(reference), color="red", label="reference")
ax.set_title(f"{title}\nr = {r:.2f}")
ax.legend()
print(f"{title:22s} : r = {r:.2f}")
plt.tight_layout()
plt.show()Positive correlation : r = 0.94
Inverse correlation : r = -0.95
No correlation : r = -0.00
As with DPA, the attack targets the first round of AES. As a reminder, here is how an AES-128 encryption unfolds:
state = plaintext
state = AddRoundKey(state, key[0]) # state[i] = plaintext[i] ^ key[i]
for round from 1 to 9:
state = SubBytes(state) # state[i] = sbox[state[i]], non-linear
state = ShiftRows(state)
state = MixColumns(state)
state = AddRoundKey(state, key[round])
state = SubBytes(state)
state = ShiftRows(state)
state = AddRoundKey(state, key[10])
ciphertext = stateHere, the exploited leakage lies in SubBytes, because the S-box is the only non-linear operation in AES.
With CPA, two models can be used to predict this leakage:
sbox[plaintext[i] ^ key[i]]. The more bits set to 1, the more power the chip consumes.All that remains is to correlate this Hamming weight with the traces. For a given key hypothesis, each trace yields a scalar: the Hamming weight predicted for its plaintext. This gives a series of predictions, one per trace, which we correlate with the power measured at each point in time of the traces. With the correct hypothesis, the correlation forms a spike at the moment the S-box is computed; with a wrong one, it stays close to zero.
Where DPA made a hypothesis on a single bit of the S-box output (the first one), CPA exploits the leakage of the whole byte: all eight bits contribute to the prediction, which makes it possible to recover the key with fewer traces.
We reuse the power consumption simulation from DPA: the chip leaks the Hamming weight of the S-box output, buried in Gaussian noise. Only the attack changes: instead of splitting the traces according to one bit, we correlate the predicted Hamming weight with the power consumption, for each of the 256 hypotheses.
import random
import numpy as np
random.seed(0)
# AES S-box
sbox = np.array([
0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76,
0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0,
0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15,
0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75,
0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84,
0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf,
0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8,
0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2,
0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73,
0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb,
0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79,
0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08,
0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a,
0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e,
0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf,
0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16], dtype=np.uint8)
def hamming(x):
return bin(x).count("1")
KEY = 0x2B # the key byte to recover (unknown to the attacker)
N = 100
# --- Device simulation ---
# the attacker knows the plaintexts; power consumption leaks the S-box output
plaintexts = [random.randint(0, 255) for _ in range(N)]
power = [hamming(sbox[p ^ KEY]) + random.gauss(0, 1) for p in plaintexts]
# --- Attack: try all 256 possible key bytes ---
def correlation(guess):
# for each trace, PREDICT the Hamming weight of the S-box output under hypothesis 'guess'
predicted_hw = [hamming(sbox[p ^ guess]) for p in plaintexts]
return np.corrcoef(predicted_hw, power)[0, 1] # Pearson correlation
corrs = [abs(correlation(g)) for g in range(256)]
found = max(range(256), key=lambda g: corrs[g])
print(f"Actual key : 0x{KEY:02X}")
print(f"Recovered key : 0x{found:02X}")
print(f"Correlation : {corrs[found]:.3f}")Actual key : 0x2B
Recovered key : 0x2B
Correlation : 0.784The correct hypothesis reaches a correlation of 0.784 with only 100 traces, while the best of the 255 wrong ones peaks at 0.297.
We reuse the setup described in Introduction to Power Analysis with the ChipWhisperer: the SAM4S target on the CW313, connected to the ChipWhisperer-Husky.

As in the DPA article, the notebook is split into two cells: first the trace capture, then the attack.
Cell 1: trace capture
import chipwhisperer as cw
import chipwhisperer.analyzer as cwa
import numpy as np
N = 100
ktp = cw.ktp.Basic()
project = cw.create_project("sca101_cpa", overwrite=True)
for i in range(N):
key, text = ktp.next()
trace = cw.capture_trace(scope, target, text, key)
if trace is not None:
project.traces.append(trace)
if i % 100 == 0:
print(f"{i}/{N}")Cell 2: CPA attack on all 16 bytes
As with DPA, all 256 hypotheses are computed in a single matrix operation. For each hypothesis, we predict the Hamming weight of the S-box output for each trace, then compute the Pearson correlation between these predictions and each point of the traces.
sbox = np.array([
0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76,
0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0,
0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15,
0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75,
0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84,
0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf,
0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8,
0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2,
0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73,
0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb,
0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79,
0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08,
0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a,
0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e,
0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf,
0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16], dtype=np.uint8)
hw = np.array([bin(x).count("1") for x in range(256)], dtype=np.float32) # Hamming weight of 0 to 255
textins = np.array([t.textin for t in project.traces], dtype=np.uint8)
traces = np.array([t.wave for t in project.traces])
real_key = np.array(project.traces[0].key, dtype=np.uint8)
def attack_byte_cpa(byte=0):
pt = textins[:, byte] # (N,)
guesses = np.arange(256, dtype=np.uint8)
model = hw[sbox[np.bitwise_xor(pt[None, :], guesses[:, None])]] # (256, N)
model_c = model - model.mean(axis=1, keepdims=True) # (256, N)
traces_c = traces - traces.mean(axis=0) # (N, T)
cov = model_c @ traces_c # (256, T)
std = np.outer(np.linalg.norm(model_c, axis=1),
np.linalg.norm(traces_c, axis=0)) # (256, T)
corr = cov / std # Pearson
peaks = np.abs(corr).max(axis=1) # (256,)
return int(np.argmax(peaks))
key = []
for b in range(16):
guess = attack_byte_cpa(b)
key.append(guess)
ok = "OK" if guess == real_key[b] else "X"
print(f"byte {b:2d} : guess = 0x{guess:02X} real = 0x{real_key[b]:02X} {ok}")byte 0 : guess = 0x2B real = 0x2B OK
byte 1 : guess = 0x7E real = 0x7E OK
byte 2 : guess = 0x15 real = 0x15 OK
byte 3 : guess = 0x16 real = 0x16 OK
byte 4 : guess = 0x28 real = 0x28 OK
byte 5 : guess = 0xAE real = 0xAE OK
byte 6 : guess = 0xD2 real = 0xD2 OK
byte 7 : guess = 0xA6 real = 0xA6 OK
byte 8 : guess = 0xAB real = 0xAB OK
byte 9 : guess = 0xF7 real = 0xF7 OK
byte 10 : guess = 0x15 real = 0x15 OK
byte 11 : guess = 0x88 real = 0x88 OK
byte 12 : guess = 0x09 real = 0x09 OK
byte 13 : guess = 0xCF real = 0xCF OK
byte 14 : guess = 0x4F real = 0x4F OK
byte 15 : guess = 0x3C real = 0x3C OKAll 16 bytes are recovered with only 100 traces, whereas DPA required more than 2000. The full key 2b7e151628aed2a6abf7158809cf4f3c matches the AES key used by the firmware.
The ChipWhisperer already provides these algorithms in its chipwhisperer.analyzer module: there is no need to recode the attack. Just pick the leakage model (here the S-box output, with the Hamming weight) and run the CPA on the project captured in cell 1.
import chipwhisperer.analyzer as cwa
leak_model = cwa.leakage_models.sbox_output # Hamming weight of the S-box output
attack = cwa.cpa(project, leak_model)
results = attack.run()
print(results)Subkey KGuess Correlation
00 0x2B 0.90725
01 0x7E 0.93969
02 0x15 0.87442
03 0x16 0.90793
04 0x28 0.94331
05 0xAE 0.91723
06 0xD2 0.94056
07 0xA6 0.92106
08 0xAB 0.90177
09 0xF7 0.93506
10 0x15 0.91226
11 0x88 0.92096
12 0x09 0.93570
13 0xCF 0.92990
14 0x4F 0.94586
15 0x3C 0.89173We recover the same key, along with the correlation of the best hypothesis for each byte.
By exploiting all eight bits of the S-box output through the Hamming weight, CPA recovers the full AES key with only 100 traces, whereas DPA required more than 2000. And thanks to the ChipWhisperer built-in tools, the attack fits in a few lines.
DPA is still worth understanding. It is the historical attack, the one that laid the foundations of power analysis, and its simple principle (splitting the traces into two groups according to one bit) makes it a good tool for experimentation, for example to test a new target or an unusual leakage model.