Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #116 from AvdN/develop

Develop: PolarSSL  1.1.1 integration
  • Loading branch information...
commit 3b197dc78523d274e83395a3eb9484a0552a62fe 2 parents 6ffafa9 + 26e6804
Zed A. Shaw authored
Showing with 4,220 additions and 1,014 deletions.
  1. +2 −2 src/bstr/bsafe.c
  2. +2 −2 src/io.c
  3. +1 −4 src/mongrel2.c
  4. +29 −1 src/polarssl/CMakeLists.txt
  5. +315 −60 src/polarssl/LICENSE
  6. +28 −14 src/polarssl/Makefile
  7. +8 −6 src/polarssl/aes.c
  8. +12 −3 src/polarssl/aes.h
  9. +244 −0 src/polarssl/asn1.h
  10. +260 −0 src/polarssl/asn1parse.c
  11. +2 −2 src/polarssl/base64.h
  12. +35 −21 src/polarssl/bignum.c
  13. +80 −35 src/polarssl/bignum.h
  14. +6 −2 src/polarssl/bn_mul.h
  15. +9 −7 src/polarssl/camellia.c
  16. +34 −1 src/polarssl/camellia.h
  17. +180 −35 src/polarssl/cipher.c
  18. +134 −42 src/polarssl/cipher.h
  19. +298 −52 src/polarssl/cipher_wrap.c
  20. +28 −5 src/polarssl/cipher_wrap.h
  21. +77 −3 src/polarssl/config.h
  22. +566 −0 src/polarssl/ctr_drbg.c
  23. +228 −0 src/polarssl/ctr_drbg.h
  24. +5 −5 src/polarssl/debug.h
  25. +2 −2 src/polarssl/des.c
  26. +4 −1 src/polarssl/des.h
  27. +4 −2 src/polarssl/dhm.c
  28. +5 −3 src/polarssl/dhm.h
  29. +204 −0 src/polarssl/entropy.c
  30. +150 −0 src/polarssl/entropy.h
  31. +136 −0 src/polarssl/entropy_poll.c
  32. +75 −0 src/polarssl/entropy_poll.h
  33. +138 −19 src/polarssl/error.c
  34. +15 −7 src/polarssl/error.h
  35. +21 −7 src/polarssl/havege.c
  36. +5 −1 src/polarssl/havege.h
  37. +23 −15 src/polarssl/md.c
  38. +36 −18 src/polarssl/md.h
  39. +9 −9 src/polarssl/md2.c
  40. +5 −4 src/polarssl/md2.h
  41. +3 −3 src/polarssl/md4.c
  42. +3 −2 src/polarssl/md4.h
  43. +3 −3 src/polarssl/md5.c
  44. +3 −2 src/polarssl/md5.h
  45. +4 −5 src/polarssl/md_wrap.h
  46. +4 −5 src/polarssl/net.c
  47. +3 −3 src/polarssl/net.h
  48. +4 −4 src/polarssl/openssl.h
  49. +1 −1  src/polarssl/padlock.h
  50. +2 −0  src/polarssl/pem.c
  51. +6 −7 src/polarssl/pkcs11.h
  52. +41 −32 src/polarssl/rsa.c
  53. +5 −5 src/polarssl/rsa.h
  54. +3 −3 src/polarssl/sha1.c
  55. +3 −2 src/polarssl/sha1.h
  56. +3 −3 src/polarssl/sha2.c
  57. +3 −2 src/polarssl/sha2.h
  58. +22 −22 src/polarssl/sha4.c
  59. +7 −6 src/polarssl/sha4.h
  60. +41 −16 src/polarssl/ssl.h
  61. +17 −7 src/polarssl/ssl_cli.c
  62. +16 −8 src/polarssl/ssl_srv.c
  63. +74 −24 src/polarssl/ssl_tls.c
  64. +3 −3 src/polarssl/timing.c
  65. +8 −8 src/polarssl/version.h
  66. +31 −82 src/polarssl/x509.h
  67. +465 −359 src/polarssl/x509parse.c
  68. +2 −2 src/unixy.c
  69. +2 −0  src/unixy.h
  70. +1 −1  tools/m2sh/Makefile
  71. +1 −0  tools/procer/Makefile
  72. +21 −4 tools/procer/procer.c
