source: trunk/CrypPlugins/KeyPatternJob/DistributableKeyPatternJob.cs @ 1182

Last change on this file since 1182 was 1182, checked in by arnold, 12 years ago

Fixed serialization bug in KeyPattern, so AES bruteforcing will be possible now
Fixed AES Bruteforce Sample (there were wrong Modi settings)

File size: 9.9 KB
Line 
1/* Copyright 2010 Team CrypTool (Christian Arnold), Uni Duisburg-Essen
2
3   Licensed under the Apache License, Version 2.0 (the "License");
4   you may not use this file except in compliance with the License.
5   You may obtain a copy of the License at
6
7       http://www.apache.org/licenses/LICENSE-2.0
8
9   Unless required by applicable law or agreed to in writing, software
10   distributed under the License is distributed on an "AS IS" BASIS,
11   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   See the License for the specific language governing permissions and
13   limitations under the License.
14*/
15
16using System;
17using System.Collections.Generic;
18using System.Linq;
19using System.Text;
20using KeySearcher;
21using Cryptool.PluginBase.Miscellaneous;
22
23namespace Cryptool.Plugins.PeerToPeer.Jobs
24{
25    public class DistributableKeyPatternJob : IDistributableJob
26    {
27        #region Variables and Properties
28
29        byte[] encryptData;
30        public byte[] EncryptData
31        {
32            get { return this.encryptData ; }
33            set { this.encryptData = value; }
34        }
35
36        byte[] initVector;
37        public byte[] InitVector
38        { 
39            get { return this.initVector; }
40            set { this.initVector = value; }
41        }
42
43        /// <summary>
44        /// increment this counter every time you produce a NEW job. When you pop a
45        /// already created Job from the pattern Buffer, don't increment this value.
46        /// Use the JobId existing in the KeyPatternJobPart-Element.
47        /// </summary>
48        BigInteger jobIdCounter = 0;
49        /// <summary>
50        /// main pattern stack
51        /// </summary>
52        KeyPatternPool patternPool;
53
54        /// <summary>
55        /// buffer for patterns which were popped from the patternPool, but were declined by the worker
56        /// </summary>
57        Stack<KeyPatternJobPart> patternBuffer;
58        /// <summary>
59        /// when a new Pattern will be requested, it will be copied
60        /// in this list. When a JobAccepted message arrived, move this
61        /// entry to the processingPatterns Dictionary. When a JobDeclined
62        /// Message arrived, remove the pattern from this Dictionary and
63        /// add the Job to the patternBuffer.
64        /// </summary>
65        Dictionary<BigInteger, KeyPatternJobPart> allocatedPatterns;
66        /// <summary>
67        /// Not before receiving the JobAccepted message from the worker,
68        /// the allocatedPattern entry will be moved to this list
69        /// </summary>
70        Dictionary<BigInteger, KeyPatternJobPart> patternsInProgress;
71        Dictionary<BigInteger, KeyPatternJobResult> finishedPatterns;
72
73        private LinkedList<KeySearcher.KeySearcher.ValueKey> globalResultList;
74        public LinkedList<KeySearcher.KeySearcher.ValueKey> GlobalResultList
75        {
76            get { return this.globalResultList;}
77            private set { this.globalResultList = value; }
78        }
79
80        public BigInteger TotalAmount
81        {
82            get { return this.patternPool.Count() + this.patternBuffer.Count + this.patternsInProgress.Count + this.allocatedPatterns.Count + this.finishedPatterns.Count; }
83            set { throw new NotImplementedException(); }
84        }
85
86        public BigInteger AllocatedAmount
87        {
88            get { return this.allocatedPatterns.Count + this.patternsInProgress.Count; }
89            set { throw new NotImplementedException(); }
90        }
91
92        public BigInteger FinishedAmount
93        {
94            get { return this.finishedPatterns.Count; }
95            set { throw new NotImplementedException(); }
96        }
97
98        #endregion
99
100        public DistributableKeyPatternJob(KeyPattern pattern, BigInteger partSize, byte[] encryptData, byte[] initVector)
101        {
102            this.EncryptData = encryptData;
103            this.InitVector = initVector;
104
105            this.patternPool = new KeyPatternPool(pattern, partSize);
106            this.patternBuffer = new Stack<KeyPatternJobPart>();
107            this.allocatedPatterns = new Dictionary<BigInteger, KeyPatternJobPart>();
108            this.patternsInProgress = new Dictionary<BigInteger, KeyPatternJobPart>();
109            this.finishedPatterns = new Dictionary<BigInteger, KeyPatternJobResult>();
110
111            this.GlobalResultList = new LinkedList<KeySearcher.KeySearcher.ValueKey>();
112        }
113
114        #region IDistributableJob Members
115
116        public byte[] Pop(out BigInteger jobId)
117        {
118            byte[] serializedJob = null;
119            jobId = null;
120
121            if (this.patternBuffer.Count > 0)
122            {
123                KeyPatternJobPart jobPart = this.patternBuffer.Pop();
124                jobId = jobPart.JobId;
125                serializedJob = jobPart.Serialize();
126                this.allocatedPatterns.Add(jobId, jobPart);
127            }
128            else
129            {
130                KeyPattern poppedPattern = this.patternPool.Pop();
131                if (poppedPattern != null)
132                {
133                    // create a new JobPart element
134                    jobId = jobIdCounter++;
135                    KeyPatternJobPart jobPart = new KeyPatternJobPart(jobId, poppedPattern, this.EncryptData, this.InitVector);
136                    serializedJob = jobPart.Serialize();
137                    this.allocatedPatterns.Add(jobId, jobPart);
138                }
139            }
140            return serializedJob;
141        }
142
143        public void Push(BigInteger jobId)
144        {
145            if (this.allocatedPatterns.ContainsKey(jobId))
146            {
147                this.patternBuffer.Push(this.allocatedPatterns[jobId]);
148                //when a job is pushed on the "patternBuffer", this indicates, that worker leaves the network
149                this.allocatedPatterns.Remove(jobId);
150            }
151
152            if (this.patternsInProgress.ContainsKey(jobId))
153            {
154                this.patternBuffer.Push(this.patternsInProgress[jobId]);
155                //when a job is pushed on the "patternBuffer", this indicates, that worker leaves the network
156                this.patternsInProgress.Remove(jobId);
157            }
158        }
159
160        public double ProcessProgress()
161        {
162            throw new NotImplementedException();
163        }
164
165        public void JobAccepted(BigInteger jobId)
166        {
167            if (this.allocatedPatterns.ContainsKey(jobId))
168            {
169                this.patternsInProgress.Add(jobId, this.allocatedPatterns[jobId]);
170                this.allocatedPatterns.Remove(jobId);
171            }
172            //dirty workaround because P2PJobAdmin sends the accepted/declined msg twice...
173            //else
174            //    throw (new Exception("The job-accepted-message for jobId '"+ jobId.ToString() + "' isn't valid"));
175        }
176
177        public void JobDeclined(BigInteger jobId)
178        {
179            if (this.allocatedPatterns.ContainsKey(jobId))
180            {
181                this.patternBuffer.Push(this.allocatedPatterns[jobId]);
182                this.allocatedPatterns.Remove(jobId);
183            }
184            //dirty workaround because P2PJobAdmin sends the accepted/declined msg twice...
185            //else
186            //    throw (new Exception("The job-declined-message for jobId '" + jobId.ToString() + "' isn't valid"));
187        }
188
189        public TimeSpan SetResult(BigInteger jobId, byte[] result)
190        {
191            TimeSpan returnTimeSpan = new TimeSpan(0);
192            if (this.patternsInProgress.ContainsKey(jobId))
193            {
194                KeyPatternJobResult deserializedJobResult = new KeyPatternJobResult(result);
195               
196                MergeGlobalList(deserializedJobResult.Result);
197
198                returnTimeSpan = deserializedJobResult.ProcessingTime;
199                this.finishedPatterns.Add(jobId, deserializedJobResult);
200                this.patternsInProgress.Remove(jobId);
201            }
202            //dirty workaround because P2PJobAdmin sends the result msg twice...
203            //else
204            //    throw(new Exception("Received result from a job, which isn't in 'patternsInProgress' List."));
205            return returnTimeSpan;
206        }
207
208        #endregion
209
210        #region Special Global Result List
211
212        private LinkedList<KeySearcher.KeySearcher.ValueKey> MergeGlobalList(LinkedList<KeySearcher.KeySearcher.ValueKey> listToCompare)
213        {
214            if (this.GlobalResultList.Count == 0)
215            {
216                this.GlobalResultList = listToCompare;
217                return this.GlobalResultList;
218            }
219
220            if (this.GlobalResultList.Last().value >= listToCompare.First().value)
221                return this.GlobalResultList;
222
223            LinkedListNode<KeySearcher.KeySearcher.ValueKey> globalNode = this.GlobalResultList.First;
224            LinkedListNode<KeySearcher.KeySearcher.ValueKey> temp_node = this.GlobalResultList.First;
225            LinkedListNode<KeySearcher.KeySearcher.ValueKey> localNode = listToCompare.First;
226
227            if (this.GlobalResultList.Count != listToCompare.Count)
228                throw (new Exception("The two lists, which you want to merge haven't the same length (GlobalList: " + this.GlobalResultList.Count + ", LocalList: " + listToCompare.Count));
229
230            while(localNode != null)
231            {
232                temp_node = globalNode;
233                while (globalNode != null)
234                {
235                    if (localNode.Value.value >= globalNode.Value.value)
236                    {
237                        this.GlobalResultList.AddBefore(globalNode, localNode.Value);
238                        this.GlobalResultList.RemoveLast();
239                        break;
240                    }
241                    globalNode = globalNode.Next;
242                }
243                globalNode = temp_node;
244                localNode = localNode.Next;
245            }
246
247            return this.GlobalResultList;
248        }
249
250        #endregion
251    }
252}
Note: See TracBrowser for help on using the repository browser.