Class: Kibuvits_krl171bt4_cryptcodec_txor_t1

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/kibuvits_ruby_library_krl171bt4_.rb

Overview

Constant Summary collapse

@@i_n_of_datasalt_digits =
7
@@i_key_param_max_number_of_bytes_per_character =
7

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKibuvits_krl171bt4_cryptcodec_txor_t1



21683
21684
21685
21686
21687
21688
21689
21690
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 21683

def initialize
   @s_key_type_t1="kibuvits_krl171bt4_series_of_whole_numbers_t2"
   @s_lc_s_key_type="s_key_type"
   @s_lc_i_n_of_datasalt_digits="i_n_of_datasalt_digits".freeze
   @s_lc_i_saltfree_data_max="i_saltfree_data_max".freeze
   @s_cryptocodec_type_t1="kibuvits_krl171bt4_wearlevelling_t1d"
   @s_lc_s_key_id="s_key_id"
end

Class Method Details

.ht_deserialize_key(s_progfte) ⇒ Object

ht_deserialize_key



21846
21847
21848
21849
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 21846

def Kibuvits_krl171bt4_cryptcodec_txor_t1.ht_deserialize_key(s_progfte)
   ht_out=Kibuvits_krl171bt4_cryptcodec_txor_t1.instance.ht_deserialize_key(s_progfte)
   return ht_out
end

.ht_generate_key_t1(i_key_length, i_max_number_of_bytes_per_character = @@i_key_param_max_number_of_bytes_per_character) ⇒ Object

ht_generate_key_t1



21759
21760
21761
21762
21763
21764
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 21759

def Kibuvits_krl171bt4_cryptcodec_txor_t1.ht_generate_key_t1(i_key_length,
   i_max_number_of_bytes_per_character=@@i_key_param_max_number_of_bytes_per_character)
   ht_key=Kibuvits_krl171bt4_cryptcodec_txor_t1.instance.ht_generate_key_t1(
   i_key_length,i_max_number_of_bytes_per_character)
   return ht_key
end

.s_decrypt_wearlevelling_t1(s_ciphertext_with_header, ht_key, msgcs) ⇒ Object

s_decrypt_wearlevelling_t1



22308
22309
22310
22311
22312
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 22308

def Kibuvits_krl171bt4_cryptcodec_txor_t1.s_decrypt_wearlevelling_t1(s_ciphertext_with_header,ht_key,msgcs)
   s_out=Kibuvits_krl171bt4_cryptcodec_txor_t1.instance.s_decrypt_wearlevelling_t1(
   s_ciphertext_with_header,ht_key,msgcs)
   return s_out
end

.s_encrypt_wearlevelling_t1(s_cleartext, ht_key, s_prefix_of_the_output_string = $kibuvits_krl171bt4_lc_emptystring, i_estimated_median_of_lengths_of_nonnormalized_cleartexts = Kibuvits_krl171bt4_cleartext_length_normalization.i_const_t1, i_estimated_standard_deviation_of_lengths_of_nonnormalized_cleartexts = ( (i_estimated_median_of_lengths_of_nonnormalized_cleartexts.to_r/3).floor*2)) ⇒ Object



22226
22227
22228
22229
22230
22231
22232
22233
22234
22235
22236
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 22226

def Kibuvits_krl171bt4_cryptcodec_txor_t1.s_encrypt_wearlevelling_t1(s_cleartext,ht_key,
   s_prefix_of_the_output_string=$kibuvits_krl171bt4_lc_emptystring,
   i_estimated_median_of_lengths_of_nonnormalized_cleartexts=Kibuvits_krl171bt4_cleartext_length_normalization.i_const_t1,
   i_estimated_standard_deviation_of_lengths_of_nonnormalized_cleartexts=(
   (i_estimated_median_of_lengths_of_nonnormalized_cleartexts.to_r/3).floor*2))
   s_out=Kibuvits_krl171bt4_cryptcodec_txor_t1.instance.s_encrypt_wearlevelling_t1(
   s_cleartext,ht_key,s_prefix_of_the_output_string,
   i_estimated_median_of_lengths_of_nonnormalized_cleartexts,
   i_estimated_standard_deviation_of_lengths_of_nonnormalized_cleartexts)
   return s_out
