seccure-0.3/0000755000175100001440000000000010470676165012375 5ustar bertramusersseccure-0.3/curves.c0000644000175100001440000001740710470676205014054 0ustar bertramusers/* * seccure - Copyright 2006 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which is * part of the GnuPG project). The code compiles successfully with * libgcrypt 1.2.2. Use the included Makefile to build the binary. * * Compile with -D NOMEMLOCK if your machine doesn't support memory * locking. * * Report bugs to: seccure AT point-at-infinity.org * */ #include #include #include #include "curves.h" #include "ecc.h" #include "serialize.h" /******************************************************************************/ #define CURVE_NUM 8 struct curve { const char *name, *a, *b, *m, *base_x, *base_y, *order; int cofactor; int pk_len_compact; }; static const struct curve curves[CURVE_NUM] = { { "secp112r1", "db7c2abf62e35e668076bead2088", "659ef8ba043916eede8911702b22", "db7c2abf62e35e668076bead208b", "09487239995a5ee76b55f9c2f098", "a89ce5af8724c0a23e0e0ff77500", "db7c2abf62e35e7628dfac6561c5", 1, 18 }, { "secp128r1", "fffffffdfffffffffffffffffffffffc", "e87579c11079f43dd824993c2cee5ed3", "fffffffdffffffffffffffffffffffff", "161ff7528b899b2d0c28607ca52c5b86", "cf5ac8395bafeb13c02da292dded7a83", "fffffffe0000000075a30d1b9038a115", 1, 20 }, { "secp160r1", "ffffffffffffffffffffffffffffffff7ffffffc", "1c97befc54bd7a8b65acf89f81d4d4adc565fa45", "ffffffffffffffffffffffffffffffff7fffffff", "4a96b5688ef573284664698968c38bb913cbfc82", "23a628553168947d59dcc912042351377ac5fb32", "0100000000000000000001f4c8f927aed3ca752257", 1, 25 }, { "secp192r1/nistp192", "fffffffffffffffffffffffffffffffefffffffffffffffc", "64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1", "fffffffffffffffffffffffffffffffeffffffffffffffff", "188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012", "07192b95ffc8da78631011ed6b24cdd573f977a11e794811", "ffffffffffffffffffffffff99def836146bc9b1b4d22831", 1, 30 }, { "secp224r1/nistp224", "fffffffffffffffffffffffffffffffefffffffffffffffffffffffe", "b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4", "ffffffffffffffffffffffffffffffff000000000000000000000001", "b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21", "bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34", "ffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d", 1, 35 }, { "secp256r1/nistp256", "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc", "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b", "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff", "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5", "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551", 1, 40 }, { "secp384r1/nistp384", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc", "b3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088f5013875ac656398d8a2ed19d2a85c8edd3ec2aef", "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000ffffffff", "aa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741e082542a385502f25dbf55296c3a545e3872760ab7", "3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f", "ffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf581a0db248b0a77aecec196accc52973", 1, 60 }, { "secp521r1/nistp521", "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc", "051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00", "1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "0c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66", "11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650", "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409", 1, 81 }, }; /******************************************************************************/ #define SCAN(x,s) assert(! gcry_mpi_scan(x, GCRYMPI_FMT_HEX, s, 0, NULL)) static struct curve_params* load_curve(const struct curve *c) { gcry_mpi_t h; struct curve_params *cp; struct domain_params *dp; if (! (cp = malloc(sizeof(struct curve_params)))) return NULL; cp->name = c->name; dp = &cp->dp; SCAN(&dp->a, c->a); SCAN(&dp->b, c->b); SCAN(&dp->m, c->m); SCAN(&dp->order, c->order); SCAN(&dp->base.x, c->base_x); SCAN(&dp->base.y, c->base_y); dp->cofactor = c->cofactor; h = gcry_mpi_new(0); gcry_mpi_add(h, dp->m, dp->m); gcry_mpi_sub_ui(h, h, 1); cp->pk_len_bin = get_serialization_len(h, DF_BIN); cp->pk_len_compact = get_serialization_len(h, DF_COMPACT); gcry_mpi_mul(h, dp->order, dp->order); gcry_mpi_sub_ui(h, h, 1); cp->sig_len_bin = get_serialization_len(h, DF_BIN); cp->sig_len_compact = get_serialization_len(h, DF_COMPACT); cp->dh_len_bin = (gcry_mpi_get_nbits(dp->order) / 2 + 7) / 8; if (cp->dh_len_bin > 32) cp->dh_len_bin = 32; gcry_mpi_set_ui(h, 1); /* gcry_mpi_set_bit doesn't work properly */ gcry_mpi_mul_2exp(h, h, 8 * cp->dh_len_bin); /* in libgcrypt-1.2.2 */ gcry_mpi_sub_ui(h, h, 1); cp->dh_len_compact = get_serialization_len(h, DF_COMPACT); cp->elem_len_bin = get_serialization_len(dp->m, DF_BIN); #if 0 if (cp->pk_len_compact != c->pk_len_compact) fprintf(stderr, "FATAL: c->pk_len_compact != %d!\n", cp->pk_len_compact); assert(point_on_curve(&dp->base, dp)); struct affine_point p = pointmul(&dp->base, dp->order, dp); assert(point_is_zero(&p)); point_release(&p); #endif gcry_mpi_release(h); return cp; } struct curve_params* curve_by_name(const char *name) { const struct curve *c = curves; int i; for(i = 0; i < CURVE_NUM; i++, c++) if (strstr(c->name, name)) return load_curve(c); return NULL; } struct curve_params* curve_by_pk_len_compact(int len) { const struct curve *c = curves; int i; for(i = 0; i < CURVE_NUM; i++, c++) if (c->pk_len_compact == len) return load_curve(c); return NULL; } void curve_release(struct curve_params *cp) { struct domain_params *dp = &cp->dp; gcry_mpi_release(dp->a); gcry_mpi_release(dp->b); gcry_mpi_release(dp->m); gcry_mpi_release(dp->order); gcry_mpi_release(dp->base.x); gcry_mpi_release(dp->base.y); } seccure-0.3/HISTORY0000644000175100001440000000033610470676205013456 0ustar bertramusersv0.3: (Aug 2006) - added signcryption as shortcut for signing and subsequent encryption - added -a mode (inline signature) v0.2: (Aug 2006) - added Diffie-Hellman key exchange v0.1: (July 2006) - initial release seccure-0.3/LICENSE0000644000175100001440000004313310470676205013401 0ustar bertramusers GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. seccure-0.3/protocol.h0000644000175100001440000000510010470676205014376 0ustar bertramusers/* * seccure - Copyright 2006 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which is * part of the GnuPG project). The code compiles successfully with * libgcrypt 1.2.2. Use the included Makefile to build the binary. * * Compile with -D NOMEMLOCK if your machine doesn't support memory * locking. * * Report bugs to: seccure AT point-at-infinity.org * */ #ifndef INC_PROTOCOL_H #define INC_PROTOCOL_H #include "curves.h" #include "serialize.h" gcry_mpi_t hash_to_exponent(const char *hash, const struct curve_params *cp); gcry_mpi_t get_random_exponent(const struct curve_params *cp); void compress_to_string(char *buf, enum disp_format df, const struct affine_point *P, const struct curve_params *cp); int decompress_from_string(struct affine_point *P, const char *buf, enum disp_format df, const struct curve_params *cp); gcry_mpi_t ECDSA_sign(const char *msg, const gcry_mpi_t d, const struct curve_params *cp); int ECDSA_verify(const char *msg, const struct affine_point *Q, const gcry_mpi_t sig, const struct curve_params *cp); struct affine_point ECIES_encryption(char *key, const struct affine_point *Q, const struct curve_params *cp); int ECIES_decryption(char *key, const struct affine_point *R, const gcry_mpi_t d, const struct curve_params *cp); gcry_mpi_t DH_step1(struct affine_point *A, const struct curve_params *cp); int DH_step2(char *key, const struct affine_point *B, const gcry_mpi_t exp, const struct curve_params *cp); #endif /* INC_PROTOCOL_H */ seccure-0.3/serialize.c0000644000175100001440000001007110470676205014522 0ustar bertramusers/* * seccure - Copyright 2006 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which is * part of the GnuPG project). The code compiles successfully with * libgcrypt 1.2.2. Use the included Makefile to build the binary. * * Compile with -D NOMEMLOCK if your machine doesn't support memory * locking. * * Report bugs to: seccure AT point-at-infinity.org * */ #include #include #include #include "serialize.h" /******************************************************************************/ const char compact_digits[COMPACT_DIGITS_COUNT] = { '!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', ']', '^', '_', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~' }; int get_serialization_len(const gcry_mpi_t x, enum disp_format df) { int res; switch(df) { case DF_BIN: res = (gcry_mpi_get_nbits(x) + 7) / 8; break; case DF_COMPACT: { gcry_mpi_t base, Q; base = gcry_mpi_set_ui(NULL, COMPACT_DIGITS_COUNT); Q = gcry_mpi_copy(x); for(res = 0; gcry_mpi_cmp_ui(Q, 0); res++) gcry_mpi_div(Q, NULL, Q, base, 0); gcry_mpi_release(base); gcry_mpi_release(Q); } break; default: assert(0); } return res; } void serialize_mpi(char *outbuf, int outlen, enum disp_format df, const gcry_mpi_t x) { switch(df) { case DF_BIN: { int len = (gcry_mpi_get_nbits(x) + 7) / 8; assert(len <= outlen); memset(outbuf, 0, outlen - len); gcry_mpi_print(GCRYMPI_FMT_USG, (unsigned char*)outbuf + (outlen - len), len, NULL, x); } break; case DF_COMPACT: { gcry_mpi_t base, Q, R; int i; base = gcry_mpi_set_ui(NULL, COMPACT_DIGITS_COUNT); Q = gcry_mpi_copy(x); R = gcry_mpi_new(0); for(i = outlen - 1; i >= 0; i--) { unsigned char digit = 0; gcry_mpi_div(Q, R, Q, base, 0); gcry_mpi_print(GCRYMPI_FMT_USG, &digit, 1, NULL, R); assert(digit < COMPACT_DIGITS_COUNT); outbuf[i] = compact_digits[digit]; } assert(! gcry_mpi_cmp_ui(Q, 0)); gcry_mpi_release(base); gcry_mpi_release(Q); gcry_mpi_release(R); } break; default: assert(0); } } int deserialize_mpi(gcry_mpi_t *x, enum disp_format df, const char *buf, int inlen) { switch(df) { case DF_BIN: gcry_mpi_scan(x, GCRYMPI_FMT_USG, buf, inlen, NULL); break; case DF_COMPACT: { char *d; int i; *x = gcry_mpi_new(0); for(i = 0; i < inlen; i++) { if (! (d = memchr(compact_digits, buf[i], COMPACT_DIGITS_COUNT))) { gcry_mpi_release(*x); return 0; } gcry_mpi_mul_ui(*x, *x, COMPACT_DIGITS_COUNT); gcry_mpi_add_ui(*x, *x, d - compact_digits); } } break; default: assert(0); } return 1; } seccure-0.3/Makefile0000644000175100001440000000435610470676205014040 0ustar bertramusersCFLAGS = -O2 # -D NOBEEP # -D NOMEMLOCK default: binaries # doc binaries: seccure-key seccure-encrypt seccure-decrypt seccure-sign \ seccure-verify seccure-signcrypt seccure-veridec seccure-dh doc: seccure.1 seccure.1.html install: default install -m0755 seccure-key $(DESTDIR)/usr/bin ln -f $(DESTDIR)/usr/bin/seccure-key $(DESTDIR)/usr/bin/seccure-encrypt ln -f $(DESTDIR)/usr/bin/seccure-key $(DESTDIR)/usr/bin/seccure-decrypt ln -f $(DESTDIR)/usr/bin/seccure-key $(DESTDIR)/usr/bin/seccure-sign ln -f $(DESTDIR)/usr/bin/seccure-key $(DESTDIR)/usr/bin/seccure-verify ln -f $(DESTDIR)/usr/bin/seccure-key $(DESTDIR)/usr/bin/seccure-signcrypt ln -f $(DESTDIR)/usr/bin/seccure-key $(DESTDIR)/usr/bin/seccure-veridec ln -f $(DESTDIR)/usr/bin/seccure-key $(DESTDIR)/usr/bin/seccure-dh install -m0644 seccure.1 $(DESTDIR)/usr/share/man/man1 clean: rm -f *.o seccure-key seccure-encrypt seccure-decrypt seccure-sign \ seccure-verify seccure-signcrypt seccure-veridec \ seccure-dh # seccure.1 seccure.1.html rebuild: clean default seccure-key: seccure.o numtheory.o ecc.o serialize.o protocol.o curves.o aes256ctr.o $(CC) $(CFLAGS) -o seccure-key -lgcrypt seccure.o numtheory.o ecc.o \ curves.o serialize.o protocol.o aes256ctr.o seccure-encrypt: seccure-key ln -f seccure-key seccure-encrypt seccure-decrypt: seccure-key ln -f seccure-key seccure-decrypt seccure-sign: seccure-key ln -f seccure-key seccure-sign seccure-verify: seccure-key ln -f seccure-key seccure-verify seccure-signcrypt: seccure-key ln -f seccure-key seccure-signcrypt seccure-veridec: seccure-key ln -f seccure-key seccure-veridec seccure-dh: seccure-key ln -f seccure-key seccure-dh seccure.o: seccure.c $(CC) $(CFLAGS) -c seccure.c numtheory.o: numtheory.c numtheory.h $(CC) $(CFLAGS) -c numtheory.c ecc.o: ecc.c ecc.h $(CC) $(CFLAGS) -c ecc.c curves.o: curves.c curves.h $(CC) $(CFLAGS) -c curves.c serialize.o: serialize.c serialize.h $(CC) $(CFLAGS) -c serialize.c protocol.o: protocol.c protocol.h $(CC) $(CFLAGS) -c protocol.c aes256ctr.o: aes256ctr.c aes256ctr.h $(CC) $(CFLAGS) -c aes256ctr.c seccure.1: seccure.manpage.xml xmltoman seccure.manpage.xml > seccure.1 seccure.1.html: seccure.manpage.xml xmlmantohtml seccure.manpage.xml > seccure.1.html seccure-0.3/numtheory.c0000644000175100001440000000750510470676205014575 0ustar bertramusers/* * seccure - Copyright 2006 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which is * part of the GnuPG project). The code compiles successfully with * libgcrypt 1.2.2. Use the included Makefile to build the binary. * * Compile with -D NOMEMLOCK if your machine doesn't support memory * locking. * * Report bugs to: seccure AT point-at-infinity.org * */ #include #include "numtheory.h" /******************************************************************************/ /* Even though this behaviour is not documented the gcry_mpi_rshift function seems to always interpret the N parameter as N % 32. Therefore an rshift by 32, 64 or 96 positions is practically a NOP. The following function + macro is a quick hack to fix this problem. */ void my_gcry_mpi_rshift(gcry_mpi_t X, gcry_mpi_t A, unsigned int N) { gcry_mpi_set(X, A); while(N--) gcry_mpi_rshift(X, X, 1); } #define gcry_mpi_rshift(X, A, N) my_gcry_mpi_rshift(X, A, N) /******************************************************************************/ /* Fact 2.146(i) in the "Handbook of Applied Cryptography" */ int mod_issquare(const gcry_mpi_t a, const gcry_mpi_t p) { if (gcry_mpi_cmp_ui(a, 0)) { gcry_mpi_t p1, p2; int res; p1 = gcry_mpi_new(0); p2 = gcry_mpi_new(0); gcry_mpi_rshift(p1, p, 1); gcry_mpi_powm(p2, a, p1, p); res = ! gcry_mpi_cmp_ui(p2, 1); gcry_mpi_release(p1); gcry_mpi_release(p2); return res; } else return 1; } /* Algorithm II.8 in "Elliptic Curves in Cryptography" */ int mod_root(gcry_mpi_t x, const gcry_mpi_t a, const gcry_mpi_t p) { gcry_mpi_t h, n, q, y, b, t; int r, m; if (! gcry_mpi_cmp_ui(a, 0)) { gcry_mpi_set_ui(x, 0); return 1; } if (! mod_issquare(a, p)) return 0; h = gcry_mpi_new(0); n = gcry_mpi_new(0); gcry_mpi_set_ui(n, 2); while (mod_issquare(n, p)) gcry_mpi_add_ui(n, n, 1); q = gcry_mpi_new(0); gcry_mpi_sub_ui(q, p, 1); for(r = 0; ! gcry_mpi_test_bit(q, r); r++); gcry_mpi_rshift(q, q, r); y = gcry_mpi_new(0); gcry_mpi_powm(y, n, q, p); b = gcry_mpi_new(0); gcry_mpi_rshift(h, q, 1); gcry_mpi_powm(b, a, h, p); gcry_mpi_mulm(x, a, b, p); gcry_mpi_mulm(b, b, x, p); t = gcry_mpi_new(0); while (gcry_mpi_cmp_ui(b, 1)) { gcry_mpi_mulm(h, b, b, p); for(m = 1; gcry_mpi_cmp_ui(h, 1); m++) gcry_mpi_mulm(h, h, h, p); gcry_mpi_set_ui(h, 0); gcry_mpi_set_bit(h, r - m - 1); gcry_mpi_powm(t, y, h, p); gcry_mpi_mulm(y, t, t, p); r = m; gcry_mpi_mulm(x, x, t, p); gcry_mpi_mulm(b, b, y, p); } gcry_mpi_release(h); gcry_mpi_release(n); gcry_mpi_release(q); gcry_mpi_release(y); gcry_mpi_release(b); gcry_mpi_release(t); return 1; } seccure-0.3/serialize.h0000644000175100001440000000357210470676205014537 0ustar bertramusers/* * seccure - Copyright 2006 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which is * part of the GnuPG project). The code compiles successfully with * libgcrypt 1.2.2. Use the included Makefile to build the binary. * * Compile with -D NOMEMLOCK if your machine doesn't support memory * locking. * * Report bugs to: seccure AT point-at-infinity.org * */ #ifndef INC_SERIALIZE_H #define INC_SERIALIZE_H #include enum disp_format { DF_BIN, DF_COMPACT }; #define COMPACT_DIGITS_COUNT 90 extern const char compact_digits[]; int get_serialization_len(const gcry_mpi_t x, enum disp_format df); void serialize_mpi(char *outbuf, int outlen, enum disp_format df, const gcry_mpi_t x); int deserialize_mpi(gcry_mpi_t *x, enum disp_format ds, const char *buf, int inlen); #endif /* INC_SERIALIZE_H */ seccure-0.3/numtheory.h0000644000175100001440000000321410470676205014573 0ustar bertramusers/* * seccure - Copyright 2006 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which is * part of the GnuPG project). The code compiles successfully with * libgcrypt 1.2.2. Use the included Makefile to build the binary. * * Compile with -D NOMEMLOCK if your machine doesn't support memory * locking. * * Report bugs to: seccure AT point-at-infinity.org * */ #ifndef INC_NUMTHEORY_H #define INC_NUMTHEORY_H #include int mod_issquare(const gcry_mpi_t a, const gcry_mpi_t p); int mod_root(gcry_mpi_t x, const gcry_mpi_t a, const gcry_mpi_t p); #endif /* INC_NUMTHEORY_H */ seccure-0.3/ecc.c0000644000175100001440000002571010470676205013273 0ustar bertramusers/* * seccure - Copyright 2006 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which is * part of the GnuPG project). The code compiles successfully with * libgcrypt 1.2.2. Use the included Makefile to build the binary. * * Compile with -D NOMEMLOCK if your machine doesn't support memory * locking. * * Report bugs to: seccure AT point-at-infinity.org * */ #include #include #include "ecc.h" #include "numtheory.h" /******************************************************************************/ /* Chapter 3.1.2 in the "Guide to Elliptic Curve Cryptography" */ struct affine_point point_new(void) { struct affine_point r; r.x = gcry_mpi_new(0); r.y = gcry_mpi_new(0); return r; } void point_release(struct affine_point *p) { gcry_mpi_release(p->x); gcry_mpi_release(p->y); } void point_set(struct affine_point *p1, const struct affine_point *p2) { gcry_mpi_set(p1->x, p2->x); gcry_mpi_set(p1->y, p2->y); } void point_load_zero(struct affine_point *p) { gcry_mpi_set_ui(p->x, 0); gcry_mpi_set_ui(p->y, 0); } int point_is_zero(const struct affine_point *p) { return ! gcry_mpi_cmp_ui(p->x, 0) && ! gcry_mpi_cmp_ui(p->y, 0); } int point_on_curve(const struct affine_point *p, const struct domain_params *dp) { int res; if (! (res = point_is_zero(p))) { gcry_mpi_t h1, h2; h1 = gcry_mpi_new(0); h2 = gcry_mpi_new(0); gcry_mpi_mulm(h1, p->x, p->x, dp->m); gcry_mpi_mulm(h1, h1, p->x, dp->m); gcry_mpi_mulm(h2, dp->a, p->x, dp->m); gcry_mpi_addm(h1, h2, h1, dp->m); gcry_mpi_addm(h1, h1, dp->b, dp->m); gcry_mpi_mulm(h2, p->y, p->y, dp->m); res = ! gcry_mpi_cmp(h1, h2); gcry_mpi_release(h1); gcry_mpi_release(h2); } return res; } int point_compress(const struct affine_point *p) { return gcry_mpi_test_bit(p->y, 0); } int point_decompress(struct affine_point *p, const gcry_mpi_t x, int yflag, const struct domain_params *dp) { gcry_mpi_t h1, h2; int res; h1 = gcry_mpi_new(0); h2 = gcry_mpi_new(0); gcry_mpi_mulm(h1, x, x, dp->m); gcry_mpi_mulm(h1, h1, x, dp->m); gcry_mpi_mulm(h2, dp->a, x, dp->m); gcry_mpi_addm(h1, h1, h2, dp->m); gcry_mpi_addm(h1, h1, dp->b, dp->m); if ((res = mod_root(h2, h1, dp->m))) if ((res = (gcry_mpi_cmp_ui(h2, 0) || ! yflag))) { if (yflag != gcry_mpi_test_bit(h2, 0)) gcry_mpi_sub(h2, dp->m, h2); p->x = gcry_mpi_copy(x); p->y = gcry_mpi_copy(h2); assert(point_on_curve(p, dp)); } gcry_mpi_release(h1); gcry_mpi_release(h2); return res; } void point_double(struct affine_point *p, const struct domain_params *dp) { if (gcry_mpi_cmp_ui(p->y, 0)) { gcry_mpi_t t1, t2; t1 = gcry_mpi_new(0); t2 = gcry_mpi_new(0); gcry_mpi_mulm(t2, p->x, p->x, dp->m); gcry_mpi_addm(t1, t2, t2, dp->m); gcry_mpi_addm(t1, t1, t2, dp->m); gcry_mpi_addm(t1, t1, dp->a, dp->m); gcry_mpi_addm(t2, p->y, p->y, dp->m); gcry_mpi_invm(t2, t2, dp->m); gcry_mpi_mulm(t1, t1, t2, dp->m); gcry_mpi_mulm(t2, t1, t1, dp->m); gcry_mpi_subm(t2, t2, p->x, dp->m); gcry_mpi_subm(t2, t2, p->x, dp->m); gcry_mpi_subm(p->x, p->x, t2, dp->m); gcry_mpi_mulm(t1, t1, p->x, dp->m); gcry_mpi_subm(p->y, t1, p->y, dp->m); gcry_mpi_set(p->x, t2); gcry_mpi_release(t1); gcry_mpi_release(t2); } else gcry_mpi_set_ui(p->x, 0); } void point_add(struct affine_point *p1, const struct affine_point *p2, const struct domain_params *dp) { if (! point_is_zero(p2)) { if (! point_is_zero(p1)) { if (! gcry_mpi_cmp(p1->x, p2->x)) { if (! gcry_mpi_cmp(p1->y, p2->y)) point_double(p1, dp); else point_load_zero(p1); } else { gcry_mpi_t t; t = gcry_mpi_new(0); gcry_mpi_subm(t, p1->y, p2->y, dp->m); gcry_mpi_subm(p1->y, p1->x, p2->x, dp->m); gcry_mpi_invm(p1->y, p1->y, dp->m); gcry_mpi_mulm(p1->y, t, p1->y, dp->m); gcry_mpi_mulm(t, p1->y, p1->y, dp->m); gcry_mpi_addm(p1->x, p1->x, p2->x, dp->m); gcry_mpi_subm(p1->x, t, p1->x, dp->m); gcry_mpi_subm(t, p2->x, p1->x, dp->m); gcry_mpi_mulm(p1->y, p1->y, t, dp->m); gcry_mpi_subm(p1->y, p1->y, p2->y, dp->m); gcry_mpi_release(t); } } else point_set(p1, p2); } } /******************************************************************************/ /* Chapter 3.2.2 in the "Guide to Elliptic Curve Cryptography" */ struct jacobian_point jacobian_new(void) { struct jacobian_point r; r.x = gcry_mpi_new(0); r.y = gcry_mpi_new(0); r.z = gcry_mpi_new(0); return r; } void jacobian_release(struct jacobian_point *p) { gcry_mpi_release(p->x); gcry_mpi_release(p->y); gcry_mpi_release(p->z); } void jacobian_load_affine(struct jacobian_point *p1, const struct affine_point *p2) { if (! point_is_zero(p2)) { gcry_mpi_set(p1->x, p2->x); gcry_mpi_set(p1->y, p2->y); gcry_mpi_set_ui(p1->z, 1); } else gcry_mpi_set_ui(p1->z, 0); } void jacobian_load_zero(struct jacobian_point *p) { gcry_mpi_set_ui(p->z, 0); } int jacobian_is_zero(const struct jacobian_point *p) { return ! gcry_mpi_cmp_ui(p->z, 0); } void jacobian_double(struct jacobian_point *p, const struct domain_params *dp) { if (gcry_mpi_cmp_ui(p->z, 0)) { if (gcry_mpi_cmp_ui(p->y, 0)) { gcry_mpi_t t1, t2; t1 = gcry_mpi_new(0); t2 = gcry_mpi_new(0); gcry_mpi_mulm(t1, p->x, p->x, dp->m); gcry_mpi_addm(t2, t1, t1, dp->m); gcry_mpi_addm(t2, t2, t1, dp->m); gcry_mpi_mulm(t1, p->z, p->z, dp->m); gcry_mpi_mulm(t1, t1, t1, dp->m); gcry_mpi_mulm(t1, t1, dp->a, dp->m); gcry_mpi_addm(t1, t1, t2, dp->m); gcry_mpi_mulm(p->z, p->z, p->y, dp->m); gcry_mpi_addm(p->z, p->z, p->z, dp->m); gcry_mpi_mulm(p->y, p->y, p->y, dp->m); gcry_mpi_addm(p->y, p->y, p->y, dp->m); gcry_mpi_mulm(t2, p->x, p->y, dp->m); gcry_mpi_addm(t2, t2, t2, dp->m); gcry_mpi_mulm(p->x, t1, t1, dp->m); gcry_mpi_subm(p->x, p->x, t2, dp->m); gcry_mpi_subm(p->x, p->x, t2, dp->m); gcry_mpi_subm(t2, t2, p->x, dp->m); gcry_mpi_mulm(t1, t1, t2, dp->m); gcry_mpi_mulm(t2, p->y, p->y, dp->m); gcry_mpi_addm(t2, t2, t2, dp->m); gcry_mpi_subm(p->y, t1, t2, dp->m); gcry_mpi_release(t1); gcry_mpi_release(t2); } else gcry_mpi_set_ui(p->z, 0); } } void jacobian_affine_point_add(struct jacobian_point *p1, const struct affine_point *p2, const struct domain_params *dp) { if (! point_is_zero(p2)) { if (gcry_mpi_cmp_ui(p1->z, 0)) { gcry_mpi_t t1, t2, t3; t1 = gcry_mpi_new(0); t2 = gcry_mpi_new(0); gcry_mpi_mulm(t1, p1->z, p1->z, dp->m); gcry_mpi_mulm(t2, t1, p2->x, dp->m); gcry_mpi_mulm(t1, t1, p1->z, dp->m); gcry_mpi_mulm(t1, t1, p2->y, dp->m); if (! gcry_mpi_cmp(p1->x, t2)) { if (! gcry_mpi_cmp(p1->y, t1)) jacobian_double(p1, dp); else jacobian_load_zero(p1); } else { t3 = gcry_mpi_new(0); gcry_mpi_subm(p1->x, p1->x, t2, dp->m); gcry_mpi_subm(p1->y, p1->y, t1, dp->m); gcry_mpi_mulm(p1->z, p1->z, p1->x, dp->m); gcry_mpi_mulm(t3, p1->x, p1->x, dp->m); gcry_mpi_mulm(t2, t2, t3, dp->m); gcry_mpi_mulm(t3, t3, p1->x, dp->m); gcry_mpi_mulm(t1, t1, t3, dp->m); gcry_mpi_mulm(p1->x, p1->y, p1->y, dp->m); gcry_mpi_subm(p1->x, p1->x, t3, dp->m); gcry_mpi_subm(p1->x, p1->x, t2, dp->m); gcry_mpi_subm(p1->x, p1->x, t2, dp->m); gcry_mpi_subm(t2, t2, p1->x, dp->m); gcry_mpi_mulm(p1->y, p1->y, t2, dp->m); gcry_mpi_subm(p1->y, p1->y, t1, dp->m); gcry_mpi_release(t3); } gcry_mpi_release(t1); gcry_mpi_release(t2); } else jacobian_load_affine(p1, p2); } } struct affine_point jacobian_to_affine(const struct jacobian_point *p, const struct domain_params *dp) { struct affine_point r = point_new(); if (gcry_mpi_cmp_ui(p->z, 0)) { gcry_mpi_t h; h = gcry_mpi_new(0); gcry_mpi_invm(h, p->z, dp->m); gcry_mpi_mulm(r.y, h, h, dp->m); gcry_mpi_mulm(r.x, p->x, r.y, dp->m); gcry_mpi_mulm(r.y, r.y, h, dp->m); gcry_mpi_mulm(r.y, r.y, p->y, dp->m); gcry_mpi_release(h); } return r; } /******************************************************************************/ /* Algorithm 3.27 in the "Guide to Elliptic Curve Cryptography" */ #if 0 struct affine_point pointmul(const struct affine_point *p, const gcry_mpi_t exp, const struct domain_params *dp) { struct affine_point r = point_new(); int n = gcry_mpi_get_nbits(exp); while (n) { point_double(&r, dp); if (gcry_mpi_test_bit(exp, --n)) point_add(&r, p, dp); } assert(point_on_curve(&r, dp)); return r; } #else struct affine_point pointmul(const struct affine_point *p, const gcry_mpi_t exp, const struct domain_params *dp) { struct jacobian_point r = jacobian_new(); struct affine_point R; int n = gcry_mpi_get_nbits(exp); while (n) { jacobian_double(&r, dp); if (gcry_mpi_test_bit(exp, --n)) jacobian_affine_point_add(&r, p, dp); } R = jacobian_to_affine(&r, dp); jacobian_release(&r); assert(point_on_curve(&R, dp)); return R; } #endif /******************************************************************************/ /* Algorithm 4.26 in the "Guide to Elliptic Curve Cryptography" */ int embedded_key_validation(const struct affine_point *p, const struct domain_params *dp) { if (gcry_mpi_cmp_ui(p->x, 0) < 0 || gcry_mpi_cmp(p->x, dp->m) >= 0 || gcry_mpi_cmp_ui(p->y, 0) < 0 || gcry_mpi_cmp(p->y, dp->m) >= 0) return 0; return ! point_is_zero(p) && point_on_curve(p, dp); } /* Algorithm 4.25 in the "Guide to Elliptic Curve Cryptography" */ int full_key_validation(const struct affine_point *p, const struct domain_params *dp) { if (! embedded_key_validation(p, dp)) return 0; if (dp->cofactor != 1) { struct affine_point bp; int res; bp = pointmul(p, dp->order, dp); res = point_is_zero(&bp); point_release(&bp); return res; } else return 1; } seccure-0.3/ecc.h0000644000175100001440000000644610470676205013305 0ustar bertramusers/* * seccure - Copyright 2006 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which is * part of the GnuPG project). The code compiles successfully with * libgcrypt 1.2.2. Use the included Makefile to build the binary. * * Compile with -D NOMEMLOCK if your machine doesn't support memory * locking. * * Report bugs to: seccure AT point-at-infinity.org * */ #ifndef INC_ECC_H #define INC_ECC_H #include struct affine_point { gcry_mpi_t x, y; }; struct jacobian_point { gcry_mpi_t x, y, z; }; struct domain_params { gcry_mpi_t a, b, m, order; struct affine_point base; int cofactor; }; struct affine_point point_new(void); void point_release(struct affine_point *p); void point_set(struct affine_point *p1, const struct affine_point *p2); void point_load_zero(struct affine_point *p); int point_is_zero(const struct affine_point *p); int point_on_curve(const struct affine_point *p, const struct domain_params *dp); int point_compress(const struct affine_point *p); int point_decompress(struct affine_point *p, const gcry_mpi_t x, int yflag, const struct domain_params *dp); void point_double(struct affine_point *p, const struct domain_params *dp); void point_add(struct affine_point *p1, const struct affine_point *p2, const struct domain_params *dp); struct jacobian_point jacobian_new(void); void jacobian_release(struct jacobian_point *p); void jacobian_load_affine(struct jacobian_point *p1, const struct affine_point *p2); void jacobian_load_zero(struct jacobian_point *p); int jacobian_is_zero(const struct jacobian_point *p); void jacobian_double(struct jacobian_point *p, const struct domain_params *dp); void jacobian_affine_point_add(struct jacobian_point *p1, const struct affine_point *p2, const struct domain_params *dp); struct affine_point jacobian_to_affine(const struct jacobian_point *p, const struct domain_params *dp); struct affine_point pointmul(const struct affine_point *p, const gcry_mpi_t exp, const struct domain_params *dp); int embedded_key_validation(const struct affine_point *p, const struct domain_params *dp); int full_key_validation(const struct affine_point *p, const struct domain_params *dp); #endif /* INC_ECC_H */ seccure-0.3/seccure.10000644000175100001440000001603510470676205014110 0ustar bertramusers.TH seccure 1 User Manuals .SH NAME seccure \- SECCURE Elliptic Curve Crypto Utility for Reliable Encryption .SH SYNOPSIS \fBseccure-key [-c \fIcurve\fB] [-F \fIpwfile\fB] [-d] [-v] [-q] seccure-encrypt [-m \fImaclen\fB] [-c \fIcurve\fB] [-i \fIinfile\fB] [-o \fIoutfile\fB] [-v] [-q] \fIkey\fB seccure-decrypt [-m \fImaclen\fB] [-c \fIcurve\fB] [-i \fIinfile\fB] [-o \fIoutfile\fB] [-F \fIpwfile\fB] [-d] [-v] [-q] seccure-sign [-f] [-b] [-a] [-c \fIcurve\fB] [-s \fIsigfile\fB] [-i \fIinfile\fB] [-o \fIoutfile\fB] [-F \fIpwfile\fB] [-d] [-v] [-q] seccure-verify [-f] [-b] [-a] [-c \fIcurve\fB] [-s \fIsigfile\fB] [-i \fIinfile\fB] [-o \fIoutfile\fB] [-v] [-q] \fIkey\fB [\fIsig\fB] seccure-signcrypt [-c \fIsig_curve\fB [-c \fIenc_curve\fB]] [-i \fIinfile\fB] [-o \fIoutfile\fB] [-F \fIpwfile\fB] [-d] [-v] [-q] \fIkey\fB seccure-veridec [-c \fIenc_curve\fB [-c \fIsig_curve\fB]] [-i \fIinfile\fB] [-o \fIoutfile\fB] [-F \fIpwfile\fB] [-d] [-v] [-q] \fIkey\fB seccure-dh [-c \fIcurve\fB] [-v] [-q] \f1 .SH DESCRIPTION The \fBseccure\f1 toolset implements a selection of asymmetric algorithms based on elliptic curve cryptography (ECC). In particular it offers public key encryption / decryption, signature generation / verification and key establishment. ECC schemes offer a much better key size to security ratio than classical systems (RSA, DSA). Keys are short enough to make direct specification of keys on the command line possible (sometimes this is more convenient than the management of PGP-like key rings). \fBseccure\f1 builds on this feature and therefore is the tool of choice whenever lightweight asymmetric cryptography -- independent of key servers, revocation certificates, the Web of Trust or even configuration files -- is required. .SH COMMANDS \fBseccure-key\f1: Prompt for a passphrase and calculate the corresponding public key. \fBseccure-encrypt\f1: Encrypt a message with public key \fIkey\f1. \fBseccure-decrypt\f1: Prompt for a passphrase and decrypt a \fBseccure-encrypt\f1ed message. \fBseccure-sign\f1: Prompt for a passphrase and digitally sign a message. \fBseccure-verify\f1: Verify signature \fIsig\f1 with public key \fIkey\f1. \fBseccure-signcrypt\f1: Sign a message first, encrypt it subsequently (in \fB-b -a\f1 and \fB-m 0\f1 mode, respectively). This is basically a shortcut for two separate \fBseccure\f1 invocations. \fBseccure-veridec\f1: Counterpart to signcryption. \fBseccure-dh\f1: Perform a Diffie-Hellman key exchange. .SH OPTIONS .TP \fB-c \fIcurve\fB\f1 Use elliptic curve \fIcurve\f1. Available are: \fIsecp112r1\f1, \fIsecp128r1\f1, \fIsecp160r1\f1, \fIsecp192r1/nistp192\f1, \fIsecp224r1/nistp224\f1, \fIsecp256r1/nistp256\f1, \fIsecp384r1/nistp384\f1 and \fIsecp521r1/nistp521\f1. The curve name may be abbreviated by any non-ambiguous substring (for instance it is suggested to specify \fIp224\f1 for the \fIsecp224r1/nistp224\f1 curve). The default curve is \fIp160\f1, which provides reasonable security for everyday use. (See also \fBHOW TO CHOOSE THE CURVE\f1.) Note: If a public key is given on the command line \fBseccure\f1 can determine the corresponding curve on its own. It's then unnecessary to specify the curve explicitly. .TP \fB-F \fIpwfile\fB\f1 Don't prompt for a passphrase; instead, take the first text line of \fIpwfile\f1. .TP \fB-m \fImaclen\fB\f1 Set the MAC length to \fImaclen\f1 bits. Only multiples of 8 in the range from 0 to 256 are allowed. The default MAC length is 80 bits, which provides a reasonable level of integrity protection for everyday use. .TP \fB-i \fIinfile\fB\f1 Read from \fIinfile\f1 instead of STDIN. .TP \fB-o \fIoutfile\fB\f1 Write to \fIoutfile\f1 instead of STDOUT. .TP \fB-s \fIsigfile\fB\f1 For \fBseccure-sign\f1: Write signature to \fIsigfile\f1 instead of STDERR. For \fBseccure-verify\f1: Read signature from \fIsigfile\f1 instead of using \fIsig\f1. .TP \fB-f\f1 Filter mode: Copy all data read from STDIN verbatim to STDOUT (eventually attaching or detaching a signature in \fB-a\f1 mode). .TP \fB-b\f1 Binary mode: Read/write signatures as binary strings. This leads to very compact signatures. .TP \fB-a\f1 Append mode: For \fBseccure-sign\f1: Append signature to the end of the document. Enforces \fB-f\f1 mode. For \fBseccure-verify\f1: Detach signature from the end of the document. .TP \fB-d\f1 Double prompt mode: When reading a passphrase from the console: prompt twice and assure the phrases are the same. .TP \fB-v\f1 Verbose mode: Print some extra information. .TP \fB-q\f1 Quiet mode: Disable all unnecessary output. .SH EXAMPLE Given the passphrase 'seccure is secure', run \fBseccure-key\f1 to determine the corresponding public key (which is '2@DupCaCKykHBe-QHpAP%d%B[' on curve \fIp160\f1). To encrypt the file 'document.msg' with that key run \fBseccure-encrypt -i document.msg -o document.enc '2@DupCaCKykHBe-QHpAP%d%B['\f1 The message can be recovered with \fBseccure-decrypt -i document.enc\f1 To sign the file run \fBseccure-sign -i document.msg -s document.sig\f1 and enter the passphrase. The signature is stored in 'document.sig' and can be verified with \fBseccure-verify -i document.msg -s document.sig '2@DupCaCKykHBe-QHpAP%d%B['\f1 .SH KEY ESTABLISHMENT \fBseccure-dh\f1 performs an interactive Diffie-Hellman key exchange. Two instances have to be run in parallel; the token generated by the first one is the input for the second one and vice versa. The output consists of two shared keys: it is guaranteed that no attacker can ever find out the established key as soon as the two parties can confirm that both have the same verification key. The authentic comparision of the verification keys can, for example, be realized via signed messages or via telephone (using 'voice authentication'). .SH HOW TO CHOOSE THE CURVE The number in the names of the curves measures their security level. Rule of thumb: the workload to 'break' a k-bit curve is 2^(k/2) approximately (example: it takes about 2^112 steps to break \fIsecp224r1\f1). If the 80 bit security of the default curve doesn't seem sufficient, choosing a stronger curve (\fIp192\f1 and upwards) may, of course, be considered. But the suggestion remains: \fIp160\f1 offers reasonable security for everyday use. \fBWarning:\f1 the curves \fIp112\f1 and \fIp128\f1 do not satisfy demands for long-time security. .SH ALGORITHMS \fBseccure\f1 uses derivated versions of ECIES (Elliptic Curve Integrated Encryption Scheme), ECDSA (Elliptic Curve Digital Signature Algorithm) and ECDH (Elliptic Curve Diffie-Hellman) as encryption, signature and key establishment scheme, respectively. For the symmetric parts (bulk encryption, hashing, key derivation, HMAC calculation) \fBseccure\f1 builds on AES256 (in CTR mode), SHA256 and SHA512. To my best knowledge no part of \fBseccure\f1 is covered by patents. See the file PATENTS for an explicit patent statement. .SH AUTHOR This software (v0.3) was written by B. Poettering (seccure AT point-at-infinity.org) in 2006. It is released under the terms of the GNU General Public License (GPL). Find the latest version of \fBseccure\f1 on the project's homepage: \fBhttp://point-at-infinity.org/seccure/\f1. seccure-0.3/protocol.c0000644000175100001440000002005710470676205014401 0ustar bertramusers/* * seccure - Copyright 2006 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which is * part of the GnuPG project). The code compiles successfully with * libgcrypt 1.2.2. Use the included Makefile to build the binary. * * Compile with -D NOMEMLOCK if your machine doesn't support memory * locking. * * Report bugs to: seccure AT point-at-infinity.org * */ #include #include #include "ecc.h" #include "curves.h" #include "serialize.h" #include "protocol.h" #include "aes256ctr.h" /******************************************************************************/ gcry_mpi_t hash_to_exponent(const char *hash, const struct curve_params *cp) { int bits = gcry_mpi_get_nbits(cp->dp.order); int buflen = (bits + 7) / 8; struct aes256ctr *ac; gcry_mpi_t a, b; char buf[buflen]; memset(buf, 0, buflen); assert((ac = aes256ctr_init(hash))); aes256ctr_enc(ac, buf, buflen); aes256ctr_done(ac); gcry_mpi_scan(&a, GCRYMPI_FMT_USG, buf, buflen, NULL); memset(buf, 0, buflen); b = gcry_mpi_new(0); gcry_mpi_sub_ui(b, cp->dp.order, 1); gcry_mpi_mod(a, a, b); gcry_mpi_add_ui(a, a, 1); gcry_mpi_release(b); return a; } gcry_mpi_t get_random_exponent(const struct curve_params *cp) { int bits = gcry_mpi_get_nbits(cp->dp.order); gcry_mpi_t a; a = gcry_mpi_new(0); do { gcry_mpi_randomize(a, bits, GCRY_STRONG_RANDOM); gcry_mpi_clear_highbit(a, bits); } while (! gcry_mpi_cmp_ui(a, 0) || gcry_mpi_cmp(a, cp->dp.order) >= 0); return a; } /******************************************************************************/ void compress_to_string(char *buf, enum disp_format df, const struct affine_point *P, const struct curve_params *cp) { int outlen = (df == DF_COMPACT) ? cp->pk_len_compact : cp->pk_len_bin; if (point_compress(P)) { gcry_mpi_t x; x = gcry_mpi_new(0); gcry_mpi_add(x, P->x, cp->dp.m); serialize_mpi(buf, outlen, df, x); gcry_mpi_release(x); } else serialize_mpi(buf, outlen, df, P->x); } int decompress_from_string(struct affine_point *P, const char *buf, enum disp_format df, const struct curve_params *cp) { gcry_mpi_t x; int inlen = (df == DF_COMPACT) ? cp->pk_len_compact : cp->pk_len_bin; int res; assert(df != DF_COMPACT || inlen == strlen(buf)); if ((res = deserialize_mpi(&x, df, buf, inlen))) { int yflag; if ((yflag = (gcry_mpi_cmp(x, cp->dp.m) >= 0))) gcry_mpi_sub(x, x, cp->dp.m); res = gcry_mpi_cmp_ui(x, 0) >= 0 && gcry_mpi_cmp(x, cp->dp.m) < 0 && point_decompress(P, x, yflag, &cp->dp); gcry_mpi_release(x); } return res; } /******************************************************************************/ /* Algorithms 4.29 and 4.30 in the "Guide to Elliptic Curve Cryptography" */ gcry_mpi_t ECDSA_sign(const char *msg, const gcry_mpi_t d, const struct curve_params *cp) { struct affine_point p1; gcry_mpi_t e, k, r, s; r = gcry_mpi_new(0); s = gcry_mpi_new(0); Step1: k = get_random_exponent(cp); p1 = pointmul(&cp->dp.base, k, &cp->dp); gcry_mpi_mod(r, p1.x, cp->dp.order); point_release(&p1); if (! gcry_mpi_cmp_ui(r, 0)) { gcry_mpi_release(k); goto Step1; } gcry_mpi_scan(&e, GCRYMPI_FMT_USG, msg, 64, NULL); gcry_mpi_mod(e, e, cp->dp.order); gcry_mpi_mulm(s, d, r, cp->dp.order); gcry_mpi_addm(s, s, e, cp->dp.order); gcry_mpi_invm(e, k, cp->dp.order); gcry_mpi_mulm(s, s, e, cp->dp.order); gcry_mpi_release(e); gcry_mpi_release(k); if (! gcry_mpi_cmp_ui(s, 0)) goto Step1; gcry_mpi_mul(s, s, cp->dp.order); gcry_mpi_add(s, s, r); gcry_mpi_release(r); return s; } int ECDSA_verify(const char *msg, const struct affine_point *Q, const gcry_mpi_t sig, const struct curve_params *cp) { gcry_mpi_t e, r, s; struct affine_point X1, X2; int res; r = gcry_mpi_new(0); s = gcry_mpi_new(0); gcry_mpi_div(s, r, sig, cp->dp.order, 0); if (gcry_mpi_cmp_ui(s, 0) <= 0 || gcry_mpi_cmp(s, cp->dp.order) >= 0 || gcry_mpi_cmp_ui(r, 0) <= 0 || gcry_mpi_cmp(r, cp->dp.order) >= 0) { gcry_mpi_release(r); gcry_mpi_release(s); return 0; } gcry_mpi_scan(&e, GCRYMPI_FMT_USG, msg, 64, NULL); gcry_mpi_mod(e, e, cp->dp.order); gcry_mpi_invm(s, s, cp->dp.order); gcry_mpi_mulm(e, e, s, cp->dp.order); X1 = pointmul(&cp->dp.base, e, &cp->dp); gcry_mpi_mulm(e, r, s, cp->dp.order); X2 = pointmul(Q, e, &cp->dp); point_add(&X1, &X2, &cp->dp); point_release(&X2); gcry_mpi_release(e); if (point_is_zero(&X1)) { gcry_mpi_release(r); gcry_mpi_release(s); point_release(&X1); return 0; } gcry_mpi_mod(s, X1.x, cp->dp.order); res = ! gcry_mpi_cmp(s, r); gcry_mpi_release(r); gcry_mpi_release(s); point_release(&X1); return res; } /******************************************************************************/ /* Algorithms 4.42 and 4.43 in the "Guide to Elliptic Curve Cryptography" */ static void ECIES_KDF(char *key, const gcry_mpi_t Zx, const struct affine_point *R, int elemlen) { char buf[3 * elemlen]; serialize_mpi(buf + 0 * elemlen, elemlen, DF_BIN, Zx); serialize_mpi(buf + 1 * elemlen, elemlen, DF_BIN, R->x); serialize_mpi(buf + 2 * elemlen, elemlen, DF_BIN, R->y); gcry_md_hash_buffer(GCRY_MD_SHA512, key, buf, 3 * elemlen); } struct affine_point ECIES_encryption(char *key, const struct affine_point *Q, const struct curve_params *cp) { struct affine_point Z, R; gcry_mpi_t k; Step1: k = get_random_exponent(cp); R = pointmul(&cp->dp.base, k, &cp->dp); gcry_mpi_mul_ui(k, k, cp->dp.cofactor); Z = pointmul(Q, k, &cp->dp); gcry_mpi_release(k); if (point_is_zero(&Z)) { point_release(&R); point_release(&Z); goto Step1; } ECIES_KDF(key, Z.x, &R, cp->elem_len_bin); point_release(&Z); return R; } int ECIES_decryption(char *key, const struct affine_point *R, const gcry_mpi_t d, const struct curve_params *cp) { struct affine_point Z; gcry_mpi_t e; if (! embedded_key_validation(R, &cp->dp)) return 0; e = gcry_mpi_new(0); gcry_mpi_mul_ui(e, d, cp->dp.cofactor); Z = pointmul(R, e, &cp->dp); gcry_mpi_release(e); if (point_is_zero(&Z)) { point_release(&Z); return 0; } ECIES_KDF(key, Z.x, R, cp->elem_len_bin); point_release(&Z); return 1; } /******************************************************************************/ static void DH_KDF(char *key, const struct affine_point *P, int elemlen) { char buf[2 * elemlen]; serialize_mpi(buf + 0 * elemlen, elemlen, DF_BIN, P->x); serialize_mpi(buf + 1 * elemlen, elemlen, DF_BIN, P->y); gcry_md_hash_buffer(GCRY_MD_SHA512, key, buf, 2 * elemlen); } gcry_mpi_t DH_step1(struct affine_point *A, const struct curve_params *cp) { gcry_mpi_t a; a = get_random_exponent(cp); *A = pointmul(&cp->dp.base, a, &cp->dp); return a; } int DH_step2(char *key, const struct affine_point *B, const gcry_mpi_t exp, const struct curve_params *cp) { struct affine_point P; if (! full_key_validation(B, &cp->dp)) return 0; P = pointmul(B, exp, &cp->dp); DH_KDF(key, &P, cp->elem_len_bin); point_release(&P); return 1; } seccure-0.3/seccure.c0000644000175100001440000007660610470676205014204 0ustar bertramusers/* * seccure - Copyright 2006 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which is * part of the GnuPG project). The code compiles successfully with * libgcrypt 1.2.2. Use the included Makefile to build the binary. * * Compile with -D NOMEMLOCK if your machine doesn't support memory * locking. * * Report bugs to: seccure AT point-at-infinity.org * */ #include #include #include #include #include #include #include #include #include #include #include "curves.h" #include "protocol.h" #include "serialize.h" #include "aes256ctr.h" #define ANSI_CLEAR_LINE "\033[1K\r" #define VERSION "0.3" #define COPYBUF_SIZE (1 << 20) #define DEFAULT_CURVE "p160" #define DEFAULT_MAC_LEN 10 int opt_help = 0; int opt_verbose = 0; int opt_quiet = 0; int opt_sigcopy = 0; int opt_sigbin = 0; int opt_sigappend = 0; int opt_maclen = -1; int opt_dblprompt = 0; char *opt_infile = NULL; char *opt_outfile = NULL; char *opt_curve = NULL; char *opt_curve2 = NULL; char *opt_pwfile = NULL; char *opt_sigfile = NULL; int opt_fdin = STDIN_FILENO; int opt_fdout = STDOUT_FILENO; int opt_fdpw; /******************************************************************************/ void beep_on_terminal(FILE *term) { #if ! NOBEEP if (isatty(fileno(term))) fputc('\a', term); #endif } void fatal(const char *msg) { beep_on_terminal(stderr); fprintf(stderr, "FATAL: %s.\n", msg); exit(1); } void fatal_errno(const char *msg, int err) { beep_on_terminal(stderr); fprintf(stderr, "FATAL: %s: %s.\n", msg, strerror(err)); exit(1); } void fatal_gcrypt(const char *msg, gcry_error_t err) { beep_on_terminal(stderr); fprintf(stderr, "FATAL: %s: %s.\n", msg, gcry_strerror(err)); exit(1); } void print_quiet(const char *msg, int beep) { if (! opt_quiet) { if (beep) beep_on_terminal(stderr); fprintf(stderr, "%s", msg); } } /******************************************************************************/ void write_block(int fd, const char *buf, int len) { ssize_t c; while(len) { if ((c = write(fd, buf, len)) < 0) fatal_errno("Write error", errno); buf += c; len -= c; } } int read_block(int fd, char *buf, int len) { ssize_t c; while(len) { if ((c = read(fd, buf, len)) < 0) fatal_errno("Read error", errno); if (c == 0) return 0; buf += c; len -= c; } return 1; } void encryption_loop(int fdin, int fdout, struct aes256ctr *ac, gcry_md_hd_t *mh_pre, gcry_md_hd_t *mh_post) { char buf[COPYBUF_SIZE]; ssize_t c; while ((c = read(fdin, buf, COPYBUF_SIZE)) > 0) { if (mh_pre) gcry_md_write(*mh_pre, buf, c); aes256ctr_enc(ac, buf, c); if (mh_post) gcry_md_write(*mh_post, buf, c); write_block(fdout, buf, c); } if (c < 0) fatal_errno("Read error", errno); } void decryption_loop(int fdin, int fdout, struct aes256ctr *ac, gcry_md_hd_t *mh_pre, gcry_md_hd_t *mh_post, char *tail, int taillen) { char buf[COPYBUF_SIZE]; ssize_t c; if (! read_block(fdin, buf, taillen)) fatal("Input too short"); while ((c = read(fdin, buf + taillen, COPYBUF_SIZE - taillen)) > 0) { if (mh_pre) gcry_md_write(*mh_pre, buf, c); aes256ctr_dec(ac, buf, c); if (mh_post) gcry_md_write(*mh_post, buf, c); write_block(fdout, buf, c); memmove(buf, buf + c, taillen); } if (c < 0) fatal_errno("Read error", errno); memcpy(tail, buf, taillen); } void verisign_loop(int fdin, int fdout, gcry_md_hd_t *mh, char *tail, int taillen, int copyflag) { char buf[COPYBUF_SIZE]; ssize_t c; if (! read_block(fdin, buf, taillen)) fatal("Input too short"); while((c = read(fdin, buf + taillen, COPYBUF_SIZE - taillen)) > 0) { gcry_md_write(*mh, buf, c); if (copyflag) write_block(fdout, buf, c); memmove(buf, buf + c, taillen); } if (c < 0) fatal_errno("Read error", errno); memcpy(tail, buf, taillen); } /******************************************************************************/ void do_read_passphrase(const struct termios *tios, char *hash) { gcry_error_t err; gcry_md_hd_t mh; char *md, ch; ssize_t r; err = gcry_md_open(&mh, GCRY_MD_SHA256, 0); if (gcry_err_code(err)) { if (isatty(opt_fdpw)) tcsetattr(opt_fdpw, TCSANOW, tios); fatal_gcrypt("Cannot initialize SHA256", err); } while (((r = read(opt_fdpw, &ch, 1)) > 0) && (ch != '\n')) if (ch != '\r') gcry_md_putc(mh, ch); if (r < 0) { int err = errno; if (isatty(opt_fdpw)) tcsetattr(opt_fdpw, TCSANOW, tios); fatal_errno("Cannot read text line", err); } gcry_md_final(mh); md = (char*)gcry_md_read(mh, 0); memcpy(hash, md, 32); gcry_md_close(mh); } void read_passphrase(const char *name, char *hash) { struct termios echo_orig, echo_off; if (isatty(opt_fdpw)) { tcgetattr(opt_fdpw, &echo_orig); echo_off = echo_orig; echo_off.c_lflag &= ~ECHO; tcsetattr(opt_fdpw, TCSANOW, &echo_off); if (! opt_quiet) fprintf(stderr, "Enter %s: ", name); } else if (opt_dblprompt) print_quiet("Ignoring -d flag.\n", 0); do_read_passphrase(&echo_orig, hash); if (isatty(opt_fdpw)) { if (opt_dblprompt) { char hash2[32]; if (! opt_quiet) fprintf(stderr, ANSI_CLEAR_LINE "Reenter %s: ", name); do_read_passphrase(&echo_orig, hash2); if (memcmp(hash, hash2, 32)) { tcsetattr(opt_fdpw, TCSANOW, &echo_orig); fatal("Passphrases do not match"); } } print_quiet(ANSI_CLEAR_LINE, 0); tcsetattr(opt_fdpw, TCSANOW, &echo_orig); } } /******************************************************************************/ void app_print_public_key(void) { struct curve_params *cp; if (! opt_curve) { opt_curve = DEFAULT_CURVE; fprintf(stderr, "Assuming curve " DEFAULT_CURVE ".\n"); } if ((cp = curve_by_name(opt_curve))) { char pubkey[cp->pk_len_compact + 1]; char privkey[32]; struct affine_point P; gcry_mpi_t d; if (opt_verbose) { print_quiet("VERSION: ", 0); fprintf(stderr, VERSION "\n"); print_quiet("CURVE: ", 0); fprintf(stderr, "%s\n", cp->name); } read_passphrase("private key", privkey); d = hash_to_exponent(privkey, cp); memset(privkey, 0, sizeof(privkey)); P = pointmul(&cp->dp.base, d, &cp->dp); gcry_mpi_release(d); compress_to_string(pubkey, DF_COMPACT, &P, cp); pubkey[cp->pk_len_compact] = 0; if (! opt_quiet) printf("The public key is: "); printf("%s\n", pubkey); point_release(&P); curve_release(cp); } else fatal("Invalid curve name"); } void app_encrypt(const char *pubkey) { struct affine_point P, R; struct curve_params *cp; if (opt_maclen < 0) { opt_maclen = DEFAULT_MAC_LEN; fprintf(stderr, "Assuming MAC length of %d bits.\n", 8 * DEFAULT_MAC_LEN); } if (opt_curve) { if (! (cp = curve_by_name(opt_curve))) fatal("Invalid curve name"); } else if (! (cp = curve_by_pk_len_compact(strlen(pubkey)))) fatal("Invalid encryption key (wrong length)"); if (opt_verbose) { print_quiet("VERSION: ", 0); fprintf(stderr, VERSION "\n"); print_quiet("CURVE: ", 0); fprintf(stderr, "%s\n", cp->name); print_quiet("MACLEN: ", 0); fprintf(stderr, "%d\n", 8 * opt_maclen); } if (strlen(pubkey) != cp->pk_len_compact) fatal("Invalid encryption key (wrong length)"); if (decompress_from_string(&P, pubkey, DF_COMPACT, cp)) { char rbuf[cp->pk_len_bin]; struct aes256ctr *ac; char keybuf[64], *md; gcry_md_hd_t mh; R = ECIES_encryption(keybuf, &P, cp); compress_to_string(rbuf, DF_BIN, &R, cp); point_release(&P); point_release(&R); if (opt_verbose) { int i; print_quiet("K_ENC: ", 0); for(i = 0; i < 32; i++) fprintf(stderr, "%02x", (unsigned char)keybuf[i]); fprintf(stderr, "\n"); print_quiet("K_MAC: ", 0); for(i = 32; i < 64; i++) fprintf(stderr, "%02x", (unsigned char)keybuf[i]); fprintf(stderr, "\n"); } if (! (ac = aes256ctr_init(keybuf))) fatal("Cannot initialize AES256-CTR"); if (opt_maclen && ! hmacsha256_init(&mh, keybuf + 32)) fatal("Cannot initialize HMAC-SHA256"); memset(keybuf, 0, sizeof(keybuf)); if (isatty(opt_fdin)) print_quiet("Go ahead and type your message ...\n", 0); write_block(opt_fdout, rbuf, cp->pk_len_bin); encryption_loop(opt_fdin, opt_fdout, ac, NULL, opt_maclen ? &mh : NULL); aes256ctr_done(ac); if (opt_maclen) { gcry_md_final(mh); md = (char*)gcry_md_read(mh, 0); if (opt_verbose) { int i; print_quiet("HMAC: ", 0); for(i = 0; i < opt_maclen; i++) fprintf(stderr, "%02x", (unsigned char)md[i]); fprintf(stderr, "\n"); } write_block(opt_fdout, md, opt_maclen); gcry_md_close(mh); } } else fatal("Invalid encryption key"); curve_release(cp); } int app_decrypt(void) { struct curve_params *cp; struct affine_point R; int res = 0; if (opt_maclen < 0) { opt_maclen = DEFAULT_MAC_LEN; fprintf(stderr, "Assuming MAC length of %d bits.\n", 8 * DEFAULT_MAC_LEN); } if (! opt_curve) { opt_curve = DEFAULT_CURVE; fprintf(stderr, "Assuming curve " DEFAULT_CURVE ".\n"); } if ((cp = curve_by_name(opt_curve))) { char keybuf[64], privkey[32]; char rbuf[cp->pk_len_bin]; char mdbuf[opt_maclen], *md; struct aes256ctr *ac; gcry_md_hd_t mh; gcry_mpi_t d; if (opt_verbose) { print_quiet("VERSION: ", 0); fprintf(stderr, VERSION "\n"); print_quiet("CURVE: ", 0); fprintf(stderr, "%s\n", cp->name); print_quiet("MACLEN: ", 0); fprintf(stderr, "%d\n", 8 * opt_maclen); } read_passphrase("private key", privkey); d = hash_to_exponent(privkey, cp); memset(privkey, 0, sizeof(privkey)); if (isatty(opt_fdin)) print_quiet("Go ahead and enter the ciphertext ...\n", 0); if (read_block(opt_fdin, rbuf, cp->pk_len_bin)) { if (decompress_from_string(&R, rbuf, DF_BIN, cp)) { if (ECIES_decryption(keybuf, &R, d, cp)) { if (opt_verbose) { int i; print_quiet("K_ENC: ", 0); for(i = 0; i < 32; i++) fprintf(stderr, "%02x", (unsigned char)keybuf[i]); fprintf(stderr, "\n"); print_quiet("K_MAC: ", 0); for(i = 32; i < 64; i++) fprintf(stderr, "%02x", (unsigned char)keybuf[i]); fprintf(stderr, "\n"); } if (! (ac = aes256ctr_init(keybuf))) fatal("Cannot initialize AES256-CTR"); if (opt_maclen && ! hmacsha256_init(&mh, keybuf + 32)) fatal("Cannot initialize HMAC-SHA256"); memset(keybuf, 0, sizeof(keybuf)); decryption_loop(opt_fdin, opt_fdout, ac, opt_maclen ? &mh : NULL, NULL, mdbuf, opt_maclen); aes256ctr_done(ac); res = 1; if (opt_maclen) { gcry_md_final(mh); md = (char*)gcry_md_read(mh, 0); if (opt_verbose) { int i; print_quiet("HMAC1: ", 0); for(i = 0; i < opt_maclen; i++) fprintf(stderr, "%02x", (unsigned char)md[i]); fprintf(stderr, "\n"); print_quiet("HMAC2: ", 0); for(i = 0; i < opt_maclen; i++) fprintf(stderr, "%02x", (unsigned char)mdbuf[i]); fprintf(stderr, "\n"); } if ((res = ! memcmp(mdbuf, md, opt_maclen))) print_quiet("Integrity check successful, message unforged!\n", 0); else print_quiet("WARNING: Integrity check failed, message " "forged!\n", 1); gcry_md_close(mh); } else print_quiet("Warning: No MAC available, message integrity cannot " "be verified!\n", 0); } else print_quiet("Abort: Inconsistent header.\n", 1); point_release(&R); } else print_quiet("Abort: Inconsistent header.\n", 1); } else print_quiet("Abort: Inconsistent header (too short).\n", 1); gcry_mpi_release(d); curve_release(cp); } else fatal("Invalid curve name"); return ! res; } void app_sign(void) { struct curve_params *cp; char privkey[32], *md; gcry_md_hd_t mh; gcry_error_t err; gcry_mpi_t d, sig; FILE *sigfile; if (opt_sigappend) { opt_sigcopy = 1; if (opt_sigfile) fatal("The options -s and -a may not be combined"); } if (! opt_curve) { opt_curve = DEFAULT_CURVE; fprintf(stderr, "Assuming curve " DEFAULT_CURVE ".\n"); } if ((cp = curve_by_name(opt_curve))) { if (opt_verbose) { print_quiet("VERSION: ", 0); fprintf(stderr, VERSION "\n"); print_quiet("CURVE: ", 0); fprintf(stderr, "%s\n", cp->name); } read_passphrase("private key", privkey); d = hash_to_exponent(privkey, cp); memset(privkey, 0, sizeof(privkey)); err = gcry_md_open(&mh, GCRY_MD_SHA512, 0); if (gcry_err_code(err)) fatal_gcrypt("Cannot initialize SHA512", err); if (isatty(opt_fdin)) print_quiet("Go ahead and type your message ...\n", 0); verisign_loop(opt_fdin, opt_fdout, &mh, NULL, 0, opt_sigcopy); gcry_md_final(mh); md = (char*)gcry_md_read(mh, 0); if (opt_verbose) { int i; print_quiet("SHA512: ", 0); for(i = 0; i < 64; i++) fprintf(stderr, "%02x", (unsigned char)md[i]); fprintf(stderr, "\n"); } sig = ECDSA_sign(md, d, cp); gcry_mpi_release(d); if (opt_sigfile) { if (! (sigfile = fopen(opt_sigfile, "w"))) fatal_errno("Cannot open signature file", errno); } else sigfile = stderr; if (opt_sigbin) { char sigbuf[cp->sig_len_bin]; serialize_mpi(sigbuf, cp->sig_len_bin, DF_BIN, sig); if (opt_sigappend) write_block(opt_fdout, sigbuf, cp->sig_len_bin); else if (fwrite(sigbuf, cp->sig_len_bin, 1, sigfile) != 1) fatal_errno("Cannot write signature", errno); } else { char sigbuf[cp->sig_len_compact + 1]; serialize_mpi(sigbuf, cp->sig_len_compact, DF_COMPACT, sig); if (opt_sigappend) write_block(opt_fdout, sigbuf, cp->sig_len_compact); else { sigbuf[cp->sig_len_compact] = 0; if (sigfile == stderr) print_quiet("Signature: ", 0); if (fprintf(sigfile, "%s\n", sigbuf) < 0) fatal_errno("Cannot write signature", errno); } } if (opt_sigfile && fclose(sigfile)) fatal_errno("Cannot close signature file", errno); gcry_mpi_release(sig); gcry_md_close(mh); curve_release(cp); } else fatal("Invalid curve name"); } int app_verify(const char *pubkey, const char *sig) { struct curve_params *cp; struct affine_point Q; gcry_mpi_t s; gcry_md_hd_t mh; gcry_error_t err; char *md; int res = 0; if (!! sig + !! opt_sigfile + !! opt_sigappend != 1) fatal("Exactly one signature has to be specified"); if (sig) opt_sigbin = 0; if (opt_curve) { if (! (cp = curve_by_name(opt_curve))) fatal("Invalid curve name"); } else if (! (cp = curve_by_pk_len_compact(strlen(pubkey)))) fatal("Invalid verification key (wrong length)"); if (opt_verbose) { print_quiet("VERSION: ", 0); fprintf(stderr, VERSION "\n"); print_quiet("CURVE: ", 0); fprintf(stderr, "%s\n", cp->name); } if (strlen(pubkey) != cp->pk_len_compact) fatal("Invalid verification key (wrong length)"); if (decompress_from_string(&Q, pubkey, DF_COMPACT, cp)) { union { char compact[cp->sig_len_compact + 2]; char bin[cp->sig_len_bin]; } sigbuf; err = gcry_md_open(&mh, GCRY_MD_SHA512, 0); if (gcry_err_code(err)) fatal_gcrypt("Cannot initialize SHA512", err); if (opt_sigfile) { FILE *sigfile; if (! (sigfile = fopen(opt_sigfile, "r"))) fatal_errno("Cannot open signature file", errno); if (opt_sigbin) { if (fread(sigbuf.bin, cp->sig_len_bin, 1, sigfile) != 1) { if (ferror(sigfile)) fatal_errno("Cannot read signature", errno); else { print_quiet("Invalid signature (wrong length)!\n", 1); goto error; } } } else { sigbuf.compact[0] = 0; if (! fgets(sigbuf.compact, cp->sig_len_compact + 2, sigfile) && ferror(sigfile)) fatal_errno("Cannot read signature", errno); sigbuf.compact[strcspn(sigbuf.compact, " \r\n")] = '\0'; } if (fclose(sigfile)) fatal_errno("Cannot close signature file", errno); } if (isatty(opt_fdin)) print_quiet("Go ahead and type your message ...\n", 0); if (opt_sigappend) { if (opt_sigbin) verisign_loop(opt_fdin, opt_fdout, &mh, sigbuf.bin, cp->sig_len_bin, opt_sigcopy); else { verisign_loop(opt_fdin, opt_fdout, &mh, sigbuf.compact, cp->sig_len_compact, opt_sigcopy); sigbuf.compact[cp->sig_len_compact] = 0; } } else verisign_loop(opt_fdin, opt_fdout, &mh, NULL, 0, opt_sigcopy); if (opt_sigbin) assert(deserialize_mpi(&s, DF_BIN, sigbuf.bin, cp->sig_len_bin)); else { if (! sig) sig = sigbuf.compact; if (strlen(sig) != cp->sig_len_compact) { print_quiet("Invalid signature (wrong length)!\n", 1); goto error; } else if (! deserialize_mpi(&s, DF_COMPACT, sig, cp->sig_len_compact)) { print_quiet("Invalid signature (inconsistent structure)!\n", 1); goto error; } } gcry_md_final(mh); md = (char*)gcry_md_read(mh, 0); if (opt_verbose) { int i; print_quiet("SHA512: ", 0); for(i = 0; i < 64; i++) fprintf(stderr, "%02x", (unsigned char)md[i]); fprintf(stderr, "\n"); } if ((res = ECDSA_verify(md, &Q, s, cp))) print_quiet("Signature successfully verified!\n", 0); else print_quiet("Invalid signature, message forged!\n", 1); gcry_mpi_release(s); error: gcry_md_close(mh); point_release(&Q); } else fatal("Invalid verification key"); curve_release(cp); return ! res; } void app_signcrypt(const char *pubkey) { struct curve_params *cp_enc, *cp_sig; struct affine_point P, R; if (! opt_curve) { opt_curve = DEFAULT_CURVE; fprintf(stderr, "Assuming signature curve " DEFAULT_CURVE ".\n"); } if (! (cp_sig = curve_by_name(opt_curve))) fatal("Invalid curve name"); if (opt_curve2) { if (! (cp_enc = curve_by_name(opt_curve2))) fatal("Invalid curve name"); } else if (! (cp_enc = curve_by_pk_len_compact(strlen(pubkey)))) fatal("Invalid encryption key (wrong length)"); if (opt_verbose) { print_quiet("VERSION: ", 0); fprintf(stderr, VERSION "\n"); print_quiet("SIGNATURE CURVE: ", 0); fprintf(stderr, "%s\n", cp_sig->name); print_quiet("ENCRYPTION CURVE: ", 0); fprintf(stderr, "%s\n", cp_enc->name); } if (strlen(pubkey) != cp_enc->pk_len_compact) fatal("Invalid encryption key (wrong length)"); if (decompress_from_string(&P, pubkey, DF_COMPACT, cp_enc)) { char sigbuf[cp_sig->sig_len_bin]; char rbuf[cp_enc->pk_len_bin]; char keybuf[64], privkey[32], *md; struct aes256ctr *ac; gcry_mpi_t d, sig; gcry_md_hd_t mh; gcry_error_t err; read_passphrase("private signing key", privkey); d = hash_to_exponent(privkey, cp_sig); memset(privkey, 0, sizeof(privkey)); R = ECIES_encryption(keybuf, &P, cp_enc); compress_to_string(rbuf, DF_BIN, &R, cp_enc); point_release(&P); point_release(&R); if (opt_verbose) { int i; print_quiet("K_ENC: ", 0); for(i = 0; i < 32; i++) fprintf(stderr, "%02x", (unsigned char)keybuf[i]); fprintf(stderr, "\n"); } if (! (ac = aes256ctr_init(keybuf))) fatal("Cannot initialize AES256-CTR"); memset(keybuf, 0, sizeof(keybuf)); err = gcry_md_open(&mh, GCRY_MD_SHA512, 0); if (gcry_err_code(err)) fatal_gcrypt("Cannot initialize SHA512", err); if (isatty(opt_fdin)) print_quiet("Go ahead and type your message ...\n", 0); write_block(opt_fdout, rbuf, cp_enc->pk_len_bin); encryption_loop(opt_fdin, opt_fdout, ac, &mh, NULL); gcry_md_final(mh); md = (char*)gcry_md_read(mh, 0); if (opt_verbose) { int i; print_quiet("SHA512: ", 0); for(i = 0; i < 64; i++) fprintf(stderr, "%02x", (unsigned char)md[i]); fprintf(stderr, "\n"); } sig = ECDSA_sign(md, d, cp_sig); serialize_mpi(sigbuf, cp_sig->sig_len_bin, DF_BIN, sig); aes256ctr_enc(ac, sigbuf, cp_sig->sig_len_bin); write_block(opt_fdout, sigbuf, cp_sig->sig_len_bin); aes256ctr_done(ac); gcry_mpi_release(d); gcry_mpi_release(sig); gcry_md_close(mh); } else fatal("Invalid encryption key"); curve_release(cp_sig); curve_release(cp_enc); } int app_veridec(const char *pubkey) { struct curve_params *cp_enc, *cp_sig; struct affine_point Q, R; int res = 0; if (! opt_curve) { opt_curve = DEFAULT_CURVE; fprintf(stderr, "Assuming encryption curve " DEFAULT_CURVE ".\n"); } if (! (cp_enc = curve_by_name(opt_curve))) fatal("Invalid curve name"); if (opt_curve2) { if (! (cp_sig = curve_by_name(opt_curve2))) fatal("Invalid curve name"); } else if (! (cp_sig = curve_by_pk_len_compact(strlen(pubkey)))) fatal("Invalid verification key (wrong length)"); if (opt_verbose) { print_quiet("VERSION: ", 0); fprintf(stderr, VERSION "\n"); print_quiet("SIGNATURE CURVE: ", 0); fprintf(stderr, "%s\n", cp_sig->name); print_quiet("ENCRYPTION CURVE: ", 0); fprintf(stderr, "%s\n", cp_enc->name); } if (strlen(pubkey) != cp_sig->pk_len_compact) fatal("Invalid verification key (wrong length)"); if (decompress_from_string(&Q, pubkey, DF_COMPACT, cp_sig)) { char sigbuf[cp_sig->sig_len_bin]; char rbuf[cp_enc->pk_len_bin]; char keybuf[64], privkey[32], *md; gcry_mpi_t d, sig; struct aes256ctr *ac; gcry_md_hd_t mh; gcry_error_t err; read_passphrase("private decryption key", privkey); d = hash_to_exponent(privkey, cp_enc); memset(privkey, 0, sizeof(privkey)); if (isatty(opt_fdin)) print_quiet("Go ahead and enter the ciphertext ...\n", 0); if (read_block(opt_fdin, rbuf, cp_enc->pk_len_bin)) { if (decompress_from_string(&R, rbuf, DF_BIN, cp_enc)) { if (ECIES_decryption(keybuf, &R, d, cp_enc)) { if (opt_verbose) { int i; print_quiet("K_ENC: ", 0); for(i = 0; i < 32; i++) fprintf(stderr, "%02x", (unsigned char)keybuf[i]); fprintf(stderr, "\n"); } if (! (ac = aes256ctr_init(keybuf))) fatal("Cannot initialize AES256-CTR"); memset(keybuf, 0, sizeof(keybuf)); err = gcry_md_open(&mh, GCRY_MD_SHA512, 0); if (gcry_err_code(err)) fatal_gcrypt("Cannot initialize SHA512", err); decryption_loop(opt_fdin, opt_fdout, ac, NULL, &mh, sigbuf, cp_sig->sig_len_bin); gcry_md_final(mh); md = (char*)gcry_md_read(mh, 0); if (opt_verbose) { int i; print_quiet("SHA512: ", 0); for(i = 0; i < 64; i++) fprintf(stderr, "%02x", (unsigned char)md[i]); fprintf(stderr, "\n"); } aes256ctr_dec(ac, sigbuf, cp_sig->sig_len_bin); assert(deserialize_mpi(&sig, DF_BIN, sigbuf, cp_sig->sig_len_bin)); if ((res = ECDSA_verify(md, &Q, sig, cp_sig))) print_quiet("Signature successfully verified!\n", 0); else print_quiet("WARNING: Invalid signature, message forged!\n", 1); aes256ctr_done(ac); gcry_md_close(mh); gcry_mpi_release(sig); } else print_quiet("Abort: Inconsistent header.\n", 1); point_release(&R); } else print_quiet("Abort: Inconsistent header.\n", 1); } else print_quiet("Abort: Inconsistent header (too short).\n", 1); gcry_mpi_release(d); point_release(&Q); } else fatal("Invalid verification key"); curve_release(cp_enc); curve_release(cp_sig); return ! res; } void app_dh(void) { struct curve_params *cp; if (! opt_curve) { opt_curve = DEFAULT_CURVE; fprintf(stderr, "Assuming curve " DEFAULT_CURVE ".\n"); } if ((cp = curve_by_name(opt_curve))) { char keyA[cp->pk_len_compact + 1]; char keyB[cp->pk_len_compact + 2]; char kbuf[cp->dh_len_compact + 1]; char vbuf[cp->dh_len_compact + 1]; struct affine_point A, B; gcry_mpi_t exp, h; char keybuf[64]; if (opt_verbose) { print_quiet("VERSION: ", 0); fprintf(stderr, VERSION "\n"); print_quiet("CURVE: ", 0); fprintf(stderr, "%s\n", cp->name); } exp = DH_step1(&A, cp); compress_to_string(keyA, DF_COMPACT, &A, cp); point_release(&A); keyA[cp->pk_len_compact] = 0; print_quiet("Pass the following key to your peer: ", 0); fprintf(stderr, "%s\n", keyA); print_quiet("Enter your peer's key: ", 0); keyB[0] = 0; if (! fgets(keyB, cp->pk_len_compact + 2, stdin) && ferror(stdin)) fatal_errno("Cannot read text line", errno); keyB[strcspn(keyB, "\r\n")] = '\0'; if (strlen(keyB) != cp->pk_len_compact) fatal("Invalid key (wrong length)"); if (decompress_from_string(&B, keyB, DF_COMPACT, cp)) { if (DH_step2(keybuf, &B, exp, cp)) { assert(cp->dh_len_bin <= 32); if (opt_verbose) { int i; print_quiet("K_ESTABLISHED: ", 0); for(i = 0; i < cp->dh_len_bin; i++) fprintf(stderr, "%02x", (unsigned char)keybuf[i]); fprintf(stderr, "\n"); print_quiet("K_VERIFICATION: ", 0); for(i = 0; i < cp->dh_len_bin; i++) fprintf(stderr, "%02x", (unsigned char)keybuf[32 + i]); fprintf(stderr, "\n"); } assert(deserialize_mpi(&h, DF_BIN, keybuf, cp->dh_len_bin)); serialize_mpi(kbuf, cp->dh_len_compact, DF_COMPACT, h); kbuf[cp->dh_len_compact] = 0; gcry_mpi_release(h); assert(deserialize_mpi(&h, DF_BIN, keybuf + 32, cp->dh_len_bin)); serialize_mpi(vbuf, cp->dh_len_compact, DF_COMPACT, h); vbuf[cp->dh_len_compact] = 0; gcry_mpi_release(h); if (! opt_quiet) printf("Established key: "); printf("%s\n", kbuf); if (! opt_quiet) printf("Verification key: "); printf("%s\n", vbuf); } else fatal("Invalid key"); point_release(&B); } else fatal("Invalid key"); gcry_mpi_release(exp); curve_release(cp); } else fatal("Invalid curve name"); } /******************************************************************************/ int main(int argc, char **argv) { gcry_error_t err; char *progname; int res = 0, i; #if ! NOMEMLOCK if (mlockall(MCL_CURRENT | MCL_FUTURE) < 0) fatal_errno("Cannot obtain memory lock", errno); #endif /* As we already have locked all memory we don't need gcrypt's mlocking */ err = gcry_control(GCRYCTL_DISABLE_SECMEM, 0); if (gcry_err_code(err)) fatal_gcrypt("Cannot disable gcrypt's secure memory", err); if (getuid() != geteuid()) seteuid(getuid()); if ((progname = strrchr(argv[0], '/')) == NULL) progname = argv[0]; while((i = getopt(argc, argv, "fbadm:i:o:F:s:c:hvq")) != -1) switch(i) { case 'f': opt_sigcopy = 1; break; case 'b': opt_sigbin = 1; break; case 'a': opt_sigappend = 1; break; case 'd': opt_dblprompt = 1; break; case 'm': opt_maclen = atoi(optarg); if (opt_maclen < 0 || opt_maclen > 256 || opt_maclen % 8) fatal("Invalid MAC length"); opt_maclen /= 8; break; case 'i': opt_infile = optarg; break; case 'o': opt_outfile = optarg; break; case 'F': opt_pwfile = optarg; break; case 's': opt_sigfile = optarg; break; case 'c': if (! opt_curve) opt_curve = optarg; else opt_curve2 = optarg; break; case 'h': opt_help = 1; break; case 'v': opt_verbose = 1; break; case 'q': opt_quiet = 1; break; default: exit(1); } if (opt_infile) if ((opt_fdin = open(opt_infile, O_RDONLY)) < 0) fatal_errno("Cannot open input file", errno); if (opt_outfile) { int openmode = strstr(progname, "decrypt") ? 0600 : 0644; if ((opt_fdout = open(opt_outfile, O_WRONLY | O_CREAT | O_TRUNC, openmode)) < 0) fatal_errno("Cannot open output file", errno); } if (opt_pwfile) { if ((opt_fdpw = open(opt_pwfile, O_RDONLY)) < 0) fatal_errno("Cannot open password file", errno); } else if (opt_infile || isatty(STDIN_FILENO)) opt_fdpw = STDIN_FILENO; else if ((opt_fdpw = open("/dev/tty", O_RDONLY)) < 0) fatal_errno("Cannot open tty", errno); if (strstr(progname, "key")) { if (opt_help || optind != argc) puts("Generate public key from secret key (seccure version " VERSION ").\n" "\n" "seccure-key [-c curve] [-F pwfile] [-d]"); else app_print_public_key(); } else if (strstr(progname, "encrypt")) { if (opt_help || optind != argc - 1) puts("Encrypt using a public key (seccure version " VERSION ").\n" "\n" "seccure-encrypt [-m maclen] [-c curve] [-i infile] [-o outfile] key"); else app_encrypt(argv[optind]); } else if (strstr(progname, "decrypt")) { if (opt_help || optind != argc) puts("Decrypt using a secret key (seccure version " VERSION ").\n" "\n" "seccure-decrypt [-m maclen] [-c curve] [-i infile] [-o outfile]\n" " [-F pwfile] [-d]"); else res = app_decrypt(); } else if (strstr(progname, "signcrypt")) { if (opt_help || optind != argc - 1) puts("Signcrypt a message (seccure version " VERSION ").\n" "\n" "seccure-signcrypt [-c sig_curve [-c enc_curve]] [-i infile] [-o outfile]\n" " [-F pwfile] [-d] key"); else app_signcrypt(argv[optind]); } else if (strstr(progname, "veridec")) { if (opt_help || optind != argc - 1) puts("Decrypt a signcrypted message (seccure version " VERSION ").\n" "\n" "seccure-veridec [-c enc_curve [-c sig_curve]] [-i infile] [-o outfile]\n" " [-F pwfile] [-d] key"); else res = app_veridec(argv[optind]); } else if (strstr(progname, "sign")) { if (opt_help || optind != argc) puts("Generate a signature (seccure version " VERSION ").\n" "\n" "seccure-sign [-f] [-b] [-a] [-c curve] [-s sigfile] [-i infile]\n" " [-o outfile] [-F pwfile] [-d]"); else app_sign(); } else if (strstr(progname, "verify")) { if (opt_help || (optind != argc - 2 && optind != argc - 1)) puts("Verify a signature (seccure version " VERSION ").\n" "\n" "seccure-verify [-f] [-b] [-a] [-c curve] [-s sigfile] [-i infile] [-o outfile]\n" " key [signature]"); else res = app_verify(argv[optind], argv[optind + 1]); } else if (strstr(progname, "dh")) { if (opt_help || optind != argc) puts("Perform an interactive Diffie-Hellman key exchange (seccure version " VERSION ").\n" "\n" "seccure-dh [-c curve]"); else app_dh(); } else fatal("Unknown command"); if (opt_infile) close(opt_fdin); if (opt_outfile) close(opt_fdout); if (opt_pwfile || (! opt_infile && ! isatty(STDIN_FILENO))) close(opt_fdpw); exit(res); } seccure-0.3/seccure.1.html0000644000175100001440000002057210470676205015054 0ustar bertramusers

