des_block
index
/home/guilley/dpacontest/code/reference/des_block.py

des_block.py implements the DES cryptographic algorithm
# allowing to compute any intermediate value.
# Copyright (C) 2008 Florent Flament (florent.flament@telecom-paristech.fr)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

 
Classes
       
des_block

 
class des_block
    Class providing every operations done to a message in the DES datapath.
 
  Methods defined here:
__init__(self, hex_msg=None, nbits=0)
Constructor takes the message from which the des_block will be initialized.
It must be an hexadecimal string representation of a 64 bits message.
Example: 0f564a334af654ab
__len__(self)
__str__(self)
Returns the string representation in hexadecimal of the data stored
in the structure.
concat(self, db)
Return the concatenated des_block between self and db.
e(self)
Returns a des_block resulting of the application of the
E (Expansion) function on the current des_block
encipher(self, key)
Encipher the block as a clear message into a cryptogram.
According to the given key, with the DES algorithm.
f(self, k)
Returns the result of the f function for the given k key.
fill(self, db)
Returns a des_block where bit set to None are filled with bits from db.
db must be another des_block
get(self, n)
Return the n'th element of the block
hw(self)
Returns the hamming weight of the block
ip(self, direc=1)
Returns a des_block resulting of the application of the
IP (Initial Permutation) function on the current des_block
ls(self, n)
Returns the result of left shifting the data by n bits.
p(self, direc=1)
Apply the P permutation to this des_block and returns the result.
dir is the direction in which to apply the permutation.
1 for the forward direction
-1 for the backward direction
Defaults to forward.
pc1(self, direc=1)
Apply the PC1 permutation and returns the result.
pc2(self, direc=1)
Apply the PC2 permutation and returns the result.
rs(self, n)
Returns the result of right shifting the data by n bits.
s(self, n_sbox)
Returns the output of the n_sbox sbox with des_block data for
its inputs.
subblock(self, begin, end)
Returns the subblock of data between begin and end (not included).
value(self)
Returns the integer value corresponding to this block.
xor(self, db)
Returns the results of __data xor db.

 
Functions
       
__from_int__(i_msg, nbits)
Builds a des_block on nbits bits from the given i_msg int.
Should only be used by internal functions.
__from_sequence__(seq)
Builds a des_block from the given sequence (seq).
Should only by used by des_block internals.
__int_to_seq__(i_msg, nbits)
Integer to bool vector conversion.
For internal use only.
test()

 
Data
        __E_PERM__ = (31, 0, 1, 2, 3, 4, 3, 4, 5, 6, 7, 8, 7, 8, 9, 10, 11, 12, 11, 12, ...)
__IP_PERM__ = (57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, ...)
__MSG_SIZE__ = 64
__PC1_PERM__ = (56, 48, 40, 32, 24, 16, 8, 0, 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, ...)
__PC2_PERM__ = (13, 16, 10, 23, 0, 4, 2, 27, 14, 5, 20, 9, 22, 18, 11, 3, 25, 7, 15, 6, ...)
__PS_PERM__ = (0, 5, 1, 2, 3, 4)
__P_PERM__ = (15, 6, 19, 20, 28, 11, 27, 16, 0, 14, 22, 25, 4, 17, 30, 9, 1, 7, 23, 13, ...)
__S_LUT__ = ((14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 0, 15, 7, 4, ...), (15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 3, 13, 4, 7, ...), (10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 13, 7, 0, 9, ...), (7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 13, 8, 11, 5, ...), (2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 14, 11, 2, 12, ...), (12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 10, 15, 4, 2, ...), (4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13, 0, 11, 7, ...), (13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1, 15, 13, 8, ...))