end

.s_serialize_key(ht_key) ⇒ Object

s_serialize_key



21821
21822
21823
21824
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 21821

def Kibuvits_krl171bt4_cryptcodec_txor_t1.s_serialize_key(ht_key)
   s_out=Kibuvits_krl171bt4_cryptcodec_txor_t1.instance.s_serialize_key(ht_key)
   return s_out
end

Instance Method Details

#ht_deserialize_key(s_progfte) ⇒ Object




21828
21829
21830
21831
21832
21833
21834
21835
21836
21837
21838
21839
21840
21841
21842
21843
21844
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 21828

def ht_deserialize_key(s_progfte)
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding()
      kibuvits_krl171bt4_typecheck bn, String, s_progfte
   end # if
   ht_out=Kibuvits_krl171bt4_ProgFTE.to_ht(s_progfte)
   s_ar=ht_out[$kibuvits_krl171bt4_lc_ar]
   ar_of_s=s_ar.split($kibuvits_krl171bt4_lc_pillar)
   i_len=ar_of_s.size
   ar=Array.new
   i_len.times{|i| ar<<ar_of_s[i].to_i(16)}
   ht_out[$kibuvits_krl171bt4_lc_ar]=ar
   ht_out[$kibuvits_krl171bt4_lc_i_m]=ht_out[$kibuvits_krl171bt4_lc_i_m].to_i
   ht_out[@s_lc_i_n_of_datasalt_digits]=ht_out[@s_lc_i_n_of_datasalt_digits].to_i
   ht_out[@s_lc_i_saltfree_data_max]=ht_out[@s_lc_i_saltfree_data_max].to_i
   return ht_out
end

#ht_generate_key_t1(i_key_length, i_max_number_of_bytes_per_character = @@i_key_param_max_number_of_bytes_per_character) ⇒ Object



21712
21713
21714
21715
21716
21717
21718
21719
21720
21721
21722
21723
21724
21725
21726
21727
21728
21729
21730
21731
21732
21733
21734
21735
21736
21737
21738
21739
21740
21741
21742
21743
21744
21745
21746
21747
21748
21749
21750
21751
21752
21753
21754
21755
21756
21757
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 21712

def ht_generate_key_t1(i_key_length,
   i_max_number_of_bytes_per_character=@@i_key_param_max_number_of_bytes_per_character)
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding()
      kibuvits_krl171bt4_typecheck bn, [Fixnum,Bignum], i_key_length
      kibuvits_krl171bt4_typecheck bn, Fixnum, i_max_number_of_bytes_per_character
      kibuvits_krl171bt4_assert_is_smaller_than_or_equal_to(bn,
      1, [i_key_length,i_max_number_of_bytes_per_character],
      "\n GUID='14a83a21-b734-4702-9143-c13290a118e7'")
   end # if
   ht_key=Hash.new
   ht_key[@s_lc_i_n_of_datasalt_digits]=@@i_n_of_datasalt_digits
   ht_key[@s_lc_s_key_id]=Kibuvits_krl171bt4_GUID_generator.generate_GUID
   i_number_of_saltfree_values_max=2**(8*i_max_number_of_bytes_per_character)
   i_saltfree_data_max=i_number_of_saltfree_values_max-1
   ht_key[@s_lc_i_saltfree_data_max]=i_saltfree_data_max

   # The m is a term from the TXOR specification.
   # http://longterm.softf1.com/specifications/txor/
   i_m=i_number_of_saltfree_values_max*(10**@@i_n_of_datasalt_digits)+
   Kibuvits_krl171bt4_rng.i_random_t1(10**4) # + random to counter rainbow table style attacks

   ht_key[$kibuvits_krl171bt4_lc_i_m]=i_m
   ar=Array.new
   i_key_length.times{ar<<Kibuvits_krl171bt4_rng.i_random_t1(i_m)}
   ht_key[$kibuvits_krl171bt4_lc_ar]=ar
   ht_key[@s_lc_s_key_type]=@s_key_type_t1
   #----------
   # "mode_1" might be a mode, where the key
   # is being used as the traditional one-time pad,
   # where key characters are used up sequentially.
   #
   # "mode_2" is a one-time pad,
   # where key characters are chosen the way they are
   # chosen in mode_0.
   #
   # There might be other modes, that shift the
   # random selection target window among the array of
   # key characters.
   #
   # The thing to note is that decryption algorithm
   # does not depend on the key wearlevelling mode.
   ht_key["s_wearlevelling_mode"]="mode_0"
   #----------
   return ht_key