seccure

SECCURE Elliptic Curve Crypto Utility for Reliable Encryption

Synopsis

seccure-key [-c curve] [-F pwfile] [-d] [-v] [-q]
seccure-encrypt [-m maclen] [-c curve] [-i infile] [-o outfile] [-v] [-q] key
seccure-decrypt [-m maclen] [-c curve] [-i infile] [-o outfile] [-F pwfile] [-d] [-v] [-q]
seccure-sign [-f] [-b] [-a] [-c curve] [-s sigfile] [-i infile] [-o outfile] [-F pwfile] [-d] [-v] [-q]
seccure-verify [-f] [-b] [-a] [-c curve] [-s sigfile] [-i infile] [-o outfile] [-v] [-q] key [sig]
seccure-signcrypt [-c sig_curve [-c enc_curve]] [-i infile] [-o outfile] [-F pwfile] [-d] [-v] [-q] key
seccure-veridec [-c enc_curve [-c sig_curve]] [-i infile] [-o outfile] [-F pwfile] [-d] [-v] [-q] key
seccure-dh [-c curve] [-v] [-q]

Description

The seccure toolset implements a selection of asymmetric algorithms based on elliptic curve cryptography (ECC). In particular it offers public key encryption / decryption, signature generation / verification and key establishment.

ECC schemes offer a much better key size to security ratio than classical systems (RSA, DSA). Keys are short enough to make direct specification of keys on the command line possible (sometimes this is more convenient than the management of PGP-like key rings). seccure builds on this feature and therefore is the tool of choice whenever lightweight asymmetric cryptography -- independent of key servers, revocation certificates, the Web of Trust or even configuration files -- is required.

