source: trunk/CrypPlugins/PKCS5/PKCS5.cs @ 198

Last change on this file since 198 was 198, checked in by Gerhard Junker, 13 years ago

Implementing Tiger and Whirlpool as HMAC (was HashAlgorithm)
Added Tiger and Whirlpool to PKCS#5

  • Property svn:keywords set to URL Author Date Rev Id
File size: 12.2 KB
Line 
1//////////////////////////////////////////////////////////////////////////////////////////////////
2// CrypTool V2
3// © 2008 - Gerhard Junker
4// Apache License see http://www.apache.org/licenses/
5//
6// $HeadURL: trunk/CrypPlugins/PKCS5/PKCS5.cs $
7//////////////////////////////////////////////////////////////////////////////////////////////////
8// $Revision:: 198                                                                            $://
9// $Author:: junker                                                                           $://
10// $Date:: 2009-01-16 10:18:00 +0000 (Fri, 16 Jan 2009)                                       $://
11//////////////////////////////////////////////////////////////////////////////////////////////////
12
13// see http://tools.ietf.org/html/rfc2898
14// based on ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-5v2/pkcs5v2_1.pdf
15
16
17using System;
18using System.Collections.Generic;
19using System.ComponentModel;
20using System.IO;
21using System.Security.Cryptography;
22using System.Text;
23
24using Cryptool.PluginBase;
25using Cryptool.PluginBase.Cryptography;
26using Cryptool.PluginBase.IO;
27using Cryptool.PluginBase.Miscellaneous;
28
29namespace PKCS5
30{
31  [Author("Gerhard Junker", null, "private project member", null)]
32  [PluginInfo(false, "PKCS#5", "PKCS#5 V2.1 Hash", "http://tools.ietf.org/html/rfc2898", "PKCS5/PKCS5.png")]
33  public class PKCS5 : IHash
34  {
35
36    /// <summary>
37    /// Initializes a new instance of the <see cref="PKCS5"/> class.
38    /// </summary>
39    public PKCS5()
40    {
41      this.settings = new PKCS5Settings();
42    }
43
44    #region Settings
45
46    private PKCS5Settings settings;
47
48    /// <summary>
49    /// Gets or sets the settings.
50    /// </summary>
51    /// <value>The settings.</value>
52    public ISettings Settings
53    {
54      get
55      {
56        return settings;
57      }
58      set
59      {
60        settings = (PKCS5Settings)value;
61        OnPropertyChanged("Settings");
62        GuiLogMessage("Settings changed.", NotificationLevel.Debug);
63      }
64    }
65
66
67    /// <summary>
68    /// Gets or sets a value indicating whether this instance has changes.
69    /// </summary>
70    /// <value>
71    ///         <c>true</c> if this instance has changes; otherwise, <c>false</c>.
72    /// </value>
73    public bool HasChanges
74    {
75      get
76      {
77        return settings.HasChanges;
78      }
79
80      set
81      {
82        settings.HasChanges = value;
83        GuiLogMessage("HasChanges changed.", NotificationLevel.Debug);
84      }
85    }
86
87    #endregion
88
89    #region Input key / password
90
91    // Input key
92    private byte[] key = { };
93
94    /// <summary>
95    /// Notifies the update input.
96    /// </summary>
97    private void NotifyUpdateKey()
98    {
99      OnPropertyChanged("KeyStream");
100      OnPropertyChanged("KeyData");
101    }
102
103    /// <summary>
104    /// Gets or sets the input data.
105    /// </summary>
106    /// <value>The input key.</value>
107    [PropertyInfo(Direction.Input, "Key Stream", "Key stream to be hashed", "", false, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, null)]
108    public CryptoolStream KeyStream
109    {
110      get
111      {
112        CryptoolStream keyDataStream = new CryptoolStream();
113        keyDataStream.OpenRead(this.GetPluginInfoAttribute().Caption, key);
114        return keyDataStream;
115      }
116      set
117      {
118        if (null == value)
119          return;
120
121        long len = value.Length;
122        key = new byte[len];
123
124        for (long i = 0; i < len; i++)
125          key[i] = (byte)value.ReadByte();
126
127        NotifyUpdateKey();
128        GuiLogMessage("KeyStream changed.", NotificationLevel.Debug);
129      }
130    }
131
132    /// <summary>
133    /// Gets the input data.
134    /// </summary>
135    /// <value>The input data.</value>
136    [PropertyInfo(Direction.Input, "Key Data", "Key stream to be hashed", "", false, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, null)]
137    public byte[] KeyData
138    {
139      get
140      {
141        return key;
142      }
143      set
144      {
145        long len = value.Length;
146        key = new byte[len];
147
148        for (long i = 0; i < len; i++)
149          key[i] = value[i];
150
151        NotifyUpdateKey();
152        GuiLogMessage("KeyData changed.", NotificationLevel.Debug);
153      }
154    }
155    #endregion
156
157    #region Salt data / Seed data
158
159    // Salt Data
160    private byte[] salt = { };
161
162    /// <summary>
163    /// Notifies the update salt.
164    /// </summary>
165    private void NotifyUpdateSalt()
166    {
167      OnPropertyChanged("SaltStream");
168      OnPropertyChanged("SaltData");
169    }
170
171    /// <summary>
172    /// Gets or sets the salt data.
173    /// </summary>
174    /// <value>The salt data.</value>
175    [PropertyInfo(Direction.Input, "Salt Stream", "Salt - Input salt data to change the PKCS hash", "", false, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, null)]
176    public CryptoolStream SaltStream
177    {
178      get
179      {
180        CryptoolStream saltDataStream = new CryptoolStream();
181        saltDataStream.OpenRead(this.GetPluginInfoAttribute().Caption, salt);
182        return saltDataStream;
183      }
184      set
185      {
186        if (null == value)
187          return;
188        long len = value.Length;
189        salt = new byte[len];
190
191        for (long i = 0; i < len; i++)
192          salt[i] = (byte)value.ReadByte();
193
194        NotifyUpdateSalt();
195        GuiLogMessage("SaltStream changed.", NotificationLevel.Debug);
196      }
197    }
198
199    /// <summary>
200    /// Gets or sets the salt data.
201    /// </summary>
202    /// <value>The salt data.</value>
203    [PropertyInfo(Direction.Input, "Salt Data", "Salt - Input salt data to to be change the PKCS hash", "", false, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, null)]
204    public byte[] SaltData
205    {
206      get
207      {
208        return salt;
209      }
210
211      set
212      {
213        long len = value.Length;
214        salt = new byte[len];
215
216        for (long i = 0; i < len; i++)
217          salt[i] = value[i];
218
219        NotifyUpdateSalt();
220        GuiLogMessage("SaltData changed.", NotificationLevel.Debug);
221      }
222    }
223    #endregion
224
225    #region Output
226
227    // Output
228    private List<CryptoolStream> listCryptoolStreamsOut = new List<CryptoolStream>();
229    private byte[] outputData = { };
230
231    /// <summary>
232    /// Notifies the update output.
233    /// </summary>
234    private void NotifyUpdateOutput()
235    {
236      OnPropertyChanged("HashOutputStream");
237      OnPropertyChanged("HashOutputData");
238    }
239
240
241    /// <summary>
242    /// Gets or sets the output data stream.
243    /// </summary>
244    /// <value>The output data stream.</value>
245    [PropertyInfo(Direction.Output, "Hashed Stream", "Output stream of the hashed value", "",
246      true, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, null)]
247    public CryptoolStream HashOutputStream
248    {
249      get
250      {
251        CryptoolStream outputDataStream = null;
252        if (outputData != null)
253        {
254          outputDataStream = new CryptoolStream();
255          outputDataStream.OpenRead(this.GetPluginInfoAttribute().Caption, outputData);
256          listCryptoolStreamsOut.Add(outputDataStream);
257        }
258        GuiLogMessage("Got HashOutputStream.", NotificationLevel.Debug);
259        return outputDataStream;
260      }
261      //set
262      //{
263      //} //readonly
264    }
265
266    /// <summary>
267    /// Gets the output data.
268    /// </summary>
269    /// <value>The output data.</value>
270    [PropertyInfo(Direction.Output, "Hashed Data", "Output data of the hashed value", "",
271      true, false, DisplayLevel.Beginner, QuickWatchFormat.Hex, null)]
272    public byte[] HashOutputData
273    {
274      get
275      {
276        GuiLogMessage("Got HashOutputData.", NotificationLevel.Debug);
277        return this.outputData;
278      }
279    }
280
281    /// <summary>
282    /// Hashes this instance.
283    /// </summary>
284    public void Hash()
285    {
286      System.Security.Cryptography.PKCS5MaskGenerationMethod pkcs5Hash = 
287                                                        new System.Security.Cryptography.PKCS5MaskGenerationMethod();
288
289      pkcs5Hash.SelectedShaFunction =
290        (PKCS5MaskGenerationMethod.ShaFunction)settings.SHAFunction;
291
292      outputData =
293        pkcs5Hash.GenerateMask(this.key, this.salt, settings.Count, settings.Length >> 3);
294
295      NotifyUpdateOutput();
296    }
297    #endregion
298
299    #region IPlugin Member
300
301#pragma warning disable 67
302    public event StatusChangedEventHandler OnPluginStatusChanged;
303    public event GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
304    public event PluginProgressChangedEventHandler OnPluginProgressChanged;
305#pragma warning restore
306
307    /// <summary>
308    /// Provide all presentation stuff in this user control, it will be opened in an tab.
309    /// Return null if your plugin has no presentation.
310    /// </summary>
311    /// <value>The presentation.</value>
312    public System.Windows.Controls.UserControl Presentation
313    {
314      get
315      {
316        return null;
317      }
318    }
319
320    /// <summary>
321    /// Gets the quick watch presentation - will be displayed inside of the plugin presentation-element. You
322    /// can return the existing Presentation if it makes sense to display it inside a small area. But be aware that
323    /// if Presentation is displayed in QuickWatchPresentation you can't open Presentation it in a tab before you
324    /// you close QuickWatchPresentation;
325    /// Return null if your plugin has no QuickWatchPresentation.
326    /// </summary>
327    /// <value>The quick watch presentation.</value>
328    public System.Windows.Controls.UserControl QuickWatchPresentation
329    {
330      get
331      {
332        return null;
333      }
334    }
335
336    /// <summary>
337    /// Will be called from editor after restoring settings and before adding to workspace.
338    /// </summary>
339    public void Initialize()
340    {
341      GuiLogMessage("Initialize.", NotificationLevel.Debug);
342    }
343
344    /// <summary>
345    /// Will be called from editor before right before chain-run starts
346    /// </summary>
347    public void PreExecution()
348    {
349      GuiLogMessage("PreExecution.", NotificationLevel.Debug);
350    }
351
352    /// <summary>
353    /// Will be called from editor while chain-run is active and after last necessary input
354    /// for plugin has been set.
355    /// </summary>
356    public void Execute()
357    {
358      GuiLogMessage("Execute.", NotificationLevel.Debug);
359      Hash();
360    }
361
362    /// <summary>
363    /// Will be called from editor after last plugin in chain has finished its work.
364    /// </summary>
365    public void PostExecution()
366    {
367      GuiLogMessage("PostExecution.", NotificationLevel.Debug);
368    }
369
370    /// <summary>
371    /// Not defined yet.
372    /// </summary>
373    public void Pause()
374    {
375      GuiLogMessage("Pause.", NotificationLevel.Debug);
376    }
377
378    /// <summary>
379    /// Will be called from editor while chain-run is active. Plugin hast to stop work immediately.
380    /// </summary>
381    public void Stop()
382    {
383      GuiLogMessage("Stop.", NotificationLevel.Debug);
384    }
385
386    /// <summary>
387    /// Will be called from editor when element is deleted from worksapce.
388    /// Releases unmanaged and - optionally - managed resources
389    /// </summary>
390    public void Dispose()
391    {
392      foreach (CryptoolStream stream in listCryptoolStreamsOut)
393      {
394        stream.Close();
395      }
396      listCryptoolStreamsOut.Clear();
397      GuiLogMessage("Dispose.", NotificationLevel.Debug);
398    }
399
400    #endregion
401
402    #region INotifyPropertyChanged Member
403
404    /// <summary>
405    /// Occurs when a property value changes.
406    /// </summary>
407    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
408
409    /// <summary>
410    /// Called when [property changed].
411    /// </summary>
412    /// <param name="name">The name.</param>
413    protected void OnPropertyChanged(string name)
414    {
415      if (PropertyChanged != null)
416      {
417        if (name == "Settings")
418          Hash();
419        else
420          PropertyChanged(this, new PropertyChangedEventArgs(name));
421      }
422    }
423
424    /// <summary>
425    /// GUIs the log message.
426    /// </summary>
427    /// <param name="message">The message.</param>
428    /// <param name="logLevel">The log level.</param>
429    private void GuiLogMessage(string message, NotificationLevel logLevel)
430    {
431      EventsHelper.GuiLogMessage(OnGuiLogNotificationOccured, this,
432        new GuiLogEventArgs(message, this, logLevel));
433    }
434
435    #endregion
436  }
437}
Note: See TracBrowser for help on using the repository browser.