end

#s_decrypt_wearlevelling_t1(s_ciphertext_with_header, ht_key, msgcs) ⇒ Object




22240
22241
22242
22243
22244
22245
22246
22247
22248
22249
22250
22251
22252
22253
22254
22255
22256
22257
22258
22259
22260
22261
22262
22263
22264
22265
22266
22267
22268
22269
22270
22271
22272
22273
22274
22275
22276
22277
22278
22279
22280
22281
22282
22283
22284
22285
22286
22287
22288
22289
22290
22291
22292
22293
22294
22295
22296
22297
22298
22299
22300
22301
22302
22303
22304
22305
22306
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 22240

def s_decrypt_wearlevelling_t1(s_ciphertext_with_header,ht_key,msgcs)
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding()
      kibuvits_krl171bt4_typecheck bn, String, s_ciphertext_with_header
      exc_verify_t1(bn,ht_key)
      kibuvits_krl171bt4_typecheck bn, Kibuvits_krl171bt4_msgc_stack, msgcs
      msgcs.assert_lack_of_failures(
      "GUID='f6de4c49-bc06-4bb2-8232-c13290a118e7'")
   end # if
   #--------------
   s_out=$kibuvits_krl171bt4_lc_emptystring
   s_header_progfte,s_ciphertext=Kibuvits_krl171bt4_str.s_s_bisect_by_header_t1(
   s_ciphertext_with_header,msgcs)
   return s_out if msgcs.b_failure
   #--------------
   ht=Kibuvits_krl171bt4_ProgFTE.to_ht(s_header_progfte)
   s_cryptocodec_type=ht["s_cryptocodec_type"]
   if s_cryptocodec_type!=@s_cryptocodec_type_t1
      s_default_msg="s_cryptocodec_type == "+s_cryptocodec_type+
      " != "+@s_cryptocodec_type_t1
      s_message_id="cryptocodec_mismatch_t1"
      b_failure=true
      msgcs.cre(s_default_msg,s_message_id,b_failure,
      "54404ac3-5112-4872-9f92-c13290a118e7")
      return s_out
   end # if
   #-----
   s_key_type=ht_key[@s_lc_s_key_type]
   if s_key_type!=@s_key_type_t1
      # The throw is used here because this
      # condition indicates that the code that
      # calls this decryption function is probably,
      # but not necessarily, flawed. The existence of the
      # flaw is somewhat questionable, because
      # if only one type of cryptcodec is used, then
      # there's no point of verifying the key type.
      kibuvits_krl171bt4_throw("ht_key[\"s_key_type\"] == "+s_key_type+
      " != "+@s_key_type_t1+
      "\n GUID='befcbf4e-4294-435b-9212-c13290a118e7'")
   end # if
   #-----
   # The key ID verification has to be _after_ the
   # key type verification, because a key of wrong type
   # probably fails ID verification and if the ID verification
   # is before key type verification, the flaw that
   # causes the key type mismatch is not hinted.
   s_key_id_0=ht_key[@s_lc_s_key_id]
   s_key_id_1=ht[@s_lc_s_key_id]
   if s_key_id_0!=s_key_id_1
      s_default_msg="The ID of the key is == "+s_key_id_0+
      "\n, but the ciphertext has been encrypted with a "+
      "key that has an ID of \n"+s_key_id_1+
      $kibuvits_krl171bt4_lc_doublelinebreak
      s_message_id="decrytion_key_mismatch_t1"
      b_failure=true
      msgcs.cre(s_default_msg,s_message_id,b_failure,
      "6ab19f22-abc5-49e9-9562-c13290a118e7")
      return s_out
   end # if
   ht.clear
   #--------------
   s_cleartext_normalized=s_decrypt_wearlevelling_t1_core(s_ciphertext,ht_key,msgcs)
   s_cleartext=Kibuvits_krl171bt4_cleartext_length_normalization.s_normalize_t1_extract_cleartext(
   s_cleartext_normalized,msgcs)
   s_out=s_cleartext
   return s_out