Commands

seccure-key: Prompt for a passphrase and calculate the corresponding public key.

seccure-encrypt: Encrypt a message with public key key.

seccure-decrypt: Prompt for a passphrase and decrypt a seccure-encrypted message.

seccure-sign: Prompt for a passphrase and digitally sign a message.

seccure-verify: Verify signature sig with public key key.

seccure-signcrypt: Sign a message first, encrypt it subsequently (in -b -a and -m 0 mode, respectively). This is basically a shortcut for two separate seccure invocations.

seccure-veridec: Counterpart to signcryption.

seccure-dh: Perform a Diffie-Hellman key exchange.

Options

-c curve

Use elliptic curve curve. Available are: secp112r1, secp128r1, secp160r1, secp192r1/nistp192, secp224r1/nistp224, secp256r1/nistp256, secp384r1/nistp384 and secp521r1/nistp521. The curve name may be abbreviated by any non-ambiguous substring (for instance it is suggested to specify p224 for the secp224r1/nistp224 curve). The default curve is p160, which provides reasonable security for everyday use. (See also HOW TO CHOOSE THE CURVE.)

Note: If a public key is given on the command line seccure can determine the corresponding curve on its own. It's then unnecessary to specify the curve explicitly.

-F pwfile

Don't prompt for a passphrase; instead, take the first text line of pwfile.

