finished key scheduler

This commit is contained in:
sa 2022-03-24 02:39:02 -04:00
parent a2b900c990
commit c1dc8b34d7
2 changed files with 88 additions and 55 deletions

View File

@ -1,55 +0,0 @@
--[[
implementation of the RC5 cipher in Lua
03/11/2022
Sean Smith
sean@spacealien.xyz
w=word size in bits
r=number of rounds
b=key size in bytes
K=secret key
u=w/8 (the length of a word in bytes)
K[] is the key as an array of bytes
c=length of key in words
]]
local inspect = require 'inspect'
local insert, concat, modf, tostring, char = table.insert, table.concat, math.modf, tostring, string.char
local w=64
local r=24
local b=16
local key="globglogabgalab1"
local u=8 --length of a word in bytes
--local c=2 (with globglogabgalab1 as the key c should =2)
local L = {}
local t = 2 * (r + 1) --the number of round subkeys required
--magic constants for 64bit word size (hex)
local P = 0xb7e151628aed2a6b
local Q = 0x9e3779b97f4a7c15
--break K into words
--u = w / 8
c = math.ceil(math.max(b, 1) / u)
print(c)
--convert the key into an array of ints
local K = {string.byte(key, 1, -1)}
print(inspect(K))
L = K
print(inspect(L))
local S = {}
S[0] = P
S[1] = 1
print(t)
for i = 1, t - 1 do
S[i] = S[i-1] + Q
print(S[i])
i = i + 1
end

88
src/encrypt.lua Normal file
View File

@ -0,0 +1,88 @@
--[[
implementation of the RC5 cipher in Lua5.1
03/11/2022
Sean Smith
sean@spacealien.xyz
w=word size in bits
r=number of rounds
b=key size in bytes
K=secret key
u=w/8 (the length of a word in bytes)
K[] is the key as an array of bytes
c=length of key in words
]]
local inspect = require 'inspect'
local bit = require 'bit'
local insert, concat, modf, tostring, char = table.insert, table.concat, math.modf, tostring, string.char
local w=64
local r=24
local b=16
--local key="globglogabgalab1"
local key="0000000000000000"
local u=8 --length of a word in bytes
--local c=2 (with globglogabgalab1 as the key c should =2)
local t = 2 * (r + 1) --the number of round subkeys required
--convert the key into an array of ints (starts at K[1]
local K = {string.byte(key, 1, -1)}
local L = {}
L = K
--magic constants for 64bit word size (hex)
local P = 0xb7e151628aed2a6b
local Q = 0x9e3779b97f4a7c15
--break K into words
--u = w / 8
c = math.ceil(math.max(b, 1) / u)
--[[
print(inspect(K))
print("LLLLLL")
print(inspect(L))
]]
local S = {}
S[0] = P --initialize the S array
for i = 1, t do
S[i] = S[i-1] + Q
--print("S[i]")
--print(S[i])
--print(i)
i = i + 1
end
--mixing in the secret key
local i, j, A, B = 1, 1, 0, 0 --i flops between 1 and 2
for f = 1, 3 * math.max(t,c) do
--print(f)
--print("heres B")
--print(inspect(B))
--print("heres A")
--print(inspect(A))
print("heres L[]")
print(inspect(L))
--print("heres S[]")
--print(inspect(S))
--print("heres j")
--print(j)
--print("heres i")
--print(i)
S[i] = bit.rol((S[i] + A + B), 3)
A = S[i]
L[j] = bit.rol((L[j] + A + B), (A + B))
B = L[j]
i = (i % t) + 1
j = (j % c) + 1
--[[
due to lua prefering arrays starting at 1 instead
of zero, you need to add 1 so its filling array 1 and
2 instead of 0 and 1
]]
end