OKlibrary  0.2.1.6
Block.mac
Go to the documentation of this file.
00001 /* Matthew Gwynne, 21.01.2010 (Swansea) */
00002 /* Copyright 2010, 2011 Oliver Kullmann
00003 This file is part of the OKlibrary. OKlibrary is free software; you can redistribute
00004 it and/or modify it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation and included in this library; either version 3 of the
00006 License, or any later version. */
00007 
00022 oklib_include("OKlib/ComputerAlgebra/DataStructures/Lisp/Lists.mac")$
00023 
00024 
00025 /*
00026    There are 3 main representations of the AES message and key blocks
00027    (which are treated in the same way).
00028 
00029 TODO: "n_b = 4xn" ???
00030 
00031    The main representation is a n_b = 4xn matrix where the block size
00032    n_b is one of {128,164,192,224,256}, and this matrix corresponds
00033    exactly to the matrices as described in the AES standard.
00034 
00035 TODO: And, what does this mean?
00036 
00037    The elements
00038    of the matrix are polynomials.
00039 
00040    Another representation is that of a list of polynomials which lists
00041    the elements of the AES block column by column.
00042 
00043    The final representation is that of a list of integers which lists
00044    the elements of the AES block column by column where each element is
00045    an integer representing the polynomials as used in the above
00046    representations, where the exact details of the correspondence are given in
00047    polybin2nat etc. in ByteField.mac.
00048 */
00049 
00050 /* Converting polynomial matrices to polynomial lists and back: */
00051 rijn_m2l(M) := m2l_r(transpose(M))$
00052 rijn_l2m(L) := transpose(apply(matrix,partition_elements(L,4)))$
00053 
00054 /* Converting polynomial matrices to polynomial lists and back for
00055    matrices over arbitrary small scale fields and arbitrary matrices */
00056 ss_m2l(M) := m2l_r(transpose(M))$
00057 ss_l2m(L,n_R) := transpose(apply(matrix,partition_elements(L,n_R)))$
00058 
00059 /* Converting integer lists to polynomial matrices and back: */
00060 rijn_natl2m(L) := rijn_l2m(map(nat2polybin,L))$
00061 rijn_m2natl(M) := rijn_m2l(matrixmap(polybin2nat,M))$
00062 
00063 ss_natl2m(L,b,n_R) := 
00064   ss_l2m(map(lambda([n],nat2poly(n,b)),L),n_R)$
00065 ss_m2natl(M,b) := 
00066   ss_m2l(matrixmap(lambda([p],poly2nat(p,b)),M))$
00067 
00068 /* Converting small scale matrices of polynomials to the standard hexidecimal
00069    notation based on [Design of Rijndael] and used in [Small Scale Advanced
00070    Encryption Standard]. */
00071 ss_matrix2hex(m, b,e,mod_poly) := block([nl],
00072   nl : ss_m2natl(m,b),
00073   return(
00074     apply(sconcat,map(
00075         lambda([a],lpad(int2polyadicstr(a,16),"0", ceiling(fld(b^e)/fld(16)))),
00076         nl))
00077     ))$
00078 
00079 ss_hex2matrix(hex_str,b,e,mod_poly, num_rows) := block([hex_bits, hexl, nl],
00080   hex_bits : ceiling(fld(b^e)/fld(16)),
00081   hexl : map(lambda([a],apply(sconcat,a)),
00082     partition_elements(str2chrl(hex_str),hex_bits)),
00083   nl : map(lambda([a], polyadicstr2int(a,16)), hexl),
00084   return(ss_natl2m(nl, b, num_rows))
00085   )$
00086