-m maclen

Set the MAC length to maclen bits. Only multiples of 8 in the range from 0 to 256 are allowed. The default MAC length is 80 bits, which provides a reasonable level of integrity protection for everyday use.

-i infile

Read from infile instead of STDIN.

-o outfile

Write to outfile instead of STDOUT.

-s sigfile

For seccure-sign: Write signature to sigfile instead of STDERR.

For seccure-verify: Read signature from sigfile instead of using sig.

-f

Filter mode: Copy all data read from STDIN verbatim to STDOUT (eventually attaching or detaching a signature in -a mode).

-b

Binary mode: Read/write signatures as binary strings. This leads to very compact signatures.

-a

Append mode:

For seccure-sign: Append signature to the end of the document. Enforces -f mode.

For seccure-verify: Detach signature from the end of the document.

-d

Double prompt mode: When reading a passphrase from the console: prompt twice and assure the phrases are the same.

-v

Verbose mode: Print some extra information.

-q

Quiet mode: Disable all unnecessary output.

Example

Given the passphrase 'seccure is secure', run

seccure-key

to determine the corresponding public key (which is '2@DupCaCKykHBe-QHpAP%d%B[' on curve p160).

To encrypt the file 'document.msg' with that key run

seccure-encrypt -i document.msg -o document.enc '2@DupCaCKykHBe-QHpAP%d%B['