end

#s_encrypt_wearlevelling_t1(s_cleartext, ht_key, s_prefix_of_the_output_string = $kibuvits_krl171bt4_lc_emptystring, i_estimated_median_of_lengths_of_nonnormalized_cleartexts = Kibuvits_krl171bt4_cleartext_length_normalization.i_const_t1, i_estimated_standard_deviation_of_lengths_of_nonnormalized_cleartexts = ( (i_estimated_median_of_lengths_of_nonnormalized_cleartexts.to_r/3).floor*2)) ⇒ Object

The client code must verify, that the ht_key==“kibuvits_krl171bt4_series_of_whole_numbers_t1” Otherwise this method throws an exception.

The s_prefix_of_the_output_string is a considerable speed-hack that is based on the following scheme:

s_armouring_header=s_prefix_of_the_output_string
s_output=s_armouring_header+s_ciphertext_with_its_own_header

The speed increase comes from avoiding

s_output=s_something_quite_short+s_something_very_long

by using the kibuvits_krl171bt4_s_concat_array_of_strings(…) and this is not a joke, because the “s_something_very_long” contains the whole encrypted message and really can be a considerably long temporary string, taking up tens of megabytes of memory.

This crypto-algorithm is probably not affected by quantum computer based attacks, because it does not rely on any kind of factorization, nor does it explicitly rely on the lack of knowledge about some function.



22174
22175
22176
22177
22178
22179
22180
22181
22182
22183
22184
22185
22186
22187
22188
22189
22190
22191
22192
22193
22194
22195
22196
22197
22198
22199
22200
22201
22202
22203
22204
22205
22206
22207
22208
22209
22210
22211
22212
22213
22214
22215
22216
22217
22218
22219
22220
22221
22222
22223
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 22174

