"Fossies" - the Fresh Open Source Software Archive

Member "seed7/lib/tdes.s7i" (1 Apr 2017, 5972 Bytes) of package /linux/misc/seed7_05_20210223.tgz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 
    2 (********************************************************************)
    3 (*                                                                  *)
    4 (*  tdes.s7i      TDES (Triple DES) cipher support.                 *)
    5 (*  Copyright (C) 2014  Thomas Mertes                               *)
    6 (*                                                                  *)
    7 (*  This file is part of the Seed7 Runtime Library.                 *)
    8 (*                                                                  *)
    9 (*  The Seed7 Runtime Library is free software; you can             *)
   10 (*  redistribute it and/or modify it under the terms of the GNU     *)
   11 (*  Lesser General Public License as published by the Free Software *)
   12 (*  Foundation; either version 2.1 of the License, or (at your      *)
   13 (*  option) any later version.                                      *)
   14 (*                                                                  *)
   15 (*  The Seed7 Runtime Library is distributed in the hope that it    *)
   16 (*  will be useful, but WITHOUT ANY WARRANTY; without even the      *)
   17 (*  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *)
   18 (*  PURPOSE.  See the GNU Lesser General Public License for more    *)
   19 (*  details.                                                        *)
   20 (*                                                                  *)
   21 (*  You should have received a copy of the GNU Lesser General       *)
   22 (*  Public License along with this program; if not, write to the    *)
   23 (*  Free Software Foundation, Inc., 51 Franklin Street,             *)
   24 (*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
   25 (*                                                                  *)
   26 (********************************************************************)
   27 
   28 
   29 include "bin32.s7i";
   30 include "cipher.s7i";
   31 include "des.s7i";
   32 
   33 
   34 (**
   35  *  [[cipher|cipherState]] implementation type describing the state of a TDES cipher.
   36  *  The data is encrypted / decrypted with the TDES (Triple DES)
   37  *  block cipher.
   38  *)
   39 const type: tdesState is new struct
   40     var array bin32: encryptionSubKey1 is 0 times bin32.value;
   41     var array bin32: encryptionSubKey2 is 0 times bin32.value;
   42     var array bin32: encryptionSubKey3 is 0 times bin32.value;
   43     var array bin32: decryptionSubKey1 is 0 times bin32.value;
   44     var array bin32: decryptionSubKey2 is 0 times bin32.value;
   45     var array bin32: decryptionSubKey3 is 0 times bin32.value;
   46     var string: cipherBlock is "";
   47   end struct;
   48 
   49 
   50 type_implements_interface(tdesState, cipherState);
   51 
   52 
   53 (**
   54  *  Block size used by the TDES (Triple DES) block cipher.
   55  *  @return the block size used by the TDES cipher.
   56  *)
   57 const func integer: blockSize (TDES) is 8;
   58 
   59 
   60 (**
   61  *  Set key and initialization vector for the TDES (Triple DES) block cipher.
   62  *  @param desKey The key to be used for TDES.
   63  *  @param initializationVector The initialisation vector (IV) for TDES.
   64  *  @return the TDES (Triple DES) cipher state.
   65  *)
   66 const func tdesState: setTdesKey (in string: desKey, in string: initializationVector) is func
   67   result
   68     var tdesState: state is tdesState.value;
   69   begin
   70     state.encryptionSubKey1 := setDesKey(desKey[1 len 8]);
   71     state.decryptionSubKey1 := reverseKeyScheduleOrder(state.encryptionSubKey1);
   72     state.encryptionSubKey2 := setDesKey(desKey[9 len 8]);
   73     state.decryptionSubKey2 := reverseKeyScheduleOrder(state.encryptionSubKey2);
   74     state.encryptionSubKey3 := setDesKey(desKey[17 len 8]);
   75     state.decryptionSubKey3 := reverseKeyScheduleOrder(state.encryptionSubKey3);
   76     state.cipherBlock := initializationVector;
   77   end func;
   78 
   79 
   80 (**
   81  *  Set key and initialization vector for the TDES (Triple DES) block cipher.
   82  *  @param cipherKey The key to be used for TDES.
   83  *  @param initializationVector The initialisation vector (IV) for TDES.
   84  *  @return the initial ''cipherState'' of a TDES cipher.
   85  *)
   86 const func cipherState: setCipherKey (TDES, in string: cipherKey,
   87     in string: initializationVector) is
   88   return toInterface(setTdesKey(cipherKey, initializationVector));
   89 
   90 
   91 (**
   92  *  Encode a string with the TDES (Triple DES) block cipher.
   93  *  @return the encoded string.
   94  *)
   95 const func string: encode (inout tdesState: state, in string: plaintext) is func
   96   result
   97     var string: encoded is "";
   98   local
   99     var integer: index is 0;
  100     var integer: subIndex is 0;
  101     var string: dataBlock is "";
  102     var string: cipherBlock is "";
  103   begin
  104     for index range 1 to length(plaintext) step blockSize(TDES) do
  105       dataBlock := "";
  106       for subIndex range 1 to blockSize(TDES) do
  107         dataBlock &:= chr(ord(bin32(plaintext[pred(index + subIndex)]) ><
  108                               bin32(state.cipherBlock[subIndex])));
  109       end for;
  110       cipherBlock := processDesBlock(state.encryptionSubKey3,
  111                      processDesBlock(state.decryptionSubKey2,
  112                      processDesBlock(state.encryptionSubKey1, dataBlock)));
  113       state.cipherBlock := cipherBlock;
  114       encoded &:= cipherBlock;
  115     end for;
  116   end func;
  117 
  118 
  119 (**
  120  *  Decode a string with the TDES (Triple DES) block cipher.
  121  *  @return the decoded string.
  122  *)
  123 const func string: decode (inout tdesState: state, in string: encoded) is func
  124   result
  125     var string: plaintext is "";
  126   local
  127     var integer: index is 0;
  128     var integer: subIndex is 0;
  129     var string: cipherBlock is "";
  130     var string: dataBlock is "";
  131     var string: plainBlock is "";
  132   begin
  133     for index range 1 to length(encoded) step blockSize(TDES) do
  134       cipherBlock := encoded[index len blockSize(TDES)];
  135       dataBlock := processDesBlock(state.decryptionSubKey1,
  136                    processDesBlock(state.encryptionSubKey2,
  137                    processDesBlock(state.decryptionSubKey3, cipherBlock)));
  138       for subIndex range 1 to blockSize(TDES) do
  139         plaintext &:= chr(ord(bin32(dataBlock[subIndex]) ><
  140                               bin32(state.cipherBlock[subIndex])));
  141       end for;
  142       state.cipherBlock := cipherBlock;
  143     end for;
  144   end func;