The message can be recovered with

seccure-decrypt -i document.enc

To sign the file run

seccure-sign -i document.msg -s document.sig

and enter the passphrase. The signature is stored in 'document.sig' and can be verified with

seccure-verify -i document.msg -s document.sig '2@DupCaCKykHBe-QHpAP%d%B['

Key establishment

seccure-dh performs an interactive Diffie-Hellman key exchange. Two instances have to be run in parallel; the token generated by the first one is the input for the second one and vice versa. The output consists of two shared keys: it is guaranteed that no attacker can ever find out the established key as soon as the two parties can confirm that both have the same verification key. The authentic comparision of the verification keys can, for example, be realized via signed messages or via telephone (using 'voice authentication').

How to choose the curve

The number in the names of the curves measures their security level. Rule of thumb: the workload to 'break' a k-bit curve is 2^(k/2) approximately (example: it takes about 2^112 steps to break secp224r1). If the 80 bit security of the default curve doesn't seem sufficient, choosing a stronger curve (p192 and upwards) may, of course, be considered. But the suggestion remains: p160 offers reasonable security for everyday use. Warning: the curves p112 and p128 do not satisfy demands for long-time security.

Algorithms

seccure uses derivated versions of ECIES (Elliptic Curve Integrated Encryption Scheme), ECDSA (Elliptic Curve Digital Signature Algorithm) and ECDH (Elliptic Curve Diffie-Hellman) as encryption, signature and key establishment scheme, respectively. For the symmetric parts (bulk encryption, hashing, key derivation, HMAC calculation) seccure builds on AES256 (in CTR mode), SHA256 and SHA512. To my best knowledge no part of seccure is covered by patents. See the file PATENTS for an explicit patent statement.

