Blog: Update flash filesystem encryption

This commit is contained in:
Pascal Engélibert 2026-06-14 21:40:19 +02:00
commit e9a1608a8f
7 changed files with 165 additions and 20 deletions

View file

@ -31,7 +31,7 @@ SVG = """\
stroke-width: 2px;
fill: none;
}}
path.f {{
path.f, circle.f {{
stroke: none;
fill: {BLACK};
}}
@ -43,7 +43,7 @@ SVG = """\
circle, line, rect, path.s {{
stroke: {WHITE};
}}
path.f {{
path.f, circle.f {{
fill: {WHITE};
}}
}}
@ -89,6 +89,43 @@ class Svg:
"""
return Block(self, x, y, XOR_R)
def plus(self, x, y):
xl = x-XOR_R
xr = x+XOR_R
yt = y-XOR_R
yb = y+XOR_R
size = XOR_R * 2
self.t += f"""\
<rect x="{xl}" y="{yt}" width="{size}" height="{size}"/>
<line x1="{xl}" y1="{y}" x2="{xr}" y2="{y}"/>
<line x1="{x}" y1="{yt}" x2="{x}" y2="{yb}"/>
"""
return Block(self, x, y, XOR_R)
def minus(self, x, y):
xl = x-XOR_R
xr = x+XOR_R
yt = y-XOR_R
size = XOR_R * 2
self.t += f"""\
<rect x="{xl}" y="{yt}" width="{size}" height="{size}"/>
<line x1="{xl}" y1="{y}" x2="{xr}" y2="{y}"/>
"""
return Block(self, x, y, XOR_R)
def rotl(self, x, y, t):
xl = x-XOR_R
yt = y-XOR_R
ymt = y-XOR_R/2
ymb = y+XOR_R/2
size = XOR_R * 2
self.t += f"""\
<rect x="{xl}" y="{yt}" width="{size}" height="{size}"/>
<text class="t" x="{x}" y="{ymt}">&lt;&lt;&lt;</text>
<text class="t" x="{x}" y="{ymb}">{t}</text>
"""
return Block(self, x, y, XOR_R)
def encrypt(self, x, y):
xl = x-ENCRYPT_SIZE//2
yt = y-ENCRYPT_SIZE//2
@ -113,6 +150,28 @@ class Svg:
"""
return Block(self, x, y, ENCRYPT_SIZE//2)
def block(self, x, y, wcells, hcells, texts, borders):
cell_w = ENCRYPT_SIZE
cell_h = ENCRYPT_SIZE//2
w = cell_w * wcells
h = cell_h * hcells
xl = x-w//2
yt = y-h//2
self.t += f"""\
<rect x="{xl}" y="{yt}" width="{w}" height="{h}"/>
"""
for (tx, ty, t) in texts:
tx_ = (tx+0.5)*cell_w+xl
ty_ = (ty+0.5)*cell_h+yt
self.t += f"""<text class="t" x="{tx_}" y="{ty_}">{t}</text>\n"""
return Block(self, x, y, ENCRYPT_SIZE//2)
def knot(self, x, y):
self.t += f"""\
<circle class="f" r="4px" cx="{x}" cy="{y}"/>
"""
return Block(self, x, y, 0)
ARROW_TIP_START = 8
ARROW_TIP_BACK = 12
ARROW_TIP_R = 6
@ -244,6 +303,91 @@ def xts():
return SVG.format(body=s.t, title="XTS", w=372, h=192, **ARGS)
# ChaCha Quarter Round
def chacha_qr():
ax = 16 + 64*0
bx = 16 + 64*1
cx = 16 + 64*2
dx = 16 + 64*3
y = [64 + 32*i for i in range(12)]
s = Svg()
a1 = s.text(ax, 16, "a")
b1 = s.text(bx, 16, "b")
c1 = s.text(cx, 16, "c")
d1 = s.text(dx, 16, "d")
P1 = s.plus(ax, y[0])
N1 = s.knot(bx, y[0])
N2 = s.knot(ax, y[1])
X1 = s.xor(dx, y[1])
R1 = s.rotl(dx, y[2], "16")
N3 = s.knot(dx, y[3])
P2 = s.plus(cx, y[3])
N4 = s.knot(cx, y[4])
X2 = s.xor(bx, y[4])
R2 = s.rotl(bx, y[5], "12")
N5 = s.knot(bx, y[6])
P3 = s.plus(ax, y[6])
N6 = s.knot(ax, y[7])
X3 = s.xor(dx, y[7])
R3 = s.rotl(dx, y[8], "8")
N7 = s.knot(dx, y[9])
P4 = s.plus(cx, y[9])
N8 = s.knot(cx, y[10])
X4 = s.xor(bx, y[10])
R4 = s.rotl(bx, y[11], "7")
a2 = s.text(ax, y[-1]+48, "a")
b2 = s.text(bx, y[-1]+48, "b")
c2 = s.text(cx, y[-1]+48, "c")
d2 = s.text(dx, y[-1]+48, "d")
a1.to(P1)
P1.to(P3)
P3.to(a2)
b1.to(X2)
X2.to(R2)
R2.to(X4)
X4.to(R4)
R4.to(b2)
c1.to(P2)
P2.to(P4)
P4.to(c2)
d1.to(X1)
X1.to(R1)
R1.to(X3)
X3.to(R3)
R3.to(d2)
N1.to(P1)
N2.to(X1)
N3.to(P2)
N4.to(X2)
N5.to(P3)
N6.to(X3)
N7.to(P4)
N8.to(X4)
return SVG.format(body=s.t, title="ChaCha Quarter Round", w=372, h=y[-1]+64, **ARGS)
def chacha_encryption():
s = Svg()
B = s.block(64, 32, 4, 4, [
(1.5, 0, "const"),
(1.5, 1.5, "K"),
(0.5, 3, "count"),
(2.5, 3, "nonce"),
],
[])
P = s.text(16, 144, "P")
E = s.encrypt(64, 96)
X = s.xor(64, 144)
C = s.text(64, 180, "C")
return SVG.format(body=s.t, title="ChaCha Encryption", w=372, h=192, **ARGS)
def save(name, data):
f = open(f"{name}.svg", "w")
f.write(data)
@ -254,3 +398,4 @@ if __name__ == "__main__":
save("ctr", ctr())
save("cbc", cbc())
save("xts", xts())
save("../flash-filesystem-encryption-2/chacha-encryption", chacha_encryption())