Hello ,
i have a little error with matrix dimensions
Can you help me ?
%KEY EXPANSION FUNCTION--------------
function [S] = setup(K_str)
% call global variables
global w;global r;global b;
% required parameters
t=2*(r+1); % expanded key array length
u=w/8; % number of bytes per word (4)
c=floor(max(b,1))/u; % words per key
m=uint64(pow2(w)); % modulo parameters for modulo sums
Kxxxx='91 5F 46 19 BE 41 B2 51 63 55 A5 01 10 A9 CE 91';
% process secret key
K_ch=char(strrep(Kxxxx,' ','')); % turn into char array
% split the secret key into bytes
for i=1:2:length(K_ch)
K(0.5*(i+1),:)=['0x' K_ch(i:i+1)]; % add 0x prefix and divide by bytes
end
K=uint64(str2num(K)); % convert to unsigned integer
% look-up table for magit constants
P=uint64(((w==16)*47073)+((w==32)*3084996963)+((w==64)*13249961062380153451));
Q=uint64(((w==16)*40503)+((w==32)*2654435769)+((w==64)*11400714819323198485));
% convert key from bytes to words
L=uint64(zeros(c,1));
for i=b:-1:1
L(ceil(i/u),:)=bitshift(L(ceil(i/u),:),8)+K(i,:);
end
% fill expanded key array, sub-keys
S(1,1)=P;
for i=2:t
S(i,1)=mod(S(i-1,1)+Q,m);
end
% mix the secret key
i=0;j=0;%loop variables
A=0;B=0;%loop variables
for k=0:3*max(t,c)-1
%update subkey array
S(i+1,:)=ROTL(mod(S(i+1,:)+(A+B),m),3,w);
A=S(i+1,:);
%update word array
L(j+1,:)=ROTL(mod(L(j+1,:)+(A+B),m),(A+B),w);
B=L(j+1,:);
%update indexes
i=mod(i+1,t);
j=mod(j+1,c);
end
function q=ROTL(x,y,w)
q=mod(bitor(bitshift(x,bitand(y,(w-1))),...
bitsra(x,w-bitand(y,(w-1)))),pow2(w));
end
end

7 Kommentare

Rik
Rik am 19 Nov. 2021
You are indexing variables based on i, which is equal to b. Since b is a global, we have no idea what value it might have. Why are you using globals anyway?
sorry for this mistake :)
b = 16 ;
b=16;
w=16;
r=1;
t=2*(r+1); % expanded key array length
u=w/8; % number of bytes per word (4)
c=floor(max(b,1))/u; % words per key
m=uint64(pow2(w)); % modulo parameters for modulo sums
Kxxxx='91 5F 46 19 BE 41 B2 51 63 55 A5 01 10 A9 CE 91';
% process secret key
K_ch=char(strrep(Kxxxx,' ','')); % turn into char array
% split the secret key into bytes
for i=1:2:length(K_ch)
K(0.5*(i+1),:)=['0x' K_ch(i:i+1)]; % add 0x prefix and divide by bytes
end
K=uint64(str2num(K)); % convert to unsigned integer
% look-up table for magit constants
P=uint64(((w==16)*47073)+((w==32)*3084996963)+((w==64)*13249961062380153451));
Q=uint64(((w==16)*40503)+((w==32)*2654435769)+((w==64)*11400714819323198485));
% convert key from bytes to words
L=uint64(zeros(c,1));
for i=b:-1:1
L(ceil(i/u),:)=bitshift(L(ceil(i/u),:),8)+K(i,:);
end
As you can see, this runs without error.
You should consider not using global variables. They are a bad design idea. Any function can edit them, especially with such short names. There is almost always a way around using them.
It gives me the same error as before
The problem occurs because of this line.
K=uint64(str2num(K));
Here, str2num() takes a cell array of hex bytes and returns an empty numeric vector. This appears to work in later versions (tested in R2019b), but not in R2015b. Knowing when the behavior of str2num() changed would be difficult and ultimately pointless. You'll have to come up with another way to convert K to a numeric form.
Rik
Rik am 19 Nov. 2021
Since str2num calls eval, it actually isn't that hard to find out. A quick search for 'hexadecimal' in the release notes is enough to see that it was introduced in R2019b.
DGM
DGM am 19 Nov. 2021
Ah. Well, I was foolish enough to instead search the release notes for 'str2num'. I'm utterly used to the absence of results being an indicator that the target information has simply been purged from what's available online.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

DGM
DGM am 19 Nov. 2021
Bearbeitet: DGM am 19 Nov. 2021

0 Stimmen

Replace this line
K=uint64(str2num(K));
with this
K = uint64(hex2dec(K(:,3:4)));
That should work in older versions, and the results returned are the same as the original code in newer versions.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2016a

Tags

Gefragt:

am 19 Nov. 2021

Kommentiert:

DGM
am 19 Nov. 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by