Author

This software (v0.3) was written by B. Poettering (seccure AT point-at-infinity.org) in 2006. It is released under the terms of the GNU General Public License (GPL). Find the latest version of seccure on the project's homepage: http://point-at-infinity.org/seccure/.
seccure-0.3/README0000644000175100001440000000132310470676205013247 0ustar bertramusers SECCURE Elliptic Curve Crypto Utility for Reliable Encryption http://point-at-infinity.org/seccure/ The SECCURE toolset implements a selection of asymmetric algorithms based on elliptic curve cryptography (ECC). See the manpage or the project's homepage for further details. SECCURE is released under the GNU General Public License (GPL). The code links against the GNU gcrypt library "libgcrypt" (which is part of the GnuPG project). The code compiles successfully with gcc-4.0 and libgcrypt 1.2.2. Use the included Makefile to build the binary. Compile with -D NOMEMLOCK if your machine doesn't support memory locking. Report bugs to: seccure AT point-at-infinity.org Copyright 2006 B. Poettering seccure-0.3/curves.h0000644000175100001440000000354110470676205014053 0ustar bertramusers/* * seccure - Copyright 2006 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which is * part of the GnuPG project). The code compiles successfully with * libgcrypt 1.2.2. Use the included Makefile to build the binary. * * Compile with -D NOMEMLOCK if your machine doesn't support memory * locking. * * Report bugs to: seccure AT point-at-infinity.org * */ #ifndef INC_CURVES_H #define INC_CURVES_H #include "ecc.h" struct curve_params { const char *name; struct domain_params dp; int pk_len_bin, pk_len_compact; int sig_len_bin, sig_len_compact; int dh_len_bin, dh_len_compact; int elem_len_bin; }; struct curve_params* curve_by_name(const char *name); struct curve_params* curve_by_pk_len_compact(int len); void curve_release(struct curve_params *cp); #endif /* INC_CURVES_H */ seccure-0.3/TODO0000644000175100001440000000054710470676205013066 0ustar bertramusers- make use of libgcrypt's capabilities to allocate and manage secure memory. Memlocking everything -- this is what we are doing now -- is a bit overkill, perhaps. - implement an authenticated key establishment scheme. STS, IKE? Or perhaps something password based like SRP? - speed up the point multiplication routines using NAF or wNAF techniques. seccure-0.3/seccure.manpage.xml0000644000175100001440000002427310470676205016162 0ustar bertramusers seccure-key [-c curve] [-F pwfile] [-d] [-v] [-q] seccure-encrypt [-m maclen] [-c curve] [-i infile] [-o outfile] [-v] [-q] key seccure-decrypt [-m maclen] [-c curve] [-i infile] [-o outfile] [-F pwfile] [-d] [-v] [-q] seccure-sign [-f] [-b] [-a] [-c curve] [-s sigfile] [-i infile] [-o outfile] [-F pwfile] [-d] [-v] [-q] seccure-verify [-f] [-b] [-a] [-c curve] [-s sigfile] [-i infile] [-o outfile] [-v] [-q] key [sig] seccure-signcrypt [-c sig_curve [-c enc_curve]] [-i infile] [-o outfile] [-F pwfile] [-d] [-v] [-q] key seccure-veridec [-c enc_curve [-c sig_curve]] [-i infile] [-o outfile] [-F pwfile] [-d] [-v] [-q] key seccure-dh [-c curve] [-v] [-q]

