OKlibrary  0.2.1.6
Cipher.mac
Go to the documentation of this file.
00001 /* Oliver Kullmann, 20.3.2011 (Swansea) */
00002 /* Copyright 2011, 2012 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/NumberTheory/Lisp/Auxiliary.mac")$
00023 oklib_include("OKlib/ComputerAlgebra/Cryptology/Lisp/CryptoSystems/DataEncryptionStandard/RoundFunction.mac")$
00024 oklib_include("OKlib/ComputerAlgebra/Cryptology/Lisp/CryptoSystems/DataEncryptionStandard/KeySchedule.mac")$
00025 oklib_include("OKlib/ComputerAlgebra/DataStructures/Lisp/Lists.mac")$
00026 
00027 des_init_permutation : [
00028 58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,
00029 62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,
00030 57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3,
00031 61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7]$
00032 
00033 des_final_permutation : [
00034 40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,
00035 38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,
00036 36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,
00037 34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25]$
00038 
00039 /* text and key are boolean vectors of length 64: */
00040 des_template(text, roundkeys) := block([previous,current,result],
00041  text : map(lambda([x],text[x]), des_init_permutation),
00042  previous : take_l(32,text),
00043  current : rest(text,32),
00044  for key in roundkeys do block(
00045   [new : des_round(previous,current,key)],
00046    previous : current,
00047    current : new
00048  ),
00049  result : append(current,previous),
00050  map(lambda([x],result[x]), des_final_permutation))$
00051 
00052 /* plaintext, ciphertext and key are boolean vectors of length 64: */
00053 des_encryption(plaintext, key) :=
00054  des_template(plaintext, des_round_keys(key))$
00055 des_decryption(ciphertext, key) :=
00056  des_template(ciphertext, reverse(des_round_keys(key)))$
00057 
00058 /* variants with an arbitrary number of rounds */
00059 des_encryption_gen(r, plaintext, key) :=
00060   des_template(plaintext, take_elements(r,des_round_keys(key)))$
00061 des_decryption_gen(r, plaintext, key) :=
00062   des_template(plaintext, reverse(take_elements(r,des_round_keys(key))))$
00063 
00064 des_validation_key : [
00065 0,0,0,0, 0,0,0,1, 0,0,1,0, 0,0,1,1, 0,1,0,0, 0,1,0,1, 0,1,1,0, 0,1,1,1,
00066 1,0,0,0, 1,0,0,1, 1,0,1,0, 1,0,1,1, 1,1,0,0, 1,1,0,1, 1,1,1,0, 1,1,1,1]$
00067 
00068 des_validation_plain :  [
00069 0,0,0,0, 0,0,0,1, 0,0,1,0, 0,0,1,1, 0,1,0,0, 0,1,0,1, 0,1,1,0, 0,1,1,1,
00070 1,0,0,0, 1,0,0,1, 1,0,1,0, 1,0,1,1, 1,1,0,0, 1,1,0,1, 1,1,1,0, 0,1,1,1]$
00071 
00072 des_validation_cipher : [
00073 1,1,0,0, 1,0,0,1, 0,1,0,1, 0,1,1,1, 0,1,0,0, 0,1,0,0, 0,0,1,0, 0,1,0,1,
00074 0,1,1,0, 1,0,1,0, 0,1,0,1, 1,1,1,0, 1,1,0,1, 0,0,1,1, 0,0,0,1, 1,1,0,1]$
00075 
00076 /* is(des_encryption(des_validation_plain,des_validation_key)=des_validation_cipher)
00077    = true
00078    is(des_decryption(des_validation_cipher, des_validation_key)=des_validation_plain)
00079    = true
00080 */
00081 
00082 /* The hexadecimal versions: */
00083 des_encryption_hex(plaintext, key) :=
00084  binv2hexstr(des_encryption(hexstr2binv(plaintext), hexstr2binv(key)))$
00085 des_decryption_hex(ciphertext, key) :=
00086  binv2hexstr(des_decryption(hexstr2binv(ciphertext), hexstr2binv(key)))$
00087 des_encryption_hex_gen(r, plaintext, key) :=
00088  binv2hexstr(des_encryption_gen(r, hexstr2binv(plaintext), hexstr2binv(key)))$
00089 des_decryption_hex_gen(r, ciphertext, key) :=
00090  binv2hexstr(des_decryption_gen(r, hexstr2binv(ciphertext), hexstr2binv(key)))$
00091 
00092 des_validation_key_hex : "15FBC08D31B0D521"$
00093 des_validation_plain_hex : "038E596D4841D03B"$
00094 des_validation_cipher_hex : "A2FB6032638EC79D"$
00095 
00096 /* is(des_encryption_hex(des_validation_plain_hex,des_validation_key_hex)=des_validation_cipher_hex)
00097    = true
00098    is(des_decryption_hex(des_validation_cipher_hex, des_validation_key_hex)=des_validation_plain_hex)
00099    = true
00100 */
00101