4 src/bstr/bsafe.c
View
@@ -34,7 +34,7 @@ char * strcpy (char *dst, const char *src) {
char * strcat (char *dst, const char *src) {
dst = dst;
src = src;
- fprintf (stderr, "bsafe error: strcat() is not safe, use bstrcat instead.\n");
+ fprintf (stderr, "bsafe error: strcat() is not safe, use bconcat instead.\n");
if (bsafeShouldExit) exit (-1);
return NULL;
}
@@ -61,7 +61,7 @@ char * (strncat) (char *dst, const char *src, size_t n) {
dst = dst;
src = src;
n = n;
- fprintf (stderr, "bsafe error: strncat() is not safe, use bstrcat then btrunc\n\tor cstr2tbstr, btrunc then bstrcat instead.\n");
+ fprintf (stderr, "bsafe error: strncat() is not safe, use bconcat then btrunc\n\tor cstr2tbstr, btrunc then bconcat instead.\n");
if (bsafeShouldExit) exit (-1);
return NULL;
}
4 src/io.c
View
@@ -144,7 +144,7 @@ static int ssl_do_handshake(IOBuf *iob)
check(!iob->handshake_performed, "ssl_do_handshake called unnecessarily");
while((rcode = ssl_handshake(&iob->ssl)) != 0) {
- check(rcode == 0 || rcode == POLARSSL_ERR_NET_WANT_READ
+ check(rcode == POLARSSL_ERR_NET_WANT_READ
|| rcode == POLARSSL_ERR_NET_WANT_WRITE, "Handshake failed with error code: %d", rcode);
}
iob->handshake_performed = 1;
@@ -356,7 +356,7 @@ static inline int iobuf_ssl_setup(IOBuf *buf)
ssl_set_authmode(&buf->ssl, IO_SSL_VERIFY_METHOD);
havege_init(&buf->hs);
- ssl_set_rng(&buf->ssl, havege_rand, &buf->hs);
+ ssl_set_rng(&buf->ssl, havege_random, &buf->hs);
#ifndef DEBUG
ssl_set_dbg(&buf->ssl, ssl_debug, NULL);
5 src/mongrel2.c
View
@@ -224,9 +224,6 @@ int attempt_chroot_drop(Server *srv)
log_info("-- Starting " VERSION ". Copyright (C) Zed A. Shaw. Licensed BSD.\n");
log_info("-- Look in %s for startup messages and errors.", bdata(srv->error_log));
- check(access("/run", F_OK) == 0, "/run directory doesn't exist in %s or isn't owned right.", bdata(srv->chroot));
- check(access("/tmp", F_OK) == 0, "/tmp directory doesn't exist in %s or isn't owned right.", bdata(srv->chroot));
-
rc = Unixy_daemonize();
check(rc == 0, "Failed to daemonize, looks like you're hosed.");
@@ -375,7 +372,7 @@ void taskmain(int argc, char **argv)
dbg_set_log(stderr);
int rc = 0;
- check(argc == 3 || argc == 4, "usage: mongrel2 config.sqlite server_uuid [config_module.so]");
+ check(argc == 3 || argc == 4, "usage: %s config.sqlite server_uuid [config_module.so]", m2program);
if(argc == 4) {
log_info("Using configuration module %s to load configs.", argv[3]);
30 src/polarssl/CMakeLists.txt
View
@@ -1,19 +1,32 @@
-add_library(polarssl STATIC
+option(USE_SHARED_POLARSSL_LIBRARY "Build PolarSSL as a shared library." OFF)
+
+set(src
aes.c
arc4.c
+ asn1parse.c
base64.c
bignum.c
camellia.c
certs.c
+ cipher.c
+ cipher_wrap.c
+ ctr_drbg.c
debug.c
des.c
dhm.c
+ entropy.c
+ entropy_poll.c
+ error.c
havege.c
+ md.c
+ md_wrap.c
md2.c
md4.c
md5.c
net.c
padlock.c
+ pem.c
+ pkcs11.c
rsa.c
sha1.c
sha2.c
@@ -26,3 +39,18 @@ add_library(polarssl STATIC
x509parse.c
xtea.c
)
+
+if(NOT USE_SHARED_POLARSSL_LIBRARY)
+
+add_library(polarssl STATIC ${src})
+
+else(NOT USE_SHARED_POLARSSL_LIBRARY)
+
+add_library(polarssl SHARED ${src})
+set_target_properties(polarssl PROPERTIES VERSION 1.1.1 SOVERSION 1)
+
+endif(NOT USE_SHARED_POLARSSL_LIBRARY)
+
+install(TARGETS polarssl
+ DESTINATION ${LIB_INSTALL_DIR}
+ PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
375 src/polarssl/LICENSE
View
@@ -1,84 +1,339 @@
-PolarSSL is licensed under their FOSS Exception License, or the GPLv2 depending
-on what your usage is. If you plan on releasing a commercial modification of Mongrel2 then you have to either remove PolarSSL, or contact them about a commercial license.
+ GNU GENERAL PUBLIC LICENSE
+ Version 2, June 1991
-Here's the license as of when we started using PolarSSL:
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
+ 51 Franklin Street, 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
-What is the FOSS License Exception?
+ 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 Lesser General Public License instead.) You can apply it to
+your programs, too.
-This Free and Open Source Software ("FOSS") License Exception allows developers
-of FOSS applications to include PolarSSL with their FOSS applications. PolarSSL
-is typically licensed pursuant to version 2 (or later) of the General Public
-License ("GPL"), but this exception permits distribution of PolarSSL with a
-developer.s FOSS applications licensed under the terms of another FOSS license
-listed below, even though such other FOSS license may be incompatible with the
-GPL.
+ 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.
-The following terms and conditions describe the circumstances under which this
-FOSS License Exception applies. FOSS License Exception Terms and Conditions
+ 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.
-1. Definitions.
+ 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.
-"Derivative Work" means a derivative work, as defined under applicable
-copyright law, formed entirely from the Program and one or more FOSS
-Applications.
+ 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.
-"FOSS Application" means a free and open source software application
-distributed subject to a license listed in the section below titled "FOSS
-License List."
+ 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.
-"FOSS Notice" means a notice placed by Brainspark B.V. in a copy of the
-PolarSSL library stating that such copy of the PolarSSL library may be
-distributed under Brainspark's or PolarSSL's FOSS License Exception.
+ 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.
-"Independent Work" means portions of the Derivative Work that are not derived
-from the Program and can reasonably be considered independent and separate
-works.
+ The precise terms and conditions for copying, distribution and
+modification follow.
-"Program" means a copy of Brainspark's PolarSSL library that contains a FOSS
-Notice.
+ 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".
-2. A FOSS application developer ("you" or "your") may distribute a Derivative
-Work provided that you and the Derivative Work meet all of the following
-conditions:
+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 obey the GPL in all respects for the Program and all portions
- (including modifications) of the Program included in the Derivative
- Work (provided that this condition does not apply to Independent
- Works);
+ 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.
- 2. The Derivative Work does not include any work licensed under the GPL
- other than 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.
- 3. You distribute Independent Works subject to a license listed in the
- section below titled "FOSS License List";
+ 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:
- 4. You distribute Independent Works in object code or executable form
- with the complete corresponding machine-readable source code on the
- same medium and under the same FOSS license applying to the object code
- or executable forms;
+ a) You must cause the modified files to carry prominent notices
+ stating that you changed the files and the date of any change.
- 5. All works that are aggregated with the Program or the Derivative
- Work on a medium or volume of storage are not derivative works of the
- Program, Derivative Work or FOSS Application, and must reasonably be
- considered independent and separate works.
+ 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.
-3. Brainspark B.V. reserves all rights not expressly granted in these terms and
-conditions. If all of the above conditions are not met, then this FOSS License
-Exception does not apply to you or your Derivative Work.
+ 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.
-FOSS License List
+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.
-License Name Version(s)/Copyright Date
-Apache Software License 1.0 / 1.1 / 2.0
-BSD License "July 22 1999"
-Creative Commons Zero 1.0
-European Union Public License (EUPL)1 1.0
-GNU Library or "Lesser" General Public License (LGPL) 2.0 / 2.1 / 3.0
-WTFPL 2
-X11 "2001"
-Zlib/libpng License -
+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.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ 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 Street, 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.
+
+ <signature of Ty Coon>, 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 Lesser General
+Public License instead of this License.
42 src/polarssl/Makefile
View
@@ -1,7 +1,7 @@
# Also see "include/polarssl/config.h"
-CFLAGS = -I../include -D_FILE_OFFSET_BITS=64 -Wall -Wdeclaration-after-statement
+CFLAGS += -I../include -D_FILE_OFFSET_BITS=64 -Wall -W -Wdeclaration-after-statement
OFLAGS = -O
# MicroBlaze specific options:
@@ -13,19 +13,29 @@ OFLAGS = -O
# To compile as a shared library:
# CFLAGS += -fPIC
+SONAME=libpolarssl.so.0
+
DLEXT=so
# OSX shared library extension:
# DLEXT=dylib
-OBJS= aes.o arc4.o base64.o \
- bignum.o certs.o debug.o \
- des.o dhm.o havege.o \
- md2.o md4.o md5.o \
- net.o padlock.o rsa.o \
- sha1.o sha2.o sha4.o \
- ssl_cli.o ssl_srv.o ssl_tls.o \
- timing.o x509parse.o xtea.o \
- camellia.o version.o
+# Windows shared library extension:
+# DLEXT=dll
+
+OBJS= aes.o arc4.o asn1parse.o \
+ base64.o bignum.o camellia.o \
+ certs.o cipher.o cipher_wrap.o \
+ ctr_drbg.o debug.o des.o \
+ dhm.o entropy.o entropy_poll.o \
+ error.o havege.o \
+ md.o md_wrap.o md2.o \
+ md4.o md5.o net.o \
+ padlock.o pem.o pkcs11.o \
+ rsa.o sha1.o sha2.o \
+ sha4.o ssl_cli.o ssl_srv.o \
+ ssl_tls.o timing.o version.o \
+ x509parse.o xtea.o
+
.SILENT:
@@ -37,17 +47,21 @@ shared: libpolarssl.$(DLEXT)
libpolarssl.a: $(OBJS)
echo " AR $@"
- ar r $@ $(OBJS)
+ $(AR) r $@ $(OBJS)
echo " RL $@"
- ranlib $@
+ $(AR) s $@
libpolarssl.so: libpolarssl.a
echo " LD $@"
- $(CC) -shared -Wl,-soname,$@ -o $@ $(OBJS)
+ $(CC) ${LDFLAGS} -shared -Wl,-soname,$(SONAME) -o $@ $(OBJS)
libpolarssl.dylib: libpolarssl.a
echo " LD $@"
- $(CC) -dynamiclib -o $@ $(OBJS)
+ $(CC) ${LDFLAGS} -dynamiclib -o $@ $(OBJS)
+
+libpolarssl.dll: libpolarssl.a
+ echo " LD $@"
+ $(CC) -shared -Wl,-soname,$@ -o $@ $(OBJS) -lws2_32 -lwinmm -lgdi32
.c.o:
echo " CC $<"
14 src/polarssl/aes.c
View
@@ -822,12 +822,13 @@ int aes_crypt_cbc( aes_context *ctx,
int aes_crypt_cfb128( aes_context *ctx,
int mode,
size_t length,
- int *iv_off,
+ size_t *iv_off,
unsigned char iv[16],
const unsigned char *input,
unsigned char *output )
{
- int c, n = *iv_off;
+ int c;
+ size_t n = *iv_off;
if( mode == AES_DECRYPT )
{
@@ -867,14 +868,15 @@ int aes_crypt_cfb128( aes_context *ctx,
* AES-CTR buffer encryption/decryption
*/
int aes_crypt_ctr( aes_context *ctx,
- int length,
- int *nc_off,
+ size_t length,
+ size_t *nc_off,
unsigned char nonce_counter[16],
unsigned char stream_block[16],
const unsigned char *input,
unsigned char *output )
{
- int c, n = *nc_off, i, cb;
+ int c, i, cb;
+ size_t n = *nc_off;
while( length-- )
{
@@ -1089,7 +1091,7 @@ int aes_self_test( int verbose )
unsigned char prv[16];
unsigned char iv[16];
#if defined(POLARSSL_CIPHER_MODE_CTR) || defined(POLARSSL_CIPHER_MODE_CFB)
- int offset;
+ size_t offset;
#endif
#if defined(POLARSSL_CIPHER_MODE_CTR)
int len;
15 src/polarssl/aes.h
View
@@ -111,6 +111,11 @@ int aes_crypt_cbc( aes_context *ctx,
/**
* \brief AES-CFB128 buffer encryption/decryption.
*
+ * Note: Due to the nature of CFB you should use the same key schedule for
+ * both encryption and decryption. So a context initialized with
+ * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
+ *
+ * both
* \param ctx AES context
* \param mode AES_ENCRYPT or AES_DECRYPT
* \param length length of the input data
@@ -124,7 +129,7 @@ int aes_crypt_cbc( aes_context *ctx,
int aes_crypt_cfb128( aes_context *ctx,
int mode,
size_t length,
- int *iv_off,
+ size_t *iv_off,
unsigned char iv[16],
const unsigned char *input,
unsigned char *output );
@@ -134,6 +139,10 @@ int aes_crypt_cfb128( aes_context *ctx,
*
* Warning: You have to keep the maximum use of your counter in mind!
*
+ * Note: Due to the nature of CTR you should use the same key schedule for
+ * both encryption and decryption. So a context initialized with
+ * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
+ *
* \param length The length of the data
* \param nc_off The offset in the current stream_block (for resuming
* within current cipher stream). The offset pointer to
@@ -147,8 +156,8 @@ int aes_crypt_cfb128( aes_context *ctx,
* \return 0 if successful
*/
int aes_crypt_ctr( aes_context *ctx,
- int length,
- int *nc_off,
+ size_t length,
+ size_t *nc_off,
unsigned char nonce_counter[16],
unsigned char stream_block[16],
const unsigned char *input,
244 src/polarssl/asn1.h
View
@@ -0,0 +1,244 @@
+/**
+ * \file asn1.h
+ *
+ * \brief Generic ASN.1 parsing
+ *
+ * Copyright (C) 2006-2011, Brainspark B.V.
+ *
+ * This file is part of PolarSSL (http://www.polarssl.org)
+ * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ * All rights reserved.
+ *
+ * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#ifndef POLARSSL_ASN1_H
+#define POLARSSL_ASN1_H
+
+#include "config.h"
+
+#if defined(POLARSSL_BIGNUM_C)
+#include "bignum.h"
+#endif
+
+#include <string.h>
+
+/**
+ * \addtogroup asn1_module
+ * \{
+ */
+
+/**
+ * \name ASN1 Error codes
+ * These error codes are OR'ed to X509 error codes for
+ * higher error granularity.
+ * ASN1 is a standard to specify data structures.
+ * \{
+ */
+#define POLARSSL_ERR_ASN1_OUT_OF_DATA -0x0014 /**< Out of data when parsing an ASN1 data structure. */
+#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG -0x0016 /**< ASN1 tag was of an unexpected value. */
+#define POLARSSL_ERR_ASN1_INVALID_LENGTH -0x0018 /**< Error when trying to determine the length or invalid length. */
+#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH -0x001A /**< Actual length differs from expected length. */
+#define POLARSSL_ERR_ASN1_INVALID_DATA -0x001C /**< Data is invalid. (not used) */
+#define POLARSSL_ERR_ASN1_MALLOC_FAILED -0x001E /**< Memory allocation failed */
+/* \} name */
+
+/**
+ * \name DER constants
+ * These constants comply with DER encoded the ANS1 type tags.
+ * DER encoding uses hexadecimal representation.
+ * An example DER sequence is:\n
+ * - 0x02 -- tag indicating INTEGER
+ * - 0x01 -- length in octets
+ * - 0x05 -- value
+ * Such sequences are typically read into \c ::x509_buf.
+ * \{
+ */
+#define ASN1_BOOLEAN 0x01
+#define ASN1_INTEGER 0x02
+#define ASN1_BIT_STRING 0x03
+#define ASN1_OCTET_STRING 0x04
+#define ASN1_NULL 0x05
+#define ASN1_OID 0x06
+#define ASN1_UTF8_STRING 0x0C
+#define ASN1_SEQUENCE 0x10
+#define ASN1_SET 0x11
+#define ASN1_PRINTABLE_STRING 0x13
+#define ASN1_T61_STRING 0x14
+#define ASN1_IA5_STRING 0x16
+#define ASN1_UTC_TIME 0x17
+#define ASN1_GENERALIZED_TIME 0x18
+#define ASN1_UNIVERSAL_STRING 0x1C
+#define ASN1_BMP_STRING 0x1E
+#define ASN1_PRIMITIVE 0x00
+#define ASN1_CONSTRUCTED 0x20
+#define ASN1_CONTEXT_SPECIFIC 0x80
+/* \} name */
+/* \} addtogroup asn1_module */
+
+/** Returns the size of the binary string, without the trailing \\0 */
+#define OID_SIZE(x) (sizeof(x) - 1)
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \name Functions to parse ASN.1 data structures
+ * \{
+ */
+
+/**
+ * Type-length-value structure that allows for ASN1 using DER.
+ */
+typedef struct _asn1_buf
+{
+ int tag; /**< ASN1 type, e.g. ASN1_UTF8_STRING. */
+ size_t len; /**< ASN1 length, e.g. in octets. */
+ unsigned char *p; /**< ASN1 data, e.g. in ASCII. */
+}
+asn1_buf;
+
+/**
+ * Container for ASN1 bit strings.
+ */
+typedef struct _asn1_bitstring
+{
+ size_t len; /**< ASN1 length, e.g. in octets. */
+ unsigned char unused_bits; /**< Number of unused bits at the end of the string */
+ unsigned char *p; /**< Raw ASN1 data for the bit string */
+}
+asn1_bitstring;
+
+/**
+ * Container for a sequence of ASN.1 items
+ */
+typedef struct _asn1_sequence
+{
+ asn1_buf buf; /**< Buffer containing the given ASN.1 item. */
+ struct _asn1_sequence *next; /**< The next entry in the sequence. */
+}
+asn1_sequence;
+
+/**
+ * Get the length of an ASN.1 element.
+ * Updates the pointer to immediately behind the length.
+ *
+ * \param p The position in the ASN.1 data
+ * \param end End of data
+ * \param len The variable that will receive the value
+ *
+ * \return 0 if successful, POLARSSL_ERR_ASN1_OUT_OF_DATA on reaching
+ * end of data, POLARSSL_ERR_ASN1_INVALID_LENGTH if length is
+ * unparseable.
+ */
+int asn1_get_len( unsigned char **p,
+ const unsigned char *end,
+ size_t *len );
+
+/**
+ * Get the tag and length of the tag. Check for the requested tag.
+ * Updates the pointer to immediately behind the tag and length.
+ *
+ * \param p The position in the ASN.1 data
+ * \param end End of data
+ * \param len The variable that will receive the length
+ * \param tag The expected tag
+ *
+ * \return 0 if successful, POLARSSL_ERR_ASN1_UNEXPECTED_TAG if tag did
+ * not match requested tag, or another specific ASN.1 error code.
+ */
+int asn1_get_tag( unsigned char **p,
+ const unsigned char *end,
+ size_t *len, int tag );
+
+/**
+ * Retrieve a boolean ASN.1 tag and its value.
+ * Updates the pointer to immediately behind the full tag.
+ *
+ * \param p The position in the ASN.1 data
+ * \param end End of data
+ * \param val The variable that will receive the value
+ *
+ * \return 0 if successful or a specific ASN.1 error code.
+ */
+int asn1_get_bool( unsigned char **p,
+ const unsigned char *end,
+ int *val );
+
+/**
+ * Retrieve an integer ASN.1 tag and its value.
+ * Updates the pointer to immediately behind the full tag.
+ *
+ * \param p The position in the ASN.1 data
+ * \param end End of data
+ * \param val The variable that will receive the value
+ *
+ * \return 0 if successful or a specific ASN.1 error code.
+ */
+int asn1_get_int( unsigned char **p,
+ const unsigned char *end,
+ int *val );
+
+/**
+ * Retrieve a bitstring ASN.1 tag and its value.
+ * Updates the pointer to immediately behind the full tag.
+ *
+ * \param p The position in the ASN.1 data
+ * \param end End of data
+ * \param bs The variable that will receive the value
+ *
+ * \return 0 if successful or a specific ASN.1 error code.
+ */
+int asn1_get_bitstring( unsigned char **p, const unsigned char *end,
+ asn1_bitstring *bs);
+
+/**
+ * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
+ * Updated the pointer to immediately behind the full sequence tag.
+ *
+ * \param p The position in the ASN.1 data
+ * \param end End of data
+ * \param cur First variable in the chain to fill
+ * \param tag Type of sequence
+ *
+ * \return 0 if successful or a specific ASN.1 error code.
+ */
+int asn1_get_sequence_of( unsigned char **p,
+ const unsigned char *end,
+ asn1_sequence *cur,
+ int tag);
+
+#if defined(POLARSSL_BIGNUM_C)
+/**
+ * Retrieve a MPI value from an integer ASN.1 tag.
+ * Updates the pointer to immediately behind the full tag.
+ *
+ * \param p The position in the ASN.1 data
+ * \param end End of data
+ * \param X The MPI that will receive the value
+ *
+ * \return 0 if successful or a specific ASN.1 or MPI error code.
+ */
+int asn1_get_mpi( unsigned char **p,
+ const unsigned char *end,
+ mpi *X );
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* asn1.h */
260 src/polarssl/asn1parse.c
View
@@ -0,0 +1,260 @@
+/*
+ * Generic ASN.1 parsing
+ *
+ * Copyright (C) 2006-2011, Brainspark B.V.
+ *
+ * This file is part of PolarSSL (http://www.polarssl.org)
+ * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ * All rights reserved.
+ *
+ * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "polarssl/config.h"
+
+#if defined(POLARSSL_ASN1_PARSE_C)
+
+#include "polarssl/asn1.h"
+
+#if defined(POLARSSL_BIGNUM_C)
+#include "polarssl/bignum.h"
+#endif
+
+#include <string.h>
+#include <stdlib.h>
+#include <time.h>
+
+/*
+ * ASN.1 DER decoding routines
+ */
+int asn1_get_len( unsigned char **p,
+ const unsigned char *end,
+ size_t *len )
+{
+ if( ( end - *p ) < 1 )
+ return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+
+ if( ( **p & 0x80 ) == 0 )
+ *len = *(*p)++;
+ else
+ {
+ switch( **p & 0x7F )
+ {
+ case 1:
+ if( ( end - *p ) < 2 )
+ return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+
+ *len = (*p)[1];
+ (*p) += 2;
+ break;
+
+ case 2:
+ if( ( end - *p ) < 3 )
+ return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+
+ *len = ( (*p)[1] << 8 ) | (*p)[2];
+ (*p) += 3;
+ break;
+
+ case 3:
+ if( ( end - *p ) < 4 )
+ return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+
+ *len = ( (*p)[1] << 16 ) | ( (*p)[2] << 8 ) | (*p)[3];
+ (*p) += 4;
+ break;
+
+ case 4:
+ if( ( end - *p ) < 5 )
+ return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+
+ *len = ( (*p)[1] << 24 ) | ( (*p)[2] << 16 ) | ( (*p)[3] << 8 ) | (*p)[4];
+ (*p) += 5;
+ break;
+
+ default:
+ return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
+ }
+ }
+
+ if( *len > (size_t) ( end - *p ) )
+ return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+
+ return( 0 );
+}
+
+int asn1_get_tag( unsigned char **p,
+ const unsigned char *end,
+ size_t *len, int tag )
+{
+ if( ( end - *p ) < 1 )
+ return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+
+ if( **p != tag )
+ return( POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
+
+ (*p)++;
+
+ return( asn1_get_len( p, end, len ) );
+}
+
+int asn1_get_bool( unsigned char **p,
+ const unsigned char *end,
+ int *val )
+{
+ int ret;
+ size_t len;
+
+ if( ( ret = asn1_get_tag( p, end, &len, ASN1_BOOLEAN ) ) != 0 )
+ return( ret );
+
+ if( len != 1 )
+ return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
+
+ *val = ( **p != 0 ) ? 1 : 0;
+ (*p)++;
+
+ return( 0 );
+}
+
+int asn1_get_int( unsigned char **p,
+ const unsigned char *end,
+ int *val )
+{
+ int ret;
+ size_t len;
+
+ if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
+ return( ret );
+
+ if( len > sizeof( int ) || ( **p & 0x80 ) != 0 )
+ return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
+
+ *val = 0;
+
+ while( len-- > 0 )
+ {
+ *val = ( *val << 8 ) | **p;
+ (*p)++;
+ }
+
+ return( 0 );
+}
+
+#if defined(POLARSSL_BIGNUM_C)
+int asn1_get_mpi( unsigned char **p,
+ const unsigned char *end,
+ mpi *X )
+{
+ int ret;
+ size_t len;
+
+ if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
+ return( ret );
+
+ ret = mpi_read_binary( X, *p, len );
+
+ *p += len;
+
+ return( ret );
+}
+#endif /* POLARSSL_BIGNUM_C */
+
+int asn1_get_bitstring( unsigned char **p, const unsigned char *end,
+ asn1_bitstring *bs)
+{
+ int ret;
+
+ /* Certificate type is a single byte bitstring */
+ if( ( ret = asn1_get_tag( p, end, &bs->len, ASN1_BIT_STRING ) ) != 0 )
+ return( ret );
+
+ /* Check length, subtract one for actual bit string length */
+ if ( bs->len < 1 )
+ return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+ bs->len -= 1;
+
+ /* Get number of unused bits, ensure unused bits <= 7 */
+ bs->unused_bits = **p;
+ if( bs->unused_bits > 7 )
+ return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
+ (*p)++;
+
+ /* Get actual bitstring */
+ bs->p = *p;
+ *p += bs->len;
+
+ if( *p != end )
+ return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
+
+ return 0;
+}
+
+
+/*
+ * Parses and splits an ASN.1 "SEQUENCE OF <tag>"
+ */
+int asn1_get_sequence_of( unsigned char **p,
+ const unsigned char *end,
+ asn1_sequence *cur,
+ int tag)
+{
+ int ret;
+ size_t len;
+ asn1_buf *buf;
+
+ /* Get main sequence tag */
+ if( ( ret = asn1_get_tag( p, end, &len,
+ ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
+ return( ret );
+
+ if( *p + len != end )
+ return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
+
+ while( *p < end )
+ {
+ buf = &(cur->buf);
+ buf->tag = **p;
+
+ if( ( ret = asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
+ return( ret );
+
+ buf->p = *p;
+ *p += buf->len;
+
+ /* Allocate and assign next pointer */
+ if (*p < end)
+ {
+ cur->next = (asn1_sequence *) malloc(
+ sizeof( asn1_sequence ) );
+
+ if( cur->next == NULL )
+ return( POLARSSL_ERR_ASN1_MALLOC_FAILED );
+
+ cur = cur->next;
+ }
+ }
+
+ /* Set final sequence entry's next pointer to NULL */
+ cur->next = NULL;
+
+ if( *p != end )
+ return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
+
+ return( 0 );
+}
+
+#endif
4 src/polarssl/base64.h
View
@@ -29,8 +29,8 @@
#include <string.h>
-#define POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL -0x0010 /**< Output buffer too small. */
-#define POLARSSL_ERR_BASE64_INVALID_CHARACTER -0x0012 /**< Invalid character in input. */
+#define POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */
+#define POLARSSL_ERR_BASE64_INVALID_CHARACTER -0x002C /**< Invalid character in input. */
#ifdef __cplusplus
extern "C" {
56 src/polarssl/bignum.c
View
@@ -89,12 +89,12 @@ int mpi_grow( mpi *X, size_t nblimbs )
t_uint *p;
if( nblimbs > POLARSSL_MPI_MAX_LIMBS )
- return( 1 );
+ return( POLARSSL_ERR_MPI_MALLOC_FAILED );
if( X->n < nblimbs )
{
if( ( p = (t_uint *) malloc( nblimbs * ciL ) ) == NULL )
- return( 1 );
+ return( POLARSSL_ERR_MPI_MALLOC_FAILED );
memset( p, 0, nblimbs * ciL );
@@ -440,13 +440,20 @@ int mpi_read_file( mpi *X, int radix, FILE *fin )
t_uint d;
size_t slen;
char *p;
- char s[1024];
+ /*
+ * Buffer should have space for (short) label and decimal formatted MPI,
+ * newline characters and '\0'
+ */
+ char s[ POLARSSL_MPI_READ_BUFFER_SIZE ];
memset( s, 0, sizeof( s ) );
if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
return( POLARSSL_ERR_MPI_FILE_IO_ERROR );
slen = strlen( s );
+ if( slen == sizeof( s ) - 2 )
+ return( POLARSSL_ERR_MPI_BUFFER_TOO_SMALL );
+
if( s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
if( s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
@@ -465,7 +472,10 @@ int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout )
{
int ret;
size_t n, slen, plen;
- char s[2048];
+ /*
+ * Buffer should have space for minus sign, hexified MPI and '\0'
+ */
+ char s[ 2 * POLARSSL_MPI_MAX_SIZE + 2 ];
n = sizeof( s );
memset( s, 0, n );
@@ -1092,13 +1102,13 @@ int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B )
else
{
#if defined(POLARSSL_HAVE_LONGLONG)
- t_dbl r;
+ t_udbl r;
- r = (t_dbl) X.p[i] << biL;
- r |= (t_dbl) X.p[i - 1];
+ r = (t_udbl) X.p[i] << biL;
+ r |= (t_udbl) X.p[i - 1];
r /= Y.p[t];
- if( r > ((t_dbl) 1 << biL) - 1)
- r = ((t_dbl) 1 << biL) - 1;
+ if( r > ((t_udbl) 1 << biL) - 1)
+ r = ((t_udbl) 1 << biL) - 1;
Z.p[i - t - 1] = (t_uint) r;
#else
@@ -1377,7 +1387,7 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR )
size_t i, j, nblimbs;
size_t bufsize, nbits;
t_uint ei, mm, state;
- mpi RR, T, W[64];
+ mpi RR, T, W[ 2 << POLARSSL_MPI_WINDOW_SIZE ];
if( mpi_cmp_int( N, 0 ) < 0 || ( N->p[0] & 1 ) == 0 )
return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
@@ -1394,6 +1404,9 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR )
wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
( i > 79 ) ? 4 : ( i > 23 ) ? 3 : 1;
+ if( wsize > POLARSSL_MPI_WINDOW_SIZE )
+ wsize = POLARSSL_MPI_WINDOW_SIZE;
+
j = N->n + 1;
MPI_CHK( mpi_grow( X, j ) );
MPI_CHK( mpi_grow( &W[1], j ) );
@@ -1599,18 +1612,16 @@ int mpi_gcd( mpi *G, const mpi *A, const mpi *B )
return( ret );
}
-int mpi_fill_random( mpi *X, size_t size, int (*f_rng)(void *), void *p_rng )
+int mpi_fill_random( mpi *X, size_t size,
+ int (*f_rng)(void *, unsigned char *, size_t),
+ void *p_rng )
{
int ret;
- size_t k;
- unsigned char *p;
MPI_CHK( mpi_grow( X, size ) );
MPI_CHK( mpi_lset( X, 0 ) );
- p = (unsigned char *) X->p;
- for( k = 0; k < X->n * ciL; k++ )
- *p++ = (unsigned char) f_rng( p_rng );
+ MPI_CHK( f_rng( p_rng, (unsigned char *) X->p, X->n * ciL ) );
cleanup:
return( ret );
@@ -1741,7 +1752,9 @@ static const int small_prime[] =
/*
* Miller-Rabin primality test (HAC 4.24)
*/
-int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng )
+int mpi_is_prime( mpi *X,
+ int (*f_rng)(void *, unsigned char *, size_t),
+ void *p_rng )
{
int ret, xs;
size_t i, j, n, s;
@@ -1800,7 +1813,7 @@ int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng )
/*
* pick a random A, 1 < A < |X| - 1
*/
- mpi_fill_random( &A, X->n, f_rng, p_rng );
+ MPI_CHK( mpi_fill_random( &A, X->n, f_rng, p_rng ) );
if( mpi_cmp_mpi( &A, &W ) >= 0 )
{
@@ -1858,20 +1871,21 @@ int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng )
* Prime number generation
*/
int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
- int (*f_rng)(void *), void *p_rng )
+ int (*f_rng)(void *, unsigned char *, size_t),
+ void *p_rng )
{
int ret;
size_t k, n;
mpi Y;
- if( nbits < 3 || nbits > 4096 )
+ if( nbits < 3 || nbits > POLARSSL_MPI_MAX_BITS )
return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
mpi_init( &Y );
n = BITS_TO_LIMBS( nbits );
- mpi_fill_random( X, n, f_rng, p_rng );
+ MPI_CHK( mpi_fill_random( X, n, f_rng, p_rng ) );
k = mpi_msb( X );
if( k < nbits ) MPI_CHK( mpi_shift_l( X, nbits - k ) );
115 src/polarssl/bignum.h
View
@@ -30,13 +30,16 @@
#include <stdio.h>
#include <string.h>
+#include "config.h"
+
#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
#define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
-#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The output buffer is too small to write too. */
+#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
+#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */
#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
@@ -46,6 +49,37 @@
#define POLARSSL_MPI_MAX_LIMBS 10000
/*
+ * Maximum window size used for modular exponentiation. Default: 6
+ * Minimum value: 1. Maximum value: 6.
+ *
+ * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
+ * for the sliding window calculation. (So 64 by default)
+ *
+ * Reduction in size, reduces speed.
+ */
+#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
+
+/*
+ * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
+ * ( Default: 512 bytes => 4096 bits )
+ *
+ * Note: Calculations can results temporarily in larger MPIs. So the number
+ * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
+ */
+#define POLARSSL_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */
+#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
+
+/*
+ * When reading from files with mpi_read_file() the buffer should have space
+ * for a (short) label, the MPI (in the provided radix), the newline
+ * characters and the '\0'.
+ *
+ * By default we assume at least a 10 char label, a minimum radix of 10
+ * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
+ */
+#define POLARSSL_MPI_READ_BUFFER_SIZE 1250
+
+/*
* Define the base integer type, architecture-wise
*/
#if defined(POLARSSL_HAVE_INT8)
@@ -63,10 +97,14 @@ typedef unsigned long t_udbl;
#if defined(_MSC_VER) && defined(_M_IX86)
typedef unsigned __int64 t_udbl;
#else
- #if defined(__amd64__) || defined(__x86_64__) || \
+ #if defined(__GNUC__) && ( \
+ defined(__amd64__) || defined(__x86_64__) || \
defined(__ppc64__) || defined(__powerpc64__) || \
- defined(__ia64__) || defined(__alpha__)
+ defined(__ia64__) || defined(__alpha__) || \
+ (defined(__sparc__) && defined(__arch64__)) || \
+ defined(__s390x__) )
typedef unsigned int t_udbl __attribute__((mode(TI)));
+ #define POLARSSL_HAVE_LONGLONG
#else
#if defined(POLARSSL_HAVE_LONGLONG)
typedef unsigned long long t_udbl;
@@ -112,7 +150,7 @@ void mpi_free( mpi *X );
* \param nblimbs The target number of limbs
*
* \return 0 if successful,
- * 1 if memory allocation failed
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
*/
int mpi_grow( mpi *X, size_t nblimbs );
@@ -123,7 +161,7 @@ int mpi_grow( mpi *X, size_t nblimbs );
* \param Y Source MPI
*
* \return 0 if successful,
- * 1 if memory allocation failed
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
*/
int mpi_copy( mpi *X, const mpi *Y );
@@ -142,7 +180,7 @@ void mpi_swap( mpi *X, mpi *Y );
* \param z Value to use
*
* \return 0 if successful,
- * 1 if memory allocation failed
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
*/
int mpi_lset( mpi *X, t_sint z );
@@ -167,7 +205,7 @@ int mpi_get_bit( mpi *X, size_t pos );
* \param val The value to set the bit to (0 or 1)
*
* \return 0 if successful,
- * 1 if memory allocation failed,
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
*/
int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
@@ -200,7 +238,7 @@ size_t mpi_size( const mpi *X );
* \param radix Input numeric base
* \param s Null-terminated string buffer
*
- * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
+ * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
*/
int mpi_read_string( mpi *X, int radix, const char *s );
@@ -212,7 +250,7 @@ int mpi_read_string( mpi *X, int radix, const char *s );
* \param s String buffer
* \param slen String buffer size
*
- * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code.
+ * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
* *slen is always updated to reflect the amount
* of data that has (or would have) been written.
*
@@ -228,7 +266,9 @@ int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
* \param radix Input numeric base
* \param fin Input file handle
*
- * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
+ * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
+ * the file read buffer is too small or a
+ * POLARSSL_ERR_MPI_XXX error code
*/
int mpi_read_file( mpi *X, int radix, FILE *fin );
@@ -240,7 +280,7 @@ int mpi_read_file( mpi *X, int radix, FILE *fin );
* \param radix Output numeric base
* \param fout Output file handle (can be NULL)
*
- * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
+ * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
*
* \note Set fout == NULL to print X on the console.
*/
@@ -254,7 +294,7 @@ int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
* \param buflen Input buffer size
*
* \return 0 if successful,
- * 1 if memory allocation failed
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
*/
int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
@@ -277,7 +317,7 @@ int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen );
* \param count Amount to shift
*
* \return 0 if successful,
- * 1 if memory allocation failed
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
*/
int mpi_shift_l( mpi *X, size_t count );
@@ -288,7 +328,7 @@ int mpi_shift_l( mpi *X, size_t count );
* \param count Amount to shift
*
* \return 0 if successful,
- * 1 if memory allocation failed
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
*/
int mpi_shift_r( mpi *X, size_t count );
@@ -336,7 +376,7 @@ int mpi_cmp_int( const mpi *X, t_sint z );
* \param B Right-hand MPI
*
* \return 0 if successful,
- * 1 if memory allocation failed
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
*/
int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
@@ -360,7 +400,7 @@ int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
* \param B Right-hand MPI
*
* \return 0 if successful,
- * 1 if memory allocation failed
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
*/
int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
@@ -372,7 +412,7 @@ int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
* \param B Right-hand MPI
*
* \return 0 if successful,
- * 1 if memory allocation failed
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
*/
int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
@@ -384,7 +424,7 @@ int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
* \param b The integer value to add
*
* \return 0 if successful,
- * 1 if memory allocation failed
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
*/
int mpi_add_int( mpi *X, const mpi *A, t_sint b );
@@ -396,7 +436,7 @@ int mpi_add_int( mpi *X, const mpi *A, t_sint b );
* \param b The integer value to subtract
*
* \return 0 if successful,
- * 1 if memory allocation failed
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
*/
int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
@@ -408,7 +448,7 @@ int mpi_sub_int( mpi *X, const mpi *A, t_sint b );
* \param B Right-hand MPI
*
* \return 0 if successful,
- * 1 if memory allocation failed
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
*/
int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
@@ -422,7 +462,7 @@ int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
* \param b The integer value to multiply with
*
* \return 0 if successful,
- * 1 if memory allocation failed
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
*/
int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
@@ -435,7 +475,7 @@ int mpi_mul_int( mpi *X, const mpi *A, t_sint b );
* \param B Right-hand MPI
*
* \return 0 if successful,
- * 1 if memory allocation failed,
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
* POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
*
* \note Either Q or R can be NULL.
@@ -451,7 +491,7 @@ int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
* \param b Integer to divide by
*
* \return 0 if successful,
- * 1 if memory allocation failed,
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
* POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
*
* \note Either Q or R can be NULL.
@@ -466,7 +506,7 @@ int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b );
* \param B Right-hand MPI
*
* \return 0 if successful,
- * 1 if memory allocation failed,
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
* POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
* POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
*/
@@ -480,7 +520,7 @@ int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
* \param b Integer to divide by
*
* \return 0 if successful,
- * 1 if memory allocation failed,
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
* POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
* POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
*/
@@ -496,7 +536,7 @@ int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
* \param _RR Speed-up MPI used for recalculations
*
* \return 0 if successful,
- * 1 if memory allocation failed,
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
*
* \note _RR is used to avoid re-computing R*R mod N across
@@ -514,9 +554,11 @@ int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
* \param p_rng RNG parameter
*
* \return 0 if successful,
- * 1 if memory allocation failed
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
*/
-int mpi_fill_random( mpi *X, size_t size, int (*f_rng)(void *), void *p_rng );
+int mpi_fill_random( mpi *X, size_t size,
+ int (*f_rng)(void *, unsigned char *, size_t),
+ void *p_rng );
/**
* \brief Greatest common divisor: G = gcd(A, B)
@@ -526,7 +568,7 @@ int mpi_fill_random( mpi *X, size_t size, int (*f_rng)(void *), void *p_rng );
* \param B Right-hand MPI
*
* \return 0 if successful,
- * 1 if memory allocation failed
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
*/
int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
@@ -538,7 +580,7 @@ int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
* \param N Right-hand MPI
*
* \return 0 if successful,
- * 1 if memory allocation failed,
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
*/
@@ -552,26 +594,29 @@ int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
* \param p_rng RNG parameter
*
* \return 0 if successful (probably prime),
- * 1 if memory allocation failed,
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
* POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
*/
-int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
+int mpi_is_prime( mpi *X,
+ int (*f_rng)(void *, unsigned char *, size_t),
+ void *p_rng );
/**
* \brief Prime number generation
*
* \param X Destination MPI
- * \param nbits Required size of X in bits ( 3 <= nbits <= 4096 )
+ * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
* \param dh_flag If 1, then (X-1)/2 will be prime too
* \param f_rng RNG function
* \param p_rng RNG parameter
*
* \return 0 if successful (probably prime),
- * 1 if memory allocation failed,
+ * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
*/
int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
- int (*f_rng)(void *), void *p_rng );
+ int (*f_rng)(void *, unsigned char *, size_t),
+ void *p_rng );
/**
* \brief Checkup routine
8 src/polarssl/bn_mul.h
View
@@ -41,7 +41,7 @@
#ifndef POLARSSL_BN_MUL_H
#define POLARSSL_BN_MUL_H
-#include "polarssl/config.h"
+#include "bignum.h"
#if defined(POLARSSL_HAVE_ASM)
@@ -498,6 +498,8 @@
#if defined(__arm__)
+#if !defined(__thumb__)
+
#define MULADDC_INIT \
asm( "ldr r0, %0 " :: "m" (s)); \
asm( "ldr r1, %0 " :: "m" (d)); \
@@ -519,6 +521,8 @@
asm( "str r0, %0 " : "=m" (s) :: \
"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7" );
+#endif /* Thumb */
+
#endif /* ARMv3 */
#if defined(__alpha__)
@@ -697,7 +701,7 @@
t_uint r0, r1;
#define MULADDC_CORE \
- r = *(s++) * (t_dbl) b; \
+ r = *(s++) * (t_udbl) b; \
r0 = r; \
r1 = r >> biL; \
r0 += c; r1 += (r0 < c); \
16 src/polarssl/camellia.c
View
@@ -68,7 +68,7 @@ static const unsigned char SIGMA_CHARS[6][8] =
{ 0xb0, 0x56, 0x88, 0xc2, 0xb3, 0xe6, 0xc1, 0xfd }
};
-#ifdef POLARSSL_CAMELLIA_SMALL_MEMORY
+#if defined(POLARSSL_CAMELLIA_SMALL_MEMORY)
static const unsigned char FSb[256] =
{
@@ -580,12 +580,13 @@ int camellia_crypt_cbc( camellia_context *ctx,
int camellia_crypt_cfb128( camellia_context *ctx,
int mode,
size_t length,
- int *iv_off,
+ size_t *iv_off,
unsigned char iv[16],
const unsigned char *input,
unsigned char *output )
{
- int c, n = *iv_off;
+ int c;
+ size_t n = *iv_off;
if( mode == CAMELLIA_DECRYPT )
{
@@ -625,14 +626,15 @@ int camellia_crypt_cfb128( camellia_context *ctx,
* Camellia-CTR buffer encryption/decryption
*/
int camellia_crypt_ctr( camellia_context *ctx,
- int length,
- int *nc_off,
+ size_t length,
+ size_t *nc_off,
unsigned char nonce_counter[16],
unsigned char stream_block[16],
const unsigned char *input,
unsigned char *output )
{
- int c, n = *nc_off, i, cb;
+ int c, i, cb;
+ size_t n = *nc_off;
while( length-- )
{
@@ -867,7 +869,7 @@ int camellia_self_test( int verbose )
unsigned char dst[16];
unsigned char iv[16];
#if defined(POLARSSL_CIPHER_MODE_CTR)
- int offset, len;
+ size_t offset, len;
unsigned char nonce_counter[16];
unsigned char stream_block[16];
#endif
35 src/polarssl/camellia.h
View
@@ -117,6 +117,10 @@ int camellia_crypt_cbc( camellia_context *ctx,
/**
* \brief CAMELLIA-CFB128 buffer encryption/decryption
*
+ * Note: Due to the nature of CFB you should use the same key schedule for
+ * both encryption and decryption. So a context initialized with
+ * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT.
+ *
* \param ctx CAMELLIA context
* \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT
* \param length length of the input data
@@ -130,11 +134,40 @@ int camellia_crypt_cbc( camellia_context *ctx,
int camellia_crypt_cfb128( camellia_context *ctx,
int mode,
size_t length,
- int *iv_off,
+ size_t *iv_off,
unsigned char iv[16],
const unsigned char *input,
unsigned char *output );
+/*
+ * \brief CAMELLIA-CTR buffer encryption/decryption
+ *
+ * Warning: You have to keep the maximum use of your counter in mind!
+ *
+ * Note: Due to the nature of CTR you should use the same key schedule for
+ * both encryption and decryption. So a context initialized with
+ * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIA_DECRYPT.
+ *
+ * \param length The length of the data
+ * \param nc_off The offset in the current stream_block (for resuming
+ * within current cipher stream). The offset pointer to
+ * should be 0 at the start of a stream.
+ * \param nonce_counter The 128-bit nonce and counter.
+ * \param stream_block The saved stream-block for resuming. Is overwritten
+ * by the function.
+ * \param input The input data stream
+ * \param output The output data stream
+ *
+ * \return 0 if successful
+ */
+int camellia_crypt_ctr( camellia_context *ctx,
+ size_t length,
+ size_t *nc_off,
+ unsigned char nonce_counter[16],
+ unsigned char stream_block[16],
+ const unsigned char *input,
+ unsigned char *output );
+
/**
* \brief Checkup routine
*
215 src/polarssl/cipher.c
View
@@ -46,12 +46,38 @@ static const int supported_ciphers[] = {
POLARSSL_CIPHER_AES_128_CBC,
POLARSSL_CIPHER_AES_192_CBC,
POLARSSL_CIPHER_AES_256_CBC,
+
+#if defined(POLARSSL_CIPHER_MODE_CFB)
+ POLARSSL_CIPHER_AES_128_CFB128,
+ POLARSSL_CIPHER_AES_192_CFB128,
+ POLARSSL_CIPHER_AES_256_CFB128,
+#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
+
+#if defined(POLARSSL_CIPHER_MODE_CTR)
+ POLARSSL_CIPHER_AES_128_CTR,
+ POLARSSL_CIPHER_AES_192_CTR,
+ POLARSSL_CIPHER_AES_256_CTR,
+#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
+
#endif /* defined(POLARSSL_AES_C) */
#if defined(POLARSSL_CAMELLIA_C)
POLARSSL_CIPHER_CAMELLIA_128_CBC,
POLARSSL_CIPHER_CAMELLIA_192_CBC,
POLARSSL_CIPHER_CAMELLIA_256_CBC,
+
+#if defined(POLARSSL_CIPHER_MODE_CFB)
+ POLARSSL_CIPHER_CAMELLIA_128_CFB128,
+ POLARSSL_CIPHER_CAMELLIA_192_CFB128,
+ POLARSSL_CIPHER_CAMELLIA_256_CFB128,
+#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
+
+#if defined(POLARSSL_CIPHER_MODE_CTR)
+ POLARSSL_CIPHER_CAMELLIA_128_CTR,
+ POLARSSL_CIPHER_CAMELLIA_192_CTR,
+ POLARSSL_CIPHER_CAMELLIA_256_CTR,
+#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
+
#endif /* defined(POLARSSL_CAMELLIA_C) */
#if defined(POLARSSL_DES_C)
@@ -68,7 +94,7 @@ const int *cipher_list( void )
return supported_ciphers;
}
-const cipher_info_t *cipher_info_from_type( cipher_type_t cipher_type )
+const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
{
/* Find static cipher information */
switch ( cipher_type )
@@ -80,6 +106,25 @@ const cipher_info_t *cipher_info_from_type( cipher_type_t cipher_type )
return &aes_192_cbc_info;
case POLARSSL_CIPHER_AES_256_CBC:
return &aes_256_cbc_info;
+
+#if defined(POLARSSL_CIPHER_MODE_CFB)
+ case POLARSSL_CIPHER_AES_128_CFB128:
+ return &aes_128_cfb128_info;
+ case POLARSSL_CIPHER_AES_192_CFB128:
+ return &aes_192_cfb128_info;
+ case POLARSSL_CIPHER_AES_256_CFB128:
+ return &aes_256_cfb128_info;
+#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
+
+#if defined(POLARSSL_CIPHER_MODE_CTR)
+ case POLARSSL_CIPHER_AES_128_CTR:
+ return &aes_128_ctr_info;
+ case POLARSSL_CIPHER_AES_192_CTR:
+ return &aes_192_ctr_info;
+ case POLARSSL_CIPHER_AES_256_CTR:
+ return &aes_256_ctr_info;
+#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
+
#endif
#if defined(POLARSSL_CAMELLIA_C)
@@ -89,6 +134,25 @@ const cipher_info_t *cipher_info_from_type( cipher_type_t cipher_type )
return &camellia_192_cbc_info;
case POLARSSL_CIPHER_CAMELLIA_256_CBC:
return &camellia_256_cbc_info;
+
+#if defined(POLARSSL_CIPHER_MODE_CFB)
+ case POLARSSL_CIPHER_CAMELLIA_128_CFB128:
+ return &camellia_128_cfb128_info;
+ case POLARSSL_CIPHER_CAMELLIA_192_CFB128:
+ return &camellia_192_cfb128_info;
+ case POLARSSL_CIPHER_CAMELLIA_256_CFB128:
+ return &camellia_256_cfb128_info;
+#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
+
+#if defined(POLARSSL_CIPHER_MODE_CTR)
+ case POLARSSL_CIPHER_CAMELLIA_128_CTR:
+ return &camellia_128_ctr_info;
+ case POLARSSL_CIPHER_CAMELLIA_192_CTR:
+ return &camellia_192_ctr_info;
+ case POLARSSL_CIPHER_CAMELLIA_256_CTR:
+ return &camellia_256_ctr_info;
+#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
+
#endif
#if defined(POLARSSL_DES_C)
@@ -110,7 +174,7 @@ const cipher_info_t *cipher_info_from_string( const char *cipher_name )
if( NULL == cipher_name )
return NULL;
- /* Get the appropriate digest information */
+ /* Get the appropriate cipher information */
#if defined(POLARSSL_CAMELLIA_C)
if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC );
@@ -118,7 +182,26 @@ const cipher_info_t *cipher_info_from_string( const char *cipher_name )
return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC );
if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC );
+
+#if defined(POLARSSL_CIPHER_MODE_CFB)
+ if( !strcasecmp( "CAMELLIA-128-CFB128", cipher_name ) )
+ return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CFB128 );
+ if( !strcasecmp( "CAMELLIA-192-CFB128", cipher_name ) )
+ return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CFB128 );
+ if( !strcasecmp( "CAMELLIA-256-CFB128", cipher_name ) )
+ return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CFB128 );
+#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
+
+#if defined(POLARSSL_CIPHER_MODE_CTR)
+ if( !strcasecmp( "CAMELLIA-128-CTR", cipher_name ) )
+ return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CTR );
+ if( !strcasecmp( "CAMELLIA-192-CTR", cipher_name ) )
+ return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CTR );
+ if( !strcasecmp( "CAMELLIA-256-CTR", cipher_name ) )
+ return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CTR );
+#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
#endif
+
#if defined(POLARSSL_AES_C)
if( !strcasecmp( "AES-128-CBC", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
@@ -126,7 +209,26 @@ const cipher_info_t *cipher_info_from_string( const char *cipher_name )
return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC );
if( !strcasecmp( "AES-256-CBC", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC );
+
+#if defined(POLARSSL_CIPHER_MODE_CFB)
+ if( !strcasecmp( "AES-128-CFB128", cipher_name ) )
+ return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 );
+ if( !strcasecmp( "AES-192-CFB128", cipher_name ) )
+ return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CFB128 );
+ if( !strcasecmp( "AES-256-CFB128", cipher_name ) )
+ return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CFB128 );
+#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
+
+#if defined(POLARSSL_CIPHER_MODE_CTR)
+ if( !strcasecmp( "AES-128-CTR", cipher_name ) )
+ return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR );
+ if( !strcasecmp( "AES-192-CTR", cipher_name ) )
+ return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CTR );
+ if( !strcasecmp( "AES-256-CTR", cipher_name ) )
+ return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CTR );
+#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
#endif
+
#if defined(POLARSSL_DES_C)
if( !strcasecmp( "DES-CBC", cipher_name ) )
return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
@@ -141,12 +243,12 @@ const cipher_info_t *cipher_info_from_string( const char *cipher_name )
int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
{
if( NULL == cipher_info || NULL == ctx )
- return 1;
+ return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
memset( ctx, 0, sizeof( ctx ) );
- if( NULL == ( ctx->cipher_ctx = cipher_info->ctx_alloc_func() ) )
- return 2;
+ if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
+ return POLARSSL_ERR_CIPHER_ALLOC_FAILED;
ctx->cipher_info = cipher_info;
@@ -156,9 +258,9 @@ int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
int cipher_free_ctx( cipher_context_t *ctx )
{
if( ctx == NULL || ctx->cipher_info == NULL )
- return 1;
+ return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
- ctx->cipher_info->ctx_free_func( ctx->cipher_ctx );
+ ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
return 0;
}
@@ -167,26 +269,33 @@ int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
int key_length, const operation_t operation )
{
if( NULL == ctx || NULL == ctx->cipher_info )
- return 1;
+ return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
ctx->key_length = key_length;
ctx->operation = operation;
- if (POLARSSL_ENCRYPT == operation)
- return ctx->cipher_info->setkey_enc_func( ctx->cipher_ctx, key,
+ /*
+ * For CFB128 and CTR mode always use the encryption key schedule
+ */
+ if( POLARSSL_ENCRYPT == operation ||
+ POLARSSL_MODE_CFB128 == ctx->cipher_info->mode ||
+ POLARSSL_MODE_CTR == ctx->cipher_info->mode )
+ {
+ return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
ctx->key_length );
+ }
- if (POLARSSL_DECRYPT == operation)
- return ctx->cipher_info->setkey_dec_func( ctx->cipher_ctx, key,
+ if( POLARSSL_DECRYPT == operation )
+ return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
ctx->key_length );
- return 1;
+ return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
}
int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
{
if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
- return 1;
+ return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
ctx->unprocessed_len = 0;
@@ -198,12 +307,13 @@ int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
unsigned char *output, size_t *olen )
{
+ int ret;
size_t copy_len = 0;
if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ||
input == output )
{
- return 1;
+ return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
}
*olen = 0;
@@ -235,11 +345,11 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ile
memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
copy_len );
- if( 0 != ctx->cipher_info->cbc_func( ctx->cipher_ctx,
+ if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
- ctx->unprocessed_data, output) )
+ ctx->unprocessed_data, output ) ) )
{
- return 1;
+ return ret;
}
*olen += cipher_get_block_size( ctx );
@@ -271,10 +381,10 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ile
*/
if( ilen )
{
- if( 0 != ctx->cipher_info->cbc_func( ctx->cipher_ctx,
- ctx->operation, ilen, ctx->iv, input, output ) )
+ if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
+ ctx->operation, ilen, ctx->iv, input, output ) ) )
{
- return 1;
+ return ret;
}
*olen += ilen;
}
@@ -282,7 +392,35 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ile
return 0;
}
- return 1;
+ if( ctx->cipher_info->mode == POLARSSL_MODE_CFB128 )
+ {
+ if( 0 != ( ret = ctx->cipher_info->base->cfb128_func( ctx->cipher_ctx,
+ ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
+ input, output ) ) )
+ {
+ return ret;
+ }
+
+ *olen = ilen;
+
+ return 0;
+ }
+
+ if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
+ {
+ if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
+ ilen, &ctx->unprocessed_len, ctx->iv,
+ ctx->unprocessed_data, input, output ) ) )
+ {
+ return ret;
+ }
+
+ *olen = ilen;
+
+ return 0;
+ }
+
+ return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
}
static void add_pkcs_padding( unsigned char *output, size_t output_len,
@@ -295,23 +433,22 @@ static void add_pkcs_padding( unsigned char *output, size_t output_len,
output[data_len + i] = (unsigned char) padding_len;
}
-static int get_pkcs_padding( unsigned char *input, unsigned char input_len,
+static int get_pkcs_padding( unsigned char *input, unsigned int input_len,
size_t *data_len)
{
- int i = 0;
- unsigned char padding_len = 0;
+ unsigned int i, padding_len = 0;
if( NULL == input || NULL == data_len )
- return 1;
+ return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
padding_len = input[input_len - 1];
if( padding_len > input_len )
- return 2;
+ return POLARSSL_ERR_CIPHER_INVALID_PADDING;
for( i = input_len - padding_len; i < input_len; i++ )
if( input[i] != padding_len )
- return 2;
+ return POLARSSL_ERR_CIPHER_INVALID_PADDING;
*data_len = input_len - padding_len;
@@ -320,11 +457,19 @@ static int get_pkcs_padding( unsigned char *input, unsigned char input_len,
int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
{
+ int ret = 0;
+
if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
- return 1;
+ return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
*olen = 0;
+ if( POLARSSL_MODE_CFB128 == ctx->cipher_info->mode ||
+ POLARSSL_MODE_CTR == ctx->cipher_info->mode )
+ {
+ return 0;
+ }
+
if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
{
if( POLARSSL_ENCRYPT == ctx->operation )
@@ -335,15 +480,15 @@ int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
{
/* For decrypt operations, expect a full block */
- return 1;
+ return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
}
/* cipher block */
- if( 0 != ctx->cipher_info->cbc_func( ctx->cipher_ctx, ctx->operation,
- cipher_get_block_size( ctx ), ctx->iv, ctx->unprocessed_data,
- output ) )
+ if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
+ ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
+ ctx->unprocessed_data, output ) ) )
{
- return 1;
+ return ret;
}
/* Set output size for decryption */
@@ -355,7 +500,7 @@ int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
return 0;
}
- return 1;
+ return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
}
#if defined(POLARSSL_SELF_TEST)
176 src/polarssl/cipher.h
View
@@ -5,7 +5,7 @@
*
* \author Adriaan de Jong <dejong@fox-it.com>
*
- * Copyright (C) 2006-2010, Brainspark B.V.
+ * Copyright (C) 2006-2011, Brainspark B.V.
*
* This file is part of PolarSSL (http://www.polarssl.org)
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
@@ -32,9 +32,19 @@
#include <string.h>
-#ifdef _MSC_VER
+#if defined(_MSC_VER) && !defined(inline)
#define inline _inline
-#endif
+#else
+#if defined(__ARMCC_VERSION) && !defined(inline)
+#define inline __inline
+#endif /* __ARMCC_VERSION */
+#endif /*_MSC_VER */
+
+#define POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */
+#define POLARSSL_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters to function. */
+#define POLARSSL_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */
+#define POLARSSL_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
+#define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
typedef enum {
POLARSSL_CIPHER_ID_NONE = 0,
@@ -46,12 +56,24 @@ typedef enum {
typedef enum {
POLARSSL_CIPHER_NONE = 0,
- POLARSSL_CIPHER_CAMELLIA_128_CBC,
- POLARSSL_CIPHER_CAMELLIA_192_CBC,
- POLARSSL_CIPHER_CAMELLIA_256_CBC,
POLARSSL_CIPHER_AES_128_CBC,
POLARSSL_CIPHER_AES_192_CBC,
POLARSSL_CIPHER_AES_256_CBC,
+ POLARSSL_CIPHER_AES_128_CFB128,
+ POLARSSL_CIPHER_AES_192_CFB128,
+ POLARSSL_CIPHER_AES_256_CFB128,
+ POLARSSL_CIPHER_AES_128_CTR,
+ POLARSSL_CIPHER_AES_192_CTR,
+ POLARSSL_CIPHER_AES_256_CTR,