The seccure toolset implements a selection of asymmetric algorithms based on elliptic curve cryptography (ECC). In particular it offers public key encryption / decryption, signature generation / verification and key establishment.

ECC schemes offer a much better key size to security ratio than classical systems (RSA, DSA). Keys are short enough to make direct specification of keys on the command line possible (sometimes this is more convenient than the management of PGP-like key rings). seccure builds on this feature and therefore is the tool of choice whenever lightweight asymmetric cryptography -- independent of key servers, revocation certificates, the Web of Trust or even configuration files -- is required.

seccure-key: Prompt for a passphrase and calculate the corresponding public key.

seccure-encrypt: Encrypt a message with public key key.

seccure-decrypt: Prompt for a passphrase and decrypt a seccure-encrypted message.

seccure-sign: Prompt for a passphrase and digitally sign a message.

seccure-verify: Verify signature sig with public key key.

seccure-signcrypt: Sign a message first, encrypt it subsequently (in -b -a and -m 0 mode, respectively). This is basically a shortcut for two separate seccure invocations.

seccure-veridec: Counterpart to signcryption.

seccure-dh: Perform a Diffie-Hellman key exchange.

Given the passphrase 'seccure is secure', run

seccure-key

to determine the corresponding public key (which is '2@DupCaCKykHBe-QHpAP%d%B[' on curve p160).

