├── LICENSE ├── README.md ├── hardware ├── GM803-manual-v1.1.pdf ├── frontplate │ ├── BOM_frontplate.xlsx │ ├── Gerber_frontplate.zip │ ├── PickAndPlace_frontplate.xlsx │ └── Schematic_frontplate.pdf ├── mainplate │ ├── BOM_mainplate.xlsx │ ├── Gerber_mainplate.zip │ ├── PickAndPlace_mainplate.xlsx │ └── Schematic_mainplate.pdf └── spacer.stl ├── images ├── basic11.png ├── basic111.png ├── clone.png ├── clone2.png ├── construction11.png ├── construction22.png ├── construction33.png ├── construction44.png ├── construction55.png ├── construction66.png ├── first.txt ├── qrcode1.png ├── readmessage1.png ├── readmessage3.gif ├── regular.png ├── reset.png ├── serialoutput.png ├── setup.png ├── sharemyid1.png └── writemessage.gif └── software └── qryptr.ino /LICENSE: -------------------------------------------------------------------------------- 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 | # qryptr - airgapped ecc text messenger 2 | 3 | Are you a smartphone user worried about [spyware](https://en.wikipedia.org/wiki/Spyware), [advanced actors](https://en.wikipedia.org/wiki/Advanced_persistent_threat), [backdoors](https://en.wikipedia.org/wiki/Backdoor_(computing)), [zero-days](https://en.wikipedia.org/wiki/Zero-day_vulnerability) or [side-channel attacks](https://en.wikipedia.org/wiki/Side-channel_attack)? These routinely bypass end-to-end encryption through keyloggers, screen capture and compromised keys. Smartphones are part of complex ecosystems with dozens of hardware and software components and remain vulnerable despite vendor and political efforts. 4 | 5 | We introduce a simple, offline, airgapped device to counter such threats. 6 | 7 | Plain text messages are ECC encrypted and displayed as QR codes. These QR codes can be photographed and shared using your smartphone. This method offers additional endpoint security as plaintext and cryptographic keys are physically seperated from your smartphone. 8 | 9 | This repository contains all hardware and software to create such a device, which allows text messages of up to 299 characters. 10 | 11 | 12 | 13 | 14 | # How it works 15 | The device has cryptographic software, a keyboard, camera, screen and is based on the RP2040 microcontroller. 16 | 17 | Each user has a device. Upon receiving it, the user can generate his/her ECC keypair. The user's ECC keypair uniquely identifies him/her, that is why it is referred to as the user ID. 18 | 19 | The public key of the user ID can be displayed as a QR code. Another user can add that user ID scanning it. This is preferably done in-person, not remotely, to prevent man-in-the-middle attacks. 20 | 21 | After two users have added each others' ID's, they can write text messages which are ECC encrypted, base64 encoded and displayed as QR codes. 22 | 23 | Using their smartphones, users can photograph/share these QR-codes with their preferred messaging app, such as Signal, Whatsapp, Telegram, Viber, or even email or print the QR codes. 24 | 25 | The recipient can scan the QR code with his/her device, which will read, decode, decrypt and display the text message. The recommended read distance is 10 cm between smartphone screen and camera. The letter "Z" is marked on the frontpanel to help a user center the qr code in front of the camera. 26 | 27 | 28 | ### Share and add IDs 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | ### Write and read messages 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | # Use cases 54 | -Sharing sensitive information between journalists, lawyers, political actors or civil servants. 55 | 56 | -Sharing passwords between system administrators. 57 | 58 | -Sharing passwords for crypto wallets. 59 | 60 | -Sharing key material or passwords for HSM procedures. 61 | 62 | -Thwart spyware or advanced threats. 63 | 64 | # Implementation 65 | ## hardware 66 | ### Printed Circuit Boards (PCBs) 67 | The device consists of 2 printed circuit boards (PCBs): a mainplate and frontplate with keys. We chose a microcontroller platform to minimize platform complexity and dependencies: the RP2040. Design files are available in the /hardware folder. With the Gerber, BOM and pick-and-place files, you can order directly from jlcpcb.com or other PCB manufacturers. 68 | 69 | Alternatively, you can view or clone these at the following urls to make adjustments. 70 | 71 | https://oshwlab.com/thomas255/mainplate-public (click "clone") 72 | 73 | https://oshwlab.com/thomas255/frontplate-public (click "clone") 74 | 75 | 76 | ### Camera 77 | 78 | QR codes are read using a hardware camera, the GM803, available on aliexpress. Get the GM803-S version with short focal range. This camera will read the QR code towards the serial interface of the RP2040. 79 | 80 | ### Display 81 | 82 | We are using the Sharp LS027B7DH01 display, available on aliexpress. It is flat, requires few components, has a high resolution and low power requirements. Other u8g2 supported displays could be used as well with some adjustments. 83 | 84 | ### Other parts 85 | 86 | Some parts needs to be ordered seperately from [lscs.com](https://www.lcsc.com/): 87 | 88 | -LCSC#: C2911889 Clamshell 18P Bottom Contact Surface Mount FFC connector. We order this part seperately to solder it manually so that we can use one sided PCB assembly at JLCPCB to save costs. Alternatively, two sided assembly for the frontplate can be used at JLCPCB, in which case this part does not need to be ordered seperately. 89 | 90 | -LCSC#: C2857713 18P Opposite Side 5cm P=0.5mm flat flexible cable to connect the frontplate (keyboard) with the mainplate 91 | 92 | -LCSC#: C5151979 12P Opposite Side 5cm P=0.5mm flat flexible cable to connect the GM-803 camera to the mainplate 93 | 94 | -M2 nuts and M2 bolts of 3mm length. 95 | 96 | -A plastic spacer between the mainplate and frontplate, for mechanical stability and assembly, which can be 3d printed with the provided ./hardware/spacer.stl file. 97 | 98 | -A 302040 lipo battery: https://www.aliexpress.com/item/33009055815.html 99 | 100 | 101 | 102 | ## software 103 | We use the Arduino IDE. 104 | 105 | Go to the board manager and install Arduino mbed OS RP2040 Boards (3.5.4). This should set target_platform=mbed_rp2040 in preferences.txt for the arduino IDE. 106 | 107 | We use the Arduino mbed_rp2040 target platform, because we want to use the Sharp LS027B7DH01 display, and the earle philhower core does not allow changing the HW SPI pin assignment. We want to use different pins for driving the SPI display in the u8g2 display constructor, because the original pin assignment is very slow. 108 | 109 | Using the library manager in the Arduino IDE, install the following libraries: 110 | 111 | -QRCode by Richard Moore (https://github.com/ricmoo/qrcode/) 112 | 113 | 114 | -U8G2 by Oliver Kraus (https://github.com/olikraus/u8g2) 115 | 116 | 117 | -Crypto by Rhys Weatherley and Brandon Wiley (https://github.com/OperatorFoundation/Crypto) 118 | 119 | 120 | To upload the software, connect with PC through USB, turn on device, upload ./software/qryptr.ino through the arduino IDE. If that does not work, there is a reset button. Turn off the device, hold the reset button, connect with PC, release the reset button. Then upload the arduino sketch through the arduino IDE. 121 | 122 | Once uploaded, navigate the menu with the arrow buttons (left, right, up, down). There is no enter key. To select an option or continue, use the right arrow. You can always go back with left arrow or interrupt reading by pressing any key. 123 | 124 | 125 | # Todos 126 | ## hardware 127 | -Add resistor divider to measure the battery voltage 128 | 129 | -Expose SWD pins on PCB to enable loading the software through SWD pins. This will allow us to disconnect the datalines from the USB port and prevent an attack vector. 130 | 131 | 132 | ## software 133 | -Replace Arduino Crypto library with Monocypher (https://github.com/LoupVaillant/Monocypher). Monocypher is more widely used and more regularly updated. 134 | 135 | -Create a software procedure to turn the camera on/off to save power. 136 | 137 | -Implement soft poweroff after timeout. 138 | 139 | -Implement symmetric encryption on the long-term private key of the user. This will require a password prompt for decrypting messages. 140 | 141 | -Enable multiple personal ID's (cryptographic keys) 142 | 143 | -Add start and end tags for messages and keys, so that it is clear whether a valid messages/key was received. This will prevent bad reads from crashing the device as well. 144 | 145 | -Use camera without character encoding (raw bytes). 146 | 147 | -Write special characters (diacritics) with ctrl button. 148 | 149 | -On screen arrows to make navigation more intuitive. 150 | 151 | # FAQ 152 | ## hardware 153 | Q: Could we run the whole device on 3.3V? 154 | 155 | A: The Sharp LS027B7DH01 display requires 5V according to the specifications, although it seems to work on 3.3V as well. Possibly, the charge boost circuit could be left out if the display is used at 3.3V. 156 | 157 | Q: Could we create a single PCB instead of a backplate and frontplate? 158 | 159 | A: Yes, it could be done, although a singular PCB would need double-sided PCB assembly which is more expensive for small quantities @ JLCPCB. It likely would require a 4 layer PCB instead of 2 layer PCBs. Also, a seperate backplate would need to be introduced. 160 | 161 | Q: How about using an epaper display instead of the Sharp display? 162 | 163 | A: It has been considered, but the Sharp display has a higher refresh rate and is very flat and easy to integrate with just a couple of capacitors. The Sharp display is easy to use with the U8G2 library. There are other advantages to using epaper displays such as low glare which would make it easier to photograph the screen, so this could still be a good option. 164 | 165 | ## software 166 | Q: How is encryption done? 167 | 168 | A: Using the arduino Crypto library, with 32 byte ECC curve25519 keys, using ChaChaPoly cipher. Documented in the code, according to the procedure described here: 169 | 170 | https://crypto.stackexchange.com/questions/101420/eccdh-direct-or-with-temporary-ecc-keypairs 171 | 172 | 173 | # Assembly 174 | Place the battery and solder the wires as shown below to the pads. 175 | 176 | 177 | 178 | 179 | Place the 3d printed spacer as shown below, insert the m2 nuts with some glue. 180 | 181 | 182 | 183 | 184 | Connect the 18 pin keyboard connector as shown below, place the GM-803 camera module and connect with the 12 pin ffc connector cable. 185 | 186 | 187 | 188 | 189 | Place the screen as shown below. 190 | 191 | 192 | 193 | 194 | Solder the frontplate 18 pin connector (or order an assembled frontplate from JLCPCB), and connect it. Soldering this 0.5mm pitch connector can be a bit tricky. 195 | 196 | 197 | 198 | 199 | Use the m2 3mm bolts to screw the frontplate down. 200 | 201 | 202 | 203 | ## Setup GM803 camera 204 | 205 | After charging, a green led will light up. Turn on the device (PWR top right push button). You have to enable serial output of the GM803 camera. To do so, scan the following QR codes. 206 | 207 | ### Reset (GM803 factory default) 208 | 209 | 210 | 211 | ### Enable serial output 212 | 213 | 214 | 215 | 216 | 217 | -------------------------------------------------------------------------------- /hardware/GM803-manual-v1.1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/hardware/GM803-manual-v1.1.pdf -------------------------------------------------------------------------------- /hardware/frontplate/BOM_frontplate.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/hardware/frontplate/BOM_frontplate.xlsx -------------------------------------------------------------------------------- /hardware/frontplate/Gerber_frontplate.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/hardware/frontplate/Gerber_frontplate.zip -------------------------------------------------------------------------------- /hardware/frontplate/PickAndPlace_frontplate.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/hardware/frontplate/PickAndPlace_frontplate.xlsx -------------------------------------------------------------------------------- /hardware/frontplate/Schematic_frontplate.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/hardware/frontplate/Schematic_frontplate.pdf -------------------------------------------------------------------------------- /hardware/mainplate/BOM_mainplate.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/hardware/mainplate/BOM_mainplate.xlsx -------------------------------------------------------------------------------- /hardware/mainplate/Gerber_mainplate.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/hardware/mainplate/Gerber_mainplate.zip -------------------------------------------------------------------------------- /hardware/mainplate/PickAndPlace_mainplate.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/hardware/mainplate/PickAndPlace_mainplate.xlsx -------------------------------------------------------------------------------- /hardware/mainplate/Schematic_mainplate.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/hardware/mainplate/Schematic_mainplate.pdf -------------------------------------------------------------------------------- /hardware/spacer.stl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/hardware/spacer.stl -------------------------------------------------------------------------------- /images/basic11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/basic11.png -------------------------------------------------------------------------------- /images/basic111.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/basic111.png -------------------------------------------------------------------------------- /images/clone.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/clone.png -------------------------------------------------------------------------------- /images/clone2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/clone2.png -------------------------------------------------------------------------------- /images/construction11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/construction11.png -------------------------------------------------------------------------------- /images/construction22.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/construction22.png -------------------------------------------------------------------------------- /images/construction33.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/construction33.png -------------------------------------------------------------------------------- /images/construction44.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/construction44.png -------------------------------------------------------------------------------- /images/construction55.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/construction55.png -------------------------------------------------------------------------------- /images/construction66.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/construction66.png -------------------------------------------------------------------------------- /images/first.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /images/qrcode1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/qrcode1.png -------------------------------------------------------------------------------- /images/readmessage1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/readmessage1.png -------------------------------------------------------------------------------- /images/readmessage3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/readmessage3.gif -------------------------------------------------------------------------------- /images/regular.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/regular.png -------------------------------------------------------------------------------- /images/reset.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/reset.png -------------------------------------------------------------------------------- /images/serialoutput.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/serialoutput.png -------------------------------------------------------------------------------- /images/setup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/setup.png -------------------------------------------------------------------------------- /images/sharemyid1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/sharemyid1.png -------------------------------------------------------------------------------- /images/writemessage.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gappuser/qryptr/055129a3f9b9850583210e3a16659c79110a534d/images/writemessage.gif -------------------------------------------------------------------------------- /software/qryptr.ino: -------------------------------------------------------------------------------- 1 | //Copyright (C) 2024 Thomas Schellekens 2 | 3 | //This program is free software: you can redistribute it and/or modify 4 | //it under the terms of the GNU General Public License as published by 5 | //the Free Software Foundation, either version 3 of the License, or 6 | //(at your option) any later version. 7 | 8 | //This program is distributed in the hope that it will be useful, 9 | //but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | //GNU General Public License for more details. 12 | 13 | //You should have received a copy of the GNU General Public License 14 | //along with this program. If not, see . 15 | 16 | 17 | //TODOS 18 | //better prompts 19 | //on screen arrows to indicate back/forth 20 | //voltage readings of battery 21 | //write special characters with control button 22 | //multiple IDs 23 | //use camera without character encoding. Currently, the GM-803 will always apply character encoding (UTF-8) to any byte. This means we need to apply base64 encoding before encoding into a QR code. 24 | //Section 7.8 of the GM-803 manual seems to suggest there is a way to turn off character encoding, but this is not clear yet. We'd prefer to send raw bytes instead of base64-encoded raw bytes because it would allow for longer messages 25 | //add option for symmetric encryption of private key (password prompt to make sure the private key is not kept in plain form on the memory chip) 26 | //add and tags for messages and keys, so that upon decryption, it is clear whether a valid message was received or whether a error should be displayed 27 | 28 | 29 | //Pin assignment 30 | //Keyboard 31 | //PICO_GP2_ROW1 32 | //PICO_GP3_ROW2 33 | //PICO_GP4_ROW3 34 | //PICO_GP5_ROW4 35 | //PICO_GP6_ROW5 36 | //PICO_GP7_COL1 37 | //PICO_GP8_COL2 38 | //PICO_GP9_COL3 39 | //PICO_GP10_COL4 40 | //PICO_GP11_COL5 41 | //PICO_GP12_COL6 42 | //PICO_GP13_COL7 43 | //PICO_GP14_COL8 44 | //PICO_GP15_COL9 45 | //PICO_GP16_COL10 46 | //Display 47 | //PICO_GP17_DISP_CS_3 48 | //PICO_GP18_DISP_SCL_1 49 | //PICO_GP19_DISP_MOSI_2 50 | //GP_20 GPIOSHUTDOWN (todo for soft shutdown/timeout shutdown) 51 | //PICO_GP21_SHIFT 52 | //PICO_GP22_CTRL 53 | //GP_23 unused 54 | //GP_24 unused 55 | //GP25 PI_LED for testing purposes 56 | //GP28 ADC2 random input for ring oscillator (rosc) 57 | //GP29 ADC3 10kOhm to battery for voltage indicator. Needs a resistor voltage divider still. 58 | 59 | 60 | //CHARACTER ENCODING 61 | //GM803 will always apply a character encoding, will not send pure bytes from QR code. So ecc keys need to be encoded with base64 (33% overhead). 62 | //the terminal character is not an alphanumeric character. which causes ricmoo/qrcode to encode as BYTES, with all limitations that come along with that.. 63 | //strlen(), which is used by ricmoo/qrcode, only works if a null terminator is present. 64 | //but that null terminator will be interpreted as a byte, which causes ricmoo/qrcode to never switch to alphanumeric mode.. 65 | //so, you have to use base64 encoding. 66 | 67 | 68 | //FLASH WRITE STUFF 69 | //always need to erase first. 70 | //erase minimum is 4096 bytes 71 | //write is 256 bytes minium. But can you only program bytes from a certain sector that was erased. 72 | //have one fixed location for pub and private key (sector -2 and -3 before sector -200) 73 | //https://www.makermatrix.com/blog/read-and-write-data-with-the-pi-pico-onboard-flash/ 74 | //we save contacts on individual sectors, 200 sectors from the end of the available memory. So we can have 200 contacts. 75 | 76 | //Cryptographic procedures follow this structure: 77 | //https://crypto.stackexchange.com/questions/101420/eccdh-direct-or-with-temporary-ecc-keypairs 78 | 79 | extern "C" { 80 | #include 81 | #include 82 | }; 83 | 84 | #include 85 | #include 86 | #include 87 | #include 88 | #include 89 | #include 90 | #include 91 | #include 92 | #include 93 | #include 94 | 95 | 96 | //Cryptography libraries from Rhys Weatherly and Brandon Wiley 97 | //https://www.arduino.cc/reference/en/libraries/crypto/ 98 | #include 99 | #include 100 | #include 101 | #include 102 | #include 103 | #include 104 | #include 105 | #include 106 | 107 | 108 | #define MAX_PLAINTEXT_LEN 332 109 | #define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE) 110 | 111 | //old 128x64 st7920 lcd, someone may still want to try this as an alternative display 112 | //U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* CS=*/ 10, /* reset=*/ 8); 113 | 114 | //Works only with the official rp2040 mbed arduino core, not with the earle philhower core, because you cannot change the HW SPI pin assignment. And the original pin assignment is very slow. 115 | //u8g2 sendBuffer takes about 196ms, found no room for improvement. 116 | U8G2_LS027B7DH01_400X240_F_4W_HW_SPI u8g2(U8G2_R0, 17, U8X8_PIN_NONE); 117 | 118 | 119 | //UART variables, for use with the GM-803 camera 120 | #define UART0_ID uart0 121 | #define UART0_BAUD_RATE 9600 122 | #define DATA_BITS 8 123 | #define STOP_BITS 1 124 | #define PARITY UART_PARITY_NONE 125 | 126 | //GPIO pins 12, 14, 15 are hardware SPI pins based on mbed2040 library (other rp2040 arduino implementation could only use software SPI pins for display initialization) 127 | int pinrow1 = 2; 128 | int pinrow2 = 3; 129 | int pinrow3 = 4; 130 | int pinrow4 = 5; 131 | int pinrow5 = 6; 132 | int pincolumn1 = 7; 133 | int pincolumn2 = 8; 134 | int pincolumn3 = 9; 135 | int pincolumn4 = 10; 136 | int pincolumn5 = 11; 137 | int pincolumn6 = 12; 138 | int pincolumn7 = 13; 139 | int pincolumn8 = 14; 140 | int pincolumn9 = 15; 141 | int pincolumn10 = 16; 142 | int pinshift = 21; 143 | int pincontrol= 22; 144 | 145 | int rowPins[5] = {pinrow1, pinrow2, pinrow3, pinrow4, pinrow5}; 146 | int columnPins[10] = {pincolumn1, pincolumn2, pincolumn3, pincolumn4, pincolumn5, pincolumn6, pincolumn7, pincolumn8, pincolumn9, pincolumn10}; 147 | int debouncetime = 40; //40ms //useless since rendering screen takes 150ms. 148 | 149 | 150 | //buffer required by main loop, needs to be global 151 | //char mainloopbuf[10]; this is too little. it crashes. 152 | char mainloopbuf[100]; 153 | int mainloopbufpos = 0; 154 | int prevmainloopbufpos = 0; 155 | int * mainloopbufpospointer = &mainloopbufpos; 156 | int * prevmainloopbufpospointer = &prevmainloopbufpos; 157 | 158 | long lastdebouncetime[5][22] = {0}; 159 | int lastinputstate[5][22] = {1}; 160 | 161 | bool rendermainagain = true; 162 | 163 | //these are global for now and used by prompt, selectContact, (main) loop. Not pretty but practical for now. 164 | int menustate = 0; 165 | int prevmenustate = 0; 166 | int menuhighlight = 0; 167 | int state = 0; 168 | 169 | //for enabling scrolling 170 | int startpositionarrowed = 0; //for scrolling through the page of writing a message. the starting line, taking into account up and down arrows. 171 | int uparrows = 0; //keep track of number of up arrows pressed 172 | int downarrows = 0; //keep track of number of down arrows pressed 173 | 174 | //ORIGINAL SETUP 19 height 175 | //u8g2.setFont(u8g2_font_profont29_mf); 176 | //int menurowcount = 5; 177 | //int screenwidth = 400; 178 | //int screenheight = 240; 179 | //int menuitemboxy = 40; 180 | //int menuitemboxytextoffset = 30; 181 | //int headermargin = 8; 182 | //int menuitemboxxtextoffset = 2; 183 | 184 | //SMALLER SETUP 14 pixel height 185 | //u8g2.setFont(u8g2_font_profont22_mf); 186 | int menurowcount = 6; 187 | int screenwidth = 400; 188 | int screenheight = 240; 189 | int menuitemboxy = 34; 190 | int menuitemboxytextoffset = 24; 191 | int headermargin = 8; 192 | int menuitemboxxtextoffset = 2; 193 | int maxcharacters = 32; //maximum characters on single line given this font size. 194 | 195 | 196 | //u8g2.setFont(u8g2_font_profont17_mf); //11 pixel height 197 | //u8g2.setFont(u8g2_font_profont15_mf); //9 pixel height 198 | 199 | int mainmenuitemstotal = 7; //including 0, total number of items in mainmenustrings 200 | char * mainmenustrings[] = {"Generate new ID", "Share my ID", "Add contact ID", "Write message", "Read message", "Delete contact", "Replace contact", "Reset"}; 201 | 202 | bool displaydecryptmessage = false; 203 | bool globalbackspace = false; 204 | bool firstrender = true; 205 | 206 | //We will use int char values 17, 18, 19, 20 for up, right, down, left 207 | //B is backspace 208 | char keyboardlowercase[5][10] = { { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' }, 209 | { 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p' }, 210 | { 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'B' }, 211 | { 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '?'}, 212 | { '0', '0', '0', ' ', '/', '_', 20, 17, 19, 18}}; 213 | 214 | char keyboarduppercase[5][10] = { { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')' }, 215 | { 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P' }, 216 | { 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'B' }, 217 | { 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '/'}, 218 | { '0', '0', '0', ' ', '|', '_', 20, 17, 19, 18}}; 219 | 220 | struct TestVector 221 | { 222 | const char *name; 223 | uint8_t key[32]; 224 | uint8_t plaintext[MAX_PLAINTEXT_LEN]; 225 | uint8_t ciphertext[MAX_PLAINTEXT_LEN]; 226 | uint8_t authdata[16]; 227 | uint8_t iv[16]; 228 | uint8_t tag[16]; 229 | size_t authsize; 230 | size_t datasize; 231 | size_t tagsize; 232 | size_t ivsize; 233 | }; 234 | 235 | 236 | 237 | void setup_uart(){ 238 | uart_init(UART0_ID, UART0_BAUD_RATE); 239 | gpio_set_function(0, GPIO_FUNC_UART); 240 | gpio_set_function(1, GPIO_FUNC_UART); 241 | 242 | // Set UART flow control CTS/RTS, we don't want these, so turn them off (for GM-803) 243 | uart_set_hw_flow(UART0_ID, false, false); 244 | 245 | // Set our data format, as required by GM-803 camera 246 | uart_set_format(UART0_ID, DATA_BITS, STOP_BITS, PARITY); 247 | } 248 | 249 | void setupInput(){ 250 | 251 | pinMode(2, OUTPUT); 252 | pinMode(3, OUTPUT); // Set the button as an input 253 | pinMode(4, OUTPUT); // Set the button as an input 254 | pinMode(5, OUTPUT); // Set the button as an input 255 | pinMode(6, OUTPUT); // Set the button as an input 256 | 257 | pinMode(7, INPUT_PULLUP); // Set the button as an input 258 | pinMode(8, INPUT_PULLUP); // Set the button as an input 259 | pinMode(9, INPUT_PULLUP); // Set the button as an input 260 | pinMode(10, INPUT_PULLUP); // Set the button as an input 261 | pinMode(11, INPUT_PULLUP); // Set the button as an input 262 | pinMode(12, INPUT_PULLUP); // Set the button as an input 263 | pinMode(13, INPUT_PULLUP); // Set the button as an input 264 | pinMode(14, INPUT_PULLUP); // Set the button as an input 265 | pinMode(15, INPUT_PULLUP); // Set the button as an input 266 | pinMode(16, INPUT_PULLUP); // Set the button as an input 267 | pinMode(21, INPUT_PULLUP); // Set the button as an input 268 | pinMode(22, INPUT_PULLUP); // Set the button as an input 269 | 270 | digitalWrite(2, HIGH); // Pull the button high 271 | digitalWrite(3, HIGH); // Pull the button high 272 | digitalWrite(4, HIGH); // Pull the button high 273 | digitalWrite(5, HIGH); // Pull the button high 274 | digitalWrite(6, HIGH); // Pull the button high 275 | 276 | } 277 | 278 | void setDriveStrenthLow(){ 279 | gpio_set_drive_strength (2, GPIO_DRIVE_STRENGTH_2MA); 280 | gpio_set_drive_strength (3, GPIO_DRIVE_STRENGTH_2MA); 281 | gpio_set_drive_strength (4, GPIO_DRIVE_STRENGTH_2MA); 282 | gpio_set_drive_strength (5, GPIO_DRIVE_STRENGTH_2MA); 283 | gpio_set_drive_strength (6, GPIO_DRIVE_STRENGTH_2MA); 284 | gpio_set_drive_strength (7, GPIO_DRIVE_STRENGTH_2MA); 285 | gpio_set_drive_strength (8, GPIO_DRIVE_STRENGTH_2MA); 286 | gpio_set_drive_strength (9, GPIO_DRIVE_STRENGTH_2MA); 287 | gpio_set_drive_strength (10, GPIO_DRIVE_STRENGTH_2MA); 288 | gpio_set_drive_strength (11, GPIO_DRIVE_STRENGTH_2MA); 289 | gpio_set_drive_strength (12, GPIO_DRIVE_STRENGTH_2MA); 290 | gpio_set_drive_strength (13, GPIO_DRIVE_STRENGTH_2MA); 291 | gpio_set_drive_strength (14, GPIO_DRIVE_STRENGTH_2MA); 292 | gpio_set_drive_strength (15, GPIO_DRIVE_STRENGTH_2MA); 293 | gpio_set_drive_strength (16, GPIO_DRIVE_STRENGTH_2MA); 294 | gpio_set_drive_strength (21, GPIO_DRIVE_STRENGTH_2MA); 295 | gpio_set_drive_strength (22, GPIO_DRIVE_STRENGTH_2MA); 296 | } 297 | void setup() 298 | { 299 | setup_uart(); 300 | 301 | for(int i = 0;i<5;i++){ 302 | for(int j = 0; j<22;j++){ 303 | lastdebouncetime[i][j] = 0; 304 | lastinputstate[i][j] = 1; 305 | } 306 | } 307 | 308 | u8g2.begin(); 309 | u8g2.setDisplayRotation(U8G2_R0); 310 | u8g2.clearBuffer(); 311 | //this is the original for menurowcount5 312 | //u8g2.setFont(u8g2_font_profont29_mf); //19 pixel height, no overlap, mono, full 313 | u8g2.setFont(u8g2_font_profont22_mf); //14 pixel height, no overlap, mono, full 314 | pinMode(20, OUTPUT); //GPIO shutdown for software-controlled shutdown after timeout 315 | digitalWrite(20, LOW); //Pull shutdown low. 316 | //builtin led 317 | pinMode(25, OUTPUT); 318 | 319 | //adc's for sampling battery voltage. Needs a voltage divider on pcb still. 320 | //adc_init(); 321 | //adc_gpio_init(29); 322 | //adc_select_input(3); 323 | 324 | setupInput(); 325 | 326 | //set all keyboard pins to 2mA.. 327 | setDriveStrenthLow(); 328 | } 329 | 330 | //These camera control functions are not working yet. The hex strings come from the GM-803 camera module manual 331 | void cameraReset(){ 332 | setup_uart(); 333 | const char camreset[] = "\x7E\x00\x08\x01\x00\xD9\x50\xAB\xCD"; 334 | uart_puts(UART0_ID, camreset); 335 | delay(50); 336 | uart_deinit(UART0_ID); 337 | } 338 | void cameraSleep(){ 339 | setup_uart(); 340 | const char camsleep[] = "\x7E\x00\x08\x01\x00\xD9\xA5\xAB\xCD"; 341 | uart_puts(UART0_ID, camsleep); 342 | delay(50); 343 | uart_deinit(UART0_ID); 344 | } 345 | void cameraWake(){ 346 | setup_uart(); 347 | const char camwake[] = "\x7E\x00\x08\x01\x00\xD9\x00\xAB\xCD"; 348 | uart_puts(UART0_ID, camwake); 349 | delay(50); 350 | uart_deinit(UART0_ID); 351 | } 352 | //turning off UTF-8 encoding, and sending pure data (pure bytes) could potentially allow us to not use base64 encoding, saving 33% space in our QR codes. 353 | void cameraPureData(){ 354 | const unsigned char camsleep[] = {0x7E, 0x00, 0x08, 0x01, 0x00, 0x60, 0x00, 0xAB, 0xCD }; 355 | } 356 | void cameraWithProtocol(){ 357 | const unsigned char camwake[] = {0x7E, 0x00, 0x08, 0x01, 0x00, 0x60, 0x01, 0xAB, 0xCD }; 358 | } 359 | 360 | //helper functions to print keys to serial output when writing/debugging. 361 | void printuCharAsHex(unsigned char c) { 362 | char hexCar[2]; 363 | sprintf(hexCar, "%02X", c); 364 | Serial.print(hexCar); 365 | } 366 | 367 | void printNarrayuchar(unsigned char num[], int size1) { 368 | for(int i=0; irandombit & 1; //out is a pointer to a byte. initialize it, do AND with a bit from the random register 390 | for(int k=0; k<8; k++) { //8 times.. do.. (could be any other value here, but at least 8 so we fill a whole byte. 391 | *out = (*out << 1) ^ (rosc_hw->randombit & 1); //left shift one position, XOR with another random byte. 392 | } 393 | } 394 | 395 | int prompt(char * promptmessage, char * options[], int totaloptions){ 396 | int returnvalue; 397 | 398 | delay(400); //to prevent a right arrow to be carried over from the previous "next" button press 399 | char writemessagebuf[10000]; 400 | int writemessagebufpos = 0; 401 | int prevwritemessagebufpos = 0; 402 | int * writemessagebufpospointer = &writemessagebufpos; 403 | int * prevwritemessagebufpospointer = &prevwritemessagebufpos; 404 | 405 | rendermainagain = true; //to render it at least once.. 406 | menustate = 0; 407 | prevmenustate = 0; 408 | menuhighlight = 0; 409 | 410 | while(writemessagebuf[*writemessagebufpospointer-1] != 18 && writemessagebuf[*writemessagebufpospointer-1] != 20){ //no right or left arrow.. this sort of works.. 411 | collectInput(writemessagebuf, writemessagebufpospointer, prevwritemessagebufpospointer); 412 | evaluateBuffers(writemessagebuf, *writemessagebufpospointer, *prevwritemessagebufpospointer, totaloptions-1); //this checks what is the last character in a specific buffer for a specific state.. and depending on that, sets the state.. 413 | renderMenu(options, promptmessage, totaloptions); 414 | } 415 | if(writemessagebuf[*writemessagebufpospointer-1] == 20){ //20 is left arrow. if right arrow, don't set state. 416 | returnvalue = 0; //aborted (left arrow) or selected "no" 417 | } 418 | if(writemessagebuf[*writemessagebufpospointer-1] == 18){ //18 is right arrow. if right arrow, set the recipient 419 | returnvalue = menustate; //0 for no, 1 for yes 420 | } 421 | resetCharBuffer(writemessagebuf, writemessagebufpospointer, prevwritemessagebufpospointer); 422 | return returnvalue; 423 | } 424 | 425 | void deleteContact(int targetcontact, int totalcontacts){ 426 | char targetRecipient[17]; 427 | int targetRecipientLength = 0; 428 | int * targetRecipientLengthPointer = &targetRecipientLength; 429 | readRecipientNameAndLength(targetcontact,targetRecipient,targetRecipientLengthPointer); 430 | 431 | //are you sure 432 | char * options[] = {"No", "Yes"}; 433 | char* str = "Delete "; 434 | char dest[32]; 435 | 436 | strcpy(dest, str ); 437 | strcat(dest, targetRecipient); 438 | 439 | int goahead = 0; 440 | goahead = prompt(dest, options, 2); 441 | if(goahead == 1){ 442 | eraseSector(targetcontact); 443 | readRecipientNameAndLength(totalcontacts-1,targetRecipient,targetRecipientLengthPointer); 444 | 445 | unsigned char lastcontactpublickey[32] = {0}; 446 | readKey(lastcontactpublickey, 32, totalcontacts-1, 0); 447 | 448 | saveKey(lastcontactpublickey, 32, targetcontact, 0, targetRecipient, 16); 449 | eraseSector(totalcontacts-1); 450 | } 451 | } 452 | 453 | void readmessage(int targetcontact){ 454 | displayMessage("Scanning QR code",14,32); 455 | bool camerainterrupted = false; 456 | int readlength = 424; 457 | char camerainputbuffer[readlength]; 458 | 459 | camerainterrupted = readCamera(readlength, camerainputbuffer); 460 | if(!camerainterrupted){ 461 | delay(100); 462 | int decodedLength1 = Base64.decodedLength(camerainputbuffer, readlength); 463 | char decodedString[decodedLength1]; 464 | Base64.decode(decodedString, camerainputbuffer, readlength); 465 | uint8_t unsignedDecodedString[decodedLength1] = {0}; 466 | for(int i = 0;i hkdf; 562 | hkdf.setKey(keyderivationfunctioninput, sizeof(64)); 563 | hkdf.extract(kdfoutput, sizeof(kdfoutput), kdfinfofield, 96); 564 | 565 | 566 | static TestVector const testVectorChaChaPoly_1 PROGMEM = { 567 | .name = "ChaChaPoly #1", 568 | .key = {}, 569 | .plaintext = {}, 570 | .ciphertext = {}, 571 | .authdata = {}, 572 | .iv = {}, 573 | .tag = {}, 574 | .authsize = 12, 575 | .datasize = 114, 576 | .tagsize = 16, 577 | .ivsize = 12 578 | }; 579 | 580 | ChaChaPoly chachapolycipher; 581 | ChaChaPoly * chachapolycipherpointer; 582 | chachapolycipherpointer = &chachapolycipher; 583 | TestVector testVector; 584 | const struct TestVector *test; 585 | memcpy_P(&testVector, &testVectorChaChaPoly_1, sizeof(TestVector)); 586 | test = &testVector; 587 | chachapolycipherpointer->clear(); 588 | 589 | bool setkeyreturn = false; 590 | //not sure what setkeyreturn was supposed to do. 591 | setkeyreturn = chachapolycipherpointer->setKey(kdfoutput, 32); 592 | 593 | bool setivreturn = false; 594 | setivreturn = chachapolycipherpointer->setIV(test->iv, test->ivsize); 595 | 596 | //we change the key every time, so we can have a static IV here. 597 | 598 | //pad to x chars always 599 | //425 is total bytes with mode 13,0 600 | //318 is base64 reduced length of 425 (/4, *3). 601 | 602 | int totallength = 318; 603 | int targetlength = totallength - 32; 604 | 605 | //Message is after ephemeral public key. 606 | static byte message[286]; 607 | for(int i = 32;i<318;i++){ 608 | message[i-32]=encryptedbuffer[i]; 609 | } 610 | static byte outputbuffer[286] = {0}; 611 | 612 | chachapolycipherpointer->decrypt(outputbuffer, message, targetlength); 613 | 614 | //how to deal with unsuccesful decrypt? It might crash? 615 | //optional chachapolycipher->addAuthData(buffer2, 128); 616 | 617 | char result[286]; 618 | for(int i = 0;i<285;i++){ 619 | result[i] = outputbuffer[i]; 620 | } 621 | 622 | int p1 = 285; 623 | int p2 = 285; 624 | int * pp1 = &p1; 625 | int * pp2 = &p2; 626 | 627 | displaydecryptmessage = true; 628 | readdecryptedmessageloop(targetcontact, result, pp1, pp2); 629 | delay(300); 630 | } 631 | 632 | 633 | void encryptmessage(char * messagebuf, int * messagebufsize, int recipient){ 634 | //We follow the this procedure for encrypting our message: https://crypto.stackexchange.com/questions/101420/eccdh-direct-or-with-temporary-ecc-keypairs 635 | 636 | //The sender generates an ephemeral key pair. 637 | //The sender computes an ephemeral shared secret using their ephemeral private key and the recipient's long-term public key. The ephemeral private key is then erased from memory. 638 | //The sender also computes a long-term shared secret using their long-term private key and the recipient's long-term public key. 639 | //The sender concatenates the ephemeral shared secret and long-term shared secret to form the input keying material for a KDF. 640 | //The output keying material is used as the key to encrypt a message using an AEAD. 641 | //The sender's ephemeral public key is prepended to the ciphertext, and the ciphertext is sent to the recipient. 642 | //The total is encoded as a QR code and displayed 643 | 644 | //reverse at receiver 645 | 646 | //Sender's long term private key is conventionally at sector -3 647 | unsigned char senderlongtermprivatekeyarray[32] = {0}; 648 | readKey(senderlongtermprivatekeyarray, 32, -3, 0); 649 | 650 | //Sender's long term public key is conventionally at sector -2 651 | unsigned char senderlongtermpublickeyarray[32] = {0}; 652 | readKey(senderlongtermpublickeyarray, 32, -2, 0); 653 | 654 | //Recipient long term key is read from contacts at sector "recipient", passed into this method. 655 | unsigned char recipientlongtermpublickeyarray[32] = {0}; 656 | readKey(recipientlongtermpublickeyarray, 32, recipient, 0); 657 | 658 | //Generate ephemeral keypair, just for this message. 659 | static uint8_t ephemeralkeypairpublicuint[32]; 660 | static uint8_t ephemeralkeypairprivateuint[32]; 661 | while(!RNG.available(32)){ 662 | uint8_t rosc_single_byte2; 663 | uint8_t * rosc_byte_byte_pointer2 = &rosc_single_byte2; 664 | rosc_single_byte(rosc_byte_byte_pointer2); 665 | //1 bit entropy credit per single byte, very conservative. 666 | RNG.stir(rosc_byte_byte_pointer2, 1, 1); 667 | } 668 | Curve25519::dh1(ephemeralkeypairpublicuint, ephemeralkeypairprivateuint); 669 | 670 | //make some copies, all neccessary.. 671 | uint8_t recipientlongtermpublickeyarrayuint[32]; 672 | for(int i = 0;i<32;i++){ 673 | recipientlongtermpublickeyarrayuint[i]=recipientlongtermpublickeyarray[i]; 674 | } 675 | 676 | uint8_t recipientlongtermpublickeyarrayuint2[32]; 677 | for(int i = 0;i<32;i++){ 678 | recipientlongtermpublickeyarrayuint2[i]=recipientlongtermpublickeyarrayuint[i]; 679 | } 680 | 681 | uint8_t recipientlongtermpublickeyarrayuint3[32]; 682 | for(int i = 0;i<32;i++){ 683 | recipientlongtermpublickeyarrayuint3[i]=recipientlongtermpublickeyarrayuint[i]; 684 | } 685 | 686 | uint8_t senderlongtermprivatekeyuint[32]; 687 | for(int i = 0;i<32;i++){ 688 | senderlongtermprivatekeyuint[i]=senderlongtermprivatekeyarray[i]; 689 | } 690 | 691 | uint8_t senderlongtermpublickeyuint[32]; 692 | for(int i = 0;i<32;i++){ 693 | senderlongtermpublickeyuint[i]=senderlongtermpublickeyarray[i]; 694 | } 695 | 696 | //generate the shared secret, put in the first argument, bit strange way of doing it, but that is how the library works. 697 | //Curve25519::dh2(alice_k, bob_f); 698 | //should validate public key actually still? 699 | 700 | //Ephemeral shared secret 701 | //The sender computes an ephemeral shared secret using their ephemeral private key and the recipient's long-term public key. The ephemeral private key is then erased from memory. 702 | Curve25519::dh2(recipientlongtermpublickeyarrayuint, ephemeralkeypairprivateuint); 703 | //now recipientlongtermpublickeyarrayuint became the ephemeral shared secret 704 | 705 | //Long term shared secret 706 | //The sender also computes a long-term shared secret using their long-term private key and the recipient's long-term public key. 707 | Curve25519::dh2(recipientlongtermpublickeyarrayuint2, senderlongtermprivatekeyuint); 708 | //now recipientlongtermpublickeyarrayuint2 became the long term secret key.. 709 | 710 | //The sender concatenates the ephemeral shared secret and long-term shared secret to form the input keying material for a KDF. 711 | uint8_t keyderivationfunctioninput[64]; 712 | for(int i = 0;i<32;i++){ 713 | keyderivationfunctioninput[i]=recipientlongtermpublickeyarrayuint[i]; 714 | } 715 | for(int i = 32;i<64;i++){ 716 | keyderivationfunctioninput[i]=recipientlongtermpublickeyarrayuint2[i-32]; 717 | } 718 | 719 | //Concatenate all public keys for the key derivation function (kdf) info field, in this order 720 | //ephemeralkeypairpublicuint 721 | //recipientlongtermpublickeyarrayuint3 722 | //senderlongtermpublickeyuint 723 | 724 | uint8_t kdfinfofield[96]; 725 | for(int i = 0;i<32;i++){ 726 | kdfinfofield[i]=ephemeralkeypairpublicuint[i]; 727 | } 728 | for(int i = 32;i<64;i++){ 729 | kdfinfofield[i]=recipientlongtermpublickeyarrayuint3[i-32]; 730 | } 731 | for(int i = 64;i<96;i++){ 732 | kdfinfofield[i]=senderlongtermpublickeyuint[i-64]; 733 | } 734 | 735 | //KDF: include all three public keys in the info parameter/context material/public context/public keys. salt is also publicly known 736 | //https://hal.inria.fr/hal-01463822/document (page 4) 737 | //https://www.rfc-editor.org/rfc/rfc5869 738 | //should salt still? 739 | //info binds the derived key material to the public keys if you include them. 740 | //Make sure you include the sender's and recipient's public key in the key derivation. 741 | //"typically including all 3 public keys involved": https://neilmadden.blog/2018/11/26/public-key-authenticated-encryption-and-why-you-want-it-part-ii/ 742 | //(for each public key, there are several publicly computable public keys that are equivalent to it. That is why you should include them). https://www.rfc-editor.org/rfc/rfc7748#section-7 743 | 744 | uint8_t kdfoutput[32]; 745 | 746 | HKDF hkdf; 747 | hkdf.setKey(keyderivationfunctioninput, sizeof(64)); 748 | hkdf.extract(kdfoutput, sizeof(kdfoutput), kdfinfofield, 96); 749 | 750 | Serial.println("KDF output is" ); 751 | printNarray(kdfoutput, 32); 752 | 753 | 754 | static TestVector const testVectorChaChaPoly_1 PROGMEM = { 755 | .name = "ChaChaPoly #1", 756 | .key = {}, 757 | .plaintext = {}, 758 | .ciphertext = {}, 759 | .authdata = {}, 760 | .iv = {}, 761 | .tag = {}, 762 | .authsize = 12, 763 | .datasize = 114, 764 | .tagsize = 16, 765 | .ivsize = 12 766 | }; 767 | 768 | 769 | ChaChaPoly chachapolycipher; 770 | ChaChaPoly * chachapolycipherpointer; 771 | chachapolycipherpointer = &chachapolycipher; 772 | TestVector testVector; 773 | const struct TestVector *test; 774 | memcpy_P(&testVector, &testVectorChaChaPoly_1, sizeof(TestVector)); 775 | test = &testVector; 776 | chachapolycipherpointer->clear(); 777 | 778 | bool setkeyreturn = false; 779 | setkeyreturn = chachapolycipherpointer->setKey(kdfoutput, 32); 780 | 781 | bool setivreturn = false; 782 | setivreturn = chachapolycipherpointer->setIV(test->iv, test->ivsize); 783 | 784 | //uint8_t kdfoutput[32]; 785 | //we change the key every time, so we can have a static initialization vector (iv) here. 786 | 787 | //pad to 299 chars always 788 | 789 | int buffersize = *messagebufsize; 790 | 791 | //425 is total bytes with mode 13,0 792 | //318 is base64 reduced length of 425 (/4, *3). 793 | 794 | int totallength = 318; 795 | int targetlength = totallength - 32; 796 | byte inputbuffer[targetlength] = {0}; 797 | byte outputbuffer[targetlength] = {0}; 798 | 799 | //fill up inputbuffer with message 800 | for(int i = 0;iencrypt(outputbuffer, inputbuffer, targetlength); 810 | 811 | //optional? 812 | //chachapolycipher->addAuthData(buffer2, 128); 813 | 814 | //The sender's ephemeral public key is prepended to the ciphertext, and the ciphertext is sent to the recipient. 815 | byte totalbuffer[totallength]; 816 | for(int i = 0;i<32;i++){ 817 | totalbuffer[i]=ephemeralkeypairpublicuint[i]; 818 | } 819 | for(int i = 32;i 299){ 912 | maxlengthprint = 299; 913 | } 914 | sprintf(mssglengthbuffer,"%d",maxlengthprint); 915 | 916 | //only display if something was added to the buffer, when backspace was pressed, or upon first render 917 | if(*writemessageinputpospointer3 != *prevwritemessageinputpospointer3 || globalbackspace || firstrender){ 918 | u8g2.clearBuffer(); // clear the internal memory 919 | u8g2.setFontMode(1); /* activate transparent font mode */ //addition 920 | u8g2.setDrawColor(1); 921 | u8g2.drawBox(0, 0, screenwidth, menuitemboxy); //addition 922 | 923 | u8g2.setDrawColor(2); 924 | u8g2.drawStr(0+menuitemboxxtextoffset,menuitemboxytextoffset,dest); 925 | u8g2.drawStr(350,menuitemboxytextoffset,mssglengthbuffer); 926 | 927 | //headermargin additions 928 | u8g2.setDrawColor(0); /* color 0 for the headermargin */ 929 | u8g2.drawBox(0, menuitemboxy, screenwidth, headermargin); 930 | 931 | u8g2.setDrawColor(1); /* color 1 for white background */ 932 | u8g2.drawBox(0, menuitemboxy+headermargin, screenwidth, 240-(menuitemboxy+headermargin)); 933 | u8g2.setDrawColor(2); 934 | processDisplayStrings(writemessageinputbuf3, writemessageinputpospointer3, prevwritemessageinputpospointer3); 935 | //updateDisplayArea from u8g2 library not working. 936 | u8g2.sendBuffer(); 937 | globalbackspace = false; 938 | firstrender = false; 939 | } 940 | } 941 | if(writemessageinputbuf3[*writemessageinputpospointer3-1] == 18){ //if enter, or right arrow 942 | //here it will just take the first 299 characters.. 943 | encryptmessage(writemessageinputbuf3, writemessageinputpospointer3, targetcontact); 944 | } 945 | //is this really necessary? 946 | //should not even be necessary. 947 | resetCharBuffer(writemessageinputbuf3, writemessageinputpospointer3, prevwritemessageinputpospointer3); 948 | firstrender = true; 949 | 950 | delay(300); 951 | } 952 | 953 | void readdecryptedmessageloop(int targetcontact, char * readmessagebuf, int * readmessagebufpospointer, int * prevreadmessagebufpospointer){ 954 | char displaystrings[30][300]; 955 | 956 | char targetRecipient[17]; //char targetRecipient[16]; //this crashes at strcat(dest, targetRecipient) 957 | int targetRecipientLength = 0; 958 | int * targetRecipientLengthPointer = &targetRecipientLength; 959 | readRecipientNameAndLength(targetcontact,targetRecipient,targetRecipientLengthPointer); 960 | 961 | char* str = "message from: "; 962 | char dest[32]; 963 | 964 | strcpy(dest, str ); 965 | strcat(dest, targetRecipient); 966 | 967 | while(readmessagebuf[*readmessagebufpospointer-1] != 18 && readmessagebuf[*readmessagebufpospointer-1] != 20){ //no right or left arrow.. this sort of works.. 968 | 969 | collectInput(readmessagebuf, readmessagebufpospointer, prevreadmessagebufpospointer); 970 | 971 | u8g2.clearBuffer(); 972 | u8g2.setFontMode(1); 973 | u8g2.setDrawColor(1); 974 | u8g2.drawBox(0, 0, screenwidth, menuitemboxy); 975 | 976 | u8g2.setDrawColor(2); 977 | u8g2.drawStr(0+menuitemboxxtextoffset,menuitemboxytextoffset,dest); 978 | 979 | //headermargin additions 980 | u8g2.setDrawColor(0); 981 | u8g2.drawBox(0, menuitemboxy, screenwidth, headermargin); 982 | 983 | u8g2.setDrawColor(1); 984 | u8g2.drawBox(0, menuitemboxy+headermargin, screenwidth, 240-(menuitemboxy+headermargin)); 985 | u8g2.setDrawColor(2); 986 | processDisplayStrings(readmessagebuf, readmessagebufpospointer, prevreadmessagebufpospointer); 987 | u8g2.sendBuffer(); 988 | globalbackspace = false; 989 | firstrender = false; 990 | } 991 | 992 | //empty plaintext from memory 993 | for(int i = 0;i<280;i++){ 994 | readmessagebuf[i] = 0; 995 | } 996 | *readmessagebufpospointer = 0; 997 | *prevreadmessagebufpospointer = 0; 998 | 999 | } 1000 | 1001 | //modify the first line that does not end on a space and does is not a full line of characters, which we want to allow. 1002 | //if found, insert those spaces, modify the passed buffer. 1003 | //return amount of spaces added, 0 when done. 1004 | int modifyFirstLine(char * passedbuffer, int passedbuffertotallength, int passedbufferend){ 1005 | 1006 | int totallines = passedbufferend/maxcharacters; 1007 | //find first line with a space.. 1008 | int firstlinewithspace = -1; 1009 | int firstspace = -1; 1010 | 1011 | //find first line that does not end in space. 1012 | //and is not all characters 1013 | int firstillegallinewithoutspace = -1; 1014 | 1015 | bool fullcharacterline = true; 1016 | for(int i = 0;i=0;i--){ 1046 | if(passedbuffer[((firstillegallinewithoutspace)*maxcharacters)+i]==' '){ 1047 | lastspace = i; 1048 | break; 1049 | } 1050 | } 1051 | 1052 | if(lastspace == -1){ 1053 | //illegal condition, there should always be a space.. 1054 | } 1055 | 1056 | int shiftright = maxcharacters-lastspace-1; //added -1 to prevent space in front of line 1057 | int positionoflastspace = ((firstillegallinewithoutspace)*maxcharacters)+lastspace; 1058 | //shift right 1059 | for(int i = passedbufferend+shiftright;i>positionoflastspace;i--){ 1060 | passedbuffer[i]=passedbuffer[i-shiftright]; 1061 | } 1062 | //insert some spaces there.. 1063 | for(int i = positionoflastspace;i *prevwritemessagebufpospointer) { //up arrow 1140 | if(startpositionarrowed > 0){ 1141 | uparrows++; 1142 | } 1143 | //make sure one arrow gets removed from the buffer itself. 1144 | writemessagebuf[*writemessagebufpospointer-1]=' '; 1145 | if(*writemessagebufpospointer <= 0){ //this works like a charm, actually! :) 1146 | *writemessagebufpospointer=0; 1147 | *prevwritemessagebufpospointer=0; 1148 | } 1149 | else{ 1150 | --*writemessagebufpospointer; 1151 | --*prevwritemessagebufpospointer; 1152 | } 1153 | } 1154 | if (writemessagebuf[*writemessagebufpospointer-1] == 19 && *writemessagebufpospointer > *prevwritemessagebufpospointer) { //down arrow 1155 | if(startpositionarrowed < (modifiedrowcount+1) - menurowcount){ 1156 | downarrows++; 1157 | } 1158 | //make sure one arrow gets removed from the buffer itself. 1159 | writemessagebuf[*writemessagebufpospointer-1]=' '; 1160 | if(*writemessagebufpospointer <= 0){ 1161 | *writemessagebufpospointer=0; 1162 | *prevwritemessagebufpospointer=0; 1163 | } 1164 | else{ 1165 | --*writemessagebufpospointer; 1166 | --*prevwritemessagebufpospointer; 1167 | } 1168 | } 1169 | if(modifiedrowcount+1>menurowcount){ 1170 | startpositionarrowed = (modifiedrowcount+1) - menurowcount - uparrows + downarrows; 1171 | } 1172 | else{ 1173 | startpositionarrowed = 0; 1174 | } 1175 | if(startpositionarrowed < 0){ 1176 | startpositionarrowed = 0; 1177 | } 1178 | //to display message from the start.. 1179 | //something like firstrender this.. 1180 | if(displaydecryptmessage){ 1181 | startpositionarrowed = 0; 1182 | if(uparrows != 0 || downarrows != 0){ 1183 | displaydecryptmessage = false; 1184 | uparrows = modifiedrowcount - menurowcount; //to start from the beginning instead of the end.. 1185 | startpositionarrowed = modifiedrowcount - menurowcount - uparrows + downarrows; 1186 | } 1187 | } 1188 | //Serial.println("menurowcount " + String(menurowcount) + " modifiedrowcount " + modifiedrowcount + " startpositionarr " + String(startpositionarrowed) + " uparrows " + String(uparrows) + " downarrows " + String(downarrows)); 1189 | } 1190 | 1191 | //read keypad, debounce input. 1192 | void collectInput(char *inputbuffer, int* inputbufferpositionpointer, int* inputbufferpreviouspositionpointer){ 1193 | //active low, low reading is input detected. 1194 | int rowreading = 0; 1195 | int colreading = 0; 1196 | int output = 0; 1197 | 1198 | for (int row=0; row<5; row++) 1199 | { 1200 | pinMode(rowPins[row], OUTPUT); //set the row pin as an output 1201 | digitalWrite(rowPins[row], LOW); //pull that row pin low 1202 | for (int col=0; col<10; col++) 1203 | { 1204 | int reading = digitalRead(columnPins[col]); 1205 | if (!reading) //active low 1206 | { 1207 | if(lastinputstate[row][col]!=reading){ //and it's not the same as before 1208 | if ((millis() - lastdebouncetime[row][col]) > debouncetime){ //and enough time has passed 1209 | rowreading = row; 1210 | colreading = col; 1211 | //lastinputstate[row][col] = reading; 1212 | lastinputstate[row][col] = 1; //reset to inactive right away. otherwise reset further up to prevent retriggers 1213 | output = (((row+1)*100) + (col+1)); //set the status bit of the keypad return value 1214 | lastdebouncetime[row][col] = millis(); //update last detected keypress for debouncing 1215 | } 1216 | } 1217 | } 1218 | } 1219 | pinMode(rowPins[row], INPUT); //reset the row pin as an input 1220 | digitalWrite(rowPins[row], HIGH); //pull the row pin high 1221 | } 1222 | 1223 | if (output != 0){ 1224 | //set inputbufferpreviouspositionpointer because sendKeyPress updates inputbufferpositionpointer 1225 | *inputbufferpreviouspositionpointer = *inputbufferpositionpointer; 1226 | sendKeyPress(rowreading, colreading, inputbuffer, inputbufferpositionpointer, inputbufferpreviouspositionpointer); 1227 | } 1228 | else{ 1229 | //no input, so prev=current. 1230 | *inputbufferpreviouspositionpointer = *inputbufferpositionpointer; 1231 | } 1232 | } 1233 | 1234 | 1235 | //evaluateBuffers was originally written to cooperate with renderMenu, figure out which arrows were pressed and set menustate accordingly. 1236 | //renderMenu/evaluateBuffers also used for displaying select contact menu and prompt. 1237 | void evaluateBuffers(char passedbuffer[], int passedbufferpositionpointer, int passedpreviousbufferpositionpointer, int targetmenuitemstotal){ 1238 | //characters 17, 18, 19, 20 are used conventionally for up, right, down, left arrows 1239 | //UP ARROW 1240 | if (passedbuffer[passedbufferpositionpointer-1] == 17 && passedbufferpositionpointer > passedpreviousbufferpositionpointer) { 1241 | if (menustate <= 0 ){ 1242 | menustate = 0; //for ending at the end. no scrolling around 1243 | } 1244 | else{ 1245 | menustate--; 1246 | } 1247 | } 1248 | //DOWN ARROW 1249 | else if (passedbuffer[passedbufferpositionpointer-1] == 19 && passedbufferpositionpointer > passedpreviousbufferpositionpointer){ //down arrow && menustate != prevmenustate 1250 | if (menustate >= 0 && menustate < targetmenuitemstotal){ 1251 | menustate++; 1252 | } 1253 | else if (menustate >= targetmenuitemstotal){ 1254 | menustate = targetmenuitemstotal; 1255 | } 1256 | else{ 1257 | 1258 | } 1259 | } 1260 | //RIGHT ARROW 1261 | else if (passedbuffer[passedbufferpositionpointer-1] == 18 && passedbufferpositionpointer > passedpreviousbufferpositionpointer){ 1262 | //if in main menu, set the state.. ok that makes sense.. 1263 | //if in any other menu, those "right arrows" are caught locally for example for nameInputLoop and prompt and selectContact 1264 | if(state == 0){ 1265 | state=menustate+1; 1266 | } 1267 | } 1268 | //LEFT ARROW 1269 | else if (passedbuffer[passedbufferpositionpointer-1] == 20 && passedbufferpositionpointer > passedpreviousbufferpositionpointer){ 1270 | state=0; 1271 | } 1272 | else{ 1273 | //some other character received while scrolling the passed menu. Ignore. 1274 | } 1275 | } 1276 | 1277 | bool nameInputLoop(char passedbuffer[], int *passedbufferpositionpointer, int *passedpreviousbufferpositionpointer){ 1278 | bool breakreturn = false; 1279 | //header 1280 | u8g2.clearBuffer(); 1281 | u8g2.setFontMode(1); 1282 | u8g2.setDrawColor(1); 1283 | u8g2.drawBox(0, 0, screenwidth, menuitemboxy); 1284 | u8g2.setDrawColor(0); 1285 | u8g2.drawStr(0+menuitemboxxtextoffset, menuitemboxytextoffset, "Enter contact name"); 1286 | 1287 | //headermargin additions 1288 | u8g2.setDrawColor(0); //color 0 for the headermargin (black) 1289 | u8g2.drawBox(0, menuitemboxy, screenwidth, headermargin); 1290 | 1291 | //white box 1292 | u8g2.setDrawColor(1); // color 1 for white background (white) 1293 | u8g2.drawBox(0, menuitemboxy+headermargin, screenwidth, 240-(menuitemboxy+headermargin)); 1294 | u8g2.sendBuffer(); 1295 | 1296 | while(passedbuffer[*passedbufferpositionpointer-1] != 18 && passedbuffer[*passedbufferpositionpointer-1] != 20){ //if no left or right arrow, continue 1297 | collectInput(passedbuffer, passedbufferpositionpointer, passedpreviousbufferpositionpointer); 1298 | if(*passedbufferpositionpointer != *passedpreviousbufferpositionpointer || globalbackspace || firstrender){ 1299 | u8g2.clearBuffer(); 1300 | u8g2.setDrawColor(1); 1301 | u8g2.drawBox(0, 0, screenwidth, menuitemboxy); 1302 | u8g2.setDrawColor(0); 1303 | u8g2.drawStr(0+menuitemboxxtextoffset, menuitemboxytextoffset, "Enter contact name"); 1304 | 1305 | //headermargin additions 1306 | u8g2.setDrawColor(0); //color 0 for the headermargin (black) 1307 | u8g2.drawBox(0, menuitemboxy, screenwidth, headermargin); 1308 | 1309 | //white box 1310 | u8g2.setDrawColor(1); // color 1 for white background (white) 1311 | u8g2.drawBox(0, menuitemboxy+headermargin, screenwidth, 240-(menuitemboxy+headermargin)); 1312 | 1313 | char mssglengthbuffer[2]; 1314 | int maxlengthprint = *passedbufferpositionpointer; 1315 | if(maxlengthprint > 16){ 1316 | maxlengthprint = 16; 1317 | } 1318 | sprintf(mssglengthbuffer,"%d",maxlengthprint); 1319 | //without this println, backspace does not work correctly and random commas show up.. 1320 | Serial.println("mssglengthbuffer is "+String(mssglengthbuffer)); 1321 | 1322 | char dest[5]; 1323 | strcpy(dest, mssglengthbuffer ); 1324 | strcat(dest, "/16"); 1325 | u8g2.setDrawColor(0); // color 0 for black text 1326 | u8g2.drawStr(320,menuitemboxytextoffset,dest); 1327 | char name16[16]; 1328 | name16[0]=' '; //otherwise garbage characters show up. 1329 | name16[1]=' '; //otherwise garbage characters show up. 1330 | for(int i = 0;i 16){ 1357 | *passedbufferpositionpointer = 16; 1358 | } 1359 | } 1360 | firstrender = true; 1361 | return breakreturn; 1362 | } 1363 | 1364 | bool nameExists(char passedbuffer[], int *passedbufferpositionpointer){ 1365 | bool returncheck = false; 1366 | 1367 | //every sector represents a possible contact, so firstEmptySector equals the amount of existing contacts 1368 | int amountOfContacts = firstEmptySector(); 1369 | 1370 | //this determines the length of every name and puts it in nameLenghts 1371 | uint8_t nameLengths[amountOfContacts]; 1372 | collectNameLengths(amountOfContacts, nameLengths); 1373 | for(int i = 0;i passedmenusize){ 1805 | localmenurowcount = passedmenusize; 1806 | } 1807 | u8g2.clearBuffer(); 1808 | u8g2.setFontMode(1); //activate transparent font mode 1809 | u8g2.setDrawColor(1); // color 1 for the box 1810 | u8g2.drawBox(0, 0, screenwidth, menuitemboxy); 1811 | u8g2.setDrawColor(0); 1812 | u8g2.drawStr(menuitemboxxtextoffset, menuitemboxytextoffset, headerstring); 1813 | 1814 | //headermargin additions 1815 | u8g2.setDrawColor(0); 1816 | u8g2.drawBox(0, menuitemboxy, screenwidth, headermargin); 1817 | 1818 | if(menustate == prevmenustate +1){ //scrolling down 1819 | //if(printactive){Serial.println("scrolling down");} 1820 | if(menuhighlight == 0){ //at first item, highlight = highlight +1 1821 | menuhighlight = menuhighlight +1; 1822 | prevmenustate = menustate; 1823 | for(int i = 0;i -1){ 1953 | //found at least one page with contents. So this complete sector is not empty. 1954 | returnval = false; 1955 | } 1956 | else{ 1957 | //did not find at least one page with contents. So this sector is empty. 1958 | returnval = true; 1959 | } 1960 | return returnval; 1961 | } 1962 | 1963 | 1964 | void eraseSector(int sector){ 1965 | //FLASH_PAGE_SIZE is the minimum size you can write. Is 256 bytes. 1966 | //200 is the convention for the memory offset, that is, the location, where contacts are stored. The last 200 sectors of memory are used for contacts. 1967 | int flash_target_offset = (PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE*200) + FLASH_SECTOR_SIZE*sector + FLASH_PAGE_SIZE; 1968 | uint32_t ints5 = save_and_disable_interrupts(); 1969 | flash_range_erase(flash_target_offset, FLASH_SECTOR_SIZE); 1970 | restore_interrupts (ints5); 1971 | } 1972 | 1973 | 1974 | void saveKey(uint8_t data[], int targetsize, int sector, int page, char nameinputbuffer[], int nameinputbufferpos){ 1975 | //https://www.makermatrix.com/blog/read-and-write-data-with-the-pi-pico-onboard-flash/ 1976 | //Note that when I called flash_range_program(), I cast the page buffer into (uint8_t *), as the function expects a pointer that increments on one byte boundaries 1977 | //flash_range_program programs a multiple of 256 (FLASH_PAGE_SIZE) bytes. 1978 | //200 is the convention for the memory offset, that is, the location, where contacts are stored. The last 200 sectors of memory are used for contacts. 1979 | 1980 | int flash_target_offset = (PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE*200) + FLASH_SECTOR_SIZE*sector + FLASH_PAGE_SIZE*page; 1981 | int flash_target_offset2 = (PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE*200) + FLASH_SECTOR_SIZE*sector + FLASH_PAGE_SIZE*10; //Page 10 is conventional offset for the name of the key. 1982 | int flash_target_offset3 = (PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE*200) + FLASH_SECTOR_SIZE*sector + FLASH_PAGE_SIZE*11; //Page 11 is conventional offset for the length of the name of the key. 1983 | 1984 | uint8_t *p2, page2, addr2; 1985 | addr2 = XIP_BASE + flash_target_offset; 1986 | p2 = (uint8_t *)addr2; 1987 | 1988 | //write is 256 bytes minium. 1989 | int buf[FLASH_PAGE_SIZE/sizeof(int)]; //One page buffer of ints, 64 ints in a buf. 4 bytes per int is 256 bytes total. 1990 | 1991 | for(int i = 0; i < targetsize; i++){ 1992 | buf[i] = data[i]; 1993 | } 1994 | 1995 | int buf2[FLASH_PAGE_SIZE/sizeof(int)]; //One page buffer of ints, 64 ints in a buf. 4 bytes per int is 256 bytes total. 1996 | 1997 | for(int i = 0; i <= nameinputbufferpos; i++){ 1998 | buf2[i] = nameinputbuffer[i]; 1999 | } 2000 | 2001 | //length of name 2002 | int buf3[FLASH_PAGE_SIZE/sizeof(int)]; //One page buffer of ints, 64 ints in a buf.4 bytes per int is 256 bytes total. 2003 | *buf3 = nameinputbufferpos; 2004 | 2005 | //erase before writing the key 2006 | uint32_t ints5 = save_and_disable_interrupts(); 2007 | flash_range_erase(flash_target_offset, FLASH_SECTOR_SIZE); 2008 | restore_interrupts (ints5); 2009 | 2010 | //saving the key 2011 | uint32_t ints2 = save_and_disable_interrupts(); 2012 | flash_range_program(flash_target_offset, (uint8_t *)buf, FLASH_PAGE_SIZE); 2013 | restore_interrupts (ints2); 2014 | 2015 | //saving the name 2016 | uint32_t ints3 = save_and_disable_interrupts(); 2017 | flash_range_program(flash_target_offset2, (uint8_t *)buf2, FLASH_PAGE_SIZE); 2018 | restore_interrupts (ints3); 2019 | 2020 | //saving the length of the name 2021 | uint32_t ints4 = save_and_disable_interrupts(); 2022 | flash_range_program(flash_target_offset3, (uint8_t *)buf3, FLASH_PAGE_SIZE); 2023 | restore_interrupts (ints3); 2024 | } 2025 | 2026 | void readKey(uint8_t *targetbuffer, int readamount, int sector, int page){ 2027 | //address from where we will read the key 2028 | int flash_target_offset = (PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE*200) + FLASH_SECTOR_SIZE*sector + FLASH_PAGE_SIZE*page; 2029 | 2030 | int *p2, page2, addr2; 2031 | addr2 = XIP_BASE + flash_target_offset; 2032 | //addr is an int. since this is a 32 bit architecture, increasing the address by 1 will read next first byte from 32 bits further up. 2033 | //https://www.makermatrix.com/blog/read-and-write-data-with-the-pi-pico-onboard-flash/ 2034 | p2 = (int *)addr2; 2035 | for(int i = 0;i