def s_encrypt_wearlevelling_t1(s_cleartext,ht_key,
   s_prefix_of_the_output_string=$kibuvits_krl171bt4_lc_emptystring,
   i_estimated_median_of_lengths_of_nonnormalized_cleartexts=Kibuvits_krl171bt4_cleartext_length_normalization.i_const_t1,
   i_estimated_standard_deviation_of_lengths_of_nonnormalized_cleartexts=(
   (i_estimated_median_of_lengths_of_nonnormalized_cleartexts.to_r/3).floor*2))
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding()
      exc_verify_t1(bn,ht_key)
      kibuvits_krl171bt4_typecheck bn, String, s_cleartext
      kibuvits_krl171bt4_typecheck bn, Hash, ht_key
      kibuvits_krl171bt4_typecheck bn, String, s_prefix_of_the_output_string
      kibuvits_krl171bt4_typecheck bn, [Fixnum,Bignum], i_estimated_median_of_lengths_of_nonnormalized_cleartexts
      kibuvits_krl171bt4_typecheck bn, [Fixnum,Bignum], i_estimated_standard_deviation_of_lengths_of_nonnormalized_cleartexts
      kibuvits_krl171bt4_assert_is_smaller_than_or_equal_to(bn,
      0, i_estimated_median_of_lengths_of_nonnormalized_cleartexts,
      "\n GUID='834d4c1d-1136-431d-a172-c13290a118e7'\n\n")
      kibuvits_krl171bt4_assert_is_smaller_than_or_equal_to(bn,
      0, i_estimated_standard_deviation_of_lengths_of_nonnormalized_cleartexts,
      "\n GUID='5466ee23-acfa-497d-b452-c13290a118e7'\n\n")
   end # if
   #-----
   s_key_type=ht_key[@s_lc_s_key_type]
   if s_key_type!=@s_key_type_t1
      # The throw is used here because this
      # condition indicates that the code that
      # calls this decryption function is flawed,
      # is missing a proper key type verification.
      kibuvits_krl171bt4_throw("ht_key[\"s_key_type\"] == "+s_key_type+
      " != "+@s_key_type_t1+
      "\n GUID='886d2c20-206e-4a21-8442-c13290a118e7'")
   end # if
   #-----
   ar_s=[s_prefix_of_the_output_string]
   ht_header=Hash.new
   ht_header["s_cryptocodec_type"]=@s_cryptocodec_type_t1
   ht_header[@s_lc_s_key_id]=ht_key[@s_lc_s_key_id]
   s_header=Kibuvits_krl171bt4_ProgFTE.from_ht(ht_header)
   ar_s<<(s_header.length.to_s+$kibuvits_krl171bt4_lc_pillar)
   ar_s<<(s_header+$kibuvits_krl171bt4_lc_pillar)
   #-----------------
   b_use_fast_random=false
   s_cleartext_normalized=Kibuvits_krl171bt4_cleartext_length_normalization.s_normalize_t1(
   s_cleartext,i_estimated_median_of_lengths_of_nonnormalized_cleartexts,
   b_use_fast_random,
   i_estimated_standard_deviation_of_lengths_of_nonnormalized_cleartexts)
   #-----------------
   ar_encrypt_wearlevelling_t1_core(s_cleartext_normalized,ht_key,ar_s)
   s_ciphertext_with_header=kibuvits_krl171bt4_s_concat_array_of_strings(ar_s)
   return s_ciphertext_with_header
end

#s_serialize_key(ht_key) ⇒ Object



21791
21792
21793
21794
21795
21796
21797
21798
21799
21800
21801
21802
21803
21804
21805
21806
21807
21808
21809
21810
21811
21812
21813
21814
21815
21816
21817
21818
21819
# File 'lib/kibuvits_ruby_library_krl171bt4_.rb', line 21791

def s_serialize_key(ht_key)
   if KIBUVITS_krl171bt4_b_DEBUG
      bn=binding()
      exc_verify_t1(bn,ht_key)
   end # if
   ht=Hash.new
   ht_key.each_pair do |s_key,x_value|
      if s_key==$kibuvits_krl171bt4_lc_ar
         ar_s=Array.new
         i_len=x_value.size
         b_not_first=false
         i_len.times do |ii|
            if b_not_first
               ar_s<<$kibuvits_krl171bt4_lc_pillar
            else
               b_not_first=true
            end # if
            ar_s<<x_value[ii].to_s(16) # primitive data compression
         end # loop
         ht[$kibuvits_krl171bt4_lc_ar]=kibuvits_krl171bt4_s_concat_array_of_strings(ar_s)
      else
         ht[s_key]=x_value.to_s
      end # if
   end # loop
   ht[@s_lc_i_n_of_datasalt_digits]=ht_key[@s_lc_i_n_of_datasalt_digits].to_s
   ht[@s_lc_i_saltfree_data_max]=ht_key[@s_lc_i_saltfree_data_max].to_s
   s_out=Kibuvits_krl171bt4_ProgFTE.from_ht(ht)
   return s_out
end