高级加密标准 (AES) 多项式乘法,以八度/矩阵为单位



我正在尝试执行多项式乘法,如高级加密标准(AES)草案中所述。

这是我到目前为止所拥有的:

function  y  =  multiply_x2( a )
  % a is a 1 x 8 binary vector 
  % y is a 1 x 8 binary vector 
  % t is a 1 x 8 binary vector corresponding to the "AES irreducible polynomial"
  y  =  [ a(2:8) 0 ];     % Multiply byte 'a' by 2 using "left shift" 
  t  =  [ 0 0 0 a(1) a(1) 0 a(1) a(1) ]; %  't' only becomes the "AES irreducible
                                         %  polynomial" if a(1) == 1, otherwise
                                         %  it becomes the "zero" byte array
  y  =  mod( y + t , 2 ) ;   % XOR operation on y and t, as described in AES.                
end

上面的代码用于

"y = {02} .一"

(其中"{}"表示十六进制表示法,其二进制表示可以解释为多项式中x各自幂的预感。例如,根据 AES 文档,{02}对应于 00000010,对应于多项式"x",{06}对应于"x2+x"等)

我想将a乘以0e090d0b。他们每个人的代码将如何?即:

"y= ( {0e} .一)"
"y= ( {09} .一)"
"y= ( {0d} .一)"
"y= ( {02} .一)"

这是一个有趣的问题。这是您链接的 AES 文档中定义的乘法通用解决方案。我使用名称 xtime 进行{02}乘法,并将"加法"(xadd)实现为 XOR 运算(即 != ) 直接,因为这更容易。

帮助程序函数:

%% in file isByte.m
function Out = isByte (a)
  a = a(:).'; % ensure horizontal vector representation
  if all (a == 0 | a == 1) && length (a) == 8; Out = true; return; end
  Out = false;
end

%% in file byte2hex.m
function Out = byte2hex (a)
  a = a(:).'; % ensure horizontal vector
  assert (isByte (a), 'Input needs to be a valid "byte" array');
  Out = sum (a .* ([2,2,2,2,2,2,2,2] .^ [7:-1:0])); Out = dec2hex (Out);  
end

%% in file hex2byte.m
function Out = hex2byte (h)
  assert (isxdigit (h) && hex2dec (h) < 256, 'Input needs to be a valid hex number below FF');
  Out = dec2bin (hex2dec (h));  Out = sprintf ('%8s', Out); 
  Out = Out - 48             ;  Out(Out == -16) = 0; % convert spaces to zeros
end

多项式函数:

%% in file xadd.m
function Out = xadd (a, b)
  a = a(:).'; b = b(:).'; % ensure horizontal vector representations
  assert (isByte (a) && isByte (b), 'Inputs need to be valid "byte" arrays');
  Out = a != b;
end

%% in file xtime.m
function Out = xtime (a)
  a = a(:).'; % ensure horizontal vector
  assert (isByte (a), 'Input needs to be a valid "byte" array');
  Out = [a(2 : 8), 0]; % left shift
  if a(1) == 1
    Out = xadd (Out, [0, 0, 0, 1, 1, 0, 1, 1]);  % subtract (= add) the AES m(x) polynomial
  end  
end

% in file xmultiply.m
function Out = xmultiply (a, b)
  a = a(:)'; b = b(:)'; % ensure horizontal vector representations
  assert (isByte(a) && isByte(b), 'Inputs need to be valid "byte" arrays');
  Out = [0,0,0,0,0,0,0,0];
  if a == [0, 0, 0, 0, 0, 0, 0, 0] || b == [ 0, 0, 0, 0, 0, 0, 0, 0]; return; end
  if b(8) == 1; Out = xadd (Out, a); end % yes this could be done recursively but, why bother.
  if b(7) == 1; Out = xadd (Out, xtime (a)); end
  if b(6) == 1; Out = xadd (Out, xtime (xtime (a))); end
  if b(5) == 1; Out = xadd (Out, xtime (xtime (xtime (a)))); end
  if b(4) == 1; Out = xadd (Out, xtime (xtime (xtime (xtime (a))))); end
  if b(3) == 1; Out = xadd (Out, xtime (xtime (xtime (xtime (xtime (a)))))); end
  if b(2) == 1; Out = xadd (Out, xtime (xtime (xtime (xtime (xtime (xtime (a))))))); end
  if b(1) == 1; Out = xadd (Out, xtime (xtime (xtime (xtime (xtime (xtime (xtime (a)))))))); end  
end

使用示例:(与 AES 文档中的示例相同)

octave:1> a = hex2byte("57")
  a =
     0   1   0   1   0   1   1   1
octave:2> b = hex2byte("13")
  b =
     0   0   0   1   0   0   1   1
octave:3> c = xmultiply(a, b)
  c =
     1  1  1  1  1  1  1  0
octave:4> byte2hex(c)
  ans = FE

在 MATLAB/Octave 中,convdeconv 分别对应于多项式的乘法和(除法/模)运算。

function out = multiply(A, x)
    mult = mod(conv(A,x), 2); % poynomial multiplication
    [~, modulo] = deconv(mult, [1 0 0 0 1 1 0 1 1]); %modulo operation
    out = mod(modulo(end-7:end) , 2); %extract last 8 bits
end

例如,乘以0x570x13

a = [1 0 1 0 1 1 1]; %0x57
b = [1 0 0 1 1]; %0x13
result = multiply(a,b)
result = 
    1   1   1   1   1   1   1   0

这是0xFE的二进制表示

感谢您的关注。我正在尝试在 matlab 中实现 AES。我在 http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf 的页面中找到了解决方案.这是y= ( {09} . a )乘法运算的函数;

function  y  =  multiply_x9( a )
% Multiply byte A by 9 over finite field of AES
y2  =  multiply_x2( a ) ;
y4  =  multiply_x2( y2 ) ;
y8  =  multiply_x2( y4 ) ;
y  =  mod( y8 + a , 2 ) ;
end

并且对于任何矩阵乘法multiply_xx (a, b, p )都可以使用函数。这是函数;

function  y  =  multiply_xx (a, b, p ) 

% Determine input lengths and check if they are equal
n  =  length( a ) ;
if  ( n ~= length(b) ) ,
    error( 'Operand input lengths not equal to each other!' ) ;
elseif  ( n ~= length(p) ) ,
    error( 'Operand input lengths not equal to modulus length!' ) ;
end

% Initialize result to zeros and start iteration row by row
y  =  zeros( 1 , n ) ;
for  i  =  1  :  n ,
    m  =  a(i) * b ;
    y  =  mod( y(1) * p  +  [ y(2:n) 0 ]  +  m , 2 ) ;
end

end

最新更新