├── LICENSE.md
├── README.md
├── digital_signature
├── PC
│ ├── RSA.py
│ ├── cycles.py
│ ├── cycles_md5.py
│ ├── digital_sign.py
│ └── util.py
└── arduino
│ ├── digisign.ino
│ ├── md5.h
│ ├── rsa.h
│ └── util.h
├── key_exchange_DiffieHellman
├── PC
│ ├── cycles.py
│ ├── diffiehellman_oneparty.py
│ └── util.py
└── arduino
│ ├── aes.h
│ ├── diffie_hellman_final.ino
│ └── util.h
└── key_exchange_RSA
├── PC
├── Euclid.py
├── RSA.py
├── cycles.py
├── prime.py
├── rsa_aes_protocol.py
└── util.py
└── arduino
├── aes.h
├── rsa.h
├── rsa_aes_protocol.ino
└── util.h
/LICENSE.md:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | This project demonstrates how an Arduino board and a PC can implement cryptographic protocols.
2 |
3 | The protocols implemented are (i) Digital Signature, (ii) Key Exchange using Diffie-Hellman algorithm, and (iii) Key Exchange using RSA (a public-key encryption algorithm).
4 |
5 | ### COPYRIGHT NOTICE
6 |
7 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu, Aditya Pandey and Archit Pandey
8 |
9 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
10 | it under the terms of the GNU General Public License as published by
11 | the Free Software Foundation, either version 3 of the License, or
12 | (at your option) any later version.
13 |
14 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | GNU General Public License for more details.
18 |
19 | You should have received a copy of the GNU General Public License
20 | along with the software. It is in the file LICENSE.MD.
21 | If not available there, see
22 |
--------------------------------------------------------------------------------
/digital_signature/PC/RSA.py:
--------------------------------------------------------------------------------
1 | """
2 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
3 |
4 | This file is part of cryptographic-protocols-arduino-and-PC source code.
5 |
6 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with the software. If not, see .
18 | """
19 |
20 | from random import randint
21 |
22 | #Public Key = {e, n} and Private Key = {d, n}
23 |
24 | def rsa_encrypt(plaintext, key):
25 | n, e = key
26 | plaintext_list = string2ascii_int_list(plaintext)
27 | ciphertext_list = []
28 |
29 | for M in plaintext_list:
30 | C = pow(M, e, n) # pow (a,b[,c]) returns (a**b) mod c
31 | ciphertext_list.append(C)
32 |
33 | return ciphertext_list
34 |
35 |
36 | def rsa_decrypt(ciphertext_list, key):
37 | n, d = key
38 | plaintext_list = []
39 |
40 | for C in ciphertext_list:
41 | M = pow(C, d, n)
42 | plaintext_list.append(M)
43 | plaintext = ascii_int_list2string(plaintext_list)
44 | return plaintext
45 |
--------------------------------------------------------------------------------
/digital_signature/PC/cycles.py:
--------------------------------------------------------------------------------
1 | """
2 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
3 |
4 | This file is part of cryptographic-protocols-arduino-and-PC source code.
5 |
6 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with the software. If not, see .
18 | """
19 |
20 | from util import *
21 | from Crypto.Cipher import AES
22 | from time import sleep
23 |
24 | def cycle_arduino_to_PC(AES_key_exchange_function, arduino, caller="DiffieHellman"):
25 | #receiving ciphertext
26 | ctext = []
27 |
28 | AES_Key = AES_key_exchange_function(arduino)
29 | if caller == "RSA":
30 | arduino.readline()
31 |
32 | length = int(arduino.readline())
33 | AES_Key_string = ascii_int_list2string(AES_Key)
34 |
35 | pycrypto_AES = AES.new(AES_Key_string, AES.MODE_ECB)
36 |
37 | ptext_actual = zeropadding("Drones are hovering over your head", 16)[0]
38 |
39 | print "length = ", length
40 |
41 | for i in range(length):
42 | x = int(arduino.readline())
43 | ctext.append(x)
44 |
45 | ctext_string = ascii_int_list2string(ctext)
46 |
47 | ptext_string = pycrypto_AES.decrypt(ctext_string)
48 |
49 | print "ciphertext (string) = ", ctext_string
50 | print "ciphertext (ASCII values) = ", ctext
51 | print "plaintext (string) = ", ptext_string
52 | print "plaintext (ASCII values) = ", string2ascii_int_list(ptext_string)
53 |
54 | def cycle_PC_to_arduino(AES_Key, plaintext, arduino, caller = "DiffieHellman"):
55 | ptext_string = plaintext
56 | ptext_string = zeropadding(ptext_string, 16)[0]
57 | AES_Key_string = ascii_int_list2string(AES_Key)
58 | pycrypto_AES = AES.new(AES_Key_string, AES.MODE_ECB)
59 | ctext_string = pycrypto_AES.encrypt(ptext_string)
60 | ctext = string2ascii_int_list(ctext_string)
61 |
62 | if 0 in ctext:
63 | return 0
64 | else:
65 | arduino.write("2&g&xb3leL")
66 |
67 | print "AES Key = ", AES_Key
68 | print "Actual length = ", len(ctext)
69 | arduino.write(append_zeros_at_front(len(ctext), MOST_NO_OF_DIGITS))
70 |
71 | print "Ciphertext (int list) = ", ctext
72 | print "Ciphertext = ", ctext_string
73 |
74 | for i in range(len(ctext)):
75 | sleep(0.002)
76 | arduino.write(append_zeros_at_front(ctext[i], 3))
77 | if caller == "RSA":
78 | arduino.readline()
79 | return 1
80 |
81 | def cycle_PC_to_arduino_executer(plaintext, AES_key_exchange_function, arduino, caller="DiffieHellman"):
82 | flag = 0
83 | while flag == 0:
84 | AES_key = AES_key_exchange_function(arduino)
85 |
86 | flag = cycle_PC_to_arduino(AES_key, plaintext, arduino, caller)
87 |
88 | if flag == 0:
89 | arduino.write("K2qkR^ICGU")
90 |
--------------------------------------------------------------------------------
/digital_signature/PC/cycles_md5.py:
--------------------------------------------------------------------------------
1 | """
2 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
3 |
4 | This file is part of cryptographic-protocols-arduino-and-PC source code.
5 |
6 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with the software. If not, see .
18 | """
19 |
20 | import RSA
21 | from Crypto.Hash import MD5
22 | from util_MD5 import *
23 |
24 | RSA_Others_Public_Key = [5723, 5]
25 | RSA_Own_Private_Key = [6887, 611]
26 | MOST_NO_OF_DIGITS = 10
27 |
28 | def cycle_arduino_to_pc(arduino):
29 |
30 | arduino_encryted_hash = []
31 | arduino.readline()
32 | arduino.readline()
33 |
34 | for i in range(16):
35 | arduino_encryted_hash.append(int(arduino.readline()))
36 |
37 | print "arduino_encryted_hash = ", arduino_encryted_hash
38 | arduino_msg_length = int(arduino.readline())
39 |
40 | arduino_msg = ''
41 | for i in range(arduino_msg_length):
42 | arduino_msg += arduino.readline()[0]
43 |
44 | print "msg =" , arduino_msg
45 |
46 | msg_hash_by_pc = MD5.new(arduino_msg)
47 | msg_hash_by_pc_string = msg_hash_by_pc.digest()
48 | print "msg_hash_by_pc_string = ", msg_hash_by_pc_string
49 |
50 | msg_hash_by_pc_int_list = []
51 |
52 | for i in range(len(msg_hash_by_pc_string)):
53 | msg_hash_by_pc_int_list.append(ord(msg_hash_by_pc_string[i]))
54 |
55 | print "msg_hash_by_pc_int_list = ", msg_hash_by_pc_int_list
56 |
57 |
58 | plain_hash = RSA.rsa_decrypt(arduino_encryted_hash, RSA_Others_Public_Key);
59 |
60 | hash_int_list = []
61 |
62 | for val in plain_hash:
63 | hash_int_list.append(ord(val))
64 |
65 | print "hash_int_list = ", hash_int_list
66 |
67 | if hash_int_list == msg_hash_by_pc_int_list :
68 | print "Hash and Message match!"
69 | else:
70 | print "Hash and Message do not match!"
71 |
72 | def cycle_pc_to_arduino(pc_msg, arduino):
73 | send_pc_msg_to_arduino(pc_msg, arduino)
74 |
75 | pc_hash = MD5.new(pc_msg)
76 | pc_hash_string = pc_hash.digest()
77 |
78 | #send_pc_msg_to_arduino(pc_msg)
79 | print "pc_hash_string =", pc_hash_string
80 | pc_hash_int_list = []
81 | for i in range(len(pc_hash_string)):
82 | pc_hash_int_list.append(ord(pc_hash_string[i])) # Packing pc_msg hash as int list for sending to arduino
83 |
84 | print "pc_hash_int_list = ", pc_hash_int_list
85 | length = len(pc_hash_int_list)
86 |
87 | encrypted_pc_hash = RSA.rsa_encrypt(pc_hash_string, RSA_Own_Private_Key) # Encrypting the hash by RSA
88 | print "encrypted_pc_hash = " ,encrypted_pc_hash
89 | # Send encrypted hash to Arduino
90 | for i in range(16): # Sending the Encrypted Hash
91 | arduino.write(append_zeros_at_front(encrypted_pc_hash[i], MOST_NO_OF_DIGITS))
92 | recieved_by_arduino = arduino.readline()
93 |
--------------------------------------------------------------------------------
/digital_signature/PC/digital_sign.py:
--------------------------------------------------------------------------------
1 | """
2 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
3 |
4 | This file is part of cryptographic-protocols-arduino-and-PC source code.
5 |
6 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with the software. If not, see .
18 | """
19 |
20 | import serial
21 | from time import sleep
22 | from cycles_md5 import *
23 |
24 | COMPORT = 6
25 |
26 | def synchronize(str, arduino):
27 | while arduino.readline() != str:
28 | pass
29 |
30 | def main():
31 | arduino = serial.Serial(COMPORT, 115200)
32 | arduino.timeout = 1.5
33 |
34 | print "1. Digitally sign a message and send it to Arduino from PC."
35 | print "2. Digitally sign a message and sent it to PC from Arduino."
36 |
37 | choice = raw_input("\n Enter your choice: ")
38 |
39 | while choice not in ['1','2']:
40 | choice = raw_input("Wrong choice. Enter again: ")
41 | if choice =='1':
42 | pc_msg = "Sheryl Sandberg is the author of Lean In"
43 | synchronize("Ready\n", arduino)
44 | arduino.write("PC_TO_ARDUINO")
45 | cycle_pc_to_arduino(pc_msg, arduino)
46 | elif choice == '2':
47 | synchronize("Ready\n", arduino)
48 | arduino.write("ARDUINO_TO_PC")
49 | cycle_arduino_to_pc(arduino)
50 |
51 | arduino.close()
52 |
53 | main()
54 |
--------------------------------------------------------------------------------
/digital_signature/PC/util.py:
--------------------------------------------------------------------------------
1 | """
2 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
3 |
4 | This file is part of cryptographic-protocols-arduino-and-PC source code.
5 |
6 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with the software. If not, see .
18 | """
19 |
20 | MOST_NO_OF_DIGITS = 10
21 |
22 | def zeropadding(string, block):
23 | length = len(string)
24 | no_of_blocks = length/block + 1
25 | length_last_block = length % block
26 | if length_last_block == 0:
27 | return [string, length]
28 | string = string + (no_of_blocks * block - length_last_block) * "\0"
29 | return [string, no_of_blocks * block]
30 |
31 | def string2ascii_int_list(string):
32 | ascii_int_list=[]
33 | for char in string:
34 | ascii_int_list.append(ord(char))
35 | return ascii_int_list
36 |
37 | def ascii_int_list2string(ascii_int_list):
38 | string=[]
39 | for integer in ascii_int_list:
40 | string.append(chr(integer))
41 | return ''.join(string)
42 |
43 | def append_zeros_at_front(num, no_of_digits):
44 | num_str = str(num)
45 | length = len(num_str)
46 | if length >= no_of_digits:
47 | return num_str
48 | for i in range(no_of_digits - length):
49 | num_str = '0' + num_str
50 | return num_str
51 |
52 |
53 | def synchronize(str, arduino):
54 | while arduino.readline() != str:
55 | pass
56 |
--------------------------------------------------------------------------------
/digital_signature/arduino/digisign.ino:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
4 |
5 | This file is part of cryptographic-protocols-arduino-and-PC source code.
6 |
7 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with the software. If not, see .
19 |
20 | */
21 |
22 | #include "md5.h"
23 | #include "rsa.h"
24 | #include
25 | #include
26 | #define MAX_DIGITS_IN_PC_MSG_LENGTH 10
27 | #define MOST_NO_OF_DIGITS 10
28 |
29 | long RSA_Own_Private_Key[2] = {5723, 3341};
30 | long RSA_Others_Public_Key[2] = {6887, 11};
31 |
32 | LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
33 |
34 | using namespace std;
35 |
36 | void lcd_print_string(unsigned char *text, LiquidCrystal lcd, int delay_time = 2000, int lcd_size = 32) {
37 | int length = size_string(text);
38 | int lcd_half_size = lcd_size / 2;
39 | int no_of_lines = length / lcd_half_size;
40 | int length_last_line = length % lcd_half_size;
41 | lcd.clear();
42 |
43 | for (int i = 0; i < no_of_lines + 1; ++i) {
44 | if (i % 2 == 0)
45 | lcd.setCursor(0,0);
46 | else
47 | lcd.setCursor(0,1);
48 | lcd.print((char *)(text + i * lcd_half_size));
49 | if (i % 2 ) {
50 | delay(delay_time);
51 | if (i != no_of_lines)
52 | lcd.clear();
53 | }
54 | }
55 | }
56 |
57 | void lcd_print_string(char *text, LiquidCrystal lcd, int delay_time = 2000, int lcd_size = 32) {
58 | int length = size_string(text);
59 | int lcd_half_size = lcd_size / 2;
60 | int no_of_lines = length / lcd_half_size;
61 | int length_last_line = length % lcd_half_size;
62 | lcd.clear();
63 |
64 | for (int i = 0; i < no_of_lines + 1; ++i) {
65 | if (i % 2 == 0)
66 | lcd.setCursor(0,0);
67 | else
68 | lcd.setCursor(0,1);
69 | lcd.print((char *)(text + i * lcd_half_size));
70 | if (i % 2 ) {
71 | delay(delay_time);
72 | if (i != no_of_lines)
73 | lcd.clear();
74 | }
75 | }
76 | }
77 |
78 |
79 | void cycle_arduino_to_pc(){
80 | char ch;
81 | char msg[500]="The meme for blind faith secures its won perpertuation by the simple unconscious expedient of discouraging ratinonal enquiry. -- Richard Dawkins";
82 | uint8_t *hash;
83 | long *cipher_text;
84 |
85 | long length;
86 |
87 | hash = md5sum(msg); // Hashing message
88 |
89 | cipher_text=rsa_encrypt(hash, RSA_Own_Private_Key); // Encrypting the hash
90 | for(int i = 0; i < 16; ++i ){
91 | Serial.println(cipher_text[i]); // Sending the Encrypted hash
92 | }
93 | int len = size_string(msg);
94 | Serial.println(to_string(len)); // Sending msg length to PC
95 |
96 | for(int i = 0; i < len; ++i ){
97 | Serial.println(msg[i]); // Sending the msg
98 |
99 | }
100 |
101 | }
102 |
103 |
104 |
105 |
106 | void cycle_pc_to_arduino(){
107 | char len_str[MAX_DIGITS_IN_PC_MSG_LENGTH + 1]; // Reading the length of pc_msg
108 | Serial.readBytes(len_str, MAX_DIGITS_IN_PC_MSG_LENGTH);
109 |
110 | len_str[MAX_DIGITS_IN_PC_MSG_LENGTH] = '\0';
111 | long pc_msg_length;
112 | pc_msg_length = to_num(len_str);
113 | lcd.setCursor(0,0);
114 | char *pc_msg;
115 | pc_msg = new char[pc_msg_length + 1];
116 | pc_msg[pc_msg_length] = '\0';
117 | for(long j = 0 ;j < pc_msg_length; ++j){
118 | char num_str[4];
119 | Serial.readBytes(num_str,3);
120 | num_str[3] = '\0';
121 | long pc_msg_i_int = to_num(num_str);
122 | pc_msg[j] = char(pc_msg_i_int);
123 | }
124 |
125 | Serial.println(pc_msg);
126 |
127 | long encrypted_hash_pc[16];
128 | for (long j = 0; j< 16; ++j) {
129 | char val_str[MOST_NO_OF_DIGITS + 1];
130 | Serial.readBytes(val_str, MOST_NO_OF_DIGITS);
131 |
132 | val_str[MOST_NO_OF_DIGITS] ='\0';
133 | if (j >-1){
134 | encrypted_hash_pc[j] = to_num(val_str); // Reading Encrypted hash from PC.
135 |
136 | Serial.println(encrypted_hash_pc[j]);
137 | }
138 |
139 | }
140 |
141 | long *rsa_ptext;
142 | rsa_ptext = rsa_decrypt(encrypted_hash_pc, RSA_Others_Public_Key, 16);
143 |
144 | uint8_t* pc_msg_hash;
145 | lcd.clear();
146 | lcd_print_string(pc_msg,lcd);
147 | delay(2000);
148 | pc_msg_hash = md5sum((char*)pc_msg);
149 | int i = 0;
150 | for(i = 0; i< 16;++i){
151 | if (pc_msg_hash[i] != rsa_ptext[i]){
152 | lcd.setCursor(0,0);
153 | lcd.clear();
154 | lcd.print("Hash mismatch");
155 | delay(2000);
156 | break;
157 | }
158 | }
159 | if (i == 16){
160 | lcd.setCursor(0,0);
161 | lcd.clear();
162 | lcd.print("Hash match");
163 | delay(2000);
164 |
165 | }
166 | }
167 |
168 |
169 | void setup() {
170 | lcd.begin(16,2);
171 | lcd.clear();
172 | Serial.begin(115200);
173 | Serial.write("Ready\n");
174 | lcd.print("Ready>>>");
175 |
176 | char mode[14];
177 | mode[13] = '\0';
178 | Serial.readBytes(mode,13);
179 | if (strcmp(mode, "PC_TO_ARDUINO") == 0){
180 | cycle_pc_to_arduino();
181 | }
182 | else if (strcmp( mode, "ARDUINO_TO_PC") == 0){
183 | cycle_arduino_to_pc();
184 | }
185 | else {
186 | lcd.clear();
187 | lcd.setCursor(0,0);
188 | lcd.print("NO MODE SELECTED");
189 | }
190 | }
191 |
192 | void loop() {
193 |
194 | }
195 |
--------------------------------------------------------------------------------
/digital_signature/arduino/md5.h:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
4 |
5 | This file is part of cryptographic-protocols-arduino-and-PC source code.
6 |
7 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with the software. If not, see .
19 |
20 | */
21 |
22 | #include
23 | #include
24 | #include
25 | #include
26 |
27 | // Constants are the integer part of the sines of integers (in radians) * 2^32.
28 | const uint32_t k[64] = {
29 | 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee ,
30 | 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501 ,
31 | 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be ,
32 | 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821 ,
33 | 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa ,
34 | 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8 ,
35 | 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed ,
36 | 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a ,
37 | 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c ,
38 | 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70 ,
39 | 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05 ,
40 | 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665 ,
41 | 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039 ,
42 | 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1 ,
43 | 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1 ,
44 | 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 };
45 |
46 | // leftrotate function definition
47 | #define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c))))
48 |
49 | // These vars will contain the hash
50 | uint32_t h0, h1, h2, h3;
51 |
52 | //This variable will contain the appended hash
53 | uint8_t p[16];
54 |
55 | void md5(uint8_t *initial_msg, size_t initial_len) {
56 |
57 | // Message (to prepare)
58 | uint8_t *msg = NULL;
59 | int new_len;
60 | uint32_t bits_len;
61 | int offset;
62 | uint32_t *w;
63 | uint32_t a, b, c, d, i, f, g, temp;
64 |
65 | // Note: All variables are unsigned 32 bit and wrap modulo 2^32 when calculating
66 |
67 | // r specifies the per-round shift amounts
68 | const uint32_t r[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
69 | 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
70 | 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
71 | 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
72 |
73 | // Initialize variables - simple count in nibbles:
74 | h0 = 0x67452301;
75 | h1 = 0xefcdab89;
76 | h2 = 0x98badcfe;
77 | h3 = 0x10325476;
78 |
79 | // Pre-processing: adding a single 1 bit
80 | //append "1" bit to message
81 | /* Notice: the input bytes are considered as bits strings,
82 | where the first bit is the most significant bit of the byte.[37] */
83 |
84 | // Pre-processing: padding with zeros
85 | //append "0" bit until message length in bit ≡ 448 (mod 512)
86 | //append length mod (2 pow 64) to message
87 |
88 | for(new_len = initial_len*8 + 1; new_len%512!=448; new_len++);
89 | new_len /= 8;
90 |
91 | msg = (uint8_t*)calloc(new_len + 64, 1); // also appends "0" bits
92 | // (we alloc also 64 extra bytes...)
93 | memcpy(msg, initial_msg, initial_len);
94 | msg[initial_len] = 128; // write the "1" bit
95 |
96 | bits_len = 8*initial_len; // note, we append the len
97 | memcpy(msg + new_len, &bits_len, 4); // in bits at the end of the buffer
98 |
99 | // Process the message in successive 512-bit chunks:
100 | //for each 512-bit chunk of message:
101 | for(offset=0; offset.
19 |
20 | */
21 |
22 |
23 | #include "util.h"
24 |
25 | char* dec2bin(int n) {
26 | long r;
27 | int c = 0, i, b[100];
28 | while(n > 0)
29 | {
30 | r = n % 2 ;
31 | b[c] = r ;
32 | n = n / 2 ;
33 | c++ ;
34 | }
35 |
36 | char* str;
37 | for(i = 0 ; i >= c-1 ; i++) {
38 | str[c-i-1] = b[0] + 48;
39 | }
40 |
41 |
42 | return str;
43 | }
44 |
45 | long* string2ascii_int_list(char* string) {
46 | long* ascii_int_list;
47 | int length = strlen(string);
48 | ascii_int_list = new long [length];
49 | for (long i = 0; i < length; ++i) {
50 | ascii_int_list[i] = (int)(string[i]);
51 | ascii_int_list[i] = (long) (ascii_int_list[i]);
52 | Serial.write("ascii_int_list["); Serial.print(i);
53 | Serial.write("] = ");
54 | Serial.println(ascii_int_list[i]);
55 | }
56 |
57 | return ascii_int_list;
58 | }
59 |
60 | char* ascii_int_list2string(int* ascii_int_list) {
61 | char* string;
62 | int length = sizeof(ascii_int_list) / sizeof(ascii_int_list[0]);
63 | string = new char[length];
64 | for (int i = 0; i < length; ++i) {
65 | string[i] = (char) (ascii_int_list[i]);
66 | }
67 | return string;
68 | }
69 |
70 | long getprime(long lower, long upper) {
71 | long num;
72 | while(1) {
73 | num = random(lower,upper);
74 | if(isprime(num, 2))
75 | break;
76 | }
77 | return num;
78 | }
79 |
80 | long* rsa_encrypt(char* plaintext, long* key) {
81 | //long n = key[0];
82 | //long e = key[1];
83 | long* plaintext_list;
84 | long* ciphertext_list;
85 | int length = strlen(plaintext);
86 |
87 | plaintext_list = string2ascii_int_list(plaintext);
88 |
89 | ciphertext_list = new long[length];
90 | Serial.write("inside hell called rsa_encrypt\n");
91 |
92 | for (int i = 0; i < length; ++i) {
93 | Serial.write("plaintext_list["); Serial.print(i); Serial.print("] = "); Serial.println((plaintext_list[i]));
94 | ciphertext_list[i] = raiseto_mod(plaintext_list[i], key[1], key[0]);
95 | }
96 |
97 | return ciphertext_list;
98 |
99 | }
100 |
101 | long* rsa_decrypt(long* ciphertext_list, long* key, int length) {
102 | //long n = key[0];
103 | //long d = key[1];
104 | long* plaintext_list;
105 | //int length = sizeof(ciphertext_list)/sizeof(ciphertext_list[0]);
106 | plaintext_list = new long[length];
107 | for(int i = 0; i < length; ++i) {
108 | plaintext_list[i] = raiseto_mod(ciphertext_list[i], key[1], key[0]);
109 | }
110 | return plaintext_list;
111 | }
112 |
113 | /* The following functions can be used for generating a pair of RSA keys.
114 |
115 | long gcd(long a, long b){
116 | // Returns greatest common denominator by using recursive Euclidean algorithm
117 | if (b == 0){
118 |
119 | return a;
120 | }
121 | return gcd(b, a % b);
122 | }
123 |
124 | long* extended_gcd(long a, long b){
125 | // Recursive extended Euclidean algorithm
126 | long *temp, *temp2;
127 | if (a==0){
128 | temp = new long[3];
129 | temp[0] = b; temp[1]= 0; temp[2]= 1;
130 |
131 |
132 | return temp;
133 | }
134 | else {
135 | temp = extended_gcd(b % a, a);
136 | temp2 = new long[3];
137 | temp2[0] = temp[0];
138 | temp2[1] = temp[2] - (long)(b/a)*temp[1];
139 | temp2[2] = temp[1];
140 |
141 |
142 | return temp2;
143 | }
144 | }
145 |
146 | long modinv(long a,long m){
147 | // Returns the inverse of a modulo m
148 | long *temp;
149 | temp = extended_gcd(a,m);
150 | long x = temp[1], g = temp[0]; // y = temp[2];
151 | if (g==1)
152 | return (x % m);
153 |
154 | }
155 |
156 | long getrelprime(long num){
157 | // Returns the smallest relative prime number to num
158 | for(long i=2; i< num; i++){
159 | if (gcd(i,num) == 1){
160 | return i;
161 | }
162 | }
163 | }
164 |
165 | //
166 | long* findkq(long n){
167 | // Find k and q such that n = (2**k)*q
168 | long result;
169 | result = n % (long)(2);
170 | long n_new = n-1;
171 | long k = 0;
172 | long q = 0;
173 | while (n_new) {
174 | n_new /= 2;
175 | k += 1;
176 | if (n_new % (2) != 0){
177 | q = n_new;
178 | break;
179 | }
180 | }
181 | long *temp;
182 | temp = new long[2];
183 | temp[0] = k;
184 | temp[1] = q;
185 | return temp ;
186 | }
187 |
188 | int test3(long n, long k, long q){
189 |
190 | long a = random(2, n-2); // long min = 2, max = n-2; a=rand() % (max - min + 1) + min;
191 |
192 | if (raiseto_mod(a, q, n) == 1){
193 |
194 |
195 |
196 | return 0; // "inconclusive";
197 | }
198 | for (long j = 0; j < k; j++){
199 | if (raiseto_mod(a,power(2,j),n) == n-1)
200 | return 0; //"inconclusive";
201 | }
202 | return 1; //"composite";
203 | }
204 |
205 | bool isprime(long n, int iterations = 1){
206 | // Primality test using Miller-Rabin algorithm
207 | if (n % 2 == 0){
208 |
209 | return 0;
210 | }
211 | long *t1 = findkq(n);
212 | //long k = t1[0];
213 | //long q = t1[1];
214 |
215 | for (int i = 0; i < iterations; i++){
216 | int result = test3(n,t1[0],t1[1]);
217 | if (result == 1) {
218 | return 0;
219 | }
220 | }
221 |
222 | return 1;
223 | }
224 |
225 | long* generate_keys(long lower, long upper) {
226 | long n, phi_n, p, q, e, d;
227 |
228 | while(1) {
229 |
230 | p = getprime(lower, upper);
231 | q = getprime(lower, upper);
232 | n = p * q;
233 | phi_n = (p-1) * (q-1);
234 | e = getrelprime(phi_n);
235 | d = modinv(e, phi_n);
236 | if (d>= 2)
237 | break;
238 | }
239 |
240 | long *Key;
241 | Key = new long[3];
242 | Key[0] = n; Key[1] = e; Key[2] = d;
243 |
244 | // Public Key is {n,e}
245 | // Private Key is {n, d}
246 |
247 | return Key;
248 | }
249 | */
250 |
--------------------------------------------------------------------------------
/digital_signature/arduino/util.h:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
4 |
5 | This file is part of cryptographic-protocols-arduino-and-PC source code.
6 |
7 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with the software. If not, see .
19 |
20 | */
21 |
22 |
23 | #include
24 |
25 | long power(long a, long b) {
26 | long temp = 1;
27 | for (long i = 0; i < b; ++i) {
28 | temp = temp * a;
29 | }
30 | return temp;
31 | }
32 |
33 | int size_string (unsigned char* string ) {
34 | int i;
35 | for (i = 0; string[i] != '\0'; ++i);
36 | return i;
37 | }
38 |
39 | int size_string (char* string ) {
40 | int i;
41 | for (i = 0; string[i] != '\0'; ++i);
42 | return i;
43 | }
44 | char* to_string(long num) {
45 | char* str;
46 | if (num == 0) {
47 | str = new char[2];
48 | str[0] = '0';
49 | str[1] = '\0';
50 | return str;
51 | }
52 |
53 | long num_of_digits ;//= (long) (log10(num)) + 1;
54 | static long cpy_num=num;
55 | for(num_of_digits=1;cpy_num>=1;num_of_digits++)
56 | cpy_num/=10;
57 |
58 | long rem;
59 |
60 | str = new char[num_of_digits + 1];
61 |
62 | str[num_of_digits] = '\0';
63 | for (long i = num_of_digits- 1; i >= 0; --i) {
64 |
65 | rem = num % ((long)(10));
66 |
67 | char rem_char = (char)(rem + 48); // '0'
68 | str[i] = rem_char;
69 | num = num / 10;
70 | }
71 |
72 | return str;
73 | }
74 |
75 | long to_num(char* str) {
76 | int len = size_string(str);
77 | long num=0;
78 | int number_is_zero = 1;
79 | for (int i = 0; i < len; ++i) {
80 | if (str[i] != '0') {
81 | number_is_zero = 0;
82 | break;
83 | }
84 | }
85 | if (number_is_zero == 1)
86 | return 0;
87 | for (int i = 0; i < len; ++i) {
88 | if (str[i] != '0')
89 | num += power(10, len - i - 1) * ( str[i] - '0' );
90 | }
91 | return num;
92 | }
93 |
94 | long raiseto_mod(long a, long b, long c) {
95 | long temp = 1;
96 | for (long i = 0; i < b; ++i) {
97 | temp = (temp * a) % c;
98 | }
99 | return temp;
100 | }
101 |
--------------------------------------------------------------------------------
/key_exchange_DiffieHellman/PC/cycles.py:
--------------------------------------------------------------------------------
1 | """
2 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
3 |
4 | This file is part of cryptographic-protocols-arduino-and-PC source code.
5 |
6 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with the software. If not, see .
18 | """
19 |
20 | from util import *
21 | from Crypto.Cipher import AES
22 | from time import sleep
23 |
24 | def cycle_arduino_to_PC(AES_key_exchange_function, arduino, caller="DiffieHellman"):
25 | #receiving ciphertext
26 | ctext = []
27 |
28 | AES_Key = AES_key_exchange_function(arduino)
29 | if caller == "RSA":
30 | arduino.readline()
31 |
32 | length = int(arduino.readline())
33 | AES_Key_string = ascii_int_list2string(AES_Key)
34 |
35 | pycrypto_AES = AES.new(AES_Key_string, AES.MODE_ECB)
36 |
37 | ptext_actual = zeropadding("Drones are hovering over your head", 16)[0]
38 |
39 | print "length = ", length
40 |
41 | for i in range(length):
42 | x = int(arduino.readline())
43 | ctext.append(x)
44 |
45 | ctext_string = ascii_int_list2string(ctext)
46 |
47 | ptext_string = pycrypto_AES.decrypt(ctext_string)
48 |
49 | print "ciphertext (string) = ", ctext_string
50 | print "ciphertext (ASCII values) = ", ctext
51 | print "plaintext (string) = ", ptext_string
52 | print "plaintext (ASCII values) = ", string2ascii_int_list(ptext_string)
53 |
54 | def cycle_PC_to_arduino(AES_Key, plaintext, arduino, caller = "DiffieHellman"):
55 |
56 | ptext_string = plaintext
57 | ptext_string = zeropadding(ptext_string, 16)[0]
58 | AES_Key_string = ascii_int_list2string(AES_Key)
59 | pycrypto_AES = AES.new(AES_Key_string, AES.MODE_ECB)
60 | ctext_string = pycrypto_AES.encrypt(ptext_string)
61 | ctext = string2ascii_int_list(ctext_string)
62 |
63 | if 0 in ctext:
64 | return 0
65 | else:
66 | arduino.write("2&g&xb3leL")
67 |
68 | print "AES Key = ", AES_Key
69 | print "Actual length = ", len(ctext)
70 | arduino.write(append_zeros_at_front(len(ctext), MOST_NO_OF_DIGITS))
71 |
72 | print "Ciphertext (int list) = ", ctext
73 | print "Ciphertext = ", ctext_string
74 |
75 | for i in range(len(ctext)):
76 | sleep(0.002)
77 | arduino.write(append_zeros_at_front(ctext[i], 3))
78 | if caller == "RSA":
79 | arduino.readline()
80 | return 1
81 |
82 | def cycle_PC_to_arduino_executer(plaintext, AES_key_exchange_function, arduino, caller="DiffieHellman"):
83 | flag = 0
84 | while flag == 0:
85 | AES_key = AES_key_exchange_function(arduino)
86 |
87 | flag = cycle_PC_to_arduino(AES_key, plaintext, arduino, caller)
88 |
89 | if flag == 0:
90 | arduino.write("K2qkR^ICGU")
91 |
--------------------------------------------------------------------------------
/key_exchange_DiffieHellman/PC/diffiehellman_oneparty.py:
--------------------------------------------------------------------------------
1 | """
2 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
3 |
4 | This file is part of cryptographic-protocols-arduino-and-PC source code.
5 |
6 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with the software. If not, see .
18 | """
19 |
20 | from random import randint
21 | import serial
22 | from cycles import *
23 | from util import *
24 | from time import sleep
25 |
26 | COMPORT = 6
27 | a = 6
28 | q = 761
29 | MOST_NO_OF_DIGITS = 10
30 |
31 | def Diffie_Hellman_num_exchange(arduino):
32 | X = randint(2, q-1)
33 | Y = (a ** X) % q
34 | arduino.write(append_zeros_at_front(str(Y), MOST_NO_OF_DIGITS))
35 |
36 | Y_other = int(arduino.readline())
37 |
38 | K = (Y_other ** X ) % q
39 | Diffie_Hellman_num = K % 256
40 | return Diffie_Hellman_num
41 |
42 | def Diffie_Hellman_AES_Key_exchange(arduino):
43 | AES_key = []
44 | for i in range(16):
45 | AES_key.append(Diffie_Hellman_num_exchange(arduino))
46 | return AES_key
47 |
48 |
49 | def main():
50 | arduino = serial.Serial(COMPORT, 115200)
51 | arduino.timeout = 1.5
52 |
53 | print "1. Send Encrypted Text from PC to Arduino"
54 | print "2. Send Encrypted Text from Arduino to PC"
55 | print "3. Benchmark AES"
56 |
57 | choice = raw_input("\nEnter your choice: ")
58 |
59 | while choice not in ['1','2','3']:
60 | choice = raw_input("Wrong choice. Enter again: ")
61 | if choice == '1':
62 | plaintext = "Guido van Rossum created the language Python"
63 | synchronize("TkSOzZU9Wd*!byau$U2a\n", arduino)
64 |
65 | arduino.write("PC_TO_ARDUINO")
66 | cycle_PC_to_arduino_executer(plaintext, Diffie_Hellman_AES_Key_exchange , arduino)
67 | elif choice == '2':
68 | synchronize("TkSOzZU9Wd*!byau$U2a\n", arduino)
69 | arduino.write("ARDUINO_TO_PC")
70 |
71 | cycle_arduino_to_PC(Diffie_Hellman_AES_Key_exchange, arduino)
72 | elif choice=='3':
73 | synchronize("TkSOzZU9Wd*!byau$U2a\n", arduino)
74 | arduino.write("BENCHMARK_AES")
75 |
76 | main()
77 |
--------------------------------------------------------------------------------
/key_exchange_DiffieHellman/PC/util.py:
--------------------------------------------------------------------------------
1 | """
2 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
3 |
4 | This file is part of cryptographic-protocols-arduino-and-PC source code.
5 |
6 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with the software. If not, see .
18 | """
19 |
20 | MOST_NO_OF_DIGITS = 10
21 |
22 | def zeropadding(string, block):
23 | length = len(string)
24 | no_of_blocks = length/block + 1
25 | length_last_block = length % block
26 | if length_last_block == 0:
27 | return [string, length]
28 | string = string + (no_of_blocks * block - length_last_block) * "\0"
29 | return [string, no_of_blocks * block]
30 |
31 | def string2ascii_int_list(string):
32 | ascii_int_list=[]
33 | for char in string:
34 | ascii_int_list.append(ord(char))
35 | return ascii_int_list
36 |
37 | def ascii_int_list2string(ascii_int_list):
38 | string=[]
39 | for integer in ascii_int_list:
40 | string.append(chr(integer))
41 | return ''.join(string)
42 |
43 | def append_zeros_at_front(num, no_of_digits):
44 | num_str = str(num)
45 | length = len(num_str)
46 | if length >= no_of_digits:
47 | return num_str
48 | for i in range(no_of_digits - length):
49 | num_str = '0' + num_str
50 | return num_str
51 |
52 |
53 | def synchronize(str, arduino):
54 | while arduino.readline() != str:
55 | pass
56 |
--------------------------------------------------------------------------------
/key_exchange_DiffieHellman/arduino/aes.h:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
4 |
5 | This file is part of cryptographic-protocols-arduino-and-PC source code.
6 |
7 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with the software. If not, see .
19 |
20 | */
21 |
22 | #include
23 |
24 | unsigned char sbox[256] =
25 | {
26 | 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
27 | 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
28 | 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
29 | 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
30 | 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
31 | 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
32 | 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
33 | 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
34 | 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
35 | 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
36 | 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
37 | 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
38 | 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
39 | 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
40 | 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
41 | 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
42 | };
43 | unsigned char inv_sbox[256] =
44 | {
45 | 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB,
46 | 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB,
47 | 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
48 | 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25,
49 | 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92,
50 | 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
51 | 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06,
52 | 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B,
53 | 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
54 | 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E,
55 | 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B,
56 | 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
57 | 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F,
58 | 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF,
59 | 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
60 | 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D
61 | };
62 |
63 | unsigned char rcon[11] = {0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b,0x36};
64 | const int block = 16; // 16 bytes = 128 bits (AES-128)
65 |
66 | //________________ENCRYPT SECTION START
67 |
68 | void schedule_core(unsigned char *in, unsigned char i) {
69 | unsigned char a;
70 | // Rotate the input 8 bits to the left
71 | unsigned char c;
72 | a = in[0];
73 | for(c=0;c<3;c++)
74 | in[c] = in[c + 1];
75 | in[3] = a;
76 | // Apply Rijndael's S-box on all 4 bytes
77 | for(a = 0; a < 4; a++)
78 | in[a] = sbox[in[a]];
79 | in[0] ^= rcon[i];
80 | }
81 |
82 | void expand_key(unsigned char *key) {
83 | unsigned char t[4];
84 | //first sub-key is the user-supplied key
85 | unsigned char c = 16;
86 | unsigned char i = 1;
87 | unsigned char a;
88 |
89 |
90 | while(c < 176) {
91 | for(a = 0; a < 4; a++)
92 | t[a] = key[a + c - 4];
93 |
94 | if(c % 16 == 0) {
95 | schedule_core(t,i);
96 | i++;
97 | }
98 |
99 | for(a = 0; a < 4; a++) {
100 | key[c] = key[c - 16] ^ t[a];
101 | c++;
102 | }
103 | }
104 | }
105 |
106 | void addroundkey(unsigned char *state,unsigned char *key,unsigned short int& Round)
107 | {
108 | unsigned short int index=0;
109 | index=Round; index=(index*16);
110 | for(unsigned short int i=0;i<16;i++)
111 | {
112 |
113 | state[i]^=key[index];
114 |
115 | index++;
116 | }
117 | }
118 | void substitution_byte(unsigned char *state)
119 | {
120 | unsigned short int row,col;
121 | for(unsigned int i=0;i<16;i++)
122 | {
123 | row=state[i] & 0xf0;
124 | row = row >> 4;
125 | col=state[i]&0x0f;
126 | state[i]=sbox[16*row+col];
127 |
128 | }
129 | }
130 |
131 | void shift_rows(unsigned char *state)
132 | {
133 | unsigned char temp_state[17];
134 | unsigned short int loopcount=0,state_index;
135 | for(;loopcount<4;loopcount++)
136 | {
137 | state_index=loopcount*4;
138 | temp_state[loopcount*4]=state[state_index];
139 |
140 | for(unsigned short int temp_index=((loopcount*4)+1);temp_index<((loopcount*4)+4);temp_index++)
141 | {
142 | state_index=state_index+5;
143 | if(state_index>15)
144 | state_index=state_index-16;
145 | temp_state[temp_index]=state[state_index];
146 |
147 | }
148 | }
149 | for(unsigned short int i=0;i<16;i++)
150 | state[i]=temp_state[i];
151 | }
152 |
153 | void gmix_column(unsigned char *r) {
154 | unsigned char a[4];
155 | unsigned char b[4];
156 | unsigned char c;
157 | unsigned char h;
158 | for(c=0;c<4;c++) {
159 | a[c] = r[c];
160 | h = r[c] & 0x80;
161 | b[c] = r[c] << 1;
162 | if(h == 0x80)
163 | b[c] ^= 0x1b;
164 | }
165 | r[0] = b[0] ^ a[3] ^ a[2] ^ b[1] ^ a[1];
166 | r[1] = b[1] ^ a[0] ^ a[3] ^ b[2] ^ a[2];
167 | r[2] = b[2] ^ a[1] ^ a[0] ^ b[3] ^ a[3];
168 | r[3] = b[3] ^ a[2] ^ a[1] ^ b[0] ^ a[0];
169 | }
170 |
171 | void mix_column(unsigned char *state) {
172 | for ( int i = 0; i < 4; ++i)
173 | gmix_column(state + (i*4));
174 | }
175 |
176 | void genericround(unsigned char* state, unsigned char* expandedkey, unsigned short int& Round) {
177 | substitution_byte(state);
178 | shift_rows(state);
179 | mix_column(state);
180 | addroundkey(state, expandedkey, Round);
181 | }
182 |
183 | void lastround(unsigned char* state, unsigned char* expandedkey, unsigned short int& Round) {
184 | substitution_byte(state);
185 | shift_rows(state);
186 | addroundkey(state, expandedkey, Round);
187 | }
188 |
189 | void AES_encrypt(unsigned char *state, unsigned char *key) {
190 | // First phase: addRoundKey call once
191 | unsigned short int Round = 0;
192 | addroundkey(state, key, Round);
193 |
194 | Round++;
195 | //Second phase: call genericround() 9 times
196 | for (unsigned short int i = 1; i<= 9 ; ++i) {
197 | genericround(state, key, Round);
198 | Round++;
199 | }
200 | // Third phase: call an incomplete round
201 | lastround(state,key,Round);
202 | //return state;
203 | }
204 |
205 | /*
206 | Commented out as zeropadding was included in the ECB_AES_encrypt function itself
207 |
208 | unsigned char* zeropadding(unsigned char *string, int block, int& adjusted_length) {
209 | // length_last_block is 0 if the length is a multiple of block and the length of the last block otberwise
210 |
211 | int length = size_string(string);
212 |
213 | int no_of_blocks = length / block + 1;
214 |
215 | int length_last_block = length % block;
216 | if (length_last_block == 0)
217 | return string;
218 | int pos = block * (no_of_blocks - 1) + length_last_block;
219 |
220 | for (int i = 0; i < block - length_last_block; ++i) {
221 | //pos = block * no_of_blocks;
222 | string[pos] = 0;
223 |
224 | ++pos;
225 | }
226 | string[pos] = '\0';
227 | length = pos;
228 | adjusted_length = pos;
229 |
230 | return string;
231 | }
232 | */
233 |
234 | unsigned char* ECB_AES_encrypt(unsigned char* plaintext, unsigned char* key, int& FINAL_LENGTH) {
235 | expand_key(key);
236 | int length = size_string(plaintext);
237 | int no_of_blocks;
238 | int length_last_block = length % block;
239 | if (length_last_block == 0)
240 | no_of_blocks = length / block;
241 | else
242 | no_of_blocks = length / block + 1;
243 |
244 | unsigned char* ciphertext; // = zeropadding(plaintext, block, FINAL_LENGTH );
245 |
246 | // ZERO PADDING STARTS
247 | int pos;
248 | if (length_last_block != 0) {
249 | pos = block * (no_of_blocks - 1) + length_last_block;
250 | for (int i = 0; i < block - length_last_block; ++i) {
251 | //pos = block * no_of_blocks;
252 | plaintext[pos] = 0;
253 |
254 | ++pos;
255 | }
256 | plaintext[pos] = '\0';
257 | FINAL_LENGTH = pos;
258 | }
259 |
260 | // ZERO PADDING ENDS
261 |
262 | ciphertext = plaintext;
263 | unsigned char *pointer_to_first_element_of_block;
264 | pointer_to_first_element_of_block = ciphertext;
265 | for (int i = 0; i < no_of_blocks; ++i) {
266 |
267 | AES_encrypt(pointer_to_first_element_of_block, key);
268 | pointer_to_first_element_of_block += block;
269 |
270 | }
271 |
272 | return ciphertext;
273 |
274 | }
275 | //_____________________ENCRYPT SECTION END_______________
276 | //_____________________DECRYPT SECTION START_____________
277 |
278 | void inverse_shiftrows(unsigned char *state)
279 | {
280 | unsigned char temp_state[17];
281 | unsigned short int loopcount=0,cipher_index;
282 | for(;loopcount<4;loopcount++)
283 | {
284 | cipher_index=loopcount*4;
285 | for(unsigned short int temp_index=(loopcount*4);temp_index<((loopcount*4)+4);temp_index++)
286 | {
287 | temp_state[temp_index]=state[cipher_index];
288 | cipher_index=cipher_index+13;
289 | if(cipher_index>16)
290 | cipher_index=cipher_index-16;
291 | }
292 | }
293 | for(unsigned short int i=0;i<16;i++)
294 | state[i]=temp_state[i];
295 | }
296 | void inverse_substitutionbytes(unsigned char *state)
297 | {
298 | unsigned short int row,col;
299 | for(unsigned int i=0;i<16;i++)
300 | {
301 | row=state[i] & 0xf0;
302 | row = row >> 4;
303 | col=state[i]&0x0f;
304 | state[i]=inv_sbox[16*row+col];
305 |
306 | }
307 | }
308 | unsigned char gmul(unsigned char a, unsigned char b) {
309 | unsigned char p = 0;
310 | unsigned char counter;
311 | unsigned char hi_bit_set;
312 | for(counter = 0; counter < 8; counter++) {
313 | if((b & 1) == 1)
314 | p ^= a;
315 | hi_bit_set = (a & 0x80);
316 | a <<= 1;
317 | if(hi_bit_set == 0x80)
318 | a ^= 0x1b;
319 | b >>= 1;
320 | }
321 | return p;
322 | }
323 | void inv_mix_column(unsigned char *r) {
324 | unsigned char a[4];
325 | unsigned char c;
326 | for(c=0;c<4;c++) {
327 | a[c] = r[c];
328 | }
329 | r[0] = gmul(a[0],14) ^ gmul(a[3],9) ^ gmul(a[2],13) ^ gmul(a[1],11);
330 | r[1] = gmul(a[1],14) ^ gmul(a[0],9) ^ gmul(a[3],13) ^ gmul(a[2],11);
331 | r[2] = gmul(a[2],14) ^ gmul(a[1],9) ^ gmul(a[0],13) ^ gmul(a[3],11);
332 | r[3] = gmul(a[3],14) ^ gmul(a[2],9) ^ gmul(a[1],13) ^ gmul(a[0],11);
333 | }
334 | void inverse_mix(unsigned char *state)
335 | {
336 | for(unsigned short int i=0;i<4;i++)
337 | inv_mix_column((state+i*4));
338 | }
339 | void decrypt_generic_round(unsigned char *state,unsigned char *key,unsigned short int& Round)
340 | {
341 | inverse_shiftrows(state);
342 | inverse_substitutionbytes(state);
343 | addroundkey(state,key,Round);
344 | inverse_mix(state);
345 | }
346 |
347 | void decrypt_lastround(unsigned char *state,unsigned char *key,unsigned short int& Round)
348 | {
349 | inverse_shiftrows(state);
350 | inverse_substitutionbytes(state);
351 | addroundkey(state,key,Round);
352 | }
353 | void AES_decrypt(unsigned char *ciphertext,unsigned char *key, int length)
354 | {
355 | unsigned short int Round = 10;
356 |
357 | addroundkey(ciphertext,key,Round);
358 | Round--;
359 | for(;Round>=1;Round--)
360 | decrypt_generic_round(ciphertext,key,Round);
361 | decrypt_lastround(ciphertext,key,Round);
362 |
363 | }
364 |
365 | unsigned char* ECB_AES_decrypt(unsigned char *ciphertext, unsigned char *key) {
366 | expand_key(key);
367 | int length = size_string(ciphertext);
368 | int length_last_block = length % block;
369 | int no_of_blocks;
370 | if (length_last_block == 0)
371 | no_of_blocks = length / block;
372 | else
373 | no_of_blocks = length / block + 1;
374 |
375 | unsigned char * pointer_to_first_element_of_block;
376 | pointer_to_first_element_of_block = ciphertext;
377 | for (int i = 0; i < no_of_blocks; ++i) {
378 |
379 | AES_decrypt(pointer_to_first_element_of_block, key, block);
380 | pointer_to_first_element_of_block += block;
381 |
382 |
383 | }
384 |
385 | return ciphertext;
386 | }
387 |
388 |
389 | char* int2string(long num) {
390 | char* str;
391 | int num_of_digits = (int) (log10(num)) + 1;
392 | int remainder;
393 | str = new char[num_of_digits + 1];
394 | str[num_of_digits] = '\0';
395 | for (int i = num_of_digits-1; i >= 0; --i) {
396 | remainder = num % 10;
397 | char rem_char = (char)(remainder + '0'); //ASCII value of '0' = 48
398 | str[i] = rem_char;
399 | num = num / 10;
400 | }
401 | return str;
402 | }
403 |
--------------------------------------------------------------------------------
/key_exchange_DiffieHellman/arduino/diffie_hellman_final.ino:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
4 |
5 | This file is part of cryptographic-protocols-arduino-and-PC source code.
6 |
7 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with the software. If not, see .
19 |
20 | */
21 |
22 | #include
23 | #include "aes.h"
24 |
25 | const long a = 6;
26 | const long q = 761;
27 |
28 | LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
29 |
30 | void lcd_print_string(char *text, LiquidCrystal lcd, int delay_time = 2000, int lcd_size = 32) {
31 | int length = size_string(text);
32 | int lcd_half_size = lcd_size / 2;
33 | int no_of_lines = length / lcd_half_size;
34 | int length_last_line = length % lcd_half_size;
35 | lcd.clear();
36 |
37 | for (int i = 0; i < no_of_lines + 1; ++i) {
38 | if (i % 2 == 0)
39 | lcd.setCursor(0,0);
40 | else
41 | lcd.setCursor(0,1);
42 | lcd.print(text + i * lcd_half_size);
43 | if (i % 2) {
44 | delay(delay_time);
45 | if (i != no_of_lines)
46 | lcd.clear();
47 | }
48 | }
49 | }
50 |
51 | void lcd_print_string(unsigned char *text, LiquidCrystal lcd, int delay_time = 2000, int lcd_size = 32) {
52 | int length = size_string(text);
53 | int lcd_half_size = lcd_size / 2;
54 | int no_of_lines = length / lcd_half_size;
55 | int length_last_line = length % lcd_half_size;
56 | lcd.clear();
57 |
58 | for (int i = 0; i < no_of_lines + 1; ++i) {
59 | if (i % 2 == 0)
60 | lcd.setCursor(0,0);
61 | else
62 | lcd.setCursor(0,1);
63 | lcd.print((char *)(text + i * lcd_half_size));
64 | if (i % 2 ) {
65 | delay(delay_time);
66 | if (i != no_of_lines)
67 | lcd.clear();
68 | }
69 | }
70 | }
71 |
72 |
73 | unsigned char Diffie_Hellman_num_exchange(){
74 |
75 | long X, Y, Y_other, K, temp;
76 | char Y_other_str[MOST_NO_OF_DIGITS + 1], incByte;
77 |
78 | X = random(2, q-1);
79 |
80 | Y = raiseto_mod(a, X, q);
81 |
82 | Serial.readBytes(Y_other_str, MOST_NO_OF_DIGITS);
83 | Serial.println(to_string(Y));
84 |
85 | Y_other_str[MOST_NO_OF_DIGITS] = '\0';
86 |
87 | Y_other = to_num(Y_other_str);
88 |
89 | K = raiseto_mod(Y_other, X, q);
90 | int diffie_hellman_num = K % 256;
91 | return (unsigned char) diffie_hellman_num;
92 | }
93 |
94 |
95 | void cycle_arduino_to_PC(unsigned char *AES_Key) {
96 | unsigned char ptext[100] = "North Korea invaded the United States today", *ctext;
97 |
98 | int FINAL_LENGTH = size_string(ptext);
99 | ctext = ECB_AES_encrypt(ptext, AES_Key, FINAL_LENGTH);
100 |
101 | Serial.println(to_string(FINAL_LENGTH));
102 |
103 | for (int i = 0; i < FINAL_LENGTH; ++i) {
104 | Serial.println(ctext[i]);
105 | }
106 | }
107 |
108 |
109 | void cycle_PC_to_arduino(unsigned char* AES_Key) {
110 | unsigned char ctext[100], *ptext, AES_Key_uc[178];
111 | int length;
112 | char length_string[MOST_NO_OF_DIGITS + 1];
113 | Serial.readBytes(length_string, MOST_NO_OF_DIGITS);
114 | length_string[MOST_NO_OF_DIGITS] = '\0';
115 | length = to_num(length_string);
116 | int i;
117 |
118 | for (i = 0; i < length; ++i) {
119 | char num_str[4];
120 | Serial.readBytes(num_str, 3);
121 | num_str[3] = '\0';
122 | ctext[i] = to_num(num_str);
123 | }
124 | ctext[i] = '\0';
125 |
126 | ptext = ECB_AES_decrypt(ctext, AES_Key);
127 | lcd.clear();
128 | lcd.setCursor(0,0);
129 | lcd_print_string(ptext, lcd);
130 | delay(10000);
131 | }
132 |
133 | void BenchmarkAES() {
134 | unsigned char AES_key[16], ptext[100], *ctext;
135 | unsigned long start_time_en, end_time_en, time_taken_en;
136 | unsigned long start_time_de, end_time_de, time_taken_de;
137 | int FINAL_LENGTH;
138 | for (int i = 0; i < 16; ++i) {
139 | AES_key[i] = random(1,255);
140 | }
141 | for (int i = 0; i < 48; ++i) {
142 | ptext[i] = random(1,255);
143 | }
144 | start_time_en = micros();
145 | ctext = ECB_AES_encrypt(ptext, AES_key, FINAL_LENGTH);
146 | end_time_en = micros();
147 | time_taken_en = end_time_en - start_time_en;
148 | unsigned char *ptext2;
149 | start_time_de = micros();
150 | ptext2 = ECB_AES_decrypt(ctext, AES_key);
151 | end_time_de = micros();
152 | time_taken_de = end_time_de - start_time_de;
153 | lcd.clear();
154 | lcd.setCursor(0,0);
155 | lcd.print("Encrypt:");
156 | lcd.print(time_taken_en);
157 | unsigned char mu;
158 | mu = 230;
159 | lcd.print("mcs");
160 | lcd.setCursor(0,1);
161 | lcd.print("Decrypt:");
162 | lcd.print(time_taken_de);
163 | lcd.print("mcs");
164 | delay(10000);
165 | }
166 |
167 | void setup() {
168 | lcd.begin(16, 2);
169 |
170 | Serial.begin(115200);
171 | // synchronization
172 | Serial.write("TkSOzZU9Wd*!byau$U2a\n");
173 | unsigned char AES_key[177];
174 | AES_key[176] = '\0';
175 |
176 |
177 | char mode[14];
178 | mode[13] = '\0';
179 | Serial.readBytes(mode, 13);
180 | if (strcmp(mode, "PC_TO_ARDUINO") == 0) {
181 | lcd.clear();
182 | for (int i = 0; i < 16; ++i) {
183 | AES_key[i] = Diffie_Hellman_num_exchange();
184 | }
185 | // read key starts again
186 | char indication[MOST_NO_OF_DIGITS + 1];
187 | Serial.readBytes(indication, MOST_NO_OF_DIGITS);
188 | indication[MOST_NO_OF_DIGITS] = '\0';
189 | while (strcmp(indication, "2&g&xb3leL") != 0) {
190 | for (int i = 0; i < 16; ++i) {
191 | AES_key[i] = Diffie_Hellman_num_exchange();
192 | }
193 | Serial.readBytes(indication, MOST_NO_OF_DIGITS);
194 |
195 | }
196 |
197 | cycle_PC_to_arduino(AES_key);
198 | }
199 | else if (strcmp(mode, "ARDUINO_TO_PC") == 0) {
200 | lcd.clear();
201 | lcd.print("ARDUINO TO PC");
202 |
203 | for (int i = 0; i < 16; ++i) {
204 | AES_key[i] = Diffie_Hellman_num_exchange();
205 | }
206 |
207 | cycle_arduino_to_PC(AES_key);
208 | }
209 | else if (strcmp(mode, "BENCHMARK_AES") == 0) {
210 | lcd.clear();
211 | lcd.print("BENCHMARKING AES");
212 |
213 | BenchmarkAES();
214 |
215 | }
216 | else {
217 | lcd.clear();
218 | lcd.setCursor(0,0);
219 |
220 | lcd.print("NO MODE SELECTED");
221 |
222 | delay(10000);
223 | }
224 |
225 |
226 | }
227 |
228 |
229 |
230 | void loop() { }
231 |
--------------------------------------------------------------------------------
/key_exchange_DiffieHellman/arduino/util.h:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
4 |
5 | This file is part of cryptographic-protocols-arduino-and-PC source code.
6 |
7 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with the software. If not, see .
19 |
20 | */
21 |
22 | #include
23 |
24 | const int MOST_NO_OF_DIGITS = 10;
25 | long power(long a, long b) {
26 | long temp = 1;
27 | for (long i = 0; i < b; ++i) {
28 | temp = temp * a;
29 | }
30 | return temp;
31 | }
32 |
33 | int size_string (unsigned char* string )
34 | { int i;
35 | for (i = 0; string[i] != '\0'; ++i);
36 | return i;
37 | }
38 |
39 | int size_string (char* string )
40 | { int i;
41 | for (i = 0; string[i] != '\0'; ++i);
42 | return i;
43 | }
44 |
45 | char* to_string(long num) {
46 | char* str;
47 | if (num == 0) {
48 | str = new char[2];
49 | str[0] = '0';
50 | str[1] = '\0';
51 | return str;
52 | }
53 |
54 | long num_of_digits = (long) (log10(num)) + 1;
55 | long rem;
56 |
57 | str = new char[num_of_digits + 1];
58 |
59 | str[num_of_digits] = '\0';
60 | for (long i = num_of_digits- 1; i >= 0; --i) {
61 |
62 | rem = num % ((long)(10));
63 |
64 | char rem_char = (char)(rem + 48); // '0'
65 | str[i] = rem_char;
66 | num = num / 10;
67 | }
68 |
69 | return str;
70 | }
71 |
72 | long to_num(char* str) {
73 | int len = size_string(str);
74 | long num=0;
75 | int number_is_zero = 1;
76 | for (int i = 0; i < len; ++i) {
77 | if (str[i] != '0') {
78 | number_is_zero = 0;
79 | break;
80 | }
81 | }
82 | if (number_is_zero == 1)
83 | return 0;
84 | for (int i = 0; i < len; ++i) {
85 | if (str[i] != '0')
86 | num += power(10, len - i - 1) * ( str[i] - '0' );
87 | }
88 | return num;
89 | }
90 |
91 | long raiseto_mod(long a, long b, long c) {
92 | long temp = 1;
93 | for (long i = 0; i < b; ++i) {
94 | temp = (temp * a) % c;
95 | }
96 | return temp;
97 | }
98 |
--------------------------------------------------------------------------------
/key_exchange_RSA/PC/Euclid.py:
--------------------------------------------------------------------------------
1 | """
2 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
3 |
4 | This file is part of cryptographic-protocols-arduino-and-PC source code.
5 |
6 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with the software. If not, see .
18 | """
19 |
20 | def gcd(a,b):
21 | """ Returns greatest common denominator by using recursive Euclidean algorithm """
22 | if b==0:
23 | return a
24 | return gcd(b, a % b)
25 |
26 | def extended_gcd(a, b):
27 | """ Recursive extended Euclidean algorithm """
28 | if a == 0:
29 | return (b, 0, 1)
30 | else:
31 | g, y, x = extended_gcd(b % a, a)
32 | return (g, x - (b // a) * y, y)
33 |
34 | def modinv(a, m):
35 | """ Returns the inverse of a modulo m """
36 | g, x, y = extended_gcd(a, m)
37 | if g != 1:
38 | raise Exception('modular inverse does not exist')
39 | else:
40 | return pow(x, 1, m)
41 |
42 | def getrelprime(num):
43 | """ Returns the smallest relative prime number to num """
44 | for i in xrange(2, num):
45 | if gcd(i, num) == 1:
46 | return i
47 |
--------------------------------------------------------------------------------
/key_exchange_RSA/PC/RSA.py:
--------------------------------------------------------------------------------
1 | """
2 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
3 |
4 | This file is part of cryptographic-protocols-arduino-and-PC source code.
5 |
6 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with the software. If not, see .
18 | """
19 |
20 | from random import randint
21 |
22 | def rsa_encrypt(plaintext, key):
23 | n, e = key
24 | plaintext_list = string2ascii_int_list(plaintext)
25 | ciphertext_list = []
26 |
27 | for M in plaintext_list:
28 | C = pow(M, e, n) # pow (a,b[,c]) returns (a**b) mod c
29 | ciphertext_list.append(C)
30 |
31 | return ciphertext_list
32 |
33 |
34 | def rsa_decrypt(ciphertext_list, key):
35 | n, d = key
36 | plaintext_list = []
37 |
38 | for C in ciphertext_list:
39 | M = pow(C, d, n)
40 | plaintext_list.append(M)
41 | plaintext = ascii_int_list2string(plaintext_list)
42 | return plaintext
43 |
--------------------------------------------------------------------------------
/key_exchange_RSA/PC/cycles.py:
--------------------------------------------------------------------------------
1 | """
2 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
3 |
4 | This file is part of cryptographic-protocols-arduino-and-PC source code.
5 |
6 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with the software. If not, see .
18 | """
19 |
20 | from util import *
21 | from Crypto.Cipher import AES
22 | from time import sleep
23 |
24 | def cycle_arduino_to_PC(AES_key_exchange_function, arduino, caller="DiffieHellman"):
25 | #receiving ciphertext
26 | ctext = []
27 |
28 | AES_Key = AES_key_exchange_function(arduino)
29 | if caller == "RSA":
30 | arduino.readline()
31 |
32 | length = int(arduino.readline())
33 | AES_Key_string = ascii_int_list2string(AES_Key)
34 |
35 | pycrypto_AES = AES.new(AES_Key_string, AES.MODE_ECB)
36 |
37 | ptext_actual = zeropadding("Drones are hovering over your head", 16)[0]
38 |
39 | print "length = ", length
40 |
41 | for i in range(length):
42 | x = int(arduino.readline())
43 | ctext.append(x)
44 |
45 | ctext_string = ascii_int_list2string(ctext)
46 |
47 | ptext_string = pycrypto_AES.decrypt(ctext_string)
48 |
49 | print "ciphertext (string) = ", ctext_string
50 | print "ciphertext (ASCII values) = ", ctext
51 | print "plaintext (string) = ", ptext_string
52 | print "plaintext (ASCII values) = ", string2ascii_int_list(ptext_string)
53 |
54 | def cycle_PC_to_arduino(AES_Key, plaintext, arduino, caller = "DiffieHellman"):
55 | #AES_Key = [234, 225, 2, 127, 90, 164, 181, 96, 150, 105, 99, 180, 55, 218, 185, 11]
56 | ptext_string = plaintext
57 | ptext_string = zeropadding(ptext_string, 16)[0]
58 | AES_Key_string = ascii_int_list2string(AES_Key)
59 | pycrypto_AES = AES.new(AES_Key_string, AES.MODE_ECB)
60 | ctext_string = pycrypto_AES.encrypt(ptext_string)
61 | ctext = string2ascii_int_list(ctext_string)
62 |
63 | if 0 in ctext:
64 | return 0
65 | else:
66 | arduino.write("2&g&xb3leL")
67 |
68 | print "AES Key = ", AES_Key
69 | print "Actual length = ", len(ctext)
70 | arduino.write(append_zeros_at_front(len(ctext), MOST_NO_OF_DIGITS))
71 |
72 | print "Ciphertext (int list) = ", ctext
73 | print "Ciphertext = ", ctext_string
74 |
75 | for i in range(len(ctext)):
76 | sleep(0.002)
77 | arduino.write(append_zeros_at_front(ctext[i], 3))
78 | if caller == "RSA":
79 | arduino.readline()
80 | return 1
81 |
82 | def cycle_PC_to_arduino_executer(plaintext, AES_key_exchange_function, arduino, caller="DiffieHellman"):
83 | flag = 0
84 | while flag == 0:
85 | AES_key = AES_key_exchange_function(arduino)
86 |
87 | flag = cycle_PC_to_arduino(AES_key, plaintext, arduino, caller)
88 |
89 | if flag == 0:
90 | arduino.write("K2qkR^ICGU")
91 |
--------------------------------------------------------------------------------
/key_exchange_RSA/PC/prime.py:
--------------------------------------------------------------------------------
1 | """
2 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
3 |
4 | This file is part of cryptographic-protocols-arduino-and-PC source code.
5 |
6 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with the software. If not, see .
18 | """
19 |
20 | from Euclid import *
21 | from random import randint
22 |
23 | def findkq(n):
24 | """ Find k and q such that n = (2**k)*q """
25 | if n % 2 == 0 or n<0:
26 | return "Error"
27 | n_new = n-1;
28 |
29 | # 1.Find integers k, q with k>0 and q odd, such that N = (2^k)*q
30 | k = 0
31 | q = 0
32 | while (n_new):
33 | n_new /= 2
34 | k += 1
35 | if n_new % 2 != 0:
36 | q = n_new
37 | break
38 | return [k,q]
39 |
40 | def isprime(n, iterations = 1):
41 | """ Primality test using Miller-Rabin algorithm"""
42 | if n % 2 == 0:
43 | return False
44 | # 1. Find integers k,q with k>0, q odd, so that n - 1 = (2**k)*q
45 | k, q = findkq(n)
46 |
47 |
48 | # 2. Select a random integer a, 1 < a < n-1
49 | for i in range(iterations):
50 | result = test3(n, k, q)
51 |
52 | if result =="composite":
53 | return False
54 |
55 | return True
56 |
57 | def test3(n, k, q):
58 | a = randint(2, n-2)
59 | # 3. if a**q mod n = 1, then return "inconclusive"
60 | if (a**q) % n == 1:
61 | return "inconclusive"
62 | # 4. and 5.
63 | for j in range(k):
64 | if a**(q*(2**j)) % n == n - 1:
65 | return "inconclusive"
66 | return "composite"
67 |
--------------------------------------------------------------------------------
/key_exchange_RSA/PC/rsa_aes_protocol.py:
--------------------------------------------------------------------------------
1 | """
2 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
3 |
4 | This file is part of cryptographic-protocols-arduino-and-PC source code.
5 |
6 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with the software. If not, see .
18 | """
19 |
20 | #from Crypto import Random
21 | import RSA
22 | from Crypto.Cipher import AES
23 | from random import randint
24 | from util import *
25 | #from cycles import *
26 | import serial
27 | from time import sleep
28 |
29 | RSA_Others_Public_Key = [5723, 5]
30 | MOST_NO_OF_DIGITS_IN_LENGTH = 10
31 | COMPORT = 6
32 |
33 | def choose_COM_port():
34 | global COMPORT
35 | print "Enter the COM port name. It could be an integer for COM%d, or a path like '/dev/ttyACM0'"
36 | COM_input = raw_input("COM port name (default is '%s'): " %COMPORT)
37 | if COM_input.isdigit():
38 | COMPORT = int(COM_input)
39 | elif COM_input:
40 | COMPORT = COM_input
41 | return
42 |
43 | def cycle_arduino_to_PC(AES_key_exchange_function, arduino, caller="DiffieHellman"):
44 | #receiving ciphertext
45 | ctext = []
46 |
47 | AES_Key = AES_key_exchange_function(arduino)
48 | if caller == "RSA":
49 | arduino.readline()
50 |
51 | length = int(arduino.readline())
52 | AES_Key_string = ascii_int_list2string(AES_Key)
53 |
54 | pycrypto_AES = AES.new(AES_Key_string, AES.MODE_ECB)
55 |
56 | ptext_actual = zeropadding("Drones are hovering over your head", 16)[0]
57 |
58 | print "length = ", length
59 |
60 | for i in range(length):
61 | x = int(arduino.readline())
62 | ctext.append(x)
63 |
64 | ctext_string = ascii_int_list2string(ctext)
65 |
66 | ptext_string = pycrypto_AES.decrypt(ctext_string)
67 |
68 | print "ciphertext (string) = ", ctext_string
69 | print "ciphertext (ASCII values) = ", ctext
70 | print "plaintext (string) = ", ptext_string
71 | print "plaintext (ASCII values) = ", string2ascii_int_list(ptext_string)
72 |
73 | def cycle_PC_to_arduino(AES_Key, plaintext, arduino, caller = "DiffieHellman"):
74 |
75 | ptext_string = plaintext
76 | ptext_string = zeropadding(ptext_string, 16)[0]
77 | AES_Key_string = ascii_int_list2string(AES_Key)
78 | pycrypto_AES = AES.new(AES_Key_string, AES.MODE_ECB)
79 | ctext_string = pycrypto_AES.encrypt(ptext_string)
80 | ctext = string2ascii_int_list(ctext_string)
81 |
82 | if 0 in ctext:
83 | return 0
84 | else:
85 | arduino.write("2&g&xb3leL")
86 |
87 | print "AES Key = ", AES_Key
88 | print "Actual length = ", len(ctext)
89 | arduino.write(append_zeros_at_front(len(ctext), MOST_NO_OF_DIGITS))
90 |
91 | print "Ciphertext (int list) = ", ctext
92 | print "Ciphertext = ", ctext_string
93 |
94 | for i in range(len(ctext)):
95 | sleep(0.002)
96 | arduino.write(append_zeros_at_front(ctext[i], 3))
97 | if caller == "RSA":
98 | arduino.readline()
99 | return 1
100 |
101 | def cycle_PC_to_arduino_executer(plaintext, AES_key_exchange_function, arduino, caller="DiffieHellman"):
102 | flag = 0
103 | while flag == 0:
104 | AES_key = AES_key_exchange_function(arduino)
105 |
106 | flag = cycle_PC_to_arduino(AES_key, plaintext, arduino, caller)
107 |
108 | if flag == 0:
109 | arduino.write("K2qkR^ICGU")
110 |
111 |
112 |
113 | #
114 | def generate_PR_key(length = 16):
115 | key = []
116 | for i in range(length):
117 | key.append(randint(1,255)); #randint(a,b) gives a <= N <= b
118 | return key
119 |
120 | def synchronize(str, arduino):
121 | while arduino.readline() != str:
122 | pass
123 |
124 | AES_KEY_LENGTH = 16
125 | BIGGEST_CIPHERTEXT_LENGTH = 10
126 | def generate_AES_Key_and_exchange(arduino):
127 |
128 | AES_Key = generate_PR_key()
129 | print "Generated Random AES Key = ", AES_Key
130 | AES_Key_string = ascii_int_list2string(AES_Key)
131 | AES_Key_encrypted = RSA.rsa_encrypt(AES_Key_string, RSA_Others_Public_Key)
132 | print "Encrypted AES Key (with RSA) = ", AES_Key_encrypted
133 |
134 | for i in range(AES_KEY_LENGTH):
135 | arduino.write(append_zeros_at_front(AES_Key_encrypted[i], BIGGEST_CIPHERTEXT_LENGTH))
136 | return AES_Key
137 |
138 | def main():
139 | choose_COM_port()
140 | arduino = serial.Serial(COMPORT, 115200)
141 | arduino.timeout = 1.5
142 |
143 | print "1. Send Encrypted Text from PC to Arduino"
144 | print "2. Send Encrypted Text from Arduino to PC"
145 |
146 | choice = raw_input("\nEnter your choice: ")
147 |
148 | while choice not in ['1','2']:
149 | choice = raw_input("Wrong choice. Enter again: ")
150 | print "Executing ", choice
151 | if choice == '1':
152 |
153 | plaintext="I don't know how it gets better than this"
154 | synchronize("TkSOzZU9Wd*!byau$U2a\n", arduino)
155 | arduino.write("PC_TO_ARDUINO")
156 | cycle_PC_to_arduino_executer(plaintext, generate_AES_Key_and_exchange, arduino, "RSA")
157 | elif choice == '2':
158 | synchronize("TkSOzZU9Wd*!byau$U2a\n", arduino)
159 | arduino.write("ARDUINO_TO_PC")
160 | cycle_arduino_to_PC(generate_AES_Key_and_exchange, arduino, "RSA")
161 |
162 | main()
163 |
--------------------------------------------------------------------------------
/key_exchange_RSA/PC/util.py:
--------------------------------------------------------------------------------
1 | """
2 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
3 |
4 | This file is part of cryptographic-protocols-arduino-and-PC source code.
5 |
6 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
7 | it under the terms of the GNU General Public License as published by
8 | the Free Software Foundation, either version 3 of the License, or
9 | (at your option) any later version.
10 |
11 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | GNU General Public License for more details.
15 |
16 | You should have received a copy of the GNU General Public License
17 | along with the software. If not, see .
18 | """
19 |
20 | MOST_NO_OF_DIGITS = 10
21 |
22 | def zeropadding(string, block):
23 | length = len(string)
24 | no_of_blocks = length/block + 1
25 | length_last_block = length % block
26 | if length_last_block == 0:
27 | return [string, length]
28 | string = string + (no_of_blocks * block - length_last_block) * "\0"
29 | return [string, no_of_blocks * block]
30 |
31 | def string2ascii_int_list(string):
32 | ascii_int_list=[]
33 | for char in string:
34 | ascii_int_list.append(ord(char))
35 | return ascii_int_list
36 |
37 | def ascii_int_list2string(ascii_int_list):
38 | string=[]
39 | for integer in ascii_int_list:
40 | string.append(chr(integer))
41 | return ''.join(string)
42 |
43 | def append_zeros_at_front(num, no_of_digits):
44 | num_str = str(num)
45 | length = len(num_str)
46 | if length >= no_of_digits:
47 | return num_str
48 | for i in range(no_of_digits - length):
49 | num_str = '0' + num_str
50 | return num_str
51 |
52 |
53 | def synchronize(str, arduino):
54 | while arduino.readline() != str:
55 | pass
56 |
--------------------------------------------------------------------------------
/key_exchange_RSA/arduino/aes.h:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
4 |
5 | This file is part of cryptographic-protocols-arduino-and-PC source code.
6 |
7 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with the software. If not, see .
19 |
20 | */
21 |
22 | #include
23 |
24 | unsigned char sbox[256] =
25 | {
26 | 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
27 | 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
28 | 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
29 | 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
30 | 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
31 | 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
32 | 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
33 | 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
34 | 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
35 | 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
36 | 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
37 | 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
38 | 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
39 | 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
40 | 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
41 | 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
42 | };
43 | unsigned char inv_sbox[256] =
44 | {
45 | 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB,
46 | 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB,
47 | 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
48 | 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25,
49 | 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92,
50 | 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
51 | 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06,
52 | 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B,
53 | 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
54 | 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E,
55 | 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B,
56 | 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
57 | 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F,
58 | 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF,
59 | 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
60 | 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D
61 | };
62 |
63 | unsigned char rcon[11] = {0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b,0x36};
64 | const int block = 16; // 16 bytes = 128 bits (AES-128)
65 |
66 | //________________ENCRYPT SECTION START
67 |
68 | void schedule_core(unsigned char *in, unsigned char i) {
69 | unsigned char a;
70 | // Rotate the input 8 bits to the left
71 | unsigned char c;
72 | a = in[0];
73 | for(c=0;c<3;c++)
74 | in[c] = in[c + 1];
75 | in[3] = a;
76 | // Apply Rijndael's S-box on all 4 bytes
77 | for(a = 0; a < 4; a++)
78 | in[a] = sbox[in[a]];
79 | in[0] ^= rcon[i];
80 | }
81 |
82 | void expand_key(unsigned char *key) {
83 | unsigned char t[4];
84 | //first sub-key is the user-supplied key
85 | unsigned char c = 16;
86 | unsigned char i = 1;
87 | unsigned char a;
88 |
89 |
90 | while(c < 176) {
91 | for(a = 0; a < 4; a++)
92 | t[a] = key[a + c - 4];
93 |
94 | if(c % 16 == 0) {
95 | schedule_core(t,i);
96 | i++;
97 | }
98 |
99 | for(a = 0; a < 4; a++) {
100 | key[c] = key[c - 16] ^ t[a];
101 | c++;
102 | }
103 | }
104 | }
105 |
106 | void addroundkey(unsigned char *state,unsigned char *key,unsigned short int& Round)
107 | {
108 | unsigned short int index=0;
109 | index=Round; index=(index*16);
110 | for(unsigned short int i=0;i<16;i++)
111 | {
112 |
113 | state[i]^=key[index];
114 |
115 | index++;
116 | }
117 | }
118 | void substitution_byte(unsigned char *state)
119 | {
120 | unsigned short int row,col;
121 | for(unsigned int i=0;i<16;i++)
122 | {
123 | row=state[i] & 0xf0;
124 | row = row >> 4;
125 | col=state[i]&0x0f;
126 | state[i]=sbox[16*row+col];
127 |
128 | }
129 | }
130 |
131 | void shift_rows(unsigned char *state)
132 | {
133 | unsigned char temp_state[17];
134 | unsigned short int loopcount=0,state_index;
135 | for(;loopcount<4;loopcount++)
136 | {
137 | state_index=loopcount*4;
138 | temp_state[loopcount*4]=state[state_index];
139 |
140 | for(unsigned short int temp_index=((loopcount*4)+1);temp_index<((loopcount*4)+4);temp_index++)
141 | {
142 | state_index=state_index+5;
143 | if(state_index>15)
144 | state_index=state_index-16;
145 | temp_state[temp_index]=state[state_index];
146 |
147 | }
148 | }
149 | for(unsigned short int i=0;i<16;i++)
150 | state[i]=temp_state[i];
151 | }
152 |
153 | void gmix_column(unsigned char *r) {
154 | unsigned char a[4];
155 | unsigned char b[4];
156 | unsigned char c;
157 | unsigned char h;
158 | for(c=0;c<4;c++) {
159 | a[c] = r[c];
160 | h = r[c] & 0x80;
161 | b[c] = r[c] << 1;
162 | if(h == 0x80)
163 | b[c] ^= 0x1b;
164 | }
165 | r[0] = b[0] ^ a[3] ^ a[2] ^ b[1] ^ a[1];
166 | r[1] = b[1] ^ a[0] ^ a[3] ^ b[2] ^ a[2];
167 | r[2] = b[2] ^ a[1] ^ a[0] ^ b[3] ^ a[3];
168 | r[3] = b[3] ^ a[2] ^ a[1] ^ b[0] ^ a[0];
169 | }
170 |
171 | void mix_column(unsigned char *state) {
172 | for ( int i = 0; i < 4; ++i)
173 | gmix_column(state + (i*4));
174 | }
175 |
176 | void genericround(unsigned char* state, unsigned char* expandedkey, unsigned short int& Round) {
177 | substitution_byte(state);
178 | shift_rows(state);
179 | mix_column(state);
180 | addroundkey(state, expandedkey, Round);
181 | }
182 |
183 | void lastround(unsigned char* state, unsigned char* expandedkey, unsigned short int& Round) {
184 | substitution_byte(state);
185 | shift_rows(state);
186 | addroundkey(state, expandedkey, Round);
187 | }
188 |
189 | void AES_encrypt(unsigned char *state, unsigned char *key) {
190 | // First phase: addRoundKey call once
191 | unsigned short int Round = 0;
192 | addroundkey(state, key, Round);
193 |
194 | Round++;
195 | //Second phase: call genericround() 9 times
196 | for (unsigned short int i = 1; i<= 9 ; ++i) {
197 | genericround(state, key, Round);
198 | Round++;
199 | }
200 | // Third phase: call an incomplete round
201 | lastround(state,key,Round);
202 | //return state;
203 | }
204 |
205 | /*
206 | Commented out as zeropadding was included in the ECB_AES_encrypt function itself
207 |
208 | unsigned char* zeropadding(unsigned char *string, int block, int& adjusted_length) {
209 | // length_last_block is 0 if the length is a multiple of block and the length of the last block otberwise
210 |
211 | int length = size_string(string);
212 |
213 | int no_of_blocks = length / block + 1;
214 |
215 | int length_last_block = length % block;
216 | if (length_last_block == 0)
217 | return string;
218 | int pos = block * (no_of_blocks - 1) + length_last_block;
219 |
220 | for (int i = 0; i < block - length_last_block; ++i) {
221 | //pos = block * no_of_blocks;
222 | string[pos] = 0;
223 |
224 | ++pos;
225 | }
226 | string[pos] = '\0';
227 | length = pos;
228 | adjusted_length = pos;
229 |
230 | return string;
231 | }
232 | */
233 |
234 | unsigned char* ECB_AES_encrypt(unsigned char* plaintext, unsigned char* key, int& FINAL_LENGTH) {
235 | expand_key(key);
236 | int length = size_string(plaintext);
237 | int no_of_blocks;
238 | int length_last_block = length % block;
239 | if (length_last_block == 0)
240 | no_of_blocks = length / block;
241 | else
242 | no_of_blocks = length / block + 1;
243 |
244 | unsigned char* ciphertext; // = zeropadding(plaintext, block, FINAL_LENGTH );
245 |
246 | // ZERO PADDING STARTS
247 | int pos;
248 | if (length_last_block != 0) {
249 | pos = block * (no_of_blocks - 1) + length_last_block;
250 | for (int i = 0; i < block - length_last_block; ++i) {
251 | //pos = block * no_of_blocks;
252 | plaintext[pos] = 0;
253 |
254 | ++pos;
255 | }
256 | plaintext[pos] = '\0';
257 | FINAL_LENGTH = pos;
258 | }
259 |
260 | // ZERO PADDING ENDS
261 |
262 | ciphertext = plaintext;
263 | unsigned char *pointer_to_first_element_of_block;
264 | pointer_to_first_element_of_block = ciphertext;
265 | for (int i = 0; i < no_of_blocks; ++i) {
266 |
267 | AES_encrypt(pointer_to_first_element_of_block, key);
268 | pointer_to_first_element_of_block += block;
269 |
270 | }
271 |
272 | return ciphertext;
273 |
274 | }
275 | //_____________________ENCRYPT SECTION END_______________
276 | //_____________________DECRYPT SECTION START_____________
277 |
278 | void inverse_shiftrows(unsigned char *state)
279 | {
280 | unsigned char temp_state[17];
281 | unsigned short int loopcount=0,cipher_index;
282 | for(;loopcount<4;loopcount++)
283 | {
284 | cipher_index=loopcount*4;
285 | for(unsigned short int temp_index=(loopcount*4);temp_index<((loopcount*4)+4);temp_index++)
286 | {
287 | temp_state[temp_index]=state[cipher_index];
288 | cipher_index=cipher_index+13;
289 | if(cipher_index>16)
290 | cipher_index=cipher_index-16;
291 | }
292 | }
293 | for(unsigned short int i=0;i<16;i++)
294 | state[i]=temp_state[i];
295 | }
296 | void inverse_substitutionbytes(unsigned char *state)
297 | {
298 | unsigned short int row,col;
299 | for(unsigned int i=0;i<16;i++)
300 | {
301 | row=state[i] & 0xf0;
302 | row = row >> 4;
303 | col=state[i]&0x0f;
304 | state[i]=inv_sbox[16*row+col];
305 |
306 | }
307 | }
308 | unsigned char gmul(unsigned char a, unsigned char b) {
309 | unsigned char p = 0;
310 | unsigned char counter;
311 | unsigned char hi_bit_set;
312 | for(counter = 0; counter < 8; counter++) {
313 | if((b & 1) == 1)
314 | p ^= a;
315 | hi_bit_set = (a & 0x80);
316 | a <<= 1;
317 | if(hi_bit_set == 0x80)
318 | a ^= 0x1b;
319 | b >>= 1;
320 | }
321 | return p;
322 | }
323 | void inv_mix_column(unsigned char *r) {
324 | unsigned char a[4];
325 | unsigned char c;
326 | for(c=0;c<4;c++) {
327 | a[c] = r[c];
328 | }
329 | r[0] = gmul(a[0],14) ^ gmul(a[3],9) ^ gmul(a[2],13) ^ gmul(a[1],11);
330 | r[1] = gmul(a[1],14) ^ gmul(a[0],9) ^ gmul(a[3],13) ^ gmul(a[2],11);
331 | r[2] = gmul(a[2],14) ^ gmul(a[1],9) ^ gmul(a[0],13) ^ gmul(a[3],11);
332 | r[3] = gmul(a[3],14) ^ gmul(a[2],9) ^ gmul(a[1],13) ^ gmul(a[0],11);
333 | }
334 | void inverse_mix(unsigned char *state)
335 | {
336 | for(unsigned short int i=0;i<4;i++)
337 | inv_mix_column((state+i*4));
338 | }
339 | void decrypt_generic_round(unsigned char *state,unsigned char *key,unsigned short int& Round)
340 | {
341 | inverse_shiftrows(state);
342 | inverse_substitutionbytes(state);
343 | addroundkey(state,key,Round);
344 | inverse_mix(state);
345 | }
346 |
347 | void decrypt_lastround(unsigned char *state,unsigned char *key,unsigned short int& Round)
348 | {
349 | inverse_shiftrows(state);
350 | inverse_substitutionbytes(state);
351 | addroundkey(state,key,Round);
352 | }
353 | void AES_decrypt(unsigned char *ciphertext,unsigned char *key, int length)
354 | {
355 | unsigned short int Round = 10;
356 |
357 | addroundkey(ciphertext,key,Round);
358 | Round--;
359 | for(;Round>=1;Round--)
360 | decrypt_generic_round(ciphertext,key,Round);
361 | decrypt_lastround(ciphertext,key,Round);
362 |
363 | }
364 |
365 | unsigned char* ECB_AES_decrypt(unsigned char *ciphertext, unsigned char *key) {
366 | expand_key(key);
367 | int length = size_string(ciphertext);
368 | int length_last_block = length % block;
369 | int no_of_blocks;
370 | if (length_last_block == 0)
371 | no_of_blocks = length / block;
372 | else
373 | no_of_blocks = length / block + 1;
374 |
375 | unsigned char * pointer_to_first_element_of_block;
376 | pointer_to_first_element_of_block = ciphertext;
377 | for (int i = 0; i < no_of_blocks; ++i) {
378 |
379 | AES_decrypt(pointer_to_first_element_of_block, key, block);
380 | pointer_to_first_element_of_block += block;
381 |
382 |
383 | }
384 |
385 | return ciphertext;
386 | }
387 |
388 |
389 | char* int2string(long num) {
390 | char* str;
391 | int num_of_digits = (int) (log10(num)) + 1;
392 | int remainder;
393 | str = new char[num_of_digits + 1];
394 | str[num_of_digits] = '\0';
395 | for (int i = num_of_digits-1; i >= 0; --i) {
396 | remainder = num % 10;
397 | char rem_char = (char)(remainder + '0'); //ASCII value of '0' = 48
398 | str[i] = rem_char;
399 | num = num / 10;
400 | }
401 | return str;
402 | }
403 |
--------------------------------------------------------------------------------
/key_exchange_RSA/arduino/rsa.h:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
4 |
5 | This file is part of cryptographic-protocols-arduino-and-PC source code.
6 |
7 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with the software. If not, see .
19 |
20 | */
21 |
22 | #include "util.h"
23 |
24 | extern bool isprime(long n, int iterations);
25 |
26 | char* dec2bin(int n) {
27 | long r;
28 | int c = 0, i, b[100];
29 | while(n > 0)
30 | {
31 | r = n % 2 ;
32 | b[c] = r ;
33 | n = n / 2 ;
34 | c++ ;
35 | }
36 |
37 | char* str;
38 | for(i = 0 ; i >= c-1 ; i++) {
39 | str[c-i-1] = b[0] + 48;
40 | }
41 |
42 |
43 | return str;
44 | }
45 |
46 | long* string2ascii_int_list(char* string) {
47 | long* ascii_int_list;
48 | int length = strlen(string);
49 | ascii_int_list = new long [length];
50 | for (long i = 0; i < length; ++i) {
51 | ascii_int_list[i] = (int)(string[i]);
52 | ascii_int_list[i] = (long) (ascii_int_list[i]);
53 | Serial.write("ascii_int_list["); Serial.print(i);
54 | Serial.write("] = ");
55 | Serial.println(ascii_int_list[i]);
56 | }
57 |
58 | return ascii_int_list;
59 | }
60 |
61 | char* ascii_int_list2string(int* ascii_int_list) {
62 | char* string;
63 | int length = sizeof(ascii_int_list) / sizeof(ascii_int_list[0]);
64 | string = new char[length];
65 | for (int i = 0; i < length; ++i) {
66 | string[i] = (char) (ascii_int_list[i]);
67 | }
68 | return string;
69 | }
70 |
71 | long getprime(long lower, long upper) {
72 | long num;
73 | while(1) {
74 | num = random(lower,upper);
75 | if(isprime(num, 2))
76 | break;
77 | }
78 | return num;
79 | }
80 |
81 | long* rsa_encrypt(char* plaintext, long* key) {
82 | //long n = key[0];
83 | //long e = key[1];
84 | long* plaintext_list;
85 | long* ciphertext_list;
86 | int length = strlen(plaintext);
87 |
88 | plaintext_list = string2ascii_int_list(plaintext);
89 |
90 | ciphertext_list = new long[length];
91 | Serial.write("inside hell called rsa_encrypt\n");
92 |
93 | for (int i = 0; i < length; ++i) {
94 | Serial.write("plaintext_list["); Serial.print(i); Serial.print("] = "); Serial.println((plaintext_list[i]));
95 | ciphertext_list[i] = raiseto_mod(plaintext_list[i], key[1], key[0]);
96 | }
97 |
98 | return ciphertext_list;
99 |
100 | }
101 |
102 | long* rsa_decrypt(long* ciphertext_list, long* key, int length) {
103 | //long n = key[0];
104 | //long d = key[1];
105 | long* plaintext_list;
106 | //int length = sizeof(ciphertext_list)/sizeof(ciphertext_list[0]);
107 | plaintext_list = new long[length];
108 | for(int i = 0; i < length; ++i) {
109 | plaintext_list[i] = raiseto_mod(ciphertext_list[i], key[1], key[0]);
110 | }
111 | return plaintext_list;
112 | }
113 |
114 | /* The following functions can be used for generating a pair of RSA keys. */
115 |
116 | long gcd(long a, long b){
117 | // Returns greatest common denominator by using recursive Euclidean algorithm
118 | if (b == 0){
119 |
120 | return a;
121 | }
122 | return gcd(b, a % b);
123 | }
124 |
125 | long* extended_gcd(long a, long b){
126 | // Recursive extended Euclidean algorithm
127 | long *temp, *temp2;
128 | if (a==0){
129 | temp = new long[3];
130 | temp[0] = b; temp[1]= 0; temp[2]= 1;
131 |
132 |
133 | return temp;
134 | }
135 | else {
136 | temp = extended_gcd(b % a, a);
137 | temp2 = new long[3];
138 | temp2[0] = temp[0];
139 | temp2[1] = temp[2] - (long)(b/a)*temp[1];
140 | temp2[2] = temp[1];
141 |
142 |
143 | return temp2;
144 | }
145 | }
146 |
147 | long modinv(long a,long m){
148 | // Returns the inverse of a modulo m
149 | long *temp;
150 | temp = extended_gcd(a,m);
151 | long x = temp[1], g = temp[0]; // y = temp[2];
152 | if (g==1)
153 | return (x % m);
154 |
155 | }
156 |
157 | long getrelprime(long num){
158 | // Returns the smallest relative prime number to num
159 | for(long i=2; i< num; i++){
160 | if (gcd(i,num) == 1){
161 | return i;
162 | }
163 | }
164 | }
165 |
166 | //
167 | long* findkq(long n){
168 | // Find k and q such that n = (2**k)*q
169 | long result;
170 | result = n % (long)(2);
171 | long n_new = n-1;
172 | long k = 0;
173 | long q = 0;
174 | while (n_new) {
175 | n_new /= 2;
176 | k += 1;
177 | if (n_new % (2) != 0){
178 | q = n_new;
179 | break;
180 | }
181 | }
182 | long *temp;
183 | temp = new long[2];
184 | temp[0] = k;
185 | temp[1] = q;
186 | return temp ;
187 | }
188 |
189 | int test3(long n, long k, long q){
190 |
191 | long a = random(2, n-2); // long min = 2, max = n-2; a=rand() % (max - min + 1) + min;
192 |
193 | if (raiseto_mod(a, q, n) == 1){
194 |
195 |
196 |
197 | return 0; // "inconclusive";
198 | }
199 | for (long j = 0; j < k; j++){
200 | if (raiseto_mod(a,power(2,j),n) == n-1)
201 | return 0; //"inconclusive";
202 | }
203 | return 1; //"composite";
204 | }
205 |
206 | bool isprime(long n, int iterations = 1){
207 | // Primality test using Miller-Rabin algorithm
208 | if (n % 2 == 0){
209 |
210 | return 0;
211 | }
212 | long *t1 = findkq(n);
213 | //long k = t1[0];
214 | //long q = t1[1];
215 |
216 | for (int i = 0; i < iterations; i++){
217 | int result = test3(n,t1[0],t1[1]);
218 | if (result == 1) {
219 | return 0;
220 | }
221 | }
222 |
223 | return 1;
224 | }
225 |
226 | long* generate_keys(long lower, long upper) {
227 | long n, phi_n, p, q, e, d;
228 |
229 | while(1) {
230 |
231 | p = getprime(lower, upper);
232 | q = getprime(lower, upper);
233 | n = p * q;
234 | phi_n = (p-1) * (q-1);
235 | e = getrelprime(phi_n);
236 | d = modinv(e, phi_n);
237 | if (d>= 2)
238 | break;
239 | }
240 |
241 | long *Key;
242 | Key = new long[3];
243 | Key[0] = n; Key[1] = e; Key[2] = d;
244 |
245 | // Public Key is {n,e}
246 | // Private Key is {n, d}
247 |
248 | return Key;
249 | }
250 |
--------------------------------------------------------------------------------
/key_exchange_RSA/arduino/rsa_aes_protocol.ino:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
4 |
5 | This file is part of cryptographic-protocols-arduino-and-PC source code.
6 |
7 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with the software. If not, see .
19 |
20 | */
21 |
22 | long RSA_Own_Private_Key[2] = {5723, 3341};
23 | const int MOST_NO_OF_DIGITS = 10;
24 |
25 | #include "rsa.h"
26 | #include "aes.h"
27 | #include
28 | #include
29 |
30 | LiquidCrystal lcd(8,9,10,11,12,13);
31 |
32 | void lcd_print_string(char *text, LiquidCrystal lcd, int delay_time = 2000, int lcd_size = 32) {
33 | int length = size_string(text);
34 | int lcd_half_size = lcd_size / 2;
35 | int no_of_lines = length / lcd_half_size;
36 | int length_last_line = length % lcd_half_size;
37 | lcd.clear();
38 |
39 | for (int i = 0; i < no_of_lines + 1; ++i) {
40 | if (i % 2 == 0)
41 | lcd.setCursor(0,0);
42 | else
43 | lcd.setCursor(0,1);
44 | lcd.print(text + i * lcd_half_size);
45 | if (i % 2) {
46 | delay(delay_time);
47 | if (i != no_of_lines)
48 | lcd.clear();
49 | }
50 | }
51 | }
52 |
53 | void lcd_print_string(unsigned char *text, LiquidCrystal lcd, int delay_time = 2000, int lcd_size = 32) {
54 | int length = size_string(text);
55 | int lcd_half_size = lcd_size / 2;
56 | int no_of_lines = length / lcd_half_size;
57 | int length_last_line = length % lcd_half_size;
58 | lcd.clear();
59 |
60 | for (int i = 0; i < no_of_lines + 1; ++i) {
61 | if (i % 2 == 0)
62 | lcd.setCursor(0,0);
63 | else
64 | lcd.setCursor(0,1);
65 | lcd.print((char *)(text + i * lcd_half_size));
66 | if (i % 2) {
67 | delay(delay_time);
68 | if (i != no_of_lines)
69 | lcd.clear();
70 | }
71 | }
72 | }
73 |
74 | void cycle_arduino_to_PC(long *AES_Key) {
75 | unsigned char ptext[100] = "You with your switching sides and your walk-by lies and humiliation", *ctext;
76 |
77 | int FINAL_LENGTH = size_string(ptext);
78 | unsigned char AES_Key_uc[178];
79 | AES_Key_uc[176]='\0';
80 | for (int i = 0; i < 16; ++i) {
81 | AES_Key_uc[i] = AES_Key[i];
82 | }
83 |
84 | ctext = ECB_AES_encrypt(ptext, AES_Key_uc, FINAL_LENGTH);
85 |
86 | Serial.println(to_string(FINAL_LENGTH));
87 |
88 | for (int i = 0; i < FINAL_LENGTH; ++i) {
89 | Serial.println(ctext[i]);
90 | }
91 | }
92 |
93 | void cycle_PC_to_arduino(long* AES_Key) {
94 | unsigned char ctext[100], *ptext, AES_Key_uc[178];
95 | AES_Key_uc[176] = '\0';
96 | int length;
97 | char length_string[MOST_NO_OF_DIGITS + 1];
98 | Serial.readBytes(length_string, MOST_NO_OF_DIGITS);
99 | length_string[MOST_NO_OF_DIGITS] = '\0';
100 | length = to_num(length_string);
101 | int i;
102 | for (i = 0; i < length; ++i) {
103 | char num_str[4];
104 | Serial.readBytes(num_str, 3);
105 | num_str[3] = '\0';
106 | ctext[i] = to_num(num_str);
107 | Serial.println(ctext[i]);
108 | }
109 | ctext[i] = '\0';
110 |
111 | for (int j = 0; j < 16; ++j) {
112 | AES_Key_uc[j] = AES_Key[j];
113 | }
114 |
115 | ptext = ECB_AES_decrypt(ctext, AES_Key_uc);
116 | lcd.clear();
117 | lcd.setCursor(0,0);
118 | lcd_print_string(ptext, lcd);
119 |
120 | delay(10000);
121 | }
122 |
123 | long* get_AES_Key() {
124 | long *AES_Key_Encrypted;
125 | long *AES_Key;
126 | AES_Key_Encrypted = new long[17];
127 | AES_Key_Encrypted[16] = '\0';
128 | for (int i = 0; i < 16; ++i) {
129 | char num_str[MOST_NO_OF_DIGITS + 1];
130 | num_str[MOST_NO_OF_DIGITS] = '\0';
131 | Serial.readBytes(num_str, MOST_NO_OF_DIGITS);
132 | AES_Key_Encrypted[i] = to_num(num_str);
133 | }
134 | AES_Key = rsa_decrypt(AES_Key_Encrypted, RSA_Own_Private_Key, 16);
135 | delete[] AES_Key_Encrypted;
136 | return AES_Key;
137 | }
138 |
139 | void setup() {
140 | lcd.begin(16,2);
141 | lcd.clear();
142 |
143 | Serial.begin(115200);
144 | Serial.write("TkSOzZU9Wd*!byau$U2a\n");
145 | long *AES_Key;
146 |
147 |
148 |
149 | char mode[14];
150 | mode[13] = '\0';
151 | Serial.readBytes(mode, 13);
152 | if (strcmp(mode, "PC_TO_ARDUINO") == 0) {
153 | lcd.setCursor(0,1);
154 | lcd.print("PC TO ARDUINO");
155 | AES_Key = get_AES_Key();
156 |
157 | char indication[MOST_NO_OF_DIGITS + 1];
158 | Serial.readBytes(indication, MOST_NO_OF_DIGITS);
159 | indication[MOST_NO_OF_DIGITS] = '\0';
160 | while (strcmp(indication, "2&g&xb3leL") != 0) {
161 | AES_Key = get_AES_Key();
162 | Serial.readBytes(indication, MOST_NO_OF_DIGITS);
163 | }
164 | cycle_PC_to_arduino(AES_Key);
165 | }
166 | else if (strcmp(mode, "ARDUINO_TO_PC") == 0) {
167 | lcd.setCursor(0,0);
168 | lcd.print("ARDUINO TO PC");
169 | AES_Key = get_AES_Key();
170 | cycle_arduino_to_PC(AES_Key);
171 | }
172 | else {
173 | lcd.setCursor(0,0);
174 | lcd.print("NO MODE SELECTED");
175 | delay(10000);
176 | }
177 | }
178 |
179 | void loop() {
180 |
181 |
182 | }
183 |
--------------------------------------------------------------------------------
/key_exchange_RSA/arduino/util.h:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | Copyright (C) 2013 Arpit Chauhan, Inderjit Sidhu and Archit Pandey
4 |
5 | This file is part of cryptographic-protocols-arduino-and-PC source code.
6 |
7 | cryptographic-protocols-arduino-and-PC is free software: you can redistribute it and/or modify
8 | it under the terms of the GNU General Public License as published by
9 | the Free Software Foundation, either version 3 of the License, or
10 | (at your option) any later version.
11 |
12 | cryptographic-protocols-arduino-and-PC is distributed in the hope that it will be useful,
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 | GNU General Public License for more details.
16 |
17 | You should have received a copy of the GNU General Public License
18 | along with the software. If not, see .
19 |
20 | */
21 |
22 | #include
23 |
24 | long power(long a, long b) {
25 | long temp = 1;
26 | for (long i = 0; i < b; ++i) {
27 | temp = temp * a;
28 | }
29 | return temp;
30 | }
31 |
32 | int size_string (unsigned char* string )
33 | { int i;
34 | for (i = 0; string[i] != '\0'; ++i);
35 | return i;
36 | }
37 |
38 | int size_string (char* string )
39 | { int i;
40 | for (i = 0; string[i] != '\0'; ++i);
41 | return i;
42 | }
43 |
44 |
45 | char* to_string(long num) {
46 | char* str;
47 | if (num == 0) {
48 | str = new char[2];
49 | str[0] = '0';
50 | str[1] = '\0';
51 | return str;
52 | }
53 |
54 | long num_of_digits = (long) (log10(num)) + 1;
55 | long rem;
56 |
57 | str = new char[num_of_digits + 1];
58 |
59 | str[num_of_digits] = '\0';
60 | for (long i = num_of_digits- 1; i >= 0; --i) {
61 |
62 | rem = num % ((long)(10));
63 |
64 | char rem_char = (char)(rem + 48); // '0'
65 | str[i] = rem_char;
66 | num = num / 10;
67 | }
68 |
69 | return str;
70 | }
71 |
72 | long to_num(char* str) {
73 | int len = size_string(str);
74 | long num=0;
75 | int number_is_zero = 1;
76 | for (int i = 0; i < len; ++i) {
77 | if (str[i] != '0') {
78 | number_is_zero = 0;
79 | break;
80 | }
81 | }
82 | if (number_is_zero == 1)
83 | return 0;
84 | for (int i = 0; i < len; ++i) {
85 | if (str[i] != '0')
86 | num += power(10, len - i - 1) * ( str[i] - '0' );
87 | }
88 | return num;
89 | }
90 |
91 | void Serial_print_array(long* arr, long length, char *name) {
92 | for (int i = 0; i < length; ++i) {
93 | Serial.write(name);
94 | Serial.write("[");
95 | Serial.write(to_string(i));
96 | Serial.write("] = ");
97 | Serial.println(to_string(arr[i]));
98 | }
99 | }
100 |
101 | void Serial_print_array(unsigned char* str, long length, char *name) {
102 | for (int i = 0; i < length; ++i) {
103 | Serial.write(name);
104 | Serial.write("[");
105 | Serial.write(to_string(i));
106 | Serial.write("] = ");
107 | Serial.println(str[i]);
108 | }
109 | }
110 |
111 | long raiseto_mod(long a, long b, long c) {
112 | long temp = 1;
113 | for (long i = 0; i < b; ++i) {
114 | temp = (temp * a) % c;
115 | }
116 | return temp;
117 | }
118 |
--------------------------------------------------------------------------------