To encrypt the file 'document.msg' with that key run

seccure-encrypt -i document.msg -o document.enc '2@DupCaCKykHBe-QHpAP%d%B['

The message can be recovered with

seccure-decrypt -i document.enc

To sign the file run

seccure-sign -i document.msg -s document.sig

and enter the passphrase. The signature is stored in 'document.sig' and can be verified with

seccure-verify -i document.msg -s document.sig '2@DupCaCKykHBe-QHpAP%d%B['

seccure-dh performs an interactive Diffie-Hellman key exchange. Two instances have to be run in parallel; the token generated by the first one is the input for the second one and vice versa. The output consists of two shared keys: it is guaranteed that no attacker can ever find out the established key as soon as the two parties can confirm that both have the same verification key. The authentic comparision of the verification keys can, for example, be realized via signed messages or via telephone (using 'voice authentication').

The number in the names of the curves measures their security level. Rule of thumb: the workload to 'break' a k-bit curve is 2^(k/2) approximately (example: it takes about 2^112 steps to break secp224r1). If the 80 bit security of the default curve doesn't seem sufficient, choosing a stronger curve (p192 and upwards) may, of course, be considered. But the suggestion remains: p160 offers reasonable security for everyday use. Warning: the curves p112 and p128 do not satisfy demands for long-time security.

seccure uses derivated versions of ECIES (Elliptic Curve Integrated Encryption Scheme), ECDSA (Elliptic Curve Digital Signature Algorithm) and ECDH (Elliptic Curve Diffie-Hellman) as encryption, signature and key establishment scheme, respectively. For the symmetric parts (bulk encryption, hashing, key derivation, HMAC calculation) seccure builds on AES256 (in CTR mode), SHA256 and SHA512. To my best knowledge no part of seccure is covered by patents. See the file PATENTS for an explicit patent statement.

This software (v0.3) was written by B. Poettering (seccure AT point-at-infinity.org) in 2006. It is released under the terms of the GNU General Public License (GPL). Find the latest version of seccure on the project's homepage: .
seccure-0.3/PATENTS0000644000175100001440000000163310470676205013434 0ustar bertramusers SECCURE Elliptic Curve Crypto Utility for Reliable Encryption http://point-at-infinity.org/seccure/ seccure builds on a bunch of cryptographic algorithms and techniques (a comprehensive list is presented below). To my best knowledge none of these and in particular no part of seccure is covered by patents. - ECIES, ECDSA, ECDH - AES256, SHA256, SHA512, CTR Mode, HMAC Mode - Point compression on elliptic curves over prime fields - Point multiplication using Double-And-Add-Algorithm (may be replaced by a NAF-Algorithm in the feature) Note: Diffie-Hellman key agreement actually has been patented in 1977. The patent expired regularly in 1997. Note: some of the supported elliptic curves are defined over prime fields with moduli of "Crandall type" (which may be patented). Seccure doesn't make use of the special structure these primes offer and therefore doesn't touch the Crandall patents. seccure-0.3/aes256ctr.c0000644000175100001440000000626610470676205014264 0ustar bertramusers/* * seccure - Copyright 2006 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which is * part of the GnuPG project). The code compiles successfully with * libgcrypt 1.2.2. Use the included Makefile to build the binary. * * Compile with -D NOMEMLOCK if your machine doesn't support memory * locking. * * Report bugs to: seccure AT point-at-infinity.org * */ #include #include #include #include "aes256ctr.h" /******************************************************************************/ struct aes256ctr* aes256ctr_init(const char *key) { struct aes256ctr *ac; gcry_error_t err; if (! (ac = malloc(sizeof(struct aes256ctr)))) return NULL; err = gcry_cipher_open(&ac->ch, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CTR, 0); if (gcry_err_code(err)) goto error; err = gcry_cipher_setkey(ac->ch, key, CIPHER_KEY_SIZE); if (gcry_err_code(err)) goto error; err = gcry_cipher_setctr(ac->ch, NULL, 0); if (gcry_err_code(err)) goto error; ac->idx = CIPHER_BLOCK_SIZE; return ac; error: free(ac); return NULL; } void aes256ctr_enc(struct aes256ctr *ac, char *buf, int len) { gcry_error_t err; int full_blocks; for(; len && (ac->idx < CIPHER_BLOCK_SIZE); len--) *buf++ ^= ac->buf[ac->idx++]; full_blocks = (len / CIPHER_BLOCK_SIZE) * CIPHER_BLOCK_SIZE; err = gcry_cipher_encrypt(ac->ch, buf, full_blocks, NULL, 0); assert(! gcry_err_code(err)); len -= full_blocks; buf += full_blocks; if (len) { memset(ac->buf, 0, CIPHER_BLOCK_SIZE); err = gcry_cipher_encrypt(ac->ch, ac->buf, CIPHER_BLOCK_SIZE, NULL, 0); assert(! gcry_err_code(err)); ac->idx = 0; for(; len && (ac->idx < CIPHER_BLOCK_SIZE); len--) *buf++ ^= ac->buf[ac->idx++]; } } void aes256ctr_done(struct aes256ctr *ac) { gcry_cipher_close(ac->ch); memset(ac->buf, 0, CIPHER_BLOCK_SIZE); free(ac); } int hmacsha256_init(gcry_md_hd_t *mh, const char *key) { gcry_error_t err; err = gcry_md_open(mh, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC); if (gcry_err_code(err)) return 0; err = gcry_md_setkey(*mh, key, HMAC_KEY_SIZE); return ! gcry_err_code(err); } seccure-0.3/aes256ctr.h0000644000175100001440000000366310470676205014267 0ustar bertramusers/* * seccure - Copyright 2006 B. Poettering * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA */ /* * SECCURE Elliptic Curve Crypto Utility for Reliable Encryption * * http://point-at-infinity.org/seccure/ * * * seccure implements a selection of asymmetric algorithms based on * elliptic curve cryptography (ECC). See the manpage or the project's * homepage for further details. * * This code links against the GNU gcrypt library "libgcrypt" (which is * part of the GnuPG project). The code compiles successfully with * libgcrypt 1.2.2. Use the included Makefile to build the binary. * * Compile with -D NOMEMLOCK if your machine doesn't support memory * locking. * * Report bugs to: seccure AT point-at-infinity.org * */ #ifndef INC_AES256CTR_H #define INC_AES256CTR_H #include #define CIPHER_BLOCK_SIZE 16 #define CIPHER_KEY_SIZE 32 #define HMAC_KEY_SIZE 32 struct aes256ctr { gcry_cipher_hd_t ch; int idx; char buf[CIPHER_BLOCK_SIZE]; }; struct aes256ctr* aes256ctr_init(const char *key); void aes256ctr_enc(struct aes256ctr *ac, char *buf, int len); void aes256ctr_done(struct aes256ctr *ac); #define aes256ctr_dec aes256ctr_enc int hmacsha256_init(gcry_md_hd_t *mh, const char *key); #endif /* INC_AES256CTR_H */