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

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

Plugin URLs filled
Source comments - references added
twofish partial added

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