- import of strongswan-2.7.0

- applied patch for charon
This commit is contained in:
Martin Willi
2006-04-28 07:14:48 +00:00
parent 52923c9acb
commit 997358a6c4
2043 changed files with 346842 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
ktmp
version.c
+481
View File
@@ -0,0 +1,481 @@
GNU LIBRARY GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1991 Free Software Foundation, Inc.
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
[This is the first released version of the library GPL. It is
numbered 2 because it goes with version 2 of the ordinary GPL.]
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
Licenses are intended to guarantee your freedom to share and change
free software--to make sure the software is free for all its users.
This license, the Library General Public License, applies to some
specially designated Free Software Foundation software, and to any
other libraries whose authors decide to use it. You can use it for
your libraries, 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 library, or if you modify it.
For example, if you distribute copies of the library, whether gratis
or for a fee, you must give the recipients all the rights that we gave
you. You must make sure that they, too, receive or can get the source
code. If you link a program with the library, you must provide
complete object files to the recipients so that they can relink them
with the library, after making changes to the library and recompiling
it. And you must show them these terms so they know their rights.
Our method of protecting your rights has two steps: (1) copyright
the library, and (2) offer you this license which gives you legal
permission to copy, distribute and/or modify the library.
Also, for each distributor's protection, we want to make certain
that everyone understands that there is no warranty for this free
library. If the library is modified by someone else and passed on, we
want its recipients to know that what they have is not the original
version, 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 companies distributing free
software will individually obtain patent licenses, thus in effect
transforming the program into proprietary software. To prevent this,
we have made it clear that any patent must be licensed for everyone's
free use or not licensed at all.
Most GNU software, including some libraries, is covered by the ordinary
GNU General Public License, which was designed for utility programs. This
license, the GNU Library General Public License, applies to certain
designated libraries. This license is quite different from the ordinary
one; be sure to read it in full, and don't assume that anything in it is
the same as in the ordinary license.
The reason we have a separate public license for some libraries is that
they blur the distinction we usually make between modifying or adding to a
program and simply using it. Linking a program with a library, without
changing the library, is in some sense simply using the library, and is
analogous to running a utility program or application program. However, in
a textual and legal sense, the linked executable is a combined work, a
derivative of the original library, and the ordinary General Public License
treats it as such.
Because of this blurred distinction, using the ordinary General
Public License for libraries did not effectively promote software
sharing, because most developers did not use the libraries. We
concluded that weaker conditions might promote sharing better.
However, unrestricted linking of non-free programs would deprive the
users of those programs of all benefit from the free status of the
libraries themselves. This Library General Public License is intended to
permit developers of non-free programs to use free libraries, while
preserving your freedom as a user of such programs to change the free
libraries that are incorporated in them. (We have not seen how to achieve
this as regards changes in header files, but we have achieved it as regards
changes in the actual functions of the Library.) The hope is that this
will lead to faster development of free libraries.
The precise terms and conditions for copying, distribution and
modification follow. Pay close attention to the difference between a
"work based on the library" and a "work that uses the library". The
former contains code derived from the library, while the latter only
works together with the library.
Note that it is possible for a library to be covered by the ordinary
General Public License rather than by this special one.
GNU LIBRARY GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License Agreement applies to any software library which
contains a notice placed by the copyright holder or other authorized
party saying it may be distributed under the terms of this Library
General Public License (also called "this License"). Each licensee is
addressed as "you".
A "library" means a collection of software functions and/or data
prepared so as to be conveniently linked with application programs
(which use some of those functions and data) to form executables.
The "Library", below, refers to any such software library or work
which has been distributed under these terms. A "work based on the
Library" means either the Library or any derivative work under
copyright law: that is to say, a work containing the Library or a
portion of it, either verbatim or with modifications and/or translated
straightforwardly into another language. (Hereinafter, translation is
included without limitation in the term "modification".)
"Source code" for a work means the preferred form of the work for
making modifications to it. For a library, 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 library.
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running a program using the Library is not restricted, and output from
such a program is covered only if its contents constitute a work based
on the Library (independent of the use of the Library in a tool for
writing it). Whether that is true depends on what the Library does
and what the program that uses the Library does.
1. You may copy and distribute verbatim copies of the Library's
complete 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 distribute a copy of this License along with the
Library.
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 Library or any portion
of it, thus forming a work based on the Library, 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) The modified work must itself be a software library.
b) You must cause the files modified to carry prominent notices
stating that you changed the files and the date of any change.
c) You must cause the whole of the work to be licensed at no
charge to all third parties under the terms of this License.
d) If a facility in the modified Library refers to a function or a
table of data to be supplied by an application program that uses
the facility, other than as an argument passed when the facility
is invoked, then you must make a good faith effort to ensure that,
in the event an application does not supply such function or
table, the facility still operates, and performs whatever part of
its purpose remains meaningful.
(For example, a function in a library to compute square roots has
a purpose that is entirely well-defined independent of the
application. Therefore, Subsection 2d requires that any
application-supplied function or table used by this function must
be optional: if the application does not supply it, the square
root function must still compute square roots.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Library,
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 Library, 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 Library.
In addition, mere aggregation of another work not based on the Library
with the Library (or with a work based on the Library) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may opt to apply the terms of the ordinary GNU General Public
License instead of this License to a given copy of the Library. To do
this, you must alter all the notices that refer to this License, so
that they refer to the ordinary GNU General Public License, version 2,
instead of to this License. (If a newer version than version 2 of the
ordinary GNU General Public License has appeared, then you can specify
that version instead if you wish.) Do not make any other change in
these notices.
Once this change is made in a given copy, it is irreversible for
that copy, so the ordinary GNU General Public License applies to all
subsequent copies and derivative works made from that copy.
This option is useful when you wish to copy part of the code of
the Library into a program that is not a library.
4. You may copy and distribute the Library (or a portion or
derivative of it, under Section 2) in object code or executable form
under the terms of Sections 1 and 2 above provided that you 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.
If distribution of 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 satisfies the requirement to
distribute the source code, even though third parties are not
compelled to copy the source along with the object code.
5. A program that contains no derivative of any portion of the
Library, but is designed to work with the Library by being compiled or
linked with it, is called a "work that uses the Library". Such a
work, in isolation, is not a derivative work of the Library, and
therefore falls outside the scope of this License.
However, linking a "work that uses the Library" with the Library
creates an executable that is a derivative of the Library (because it
contains portions of the Library), rather than a "work that uses the
library". The executable is therefore covered by this License.
Section 6 states terms for distribution of such executables.
When a "work that uses the Library" uses material from a header file
that is part of the Library, the object code for the work may be a
derivative work of the Library even though the source code is not.
Whether this is true is especially significant if the work can be
linked without the Library, or if the work is itself a library. The
threshold for this to be true is not precisely defined by law.
If such an object file uses only numerical parameters, data
structure layouts and accessors, and small macros and small inline
functions (ten lines or less in length), then the use of the object
file is unrestricted, regardless of whether it is legally a derivative
work. (Executables containing this object code plus portions of the
Library will still fall under Section 6.)
Otherwise, if the work is a derivative of the Library, you may
distribute the object code for the work under the terms of Section 6.
Any executables containing that work also fall under Section 6,
whether or not they are linked directly with the Library itself.
6. As an exception to the Sections above, you may also compile or
link a "work that uses the Library" with the Library to produce a
work containing portions of the Library, and distribute that work
under terms of your choice, provided that the terms permit
modification of the work for the customer's own use and reverse
engineering for debugging such modifications.
You must give prominent notice with each copy of the work that the
Library is used in it and that the Library and its use are covered by
this License. You must supply a copy of this License. If the work
during execution displays copyright notices, you must include the
copyright notice for the Library among them, as well as a reference
directing the user to the copy of this License. Also, you must do one
of these things:
a) Accompany the work with the complete corresponding
machine-readable source code for the Library including whatever
changes were used in the work (which must be distributed under
Sections 1 and 2 above); and, if the work is an executable linked
with the Library, with the complete machine-readable "work that
uses the Library", as object code and/or source code, so that the
user can modify the Library and then relink to produce a modified
executable containing the modified Library. (It is understood
that the user who changes the contents of definitions files in the
Library will not necessarily be able to recompile the application
to use the modified definitions.)
b) Accompany the work with a written offer, valid for at
least three years, to give the same user the materials
specified in Subsection 6a, above, for a charge no more
than the cost of performing this distribution.
c) If distribution of the work is made by offering access to copy
from a designated place, offer equivalent access to copy the above
specified materials from the same place.
d) Verify that the user has already received a copy of these
materials or that you have already sent this user a copy.
For an executable, the required form of the "work that uses the
Library" must include any data and utility programs needed for
reproducing the executable from it. 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.
It may happen that this requirement contradicts the license
restrictions of other proprietary libraries that do not normally
accompany the operating system. Such a contradiction means you cannot
use both them and the Library together in an executable that you
distribute.
7. You may place library facilities that are a work based on the
Library side-by-side in a single library together with other library
facilities not covered by this License, and distribute such a combined
library, provided that the separate distribution of the work based on
the Library and of the other library facilities is otherwise
permitted, and provided that you do these two things:
a) Accompany the combined library with a copy of the same work
based on the Library, uncombined with any other library
facilities. This must be distributed under the terms of the
Sections above.
b) Give prominent notice with the combined library of the fact
that part of it is a work based on the Library, and explaining
where to find the accompanying uncombined form of the same work.
8. You may not copy, modify, sublicense, link with, or distribute
the Library except as expressly provided under this License. Any
attempt otherwise to copy, modify, sublicense, link with, or
distribute the Library 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.
9. 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 Library or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Library (or any work based on the
Library), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Library or works based on it.
10. Each time you redistribute the Library (or any work based on the
Library), the recipient automatically receives a license from the
original licensor to copy, distribute, link with or modify the Library
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.
11. 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 Library at all. For example, if a patent
license would not permit royalty-free redistribution of the Library 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 Library.
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.
12. If the distribution and/or use of the Library is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Library 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.
13. The Free Software Foundation may publish revised and/or new
versions of the Library 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 Library
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 Library does not specify a
license version number, you may choose any version ever published by
the Free Software Foundation.
14. If you wish to incorporate parts of the Library into other free
programs whose distribution conditions are incompatible with these,
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
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE LIBRARY "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
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. 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 LIBRARY 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
LIBRARY (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 LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), 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 Libraries
If you develop a new library, and you want it to be of the greatest
possible use to the public, we recommend making it free software that
everyone can redistribute and change. You can do so by permitting
redistribution under these terms (or, alternatively, under the terms of the
ordinary General Public License).
To apply these terms, attach the following notices to the library. 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 library's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Also add information on how to contact you by electronic and paper mail.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the library, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the
library `Frob' (a library for tweaking knobs) written by James Random Hacker.
<signature of Ty Coon>, 1 April 1990
Ty Coon, President of Vice
That's all there is to it!
+41
View File
@@ -0,0 +1,41 @@
# Makefile for the KLIPS interface utilities
# Copyright (C) 1998, 1999 Henry Spencer.
# Copyright (C) 1999, 2000, 2001 Richard Guy Briggs
#
# 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. See <http://www.fsf.org/copyleft/gpl.txt>.
#
# 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.
#
# RCSID $Id: Makefile,v 1.1 2004/03/15 20:35:24 as Exp $
FREESWANSRCDIR=..
include ${FREESWANSRCDIR}/Makefile.inc
SUBDIRS=libfreeswan libdes
ifeq ($(USE_LWRES),true)
SUBDIRS+=liblwres
endif
ifeq ($(USE_IPSECPOLICY),true)
SUBDIRS+=libipsecpolicy
endif
def:
@echo "Please read doc/intro.html or INSTALL before running make"
@false
# programs
cleanall distclean mostlyclean realclean install programs checkprograms check clean spotless install_file_list:
@for d in $(SUBDIRS) ; \
do \
(cd $$d && $(MAKE) FREESWANSRCDIR=$(FREESWANSRCDIR)/.. $@ ) || exit 1; \
done; \
+65
View File
@@ -0,0 +1,65 @@
# FreeS/WAN library
# Copyright (C) 1998-2002 Henry Spencer.
#
# 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. See <http://www.fsf.org/copyleft/gpl.txt>.
#
# 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.
#
# RCSID $Id: Makefile.kernel,v 1.1 2004/03/15 20:35:24 as Exp $
include ../Makefile.inc
include ../Makefile.ver
ifndef TOPDIR
TOPDIR := /usr/src/linux
endif
L_TARGET := libkernel.a
obj-y := addrtoa.o datatot.o goodmask.o \
pfkey_v2_build.o pfkey_v2_debug.o pfkey_v2_ext_bits.o pfkey_v2_parse.o \
prng.o rangetoa.o satoa.o \
subnetof.o subnettoa.o ultoa.o version.o
HDRS=freeswan.h internal.h
EXTRA_CFLAGS += -I. $(KLIPSCOMPILE)
EXTRA_CFLAGS += -Wall
#EXTRA_CFLAGS += -Wconversion
#EXTRA_CFLAGS += -Wmissing-prototypes
EXTRA_CFLAGS += -Wpointer-arith
#EXTRA_CFLAGS += -Wcast-qual
#EXTRA_CFLAGS += -Wmissing-declarations
EXTRA_CFLAGS += -Wstrict-prototypes
#EXTRA_CFLAGS += -pedantic
#EXTRA_CFLAGS += -W
#EXTRA_CFLAGS += -Wwrite-strings
#EXTRA_CFLAGS += -Wbad-function-cast
active-objs := $(sort $(obj-y) $(obj-m))
L_OBJS := $(obj-y)
M_OBJS := $(obj-m)
MIX_OBJS := $(filter $(export-objs), $(active-objs))
include $(TOPDIR)/Rules.make
$(obj-y): $(HDRS)
# build version.c using version number from Makefile.ver
version.c: version.in.c
sed '/"/s/xxx/$(IPSECVERSION)/' version.in.c >$@
clean:
rm -f $(L_TARGET) *.o try* core *.core version.c
( cd des && $(MAKE) clean )
+3
View File
@@ -0,0 +1,3 @@
These are general library functions used in many places in FreeS/WAN.
They are under the GNU library license; see COPYING.LIB.
+110
View File
@@ -0,0 +1,110 @@
#ifndef _CBC_GENERIC_H
#define _CBC_GENERIC_H
/*
* CBC macro helpers
*
* Author: JuanJo Ciarlante <[email protected]>
*
* 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. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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.
*
*/
/*
* Heavily inspired in loop_AES
*/
#define CBC_IMPL_BLK16(name, ctx_type, addr_type, enc_func, dec_func) \
int name(ctx_type *ctx, const u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t * iv, int encrypt) { \
int ret=ilen, pos; \
const u_int32_t *iv_i; \
if ((ilen) % 16) return 0; \
if (encrypt) { \
pos=0; \
while(pos<ilen) { \
if (pos==0) \
iv_i=(const u_int32_t*) iv; \
else \
iv_i=(const u_int32_t*) (out-16); \
*((u_int32_t *)(&out[ 0])) = iv_i[0]^*((const u_int32_t *)(&in[ 0])); \
*((u_int32_t *)(&out[ 4])) = iv_i[1]^*((const u_int32_t *)(&in[ 4])); \
*((u_int32_t *)(&out[ 8])) = iv_i[2]^*((const u_int32_t *)(&in[ 8])); \
*((u_int32_t *)(&out[12])) = iv_i[3]^*((const u_int32_t *)(&in[12])); \
enc_func(ctx, (addr_type) out, (addr_type) out); \
in+=16; \
out+=16; \
pos+=16; \
} \
} else { \
pos=ilen-16; \
in+=pos; \
out+=pos; \
while(pos>=0) { \
dec_func(ctx, (const addr_type) in, (addr_type) out); \
if (pos==0) \
iv_i=(const u_int32_t*) (iv); \
else \
iv_i=(const u_int32_t*) (in-16); \
*((u_int32_t *)(&out[ 0])) ^= iv_i[0]; \
*((u_int32_t *)(&out[ 4])) ^= iv_i[1]; \
*((u_int32_t *)(&out[ 8])) ^= iv_i[2]; \
*((u_int32_t *)(&out[12])) ^= iv_i[3]; \
in-=16; \
out-=16; \
pos-=16; \
} \
} \
return ret; \
}
#define CBC_IMPL_BLK8(name, ctx_type, addr_type, enc_func, dec_func) \
int name(ctx_type *ctx, u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t * iv, int encrypt) { \
int ret=ilen, pos; \
const u_int32_t *iv_i; \
if ((ilen) % 8) return 0; \
if (encrypt) { \
pos=0; \
while(pos<ilen) { \
if (pos==0) \
iv_i=(const u_int32_t*) iv; \
else \
iv_i=(const u_int32_t*) (out-8); \
*((u_int32_t *)(&out[ 0])) = iv_i[0]^*((const u_int32_t *)(&in[ 0])); \
*((u_int32_t *)(&out[ 4])) = iv_i[1]^*((const u_int32_t *)(&in[ 4])); \
enc_func(ctx, (addr_type)out, (addr_type)out); \
in+=8; \
out+=8; \
pos+=8; \
} \
} else { \
pos=ilen-8; \
in+=pos; \
out+=pos; \
while(pos>=0) { \
dec_func(ctx, (const addr_type)in, (addr_type)out); \
if (pos==0) \
iv_i=(const u_int32_t*) (iv); \
else \
iv_i=(const u_int32_t*) (in-8); \
*((u_int32_t *)(&out[ 0])) ^= iv_i[0]; \
*((u_int32_t *)(&out[ 4])) ^= iv_i[1]; \
in-=8; \
out-=8; \
pos-=8; \
} \
} \
return ret; \
}
#define CBC_DECL(name, ctx_type) \
int name(ctx_type *ctx, u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t * iv, int encrypt)
/*
Eg.:
CBC_IMPL_BLK16(AES_cbc_encrypt, aes_context, u_int8_t *, aes_encrypt, aes_decrypt);
CBC_DECL(AES_cbc_encrypt, aes_context);
*/
#endif /* _CBC_GENERIC_H */
+60
View File
@@ -0,0 +1,60 @@
#ifndef _HMAC_GENERIC_H
#define _HMAC_GENERIC_H
/*
* HMAC macro helpers
*
* Author: JuanJo Ciarlante <[email protected]>
*
* 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. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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.
*
*/
#ifndef DIVUP
#define DIVUP(x,y) ((x + y -1) / y) /* divide, rounding upwards */
#endif
#ifndef HMAC_IPAD
#define HMAC_IPAD 0x36
#define HMAC_OPAD 0x5C
#endif
#define HMAC_SET_KEY_IMPL(func_name, hctx_t, blocksize, func_init, func_update) \
void func_name(hctx_t *hctx, const u_int8_t * key, int keylen) { \
int i;\
u_int8_t kb[blocksize]; \
for (i = 0; i < DIVUP(keylen*8, 8); i++) { \
kb[i] = key[i] ^ HMAC_IPAD; \
} \
for (; i < blocksize; i++) { \
kb[i] = HMAC_IPAD; \
} \
func_init(&hctx->ictx); \
func_update(&hctx->ictx, kb, blocksize); \
for (i = 0; i < blocksize; i++) { \
kb[i] ^= (HMAC_IPAD ^ HMAC_OPAD); \
} \
func_init(&hctx->octx); \
func_update(&hctx->octx, kb, blocksize); \
}
#define HMAC_HASH_IMPL(func_name, hctx_t, ctx_t, ahlen, func_update, func_result ) \
void func_name(hctx_t *hctx, const u_int8_t * dat, int len, u_int8_t * hash, int hashlen) { \
ctx_t ctx; \
ctx=hctx->ictx; \
if (dat) func_update(&ctx, dat, len); \
if (hash) { \
u_int8_t hash_buf[ahlen]; \
func_result(&ctx, hash_buf, ahlen); \
ctx=hctx->octx; \
func_update(&ctx, hash_buf, ahlen); \
func_result(&ctx, hash, hashlen); \
memset(&ctx, 0, sizeof (ctx)); \
memset(&hash_buf, 0, sizeof (hash_buf));\
} \
}
#endif /* _HMAC_GENERIC_H */
+607
View File
@@ -0,0 +1,607 @@
/* crypto/md32_common.h */
/* ====================================================================
* Copyright (c) 1999 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* [email protected].
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* ([email protected]). This product includes software written by Tim
* Hudson ([email protected]).
*
*/
/*
* This is a generic 32 bit "collector" for message digest algorithms.
* Whenever needed it collects input character stream into chunks of
* 32 bit values and invokes a block function that performs actual hash
* calculations.
*
* Porting guide.
*
* Obligatory macros:
*
* DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN
* this macro defines byte order of input stream.
* HASH_CBLOCK
* size of a unit chunk HASH_BLOCK operates on.
* HASH_LONG
* has to be at lest 32 bit wide, if it's wider, then
* HASH_LONG_LOG2 *has to* be defined along
* HASH_CTX
* context structure that at least contains following
* members:
* typedef struct {
* ...
* HASH_LONG Nl,Nh;
* HASH_LONG data[HASH_LBLOCK];
* int num;
* ...
* } HASH_CTX;
* HASH_UPDATE
* name of "Update" function, implemented here.
* HASH_TRANSFORM
* name of "Transform" function, implemented here.
* HASH_FINAL
* name of "Final" function, implemented here.
* HASH_BLOCK_HOST_ORDER
* name of "block" function treating *aligned* input message
* in host byte order, implemented externally.
* HASH_BLOCK_DATA_ORDER
* name of "block" function treating *unaligned* input message
* in original (data) byte order, implemented externally (it
* actually is optional if data and host are of the same
* "endianess").
* HASH_MAKE_STRING
* macro convering context variables to an ASCII hash string.
*
* Optional macros:
*
* B_ENDIAN or L_ENDIAN
* defines host byte-order.
* HASH_LONG_LOG2
* defaults to 2 if not states otherwise.
* HASH_LBLOCK
* assumed to be HASH_CBLOCK/4 if not stated otherwise.
* HASH_BLOCK_DATA_ORDER_ALIGNED
* alternative "block" function capable of treating
* aligned input message in original (data) order,
* implemented externally.
*
* MD5 example:
*
* #define DATA_ORDER_IS_LITTLE_ENDIAN
*
* #define HASH_LONG MD5_LONG
* #define HASH_LONG_LOG2 MD5_LONG_LOG2
* #define HASH_CTX MD5_CTX
* #define HASH_CBLOCK MD5_CBLOCK
* #define HASH_LBLOCK MD5_LBLOCK
* #define HASH_UPDATE MD5_Update
* #define HASH_TRANSFORM MD5_Transform
* #define HASH_FINAL MD5_Final
* #define HASH_BLOCK_HOST_ORDER md5_block_host_order
* #define HASH_BLOCK_DATA_ORDER md5_block_data_order
*
* <[email protected]>
*/
#if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
#error "DATA_ORDER must be defined!"
#endif
#ifndef HASH_CBLOCK
#error "HASH_CBLOCK must be defined!"
#endif
#ifndef HASH_LONG
#error "HASH_LONG must be defined!"
#endif
#ifndef HASH_CTX
#error "HASH_CTX must be defined!"
#endif
#ifndef HASH_UPDATE
#error "HASH_UPDATE must be defined!"
#endif
#ifndef HASH_TRANSFORM
#error "HASH_TRANSFORM must be defined!"
#endif
#ifndef HASH_FINAL
#error "HASH_FINAL must be defined!"
#endif
#ifndef HASH_BLOCK_HOST_ORDER
#error "HASH_BLOCK_HOST_ORDER must be defined!"
#endif
#if 0
/*
* Moved below as it's required only if HASH_BLOCK_DATA_ORDER_ALIGNED
* isn't defined.
*/
#ifndef HASH_BLOCK_DATA_ORDER
#error "HASH_BLOCK_DATA_ORDER must be defined!"
#endif
#endif
#ifndef HASH_LBLOCK
#define HASH_LBLOCK (HASH_CBLOCK/4)
#endif
#ifndef HASH_LONG_LOG2
#define HASH_LONG_LOG2 2
#endif
/*
* Engage compiler specific rotate intrinsic function if available.
*/
#undef ROTATE
#ifndef PEDANTIC
# if defined(_MSC_VER)
# define ROTATE(a,n) _lrotl(a,n)
# elif defined(__MWERKS__)
# if defined(__POWERPC__)
# define ROTATE(a,n) __rlwinm(a,n,0,31)
# elif defined(__MC68K__)
/* Motorola specific tweak. <[email protected]> */
# define ROTATE(a,n) ( n<24 ? __rol(a,n) : __ror(a,32-n) )
# else
# define ROTATE(a,n) __rol(a,n)
# endif
# elif defined(__GNUC__) && __GNUC__>=2 && !defined(NO_ASM) && !defined(NO_INLINE_ASM)
/*
* Some GNU C inline assembler templates. Note that these are
* rotates by *constant* number of bits! But that's exactly
* what we need here...
*
* <[email protected]>
*/
# if defined(__i386)
# define ROTATE(a,n) ({ register unsigned int ret; \
asm ( \
"roll %1,%0" \
: "=r"(ret) \
: "I"(n), "0"(a) \
: "cc"); \
ret; \
})
# elif defined(__powerpc) || defined(__ppc)
# define ROTATE(a,n) ({ register unsigned int ret; \
asm ( \
"rlwinm %0,%1,%2,0,31" \
: "=r"(ret) \
: "r"(a), "I"(n)); \
ret; \
})
# endif
# endif
/*
* Engage compiler specific "fetch in reverse byte order"
* intrinsic function if available.
*/
# if defined(__GNUC__) && __GNUC__>=2 && !defined(NO_ASM) && !defined(NO_INLINE_ASM)
/* some GNU C inline assembler templates by <[email protected]> */
# if defined(__i386) && !defined(I386_ONLY)
# define BE_FETCH32(a) ({ register unsigned int l=(a);\
asm ( \
"bswapl %0" \
: "=r"(l) : "0"(l)); \
l; \
})
# elif defined(__powerpc)
# define LE_FETCH32(a) ({ register unsigned int l; \
asm ( \
"lwbrx %0,0,%1" \
: "=r"(l) \
: "r"(a)); \
l; \
})
# elif defined(__sparc) && defined(ULTRASPARC)
# define LE_FETCH32(a) ({ register unsigned int l; \
asm ( \
"lda [%1]#ASI_PRIMARY_LITTLE,%0"\
: "=r"(l) \
: "r"(a)); \
l; \
})
# endif
# endif
#endif /* PEDANTIC */
#if HASH_LONG_LOG2==2 /* Engage only if sizeof(HASH_LONG)== 4 */
/* A nice byte order reversal from Wei Dai <[email protected]> */
#ifdef ROTATE
/* 5 instructions with rotate instruction, else 9 */
#define REVERSE_FETCH32(a,l) ( \
l=*(const HASH_LONG *)(a), \
((ROTATE(l,8)&0x00FF00FF)|(ROTATE((l&0x00FF00FF),24))) \
)
#else
/* 6 instructions with rotate instruction, else 8 */
#define REVERSE_FETCH32(a,l) ( \
l=*(const HASH_LONG *)(a), \
l=(((l>>8)&0x00FF00FF)|((l&0x00FF00FF)<<8)), \
ROTATE(l,16) \
)
/*
* Originally the middle line started with l=(((l&0xFF00FF00)>>8)|...
* It's rewritten as above for two reasons:
* - RISCs aren't good at long constants and have to explicitely
* compose 'em with several (well, usually 2) instructions in a
* register before performing the actual operation and (as you
* already realized:-) having same constant should inspire the
* compiler to permanently allocate the only register for it;
* - most modern CPUs have two ALUs, but usually only one has
* circuitry for shifts:-( this minor tweak inspires compiler
* to schedule shift instructions in a better way...
*
* <[email protected]>
*/
#endif
#endif
#ifndef ROTATE
#define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
#endif
/*
* Make some obvious choices. E.g., HASH_BLOCK_DATA_ORDER_ALIGNED
* and HASH_BLOCK_HOST_ORDER ought to be the same if input data
* and host are of the same "endianess". It's possible to mask
* this with blank #define HASH_BLOCK_DATA_ORDER though...
*
* <[email protected]>
*/
#if defined(B_ENDIAN)
# if defined(DATA_ORDER_IS_BIG_ENDIAN)
# if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
# define HASH_BLOCK_DATA_ORDER_ALIGNED HASH_BLOCK_HOST_ORDER
# endif
# elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
# ifndef HOST_FETCH32
# ifdef LE_FETCH32
# define HOST_FETCH32(p,l) LE_FETCH32(p)
# elif defined(REVERSE_FETCH32)
# define HOST_FETCH32(p,l) REVERSE_FETCH32(p,l)
# endif
# endif
# endif
#elif defined(L_ENDIAN)
# if defined(DATA_ORDER_IS_LITTLE_ENDIAN)
# if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
# define HASH_BLOCK_DATA_ORDER_ALIGNED HASH_BLOCK_HOST_ORDER
# endif
# elif defined(DATA_ORDER_IS_BIG_ENDIAN)
# ifndef HOST_FETCH32
# ifdef BE_FETCH32
# define HOST_FETCH32(p,l) BE_FETCH32(p)
# elif defined(REVERSE_FETCH32)
# define HOST_FETCH32(p,l) REVERSE_FETCH32(p,l)
# endif
# endif
# endif
#endif
#if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
#ifndef HASH_BLOCK_DATA_ORDER
#error "HASH_BLOCK_DATA_ORDER must be defined!"
#endif
#endif
#if defined(DATA_ORDER_IS_BIG_ENDIAN)
#define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
l|=(((unsigned long)(*((c)++)))<<16), \
l|=(((unsigned long)(*((c)++)))<< 8), \
l|=(((unsigned long)(*((c)++))) ), \
l)
#define HOST_p_c2l(c,l,n) { \
switch (n) { \
case 0: l =((unsigned long)(*((c)++)))<<24; \
case 1: l|=((unsigned long)(*((c)++)))<<16; \
case 2: l|=((unsigned long)(*((c)++)))<< 8; \
case 3: l|=((unsigned long)(*((c)++))); \
} }
#define HOST_p_c2l_p(c,l,sc,len) { \
switch (sc) { \
case 0: l =((unsigned long)(*((c)++)))<<24; \
if (--len == 0) break; \
case 1: l|=((unsigned long)(*((c)++)))<<16; \
if (--len == 0) break; \
case 2: l|=((unsigned long)(*((c)++)))<< 8; \
} }
/* NOTE the pointer is not incremented at the end of this */
#define HOST_c2l_p(c,l,n) { \
l=0; (c)+=n; \
switch (n) { \
case 3: l =((unsigned long)(*(--(c))))<< 8; \
case 2: l|=((unsigned long)(*(--(c))))<<16; \
case 1: l|=((unsigned long)(*(--(c))))<<24; \
} }
#define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
*((c)++)=(unsigned char)(((l)>>16)&0xff), \
*((c)++)=(unsigned char)(((l)>> 8)&0xff), \
*((c)++)=(unsigned char)(((l) )&0xff), \
l)
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
#define HOST_c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
l|=(((unsigned long)(*((c)++)))<< 8), \
l|=(((unsigned long)(*((c)++)))<<16), \
l|=(((unsigned long)(*((c)++)))<<24), \
l)
#define HOST_p_c2l(c,l,n) { \
switch (n) { \
case 0: l =((unsigned long)(*((c)++))); \
case 1: l|=((unsigned long)(*((c)++)))<< 8; \
case 2: l|=((unsigned long)(*((c)++)))<<16; \
case 3: l|=((unsigned long)(*((c)++)))<<24; \
} }
#define HOST_p_c2l_p(c,l,sc,len) { \
switch (sc) { \
case 0: l =((unsigned long)(*((c)++))); \
if (--len == 0) break; \
case 1: l|=((unsigned long)(*((c)++)))<< 8; \
if (--len == 0) break; \
case 2: l|=((unsigned long)(*((c)++)))<<16; \
} }
/* NOTE the pointer is not incremented at the end of this */
#define HOST_c2l_p(c,l,n) { \
l=0; (c)+=n; \
switch (n) { \
case 3: l =((unsigned long)(*(--(c))))<<16; \
case 2: l|=((unsigned long)(*(--(c))))<< 8; \
case 1: l|=((unsigned long)(*(--(c)))); \
} }
#define HOST_l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
*((c)++)=(unsigned char)(((l)>> 8)&0xff), \
*((c)++)=(unsigned char)(((l)>>16)&0xff), \
*((c)++)=(unsigned char)(((l)>>24)&0xff), \
l)
#endif
/*
* Time for some action:-)
*/
void HASH_UPDATE (HASH_CTX *c, const void *data_, unsigned long len)
{
const unsigned char *data=data_;
register HASH_LONG * p;
register unsigned long l;
int sw,sc,ew,ec;
if (len==0) return;
l=(c->Nl+(len<<3))&0xffffffffL;
/* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
* Wei Dai <[email protected]> for pointing it out. */
if (l < c->Nl) /* overflow */
c->Nh++;
c->Nh+=(len>>29);
c->Nl=l;
if (c->num != 0)
{
p=c->data;
sw=c->num>>2;
sc=c->num&0x03;
if ((c->num+len) >= HASH_CBLOCK)
{
l=p[sw]; HOST_p_c2l(data,l,sc); p[sw++]=l;
for (; sw<HASH_LBLOCK; sw++)
{
HOST_c2l(data,l); p[sw]=l;
}
HASH_BLOCK_HOST_ORDER (c,p,1);
len-=(HASH_CBLOCK-c->num);
c->num=0;
/* drop through and do the rest */
}
else
{
c->num+=len;
if ((sc+len) < 4) /* ugly, add char's to a word */
{
l=p[sw]; HOST_p_c2l_p(data,l,sc,len); p[sw]=l;
}
else
{
ew=(c->num>>2);
ec=(c->num&0x03);
l=p[sw]; HOST_p_c2l(data,l,sc); p[sw++]=l;
for (; sw < ew; sw++)
{
HOST_c2l(data,l); p[sw]=l;
}
if (ec)
{
HOST_c2l_p(data,l,ec); p[sw]=l;
}
}
return;
}
}
sw=len/HASH_CBLOCK;
if (sw > 0)
{
#if defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
/*
* Note that HASH_BLOCK_DATA_ORDER_ALIGNED gets defined
* only if sizeof(HASH_LONG)==4.
*/
if ((((unsigned long)data)%4) == 0)
{
/* data is properly aligned so that we can cast it: */
HASH_BLOCK_DATA_ORDER_ALIGNED (c,(HASH_LONG *)data,sw);
sw*=HASH_CBLOCK;
data+=sw;
len-=sw;
}
else
#if !defined(HASH_BLOCK_DATA_ORDER)
while (sw--)
{
memcpy (p=c->data,data,HASH_CBLOCK);
HASH_BLOCK_DATA_ORDER_ALIGNED(c,p,1);
data+=HASH_CBLOCK;
len-=HASH_CBLOCK;
}
#endif
#endif
#if defined(HASH_BLOCK_DATA_ORDER)
{
HASH_BLOCK_DATA_ORDER(c,data,sw);
sw*=HASH_CBLOCK;
data+=sw;
len-=sw;
}
#endif
}
if (len!=0)
{
p = c->data;
c->num = len;
ew=len>>2; /* words to copy */
ec=len&0x03;
for (; ew; ew--,p++)
{
HOST_c2l(data,l); *p=l;
}
HOST_c2l_p(data,l,ec);
*p=l;
}
}
void HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data)
{
#if defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
if ((((unsigned long)data)%4) == 0)
/* data is properly aligned so that we can cast it: */
HASH_BLOCK_DATA_ORDER_ALIGNED (c,(HASH_LONG *)data,1);
else
#if !defined(HASH_BLOCK_DATA_ORDER)
{
memcpy (c->data,data,HASH_CBLOCK);
HASH_BLOCK_DATA_ORDER_ALIGNED (c,c->data,1);
}
#endif
#endif
#if defined(HASH_BLOCK_DATA_ORDER)
HASH_BLOCK_DATA_ORDER (c,data,1);
#endif
}
void HASH_FINAL (unsigned char *md, HASH_CTX *c)
{
register HASH_LONG *p;
register unsigned long l;
register int i,j;
static const unsigned char end[4]={0x80,0x00,0x00,0x00};
const unsigned char *cp=end;
/* c->num should definitly have room for at least one more byte. */
p=c->data;
i=c->num>>2;
j=c->num&0x03;
#if 0
/* purify often complains about the following line as an
* Uninitialized Memory Read. While this can be true, the
* following p_c2l macro will reset l when that case is true.
* This is because j&0x03 contains the number of 'valid' bytes
* already in p[i]. If and only if j&0x03 == 0, the UMR will
* occur but this is also the only time p_c2l will do
* l= *(cp++) instead of l|= *(cp++)
* Many thanks to Alex Tang <[email protected]> for pickup this
* 'potential bug' */
#ifdef PURIFY
if (j==0) p[i]=0; /* Yeah, but that's not the way to fix it:-) */
#endif
l=p[i];
#else
l = (j==0) ? 0 : p[i];
#endif
HOST_p_c2l(cp,l,j); p[i++]=l; /* i is the next 'undefined word' */
if (i>(HASH_LBLOCK-2)) /* save room for Nl and Nh */
{
if (i<HASH_LBLOCK) p[i]=0;
HASH_BLOCK_HOST_ORDER (c,p,1);
i=0;
}
for (; i<(HASH_LBLOCK-2); i++)
p[i]=0;
#if defined(DATA_ORDER_IS_BIG_ENDIAN)
p[HASH_LBLOCK-2]=c->Nh;
p[HASH_LBLOCK-1]=c->Nl;
#elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
p[HASH_LBLOCK-2]=c->Nl;
p[HASH_LBLOCK-1]=c->Nh;
#endif
HASH_BLOCK_HOST_ORDER (c,p,1);
#ifndef HASH_MAKE_STRING
#error "HASH_MAKE_STRING must be defined!"
#else
HASH_MAKE_STRING(c,md);
#endif
c->num=0;
/* clear stuff, HASH_BLOCK may be leaving some stuff on the stack
* but I'm not worried :-)
memset((void *)c,0,sizeof(HASH_CTX));
*/
}
+40
View File
@@ -0,0 +1,40 @@
CFLAGS=-O3 -fomit-frame-pointer -D__KERNEL__ -Wall -Wcast-qual $(EXTRA_CFLAGS)
INC=-I../include
AES_CORE_OBJ:=aes.o
ASM-$(ARCH_ASM):=1
ASM_X86:=$(ASM-i586)$(ASM-i686)
ifneq ($(strip $(ASM_X86)),)
AES_CORE_OBJ:= asm/aes-i586.o
endif
LIBOBJ := aes_xcbc_mac.o aes_cbc.o $(AES_CORE_OBJ)
LDLIBS := -laes
LDFLAGS := -L.
BLIB := libaes.a
L_TARGET := $(BLIB)
.c.o:
$(CC) $(CPPFLAGS) $(CFLAGS) $(INC) -c $< -o $@
.S.o:
$(CC) $(AFLAGS) -c $< -o $@
$(BLIB): $(LIBOBJ)
/bin/rm -f $(BLIB)
ar cr $(BLIB) $(LIBOBJ)
-if test -s /bin/ranlib; then /bin/ranlib $(BLIB); \
else if test -s /usr/bin/ranlib; then /usr/bin/ranlib $(BLIB); \
else exit 0; fi; fi
testx: test_main_mac.o $(BLIB)
$(CC) -o $@ $^
test: test_main.o $(BLIB)
$(CC) -o $@ $^
clean:
rm -f *.[oa] asm/*.o core $(TARGET) test testx
File diff suppressed because it is too large Load Diff
+97
View File
@@ -0,0 +1,97 @@
// I retain copyright in this code but I encourage its free use provided
// that I don't carry any responsibility for the results. I am especially
// happy to see it used in free and open source software. If you do use
// it I would appreciate an acknowledgement of its origin in the code or
// the product that results and I would also appreciate knowing a little
// about the use to which it is being put. I am grateful to Frank Yellin
// for some ideas that are used in this implementation.
//
// Dr B. R. Gladman <[email protected]> 6th April 2001.
//
// This is an implementation of the AES encryption algorithm (Rijndael)
// designed by Joan Daemen and Vincent Rijmen. This version is designed
// to provide both fixed and dynamic block and key lengths and can also
// run with either big or little endian internal byte order (see aes.h).
// It inputs block and key lengths in bytes with the legal values being
// 16, 24 and 32.
/*
* Modified by Jari Ruusu, May 1 2001
* - Fixed some compile warnings, code was ok but gcc warned anyway.
* - Changed basic types: byte -> unsigned char, word -> u_int32_t
* - Major name space cleanup: Names visible to outside now begin
* with "aes_" or "AES_". A lot of stuff moved from aes.h to aes.c
* - Removed C++ and DLL support as part of name space cleanup.
* - Eliminated unnecessary recomputation of tables. (actual bug fix)
* - Merged precomputed constant tables to aes.c file.
* - Removed data alignment restrictions for portability reasons.
* - Made block and key lengths accept bit count (128/192/256)
* as well byte count (16/24/32).
* - Removed all error checks. This change also eliminated the need
* to preinitialize the context struct to zero.
* - Removed some totally unused constants.
*/
#ifndef _AES_H
#define _AES_H
#if defined(__linux__) && defined(__KERNEL__)
# include <linux/types.h>
#else
# include <sys/types.h>
#endif
// CONFIGURATION OPTIONS (see also aes.c)
//
// Define AES_BLOCK_SIZE to set the cipher block size (16, 24 or 32) or
// leave this undefined for dynamically variable block size (this will
// result in much slower code).
// IMPORTANT NOTE: AES_BLOCK_SIZE is in BYTES (16, 24, 32 or undefined). If
// left undefined a slower version providing variable block length is compiled
#define AES_BLOCK_SIZE 16
// The number of key schedule words for different block and key lengths
// allowing for method of computation which requires the length to be a
// multiple of the key length
//
// Nk = 4 6 8
// -------------
// Nb = 4 | 60 60 64
// 6 | 96 90 96
// 8 | 120 120 120
#if !defined(AES_BLOCK_SIZE) || (AES_BLOCK_SIZE == 32)
#define AES_KS_LENGTH 120
#define AES_RC_LENGTH 29
#else
#define AES_KS_LENGTH 4 * AES_BLOCK_SIZE
#define AES_RC_LENGTH (9 * AES_BLOCK_SIZE) / 8 - 8
#endif
typedef struct
{
u_int32_t aes_Nkey; // the number of words in the key input block
u_int32_t aes_Nrnd; // the number of cipher rounds
u_int32_t aes_e_key[AES_KS_LENGTH]; // the encryption key schedule
u_int32_t aes_d_key[AES_KS_LENGTH]; // the decryption key schedule
#if !defined(AES_BLOCK_SIZE)
u_int32_t aes_Ncol; // the number of columns in the cipher state
#endif
} aes_context;
// THE CIPHER INTERFACE
#if !defined(AES_BLOCK_SIZE)
extern void aes_set_blk(aes_context *, const int);
#endif
extern void aes_set_key(aes_context *, const unsigned char [], const int, const int);
extern void aes_encrypt(const aes_context *, const unsigned char [], unsigned char []);
extern void aes_decrypt(const aes_context *, const unsigned char [], unsigned char []);
// The block length inputs to aes_set_block and aes_set_key are in numbers
// of bytes or bits. The calls to subroutines must be made in the above
// order but multiple calls can be made without repeating earlier calls
// if their parameters have not changed.
#endif // _AES_H
+13
View File
@@ -0,0 +1,13 @@
#ifdef __KERNEL__
#include <linux/types.h>
#else
#include <sys/types.h>
#endif
#include "aes_cbc.h"
#include "cbc_generic.h"
/* returns bool success */
int AES_set_key(aes_context *aes_ctx, const u_int8_t *key, int keysize) {
aes_set_key(aes_ctx, key, keysize, 0);
return 1;
}
CBC_IMPL_BLK16(AES_cbc_encrypt, aes_context, u_int8_t *, aes_encrypt, aes_decrypt);
+4
View File
@@ -0,0 +1,4 @@
/* Glue header */
#include "aes.h"
int AES_set_key(aes_context *aes_ctx, const u_int8_t * key, int keysize);
int AES_cbc_encrypt(aes_context *ctx, const u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t * iv, int encrypt);
+67
View File
@@ -0,0 +1,67 @@
#ifdef __KERNEL__
#include <linux/types.h>
#include <linux/kernel.h>
#define DEBUG(x)
#else
#include <stdio.h>
#include <sys/types.h>
#define DEBUG(x) x
#endif
#include "aes.h"
#include "aes_xcbc_mac.h"
int AES_xcbc_mac_set_key(aes_context_mac *ctxm, const u_int8_t *key, int keylen)
{
int ret=1;
aes_block kn[3] = {
{ 0x01010101, 0x01010101, 0x01010101, 0x01010101 },
{ 0x02020202, 0x02020202, 0x02020202, 0x02020202 },
{ 0x03030303, 0x03030303, 0x03030303, 0x03030303 },
};
aes_set_key(&ctxm->ctx_k1, key, keylen, 0);
aes_encrypt(&ctxm->ctx_k1, (u_int8_t *) kn[0], (u_int8_t *) kn[0]);
aes_encrypt(&ctxm->ctx_k1, (u_int8_t *) kn[1], (u_int8_t *) ctxm->k2);
aes_encrypt(&ctxm->ctx_k1, (u_int8_t *) kn[2], (u_int8_t *) ctxm->k3);
aes_set_key(&ctxm->ctx_k1, (u_int8_t *) kn[0], 16, 0);
return ret;
}
static void do_pad_xor(u_int8_t *out, const u_int8_t *in, int len) {
int pos=0;
for (pos=1; pos <= 16; pos++, in++, out++) {
if (pos <= len)
*out ^= *in;
if (pos > len) {
DEBUG(printf("put 0x80 at pos=%d\n", pos));
*out ^= 0x80;
break;
}
}
}
static void xor_block(aes_block res, const aes_block op) {
res[0] ^= op[0];
res[1] ^= op[1];
res[2] ^= op[2];
res[3] ^= op[3];
}
int AES_xcbc_mac_hash(const aes_context_mac *ctxm, const u_int8_t * in, int ilen, u_int8_t hash[16]) {
int ret=ilen;
u_int32_t out[4] = { 0, 0, 0, 0 };
for (; ilen > 16 ; ilen-=16) {
xor_block(out, (const u_int32_t*) &in[0]);
aes_encrypt(&ctxm->ctx_k1, in, (u_int8_t *)&out[0]);
in+=16;
}
do_pad_xor((u_int8_t *)&out, in, ilen);
if (ilen==16) {
DEBUG(printf("using k3\n"));
xor_block(out, ctxm->k3);
}
else
{
DEBUG(printf("using k2\n"));
xor_block(out, ctxm->k2);
}
aes_encrypt(&ctxm->ctx_k1, (u_int8_t *)out, hash);
return ret;
}
+12
View File
@@ -0,0 +1,12 @@
#ifndef _AES_XCBC_MAC_H
#define _AES_XCBC_MAC_H
typedef u_int32_t aes_block[4];
typedef struct {
aes_context ctx_k1;
aes_block k2;
aes_block k3;
} aes_context_mac;
int AES_xcbc_mac_set_key(aes_context_mac *ctxm, const u_int8_t *key, int keylen);
int AES_xcbc_mac_hash(const aes_context_mac *ctxm, const u_int8_t * in, int ilen, u_int8_t hash[16]);
#endif /* _AES_XCBC_MAC_H */
+892
View File
@@ -0,0 +1,892 @@
//
// Copyright (c) 2001, Dr Brian Gladman <brg@gladman.uk.net>, Worcester, UK.
// All rights reserved.
//
// TERMS
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted subject to the following conditions:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. The copyright holder's name must not be used to endorse or promote
// any products derived from this software without his specific prior
// written permission.
//
// This software is provided 'as is' with no express or implied warranties
// of correctness or fitness for purpose.
// Modified by Jari Ruusu, December 24 2001
// - Converted syntax to GNU CPP/assembler syntax
// - C programming interface converted back to "old" API
// - Minor portability cleanups and speed optimizations
// An AES (Rijndael) implementation for the Pentium. This version only
// implements the standard AES block length (128 bits, 16 bytes). This code
// does not preserve the eax, ecx or edx registers or the artihmetic status
// flags. However, the ebx, esi, edi, and ebp registers are preserved across
// calls.
// void aes_set_key(aes_context *cx, const unsigned char key[], const int key_len, const int f)
// void aes_encrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
// void aes_decrypt(const aes_context *cx, const unsigned char in_blk[], unsigned char out_blk[])
#if defined(USE_UNDERLINE)
# define aes_set_key _aes_set_key
# define aes_encrypt _aes_encrypt
# define aes_decrypt _aes_decrypt
#endif
#if !defined(ALIGN32BYTES)
# define ALIGN32BYTES 32
#endif
.file "aes-i586.S"
.globl aes_set_key
.globl aes_encrypt
.globl aes_decrypt
#define tlen 1024 // length of each of 4 'xor' arrays (256 32-bit words)
// offsets to parameters with one register pushed onto stack
#define ctx 8 // AES context structure
#define in_blk 12 // input byte array address parameter
#define out_blk 16 // output byte array address parameter
// offsets in context structure
#define nkey 0 // key length, size 4
#define nrnd 4 // number of rounds, size 4
#define ekey 8 // encryption key schedule base address, size 256
#define dkey 264 // decryption key schedule base address, size 256
// This macro performs a forward encryption cycle. It is entered with
// the first previous round column values in %eax, %ebx, %esi and %edi and
// exits with the final values in the same registers.
#define fwd_rnd(p1,p2) \
mov %ebx,(%esp) ;\
movzbl %al,%edx ;\
mov %eax,%ecx ;\
mov p2(%ebp),%eax ;\
mov %edi,4(%esp) ;\
mov p2+12(%ebp),%edi ;\
xor p1(,%edx,4),%eax ;\
movzbl %ch,%edx ;\
shr $16,%ecx ;\
mov p2+4(%ebp),%ebx ;\
xor p1+tlen(,%edx,4),%edi ;\
movzbl %cl,%edx ;\
movzbl %ch,%ecx ;\
xor p1+3*tlen(,%ecx,4),%ebx ;\
mov %esi,%ecx ;\
mov p1+2*tlen(,%edx,4),%esi ;\
movzbl %cl,%edx ;\
xor p1(,%edx,4),%esi ;\
movzbl %ch,%edx ;\
shr $16,%ecx ;\
xor p1+tlen(,%edx,4),%ebx ;\
movzbl %cl,%edx ;\
movzbl %ch,%ecx ;\
xor p1+2*tlen(,%edx,4),%eax ;\
mov (%esp),%edx ;\
xor p1+3*tlen(,%ecx,4),%edi ;\
movzbl %dl,%ecx ;\
xor p2+8(%ebp),%esi ;\
xor p1(,%ecx,4),%ebx ;\
movzbl %dh,%ecx ;\
shr $16,%edx ;\
xor p1+tlen(,%ecx,4),%eax ;\
movzbl %dl,%ecx ;\
movzbl %dh,%edx ;\
xor p1+2*tlen(,%ecx,4),%edi ;\
mov 4(%esp),%ecx ;\
xor p1+3*tlen(,%edx,4),%esi ;\
movzbl %cl,%edx ;\
xor p1(,%edx,4),%edi ;\
movzbl %ch,%edx ;\
shr $16,%ecx ;\
xor p1+tlen(,%edx,4),%esi ;\
movzbl %cl,%edx ;\
movzbl %ch,%ecx ;\
xor p1+2*tlen(,%edx,4),%ebx ;\
xor p1+3*tlen(,%ecx,4),%eax
// This macro performs an inverse encryption cycle. It is entered with
// the first previous round column values in %eax, %ebx, %esi and %edi and
// exits with the final values in the same registers.
#define inv_rnd(p1,p2) \
movzbl %al,%edx ;\
mov %ebx,(%esp) ;\
mov %eax,%ecx ;\
mov p2(%ebp),%eax ;\
mov %edi,4(%esp) ;\
mov p2+4(%ebp),%ebx ;\
xor p1(,%edx,4),%eax ;\
movzbl %ch,%edx ;\
shr $16,%ecx ;\
mov p2+12(%ebp),%edi ;\
xor p1+tlen(,%edx,4),%ebx ;\
movzbl %cl,%edx ;\
movzbl %ch,%ecx ;\
xor p1+3*tlen(,%ecx,4),%edi ;\
mov %esi,%ecx ;\
mov p1+2*tlen(,%edx,4),%esi ;\
movzbl %cl,%edx ;\
xor p1(,%edx,4),%esi ;\
movzbl %ch,%edx ;\
shr $16,%ecx ;\
xor p1+tlen(,%edx,4),%edi ;\
movzbl %cl,%edx ;\
movzbl %ch,%ecx ;\
xor p1+2*tlen(,%edx,4),%eax ;\
mov (%esp),%edx ;\
xor p1+3*tlen(,%ecx,4),%ebx ;\
movzbl %dl,%ecx ;\
xor p2+8(%ebp),%esi ;\
xor p1(,%ecx,4),%ebx ;\
movzbl %dh,%ecx ;\
shr $16,%edx ;\
xor p1+tlen(,%ecx,4),%esi ;\
movzbl %dl,%ecx ;\
movzbl %dh,%edx ;\
xor p1+2*tlen(,%ecx,4),%edi ;\
mov 4(%esp),%ecx ;\
xor p1+3*tlen(,%edx,4),%eax ;\
movzbl %cl,%edx ;\
xor p1(,%edx,4),%edi ;\
movzbl %ch,%edx ;\
shr $16,%ecx ;\
xor p1+tlen(,%edx,4),%eax ;\
movzbl %cl,%edx ;\
movzbl %ch,%ecx ;\
xor p1+2*tlen(,%edx,4),%ebx ;\
xor p1+3*tlen(,%ecx,4),%esi
// AES (Rijndael) Encryption Subroutine
.text
.align ALIGN32BYTES
aes_encrypt:
push %ebp
mov ctx(%esp),%ebp // pointer to context
mov in_blk(%esp),%ecx
push %ebx
push %esi
push %edi
mov nrnd(%ebp),%edx // number of rounds
lea ekey+16(%ebp),%ebp // key pointer
// input four columns and xor in first round key
mov (%ecx),%eax
mov 4(%ecx),%ebx
mov 8(%ecx),%esi
mov 12(%ecx),%edi
xor -16(%ebp),%eax
xor -12(%ebp),%ebx
xor -8(%ebp),%esi
xor -4(%ebp),%edi
sub $8,%esp // space for register saves on stack
sub $10,%edx
je aes_15
add $32,%ebp
sub $2,%edx
je aes_13
add $32,%ebp
fwd_rnd(aes_ft_tab,-64) // 14 rounds for 256-bit key
fwd_rnd(aes_ft_tab,-48)
aes_13: fwd_rnd(aes_ft_tab,-32) // 12 rounds for 192-bit key
fwd_rnd(aes_ft_tab,-16)
aes_15: fwd_rnd(aes_ft_tab,0) // 10 rounds for 128-bit key
fwd_rnd(aes_ft_tab,16)
fwd_rnd(aes_ft_tab,32)
fwd_rnd(aes_ft_tab,48)
fwd_rnd(aes_ft_tab,64)
fwd_rnd(aes_ft_tab,80)
fwd_rnd(aes_ft_tab,96)
fwd_rnd(aes_ft_tab,112)
fwd_rnd(aes_ft_tab,128)
fwd_rnd(aes_fl_tab,144) // last round uses a different table
// move final values to the output array.
mov out_blk+20(%esp),%ebp
add $8,%esp
mov %eax,(%ebp)
mov %ebx,4(%ebp)
mov %esi,8(%ebp)
mov %edi,12(%ebp)
pop %edi
pop %esi
pop %ebx
pop %ebp
ret
// AES (Rijndael) Decryption Subroutine
.align ALIGN32BYTES
aes_decrypt:
push %ebp
mov ctx(%esp),%ebp // pointer to context
mov in_blk(%esp),%ecx
push %ebx
push %esi
push %edi
mov nrnd(%ebp),%edx // number of rounds
lea dkey+16(%ebp),%ebp // key pointer
// input four columns and xor in first round key
mov (%ecx),%eax
mov 4(%ecx),%ebx
mov 8(%ecx),%esi
mov 12(%ecx),%edi
xor -16(%ebp),%eax
xor -12(%ebp),%ebx
xor -8(%ebp),%esi
xor -4(%ebp),%edi
sub $8,%esp // space for register saves on stack
sub $10,%edx
je aes_25
add $32,%ebp
sub $2,%edx
je aes_23
add $32,%ebp
inv_rnd(aes_it_tab,-64) // 14 rounds for 256-bit key
inv_rnd(aes_it_tab,-48)
aes_23: inv_rnd(aes_it_tab,-32) // 12 rounds for 192-bit key
inv_rnd(aes_it_tab,-16)
aes_25: inv_rnd(aes_it_tab,0) // 10 rounds for 128-bit key
inv_rnd(aes_it_tab,16)
inv_rnd(aes_it_tab,32)
inv_rnd(aes_it_tab,48)
inv_rnd(aes_it_tab,64)
inv_rnd(aes_it_tab,80)
inv_rnd(aes_it_tab,96)
inv_rnd(aes_it_tab,112)
inv_rnd(aes_it_tab,128)
inv_rnd(aes_il_tab,144) // last round uses a different table
// move final values to the output array.
mov out_blk+20(%esp),%ebp
add $8,%esp
mov %eax,(%ebp)
mov %ebx,4(%ebp)
mov %esi,8(%ebp)
mov %edi,12(%ebp)
pop %edi
pop %esi
pop %ebx
pop %ebp
ret
// AES (Rijndael) Key Schedule Subroutine
// input/output parameters
#define aes_cx 12 // AES context
#define in_key 16 // key input array address
#define key_ln 20 // key length, bytes (16,24,32) or bits (128,192,256)
#define ed_flg 24 // 0=create both encr/decr keys, 1=create encr key only
// offsets for locals
#define cnt -4
#define kpf -8
#define slen 8
// This macro performs a column mixing operation on an input 32-bit
// word to give a 32-bit result. It uses each of the 4 bytes in the
// the input column to index 4 different tables of 256 32-bit words
// that are xored together to form the output value.
#define mix_col(p1) \
movzbl %bl,%ecx ;\
mov p1(,%ecx,4),%eax ;\
movzbl %bh,%ecx ;\
ror $16,%ebx ;\
xor p1+tlen(,%ecx,4),%eax ;\
movzbl %bl,%ecx ;\
xor p1+2*tlen(,%ecx,4),%eax ;\
movzbl %bh,%ecx ;\
xor p1+3*tlen(,%ecx,4),%eax
// Key Schedule Macros
#define ksc4(p1) \
rol $24,%ebx ;\
mix_col(aes_fl_tab) ;\
ror $8,%ebx ;\
xor 4*p1+aes_rcon_tab,%eax ;\
xor %eax,%esi ;\
xor %esi,%ebp ;\
mov %esi,16*p1(%edi) ;\
mov %ebp,16*p1+4(%edi) ;\
xor %ebp,%edx ;\
xor %edx,%ebx ;\
mov %edx,16*p1+8(%edi) ;\
mov %ebx,16*p1+12(%edi)
#define ksc6(p1) \
rol $24,%ebx ;\
mix_col(aes_fl_tab) ;\
ror $8,%ebx ;\
xor 4*p1+aes_rcon_tab,%eax ;\
xor 24*p1-24(%edi),%eax ;\
mov %eax,24*p1(%edi) ;\
xor 24*p1-20(%edi),%eax ;\
mov %eax,24*p1+4(%edi) ;\
xor %eax,%esi ;\
xor %esi,%ebp ;\
mov %esi,24*p1+8(%edi) ;\
mov %ebp,24*p1+12(%edi) ;\
xor %ebp,%edx ;\
xor %edx,%ebx ;\
mov %edx,24*p1+16(%edi) ;\
mov %ebx,24*p1+20(%edi)
#define ksc8(p1) \
rol $24,%ebx ;\
mix_col(aes_fl_tab) ;\
ror $8,%ebx ;\
xor 4*p1+aes_rcon_tab,%eax ;\
xor 32*p1-32(%edi),%eax ;\
mov %eax,32*p1(%edi) ;\
xor 32*p1-28(%edi),%eax ;\
mov %eax,32*p1+4(%edi) ;\
xor 32*p1-24(%edi),%eax ;\
mov %eax,32*p1+8(%edi) ;\
xor 32*p1-20(%edi),%eax ;\
mov %eax,32*p1+12(%edi) ;\
push %ebx ;\
mov %eax,%ebx ;\
mix_col(aes_fl_tab) ;\
pop %ebx ;\
xor %eax,%esi ;\
xor %esi,%ebp ;\
mov %esi,32*p1+16(%edi) ;\
mov %ebp,32*p1+20(%edi) ;\
xor %ebp,%edx ;\
xor %edx,%ebx ;\
mov %edx,32*p1+24(%edi) ;\
mov %ebx,32*p1+28(%edi)
.align ALIGN32BYTES
aes_set_key:
pushfl
push %ebp
mov %esp,%ebp
sub $slen,%esp
push %ebx
push %esi
push %edi
mov aes_cx(%ebp),%edx // edx -> AES context
mov key_ln(%ebp),%ecx // key length
cmpl $128,%ecx
jb aes_30
shr $3,%ecx
aes_30: cmpl $32,%ecx
je aes_32
cmpl $24,%ecx
je aes_32
mov $16,%ecx
aes_32: shr $2,%ecx
mov %ecx,nkey(%edx)
lea 6(%ecx),%eax // 10/12/14 for 4/6/8 32-bit key length
mov %eax,nrnd(%edx)
mov in_key(%ebp),%esi // key input array
lea ekey(%edx),%edi // key position in AES context
cld
push %ebp
mov %ecx,%eax // save key length in eax
rep ; movsl // words in the key schedule
mov -4(%esi),%ebx // put some values in registers
mov -8(%esi),%edx // to allow faster code
mov -12(%esi),%ebp
mov -16(%esi),%esi
cmpl $4,%eax // jump on key size
je aes_36
cmpl $6,%eax
je aes_35
ksc8(0)
ksc8(1)
ksc8(2)
ksc8(3)
ksc8(4)
ksc8(5)
ksc8(6)
jmp aes_37
aes_35: ksc6(0)
ksc6(1)
ksc6(2)
ksc6(3)
ksc6(4)
ksc6(5)
ksc6(6)
ksc6(7)
jmp aes_37
aes_36: ksc4(0)
ksc4(1)
ksc4(2)
ksc4(3)
ksc4(4)
ksc4(5)
ksc4(6)
ksc4(7)
ksc4(8)
ksc4(9)
aes_37: pop %ebp
mov aes_cx(%ebp),%edx // edx -> AES context
cmpl $0,ed_flg(%ebp)
jne aes_39
// compile decryption key schedule from encryption schedule - reverse
// order and do mix_column operation on round keys except first and last
mov nrnd(%edx),%eax // kt = cx->d_key + nc * cx->Nrnd
shl $2,%eax
lea dkey(%edx,%eax,4),%edi
lea ekey(%edx),%esi // kf = cx->e_key
movsl // copy first round key (unmodified)
movsl
movsl
movsl
sub $32,%edi
movl $1,cnt(%ebp)
aes_38: // do mix column on each column of
lodsl // each round key
mov %eax,%ebx
mix_col(aes_im_tab)
stosl
lodsl
mov %eax,%ebx
mix_col(aes_im_tab)
stosl
lodsl
mov %eax,%ebx
mix_col(aes_im_tab)
stosl
lodsl
mov %eax,%ebx
mix_col(aes_im_tab)
stosl
sub $32,%edi
incl cnt(%ebp)
mov cnt(%ebp),%eax
cmp nrnd(%edx),%eax
jb aes_38
movsl // copy last round key (unmodified)
movsl
movsl
movsl
aes_39: pop %edi
pop %esi
pop %ebx
mov %ebp,%esp
pop %ebp
popfl
ret
// finite field multiplies by {02}, {04} and {08}
#define f2(x) ((x<<1)^(((x>>7)&1)*0x11b))
#define f4(x) ((x<<2)^(((x>>6)&1)*0x11b)^(((x>>6)&2)*0x11b))
#define f8(x) ((x<<3)^(((x>>5)&1)*0x11b)^(((x>>5)&2)*0x11b)^(((x>>5)&4)*0x11b))
// finite field multiplies required in table generation
#define f3(x) (f2(x) ^ x)
#define f9(x) (f8(x) ^ x)
#define fb(x) (f8(x) ^ f2(x) ^ x)
#define fd(x) (f8(x) ^ f4(x) ^ x)
#define fe(x) (f8(x) ^ f4(x) ^ f2(x))
// These defines generate the forward table entries
#define u0(x) ((f3(x) << 24) | (x << 16) | (x << 8) | f2(x))
#define u1(x) ((x << 24) | (x << 16) | (f2(x) << 8) | f3(x))
#define u2(x) ((x << 24) | (f2(x) << 16) | (f3(x) << 8) | x)
#define u3(x) ((f2(x) << 24) | (f3(x) << 16) | (x << 8) | x)
// These defines generate the inverse table entries
#define v0(x) ((fb(x) << 24) | (fd(x) << 16) | (f9(x) << 8) | fe(x))
#define v1(x) ((fd(x) << 24) | (f9(x) << 16) | (fe(x) << 8) | fb(x))
#define v2(x) ((f9(x) << 24) | (fe(x) << 16) | (fb(x) << 8) | fd(x))
#define v3(x) ((fe(x) << 24) | (fb(x) << 16) | (fd(x) << 8) | f9(x))
// These defines generate entries for the last round tables
#define w0(x) (x)
#define w1(x) (x << 8)
#define w2(x) (x << 16)
#define w3(x) (x << 24)
// macro to generate inverse mix column tables (needed for the key schedule)
#define im_data0(p1) \
.long p1(0x00),p1(0x01),p1(0x02),p1(0x03),p1(0x04),p1(0x05),p1(0x06),p1(0x07) ;\
.long p1(0x08),p1(0x09),p1(0x0a),p1(0x0b),p1(0x0c),p1(0x0d),p1(0x0e),p1(0x0f) ;\
.long p1(0x10),p1(0x11),p1(0x12),p1(0x13),p1(0x14),p1(0x15),p1(0x16),p1(0x17) ;\
.long p1(0x18),p1(0x19),p1(0x1a),p1(0x1b),p1(0x1c),p1(0x1d),p1(0x1e),p1(0x1f)
#define im_data1(p1) \
.long p1(0x20),p1(0x21),p1(0x22),p1(0x23),p1(0x24),p1(0x25),p1(0x26),p1(0x27) ;\
.long p1(0x28),p1(0x29),p1(0x2a),p1(0x2b),p1(0x2c),p1(0x2d),p1(0x2e),p1(0x2f) ;\
.long p1(0x30),p1(0x31),p1(0x32),p1(0x33),p1(0x34),p1(0x35),p1(0x36),p1(0x37) ;\
.long p1(0x38),p1(0x39),p1(0x3a),p1(0x3b),p1(0x3c),p1(0x3d),p1(0x3e),p1(0x3f)
#define im_data2(p1) \
.long p1(0x40),p1(0x41),p1(0x42),p1(0x43),p1(0x44),p1(0x45),p1(0x46),p1(0x47) ;\
.long p1(0x48),p1(0x49),p1(0x4a),p1(0x4b),p1(0x4c),p1(0x4d),p1(0x4e),p1(0x4f) ;\
.long p1(0x50),p1(0x51),p1(0x52),p1(0x53),p1(0x54),p1(0x55),p1(0x56),p1(0x57) ;\
.long p1(0x58),p1(0x59),p1(0x5a),p1(0x5b),p1(0x5c),p1(0x5d),p1(0x5e),p1(0x5f)
#define im_data3(p1) \
.long p1(0x60),p1(0x61),p1(0x62),p1(0x63),p1(0x64),p1(0x65),p1(0x66),p1(0x67) ;\
.long p1(0x68),p1(0x69),p1(0x6a),p1(0x6b),p1(0x6c),p1(0x6d),p1(0x6e),p1(0x6f) ;\
.long p1(0x70),p1(0x71),p1(0x72),p1(0x73),p1(0x74),p1(0x75),p1(0x76),p1(0x77) ;\
.long p1(0x78),p1(0x79),p1(0x7a),p1(0x7b),p1(0x7c),p1(0x7d),p1(0x7e),p1(0x7f)
#define im_data4(p1) \
.long p1(0x80),p1(0x81),p1(0x82),p1(0x83),p1(0x84),p1(0x85),p1(0x86),p1(0x87) ;\
.long p1(0x88),p1(0x89),p1(0x8a),p1(0x8b),p1(0x8c),p1(0x8d),p1(0x8e),p1(0x8f) ;\
.long p1(0x90),p1(0x91),p1(0x92),p1(0x93),p1(0x94),p1(0x95),p1(0x96),p1(0x97) ;\
.long p1(0x98),p1(0x99),p1(0x9a),p1(0x9b),p1(0x9c),p1(0x9d),p1(0x9e),p1(0x9f)
#define im_data5(p1) \
.long p1(0xa0),p1(0xa1),p1(0xa2),p1(0xa3),p1(0xa4),p1(0xa5),p1(0xa6),p1(0xa7) ;\
.long p1(0xa8),p1(0xa9),p1(0xaa),p1(0xab),p1(0xac),p1(0xad),p1(0xae),p1(0xaf) ;\
.long p1(0xb0),p1(0xb1),p1(0xb2),p1(0xb3),p1(0xb4),p1(0xb5),p1(0xb6),p1(0xb7) ;\
.long p1(0xb8),p1(0xb9),p1(0xba),p1(0xbb),p1(0xbc),p1(0xbd),p1(0xbe),p1(0xbf)
#define im_data6(p1) \
.long p1(0xc0),p1(0xc1),p1(0xc2),p1(0xc3),p1(0xc4),p1(0xc5),p1(0xc6),p1(0xc7) ;\
.long p1(0xc8),p1(0xc9),p1(0xca),p1(0xcb),p1(0xcc),p1(0xcd),p1(0xce),p1(0xcf) ;\
.long p1(0xd0),p1(0xd1),p1(0xd2),p1(0xd3),p1(0xd4),p1(0xd5),p1(0xd6),p1(0xd7) ;\
.long p1(0xd8),p1(0xd9),p1(0xda),p1(0xdb),p1(0xdc),p1(0xdd),p1(0xde),p1(0xdf)
#define im_data7(p1) \
.long p1(0xe0),p1(0xe1),p1(0xe2),p1(0xe3),p1(0xe4),p1(0xe5),p1(0xe6),p1(0xe7) ;\
.long p1(0xe8),p1(0xe9),p1(0xea),p1(0xeb),p1(0xec),p1(0xed),p1(0xee),p1(0xef) ;\
.long p1(0xf0),p1(0xf1),p1(0xf2),p1(0xf3),p1(0xf4),p1(0xf5),p1(0xf6),p1(0xf7) ;\
.long p1(0xf8),p1(0xf9),p1(0xfa),p1(0xfb),p1(0xfc),p1(0xfd),p1(0xfe),p1(0xff)
// S-box data - 256 entries
#define sb_data0(p1) \
.long p1(0x63),p1(0x7c),p1(0x77),p1(0x7b),p1(0xf2),p1(0x6b),p1(0x6f),p1(0xc5) ;\
.long p1(0x30),p1(0x01),p1(0x67),p1(0x2b),p1(0xfe),p1(0xd7),p1(0xab),p1(0x76) ;\
.long p1(0xca),p1(0x82),p1(0xc9),p1(0x7d),p1(0xfa),p1(0x59),p1(0x47),p1(0xf0) ;\
.long p1(0xad),p1(0xd4),p1(0xa2),p1(0xaf),p1(0x9c),p1(0xa4),p1(0x72),p1(0xc0)
#define sb_data1(p1) \
.long p1(0xb7),p1(0xfd),p1(0x93),p1(0x26),p1(0x36),p1(0x3f),p1(0xf7),p1(0xcc) ;\
.long p1(0x34),p1(0xa5),p1(0xe5),p1(0xf1),p1(0x71),p1(0xd8),p1(0x31),p1(0x15) ;\
.long p1(0x04),p1(0xc7),p1(0x23),p1(0xc3),p1(0x18),p1(0x96),p1(0x05),p1(0x9a) ;\
.long p1(0x07),p1(0x12),p1(0x80),p1(0xe2),p1(0xeb),p1(0x27),p1(0xb2),p1(0x75)
#define sb_data2(p1) \
.long p1(0x09),p1(0x83),p1(0x2c),p1(0x1a),p1(0x1b),p1(0x6e),p1(0x5a),p1(0xa0) ;\
.long p1(0x52),p1(0x3b),p1(0xd6),p1(0xb3),p1(0x29),p1(0xe3),p1(0x2f),p1(0x84) ;\
.long p1(0x53),p1(0xd1),p1(0x00),p1(0xed),p1(0x20),p1(0xfc),p1(0xb1),p1(0x5b) ;\
.long p1(0x6a),p1(0xcb),p1(0xbe),p1(0x39),p1(0x4a),p1(0x4c),p1(0x58),p1(0xcf)
#define sb_data3(p1) \
.long p1(0xd0),p1(0xef),p1(0xaa),p1(0xfb),p1(0x43),p1(0x4d),p1(0x33),p1(0x85) ;\
.long p1(0x45),p1(0xf9),p1(0x02),p1(0x7f),p1(0x50),p1(0x3c),p1(0x9f),p1(0xa8) ;\
.long p1(0x51),p1(0xa3),p1(0x40),p1(0x8f),p1(0x92),p1(0x9d),p1(0x38),p1(0xf5) ;\
.long p1(0xbc),p1(0xb6),p1(0xda),p1(0x21),p1(0x10),p1(0xff),p1(0xf3),p1(0xd2)
#define sb_data4(p1) \
.long p1(0xcd),p1(0x0c),p1(0x13),p1(0xec),p1(0x5f),p1(0x97),p1(0x44),p1(0x17) ;\
.long p1(0xc4),p1(0xa7),p1(0x7e),p1(0x3d),p1(0x64),p1(0x5d),p1(0x19),p1(0x73) ;\
.long p1(0x60),p1(0x81),p1(0x4f),p1(0xdc),p1(0x22),p1(0x2a),p1(0x90),p1(0x88) ;\
.long p1(0x46),p1(0xee),p1(0xb8),p1(0x14),p1(0xde),p1(0x5e),p1(0x0b),p1(0xdb)
#define sb_data5(p1) \
.long p1(0xe0),p1(0x32),p1(0x3a),p1(0x0a),p1(0x49),p1(0x06),p1(0x24),p1(0x5c) ;\
.long p1(0xc2),p1(0xd3),p1(0xac),p1(0x62),p1(0x91),p1(0x95),p1(0xe4),p1(0x79) ;\
.long p1(0xe7),p1(0xc8),p1(0x37),p1(0x6d),p1(0x8d),p1(0xd5),p1(0x4e),p1(0xa9) ;\
.long p1(0x6c),p1(0x56),p1(0xf4),p1(0xea),p1(0x65),p1(0x7a),p1(0xae),p1(0x08)
#define sb_data6(p1) \
.long p1(0xba),p1(0x78),p1(0x25),p1(0x2e),p1(0x1c),p1(0xa6),p1(0xb4),p1(0xc6) ;\
.long p1(0xe8),p1(0xdd),p1(0x74),p1(0x1f),p1(0x4b),p1(0xbd),p1(0x8b),p1(0x8a) ;\
.long p1(0x70),p1(0x3e),p1(0xb5),p1(0x66),p1(0x48),p1(0x03),p1(0xf6),p1(0x0e) ;\
.long p1(0x61),p1(0x35),p1(0x57),p1(0xb9),p1(0x86),p1(0xc1),p1(0x1d),p1(0x9e)
#define sb_data7(p1) \
.long p1(0xe1),p1(0xf8),p1(0x98),p1(0x11),p1(0x69),p1(0xd9),p1(0x8e),p1(0x94) ;\
.long p1(0x9b),p1(0x1e),p1(0x87),p1(0xe9),p1(0xce),p1(0x55),p1(0x28),p1(0xdf) ;\
.long p1(0x8c),p1(0xa1),p1(0x89),p1(0x0d),p1(0xbf),p1(0xe6),p1(0x42),p1(0x68) ;\
.long p1(0x41),p1(0x99),p1(0x2d),p1(0x0f),p1(0xb0),p1(0x54),p1(0xbb),p1(0x16)
// Inverse S-box data - 256 entries
#define ib_data0(p1) \
.long p1(0x52),p1(0x09),p1(0x6a),p1(0xd5),p1(0x30),p1(0x36),p1(0xa5),p1(0x38) ;\
.long p1(0xbf),p1(0x40),p1(0xa3),p1(0x9e),p1(0x81),p1(0xf3),p1(0xd7),p1(0xfb) ;\
.long p1(0x7c),p1(0xe3),p1(0x39),p1(0x82),p1(0x9b),p1(0x2f),p1(0xff),p1(0x87) ;\
.long p1(0x34),p1(0x8e),p1(0x43),p1(0x44),p1(0xc4),p1(0xde),p1(0xe9),p1(0xcb)
#define ib_data1(p1) \
.long p1(0x54),p1(0x7b),p1(0x94),p1(0x32),p1(0xa6),p1(0xc2),p1(0x23),p1(0x3d) ;\
.long p1(0xee),p1(0x4c),p1(0x95),p1(0x0b),p1(0x42),p1(0xfa),p1(0xc3),p1(0x4e) ;\
.long p1(0x08),p1(0x2e),p1(0xa1),p1(0x66),p1(0x28),p1(0xd9),p1(0x24),p1(0xb2) ;\
.long p1(0x76),p1(0x5b),p1(0xa2),p1(0x49),p1(0x6d),p1(0x8b),p1(0xd1),p1(0x25)
#define ib_data2(p1) \
.long p1(0x72),p1(0xf8),p1(0xf6),p1(0x64),p1(0x86),p1(0x68),p1(0x98),p1(0x16) ;\
.long p1(0xd4),p1(0xa4),p1(0x5c),p1(0xcc),p1(0x5d),p1(0x65),p1(0xb6),p1(0x92) ;\
.long p1(0x6c),p1(0x70),p1(0x48),p1(0x50),p1(0xfd),p1(0xed),p1(0xb9),p1(0xda) ;\
.long p1(0x5e),p1(0x15),p1(0x46),p1(0x57),p1(0xa7),p1(0x8d),p1(0x9d),p1(0x84)
#define ib_data3(p1) \
.long p1(0x90),p1(0xd8),p1(0xab),p1(0x00),p1(0x8c),p1(0xbc),p1(0xd3),p1(0x0a) ;\
.long p1(0xf7),p1(0xe4),p1(0x58),p1(0x05),p1(0xb8),p1(0xb3),p1(0x45),p1(0x06) ;\
.long p1(0xd0),p1(0x2c),p1(0x1e),p1(0x8f),p1(0xca),p1(0x3f),p1(0x0f),p1(0x02) ;\
.long p1(0xc1),p1(0xaf),p1(0xbd),p1(0x03),p1(0x01),p1(0x13),p1(0x8a),p1(0x6b)
#define ib_data4(p1) \
.long p1(0x3a),p1(0x91),p1(0x11),p1(0x41),p1(0x4f),p1(0x67),p1(0xdc),p1(0xea) ;\
.long p1(0x97),p1(0xf2),p1(0xcf),p1(0xce),p1(0xf0),p1(0xb4),p1(0xe6),p1(0x73) ;\
.long p1(0x96),p1(0xac),p1(0x74),p1(0x22),p1(0xe7),p1(0xad),p1(0x35),p1(0x85) ;\
.long p1(0xe2),p1(0xf9),p1(0x37),p1(0xe8),p1(0x1c),p1(0x75),p1(0xdf),p1(0x6e)
#define ib_data5(p1) \
.long p1(0x47),p1(0xf1),p1(0x1a),p1(0x71),p1(0x1d),p1(0x29),p1(0xc5),p1(0x89) ;\
.long p1(0x6f),p1(0xb7),p1(0x62),p1(0x0e),p1(0xaa),p1(0x18),p1(0xbe),p1(0x1b) ;\
.long p1(0xfc),p1(0x56),p1(0x3e),p1(0x4b),p1(0xc6),p1(0xd2),p1(0x79),p1(0x20) ;\
.long p1(0x9a),p1(0xdb),p1(0xc0),p1(0xfe),p1(0x78),p1(0xcd),p1(0x5a),p1(0xf4)
#define ib_data6(p1) \
.long p1(0x1f),p1(0xdd),p1(0xa8),p1(0x33),p1(0x88),p1(0x07),p1(0xc7),p1(0x31) ;\
.long p1(0xb1),p1(0x12),p1(0x10),p1(0x59),p1(0x27),p1(0x80),p1(0xec),p1(0x5f) ;\
.long p1(0x60),p1(0x51),p1(0x7f),p1(0xa9),p1(0x19),p1(0xb5),p1(0x4a),p1(0x0d) ;\
.long p1(0x2d),p1(0xe5),p1(0x7a),p1(0x9f),p1(0x93),p1(0xc9),p1(0x9c),p1(0xef)
#define ib_data7(p1) \
.long p1(0xa0),p1(0xe0),p1(0x3b),p1(0x4d),p1(0xae),p1(0x2a),p1(0xf5),p1(0xb0) ;\
.long p1(0xc8),p1(0xeb),p1(0xbb),p1(0x3c),p1(0x83),p1(0x53),p1(0x99),p1(0x61) ;\
.long p1(0x17),p1(0x2b),p1(0x04),p1(0x7e),p1(0xba),p1(0x77),p1(0xd6),p1(0x26) ;\
.long p1(0xe1),p1(0x69),p1(0x14),p1(0x63),p1(0x55),p1(0x21),p1(0x0c),p1(0x7d)
// The rcon_table (needed for the key schedule)
//
// Here is original Dr Brian Gladman's source code:
// _rcon_tab:
// %assign x 1
// %rep 29
// dd x
// %assign x f2(x)
// %endrep
//
// Here is precomputed output (it's more portable this way):
.align ALIGN32BYTES
aes_rcon_tab:
.long 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80
.long 0x1b,0x36,0x6c,0xd8,0xab,0x4d,0x9a,0x2f
.long 0x5e,0xbc,0x63,0xc6,0x97,0x35,0x6a,0xd4
.long 0xb3,0x7d,0xfa,0xef,0xc5
// The forward xor tables
.align ALIGN32BYTES
aes_ft_tab:
sb_data0(u0)
sb_data1(u0)
sb_data2(u0)
sb_data3(u0)
sb_data4(u0)
sb_data5(u0)
sb_data6(u0)
sb_data7(u0)
sb_data0(u1)
sb_data1(u1)
sb_data2(u1)
sb_data3(u1)
sb_data4(u1)
sb_data5(u1)
sb_data6(u1)
sb_data7(u1)
sb_data0(u2)
sb_data1(u2)
sb_data2(u2)
sb_data3(u2)
sb_data4(u2)
sb_data5(u2)
sb_data6(u2)
sb_data7(u2)
sb_data0(u3)
sb_data1(u3)
sb_data2(u3)
sb_data3(u3)
sb_data4(u3)
sb_data5(u3)
sb_data6(u3)
sb_data7(u3)
.align ALIGN32BYTES
aes_fl_tab:
sb_data0(w0)
sb_data1(w0)
sb_data2(w0)
sb_data3(w0)
sb_data4(w0)
sb_data5(w0)
sb_data6(w0)
sb_data7(w0)
sb_data0(w1)
sb_data1(w1)
sb_data2(w1)
sb_data3(w1)
sb_data4(w1)
sb_data5(w1)
sb_data6(w1)
sb_data7(w1)
sb_data0(w2)
sb_data1(w2)
sb_data2(w2)
sb_data3(w2)
sb_data4(w2)
sb_data5(w2)
sb_data6(w2)
sb_data7(w2)
sb_data0(w3)
sb_data1(w3)
sb_data2(w3)
sb_data3(w3)
sb_data4(w3)
sb_data5(w3)
sb_data6(w3)
sb_data7(w3)
// The inverse xor tables
.align ALIGN32BYTES
aes_it_tab:
ib_data0(v0)
ib_data1(v0)
ib_data2(v0)
ib_data3(v0)
ib_data4(v0)
ib_data5(v0)
ib_data6(v0)
ib_data7(v0)
ib_data0(v1)
ib_data1(v1)
ib_data2(v1)
ib_data3(v1)
ib_data4(v1)
ib_data5(v1)
ib_data6(v1)
ib_data7(v1)
ib_data0(v2)
ib_data1(v2)
ib_data2(v2)
ib_data3(v2)
ib_data4(v2)
ib_data5(v2)
ib_data6(v2)
ib_data7(v2)
ib_data0(v3)
ib_data1(v3)
ib_data2(v3)
ib_data3(v3)
ib_data4(v3)
ib_data5(v3)
ib_data6(v3)
ib_data7(v3)
.align ALIGN32BYTES
aes_il_tab:
ib_data0(w0)
ib_data1(w0)
ib_data2(w0)
ib_data3(w0)
ib_data4(w0)
ib_data5(w0)
ib_data6(w0)
ib_data7(w0)
ib_data0(w1)
ib_data1(w1)
ib_data2(w1)
ib_data3(w1)
ib_data4(w1)
ib_data5(w1)
ib_data6(w1)
ib_data7(w1)
ib_data0(w2)
ib_data1(w2)
ib_data2(w2)
ib_data3(w2)
ib_data4(w2)
ib_data5(w2)
ib_data6(w2)
ib_data7(w2)
ib_data0(w3)
ib_data1(w3)
ib_data2(w3)
ib_data3(w3)
ib_data4(w3)
ib_data5(w3)
ib_data6(w3)
ib_data7(w3)
// The inverse mix column tables
.align ALIGN32BYTES
aes_im_tab:
im_data0(v0)
im_data1(v0)
im_data2(v0)
im_data3(v0)
im_data4(v0)
im_data5(v0)
im_data6(v0)
im_data7(v0)
im_data0(v1)
im_data1(v1)
im_data2(v1)
im_data3(v1)
im_data4(v1)
im_data5(v1)
im_data6(v1)
im_data7(v1)
im_data0(v2)
im_data1(v2)
im_data2(v2)
im_data3(v2)
im_data4(v2)
im_data5(v2)
im_data6(v2)
im_data7(v2)
im_data0(v3)
im_data1(v3)
im_data2(v3)
im_data3(v3)
im_data4(v3)
im_data5(v3)
im_data6(v3)
im_data7(v3)
+41
View File
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "aes_cbc.h"
#define AES_BLOCK_SIZE 16
#define KEY_SIZE 128 /* bits */
#define KEY "1234567890123456"
#define STR "hola guaso como estaisss ... 012"
#define STRSZ (sizeof(STR)-1)
#define EMT_AESCBC_BLKLEN AES_BLOCK_SIZE
#define AES_CONTEXT_T aes_context
#define EMT_ESPAES_KEY_SZ 16
int pretty_print(const unsigned char *buf, int count) {
int i=0;
for (;i<count;i++) {
if (i%8==0) putchar(' ');
if (i%16==0) putchar('\n');
printf ("%02hhx ", buf[i]);
}
putchar('\n');
return i;
}
//#define SIZE STRSZ/2
#define SIZE STRSZ
int main() {
int ret;
char buf0[SIZE+1], buf1[SIZE+1];
char IV[AES_BLOCK_SIZE]="\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0";
aes_context ac;
AES_set_key(&ac, KEY, KEY_SIZE);
//pretty_print((char *)&ac.aes_e_key, sizeof(ac.aes_e_key));
memset(buf0, 0, sizeof (buf0));
memset(buf1, 0, sizeof (buf1));
ret=AES_cbc_encrypt(&ac, STR, buf0, SIZE, IV, 1);
pretty_print(buf0, SIZE);
printf("size=%d ret=%d\n%s\n", SIZE, ret, buf0);
ret=AES_cbc_encrypt(&ac, buf0, buf1, SIZE, IV, 0);
printf("size=%d ret=%d\n%s\n", SIZE, ret, buf1);
return 0;
}
+30
View File
@@ -0,0 +1,30 @@
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include "aes.h"
#include "aes_xcbc_mac.h"
#define STR "Hola guasssso c|mo estais ...012"
void print_hash(const __u8 *hash) {
printf("%08x %08x %08x %08x\n",
*(__u32*)(&hash[0]),
*(__u32*)(&hash[4]),
*(__u32*)(&hash[8]),
*(__u32*)(&hash[12]));
}
int main(int argc, char *argv[]) {
aes_block key= { 0xdeadbeef, 0xceedcaca, 0xcafebabe, 0xff010204 };
__u8 hash[16];
char *str = argv[1];
aes_context_mac ctx;
if (str==NULL) {
fprintf(stderr, "pasame el str\n");
return 255;
}
AES_xcbc_mac_set_key(&ctx, (__u8 *)&key, sizeof(key));
AES_xcbc_mac_hash(&ctx, str, strlen(str), hash);
print_hash(hash);
str[2]='x';
AES_xcbc_mac_hash(&ctx, str, strlen(str), hash);
print_hash(hash);
return 0;
}
+46
View File
@@ -0,0 +1,46 @@
Copyright (C) 1995-1997 Eric Young ([email protected])
All rights reserved.
This package is an Blowfish implementation written
by Eric Young ([email protected]).
This library is free for commercial and non-commercial use as long as
the following conditions are aheared to. The following conditions
apply to all code found in this distribution.
Copyright remains Eric Young's, and as such any Copyright notices in
the code are not to be removed.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by Eric Young ([email protected])
THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
The license and distribution terms for any publically available version or
derivative of this code cannot be changed. i.e. this code cannot simply be
copied and put under another distrubution license
[including the GNU Public License.]
The reason behind this being stated in this direct manner is past
experience in code simply being copied and the attribution removed
from it and then being distributed as part of other packages. This
implementation was a non-trivial and unpaid effort.
+14
View File
@@ -0,0 +1,14 @@
This Eric Young's blowfish implementation, taken from his SSLeay library
and made available as a separate library.
The version number (0.7.2m) is the SSLeay version that this library was
taken from.
To build, just unpack and type make.
If you are not using gcc, edit the Makefile.
If you are compiling for an x86 box, try the assembler (it needs improving).
There are also some compile time options that can improve performance,
these are documented in the Makefile.
eric 15-Apr-1997
+121
View File
@@ -0,0 +1,121 @@
#
# SSLeay/crypto/blowfish/Makefile
#
DIR= bf
TOP= ../..
CC= cc
CPP= $(CC) -E
INC=-I ../include
CFLAG=-g -D__KERNEL__ -I/usr/src/linux/include
INSTALL_PREFIX=
OPENSSLDIR= /usr/local/ssl
INSTALLTOP=/usr/local/ssl
MAKE= make -f Makefile.ssl
MAKEDEPEND= $(TOP)/util/domd $(TOP)
MAKEFILE= Makefile.ssl
AR= ar r
RANLIB= ranlib
PERL= perl
CFLAGS= $(INC) $(CFLAG)
.c.o:
$(CC) $(CPPFLAGS) $(CFLAGS) $(INC) -c $< -o $@
BF_ASM-i586 := bf-586.pl
BF_ASM-i686 := bf-686.pl
BF_ENC := bf_enc.o
ASM-$(ARCH_ASM):=1
ASM_X86:=$(ASM-i586)$(ASM-i686)
ifneq ($(strip $(ASM_X86)),)
BF_ENC= asm/bx86-elf.o
BF_ASM= $(BF_ASM-$(ARCH_ASM))
endif
GENERAL=Makefile
TEST=bftest.c
APPS=
LIB=libblowfish.a
LIBSRC=bf_skey.c bf_enc.c
LIBOBJ=bf_skey.o $(BF_ENC)
SRC= $(LIBSRC)
EXHEADER= blowfish.h
HEADER= bf_pi.h bf_locl.h $(EXHEADER)
ALL= $(GENERAL) $(SRC) $(HEADER)
#top:
# (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
all: lib
lib: $(LIB)
$(LIB): $(LIBOBJ)
$(AR) $(LIB) $(LIBOBJ)
$(RANLIB) $(LIB)
# elf
asm/bx86-elf.o: asm/bx86unix.cpp
$(CPP) -DELF -x c asm/bx86unix.cpp | as -o asm/bx86-elf.o
# solaris
asm/bx86-sol.o: asm/bx86unix.cpp
$(CC) -E -DSOL asm/bx86unix.cpp | sed 's/^#.*//' > asm/bx86-sol.s
as -o asm/bx86-sol.o asm/bx86-sol.s
rm -f asm/bx86-sol.s
# a.out
asm/bx86-out.o: asm/bx86unix.cpp
$(CPP) -DOUT asm/bx86unix.cpp | as -o asm/bx86-out.o
# bsdi
asm/bx86bsdi.o: asm/bx86unix.cpp
$(CPP) -DBSDI asm/bx86unix.cpp | sed 's/ :/:/' | as -o asm/bx86bsdi.o
asm/bx86unix.cpp: asm/$(BF_ASM) ../perlasm/x86asm.pl ../perlasm/cbc.pl
(cd asm; $(PERL) $(BF_ASM) cpp $(PROCESSOR) >bx86unix.cpp)
files:
$(PERL) $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
links:
@$(TOP)/util/point.sh Makefile.ssl Makefile
@$(PERL) $(TOP)/util/mklink.pl ../../include/openssl $(EXHEADER)
@$(PERL) $(TOP)/util/mklink.pl ../../test $(TEST)
@$(PERL) $(TOP)/util/mklink.pl ../../apps $(APPS)
install: installs
installs:
@for i in $(EXHEADER) ; \
do \
(cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i; \
chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i ); \
done;
tags:
ctags $(SRC)
tests:
lint:
lint -DLINT $(INCLUDES) $(SRC)>fluff
depend:
$(MAKEDEPEND) $(INCLUDES) $(DEPFLAG) $(PROGS) $(LIBSRC)
dclean:
$(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
mv -f Makefile.new $(MAKEFILE)
clean:
rm -f asm/bx86unix.cpp *.o asm/*.o *.obj $(LIB) tags core .pure .nfs* *.old *.bak fluff
# DO NOT DELETE THIS LINE -- make depend depends on it.
+118
View File
@@ -0,0 +1,118 @@
#
# SSLeay/crypto/blowfish/Makefile
#
DIR= bf
TOP= ../..
CC= cc
CPP= $(CC) -E
INCLUDES=
CFLAG=-g
INSTALL_PREFIX=
OPENSSLDIR= /usr/local/ssl
INSTALLTOP=/usr/local/ssl
MAKE= make -f Makefile.ssl
MAKEDEPEND= $(TOP)/util/domd $(TOP)
MAKEFILE= Makefile.ssl
AR= ar r
BF_ENC= bf_enc.o
# or use
#DES_ENC= bx86-elf.o
CFLAGS= $(INCLUDES) $(CFLAG)
GENERAL=Makefile
TEST=bftest.c
APPS=
LIB=$(TOP)/libcrypto.a
LIBSRC=bf_skey.c bf_ecb.c bf_enc.c bf_cfb64.c bf_ofb64.c
LIBOBJ=bf_skey.o bf_ecb.o $(BF_ENC) bf_cfb64.o bf_ofb64.o
SRC= $(LIBSRC)
EXHEADER= blowfish.h
HEADER= bf_pi.h bf_locl.h $(EXHEADER)
ALL= $(GENERAL) $(SRC) $(HEADER)
top:
(cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
all: lib
lib: $(LIBOBJ)
$(AR) $(LIB) $(LIBOBJ)
$(RANLIB) $(LIB)
@touch lib
# elf
asm/bx86-elf.o: asm/bx86unix.cpp
$(CPP) -DELF -x c asm/bx86unix.cpp | as -o asm/bx86-elf.o
# solaris
asm/bx86-sol.o: asm/bx86unix.cpp
$(CC) -E -DSOL asm/bx86unix.cpp | sed 's/^#.*//' > asm/bx86-sol.s
as -o asm/bx86-sol.o asm/bx86-sol.s
rm -f asm/bx86-sol.s
# a.out
asm/bx86-out.o: asm/bx86unix.cpp
$(CPP) -DOUT asm/bx86unix.cpp | as -o asm/bx86-out.o
# bsdi
asm/bx86bsdi.o: asm/bx86unix.cpp
$(CPP) -DBSDI asm/bx86unix.cpp | sed 's/ :/:/' | as -o asm/bx86bsdi.o
asm/bx86unix.cpp: asm/bf-586.pl ../perlasm/x86asm.pl ../perlasm/cbc.pl
(cd asm; $(PERL) bf-586.pl cpp $(PROCESSOR) >bx86unix.cpp)
files:
$(PERL) $(TOP)/util/files.pl Makefile.ssl >> $(TOP)/MINFO
links:
@$(TOP)/util/point.sh Makefile.ssl Makefile
@$(PERL) $(TOP)/util/mklink.pl ../../include/openssl $(EXHEADER)
@$(PERL) $(TOP)/util/mklink.pl ../../test $(TEST)
@$(PERL) $(TOP)/util/mklink.pl ../../apps $(APPS)
install: installs
installs:
@for i in $(EXHEADER) ; \
do \
(cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i; \
chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i ); \
done;
tags:
ctags $(SRC)
tests:
lint:
lint -DLINT $(INCLUDES) $(SRC)>fluff
depend:
$(MAKEDEPEND) $(INCLUDES) $(DEPFLAG) $(PROGS) $(LIBSRC)
dclean:
$(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
mv -f Makefile.new $(MAKEFILE)
clean:
rm -f asm/bx86unix.cpp *.o asm/*.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
# DO NOT DELETE THIS LINE -- make depend depends on it.
bf_cfb64.o: ../../include/openssl/blowfish.h
bf_cfb64.o: ../../include/openssl/opensslconf.h bf_locl.h
bf_ecb.o: ../../include/openssl/blowfish.h ../../include/openssl/opensslconf.h
bf_ecb.o: ../../include/openssl/opensslv.h bf_locl.h
bf_enc.o: ../../include/openssl/blowfish.h ../../include/openssl/opensslconf.h
bf_enc.o: bf_locl.h
bf_ofb64.o: ../../include/openssl/blowfish.h
bf_ofb64.o: ../../include/openssl/opensslconf.h bf_locl.h
bf_skey.o: ../../include/openssl/blowfish.h ../../include/openssl/opensslconf.h
bf_skey.o: bf_locl.h bf_pi.h
+8
View File
@@ -0,0 +1,8 @@
This is a quick packaging up of my blowfish code into a library.
It has been lifted from SSLeay.
The copyright notices seem a little harsh because I have not spent the
time to rewrite the conditions from the normal SSLeay ones.
Basically if you just want to play with the library, not a problem.
eric 15-Apr-1997
+6
View File
@@ -0,0 +1,6 @@
The version numbers will follow my SSL implementation
0.7.2r - Some reasonable default compiler options from
Peter Gutman <[email protected]>
0.7.2m - the first release
+136
View File
@@ -0,0 +1,136 @@
#!/usr/bin/perl
push(@INC,"perlasm","../../perlasm");
require "x86asm.pl";
require "cbc.pl";
&asm_init($ARGV[0],"bf-586.pl",$ARGV[$#ARGV] eq "386");
$BF_ROUNDS=16;
$BF_OFF=($BF_ROUNDS+2)*4;
$L="edi";
$R="esi";
$P="ebp";
$tmp1="eax";
$tmp2="ebx";
$tmp3="ecx";
$tmp4="edx";
&BF_encrypt("BF_encrypt",1);
&BF_encrypt("BF_decrypt",0);
&cbc("BF_cbc_encrypt","BF_encrypt","BF_decrypt",1,4,5,3,-1,-1);
&asm_finish();
sub BF_encrypt
{
local($name,$enc)=@_;
&function_begin_B($name,"");
&comment("");
&push("ebp");
&push("ebx");
&mov($tmp2,&wparam(0));
&mov($P,&wparam(1));
&push("esi");
&push("edi");
&comment("Load the 2 words");
&mov($L,&DWP(0,$tmp2,"",0));
&mov($R,&DWP(4,$tmp2,"",0));
&xor( $tmp1, $tmp1);
# encrypting part
if ($enc)
{
&mov($tmp2,&DWP(0,$P,"",0));
&xor( $tmp3, $tmp3);
&xor($L,$tmp2);
for ($i=0; $i<$BF_ROUNDS; $i+=2)
{
&comment("");
&comment("Round $i");
&BF_ENCRYPT($i+1,$R,$L,$P,$tmp1,$tmp2,$tmp3,$tmp4,1);
&comment("");
&comment("Round ".sprintf("%d",$i+1));
&BF_ENCRYPT($i+2,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,1);
}
# &mov($tmp1,&wparam(0)); In last loop
&mov($tmp4,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
}
else
{
&mov($tmp2,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
&xor( $tmp3, $tmp3);
&xor($L,$tmp2);
for ($i=$BF_ROUNDS; $i>0; $i-=2)
{
&comment("");
&comment("Round $i");
&BF_ENCRYPT($i,$R,$L,$P,$tmp1,$tmp2,$tmp3,$tmp4,0);
&comment("");
&comment("Round ".sprintf("%d",$i-1));
&BF_ENCRYPT($i-1,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,0);
}
# &mov($tmp1,&wparam(0)); In last loop
&mov($tmp4,&DWP(0,$P,"",0));
}
&xor($R,$tmp4);
&mov(&DWP(4,$tmp1,"",0),$L);
&mov(&DWP(0,$tmp1,"",0),$R);
&function_end($name);
}
sub BF_ENCRYPT
{
local($i,$L,$R,$P,$tmp1,$tmp2,$tmp3,$tmp4,$enc)=@_;
&mov( $tmp4, &DWP(&n2a($i*4),$P,"",0)); # for next round
&mov( $tmp2, $R);
&xor( $L, $tmp4);
&shr( $tmp2, 16);
&mov( $tmp4, $R);
&movb( &LB($tmp1), &HB($tmp2)); # A
&and( $tmp2, 0xff); # B
&movb( &LB($tmp3), &HB($tmp4)); # C
&and( $tmp4, 0xff); # D
&mov( $tmp1, &DWP(&n2a($BF_OFF+0x0000),$P,$tmp1,4));
&mov( $tmp2, &DWP(&n2a($BF_OFF+0x0400),$P,$tmp2,4));
&add( $tmp2, $tmp1);
&mov( $tmp1, &DWP(&n2a($BF_OFF+0x0800),$P,$tmp3,4));
&xor( $tmp2, $tmp1);
&mov( $tmp4, &DWP(&n2a($BF_OFF+0x0C00),$P,$tmp4,4));
&add( $tmp2, $tmp4);
if (($enc && ($i != 16)) || ((!$enc) && ($i != 1)))
{ &xor( $tmp1, $tmp1); }
else
{
&comment("Load parameter 0 ($i) enc=$enc");
&mov($tmp1,&wparam(0));
} # In last loop
&xor( $L, $tmp2);
# delay
}
sub n2a
{
sprintf("%d",$_[0]);
}
+127
View File
@@ -0,0 +1,127 @@
#!/usr/bin/perl
push(@INC,"perlasm","../../perlasm");
require "x86asm.pl";
require "cbc.pl";
&asm_init($ARGV[0],"bf-686.pl");
$BF_ROUNDS=16;
$BF_OFF=($BF_ROUNDS+2)*4;
$L="ecx";
$R="edx";
$P="edi";
$tot="esi";
$tmp1="eax";
$tmp2="ebx";
$tmp3="ebp";
&des_encrypt("BF_encrypt",1);
&des_encrypt("BF_decrypt",0);
&cbc("BF_cbc_encrypt","BF_encrypt","BF_decrypt",1,4,5,3,-1,-1);
&asm_finish();
&file_end();
sub des_encrypt
{
local($name,$enc)=@_;
&function_begin($name,"");
&comment("");
&comment("Load the 2 words");
&mov("eax",&wparam(0));
&mov($L,&DWP(0,"eax","",0));
&mov($R,&DWP(4,"eax","",0));
&comment("");
&comment("P pointer, s and enc flag");
&mov($P,&wparam(1));
&xor( $tmp1, $tmp1);
&xor( $tmp2, $tmp2);
# encrypting part
if ($enc)
{
&xor($L,&DWP(0,$P,"",0));
for ($i=0; $i<$BF_ROUNDS; $i+=2)
{
&comment("");
&comment("Round $i");
&BF_ENCRYPT($i+1,$R,$L,$P,$tot,$tmp1,$tmp2,$tmp3);
&comment("");
&comment("Round ".sprintf("%d",$i+1));
&BF_ENCRYPT($i+2,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3);
}
&xor($R,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
&mov("eax",&wparam(0));
&mov(&DWP(0,"eax","",0),$R);
&mov(&DWP(4,"eax","",0),$L);
&function_end_A($name);
}
else
{
&xor($L,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
for ($i=$BF_ROUNDS; $i>0; $i-=2)
{
&comment("");
&comment("Round $i");
&BF_ENCRYPT($i,$R,$L,$P,$tot,$tmp1,$tmp2,$tmp3);
&comment("");
&comment("Round ".sprintf("%d",$i-1));
&BF_ENCRYPT($i-1,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3);
}
&xor($R,&DWP(0,$P,"",0));
&mov("eax",&wparam(0));
&mov(&DWP(0,"eax","",0),$R);
&mov(&DWP(4,"eax","",0),$L);
&function_end_A($name);
}
&function_end_B($name);
}
sub BF_ENCRYPT
{
local($i,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3)=@_;
&rotr( $R, 16);
&mov( $tot, &DWP(&n2a($i*4),$P,"",0));
&movb( &LB($tmp1), &HB($R));
&movb( &LB($tmp2), &LB($R));
&rotr( $R, 16);
&xor( $L, $tot);
&mov( $tot, &DWP(&n2a($BF_OFF+0x0000),$P,$tmp1,4));
&mov( $tmp3, &DWP(&n2a($BF_OFF+0x0400),$P,$tmp2,4));
&movb( &LB($tmp1), &HB($R));
&movb( &LB($tmp2), &LB($R));
&add( $tot, $tmp3);
&mov( $tmp1, &DWP(&n2a($BF_OFF+0x0800),$P,$tmp1,4)); # delay
&xor( $tot, $tmp1);
&mov( $tmp3, &DWP(&n2a($BF_OFF+0x0C00),$P,$tmp2,4));
&add( $tot, $tmp3);
&xor( $tmp1, $tmp1);
&xor( $L, $tot);
# delay
}
sub n2a
{
sprintf("%d",$_[0]);
}
+10
View File
@@ -0,0 +1,10 @@
There are blowfish assembler generation scripts.
bf-586.pl version is for the pentium and
bf-686.pl is my original version, which is faster on the pentium pro.
When using a bf-586.pl, the pentium pro/II is %8 slower than using
bf-686.pl. When using a bf-686.pl, the pentium is %16 slower
than bf-586.pl
So the default is bf-586.pl
+306
View File
@@ -0,0 +1,306 @@
/* crypto/bf/bf_enc.c */
/* Copyright (C) 1995-1998 Eric Young ([email protected])
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young ([email protected]).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson ([email protected]).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young ([email protected])"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson ([email protected])"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
#include "blowfish.h"
#include "bf_locl.h"
/* Blowfish as implemented from 'Blowfish: Springer-Verlag paper'
* (From LECTURE NOTES IN COMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION,
* CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
*/
#if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
#error If you set BF_ROUNDS to some value other than 16 or 20, you will have \
to modify the code.
#endif
void BF_encrypt(BF_LONG *data, const BF_KEY *key)
{
#ifndef BF_PTR2
const BF_LONG *p,*s;
BF_LONG l,r;
p=key->P;
s= &(key->S[0]);
l=data[0];
r=data[1];
l^=p[0];
BF_ENC(r,l,s,p[ 1]);
BF_ENC(l,r,s,p[ 2]);
BF_ENC(r,l,s,p[ 3]);
BF_ENC(l,r,s,p[ 4]);
BF_ENC(r,l,s,p[ 5]);
BF_ENC(l,r,s,p[ 6]);
BF_ENC(r,l,s,p[ 7]);
BF_ENC(l,r,s,p[ 8]);
BF_ENC(r,l,s,p[ 9]);
BF_ENC(l,r,s,p[10]);
BF_ENC(r,l,s,p[11]);
BF_ENC(l,r,s,p[12]);
BF_ENC(r,l,s,p[13]);
BF_ENC(l,r,s,p[14]);
BF_ENC(r,l,s,p[15]);
BF_ENC(l,r,s,p[16]);
#if BF_ROUNDS == 20
BF_ENC(r,l,s,p[17]);
BF_ENC(l,r,s,p[18]);
BF_ENC(r,l,s,p[19]);
BF_ENC(l,r,s,p[20]);
#endif
r^=p[BF_ROUNDS+1];
data[1]=l&0xffffffffL;
data[0]=r&0xffffffffL;
#else
BF_LONG l,r,t,*k;
l=data[0];
r=data[1];
k=(BF_LONG*)key;
l^=k[0];
BF_ENC(r,l,k, 1);
BF_ENC(l,r,k, 2);
BF_ENC(r,l,k, 3);
BF_ENC(l,r,k, 4);
BF_ENC(r,l,k, 5);
BF_ENC(l,r,k, 6);
BF_ENC(r,l,k, 7);
BF_ENC(l,r,k, 8);
BF_ENC(r,l,k, 9);
BF_ENC(l,r,k,10);
BF_ENC(r,l,k,11);
BF_ENC(l,r,k,12);
BF_ENC(r,l,k,13);
BF_ENC(l,r,k,14);
BF_ENC(r,l,k,15);
BF_ENC(l,r,k,16);
#if BF_ROUNDS == 20
BF_ENC(r,l,k,17);
BF_ENC(l,r,k,18);
BF_ENC(r,l,k,19);
BF_ENC(l,r,k,20);
#endif
r^=k[BF_ROUNDS+1];
data[1]=l&0xffffffffL;
data[0]=r&0xffffffffL;
#endif
}
#ifndef BF_DEFAULT_OPTIONS
void BF_decrypt(BF_LONG *data, const BF_KEY *key)
{
#ifndef BF_PTR2
const BF_LONG *p,*s;
BF_LONG l,r;
p=key->P;
s= &(key->S[0]);
l=data[0];
r=data[1];
l^=p[BF_ROUNDS+1];
#if BF_ROUNDS == 20
BF_ENC(r,l,s,p[20]);
BF_ENC(l,r,s,p[19]);
BF_ENC(r,l,s,p[18]);
BF_ENC(l,r,s,p[17]);
#endif
BF_ENC(r,l,s,p[16]);
BF_ENC(l,r,s,p[15]);
BF_ENC(r,l,s,p[14]);
BF_ENC(l,r,s,p[13]);
BF_ENC(r,l,s,p[12]);
BF_ENC(l,r,s,p[11]);
BF_ENC(r,l,s,p[10]);
BF_ENC(l,r,s,p[ 9]);
BF_ENC(r,l,s,p[ 8]);
BF_ENC(l,r,s,p[ 7]);
BF_ENC(r,l,s,p[ 6]);
BF_ENC(l,r,s,p[ 5]);
BF_ENC(r,l,s,p[ 4]);
BF_ENC(l,r,s,p[ 3]);
BF_ENC(r,l,s,p[ 2]);
BF_ENC(l,r,s,p[ 1]);
r^=p[0];
data[1]=l&0xffffffffL;
data[0]=r&0xffffffffL;
#else
BF_LONG l,r,t,*k;
l=data[0];
r=data[1];
k=(BF_LONG *)key;
l^=k[BF_ROUNDS+1];
#if BF_ROUNDS == 20
BF_ENC(r,l,k,20);
BF_ENC(l,r,k,19);
BF_ENC(r,l,k,18);
BF_ENC(l,r,k,17);
#endif
BF_ENC(r,l,k,16);
BF_ENC(l,r,k,15);
BF_ENC(r,l,k,14);
BF_ENC(l,r,k,13);
BF_ENC(r,l,k,12);
BF_ENC(l,r,k,11);
BF_ENC(r,l,k,10);
BF_ENC(l,r,k, 9);
BF_ENC(r,l,k, 8);
BF_ENC(l,r,k, 7);
BF_ENC(r,l,k, 6);
BF_ENC(l,r,k, 5);
BF_ENC(r,l,k, 4);
BF_ENC(l,r,k, 3);
BF_ENC(r,l,k, 2);
BF_ENC(l,r,k, 1);
r^=k[0];
data[1]=l&0xffffffffL;
data[0]=r&0xffffffffL;
#endif
}
void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
const BF_KEY *schedule, unsigned char *ivec, int encrypt)
{
BF_LONG tin0,tin1;
BF_LONG tout0,tout1,xor0,xor1;
long l=length;
BF_LONG tin[2];
if (encrypt)
{
n2l(ivec,tout0);
n2l(ivec,tout1);
ivec-=8;
for (l-=8; l>=0; l-=8)
{
n2l(in,tin0);
n2l(in,tin1);
tin0^=tout0;
tin1^=tout1;
tin[0]=tin0;
tin[1]=tin1;
BF_encrypt(tin,schedule);
tout0=tin[0];
tout1=tin[1];
l2n(tout0,out);
l2n(tout1,out);
}
if (l != -8)
{
n2ln(in,tin0,tin1,l+8);
tin0^=tout0;
tin1^=tout1;
tin[0]=tin0;
tin[1]=tin1;
BF_encrypt(tin,schedule);
tout0=tin[0];
tout1=tin[1];
l2n(tout0,out);
l2n(tout1,out);
}
l2n(tout0,ivec);
l2n(tout1,ivec);
}
else
{
n2l(ivec,xor0);
n2l(ivec,xor1);
ivec-=8;
for (l-=8; l>=0; l-=8)
{
n2l(in,tin0);
n2l(in,tin1);
tin[0]=tin0;
tin[1]=tin1;
BF_decrypt(tin,schedule);
tout0=tin[0]^xor0;
tout1=tin[1]^xor1;
l2n(tout0,out);
l2n(tout1,out);
xor0=tin0;
xor1=tin1;
}
if (l != -8)
{
n2l(in,tin0);
n2l(in,tin1);
tin[0]=tin0;
tin[1]=tin1;
BF_decrypt(tin,schedule);
tout0=tin[0]^xor0;
tout1=tin[1]^xor1;
l2nn(tout0,tout1,out,l+8);
xor0=tin0;
xor1=tin1;
}
l2n(xor0,ivec);
l2n(xor1,ivec);
}
tin0=tin1=tout0=tout1=xor0=xor1=0;
tin[0]=tin[1]=0;
}
#endif
+218
View File
@@ -0,0 +1,218 @@
/* crypto/bf/bf_locl.h */
/* Copyright (C) 1995-1997 Eric Young ([email protected])
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young ([email protected]).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson ([email protected]).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young ([email protected])"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson ([email protected])"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
#ifndef HEADER_BF_LOCL_H
#define HEADER_BF_LOCL_H
#undef c2l
#define c2l(c,l) (l =((unsigned long)(*((c)++))) , \
l|=((unsigned long)(*((c)++)))<< 8L, \
l|=((unsigned long)(*((c)++)))<<16L, \
l|=((unsigned long)(*((c)++)))<<24L)
/* NOTE - c is not incremented as per c2l */
#undef c2ln
#define c2ln(c,l1,l2,n) { \
c+=n; \
l1=l2=0; \
switch (n) { \
case 8: l2 =((unsigned long)(*(--(c))))<<24L; \
case 7: l2|=((unsigned long)(*(--(c))))<<16L; \
case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \
case 5: l2|=((unsigned long)(*(--(c)))); \
case 4: l1 =((unsigned long)(*(--(c))))<<24L; \
case 3: l1|=((unsigned long)(*(--(c))))<<16L; \
case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \
case 1: l1|=((unsigned long)(*(--(c)))); \
} \
}
#undef l2c
#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
*((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
*((c)++)=(unsigned char)(((l)>>16L)&0xff), \
*((c)++)=(unsigned char)(((l)>>24L)&0xff))
/* NOTE - c is not incremented as per l2c */
#undef l2cn
#define l2cn(l1,l2,c,n) { \
c+=n; \
switch (n) { \
case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
} \
}
/* NOTE - c is not incremented as per n2l */
#define n2ln(c,l1,l2,n) { \
c+=n; \
l1=l2=0; \
switch (n) { \
case 8: l2 =((unsigned long)(*(--(c)))) ; \
case 7: l2|=((unsigned long)(*(--(c))))<< 8; \
case 6: l2|=((unsigned long)(*(--(c))))<<16; \
case 5: l2|=((unsigned long)(*(--(c))))<<24; \
case 4: l1 =((unsigned long)(*(--(c)))) ; \
case 3: l1|=((unsigned long)(*(--(c))))<< 8; \
case 2: l1|=((unsigned long)(*(--(c))))<<16; \
case 1: l1|=((unsigned long)(*(--(c))))<<24; \
} \
}
/* NOTE - c is not incremented as per l2n */
#define l2nn(l1,l2,c,n) { \
c+=n; \
switch (n) { \
case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \
case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \
case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
} \
}
#undef n2l
#define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \
l|=((unsigned long)(*((c)++)))<<16L, \
l|=((unsigned long)(*((c)++)))<< 8L, \
l|=((unsigned long)(*((c)++))))
#undef l2n
#define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
*((c)++)=(unsigned char)(((l)>>16L)&0xff), \
*((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
*((c)++)=(unsigned char)(((l) )&0xff))
/* This is actually a big endian algorithm, the most significant byte
* is used to lookup array 0 */
#if defined(BF_PTR2)
/*
* This is basically a special Intel version. Point is that Intel
* doesn't have many registers, but offers a reach choice of addressing
* modes. So we spare some registers by directly traversing BF_KEY
* structure and hiring the most decorated addressing mode. The code
* generated by EGCS is *perfectly* competitive with assembler
* implementation!
*/
#define BF_ENC(LL,R,KEY,Pi) (\
LL^=KEY[Pi], \
t= KEY[BF_ROUNDS+2 + 0 + ((R>>24)&0xFF)], \
t+= KEY[BF_ROUNDS+2 + 256 + ((R>>16)&0xFF)], \
t^= KEY[BF_ROUNDS+2 + 512 + ((R>>8 )&0xFF)], \
t+= KEY[BF_ROUNDS+2 + 768 + ((R )&0xFF)], \
LL^=t \
)
#elif defined(BF_PTR)
#ifndef BF_LONG_LOG2
#define BF_LONG_LOG2 2 /* default to BF_LONG being 32 bits */
#endif
#define BF_M (0xFF<<BF_LONG_LOG2)
#define BF_0 (24-BF_LONG_LOG2)
#define BF_1 (16-BF_LONG_LOG2)
#define BF_2 ( 8-BF_LONG_LOG2)
#define BF_3 BF_LONG_LOG2 /* left shift */
/*
* This is normally very good on RISC platforms where normally you
* have to explicitly "multiply" array index by sizeof(BF_LONG)
* in order to calculate the effective address. This implementation
* excuses CPU from this extra work. Power[PC] uses should have most
* fun as (R>>BF_i)&BF_M gets folded into a single instruction, namely
* rlwinm. So let'em double-check if their compiler does it.
*/
#define BF_ENC(LL,R,S,P) ( \
LL^=P, \
LL^= (((*(BF_LONG *)((unsigned char *)&(S[ 0])+((R>>BF_0)&BF_M))+ \
*(BF_LONG *)((unsigned char *)&(S[256])+((R>>BF_1)&BF_M)))^ \
*(BF_LONG *)((unsigned char *)&(S[512])+((R>>BF_2)&BF_M)))+ \
*(BF_LONG *)((unsigned char *)&(S[768])+((R<<BF_3)&BF_M))) \
)
#else
/*
* This is a *generic* version. Seem to perform best on platforms that
* offer explicit support for extraction of 8-bit nibbles preferably
* complemented with "multiplying" of array index by sizeof(BF_LONG).
* For the moment of this writing the list comprises Alpha CPU featuring
* extbl and s[48]addq instructions.
*/
#define BF_ENC(LL,R,S,P) ( \
LL^=P, \
LL^=((( S[ ((int)(R>>24)&0xff)] + \
S[0x0100+((int)(R>>16)&0xff)])^ \
S[0x0200+((int)(R>> 8)&0xff)])+ \
S[0x0300+((int)(R )&0xff)])&0xffffffffL \
)
#endif
#endif
+325
View File
@@ -0,0 +1,325 @@
/* crypto/bf/bf_pi.h */
/* Copyright (C) 1995-1998 Eric Young ([email protected])
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young ([email protected]).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson ([email protected]).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young ([email protected])"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson ([email protected])"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
static const BF_KEY bf_init= {
{
0x243f6a88L, 0x85a308d3L, 0x13198a2eL, 0x03707344L,
0xa4093822L, 0x299f31d0L, 0x082efa98L, 0xec4e6c89L,
0x452821e6L, 0x38d01377L, 0xbe5466cfL, 0x34e90c6cL,
0xc0ac29b7L, 0xc97c50ddL, 0x3f84d5b5L, 0xb5470917L,
0x9216d5d9L, 0x8979fb1b
},{
0xd1310ba6L, 0x98dfb5acL, 0x2ffd72dbL, 0xd01adfb7L,
0xb8e1afedL, 0x6a267e96L, 0xba7c9045L, 0xf12c7f99L,
0x24a19947L, 0xb3916cf7L, 0x0801f2e2L, 0x858efc16L,
0x636920d8L, 0x71574e69L, 0xa458fea3L, 0xf4933d7eL,
0x0d95748fL, 0x728eb658L, 0x718bcd58L, 0x82154aeeL,
0x7b54a41dL, 0xc25a59b5L, 0x9c30d539L, 0x2af26013L,
0xc5d1b023L, 0x286085f0L, 0xca417918L, 0xb8db38efL,
0x8e79dcb0L, 0x603a180eL, 0x6c9e0e8bL, 0xb01e8a3eL,
0xd71577c1L, 0xbd314b27L, 0x78af2fdaL, 0x55605c60L,
0xe65525f3L, 0xaa55ab94L, 0x57489862L, 0x63e81440L,
0x55ca396aL, 0x2aab10b6L, 0xb4cc5c34L, 0x1141e8ceL,
0xa15486afL, 0x7c72e993L, 0xb3ee1411L, 0x636fbc2aL,
0x2ba9c55dL, 0x741831f6L, 0xce5c3e16L, 0x9b87931eL,
0xafd6ba33L, 0x6c24cf5cL, 0x7a325381L, 0x28958677L,
0x3b8f4898L, 0x6b4bb9afL, 0xc4bfe81bL, 0x66282193L,
0x61d809ccL, 0xfb21a991L, 0x487cac60L, 0x5dec8032L,
0xef845d5dL, 0xe98575b1L, 0xdc262302L, 0xeb651b88L,
0x23893e81L, 0xd396acc5L, 0x0f6d6ff3L, 0x83f44239L,
0x2e0b4482L, 0xa4842004L, 0x69c8f04aL, 0x9e1f9b5eL,
0x21c66842L, 0xf6e96c9aL, 0x670c9c61L, 0xabd388f0L,
0x6a51a0d2L, 0xd8542f68L, 0x960fa728L, 0xab5133a3L,
0x6eef0b6cL, 0x137a3be4L, 0xba3bf050L, 0x7efb2a98L,
0xa1f1651dL, 0x39af0176L, 0x66ca593eL, 0x82430e88L,
0x8cee8619L, 0x456f9fb4L, 0x7d84a5c3L, 0x3b8b5ebeL,
0xe06f75d8L, 0x85c12073L, 0x401a449fL, 0x56c16aa6L,
0x4ed3aa62L, 0x363f7706L, 0x1bfedf72L, 0x429b023dL,
0x37d0d724L, 0xd00a1248L, 0xdb0fead3L, 0x49f1c09bL,
0x075372c9L, 0x80991b7bL, 0x25d479d8L, 0xf6e8def7L,
0xe3fe501aL, 0xb6794c3bL, 0x976ce0bdL, 0x04c006baL,
0xc1a94fb6L, 0x409f60c4L, 0x5e5c9ec2L, 0x196a2463L,
0x68fb6fafL, 0x3e6c53b5L, 0x1339b2ebL, 0x3b52ec6fL,
0x6dfc511fL, 0x9b30952cL, 0xcc814544L, 0xaf5ebd09L,
0xbee3d004L, 0xde334afdL, 0x660f2807L, 0x192e4bb3L,
0xc0cba857L, 0x45c8740fL, 0xd20b5f39L, 0xb9d3fbdbL,
0x5579c0bdL, 0x1a60320aL, 0xd6a100c6L, 0x402c7279L,
0x679f25feL, 0xfb1fa3ccL, 0x8ea5e9f8L, 0xdb3222f8L,
0x3c7516dfL, 0xfd616b15L, 0x2f501ec8L, 0xad0552abL,
0x323db5faL, 0xfd238760L, 0x53317b48L, 0x3e00df82L,
0x9e5c57bbL, 0xca6f8ca0L, 0x1a87562eL, 0xdf1769dbL,
0xd542a8f6L, 0x287effc3L, 0xac6732c6L, 0x8c4f5573L,
0x695b27b0L, 0xbbca58c8L, 0xe1ffa35dL, 0xb8f011a0L,
0x10fa3d98L, 0xfd2183b8L, 0x4afcb56cL, 0x2dd1d35bL,
0x9a53e479L, 0xb6f84565L, 0xd28e49bcL, 0x4bfb9790L,
0xe1ddf2daL, 0xa4cb7e33L, 0x62fb1341L, 0xcee4c6e8L,
0xef20cadaL, 0x36774c01L, 0xd07e9efeL, 0x2bf11fb4L,
0x95dbda4dL, 0xae909198L, 0xeaad8e71L, 0x6b93d5a0L,
0xd08ed1d0L, 0xafc725e0L, 0x8e3c5b2fL, 0x8e7594b7L,
0x8ff6e2fbL, 0xf2122b64L, 0x8888b812L, 0x900df01cL,
0x4fad5ea0L, 0x688fc31cL, 0xd1cff191L, 0xb3a8c1adL,
0x2f2f2218L, 0xbe0e1777L, 0xea752dfeL, 0x8b021fa1L,
0xe5a0cc0fL, 0xb56f74e8L, 0x18acf3d6L, 0xce89e299L,
0xb4a84fe0L, 0xfd13e0b7L, 0x7cc43b81L, 0xd2ada8d9L,
0x165fa266L, 0x80957705L, 0x93cc7314L, 0x211a1477L,
0xe6ad2065L, 0x77b5fa86L, 0xc75442f5L, 0xfb9d35cfL,
0xebcdaf0cL, 0x7b3e89a0L, 0xd6411bd3L, 0xae1e7e49L,
0x00250e2dL, 0x2071b35eL, 0x226800bbL, 0x57b8e0afL,
0x2464369bL, 0xf009b91eL, 0x5563911dL, 0x59dfa6aaL,
0x78c14389L, 0xd95a537fL, 0x207d5ba2L, 0x02e5b9c5L,
0x83260376L, 0x6295cfa9L, 0x11c81968L, 0x4e734a41L,
0xb3472dcaL, 0x7b14a94aL, 0x1b510052L, 0x9a532915L,
0xd60f573fL, 0xbc9bc6e4L, 0x2b60a476L, 0x81e67400L,
0x08ba6fb5L, 0x571be91fL, 0xf296ec6bL, 0x2a0dd915L,
0xb6636521L, 0xe7b9f9b6L, 0xff34052eL, 0xc5855664L,
0x53b02d5dL, 0xa99f8fa1L, 0x08ba4799L, 0x6e85076aL,
0x4b7a70e9L, 0xb5b32944L, 0xdb75092eL, 0xc4192623L,
0xad6ea6b0L, 0x49a7df7dL, 0x9cee60b8L, 0x8fedb266L,
0xecaa8c71L, 0x699a17ffL, 0x5664526cL, 0xc2b19ee1L,
0x193602a5L, 0x75094c29L, 0xa0591340L, 0xe4183a3eL,
0x3f54989aL, 0x5b429d65L, 0x6b8fe4d6L, 0x99f73fd6L,
0xa1d29c07L, 0xefe830f5L, 0x4d2d38e6L, 0xf0255dc1L,
0x4cdd2086L, 0x8470eb26L, 0x6382e9c6L, 0x021ecc5eL,
0x09686b3fL, 0x3ebaefc9L, 0x3c971814L, 0x6b6a70a1L,
0x687f3584L, 0x52a0e286L, 0xb79c5305L, 0xaa500737L,
0x3e07841cL, 0x7fdeae5cL, 0x8e7d44ecL, 0x5716f2b8L,
0xb03ada37L, 0xf0500c0dL, 0xf01c1f04L, 0x0200b3ffL,
0xae0cf51aL, 0x3cb574b2L, 0x25837a58L, 0xdc0921bdL,
0xd19113f9L, 0x7ca92ff6L, 0x94324773L, 0x22f54701L,
0x3ae5e581L, 0x37c2dadcL, 0xc8b57634L, 0x9af3dda7L,
0xa9446146L, 0x0fd0030eL, 0xecc8c73eL, 0xa4751e41L,
0xe238cd99L, 0x3bea0e2fL, 0x3280bba1L, 0x183eb331L,
0x4e548b38L, 0x4f6db908L, 0x6f420d03L, 0xf60a04bfL,
0x2cb81290L, 0x24977c79L, 0x5679b072L, 0xbcaf89afL,
0xde9a771fL, 0xd9930810L, 0xb38bae12L, 0xdccf3f2eL,
0x5512721fL, 0x2e6b7124L, 0x501adde6L, 0x9f84cd87L,
0x7a584718L, 0x7408da17L, 0xbc9f9abcL, 0xe94b7d8cL,
0xec7aec3aL, 0xdb851dfaL, 0x63094366L, 0xc464c3d2L,
0xef1c1847L, 0x3215d908L, 0xdd433b37L, 0x24c2ba16L,
0x12a14d43L, 0x2a65c451L, 0x50940002L, 0x133ae4ddL,
0x71dff89eL, 0x10314e55L, 0x81ac77d6L, 0x5f11199bL,
0x043556f1L, 0xd7a3c76bL, 0x3c11183bL, 0x5924a509L,
0xf28fe6edL, 0x97f1fbfaL, 0x9ebabf2cL, 0x1e153c6eL,
0x86e34570L, 0xeae96fb1L, 0x860e5e0aL, 0x5a3e2ab3L,
0x771fe71cL, 0x4e3d06faL, 0x2965dcb9L, 0x99e71d0fL,
0x803e89d6L, 0x5266c825L, 0x2e4cc978L, 0x9c10b36aL,
0xc6150ebaL, 0x94e2ea78L, 0xa5fc3c53L, 0x1e0a2df4L,
0xf2f74ea7L, 0x361d2b3dL, 0x1939260fL, 0x19c27960L,
0x5223a708L, 0xf71312b6L, 0xebadfe6eL, 0xeac31f66L,
0xe3bc4595L, 0xa67bc883L, 0xb17f37d1L, 0x018cff28L,
0xc332ddefL, 0xbe6c5aa5L, 0x65582185L, 0x68ab9802L,
0xeecea50fL, 0xdb2f953bL, 0x2aef7dadL, 0x5b6e2f84L,
0x1521b628L, 0x29076170L, 0xecdd4775L, 0x619f1510L,
0x13cca830L, 0xeb61bd96L, 0x0334fe1eL, 0xaa0363cfL,
0xb5735c90L, 0x4c70a239L, 0xd59e9e0bL, 0xcbaade14L,
0xeecc86bcL, 0x60622ca7L, 0x9cab5cabL, 0xb2f3846eL,
0x648b1eafL, 0x19bdf0caL, 0xa02369b9L, 0x655abb50L,
0x40685a32L, 0x3c2ab4b3L, 0x319ee9d5L, 0xc021b8f7L,
0x9b540b19L, 0x875fa099L, 0x95f7997eL, 0x623d7da8L,
0xf837889aL, 0x97e32d77L, 0x11ed935fL, 0x16681281L,
0x0e358829L, 0xc7e61fd6L, 0x96dedfa1L, 0x7858ba99L,
0x57f584a5L, 0x1b227263L, 0x9b83c3ffL, 0x1ac24696L,
0xcdb30aebL, 0x532e3054L, 0x8fd948e4L, 0x6dbc3128L,
0x58ebf2efL, 0x34c6ffeaL, 0xfe28ed61L, 0xee7c3c73L,
0x5d4a14d9L, 0xe864b7e3L, 0x42105d14L, 0x203e13e0L,
0x45eee2b6L, 0xa3aaabeaL, 0xdb6c4f15L, 0xfacb4fd0L,
0xc742f442L, 0xef6abbb5L, 0x654f3b1dL, 0x41cd2105L,
0xd81e799eL, 0x86854dc7L, 0xe44b476aL, 0x3d816250L,
0xcf62a1f2L, 0x5b8d2646L, 0xfc8883a0L, 0xc1c7b6a3L,
0x7f1524c3L, 0x69cb7492L, 0x47848a0bL, 0x5692b285L,
0x095bbf00L, 0xad19489dL, 0x1462b174L, 0x23820e00L,
0x58428d2aL, 0x0c55f5eaL, 0x1dadf43eL, 0x233f7061L,
0x3372f092L, 0x8d937e41L, 0xd65fecf1L, 0x6c223bdbL,
0x7cde3759L, 0xcbee7460L, 0x4085f2a7L, 0xce77326eL,
0xa6078084L, 0x19f8509eL, 0xe8efd855L, 0x61d99735L,
0xa969a7aaL, 0xc50c06c2L, 0x5a04abfcL, 0x800bcadcL,
0x9e447a2eL, 0xc3453484L, 0xfdd56705L, 0x0e1e9ec9L,
0xdb73dbd3L, 0x105588cdL, 0x675fda79L, 0xe3674340L,
0xc5c43465L, 0x713e38d8L, 0x3d28f89eL, 0xf16dff20L,
0x153e21e7L, 0x8fb03d4aL, 0xe6e39f2bL, 0xdb83adf7L,
0xe93d5a68L, 0x948140f7L, 0xf64c261cL, 0x94692934L,
0x411520f7L, 0x7602d4f7L, 0xbcf46b2eL, 0xd4a20068L,
0xd4082471L, 0x3320f46aL, 0x43b7d4b7L, 0x500061afL,
0x1e39f62eL, 0x97244546L, 0x14214f74L, 0xbf8b8840L,
0x4d95fc1dL, 0x96b591afL, 0x70f4ddd3L, 0x66a02f45L,
0xbfbc09ecL, 0x03bd9785L, 0x7fac6dd0L, 0x31cb8504L,
0x96eb27b3L, 0x55fd3941L, 0xda2547e6L, 0xabca0a9aL,
0x28507825L, 0x530429f4L, 0x0a2c86daL, 0xe9b66dfbL,
0x68dc1462L, 0xd7486900L, 0x680ec0a4L, 0x27a18deeL,
0x4f3ffea2L, 0xe887ad8cL, 0xb58ce006L, 0x7af4d6b6L,
0xaace1e7cL, 0xd3375fecL, 0xce78a399L, 0x406b2a42L,
0x20fe9e35L, 0xd9f385b9L, 0xee39d7abL, 0x3b124e8bL,
0x1dc9faf7L, 0x4b6d1856L, 0x26a36631L, 0xeae397b2L,
0x3a6efa74L, 0xdd5b4332L, 0x6841e7f7L, 0xca7820fbL,
0xfb0af54eL, 0xd8feb397L, 0x454056acL, 0xba489527L,
0x55533a3aL, 0x20838d87L, 0xfe6ba9b7L, 0xd096954bL,
0x55a867bcL, 0xa1159a58L, 0xcca92963L, 0x99e1db33L,
0xa62a4a56L, 0x3f3125f9L, 0x5ef47e1cL, 0x9029317cL,
0xfdf8e802L, 0x04272f70L, 0x80bb155cL, 0x05282ce3L,
0x95c11548L, 0xe4c66d22L, 0x48c1133fL, 0xc70f86dcL,
0x07f9c9eeL, 0x41041f0fL, 0x404779a4L, 0x5d886e17L,
0x325f51ebL, 0xd59bc0d1L, 0xf2bcc18fL, 0x41113564L,
0x257b7834L, 0x602a9c60L, 0xdff8e8a3L, 0x1f636c1bL,
0x0e12b4c2L, 0x02e1329eL, 0xaf664fd1L, 0xcad18115L,
0x6b2395e0L, 0x333e92e1L, 0x3b240b62L, 0xeebeb922L,
0x85b2a20eL, 0xe6ba0d99L, 0xde720c8cL, 0x2da2f728L,
0xd0127845L, 0x95b794fdL, 0x647d0862L, 0xe7ccf5f0L,
0x5449a36fL, 0x877d48faL, 0xc39dfd27L, 0xf33e8d1eL,
0x0a476341L, 0x992eff74L, 0x3a6f6eabL, 0xf4f8fd37L,
0xa812dc60L, 0xa1ebddf8L, 0x991be14cL, 0xdb6e6b0dL,
0xc67b5510L, 0x6d672c37L, 0x2765d43bL, 0xdcd0e804L,
0xf1290dc7L, 0xcc00ffa3L, 0xb5390f92L, 0x690fed0bL,
0x667b9ffbL, 0xcedb7d9cL, 0xa091cf0bL, 0xd9155ea3L,
0xbb132f88L, 0x515bad24L, 0x7b9479bfL, 0x763bd6ebL,
0x37392eb3L, 0xcc115979L, 0x8026e297L, 0xf42e312dL,
0x6842ada7L, 0xc66a2b3bL, 0x12754cccL, 0x782ef11cL,
0x6a124237L, 0xb79251e7L, 0x06a1bbe6L, 0x4bfb6350L,
0x1a6b1018L, 0x11caedfaL, 0x3d25bdd8L, 0xe2e1c3c9L,
0x44421659L, 0x0a121386L, 0xd90cec6eL, 0xd5abea2aL,
0x64af674eL, 0xda86a85fL, 0xbebfe988L, 0x64e4c3feL,
0x9dbc8057L, 0xf0f7c086L, 0x60787bf8L, 0x6003604dL,
0xd1fd8346L, 0xf6381fb0L, 0x7745ae04L, 0xd736fcccL,
0x83426b33L, 0xf01eab71L, 0xb0804187L, 0x3c005e5fL,
0x77a057beL, 0xbde8ae24L, 0x55464299L, 0xbf582e61L,
0x4e58f48fL, 0xf2ddfda2L, 0xf474ef38L, 0x8789bdc2L,
0x5366f9c3L, 0xc8b38e74L, 0xb475f255L, 0x46fcd9b9L,
0x7aeb2661L, 0x8b1ddf84L, 0x846a0e79L, 0x915f95e2L,
0x466e598eL, 0x20b45770L, 0x8cd55591L, 0xc902de4cL,
0xb90bace1L, 0xbb8205d0L, 0x11a86248L, 0x7574a99eL,
0xb77f19b6L, 0xe0a9dc09L, 0x662d09a1L, 0xc4324633L,
0xe85a1f02L, 0x09f0be8cL, 0x4a99a025L, 0x1d6efe10L,
0x1ab93d1dL, 0x0ba5a4dfL, 0xa186f20fL, 0x2868f169L,
0xdcb7da83L, 0x573906feL, 0xa1e2ce9bL, 0x4fcd7f52L,
0x50115e01L, 0xa70683faL, 0xa002b5c4L, 0x0de6d027L,
0x9af88c27L, 0x773f8641L, 0xc3604c06L, 0x61a806b5L,
0xf0177a28L, 0xc0f586e0L, 0x006058aaL, 0x30dc7d62L,
0x11e69ed7L, 0x2338ea63L, 0x53c2dd94L, 0xc2c21634L,
0xbbcbee56L, 0x90bcb6deL, 0xebfc7da1L, 0xce591d76L,
0x6f05e409L, 0x4b7c0188L, 0x39720a3dL, 0x7c927c24L,
0x86e3725fL, 0x724d9db9L, 0x1ac15bb4L, 0xd39eb8fcL,
0xed545578L, 0x08fca5b5L, 0xd83d7cd3L, 0x4dad0fc4L,
0x1e50ef5eL, 0xb161e6f8L, 0xa28514d9L, 0x6c51133cL,
0x6fd5c7e7L, 0x56e14ec4L, 0x362abfceL, 0xddc6c837L,
0xd79a3234L, 0x92638212L, 0x670efa8eL, 0x406000e0L,
0x3a39ce37L, 0xd3faf5cfL, 0xabc27737L, 0x5ac52d1bL,
0x5cb0679eL, 0x4fa33742L, 0xd3822740L, 0x99bc9bbeL,
0xd5118e9dL, 0xbf0f7315L, 0xd62d1c7eL, 0xc700c47bL,
0xb78c1b6bL, 0x21a19045L, 0xb26eb1beL, 0x6a366eb4L,
0x5748ab2fL, 0xbc946e79L, 0xc6a376d2L, 0x6549c2c8L,
0x530ff8eeL, 0x468dde7dL, 0xd5730a1dL, 0x4cd04dc6L,
0x2939bbdbL, 0xa9ba4650L, 0xac9526e8L, 0xbe5ee304L,
0xa1fad5f0L, 0x6a2d519aL, 0x63ef8ce2L, 0x9a86ee22L,
0xc089c2b8L, 0x43242ef6L, 0xa51e03aaL, 0x9cf2d0a4L,
0x83c061baL, 0x9be96a4dL, 0x8fe51550L, 0xba645bd6L,
0x2826a2f9L, 0xa73a3ae1L, 0x4ba99586L, 0xef5562e9L,
0xc72fefd3L, 0xf752f7daL, 0x3f046f69L, 0x77fa0a59L,
0x80e4a915L, 0x87b08601L, 0x9b09e6adL, 0x3b3ee593L,
0xe990fd5aL, 0x9e34d797L, 0x2cf0b7d9L, 0x022b8b51L,
0x96d5ac3aL, 0x017da67dL, 0xd1cf3ed6L, 0x7c7d2d28L,
0x1f9f25cfL, 0xadf2b89bL, 0x5ad6b472L, 0x5a88f54cL,
0xe029ac71L, 0xe019a5e6L, 0x47b0acfdL, 0xed93fa9bL,
0xe8d3c48dL, 0x283b57ccL, 0xf8d56629L, 0x79132e28L,
0x785f0191L, 0xed756055L, 0xf7960e44L, 0xe3d35e8cL,
0x15056dd4L, 0x88f46dbaL, 0x03a16125L, 0x0564f0bdL,
0xc3eb9e15L, 0x3c9057a2L, 0x97271aecL, 0xa93a072aL,
0x1b3f6d9bL, 0x1e6321f5L, 0xf59c66fbL, 0x26dcf319L,
0x7533d928L, 0xb155fdf5L, 0x03563482L, 0x8aba3cbbL,
0x28517711L, 0xc20ad9f8L, 0xabcc5167L, 0xccad925fL,
0x4de81751L, 0x3830dc8eL, 0x379d5862L, 0x9320f991L,
0xea7a90c2L, 0xfb3e7bceL, 0x5121ce64L, 0x774fbe32L,
0xa8b6e37eL, 0xc3293d46L, 0x48de5369L, 0x6413e680L,
0xa2ae0810L, 0xdd6db224L, 0x69852dfdL, 0x09072166L,
0xb39a460aL, 0x6445c0ddL, 0x586cdecfL, 0x1c20c8aeL,
0x5bbef7ddL, 0x1b588d40L, 0xccd2017fL, 0x6bb4e3bbL,
0xdda26a7eL, 0x3a59ff45L, 0x3e350a44L, 0xbcb4cdd5L,
0x72eacea8L, 0xfa6484bbL, 0x8d6612aeL, 0xbf3c6f47L,
0xd29be463L, 0x542f5d9eL, 0xaec2771bL, 0xf64e6370L,
0x740e0d8dL, 0xe75b1357L, 0xf8721671L, 0xaf537d5dL,
0x4040cb08L, 0x4eb4e2ccL, 0x34d2466aL, 0x0115af84L,
0xe1b00428L, 0x95983a1dL, 0x06b89fb4L, 0xce6ea048L,
0x6f3f3b82L, 0x3520ab82L, 0x011a1d4bL, 0x277227f8L,
0x611560b1L, 0xe7933fdcL, 0xbb3a792bL, 0x344525bdL,
0xa08839e1L, 0x51ce794bL, 0x2f32c9b7L, 0xa01fbac9L,
0xe01cc87eL, 0xbcc7d1f6L, 0xcf0111c3L, 0xa1e8aac7L,
0x1a908749L, 0xd44fbd9aL, 0xd0dadecbL, 0xd50ada38L,
0x0339c32aL, 0xc6913667L, 0x8df9317cL, 0xe0b12b4fL,
0xf79e59b7L, 0x43f5bb3aL, 0xf2d519ffL, 0x27d9459cL,
0xbf97222cL, 0x15e6fc2aL, 0x0f91fc71L, 0x9b941525L,
0xfae59361L, 0xceb69cebL, 0xc2a86459L, 0x12baa8d1L,
0xb6c1075eL, 0xe3056a0cL, 0x10d25065L, 0xcb03a442L,
0xe0ec6e0eL, 0x1698db3bL, 0x4c98a0beL, 0x3278e964L,
0x9f1f9532L, 0xe0d392dfL, 0xd3a0342bL, 0x8971f21eL,
0x1b0a7441L, 0x4ba3348cL, 0xc5be7120L, 0xc37632d8L,
0xdf359f8dL, 0x9b992f2eL, 0xe60b6f47L, 0x0fe3f11dL,
0xe54cda54L, 0x1edad891L, 0xce6279cfL, 0xcd3e7e6fL,
0x1618b166L, 0xfd2c1d05L, 0x848fd2c5L, 0xf6fb2299L,
0xf523f357L, 0xa6327623L, 0x93a83531L, 0x56cccd02L,
0xacf08162L, 0x5a75ebb5L, 0x6e163697L, 0x88d273ccL,
0xde966292L, 0x81b949d0L, 0x4c50901bL, 0x71c65614L,
0xe6c6c7bdL, 0x327a140aL, 0x45e1d006L, 0xc3f27b9aL,
0xc9aa53fdL, 0x62a80f00L, 0xbb25bfe2L, 0x35bdd2f6L,
0x71126905L, 0xb2040222L, 0xb6cbcf7cL, 0xcd769c2bL,
0x53113ec0L, 0x1640e3d3L, 0x38abbd60L, 0x2547adf0L,
0xba38209cL, 0xf746ce76L, 0x77afa1c5L, 0x20756060L,
0x85cbfe4eL, 0x8ae88dd8L, 0x7aaaf9b0L, 0x4cf9aa7eL,
0x1948c25cL, 0x02fb8a8cL, 0x01c36ae4L, 0xd6ebe1f9L,
0x90d4f869L, 0xa65cdea0L, 0x3f09252dL, 0xc208e69fL,
0xb74e6132L, 0xce77e25bL, 0x578fdfe3L, 0x3ac372e6L,
}
};
+122
View File
@@ -0,0 +1,122 @@
/* crypto/bf/bf_skey.c */
/* Copyright (C) 1995-1998 Eric Young ([email protected])
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young ([email protected]).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson ([email protected]).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young ([email protected])"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson ([email protected])"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
#ifdef __KERNEL__
#include <linux/types.h>
#include <linux/string.h>
#else
#include <stdio.h>
#include <string.h>
#endif
#include "blowfish.h"
#include "bf_locl.h"
#include "bf_pi.h"
void BF_set_key(BF_KEY *key, int len, const unsigned char *data)
{
int i;
BF_LONG *p,ri,in[2];
const unsigned char *d,*end;
memcpy((char *)key,(const char *)&bf_init,sizeof(BF_KEY));
p=key->P;
if (len > ((BF_ROUNDS+2)*4)) len=(BF_ROUNDS+2)*4;
d=data;
end= &(data[len]);
for (i=0; i<(BF_ROUNDS+2); i++)
{
ri= *(d++);
if (d >= end) d=data;
ri<<=8;
ri|= *(d++);
if (d >= end) d=data;
ri<<=8;
ri|= *(d++);
if (d >= end) d=data;
ri<<=8;
ri|= *(d++);
if (d >= end) d=data;
p[i]^=ri;
}
in[0]=0L;
in[1]=0L;
for (i=0; i<(BF_ROUNDS+2); i+=2)
{
BF_encrypt(in,key);
p[i ]=in[0];
p[i+1]=in[1];
}
p=key->S;
for (i=0; i<4*256; i+=2)
{
BF_encrypt(in,key);
p[i ]=in[0];
p[i+1]=in[1];
}
}
+133
View File
@@ -0,0 +1,133 @@
/* crypto/bf/blowfish.h */
/* Copyright (C) 1995-1998 Eric Young ([email protected])
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young ([email protected]).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson ([email protected]).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young ([email protected])"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson ([email protected])"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
#ifndef HEADER_BLOWFISH_H
#define HEADER_BLOWFISH_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef NO_BF
#error BF is disabled.
#endif
#define BF_ENCRYPT 1
#define BF_DECRYPT 0
/*
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* ! BF_LONG has to be at least 32 bits wide. If it's wider, then !
* ! BF_LONG_LOG2 has to be defined along. !
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
#if defined(WIN16) || defined(__LP32__)
#define BF_LONG unsigned long
#elif defined(_CRAY) || defined(__ILP64__)
#define BF_LONG unsigned long
#define BF_LONG_LOG2 3
#endif
/*
* _CRAY note. I could declare short, but I have no idea what impact
* does it have on performance on none-T3E machines. I could declare
* int, but at least on C90 sizeof(int) can be chosen at compile time.
* So I've chosen long...
* <[email protected]>
*/
/* des.h-like hack <[email protected]> */
#ifndef BF_LONG
#ifdef __KERNEL__
#include <linux/types.h>
#else
#include <sys/types.h>
#endif
#define BF_LONG u_int32_t
#endif
#define BF_ROUNDS 16
#define BF_BLOCK 8
typedef struct bf_key_st
{
BF_LONG P[BF_ROUNDS+2];
BF_LONG S[4*256];
} BF_KEY;
void BF_set_key(BF_KEY *key, int len, const unsigned char *data);
void BF_encrypt(BF_LONG *data,const BF_KEY *key);
void BF_decrypt(BF_LONG *data,const BF_KEY *key);
void BF_ecb_encrypt(const unsigned char *in, unsigned char *out,
const BF_KEY *key, int enc);
void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
const BF_KEY *schedule, unsigned char *ivec, int enc);
void BF_cfb64_encrypt(const unsigned char *in, unsigned char *out, long length,
const BF_KEY *schedule, unsigned char *ivec, int *num, int enc);
void BF_ofb64_encrypt(const unsigned char *in, unsigned char *out, long length,
const BF_KEY *schedule, unsigned char *ivec, int *num);
const char *BF_options(void);
#ifdef __cplusplus
}
#endif
#endif
+20
View File
@@ -0,0 +1,20 @@
CFLAGS=-O3 -fomit-frame-pointer -D__KERNEL__ -Wall $(EXTRA_CFLAGS)
INC=-I../include
LIBOBJ=serpent.o serpent_cbc.o
BLIB=libserpent.a
.c.o:
$(CC) $(CPPFLAGS) $(CFLAGS) $(INC) -c $< -o $@
$(BLIB): $(LIBOBJ)
/bin/rm -f $(BLIB)
ar cr $(BLIB) $(LIBOBJ)
-if test -s /bin/ranlib; then /bin/ranlib $(BLIB); \
else if test -s /usr/bin/ranlib; then /usr/bin/ranlib $(BLIB); \
else exit 0; fi; fi
test: test_main.o $(BLIB)
$(CC) -o $@ $^
clean:
rm -f *.[oa] core $(TARGET) test
+995
View File
@@ -0,0 +1,995 @@
/* Optimized implementation of the Serpent AES candidate algorithm
* Designed by Anderson, Biham and Knudsen and Implemented by
* Gisle Sælensminde 2000.
*
* The implementation is based on the pentium optimised sboxes of
* Dag Arne Osvik. Even these sboxes are designed to be optimal for x86
* processors they are efficient on other processors as well, but the speedup
* isn't so impressive compared to other implementations.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*/
#ifdef __KERNEL__
#include <linux/init.h>
#include <linux/types.h>
#include <asm/byteorder.h>
#else
#include <sys/types.h>
#include <asm/byteorder.h>
#endif
#include "serpent.h"
#define rotl(reg, val) ((reg << val) | (reg >> (32 - val)))
#define rotr(reg, val) ((reg >> val) | (reg << (32 - val)))
#ifdef __cpu_to_be32
#define BLOCK_SWAP
#define io_swap(x) __cpu_to_be32(x)
#else
#undef BLOCK_SWAP
#endif
/* The sbox functions. The first four parameters is the input bits, and
* the last is a tempoary. These parameters are also used for output, but
* the bit order is permuted. The output bit order from S0 is
* (1 4 2 0 3), where 3 is the (now useless) tempoary.
*/
#define S0(r0,r1,r2,r3,r4) \
r3 = r3 ^ r0; \
r4 = r1; \
r1 = r1 & r3; \
r4 = r4 ^ r2; \
r1 = r1 ^ r0; \
r0 = r0 | r3; \
r0 = r0 ^ r4; \
r4 = r4 ^ r3; \
r3 = r3 ^ r2; \
r2 = r2 | r1; \
r2 = r2 ^ r4; \
r4 = -1 ^ r4; \
r4 = r4 | r1; \
r1 = r1 ^ r3; \
r1 = r1 ^ r4; \
r3 = r3 | r0; \
r1 = r1 ^ r3; \
r4 = r4 ^ r3;
#define S1(r0,r1,r2,r3,r4) \
r1 = -1 ^ r1; \
r4 = r0; \
r0 = r0 ^ r1; \
r4 = r4 | r1; \
r4 = r4 ^ r3; \
r3 = r3 & r0; \
r2 = r2 ^ r4; \
r3 = r3 ^ r1; \
r3 = r3 | r2; \
r0 = r0 ^ r4; \
r3 = r3 ^ r0; \
r1 = r1 & r2; \
r0 = r0 | r1; \
r1 = r1 ^ r4; \
r0 = r0 ^ r2; \
r4 = r4 | r3; \
r0 = r0 ^ r4; \
r4 = -1 ^ r4; \
r1 = r1 ^ r3; \
r4 = r4 & r2; \
r1 = -1 ^ r1; \
r4 = r4 ^ r0; \
r1 = r1 ^ r4;
#define S2(r0,r1,r2,r3,r4) \
r4 = r0; \
r0 = r0 & r2; \
r0 = r0 ^ r3; \
r2 = r2 ^ r1; \
r2 = r2 ^ r0; \
r3 = r3 | r4; \
r3 = r3 ^ r1; \
r4 = r4 ^ r2; \
r1 = r3; \
r3 = r3 | r4; \
r3 = r3 ^ r0; \
r0 = r0 & r1; \
r4 = r4 ^ r0; \
r1 = r1 ^ r3; \
r1 = r1 ^ r4; \
r4 = -1 ^ r4;
#define S3(r0,r1,r2,r3,r4) \
r4 = r0 ; \
r0 = r0 | r3; \
r3 = r3 ^ r1; \
r1 = r1 & r4; \
r4 = r4 ^ r2; \
r2 = r2 ^ r3; \
r3 = r3 & r0; \
r4 = r4 | r1; \
r3 = r3 ^ r4; \
r0 = r0 ^ r1; \
r4 = r4 & r0; \
r1 = r1 ^ r3; \
r4 = r4 ^ r2; \
r1 = r1 | r0; \
r1 = r1 ^ r2; \
r0 = r0 ^ r3; \
r2 = r1; \
r1 = r1 | r3; \
r1 = r1 ^ r0;
#define S4(r0,r1,r2,r3,r4) \
r1 = r1 ^ r3; \
r3 = -1 ^ r3; \
r2 = r2 ^ r3; \
r3 = r3 ^ r0; \
r4 = r1; \
r1 = r1 & r3; \
r1 = r1 ^ r2; \
r4 = r4 ^ r3; \
r0 = r0 ^ r4; \
r2 = r2 & r4; \
r2 = r2 ^ r0; \
r0 = r0 & r1; \
r3 = r3 ^ r0; \
r4 = r4 | r1; \
r4 = r4 ^ r0; \
r0 = r0 | r3; \
r0 = r0 ^ r2; \
r2 = r2 & r3; \
r0 = -1 ^ r0; \
r4 = r4 ^ r2;
#define S5(r0,r1,r2,r3,r4) \
r0 = r0 ^ r1; \
r1 = r1 ^ r3; \
r3 = -1 ^ r3; \
r4 = r1; \
r1 = r1 & r0; \
r2 = r2 ^ r3; \
r1 = r1 ^ r2; \
r2 = r2 | r4; \
r4 = r4 ^ r3; \
r3 = r3 & r1; \
r3 = r3 ^ r0; \
r4 = r4 ^ r1; \
r4 = r4 ^ r2; \
r2 = r2 ^ r0; \
r0 = r0 & r3; \
r2 = -1 ^ r2; \
r0 = r0 ^ r4; \
r4 = r4 | r3; \
r2 = r2 ^ r4;
#define S6(r0,r1,r2,r3,r4) \
r2 = -1 ^ r2; \
r4 = r3; \
r3 = r3 & r0; \
r0 = r0 ^ r4; \
r3 = r3 ^ r2; \
r2 = r2 | r4; \
r1 = r1 ^ r3; \
r2 = r2 ^ r0; \
r0 = r0 | r1; \
r2 = r2 ^ r1; \
r4 = r4 ^ r0; \
r0 = r0 | r3; \
r0 = r0 ^ r2; \
r4 = r4 ^ r3; \
r4 = r4 ^ r0; \
r3 = -1 ^ r3; \
r2 = r2 & r4; \
r2 = r2 ^ r3;
#define S7(r0,r1,r2,r3,r4) \
r4 = r2; \
r2 = r2 & r1; \
r2 = r2 ^ r3; \
r3 = r3 & r1; \
r4 = r4 ^ r2; \
r2 = r2 ^ r1; \
r1 = r1 ^ r0; \
r0 = r0 | r4; \
r0 = r0 ^ r2; \
r3 = r3 ^ r1; \
r2 = r2 ^ r3; \
r3 = r3 & r0; \
r3 = r3 ^ r4; \
r4 = r4 ^ r2; \
r2 = r2 & r0; \
r4 = -1 ^ r4; \
r2 = r2 ^ r4; \
r4 = r4 & r0; \
r1 = r1 ^ r3; \
r4 = r4 ^ r1;
/* The inverse sboxes */
#define I0(r0,r1,r2,r3,r4) \
r2 = r2 ^ -1; \
r4 = r1; \
r1 = r1 | r0; \
r4 = r4 ^ -1; \
r1 = r1 ^ r2; \
r2 = r2 | r4; \
r1 = r1 ^ r3; \
r0 = r0 ^ r4; \
r2 = r2 ^ r0; \
r0 = r0 & r3; \
r4 = r4 ^ r0; \
r0 = r0 | r1; \
r0 = r0 ^ r2; \
r3 = r3 ^ r4; \
r2 = r2 ^ r1; \
r3 = r3 ^ r0; \
r3 = r3 ^ r1; \
r2 = r2 & r3; \
r4 = r4 ^ r2;
#define I1(r0,r1,r2,r3,r4) \
r4 = r1; \
r1 = r1 ^ r3; \
r3 = r3 & r1; \
r4 = r4 ^ r2; \
r3 = r3 ^ r0; \
r0 = r0 | r1; \
r2 = r2 ^ r3; \
r0 = r0 ^ r4; \
r0 = r0 | r2; \
r1 = r1 ^ r3; \
r0 = r0 ^ r1; \
r1 = r1 | r3; \
r1 = r1 ^ r0; \
r4 = r4 ^ -1; \
r4 = r4 ^ r1; \
r1 = r1 | r0; \
r1 = r1 ^ r0; \
r1 = r1 | r4; \
r3 = r3 ^ r1;
#define I2(r0,r1,r2,r3,r4) \
r2 = r2 ^ r3; \
r3 = r3 ^ r0; \
r4 = r3; \
r3 = r3 & r2; \
r3 = r3 ^ r1; \
r1 = r1 | r2; \
r1 = r1 ^ r4; \
r4 = r4 & r3; \
r2 = r2 ^ r3; \
r4 = r4 & r0; \
r4 = r4 ^ r2; \
r2 = r2 & r1; \
r2 = r2 | r0; \
r3 = r3 ^ -1; \
r2 = r2 ^ r3; \
r0 = r0 ^ r3; \
r0 = r0 & r1; \
r3 = r3 ^ r4; \
r3 = r3 ^ r0;
#define I3(r0,r1,r2,r3,r4) \
r4 = r2; \
r2 = r2 ^ r1; \
r0 = r0 ^ r2; \
r4 = r4 & r2; \
r4 = r4 ^ r0; \
r0 = r0 & r1; \
r1 = r1 ^ r3; \
r3 = r3 | r4; \
r2 = r2 ^ r3; \
r0 = r0 ^ r3; \
r1 = r1 ^ r4; \
r3 = r3 & r2; \
r3 = r3 ^ r1; \
r1 = r1 ^ r0; \
r1 = r1 | r2; \
r0 = r0 ^ r3; \
r1 = r1 ^ r4; \
r0 = r0 ^ r1;
#define I4(r0,r1,r2,r3,r4) \
r4 = r2; \
r2 = r2 & r3; \
r2 = r2 ^ r1; \
r1 = r1 | r3; \
r1 = r1 & r0; \
r4 = r4 ^ r2; \
r4 = r4 ^ r1; \
r1 = r1 & r2; \
r0 = r0 ^ -1; \
r3 = r3 ^ r4; \
r1 = r1 ^ r3; \
r3 = r3 & r0; \
r3 = r3 ^ r2; \
r0 = r0 ^ r1; \
r2 = r2 & r0; \
r3 = r3 ^ r0; \
r2 = r2 ^ r4; \
r2 = r2 | r3; \
r3 = r3 ^ r0; \
r2 = r2 ^ r1;
#define I5(r0,r1,r2,r3,r4) \
r1 = r1 ^ -1; \
r4 = r3; \
r2 = r2 ^ r1; \
r3 = r3 | r0; \
r3 = r3 ^ r2; \
r2 = r2 | r1; \
r2 = r2 & r0; \
r4 = r4 ^ r3; \
r2 = r2 ^ r4; \
r4 = r4 | r0; \
r4 = r4 ^ r1; \
r1 = r1 & r2; \
r1 = r1 ^ r3; \
r4 = r4 ^ r2; \
r3 = r3 & r4; \
r4 = r4 ^ r1; \
r3 = r3 ^ r0; \
r3 = r3 ^ r4; \
r4 = r4 ^ -1;
#define I6(r0,r1,r2,r3,r4) \
r0 = r0 ^ r2; \
r4 = r2; \
r2 = r2 & r0; \
r4 = r4 ^ r3; \
r2 = r2 ^ -1; \
r3 = r3 ^ r1; \
r2 = r2 ^ r3; \
r4 = r4 | r0; \
r0 = r0 ^ r2; \
r3 = r3 ^ r4; \
r4 = r4 ^ r1; \
r1 = r1 & r3; \
r1 = r1 ^ r0; \
r0 = r0 ^ r3; \
r0 = r0 | r2; \
r3 = r3 ^ r1; \
r4 = r4 ^ r0;
#define I7(r0,r1,r2,r3,r4) \
r4 = r2; \
r2 = r2 ^ r0; \
r0 = r0 & r3; \
r4 = r4 | r3; \
r2 = r2 ^ -1; \
r3 = r3 ^ r1; \
r1 = r1 | r0; \
r0 = r0 ^ r2; \
r2 = r2 & r4; \
r3 = r3 & r4; \
r1 = r1 ^ r2; \
r2 = r2 ^ r0; \
r0 = r0 | r2; \
r4 = r4 ^ r1; \
r0 = r0 ^ r3; \
r3 = r3 ^ r4; \
r4 = r4 | r0; \
r3 = r3 ^ r2; \
r4 = r4 ^ r2;
/* forward and inverse linear transformations */
#define LINTRANS(r0,r1,r2,r3,r4) \
r0 = rotl(r0, 13); \
r2 = rotl(r2, 3); \
r3 = r3 ^ r2; \
r4 = r0 << 3; \
r1 = r1 ^ r0; \
r3 = r3 ^ r4; \
r1 = r1 ^ r2; \
r3 = rotl(r3, 7); \
r1 = rotl(r1, 1); \
r2 = r2 ^ r3; \
r4 = r1 << 7; \
r0 = r0 ^ r1; \
r2 = r2 ^ r4; \
r0 = r0 ^ r3; \
r2 = rotl(r2, 22); \
r0 = rotl(r0, 5);
#define ILINTRANS(r0,r1,r2,r3,r4) \
r2 = rotr(r2, 22); \
r0 = rotr(r0, 5); \
r2 = r2 ^ r3; \
r4 = r1 << 7; \
r0 = r0 ^ r1; \
r2 = r2 ^ r4; \
r0 = r0 ^ r3; \
r3 = rotr(r3, 7); \
r1 = rotr(r1, 1); \
r3 = r3 ^ r2; \
r4 = r0 << 3; \
r1 = r1 ^ r0; \
r3 = r3 ^ r4; \
r1 = r1 ^ r2; \
r2 = rotr(r2, 3); \
r0 = rotr(r0, 13);
#define KEYMIX(r0,r1,r2,r3,r4,IN) \
r0 = r0 ^ l_key[IN+8]; \
r1 = r1 ^ l_key[IN+9]; \
r2 = r2 ^ l_key[IN+10]; \
r3 = r3 ^ l_key[IN+11];
#define GETKEY(r0, r1, r2, r3, IN) \
r0 = l_key[IN+8]; \
r1 = l_key[IN+9]; \
r2 = l_key[IN+10]; \
r3 = l_key[IN+11];
#define SETKEY(r0, r1, r2, r3, IN) \
l_key[IN+8] = r0; \
l_key[IN+9] = r1; \
l_key[IN+10] = r2; \
l_key[IN+11] = r3;
/* initialise the key schedule from the user supplied key */
int serpent_set_key(serpent_context *cx, const unsigned char *key, int key_len)
{ const u32 *in_key = (const u32 *)key;
/* l_key - storage for the key schedule */
u32 *l_key = cx->keyinfo;
u32 i,lk,r0,r1,r2,r3,r4;
if (key_len != 16 && key_len != 24 && key_len != 32)
return -1; /* unsupported key length */
key_len *= 8;
i = 0; lk = (key_len + 31) / 32;
while(i < lk)
{
#ifdef BLOCK_SWAP
l_key[i] = io_swap(in_key[lk - i - 1]);
#else
l_key[i] = in_key[i];
#endif
i++;
}
if (key_len < 256)
{
while(i < 8)
l_key[i++] = 0;
i = key_len / 32; lk = 1 << key_len % 32;
l_key[i] &= lk - 1;
l_key[i] |= lk;
}
for(i = 0; i < 132; ++i)
{
lk = l_key[i] ^ l_key[i + 3] ^ l_key[i + 5]
^ l_key[i + 7] ^ 0x9e3779b9 ^ i;
l_key[i + 8] = (lk << 11) | (lk >> 21);
}
GETKEY(r0, r1, r2, r3, 0);
S3(r0,r1,r2,r3,r4);
SETKEY(r1, r2, r3, r4, 0)
GETKEY(r0, r1, r2, r3, 4);
S2(r0,r1,r2,r3,r4);
SETKEY(r2, r3, r1, r4, 4)
GETKEY(r0, r1, r2, r3, 8);
S1(r0,r1,r2,r3,r4);
SETKEY(r3, r1, r2, r0, 8)
GETKEY(r0, r1, r2, r3, 12);
S0(r0,r1,r2,r3,r4);
SETKEY(r1, r4, r2, r0, 12)
GETKEY(r0, r1, r2, r3, 16);
S7(r0,r1,r2,r3,r4);
SETKEY(r2, r4, r3, r0, 16)
GETKEY(r0, r1, r2, r3, 20);
S6(r0,r1,r2,r3,r4)
SETKEY(r0, r1, r4, r2, 20)
GETKEY(r0, r1, r2, r3, 24);
S5(r0,r1,r2,r3,r4);
SETKEY(r1, r3, r0, r2, 24)
GETKEY(r0, r1, r2, r3, 28);
S4(r0,r1,r2,r3,r4)
SETKEY(r1, r4, r0, r3, 28)
GETKEY(r0, r1, r2, r3, 32);
S3(r0,r1,r2,r3,r4);
SETKEY(r1, r2, r3, r4, 32)
GETKEY(r0, r1, r2, r3, 36);
S2(r0,r1,r2,r3,r4);
SETKEY(r2, r3, r1, r4, 36)
GETKEY(r0, r1, r2, r3, 40);
S1(r0,r1,r2,r3,r4);
SETKEY(r3, r1, r2, r0, 40)
GETKEY(r0, r1, r2, r3, 44);
S0(r0,r1,r2,r3,r4);
SETKEY(r1, r4, r2, r0, 44)
GETKEY(r0, r1, r2, r3, 48);
S7(r0,r1,r2,r3,r4);
SETKEY(r2, r4, r3, r0, 48)
GETKEY(r0, r1, r2, r3, 52);
S6(r0,r1,r2,r3,r4)
SETKEY(r0, r1, r4, r2, 52)
GETKEY(r0, r1, r2, r3, 56);
S5(r0,r1,r2,r3,r4);
SETKEY(r1, r3, r0, r2, 56)
GETKEY(r0, r1, r2, r3, 60);
S4(r0,r1,r2,r3,r4)
SETKEY(r1, r4, r0, r3, 60)
GETKEY(r0, r1, r2, r3, 64);
S3(r0,r1,r2,r3,r4);
SETKEY(r1, r2, r3, r4, 64)
GETKEY(r0, r1, r2, r3, 68);
S2(r0,r1,r2,r3,r4);
SETKEY(r2, r3, r1, r4, 68)
GETKEY(r0, r1, r2, r3, 72);
S1(r0,r1,r2,r3,r4);
SETKEY(r3, r1, r2, r0, 72)
GETKEY(r0, r1, r2, r3, 76);
S0(r0,r1,r2,r3,r4);
SETKEY(r1, r4, r2, r0, 76)
GETKEY(r0, r1, r2, r3, 80);
S7(r0,r1,r2,r3,r4);
SETKEY(r2, r4, r3, r0, 80)
GETKEY(r0, r1, r2, r3, 84);
S6(r0,r1,r2,r3,r4)
SETKEY(r0, r1, r4, r2, 84)
GETKEY(r0, r1, r2, r3, 88);
S5(r0,r1,r2,r3,r4);
SETKEY(r1, r3, r0, r2, 88)
GETKEY(r0, r1, r2, r3, 92);
S4(r0,r1,r2,r3,r4)
SETKEY(r1, r4, r0, r3, 92)
GETKEY(r0, r1, r2, r3, 96);
S3(r0,r1,r2,r3,r4);
SETKEY(r1, r2, r3, r4, 96)
GETKEY(r0, r1, r2, r3, 100);
S2(r0,r1,r2,r3,r4);
SETKEY(r2, r3, r1, r4, 100)
GETKEY(r0, r1, r2, r3, 104);
S1(r0,r1,r2,r3,r4);
SETKEY(r3, r1, r2, r0, 104)
GETKEY(r0, r1, r2, r3, 108);
S0(r0,r1,r2,r3,r4);
SETKEY(r1, r4, r2, r0, 108)
GETKEY(r0, r1, r2, r3, 112);
S7(r0,r1,r2,r3,r4);
SETKEY(r2, r4, r3, r0, 112)
GETKEY(r0, r1, r2, r3, 116);
S6(r0,r1,r2,r3,r4)
SETKEY(r0, r1, r4, r2, 116)
GETKEY(r0, r1, r2, r3, 120);
S5(r0,r1,r2,r3,r4);
SETKEY(r1, r3, r0, r2, 120)
GETKEY(r0, r1, r2, r3, 124);
S4(r0,r1,r2,r3,r4)
SETKEY(r1, r4, r0, r3, 124)
GETKEY(r0, r1, r2, r3, 128);
S3(r0,r1,r2,r3,r4);
SETKEY(r1, r2, r3, r4, 128)
return 0;
};
/* Encryption and decryption functions. The rounds are fully inlined.
* The sboxes alters the bit order of the output, and the altered
* bit ordrer is used progressivly. */
/* encrypt a block of text */
int serpent_encrypt(serpent_context *cx, const u8 *in,
u8 *out)
{ u32 *l_key = cx->keyinfo;
const u32 *in_blk = (const u32 *) in;
u32 *out_blk = (u32 *) out;
u32 r0,r1,r2,r3,r4;
#ifdef BLOCK_SWAP
r0 = io_swap(in_blk[3]); r1 = io_swap(in_blk[2]);
r2 = io_swap(in_blk[1]); r3 = io_swap(in_blk[0]);
#else
r0 = in_blk[0]; r1 = in_blk[1]; r2 = in_blk[2]; r3 = in_blk[3];
#endif
/* round 1 */
KEYMIX(r0,r1,r2,r3,r4,0);
S0(r0,r1,r2,r3,r4);
LINTRANS(r1,r4,r2,r0,r3);
/* round 2 */
KEYMIX(r1,r4,r2,r0,r3,4);
S1(r1,r4,r2,r0,r3);
LINTRANS(r0,r4,r2,r1,r3);
/* round 3 */
KEYMIX(r0,r4,r2,r1,r3,8);
S2(r0,r4,r2,r1,r3);
LINTRANS(r2,r1,r4,r3,r0);
/* round 4 */
KEYMIX(r2,r1,r4,r3,r0,12);
S3(r2,r1,r4,r3,r0);
LINTRANS(r1,r4,r3,r0,r2);
/* round 5 */
KEYMIX(r1,r4,r3,r0,r2,16);
S4(r1,r4,r3,r0,r2)
LINTRANS(r4,r2,r1,r0,r3);
/* round 6 */
KEYMIX(r4,r2,r1,r0,r3,20);
S5(r4,r2,r1,r0,r3);
LINTRANS(r2,r0,r4,r1,r3);
/* round 7 */
KEYMIX(r2,r0,r4,r1,r3,24);
S6(r2,r0,r4,r1,r3)
LINTRANS(r2,r0,r3,r4,r1);
/* round 8 */
KEYMIX(r2,r0,r3,r4,r1,28);
S7(r2,r0,r3,r4,r1);
LINTRANS(r3,r1,r4,r2,r0);
/* round 9 */
KEYMIX(r3,r1,r4,r2,r0,32);
S0(r3,r1,r4,r2,r0);
LINTRANS(r1,r0,r4,r3,r2);
/* round 10 */
KEYMIX(r1,r0,r4,r3,r2,36);
S1(r1,r0,r4,r3,r2);
LINTRANS(r3,r0,r4,r1,r2);
/* round 11 */
KEYMIX(r3,r0,r4,r1,r2,40);
S2(r3,r0,r4,r1,r2);
LINTRANS(r4,r1,r0,r2,r3);
/* round 12 */
KEYMIX(r4,r1,r0,r2,r3,44);
S3(r4,r1,r0,r2,r3);
LINTRANS(r1,r0,r2,r3,r4);
/* round 13 */
KEYMIX(r1,r0,r2,r3,r4,48);
S4(r1,r0,r2,r3,r4)
LINTRANS(r0,r4,r1,r3,r2);
/* round 14 */
KEYMIX(r0,r4,r1,r3,r2,52);
S5(r0,r4,r1,r3,r2);
LINTRANS(r4,r3,r0,r1,r2);
/* round 15 */
KEYMIX(r4,r3,r0,r1,r2,56);
S6(r4,r3,r0,r1,r2)
LINTRANS(r4,r3,r2,r0,r1);
/* round 16 */
KEYMIX(r4,r3,r2,r0,r1,60);
S7(r4,r3,r2,r0,r1);
LINTRANS(r2,r1,r0,r4,r3);
/* round 17 */
KEYMIX(r2,r1,r0,r4,r3,64);
S0(r2,r1,r0,r4,r3);
LINTRANS(r1,r3,r0,r2,r4);
/* round 18 */
KEYMIX(r1,r3,r0,r2,r4,68);
S1(r1,r3,r0,r2,r4);
LINTRANS(r2,r3,r0,r1,r4);
/* round 19 */
KEYMIX(r2,r3,r0,r1,r4,72);
S2(r2,r3,r0,r1,r4);
LINTRANS(r0,r1,r3,r4,r2);
/* round 20 */
KEYMIX(r0,r1,r3,r4,r2,76);
S3(r0,r1,r3,r4,r2);
LINTRANS(r1,r3,r4,r2,r0);
/* round 21 */
KEYMIX(r1,r3,r4,r2,r0,80);
S4(r1,r3,r4,r2,r0)
LINTRANS(r3,r0,r1,r2,r4);
/* round 22 */
KEYMIX(r3,r0,r1,r2,r4,84);
S5(r3,r0,r1,r2,r4);
LINTRANS(r0,r2,r3,r1,r4);
/* round 23 */
KEYMIX(r0,r2,r3,r1,r4,88);
S6(r0,r2,r3,r1,r4)
LINTRANS(r0,r2,r4,r3,r1);
/* round 24 */
KEYMIX(r0,r2,r4,r3,r1,92);
S7(r0,r2,r4,r3,r1);
LINTRANS(r4,r1,r3,r0,r2);
/* round 25 */
KEYMIX(r4,r1,r3,r0,r2,96);
S0(r4,r1,r3,r0,r2);
LINTRANS(r1,r2,r3,r4,r0);
/* round 26 */
KEYMIX(r1,r2,r3,r4,r0,100);
S1(r1,r2,r3,r4,r0);
LINTRANS(r4,r2,r3,r1,r0);
/* round 27 */
KEYMIX(r4,r2,r3,r1,r0,104);
S2(r4,r2,r3,r1,r0);
LINTRANS(r3,r1,r2,r0,r4);
/* round 28 */
KEYMIX(r3,r1,r2,r0,r4,108);
S3(r3,r1,r2,r0,r4);
LINTRANS(r1,r2,r0,r4,r3);
/* round 29 */
KEYMIX(r1,r2,r0,r4,r3,112);
S4(r1,r2,r0,r4,r3)
LINTRANS(r2,r3,r1,r4,r0);
/* round 30 */
KEYMIX(r2,r3,r1,r4,r0,116);
S5(r2,r3,r1,r4,r0);
LINTRANS(r3,r4,r2,r1,r0);
/* round 31 */
KEYMIX(r3,r4,r2,r1,r0,120);
S6(r3,r4,r2,r1,r0)
LINTRANS(r3,r4,r0,r2,r1);
/* round 32 */
KEYMIX(r3,r4,r0,r2,r1,124);
S7(r3,r4,r0,r2,r1);
KEYMIX(r0,r1,r2,r3,r4,128);
#ifdef BLOCK_SWAP
out_blk[3] = io_swap(r0); out_blk[2] = io_swap(r1);
out_blk[1] = io_swap(r2); out_blk[0] = io_swap(r3);
#else
out_blk[0] = r0; out_blk[1] = r1; out_blk[2] = r2; out_blk[3] = r3;
#endif
return 0;
};
/* decrypt a block of text */
int serpent_decrypt(serpent_context *cx, const u8 *in,
u8 *out)
{ u32 *l_key = cx->keyinfo;
const u32 *in_blk = (const u32 *)in;
u32 *out_blk = (u32 *)out;
u32 r0,r1,r2,r3,r4;
#ifdef BLOCK_SWAP
r0 = io_swap(in_blk[3]); r1 = io_swap(in_blk[2]);
r2 = io_swap(in_blk[1]); r3 = io_swap(in_blk[0]);
#else
r0 = in_blk[0]; r1 = in_blk[1]; r2 = in_blk[2]; r3 = in_blk[3];
#endif
/* round 1 */
KEYMIX(r0,r1,r2,r3,r4,128);
I7(r0,r1,r2,r3,r4);
KEYMIX(r3,r0,r1,r4,r2,124);
/* round 2 */
ILINTRANS(r3,r0,r1,r4,r2);
I6(r3,r0,r1,r4,r2);
KEYMIX(r0,r1,r2,r4,r3,120);
/* round 3 */
ILINTRANS(r0,r1,r2,r4,r3);
I5(r0,r1,r2,r4,r3);
KEYMIX(r1,r3,r4,r2,r0,116);
/* round 4 */
ILINTRANS(r1,r3,r4,r2,r0);
I4(r1,r3,r4,r2,r0);
KEYMIX(r1,r2,r4,r0,r3,112);
/* round 5 */
ILINTRANS(r1,r2,r4,r0,r3);
I3(r1,r2,r4,r0,r3);
KEYMIX(r4,r2,r0,r1,r3,108);
/* round 6 */
ILINTRANS(r4,r2,r0,r1,r3);
I2(r4,r2,r0,r1,r3);
KEYMIX(r2,r3,r0,r1,r4,104);
/* round 7 */
ILINTRANS(r2,r3,r0,r1,r4);
I1(r2,r3,r0,r1,r4);
KEYMIX(r4,r2,r1,r0,r3,100);
/* round 8 */
ILINTRANS(r4,r2,r1,r0,r3);
I0(r4,r2,r1,r0,r3);
KEYMIX(r4,r3,r2,r0,r1,96);
/* round 9 */
ILINTRANS(r4,r3,r2,r0,r1);
I7(r4,r3,r2,r0,r1);
KEYMIX(r0,r4,r3,r1,r2,92);
/* round 10 */
ILINTRANS(r0,r4,r3,r1,r2);
I6(r0,r4,r3,r1,r2);
KEYMIX(r4,r3,r2,r1,r0,88);
/* round 11 */
ILINTRANS(r4,r3,r2,r1,r0);
I5(r4,r3,r2,r1,r0);
KEYMIX(r3,r0,r1,r2,r4,84);
/* round 12 */
ILINTRANS(r3,r0,r1,r2,r4);
I4(r3,r0,r1,r2,r4);
KEYMIX(r3,r2,r1,r4,r0,80);
/* round 13 */
ILINTRANS(r3,r2,r1,r4,r0);
I3(r3,r2,r1,r4,r0);
KEYMIX(r1,r2,r4,r3,r0,76);
/* round 14 */
ILINTRANS(r1,r2,r4,r3,r0);
I2(r1,r2,r4,r3,r0);
KEYMIX(r2,r0,r4,r3,r1,72);
/* round 15 */
ILINTRANS(r2,r0,r4,r3,r1);
I1(r2,r0,r4,r3,r1);
KEYMIX(r1,r2,r3,r4,r0,68);
/* round 16 */
ILINTRANS(r1,r2,r3,r4,r0);
I0(r1,r2,r3,r4,r0);
KEYMIX(r1,r0,r2,r4,r3,64);
/* round 17 */
ILINTRANS(r1,r0,r2,r4,r3);
I7(r1,r0,r2,r4,r3);
KEYMIX(r4,r1,r0,r3,r2,60);
/* round 18 */
ILINTRANS(r4,r1,r0,r3,r2);
I6(r4,r1,r0,r3,r2);
KEYMIX(r1,r0,r2,r3,r4,56);
/* round 19 */
ILINTRANS(r1,r0,r2,r3,r4);
I5(r1,r0,r2,r3,r4);
KEYMIX(r0,r4,r3,r2,r1,52);
/* round 20 */
ILINTRANS(r0,r4,r3,r2,r1);
I4(r0,r4,r3,r2,r1);
KEYMIX(r0,r2,r3,r1,r4,48);
/* round 21 */
ILINTRANS(r0,r2,r3,r1,r4);
I3(r0,r2,r3,r1,r4);
KEYMIX(r3,r2,r1,r0,r4,44);
/* round 22 */
ILINTRANS(r3,r2,r1,r0,r4);
I2(r3,r2,r1,r0,r4);
KEYMIX(r2,r4,r1,r0,r3,40);
/* round 23 */
ILINTRANS(r2,r4,r1,r0,r3);
I1(r2,r4,r1,r0,r3);
KEYMIX(r3,r2,r0,r1,r4,36);
/* round 24 */
ILINTRANS(r3,r2,r0,r1,r4);
I0(r3,r2,r0,r1,r4);
KEYMIX(r3,r4,r2,r1,r0,32);
/* round 25 */
ILINTRANS(r3,r4,r2,r1,r0);
I7(r3,r4,r2,r1,r0);
KEYMIX(r1,r3,r4,r0,r2,28);
/* round 26 */
ILINTRANS(r1,r3,r4,r0,r2);
I6(r1,r3,r4,r0,r2);
KEYMIX(r3,r4,r2,r0,r1,24);
/* round 27 */
ILINTRANS(r3,r4,r2,r0,r1);
I5(r3,r4,r2,r0,r1);
KEYMIX(r4,r1,r0,r2,r3,20);
/* round 28 */
ILINTRANS(r4,r1,r0,r2,r3);
I4(r4,r1,r0,r2,r3);
KEYMIX(r4,r2,r0,r3,r1,16);
/* round 29 */
ILINTRANS(r4,r2,r0,r3,r1);
I3(r4,r2,r0,r3,r1);
KEYMIX(r0,r2,r3,r4,r1,12);
/* round 30 */
ILINTRANS(r0,r2,r3,r4,r1);
I2(r0,r2,r3,r4,r1);
KEYMIX(r2,r1,r3,r4,r0,8);
/* round 31 */
ILINTRANS(r2,r1,r3,r4,r0);
I1(r2,r1,r3,r4,r0);
KEYMIX(r0,r2,r4,r3,r1,4);
/* round 32 */
ILINTRANS(r0,r2,r4,r3,r1);
I0(r0,r2,r4,r3,r1);
KEYMIX(r0,r1,r2,r3,r4,0);
#ifdef BLOCK_SWAP
out_blk[3] = io_swap(r0); out_blk[2] = io_swap(r1);
out_blk[1] = io_swap(r2); out_blk[0] = io_swap(r3);
#else
out_blk[0] = r0; out_blk[1] = r1; out_blk[2] = r2; out_blk[3] = r3;
#endif
return 0;
};
+17
View File
@@ -0,0 +1,17 @@
#ifndef SERPENT_H
#define SERPENT_H
#ifdef __KERNEL__
#include <linux/types.h>
#else
#include <sys/types.h>
#define u32 u_int32_t
#define u8 u_int8_t
#endif
struct serpent_context {
u32 keyinfo[140]; /* storage for the key schedule */
};
typedef struct serpent_context serpent_context;
int serpent_set_key(serpent_context *ctx, const u8 * in_key, int key_len);
int serpent_decrypt(serpent_context *ctx, const u8 * in_blk, u8 * out_blk);
int serpent_encrypt(serpent_context *ctx, const u8 * in_blk, u8 * out_blk);
#endif /* SERPENT_H */
+8
View File
@@ -0,0 +1,8 @@
#ifdef __KERNEL__
#include <linux/types.h>
#else
#include <sys/types.h>
#endif
#include "serpent_cbc.h"
#include "cbc_generic.h"
CBC_IMPL_BLK16(serpent_cbc_encrypt, serpent_context, u_int8_t *, serpent_encrypt, serpent_decrypt);
+3
View File
@@ -0,0 +1,3 @@
/* Glue header */
#include "serpent.h"
int serpent_cbc_encrypt(serpent_context *ctx, const u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t * iv, int encrypt);
+34
View File
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <string.h>
#include "serpent_cbc.h"
#define BLOCK_SIZE 16
#define KEY_SIZE 128 /* bits */
#define KEY "1234567890123456"
#define STR "hola guaso como estaisss ... 012"
#define STRSZ (sizeof(STR)-1)
#define BLKLEN BLOCK_SIZE
#define CONTEXT_T serpent_context
static int pretty_print(const unsigned char *buf, int count) {
int i=0;
for (;i<count;i++) printf ("%02hhx ", buf[i]);
putchar('\n');
return i;
}
//#define SIZE STRSZ/2
#define SIZE STRSZ
int main() {
int ret;
char buf0[SIZE+1], buf1[SIZE+1];
char IV[BLOCK_SIZE];
CONTEXT_T ac;
serpent_set_key(&ac, (void *)KEY, KEY_SIZE);
memset(buf0, 0, sizeof (buf0));
memset(buf1, 0, sizeof (buf1));
serpent_cbc_encrypt(&ac, STR, buf0, SIZE, IV, 1);
pretty_print(buf0, SIZE);
printf("size=%d ret=%d\n%s\n", SIZE, ret, buf0);
ret=serpent_cbc_encrypt(&ac, buf0, buf1, SIZE, IV, 0);
printf("size=%d ret=%d\n%s\n", SIZE, ret, buf1);
return 0;
}
+21
View File
@@ -0,0 +1,21 @@
CFLAGS=-O3 -fomit-frame-pointer -I../include $(EXTRA_CFLAGS)
LIBOBJ := hmac_sha2.o sha2.o
BLIB := libsha2.a
.S.o:
$(CC) $(AFLAGS) -c $< -o $@
$(BLIB): $(LIBOBJ)
/bin/rm -f $(BLIB)
ar cr $(BLIB) $(LIBOBJ)
-if test -s /bin/ranlib; then /bin/ranlib $(BLIB); \
else if test -s /usr/bin/ranlib; then /usr/bin/ranlib $(BLIB); \
else exit 0; fi; fi
test: test_main.o $(BLIB)
$(CC) -o $@ $^
clean:
rm -f *.[oa] core $(TARGET) test
+32
View File
@@ -0,0 +1,32 @@
#ifdef __KERNEL__
#include <linux/types.h>
#include <linux/string.h>
#else
#include <sys/types.h>
#include <string.h>
#endif
#include "hmac_generic.h"
#include "sha2.h"
#include "hmac_sha2.h"
void inline sha256_result(sha256_context *ctx, u_int8_t * hash, int hashlen) {
sha256_final(ctx);
memcpy(hash, &ctx->sha_out[0], hashlen);
}
void inline sha512_result(sha512_context *ctx, u_int8_t * hash, int hashlen) {
sha512_final(ctx);
memcpy(hash, &ctx->sha_out[0], hashlen);
}
HMAC_SET_KEY_IMPL (sha256_hmac_set_key,
sha256_hmac_context, SHA256_BLOCKSIZE,
sha256_init, sha256_write)
HMAC_HASH_IMPL (sha256_hmac_hash,
sha256_hmac_context, sha256_context, SHA256_HASHLEN,
sha256_write, sha256_result)
HMAC_SET_KEY_IMPL (sha512_hmac_set_key,
sha512_hmac_context, SHA512_BLOCKSIZE,
sha512_init, sha512_write)
HMAC_HASH_IMPL (sha512_hmac_hash,
sha512_hmac_context, sha512_context, SHA512_HASHLEN,
sha512_write, sha512_result)
+17
View File
@@ -0,0 +1,17 @@
typedef struct {
sha256_context ictx,octx;
} sha256_hmac_context;
typedef struct {
sha512_context ictx,octx;
} sha512_hmac_context;
#define SHA256_BLOCKSIZE 64
#define SHA256_HASHLEN 32
#define SHA384_BLOCKSIZE 128 /* XXX ok? */
#define SHA384_HASHLEN 48
#define SHA512_BLOCKSIZE 128
#define SHA512_HASHLEN 64
void sha256_hmac_hash(sha256_hmac_context *hctx, const u_int8_t * dat, int len, u_int8_t * hash, int hashlen);
void sha256_hmac_set_key(sha256_hmac_context *hctx, const u_int8_t * key, int keylen);
void sha512_hmac_hash(sha512_hmac_context *hctx, const u_int8_t * dat, int len, u_int8_t * hash, int hashlen);
void sha512_hmac_set_key(sha512_hmac_context *hctx, const u_int8_t * key, int keylen);
+437
View File
@@ -0,0 +1,437 @@
/*
* sha512.c
*
* Written by Jari Ruusu, April 16 2001
*
* Copyright 2001 by Jari Ruusu.
* Redistribution of this file is permitted under the GNU Public License.
*/
#ifdef __KERNEL__
#include <linux/string.h>
#include <linux/types.h>
#else
#include <string.h>
#include <sys/types.h>
#endif
#include "sha2.h"
/* Define one or more of these. If none is defined, you get all of them */
#if !defined(SHA256_NEEDED)&&!defined(SHA512_NEEDED)&&!defined(SHA384_NEEDED)
# define SHA256_NEEDED 1
# define SHA512_NEEDED 1
# define SHA384_NEEDED 1
#endif
#if defined(SHA256_NEEDED)
static const u_int32_t sha256_hashInit[8] = {
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c,
0x1f83d9ab, 0x5be0cd19
};
static const u_int32_t sha256_K[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
#endif
#if defined(SHA512_NEEDED)
static const u_int64_t sha512_hashInit[8] = {
0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL,
0xa54ff53a5f1d36f1ULL, 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
};
#endif
#if defined(SHA384_NEEDED)
static const u_int64_t sha384_hashInit[8] = {
0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL, 0x9159015a3070dd17ULL,
0x152fecd8f70e5939ULL, 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL,
0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL
};
#endif
#if defined(SHA512_NEEDED) || defined(SHA384_NEEDED)
static const u_int64_t sha512_K[80] = {
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL,
0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL,
0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL,
0xc19bf174cf692694ULL, 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL,
0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL,
0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 0x27b70a8546d22ffcULL,
0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL,
0x92722c851482353bULL, 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL,
0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL,
0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, 0x748f82ee5defb2fcULL,
0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL,
0xc67178f2e372532bULL, 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL,
0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL,
0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
};
#endif
#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
#define R(x,y) ((y) >> (x))
#if defined(SHA256_NEEDED)
void sha256_init(sha256_context *ctx)
{
memcpy(&ctx->sha_H[0], &sha256_hashInit[0], sizeof(ctx->sha_H));
ctx->sha_blocks = 0;
ctx->sha_bufCnt = 0;
}
#define S(x,y) (((y) >> (x)) | ((y) << (32 - (x))))
#define uSig0(x) ((S(2,(x))) ^ (S(13,(x))) ^ (S(22,(x))))
#define uSig1(x) ((S(6,(x))) ^ (S(11,(x))) ^ (S(25,(x))))
#define lSig0(x) ((S(7,(x))) ^ (S(18,(x))) ^ (R(3,(x))))
#define lSig1(x) ((S(17,(x))) ^ (S(19,(x))) ^ (R(10,(x))))
static void sha256_transform(sha256_context *ctx, const unsigned char *datap)
{
register int j;
u_int32_t a, b, c, d, e, f, g, h;
u_int32_t T1, T2, W[64], Wm2, Wm15;
/* read the data, big endian byte order */
j = 0;
do {
W[j] = (((u_int32_t)(datap[0]))<<24) | (((u_int32_t)(datap[1]))<<16) |
(((u_int32_t)(datap[2]))<<8 ) | ((u_int32_t)(datap[3]));
datap += 4;
} while(++j < 16);
/* initialize variables a...h */
a = ctx->sha_H[0];
b = ctx->sha_H[1];
c = ctx->sha_H[2];
d = ctx->sha_H[3];
e = ctx->sha_H[4];
f = ctx->sha_H[5];
g = ctx->sha_H[6];
h = ctx->sha_H[7];
/* apply compression function */
j = 0;
do {
if(j >= 16) {
Wm2 = W[j - 2];
Wm15 = W[j - 15];
W[j] = lSig1(Wm2) + W[j - 7] + lSig0(Wm15) + W[j - 16];
}
T1 = h + uSig1(e) + Ch(e,f,g) + sha256_K[j] + W[j];
T2 = uSig0(a) + Maj(a,b,c);
h = g; g = f; f = e;
e = d + T1;
d = c; c = b; b = a;
a = T1 + T2;
} while(++j < 64);
/* compute intermediate hash value */
ctx->sha_H[0] += a;
ctx->sha_H[1] += b;
ctx->sha_H[2] += c;
ctx->sha_H[3] += d;
ctx->sha_H[4] += e;
ctx->sha_H[5] += f;
ctx->sha_H[6] += g;
ctx->sha_H[7] += h;
ctx->sha_blocks++;
}
void sha256_write(sha256_context *ctx, const unsigned char *datap, int length)
{
while(length > 0) {
if(!ctx->sha_bufCnt) {
while(length >= sizeof(ctx->sha_out)) {
sha256_transform(ctx, datap);
datap += sizeof(ctx->sha_out);
length -= sizeof(ctx->sha_out);
}
if(!length) return;
}
ctx->sha_out[ctx->sha_bufCnt] = *datap++;
length--;
if(++ctx->sha_bufCnt == sizeof(ctx->sha_out)) {
sha256_transform(ctx, &ctx->sha_out[0]);
ctx->sha_bufCnt = 0;
}
}
}
void sha256_final(sha256_context *ctx)
{
register int j;
u_int64_t bitLength;
u_int32_t i;
unsigned char padByte, *datap;
bitLength = (ctx->sha_blocks << 9) | (ctx->sha_bufCnt << 3);
padByte = 0x80;
sha256_write(ctx, &padByte, 1);
/* pad extra space with zeroes */
padByte = 0;
while(ctx->sha_bufCnt != 56) {
sha256_write(ctx, &padByte, 1);
}
/* write bit length, big endian byte order */
ctx->sha_out[56] = bitLength >> 56;
ctx->sha_out[57] = bitLength >> 48;
ctx->sha_out[58] = bitLength >> 40;
ctx->sha_out[59] = bitLength >> 32;
ctx->sha_out[60] = bitLength >> 24;
ctx->sha_out[61] = bitLength >> 16;
ctx->sha_out[62] = bitLength >> 8;
ctx->sha_out[63] = bitLength;
sha256_transform(ctx, &ctx->sha_out[0]);
/* return results in ctx->sha_out[0...31] */
datap = &ctx->sha_out[0];
j = 0;
do {
i = ctx->sha_H[j];
datap[0] = i >> 24;
datap[1] = i >> 16;
datap[2] = i >> 8;
datap[3] = i;
datap += 4;
} while(++j < 8);
/* clear sensitive information */
memset(&ctx->sha_out[32], 0, sizeof(sha256_context) - 32);
}
void sha256_hash_buffer(unsigned char *ib, int ile, unsigned char *ob, int ole)
{
sha256_context ctx;
if(ole < 1) return;
memset(ob, 0, ole);
if(ole > 32) ole = 32;
sha256_init(&ctx);
sha256_write(&ctx, ib, ile);
sha256_final(&ctx);
memcpy(ob, &ctx.sha_out[0], ole);
memset(&ctx, 0, sizeof(ctx));
}
#endif
#if defined(SHA512_NEEDED)
void sha512_init(sha512_context *ctx)
{
memcpy(&ctx->sha_H[0], &sha512_hashInit[0], sizeof(ctx->sha_H));
ctx->sha_blocks = 0;
ctx->sha_blocksMSB = 0;
ctx->sha_bufCnt = 0;
}
#endif
#if defined(SHA512_NEEDED) || defined(SHA384_NEEDED)
#undef S
#undef uSig0
#undef uSig1
#undef lSig0
#undef lSig1
#define S(x,y) (((y) >> (x)) | ((y) << (64 - (x))))
#define uSig0(x) ((S(28,(x))) ^ (S(34,(x))) ^ (S(39,(x))))
#define uSig1(x) ((S(14,(x))) ^ (S(18,(x))) ^ (S(41,(x))))
#define lSig0(x) ((S(1,(x))) ^ (S(8,(x))) ^ (R(7,(x))))
#define lSig1(x) ((S(19,(x))) ^ (S(61,(x))) ^ (R(6,(x))))
static void sha512_transform(sha512_context *ctx, const unsigned char *datap)
{
register int j;
u_int64_t a, b, c, d, e, f, g, h;
u_int64_t T1, T2, W[80], Wm2, Wm15;
/* read the data, big endian byte order */
j = 0;
do {
W[j] = (((u_int64_t)(datap[0]))<<56) | (((u_int64_t)(datap[1]))<<48) |
(((u_int64_t)(datap[2]))<<40) | (((u_int64_t)(datap[3]))<<32) |
(((u_int64_t)(datap[4]))<<24) | (((u_int64_t)(datap[5]))<<16) |
(((u_int64_t)(datap[6]))<<8 ) | ((u_int64_t)(datap[7]));
datap += 8;
} while(++j < 16);
/* initialize variables a...h */
a = ctx->sha_H[0];
b = ctx->sha_H[1];
c = ctx->sha_H[2];
d = ctx->sha_H[3];
e = ctx->sha_H[4];
f = ctx->sha_H[5];
g = ctx->sha_H[6];
h = ctx->sha_H[7];
/* apply compression function */
j = 0;
do {
if(j >= 16) {
Wm2 = W[j - 2];
Wm15 = W[j - 15];
W[j] = lSig1(Wm2) + W[j - 7] + lSig0(Wm15) + W[j - 16];
}
T1 = h + uSig1(e) + Ch(e,f,g) + sha512_K[j] + W[j];
T2 = uSig0(a) + Maj(a,b,c);
h = g; g = f; f = e;
e = d + T1;
d = c; c = b; b = a;
a = T1 + T2;
} while(++j < 80);
/* compute intermediate hash value */
ctx->sha_H[0] += a;
ctx->sha_H[1] += b;
ctx->sha_H[2] += c;
ctx->sha_H[3] += d;
ctx->sha_H[4] += e;
ctx->sha_H[5] += f;
ctx->sha_H[6] += g;
ctx->sha_H[7] += h;
ctx->sha_blocks++;
if(!ctx->sha_blocks) ctx->sha_blocksMSB++;
}
void sha512_write(sha512_context *ctx, const unsigned char *datap, int length)
{
while(length > 0) {
if(!ctx->sha_bufCnt) {
while(length >= sizeof(ctx->sha_out)) {
sha512_transform(ctx, datap);
datap += sizeof(ctx->sha_out);
length -= sizeof(ctx->sha_out);
}
if(!length) return;
}
ctx->sha_out[ctx->sha_bufCnt] = *datap++;
length--;
if(++ctx->sha_bufCnt == sizeof(ctx->sha_out)) {
sha512_transform(ctx, &ctx->sha_out[0]);
ctx->sha_bufCnt = 0;
}
}
}
void sha512_final(sha512_context *ctx)
{
register int j;
u_int64_t bitLength, bitLengthMSB;
u_int64_t i;
unsigned char padByte, *datap;
bitLength = (ctx->sha_blocks << 10) | (ctx->sha_bufCnt << 3);
bitLengthMSB = (ctx->sha_blocksMSB << 10) | (ctx->sha_blocks >> 54);
padByte = 0x80;
sha512_write(ctx, &padByte, 1);
/* pad extra space with zeroes */
padByte = 0;
while(ctx->sha_bufCnt != 112) {
sha512_write(ctx, &padByte, 1);
}
/* write bit length, big endian byte order */
ctx->sha_out[112] = bitLengthMSB >> 56;
ctx->sha_out[113] = bitLengthMSB >> 48;
ctx->sha_out[114] = bitLengthMSB >> 40;
ctx->sha_out[115] = bitLengthMSB >> 32;
ctx->sha_out[116] = bitLengthMSB >> 24;
ctx->sha_out[117] = bitLengthMSB >> 16;
ctx->sha_out[118] = bitLengthMSB >> 8;
ctx->sha_out[119] = bitLengthMSB;
ctx->sha_out[120] = bitLength >> 56;
ctx->sha_out[121] = bitLength >> 48;
ctx->sha_out[122] = bitLength >> 40;
ctx->sha_out[123] = bitLength >> 32;
ctx->sha_out[124] = bitLength >> 24;
ctx->sha_out[125] = bitLength >> 16;
ctx->sha_out[126] = bitLength >> 8;
ctx->sha_out[127] = bitLength;
sha512_transform(ctx, &ctx->sha_out[0]);
/* return results in ctx->sha_out[0...63] */
datap = &ctx->sha_out[0];
j = 0;
do {
i = ctx->sha_H[j];
datap[0] = i >> 56;
datap[1] = i >> 48;
datap[2] = i >> 40;
datap[3] = i >> 32;
datap[4] = i >> 24;
datap[5] = i >> 16;
datap[6] = i >> 8;
datap[7] = i;
datap += 8;
} while(++j < 8);
/* clear sensitive information */
memset(&ctx->sha_out[64], 0, sizeof(sha512_context) - 64);
}
void sha512_hash_buffer(unsigned char *ib, int ile, unsigned char *ob, int ole)
{
sha512_context ctx;
if(ole < 1) return;
memset(ob, 0, ole);
if(ole > 64) ole = 64;
sha512_init(&ctx);
sha512_write(&ctx, ib, ile);
sha512_final(&ctx);
memcpy(ob, &ctx.sha_out[0], ole);
memset(&ctx, 0, sizeof(ctx));
}
#endif
#if defined(SHA384_NEEDED)
void sha384_init(sha512_context *ctx)
{
memcpy(&ctx->sha_H[0], &sha384_hashInit[0], sizeof(ctx->sha_H));
ctx->sha_blocks = 0;
ctx->sha_blocksMSB = 0;
ctx->sha_bufCnt = 0;
}
void sha384_hash_buffer(unsigned char *ib, int ile, unsigned char *ob, int ole)
{
sha512_context ctx;
if(ole < 1) return;
memset(ob, 0, ole);
if(ole > 48) ole = 48;
sha384_init(&ctx);
sha512_write(&ctx, ib, ile);
sha512_final(&ctx);
memcpy(ob, &ctx.sha_out[0], ole);
memset(&ctx, 0, sizeof(ctx));
}
#endif
+52
View File
@@ -0,0 +1,52 @@
#ifndef _SHA2_H
#define _SHA2_H
/*
* sha512.h
*
* Written by Jari Ruusu, April 16 2001
*
* Copyright 2001 by Jari Ruusu.
* Redistribution of this file is permitted under the GNU Public License.
*/
#ifdef __KERNEL__
#include <linux/types.h>
#else
#include <sys/types.h>
#endif
typedef struct {
unsigned char sha_out[64]; /* results are here, bytes 0...31 */
u_int32_t sha_H[8];
u_int64_t sha_blocks;
int sha_bufCnt;
} sha256_context;
typedef struct {
unsigned char sha_out[128]; /* results are here, bytes 0...63 */
u_int64_t sha_H[8];
u_int64_t sha_blocks;
u_int64_t sha_blocksMSB;
int sha_bufCnt;
} sha512_context;
/* no sha384_context, use sha512_context */
/* 256 bit hash, provides 128 bits of security against collision attacks */
extern void sha256_init(sha256_context *);
extern void sha256_write(sha256_context *, const unsigned char *, int);
extern void sha256_final(sha256_context *);
extern void sha256_hash_buffer(unsigned char *, int, unsigned char *, int);
/* 512 bit hash, provides 256 bits of security against collision attacks */
extern void sha512_init(sha512_context *);
extern void sha512_write(sha512_context *, const unsigned char *, int);
extern void sha512_final(sha512_context *);
extern void sha512_hash_buffer(unsigned char *, int, unsigned char *, int);
/* 384 bit hash, provides 192 bits of security against collision attacks */
extern void sha384_init(sha512_context *);
/* no sha384_write(), use sha512_write() */
/* no sha384_final(), use sha512_final(), result in ctx->sha_out[0...47] */
extern void sha384_hash_buffer(unsigned char *, int, unsigned char *, int);
#endif /* _SHA2_H */
+21
View File
@@ -0,0 +1,21 @@
CFLAGS=-O3 -fomit-frame-pointer -D__KERNEL__ -Wall $(EXTRA_CFLAGS)
INC=-I../include
LIBOBJ=twofish.o twofish_cbc.o
BLIB=libtwofish.a
.c.o:
$(CC) $(CPPFLAGS) $(CFLAGS) $(INC) -c $< -o $@
$(BLIB): $(LIBOBJ)
/bin/rm -f $(BLIB)
ar cr $(BLIB) $(LIBOBJ)
-if test -s /bin/ranlib; then /bin/ranlib $(BLIB); \
else if test -s /usr/bin/ranlib; then /usr/bin/ranlib $(BLIB); \
else exit 0; fi; fi
test: test_main.o $(BLIB)
$(CC) -o $@ $^
clean:
rm -f *.[oa] core $(TARGET) test
+34
View File
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <string.h>
#include "twofish_cbc.h"
#define BLOCK_SIZE 16
#define KEY_SIZE 128 /* bits */
#define KEY "1234567890123456"
#define STR "hola guaso como estaisss ... 012"
#define STRSZ (sizeof(STR)-1)
#define BLKLEN BLOCK_SIZE
#define CONTEXT_T twofish_context
static int pretty_print(const unsigned char *buf, int count) {
int i=0;
for (;i<count;i++) printf ("%02hhx ", buf[i]);
putchar('\n');
return i;
}
//#define SIZE STRSZ/2
#define SIZE STRSZ
int main() {
int ret;
char buf0[SIZE+1], buf1[SIZE+1];
char IV[BLOCK_SIZE];
CONTEXT_T ac;
twofish_set_key(&ac, (void *)KEY, KEY_SIZE);
memset(buf0, 0, sizeof (buf0));
memset(buf1, 0, sizeof (buf1));
twofish_cbc_encrypt(&ac, STR, buf0, SIZE, IV, 1);
pretty_print(buf0, SIZE);
printf("size=%d ret=%d\n%s\n", SIZE, ret, buf0);
ret=twofish_cbc_encrypt(&ac, buf0, buf1, SIZE, IV, 0);
printf("size=%d ret=%d\n%s\n", SIZE, ret, buf1);
return 0;
}
+861
View File
@@ -0,0 +1,861 @@
/* NOTE: This implementation has been changed from the original
* source. See ChangeLog for more information.
* Maintained by Marc Mutz <[email protected]>
*/
/* Twofish for GPG
* By Matthew Skala <[email protected]>, July 26, 1998
* 256-bit key length added March 20, 1999
* Some modifications to reduce the text size by Werner Koch, April, 1998
*
* The original author has disclaimed all copyright interest in this
* code and thus putting it in the public domain.
*
* This code is a "clean room" implementation, written from the paper
* _Twofish: A 128-Bit Block Cipher_ by Bruce Schneier, John Kelsey,
* Doug Whiting, David Wagner, Chris Hall, and Niels Ferguson, available
* through http://www.counterpane.com/twofish.html
*
* For background information on multiplication in finite fields, used for
* the matrix operations in the key schedule, see the book _Contemporary
* Abstract Algebra_ by Joseph A. Gallian, especially chapter 22 in the
* Third Edition.
*
* Only the 128- and 256-bit key sizes are supported. This code is intended
* for GNU C on a 32-bit system, but it should work almost anywhere. Loops
* are unrolled, precomputation tables are used, etc., for maximum speed at
* some cost in memory consumption. */
#ifdef __KERNEL__
#include <linux/init.h>
#include <linux/types.h>
#else
#include <sys/types.h>
#define u8 u_int8_t
#define u32 u_int32_t
#endif
#if 0 /* shouldn't this be #ifdef rotl32 ?
* Look at wordops.h: It includes asm/wordops.h.
* Anyway, we have to search in the macros for rot's,
* since they seem to be defined in a generic way. */
#define rotl rotl32
#define rotr rotr32
#else
#define rotl generic_rotl32
#define rotr generic_rotr32
#endif
#include "twofish.h"
/* The large precomputed tables for the Twofish cipher (twofish.c)
* Taken from the same source as twofish.c
* Marc Mutz <[email protected]>
*/
/* These two tables are the q0 and q1 permutations, exactly as described in
* the Twofish paper. */
static const u8 q0[256] = {
0xA9, 0x67, 0xB3, 0xE8, 0x04, 0xFD, 0xA3, 0x76, 0x9A, 0x92, 0x80, 0x78,
0xE4, 0xDD, 0xD1, 0x38, 0x0D, 0xC6, 0x35, 0x98, 0x18, 0xF7, 0xEC, 0x6C,
0x43, 0x75, 0x37, 0x26, 0xFA, 0x13, 0x94, 0x48, 0xF2, 0xD0, 0x8B, 0x30,
0x84, 0x54, 0xDF, 0x23, 0x19, 0x5B, 0x3D, 0x59, 0xF3, 0xAE, 0xA2, 0x82,
0x63, 0x01, 0x83, 0x2E, 0xD9, 0x51, 0x9B, 0x7C, 0xA6, 0xEB, 0xA5, 0xBE,
0x16, 0x0C, 0xE3, 0x61, 0xC0, 0x8C, 0x3A, 0xF5, 0x73, 0x2C, 0x25, 0x0B,
0xBB, 0x4E, 0x89, 0x6B, 0x53, 0x6A, 0xB4, 0xF1, 0xE1, 0xE6, 0xBD, 0x45,
0xE2, 0xF4, 0xB6, 0x66, 0xCC, 0x95, 0x03, 0x56, 0xD4, 0x1C, 0x1E, 0xD7,
0xFB, 0xC3, 0x8E, 0xB5, 0xE9, 0xCF, 0xBF, 0xBA, 0xEA, 0x77, 0x39, 0xAF,
0x33, 0xC9, 0x62, 0x71, 0x81, 0x79, 0x09, 0xAD, 0x24, 0xCD, 0xF9, 0xD8,
0xE5, 0xC5, 0xB9, 0x4D, 0x44, 0x08, 0x86, 0xE7, 0xA1, 0x1D, 0xAA, 0xED,
0x06, 0x70, 0xB2, 0xD2, 0x41, 0x7B, 0xA0, 0x11, 0x31, 0xC2, 0x27, 0x90,
0x20, 0xF6, 0x60, 0xFF, 0x96, 0x5C, 0xB1, 0xAB, 0x9E, 0x9C, 0x52, 0x1B,
0x5F, 0x93, 0x0A, 0xEF, 0x91, 0x85, 0x49, 0xEE, 0x2D, 0x4F, 0x8F, 0x3B,
0x47, 0x87, 0x6D, 0x46, 0xD6, 0x3E, 0x69, 0x64, 0x2A, 0xCE, 0xCB, 0x2F,
0xFC, 0x97, 0x05, 0x7A, 0xAC, 0x7F, 0xD5, 0x1A, 0x4B, 0x0E, 0xA7, 0x5A,
0x28, 0x14, 0x3F, 0x29, 0x88, 0x3C, 0x4C, 0x02, 0xB8, 0xDA, 0xB0, 0x17,
0x55, 0x1F, 0x8A, 0x7D, 0x57, 0xC7, 0x8D, 0x74, 0xB7, 0xC4, 0x9F, 0x72,
0x7E, 0x15, 0x22, 0x12, 0x58, 0x07, 0x99, 0x34, 0x6E, 0x50, 0xDE, 0x68,
0x65, 0xBC, 0xDB, 0xF8, 0xC8, 0xA8, 0x2B, 0x40, 0xDC, 0xFE, 0x32, 0xA4,
0xCA, 0x10, 0x21, 0xF0, 0xD3, 0x5D, 0x0F, 0x00, 0x6F, 0x9D, 0x36, 0x42,
0x4A, 0x5E, 0xC1, 0xE0
};
static const u8 q1[256] = {
0x75, 0xF3, 0xC6, 0xF4, 0xDB, 0x7B, 0xFB, 0xC8, 0x4A, 0xD3, 0xE6, 0x6B,
0x45, 0x7D, 0xE8, 0x4B, 0xD6, 0x32, 0xD8, 0xFD, 0x37, 0x71, 0xF1, 0xE1,
0x30, 0x0F, 0xF8, 0x1B, 0x87, 0xFA, 0x06, 0x3F, 0x5E, 0xBA, 0xAE, 0x5B,
0x8A, 0x00, 0xBC, 0x9D, 0x6D, 0xC1, 0xB1, 0x0E, 0x80, 0x5D, 0xD2, 0xD5,
0xA0, 0x84, 0x07, 0x14, 0xB5, 0x90, 0x2C, 0xA3, 0xB2, 0x73, 0x4C, 0x54,
0x92, 0x74, 0x36, 0x51, 0x38, 0xB0, 0xBD, 0x5A, 0xFC, 0x60, 0x62, 0x96,
0x6C, 0x42, 0xF7, 0x10, 0x7C, 0x28, 0x27, 0x8C, 0x13, 0x95, 0x9C, 0xC7,
0x24, 0x46, 0x3B, 0x70, 0xCA, 0xE3, 0x85, 0xCB, 0x11, 0xD0, 0x93, 0xB8,
0xA6, 0x83, 0x20, 0xFF, 0x9F, 0x77, 0xC3, 0xCC, 0x03, 0x6F, 0x08, 0xBF,
0x40, 0xE7, 0x2B, 0xE2, 0x79, 0x0C, 0xAA, 0x82, 0x41, 0x3A, 0xEA, 0xB9,
0xE4, 0x9A, 0xA4, 0x97, 0x7E, 0xDA, 0x7A, 0x17, 0x66, 0x94, 0xA1, 0x1D,
0x3D, 0xF0, 0xDE, 0xB3, 0x0B, 0x72, 0xA7, 0x1C, 0xEF, 0xD1, 0x53, 0x3E,
0x8F, 0x33, 0x26, 0x5F, 0xEC, 0x76, 0x2A, 0x49, 0x81, 0x88, 0xEE, 0x21,
0xC4, 0x1A, 0xEB, 0xD9, 0xC5, 0x39, 0x99, 0xCD, 0xAD, 0x31, 0x8B, 0x01,
0x18, 0x23, 0xDD, 0x1F, 0x4E, 0x2D, 0xF9, 0x48, 0x4F, 0xF2, 0x65, 0x8E,
0x78, 0x5C, 0x58, 0x19, 0x8D, 0xE5, 0x98, 0x57, 0x67, 0x7F, 0x05, 0x64,
0xAF, 0x63, 0xB6, 0xFE, 0xF5, 0xB7, 0x3C, 0xA5, 0xCE, 0xE9, 0x68, 0x44,
0xE0, 0x4D, 0x43, 0x69, 0x29, 0x2E, 0xAC, 0x15, 0x59, 0xA8, 0x0A, 0x9E,
0x6E, 0x47, 0xDF, 0x34, 0x35, 0x6A, 0xCF, 0xDC, 0x22, 0xC9, 0xC0, 0x9B,
0x89, 0xD4, 0xED, 0xAB, 0x12, 0xA2, 0x0D, 0x52, 0xBB, 0x02, 0x2F, 0xA9,
0xD7, 0x61, 0x1E, 0xB4, 0x50, 0x04, 0xF6, 0xC2, 0x16, 0x25, 0x86, 0x56,
0x55, 0x09, 0xBE, 0x91
};
/* These MDS tables are actually tables of MDS composed with q0 and q1,
* because it is only ever used that way and we can save some time by
* precomputing. Of course the main saving comes from precomputing the
* GF(2^8) multiplication involved in the MDS matrix multiply; by looking
* things up in these tables we reduce the matrix multiply to four lookups
* and three XORs. Semi-formally, the definition of these tables is:
* mds[0][i] = MDS (q1[i] 0 0 0)^T mds[1][i] = MDS (0 q0[i] 0 0)^T
* mds[2][i] = MDS (0 0 q1[i] 0)^T mds[3][i] = MDS (0 0 0 q0[i])^T
* where ^T means "transpose", the matrix multiply is performed in GF(2^8)
* represented as GF(2)[x]/v(x) where v(x)=x^8+x^6+x^5+x^3+1 as described
* by Schneier et al, and I'm casually glossing over the byte/word
* conversion issues. */
static const u32 mds[4][256] = {
{0xBCBC3275, 0xECEC21F3, 0x202043C6, 0xB3B3C9F4, 0xDADA03DB, 0x02028B7B,
0xE2E22BFB, 0x9E9EFAC8, 0xC9C9EC4A, 0xD4D409D3, 0x18186BE6, 0x1E1E9F6B,
0x98980E45, 0xB2B2387D, 0xA6A6D2E8, 0x2626B74B, 0x3C3C57D6, 0x93938A32,
0x8282EED8, 0x525298FD, 0x7B7BD437, 0xBBBB3771, 0x5B5B97F1, 0x474783E1,
0x24243C30, 0x5151E20F, 0xBABAC6F8, 0x4A4AF31B, 0xBFBF4887, 0x0D0D70FA,
0xB0B0B306, 0x7575DE3F, 0xD2D2FD5E, 0x7D7D20BA, 0x666631AE, 0x3A3AA35B,
0x59591C8A, 0x00000000, 0xCDCD93BC, 0x1A1AE09D, 0xAEAE2C6D, 0x7F7FABC1,
0x2B2BC7B1, 0xBEBEB90E, 0xE0E0A080, 0x8A8A105D, 0x3B3B52D2, 0x6464BAD5,
0xD8D888A0, 0xE7E7A584, 0x5F5FE807, 0x1B1B1114, 0x2C2CC2B5, 0xFCFCB490,
0x3131272C, 0x808065A3, 0x73732AB2, 0x0C0C8173, 0x79795F4C, 0x6B6B4154,
0x4B4B0292, 0x53536974, 0x94948F36, 0x83831F51, 0x2A2A3638, 0xC4C49CB0,
0x2222C8BD, 0xD5D5F85A, 0xBDBDC3FC, 0x48487860, 0xFFFFCE62, 0x4C4C0796,
0x4141776C, 0xC7C7E642, 0xEBEB24F7, 0x1C1C1410, 0x5D5D637C, 0x36362228,
0x6767C027, 0xE9E9AF8C, 0x4444F913, 0x1414EA95, 0xF5F5BB9C, 0xCFCF18C7,
0x3F3F2D24, 0xC0C0E346, 0x7272DB3B, 0x54546C70, 0x29294CCA, 0xF0F035E3,
0x0808FE85, 0xC6C617CB, 0xF3F34F11, 0x8C8CE4D0, 0xA4A45993, 0xCACA96B8,
0x68683BA6, 0xB8B84D83, 0x38382820, 0xE5E52EFF, 0xADAD569F, 0x0B0B8477,
0xC8C81DC3, 0x9999FFCC, 0x5858ED03, 0x19199A6F, 0x0E0E0A08, 0x95957EBF,
0x70705040, 0xF7F730E7, 0x6E6ECF2B, 0x1F1F6EE2, 0xB5B53D79, 0x09090F0C,
0x616134AA, 0x57571682, 0x9F9F0B41, 0x9D9D803A, 0x111164EA, 0x2525CDB9,
0xAFAFDDE4, 0x4545089A, 0xDFDF8DA4, 0xA3A35C97, 0xEAEAD57E, 0x353558DA,
0xEDEDD07A, 0x4343FC17, 0xF8F8CB66, 0xFBFBB194, 0x3737D3A1, 0xFAFA401D,
0xC2C2683D, 0xB4B4CCF0, 0x32325DDE, 0x9C9C71B3, 0x5656E70B, 0xE3E3DA72,
0x878760A7, 0x15151B1C, 0xF9F93AEF, 0x6363BFD1, 0x3434A953, 0x9A9A853E,
0xB1B1428F, 0x7C7CD133, 0x88889B26, 0x3D3DA65F, 0xA1A1D7EC, 0xE4E4DF76,
0x8181942A, 0x91910149, 0x0F0FFB81, 0xEEEEAA88, 0x161661EE, 0xD7D77321,
0x9797F5C4, 0xA5A5A81A, 0xFEFE3FEB, 0x6D6DB5D9, 0x7878AEC5, 0xC5C56D39,
0x1D1DE599, 0x7676A4CD, 0x3E3EDCAD, 0xCBCB6731, 0xB6B6478B, 0xEFEF5B01,
0x12121E18, 0x6060C523, 0x6A6AB0DD, 0x4D4DF61F, 0xCECEE94E, 0xDEDE7C2D,
0x55559DF9, 0x7E7E5A48, 0x2121B24F, 0x03037AF2, 0xA0A02665, 0x5E5E198E,
0x5A5A6678, 0x65654B5C, 0x62624E58, 0xFDFD4519, 0x0606F48D, 0x404086E5,
0xF2F2BE98, 0x3333AC57, 0x17179067, 0x05058E7F, 0xE8E85E05, 0x4F4F7D64,
0x89896AAF, 0x10109563, 0x74742FB6, 0x0A0A75FE, 0x5C5C92F5, 0x9B9B74B7,
0x2D2D333C, 0x3030D6A5, 0x2E2E49CE, 0x494989E9, 0x46467268, 0x77775544,
0xA8A8D8E0, 0x9696044D, 0x2828BD43, 0xA9A92969, 0xD9D97929, 0x8686912E,
0xD1D187AC, 0xF4F44A15, 0x8D8D1559, 0xD6D682A8, 0xB9B9BC0A, 0x42420D9E,
0xF6F6C16E, 0x2F2FB847, 0xDDDD06DF, 0x23233934, 0xCCCC6235, 0xF1F1C46A,
0xC1C112CF, 0x8585EBDC, 0x8F8F9E22, 0x7171A1C9, 0x9090F0C0, 0xAAAA539B,
0x0101F189, 0x8B8BE1D4, 0x4E4E8CED, 0x8E8E6FAB, 0xABABA212, 0x6F6F3EA2,
0xE6E6540D, 0xDBDBF252, 0x92927BBB, 0xB7B7B602, 0x6969CA2F, 0x3939D9A9,
0xD3D30CD7, 0xA7A72361, 0xA2A2AD1E, 0xC3C399B4, 0x6C6C4450, 0x07070504,
0x04047FF6, 0x272746C2, 0xACACA716, 0xD0D07625, 0x50501386, 0xDCDCF756,
0x84841A55, 0xE1E15109, 0x7A7A25BE, 0x1313EF91},
{0xA9D93939, 0x67901717, 0xB3719C9C, 0xE8D2A6A6, 0x04050707, 0xFD985252,
0xA3658080, 0x76DFE4E4, 0x9A084545, 0x92024B4B, 0x80A0E0E0, 0x78665A5A,
0xE4DDAFAF, 0xDDB06A6A, 0xD1BF6363, 0x38362A2A, 0x0D54E6E6, 0xC6432020,
0x3562CCCC, 0x98BEF2F2, 0x181E1212, 0xF724EBEB, 0xECD7A1A1, 0x6C774141,
0x43BD2828, 0x7532BCBC, 0x37D47B7B, 0x269B8888, 0xFA700D0D, 0x13F94444,
0x94B1FBFB, 0x485A7E7E, 0xF27A0303, 0xD0E48C8C, 0x8B47B6B6, 0x303C2424,
0x84A5E7E7, 0x54416B6B, 0xDF06DDDD, 0x23C56060, 0x1945FDFD, 0x5BA33A3A,
0x3D68C2C2, 0x59158D8D, 0xF321ECEC, 0xAE316666, 0xA23E6F6F, 0x82165757,
0x63951010, 0x015BEFEF, 0x834DB8B8, 0x2E918686, 0xD9B56D6D, 0x511F8383,
0x9B53AAAA, 0x7C635D5D, 0xA63B6868, 0xEB3FFEFE, 0xA5D63030, 0xBE257A7A,
0x16A7ACAC, 0x0C0F0909, 0xE335F0F0, 0x6123A7A7, 0xC0F09090, 0x8CAFE9E9,
0x3A809D9D, 0xF5925C5C, 0x73810C0C, 0x2C273131, 0x2576D0D0, 0x0BE75656,
0xBB7B9292, 0x4EE9CECE, 0x89F10101, 0x6B9F1E1E, 0x53A93434, 0x6AC4F1F1,
0xB499C3C3, 0xF1975B5B, 0xE1834747, 0xE66B1818, 0xBDC82222, 0x450E9898,
0xE26E1F1F, 0xF4C9B3B3, 0xB62F7474, 0x66CBF8F8, 0xCCFF9999, 0x95EA1414,
0x03ED5858, 0x56F7DCDC, 0xD4E18B8B, 0x1C1B1515, 0x1EADA2A2, 0xD70CD3D3,
0xFB2BE2E2, 0xC31DC8C8, 0x8E195E5E, 0xB5C22C2C, 0xE9894949, 0xCF12C1C1,
0xBF7E9595, 0xBA207D7D, 0xEA641111, 0x77840B0B, 0x396DC5C5, 0xAF6A8989,
0x33D17C7C, 0xC9A17171, 0x62CEFFFF, 0x7137BBBB, 0x81FB0F0F, 0x793DB5B5,
0x0951E1E1, 0xADDC3E3E, 0x242D3F3F, 0xCDA47676, 0xF99D5555, 0xD8EE8282,
0xE5864040, 0xC5AE7878, 0xB9CD2525, 0x4D049696, 0x44557777, 0x080A0E0E,
0x86135050, 0xE730F7F7, 0xA1D33737, 0x1D40FAFA, 0xAA346161, 0xED8C4E4E,
0x06B3B0B0, 0x706C5454, 0xB22A7373, 0xD2523B3B, 0x410B9F9F, 0x7B8B0202,
0xA088D8D8, 0x114FF3F3, 0x3167CBCB, 0xC2462727, 0x27C06767, 0x90B4FCFC,
0x20283838, 0xF67F0404, 0x60784848, 0xFF2EE5E5, 0x96074C4C, 0x5C4B6565,
0xB1C72B2B, 0xAB6F8E8E, 0x9E0D4242, 0x9CBBF5F5, 0x52F2DBDB, 0x1BF34A4A,
0x5FA63D3D, 0x9359A4A4, 0x0ABCB9B9, 0xEF3AF9F9, 0x91EF1313, 0x85FE0808,
0x49019191, 0xEE611616, 0x2D7CDEDE, 0x4FB22121, 0x8F42B1B1, 0x3BDB7272,
0x47B82F2F, 0x8748BFBF, 0x6D2CAEAE, 0x46E3C0C0, 0xD6573C3C, 0x3E859A9A,
0x6929A9A9, 0x647D4F4F, 0x2A948181, 0xCE492E2E, 0xCB17C6C6, 0x2FCA6969,
0xFCC3BDBD, 0x975CA3A3, 0x055EE8E8, 0x7AD0EDED, 0xAC87D1D1, 0x7F8E0505,
0xD5BA6464, 0x1AA8A5A5, 0x4BB72626, 0x0EB9BEBE, 0xA7608787, 0x5AF8D5D5,
0x28223636, 0x14111B1B, 0x3FDE7575, 0x2979D9D9, 0x88AAEEEE, 0x3C332D2D,
0x4C5F7979, 0x02B6B7B7, 0xB896CACA, 0xDA583535, 0xB09CC4C4, 0x17FC4343,
0x551A8484, 0x1FF64D4D, 0x8A1C5959, 0x7D38B2B2, 0x57AC3333, 0xC718CFCF,
0x8DF40606, 0x74695353, 0xB7749B9B, 0xC4F59797, 0x9F56ADAD, 0x72DAE3E3,
0x7ED5EAEA, 0x154AF4F4, 0x229E8F8F, 0x12A2ABAB, 0x584E6262, 0x07E85F5F,
0x99E51D1D, 0x34392323, 0x6EC1F6F6, 0x50446C6C, 0xDE5D3232, 0x68724646,
0x6526A0A0, 0xBC93CDCD, 0xDB03DADA, 0xF8C6BABA, 0xC8FA9E9E, 0xA882D6D6,
0x2BCF6E6E, 0x40507070, 0xDCEB8585, 0xFE750A0A, 0x328A9393, 0xA48DDFDF,
0xCA4C2929, 0x10141C1C, 0x2173D7D7, 0xF0CCB4B4, 0xD309D4D4, 0x5D108A8A,
0x0FE25151, 0x00000000, 0x6F9A1919, 0x9DE01A1A, 0x368F9494, 0x42E6C7C7,
0x4AECC9C9, 0x5EFDD2D2, 0xC1AB7F7F, 0xE0D8A8A8},
{0xBC75BC32, 0xECF3EC21, 0x20C62043, 0xB3F4B3C9, 0xDADBDA03, 0x027B028B,
0xE2FBE22B, 0x9EC89EFA, 0xC94AC9EC, 0xD4D3D409, 0x18E6186B, 0x1E6B1E9F,
0x9845980E, 0xB27DB238, 0xA6E8A6D2, 0x264B26B7, 0x3CD63C57, 0x9332938A,
0x82D882EE, 0x52FD5298, 0x7B377BD4, 0xBB71BB37, 0x5BF15B97, 0x47E14783,
0x2430243C, 0x510F51E2, 0xBAF8BAC6, 0x4A1B4AF3, 0xBF87BF48, 0x0DFA0D70,
0xB006B0B3, 0x753F75DE, 0xD25ED2FD, 0x7DBA7D20, 0x66AE6631, 0x3A5B3AA3,
0x598A591C, 0x00000000, 0xCDBCCD93, 0x1A9D1AE0, 0xAE6DAE2C, 0x7FC17FAB,
0x2BB12BC7, 0xBE0EBEB9, 0xE080E0A0, 0x8A5D8A10, 0x3BD23B52, 0x64D564BA,
0xD8A0D888, 0xE784E7A5, 0x5F075FE8, 0x1B141B11, 0x2CB52CC2, 0xFC90FCB4,
0x312C3127, 0x80A38065, 0x73B2732A, 0x0C730C81, 0x794C795F, 0x6B546B41,
0x4B924B02, 0x53745369, 0x9436948F, 0x8351831F, 0x2A382A36, 0xC4B0C49C,
0x22BD22C8, 0xD55AD5F8, 0xBDFCBDC3, 0x48604878, 0xFF62FFCE, 0x4C964C07,
0x416C4177, 0xC742C7E6, 0xEBF7EB24, 0x1C101C14, 0x5D7C5D63, 0x36283622,
0x672767C0, 0xE98CE9AF, 0x441344F9, 0x149514EA, 0xF59CF5BB, 0xCFC7CF18,
0x3F243F2D, 0xC046C0E3, 0x723B72DB, 0x5470546C, 0x29CA294C, 0xF0E3F035,
0x088508FE, 0xC6CBC617, 0xF311F34F, 0x8CD08CE4, 0xA493A459, 0xCAB8CA96,
0x68A6683B, 0xB883B84D, 0x38203828, 0xE5FFE52E, 0xAD9FAD56, 0x0B770B84,
0xC8C3C81D, 0x99CC99FF, 0x580358ED, 0x196F199A, 0x0E080E0A, 0x95BF957E,
0x70407050, 0xF7E7F730, 0x6E2B6ECF, 0x1FE21F6E, 0xB579B53D, 0x090C090F,
0x61AA6134, 0x57825716, 0x9F419F0B, 0x9D3A9D80, 0x11EA1164, 0x25B925CD,
0xAFE4AFDD, 0x459A4508, 0xDFA4DF8D, 0xA397A35C, 0xEA7EEAD5, 0x35DA3558,
0xED7AEDD0, 0x431743FC, 0xF866F8CB, 0xFB94FBB1, 0x37A137D3, 0xFA1DFA40,
0xC23DC268, 0xB4F0B4CC, 0x32DE325D, 0x9CB39C71, 0x560B56E7, 0xE372E3DA,
0x87A78760, 0x151C151B, 0xF9EFF93A, 0x63D163BF, 0x345334A9, 0x9A3E9A85,
0xB18FB142, 0x7C337CD1, 0x8826889B, 0x3D5F3DA6, 0xA1ECA1D7, 0xE476E4DF,
0x812A8194, 0x91499101, 0x0F810FFB, 0xEE88EEAA, 0x16EE1661, 0xD721D773,
0x97C497F5, 0xA51AA5A8, 0xFEEBFE3F, 0x6DD96DB5, 0x78C578AE, 0xC539C56D,
0x1D991DE5, 0x76CD76A4, 0x3EAD3EDC, 0xCB31CB67, 0xB68BB647, 0xEF01EF5B,
0x1218121E, 0x602360C5, 0x6ADD6AB0, 0x4D1F4DF6, 0xCE4ECEE9, 0xDE2DDE7C,
0x55F9559D, 0x7E487E5A, 0x214F21B2, 0x03F2037A, 0xA065A026, 0x5E8E5E19,
0x5A785A66, 0x655C654B, 0x6258624E, 0xFD19FD45, 0x068D06F4, 0x40E54086,
0xF298F2BE, 0x335733AC, 0x17671790, 0x057F058E, 0xE805E85E, 0x4F644F7D,
0x89AF896A, 0x10631095, 0x74B6742F, 0x0AFE0A75, 0x5CF55C92, 0x9BB79B74,
0x2D3C2D33, 0x30A530D6, 0x2ECE2E49, 0x49E94989, 0x46684672, 0x77447755,
0xA8E0A8D8, 0x964D9604, 0x284328BD, 0xA969A929, 0xD929D979, 0x862E8691,
0xD1ACD187, 0xF415F44A, 0x8D598D15, 0xD6A8D682, 0xB90AB9BC, 0x429E420D,
0xF66EF6C1, 0x2F472FB8, 0xDDDFDD06, 0x23342339, 0xCC35CC62, 0xF16AF1C4,
0xC1CFC112, 0x85DC85EB, 0x8F228F9E, 0x71C971A1, 0x90C090F0, 0xAA9BAA53,
0x018901F1, 0x8BD48BE1, 0x4EED4E8C, 0x8EAB8E6F, 0xAB12ABA2, 0x6FA26F3E,
0xE60DE654, 0xDB52DBF2, 0x92BB927B, 0xB702B7B6, 0x692F69CA, 0x39A939D9,
0xD3D7D30C, 0xA761A723, 0xA21EA2AD, 0xC3B4C399, 0x6C506C44, 0x07040705,
0x04F6047F, 0x27C22746, 0xAC16ACA7, 0xD025D076, 0x50865013, 0xDC56DCF7,
0x8455841A, 0xE109E151, 0x7ABE7A25, 0x139113EF},
{0xD939A9D9, 0x90176790, 0x719CB371, 0xD2A6E8D2, 0x05070405, 0x9852FD98,
0x6580A365, 0xDFE476DF, 0x08459A08, 0x024B9202, 0xA0E080A0, 0x665A7866,
0xDDAFE4DD, 0xB06ADDB0, 0xBF63D1BF, 0x362A3836, 0x54E60D54, 0x4320C643,
0x62CC3562, 0xBEF298BE, 0x1E12181E, 0x24EBF724, 0xD7A1ECD7, 0x77416C77,
0xBD2843BD, 0x32BC7532, 0xD47B37D4, 0x9B88269B, 0x700DFA70, 0xF94413F9,
0xB1FB94B1, 0x5A7E485A, 0x7A03F27A, 0xE48CD0E4, 0x47B68B47, 0x3C24303C,
0xA5E784A5, 0x416B5441, 0x06DDDF06, 0xC56023C5, 0x45FD1945, 0xA33A5BA3,
0x68C23D68, 0x158D5915, 0x21ECF321, 0x3166AE31, 0x3E6FA23E, 0x16578216,
0x95106395, 0x5BEF015B, 0x4DB8834D, 0x91862E91, 0xB56DD9B5, 0x1F83511F,
0x53AA9B53, 0x635D7C63, 0x3B68A63B, 0x3FFEEB3F, 0xD630A5D6, 0x257ABE25,
0xA7AC16A7, 0x0F090C0F, 0x35F0E335, 0x23A76123, 0xF090C0F0, 0xAFE98CAF,
0x809D3A80, 0x925CF592, 0x810C7381, 0x27312C27, 0x76D02576, 0xE7560BE7,
0x7B92BB7B, 0xE9CE4EE9, 0xF10189F1, 0x9F1E6B9F, 0xA93453A9, 0xC4F16AC4,
0x99C3B499, 0x975BF197, 0x8347E183, 0x6B18E66B, 0xC822BDC8, 0x0E98450E,
0x6E1FE26E, 0xC9B3F4C9, 0x2F74B62F, 0xCBF866CB, 0xFF99CCFF, 0xEA1495EA,
0xED5803ED, 0xF7DC56F7, 0xE18BD4E1, 0x1B151C1B, 0xADA21EAD, 0x0CD3D70C,
0x2BE2FB2B, 0x1DC8C31D, 0x195E8E19, 0xC22CB5C2, 0x8949E989, 0x12C1CF12,
0x7E95BF7E, 0x207DBA20, 0x6411EA64, 0x840B7784, 0x6DC5396D, 0x6A89AF6A,
0xD17C33D1, 0xA171C9A1, 0xCEFF62CE, 0x37BB7137, 0xFB0F81FB, 0x3DB5793D,
0x51E10951, 0xDC3EADDC, 0x2D3F242D, 0xA476CDA4, 0x9D55F99D, 0xEE82D8EE,
0x8640E586, 0xAE78C5AE, 0xCD25B9CD, 0x04964D04, 0x55774455, 0x0A0E080A,
0x13508613, 0x30F7E730, 0xD337A1D3, 0x40FA1D40, 0x3461AA34, 0x8C4EED8C,
0xB3B006B3, 0x6C54706C, 0x2A73B22A, 0x523BD252, 0x0B9F410B, 0x8B027B8B,
0x88D8A088, 0x4FF3114F, 0x67CB3167, 0x4627C246, 0xC06727C0, 0xB4FC90B4,
0x28382028, 0x7F04F67F, 0x78486078, 0x2EE5FF2E, 0x074C9607, 0x4B655C4B,
0xC72BB1C7, 0x6F8EAB6F, 0x0D429E0D, 0xBBF59CBB, 0xF2DB52F2, 0xF34A1BF3,
0xA63D5FA6, 0x59A49359, 0xBCB90ABC, 0x3AF9EF3A, 0xEF1391EF, 0xFE0885FE,
0x01914901, 0x6116EE61, 0x7CDE2D7C, 0xB2214FB2, 0x42B18F42, 0xDB723BDB,
0xB82F47B8, 0x48BF8748, 0x2CAE6D2C, 0xE3C046E3, 0x573CD657, 0x859A3E85,
0x29A96929, 0x7D4F647D, 0x94812A94, 0x492ECE49, 0x17C6CB17, 0xCA692FCA,
0xC3BDFCC3, 0x5CA3975C, 0x5EE8055E, 0xD0ED7AD0, 0x87D1AC87, 0x8E057F8E,
0xBA64D5BA, 0xA8A51AA8, 0xB7264BB7, 0xB9BE0EB9, 0x6087A760, 0xF8D55AF8,
0x22362822, 0x111B1411, 0xDE753FDE, 0x79D92979, 0xAAEE88AA, 0x332D3C33,
0x5F794C5F, 0xB6B702B6, 0x96CAB896, 0x5835DA58, 0x9CC4B09C, 0xFC4317FC,
0x1A84551A, 0xF64D1FF6, 0x1C598A1C, 0x38B27D38, 0xAC3357AC, 0x18CFC718,
0xF4068DF4, 0x69537469, 0x749BB774, 0xF597C4F5, 0x56AD9F56, 0xDAE372DA,
0xD5EA7ED5, 0x4AF4154A, 0x9E8F229E, 0xA2AB12A2, 0x4E62584E, 0xE85F07E8,
0xE51D99E5, 0x39233439, 0xC1F66EC1, 0x446C5044, 0x5D32DE5D, 0x72466872,
0x26A06526, 0x93CDBC93, 0x03DADB03, 0xC6BAF8C6, 0xFA9EC8FA, 0x82D6A882,
0xCF6E2BCF, 0x50704050, 0xEB85DCEB, 0x750AFE75, 0x8A93328A, 0x8DDFA48D,
0x4C29CA4C, 0x141C1014, 0x73D72173, 0xCCB4F0CC, 0x09D4D309, 0x108A5D10,
0xE2510FE2, 0x00000000, 0x9A196F9A, 0xE01A9DE0, 0x8F94368F, 0xE6C742E6,
0xECC94AEC, 0xFDD25EFD, 0xAB7FC1AB, 0xD8A8E0D8}
};
/* The exp_to_poly and poly_to_exp tables are used to perform efficient
* operations in GF(2^8) represented as GF(2)[x]/w(x) where
* w(x)=x^8+x^6+x^3+x^2+1. We care about doing that because it's part of the
* definition of the RS matrix in the key schedule. Elements of that field
* are polynomials of degree not greater than 7 and all coefficients 0 or 1,
* which can be represented naturally by bytes (just substitute x=2). In that
* form, GF(2^8) addition is the same as bitwise XOR, but GF(2^8)
* multiplication is inefficient without hardware support. To multiply
* faster, I make use of the fact x is a generator for the nonzero elements,
* so that every element p of GF(2)[x]/w(x) is either 0 or equal to (x)^n for
* some n in 0..254. Note that that caret is exponentiation in GF(2^8),
* *not* polynomial notation. So if I want to compute pq where p and q are
* in GF(2^8), I can just say:
* 1. if p=0 or q=0 then pq=0
* 2. otherwise, find m and n such that p=x^m and q=x^n
* 3. pq=(x^m)(x^n)=x^(m+n), so add m and n and find pq
* The translations in steps 2 and 3 are looked up in the tables
* poly_to_exp (for step 2) and exp_to_poly (for step 3). To see this
* in action, look at the CALC_S macro. As additional wrinkles, note that
* one of my operands is always a constant, so the poly_to_exp lookup on it
* is done in advance; I included the original values in the comments so
* readers can have some chance of recognizing that this *is* the RS matrix
* from the Twofish paper. I've only included the table entries I actually
* need; I never do a lookup on a variable input of zero and the biggest
* exponents I'll ever see are 254 (variable) and 237 (constant), so they'll
* never sum to more than 491. I'm repeating part of the exp_to_poly table
* so that I don't have to do mod-255 reduction in the exponent arithmetic.
* Since I know my constant operands are never zero, I only have to worry
* about zero values in the variable operand, and I do it with a simple
* conditional branch. I know conditionals are expensive, but I couldn't
* see a non-horrible way of avoiding them, and I did manage to group the
* statements so that each if covers four group multiplications. */
static const u8 poly_to_exp[255] = {
0x00, 0x01, 0x17, 0x02, 0x2E, 0x18, 0x53, 0x03, 0x6A, 0x2F, 0x93, 0x19,
0x34, 0x54, 0x45, 0x04, 0x5C, 0x6B, 0xB6, 0x30, 0xA6, 0x94, 0x4B, 0x1A,
0x8C, 0x35, 0x81, 0x55, 0xAA, 0x46, 0x0D, 0x05, 0x24, 0x5D, 0x87, 0x6C,
0x9B, 0xB7, 0xC1, 0x31, 0x2B, 0xA7, 0xA3, 0x95, 0x98, 0x4C, 0xCA, 0x1B,
0xE6, 0x8D, 0x73, 0x36, 0xCD, 0x82, 0x12, 0x56, 0x62, 0xAB, 0xF0, 0x47,
0x4F, 0x0E, 0xBD, 0x06, 0xD4, 0x25, 0xD2, 0x5E, 0x27, 0x88, 0x66, 0x6D,
0xD6, 0x9C, 0x79, 0xB8, 0x08, 0xC2, 0xDF, 0x32, 0x68, 0x2C, 0xFD, 0xA8,
0x8A, 0xA4, 0x5A, 0x96, 0x29, 0x99, 0x22, 0x4D, 0x60, 0xCB, 0xE4, 0x1C,
0x7B, 0xE7, 0x3B, 0x8E, 0x9E, 0x74, 0xF4, 0x37, 0xD8, 0xCE, 0xF9, 0x83,
0x6F, 0x13, 0xB2, 0x57, 0xE1, 0x63, 0xDC, 0xAC, 0xC4, 0xF1, 0xAF, 0x48,
0x0A, 0x50, 0x42, 0x0F, 0xBA, 0xBE, 0xC7, 0x07, 0xDE, 0xD5, 0x78, 0x26,
0x65, 0xD3, 0xD1, 0x5F, 0xE3, 0x28, 0x21, 0x89, 0x59, 0x67, 0xFC, 0x6E,
0xB1, 0xD7, 0xF8, 0x9D, 0xF3, 0x7A, 0x3A, 0xB9, 0xC6, 0x09, 0x41, 0xC3,
0xAE, 0xE0, 0xDB, 0x33, 0x44, 0x69, 0x92, 0x2D, 0x52, 0xFE, 0x16, 0xA9,
0x0C, 0x8B, 0x80, 0xA5, 0x4A, 0x5B, 0xB5, 0x97, 0xC9, 0x2A, 0xA2, 0x9A,
0xC0, 0x23, 0x86, 0x4E, 0xBC, 0x61, 0xEF, 0xCC, 0x11, 0xE5, 0x72, 0x1D,
0x3D, 0x7C, 0xEB, 0xE8, 0xE9, 0x3C, 0xEA, 0x8F, 0x7D, 0x9F, 0xEC, 0x75,
0x1E, 0xF5, 0x3E, 0x38, 0xF6, 0xD9, 0x3F, 0xCF, 0x76, 0xFA, 0x1F, 0x84,
0xA0, 0x70, 0xED, 0x14, 0x90, 0xB3, 0x7E, 0x58, 0xFB, 0xE2, 0x20, 0x64,
0xD0, 0xDD, 0x77, 0xAD, 0xDA, 0xC5, 0x40, 0xF2, 0x39, 0xB0, 0xF7, 0x49,
0xB4, 0x0B, 0x7F, 0x51, 0x15, 0x43, 0x91, 0x10, 0x71, 0xBB, 0xEE, 0xBF,
0x85, 0xC8, 0xA1
};
static const u8 exp_to_poly[492] = {
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x4D, 0x9A, 0x79, 0xF2,
0xA9, 0x1F, 0x3E, 0x7C, 0xF8, 0xBD, 0x37, 0x6E, 0xDC, 0xF5, 0xA7, 0x03,
0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0xCD, 0xD7, 0xE3, 0x8B, 0x5B, 0xB6,
0x21, 0x42, 0x84, 0x45, 0x8A, 0x59, 0xB2, 0x29, 0x52, 0xA4, 0x05, 0x0A,
0x14, 0x28, 0x50, 0xA0, 0x0D, 0x1A, 0x34, 0x68, 0xD0, 0xED, 0x97, 0x63,
0xC6, 0xC1, 0xCF, 0xD3, 0xEB, 0x9B, 0x7B, 0xF6, 0xA1, 0x0F, 0x1E, 0x3C,
0x78, 0xF0, 0xAD, 0x17, 0x2E, 0x5C, 0xB8, 0x3D, 0x7A, 0xF4, 0xA5, 0x07,
0x0E, 0x1C, 0x38, 0x70, 0xE0, 0x8D, 0x57, 0xAE, 0x11, 0x22, 0x44, 0x88,
0x5D, 0xBA, 0x39, 0x72, 0xE4, 0x85, 0x47, 0x8E, 0x51, 0xA2, 0x09, 0x12,
0x24, 0x48, 0x90, 0x6D, 0xDA, 0xF9, 0xBF, 0x33, 0x66, 0xCC, 0xD5, 0xE7,
0x83, 0x4B, 0x96, 0x61, 0xC2, 0xC9, 0xDF, 0xF3, 0xAB, 0x1B, 0x36, 0x6C,
0xD8, 0xFD, 0xB7, 0x23, 0x46, 0x8C, 0x55, 0xAA, 0x19, 0x32, 0x64, 0xC8,
0xDD, 0xF7, 0xA3, 0x0B, 0x16, 0x2C, 0x58, 0xB0, 0x2D, 0x5A, 0xB4, 0x25,
0x4A, 0x94, 0x65, 0xCA, 0xD9, 0xFF, 0xB3, 0x2B, 0x56, 0xAC, 0x15, 0x2A,
0x54, 0xA8, 0x1D, 0x3A, 0x74, 0xE8, 0x9D, 0x77, 0xEE, 0x91, 0x6F, 0xDE,
0xF1, 0xAF, 0x13, 0x26, 0x4C, 0x98, 0x7D, 0xFA, 0xB9, 0x3F, 0x7E, 0xFC,
0xB5, 0x27, 0x4E, 0x9C, 0x75, 0xEA, 0x99, 0x7F, 0xFE, 0xB1, 0x2F, 0x5E,
0xBC, 0x35, 0x6A, 0xD4, 0xE5, 0x87, 0x43, 0x86, 0x41, 0x82, 0x49, 0x92,
0x69, 0xD2, 0xE9, 0x9F, 0x73, 0xE6, 0x81, 0x4F, 0x9E, 0x71, 0xE2, 0x89,
0x5F, 0xBE, 0x31, 0x62, 0xC4, 0xC5, 0xC7, 0xC3, 0xCB, 0xDB, 0xFB, 0xBB,
0x3B, 0x76, 0xEC, 0x95, 0x67, 0xCE, 0xD1, 0xEF, 0x93, 0x6B, 0xD6, 0xE1,
0x8F, 0x53, 0xA6, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x4D,
0x9A, 0x79, 0xF2, 0xA9, 0x1F, 0x3E, 0x7C, 0xF8, 0xBD, 0x37, 0x6E, 0xDC,
0xF5, 0xA7, 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0xCD, 0xD7, 0xE3,
0x8B, 0x5B, 0xB6, 0x21, 0x42, 0x84, 0x45, 0x8A, 0x59, 0xB2, 0x29, 0x52,
0xA4, 0x05, 0x0A, 0x14, 0x28, 0x50, 0xA0, 0x0D, 0x1A, 0x34, 0x68, 0xD0,
0xED, 0x97, 0x63, 0xC6, 0xC1, 0xCF, 0xD3, 0xEB, 0x9B, 0x7B, 0xF6, 0xA1,
0x0F, 0x1E, 0x3C, 0x78, 0xF0, 0xAD, 0x17, 0x2E, 0x5C, 0xB8, 0x3D, 0x7A,
0xF4, 0xA5, 0x07, 0x0E, 0x1C, 0x38, 0x70, 0xE0, 0x8D, 0x57, 0xAE, 0x11,
0x22, 0x44, 0x88, 0x5D, 0xBA, 0x39, 0x72, 0xE4, 0x85, 0x47, 0x8E, 0x51,
0xA2, 0x09, 0x12, 0x24, 0x48, 0x90, 0x6D, 0xDA, 0xF9, 0xBF, 0x33, 0x66,
0xCC, 0xD5, 0xE7, 0x83, 0x4B, 0x96, 0x61, 0xC2, 0xC9, 0xDF, 0xF3, 0xAB,
0x1B, 0x36, 0x6C, 0xD8, 0xFD, 0xB7, 0x23, 0x46, 0x8C, 0x55, 0xAA, 0x19,
0x32, 0x64, 0xC8, 0xDD, 0xF7, 0xA3, 0x0B, 0x16, 0x2C, 0x58, 0xB0, 0x2D,
0x5A, 0xB4, 0x25, 0x4A, 0x94, 0x65, 0xCA, 0xD9, 0xFF, 0xB3, 0x2B, 0x56,
0xAC, 0x15, 0x2A, 0x54, 0xA8, 0x1D, 0x3A, 0x74, 0xE8, 0x9D, 0x77, 0xEE,
0x91, 0x6F, 0xDE, 0xF1, 0xAF, 0x13, 0x26, 0x4C, 0x98, 0x7D, 0xFA, 0xB9,
0x3F, 0x7E, 0xFC, 0xB5, 0x27, 0x4E, 0x9C, 0x75, 0xEA, 0x99, 0x7F, 0xFE,
0xB1, 0x2F, 0x5E, 0xBC, 0x35, 0x6A, 0xD4, 0xE5, 0x87, 0x43, 0x86, 0x41,
0x82, 0x49, 0x92, 0x69, 0xD2, 0xE9, 0x9F, 0x73, 0xE6, 0x81, 0x4F, 0x9E,
0x71, 0xE2, 0x89, 0x5F, 0xBE, 0x31, 0x62, 0xC4, 0xC5, 0xC7, 0xC3, 0xCB
};
/* The table constants are indices of
* S-box entries, preprocessed through q0 and q1. */
static const u8 calc_sb_tbl[512] = {
0xA9, 0x75, 0x67, 0xF3, 0xB3, 0xC6, 0xE8, 0xF4,
0x04, 0xDB, 0xFD, 0x7B, 0xA3, 0xFB, 0x76, 0xC8,
0x9A, 0x4A, 0x92, 0xD3, 0x80, 0xE6, 0x78, 0x6B,
0xE4, 0x45, 0xDD, 0x7D, 0xD1, 0xE8, 0x38, 0x4B,
0x0D, 0xD6, 0xC6, 0x32, 0x35, 0xD8, 0x98, 0xFD,
0x18, 0x37, 0xF7, 0x71, 0xEC, 0xF1, 0x6C, 0xE1,
0x43, 0x30, 0x75, 0x0F, 0x37, 0xF8, 0x26, 0x1B,
0xFA, 0x87, 0x13, 0xFA, 0x94, 0x06, 0x48, 0x3F,
0xF2, 0x5E, 0xD0, 0xBA, 0x8B, 0xAE, 0x30, 0x5B,
0x84, 0x8A, 0x54, 0x00, 0xDF, 0xBC, 0x23, 0x9D,
0x19, 0x6D, 0x5B, 0xC1, 0x3D, 0xB1, 0x59, 0x0E,
0xF3, 0x80, 0xAE, 0x5D, 0xA2, 0xD2, 0x82, 0xD5,
0x63, 0xA0, 0x01, 0x84, 0x83, 0x07, 0x2E, 0x14,
0xD9, 0xB5, 0x51, 0x90, 0x9B, 0x2C, 0x7C, 0xA3,
0xA6, 0xB2, 0xEB, 0x73, 0xA5, 0x4C, 0xBE, 0x54,
0x16, 0x92, 0x0C, 0x74, 0xE3, 0x36, 0x61, 0x51,
0xC0, 0x38, 0x8C, 0xB0, 0x3A, 0xBD, 0xF5, 0x5A,
0x73, 0xFC, 0x2C, 0x60, 0x25, 0x62, 0x0B, 0x96,
0xBB, 0x6C, 0x4E, 0x42, 0x89, 0xF7, 0x6B, 0x10,
0x53, 0x7C, 0x6A, 0x28, 0xB4, 0x27, 0xF1, 0x8C,
0xE1, 0x13, 0xE6, 0x95, 0xBD, 0x9C, 0x45, 0xC7,
0xE2, 0x24, 0xF4, 0x46, 0xB6, 0x3B, 0x66, 0x70,
0xCC, 0xCA, 0x95, 0xE3, 0x03, 0x85, 0x56, 0xCB,
0xD4, 0x11, 0x1C, 0xD0, 0x1E, 0x93, 0xD7, 0xB8,
0xFB, 0xA6, 0xC3, 0x83, 0x8E, 0x20, 0xB5, 0xFF,
0xE9, 0x9F, 0xCF, 0x77, 0xBF, 0xC3, 0xBA, 0xCC,
0xEA, 0x03, 0x77, 0x6F, 0x39, 0x08, 0xAF, 0xBF,
0x33, 0x40, 0xC9, 0xE7, 0x62, 0x2B, 0x71, 0xE2,
0x81, 0x79, 0x79, 0x0C, 0x09, 0xAA, 0xAD, 0x82,
0x24, 0x41, 0xCD, 0x3A, 0xF9, 0xEA, 0xD8, 0xB9,
0xE5, 0xE4, 0xC5, 0x9A, 0xB9, 0xA4, 0x4D, 0x97,
0x44, 0x7E, 0x08, 0xDA, 0x86, 0x7A, 0xE7, 0x17,
0xA1, 0x66, 0x1D, 0x94, 0xAA, 0xA1, 0xED, 0x1D,
0x06, 0x3D, 0x70, 0xF0, 0xB2, 0xDE, 0xD2, 0xB3,
0x41, 0x0B, 0x7B, 0x72, 0xA0, 0xA7, 0x11, 0x1C,
0x31, 0xEF, 0xC2, 0xD1, 0x27, 0x53, 0x90, 0x3E,
0x20, 0x8F, 0xF6, 0x33, 0x60, 0x26, 0xFF, 0x5F,
0x96, 0xEC, 0x5C, 0x76, 0xB1, 0x2A, 0xAB, 0x49,
0x9E, 0x81, 0x9C, 0x88, 0x52, 0xEE, 0x1B, 0x21,
0x5F, 0xC4, 0x93, 0x1A, 0x0A, 0xEB, 0xEF, 0xD9,
0x91, 0xC5, 0x85, 0x39, 0x49, 0x99, 0xEE, 0xCD,
0x2D, 0xAD, 0x4F, 0x31, 0x8F, 0x8B, 0x3B, 0x01,
0x47, 0x18, 0x87, 0x23, 0x6D, 0xDD, 0x46, 0x1F,
0xD6, 0x4E, 0x3E, 0x2D, 0x69, 0xF9, 0x64, 0x48,
0x2A, 0x4F, 0xCE, 0xF2, 0xCB, 0x65, 0x2F, 0x8E,
0xFC, 0x78, 0x97, 0x5C, 0x05, 0x58, 0x7A, 0x19,
0xAC, 0x8D, 0x7F, 0xE5, 0xD5, 0x98, 0x1A, 0x57,
0x4B, 0x67, 0x0E, 0x7F, 0xA7, 0x05, 0x5A, 0x64,
0x28, 0xAF, 0x14, 0x63, 0x3F, 0xB6, 0x29, 0xFE,
0x88, 0xF5, 0x3C, 0xB7, 0x4C, 0x3C, 0x02, 0xA5,
0xB8, 0xCE, 0xDA, 0xE9, 0xB0, 0x68, 0x17, 0x44,
0x55, 0xE0, 0x1F, 0x4D, 0x8A, 0x43, 0x7D, 0x69,
0x57, 0x29, 0xC7, 0x2E, 0x8D, 0xAC, 0x74, 0x15,
0xB7, 0x59, 0xC4, 0xA8, 0x9F, 0x0A, 0x72, 0x9E,
0x7E, 0x6E, 0x15, 0x47, 0x22, 0xDF, 0x12, 0x34,
0x58, 0x35, 0x07, 0x6A, 0x99, 0xCF, 0x34, 0xDC,
0x6E, 0x22, 0x50, 0xC9, 0xDE, 0xC0, 0x68, 0x9B,
0x65, 0x89, 0xBC, 0xD4, 0xDB, 0xED, 0xF8, 0xAB,
0xC8, 0x12, 0xA8, 0xA2, 0x2B, 0x0D, 0x40, 0x52,
0xDC, 0xBB, 0xFE, 0x02, 0x32, 0x2F, 0xA4, 0xA9,
0xCA, 0xD7, 0x10, 0x61, 0x21, 0x1E, 0xF0, 0xB4,
0xD3, 0x50, 0x5D, 0x04, 0x0F, 0xF6, 0x00, 0xC2,
0x6F, 0x16, 0x9D, 0x25, 0x36, 0x86, 0x42, 0x56,
0x4A, 0x55, 0x5E, 0x09, 0xC1, 0xBE, 0xE0, 0x91
};
/* Macro to perform one column of the RS matrix multiplication. The
* parameters a, b, c, and d are the four bytes of output; i is the index
* of the key bytes, and w, x, y, and z, are the column of constants from
* the RS matrix, preprocessed through the poly_to_exp table. */
#define CALC_S(a, b, c, d, i, w, x, y, z) \
if (key[i]) { \
tmp = poly_to_exp[key[i] - 1]; \
(a) ^= exp_to_poly[tmp + (w)]; \
(b) ^= exp_to_poly[tmp + (x)]; \
(c) ^= exp_to_poly[tmp + (y)]; \
(d) ^= exp_to_poly[tmp + (z)]; \
}
/* Macros to calculate the key-dependent S-boxes for a 128-bit key using
* the S vector from CALC_S. CALC_SB_2 computes a single entry in all
* four S-boxes, where i is the index of the entry to compute, and a and b
* are the index numbers preprocessed through the q0 and q1 tables
* respectively. */
#define CALC_SB_2(i, a, b) \
ctx->s[0][i] = mds[0][q0[(a) ^ sa] ^ se]; \
ctx->s[1][i] = mds[1][q0[(b) ^ sb] ^ sf]; \
ctx->s[2][i] = mds[2][q1[(a) ^ sc] ^ sg]; \
ctx->s[3][i] = mds[3][q1[(b) ^ sd] ^ sh]
/* Macro exactly like CALC_SB_2, but for 192-bit keys. */
#define CALC_SB192_2(i, a, b) \
ctx->s[0][i] = mds[0][q0[q0[(b) ^ sa] ^ se] ^ si]; \
ctx->s[1][i] = mds[1][q0[q1[(b) ^ sb] ^ sf] ^ sj]; \
ctx->s[2][i] = mds[2][q1[q0[(a) ^ sc] ^ sg] ^ sk]; \
ctx->s[3][i] = mds[3][q1[q1[(a) ^ sd] ^ sh] ^ sl];
/* Macro exactly like CALC_SB_2, but for 256-bit keys. */
#define CALC_SB256_2(i, a, b) \
ctx->s[0][i] = mds[0][q0[q0[q1[(b) ^ sa] ^ se] ^ si] ^ sm]; \
ctx->s[1][i] = mds[1][q0[q1[q1[(a) ^ sb] ^ sf] ^ sj] ^ sn]; \
ctx->s[2][i] = mds[2][q1[q0[q0[(a) ^ sc] ^ sg] ^ sk] ^ so]; \
ctx->s[3][i] = mds[3][q1[q1[q0[(b) ^ sd] ^ sh] ^ sl] ^ sp];
/* Macros to calculate the whitening and round subkeys. CALC_K_2 computes the
* last two stages of the h() function for a given index (either 2i or 2i+1).
* a, b, c, and d are the four bytes going into the last two stages. For
* 128-bit keys, this is the entire h() function and a and c are the index
* preprocessed through q0 and q1 respectively; for longer keys they are the
* output of previous stages. j is the index of the first key byte to use.
* CALC_K computes a pair of subkeys for 128-bit Twofish, by calling CALC_K_2
* twice, doing the Psuedo-Hadamard Transform, and doing the necessary
* rotations. Its parameters are: a, the array to write the results into,
* j, the index of the first output entry, k and l, the preprocessed indices
* for index 2i, and m and n, the preprocessed indices for index 2i+1.
* CALC_K192_2 expands CALC_K_2 to handle 192-bit keys, by doing an
* additional lookup-and-XOR stage. The parameters a, b, c and d are the
* four bytes going into the last three stages. For 192-bit keys, c = d
* are the index preprocessed through q0, and a = b are the index
* preprocessed through q1; j is the index of the first key byte to use.
* CALC_K192 is identical to CALC_K but for using the CALC_K192_2 macro
* instead of CALC_K_2.
* CALC_K256_2 expands CALC_K192_2 to handle 256-bit keys, by doing an
* additional lookup-and-XOR stage. The parameters a and b are the index
* preprocessed through q0 and q1 respectively; j is the index of the first
* key byte to use. CALC_K256 is identical to CALC_K but for using the
* CALC_K256_2 macro instead of CALC_K_2. */
#define CALC_K_2(a, b, c, d, j) \
mds[0][q0[a ^ key[(j) + 8]] ^ key[j]] \
^ mds[1][q0[b ^ key[(j) + 9]] ^ key[(j) + 1]] \
^ mds[2][q1[c ^ key[(j) + 10]] ^ key[(j) + 2]] \
^ mds[3][q1[d ^ key[(j) + 11]] ^ key[(j) + 3]]
#define CALC_K(a, j, k, l, m, n) \
x = CALC_K_2 (k, l, k, l, 0); \
y = CALC_K_2 (m, n, m, n, 4); \
y = (y << 8) + (y >> 24); \
x += y; y += x; ctx->a[j] = x; \
ctx->a[(j) + 1] = (y << 9) + (y >> 23)
#define CALC_K192_2(a, b, c, d, j) \
CALC_K_2 (q0[a ^ key[(j) + 16]], \
q1[b ^ key[(j) + 17]], \
q0[c ^ key[(j) + 18]], \
q1[d ^ key[(j) + 19]], j)
#define CALC_K192(a, j, k, l, m, n) \
x = CALC_K192_2 (l, l, k, k, 0); \
y = CALC_K192_2 (n, n, m, m, 4); \
y = (y << 8) + (y >> 24); \
x += y; y += x; ctx->a[j] = x; \
ctx->a[(j) + 1] = (y << 9) + (y >> 23)
#define CALC_K256_2(a, b, j) \
CALC_K192_2 (q1[b ^ key[(j) + 24]], \
q1[a ^ key[(j) + 25]], \
q0[a ^ key[(j) + 26]], \
q0[b ^ key[(j) + 27]], j)
#define CALC_K256(a, j, k, l, m, n) \
x = CALC_K256_2 (k, l, 0); \
y = CALC_K256_2 (m, n, 4); \
y = (y << 8) + (y >> 24); \
x += y; y += x; ctx->a[j] = x; \
ctx->a[(j) + 1] = (y << 9) + (y >> 23)
/* Perform the key setup. */
int twofish_set_key (TWOFISH_context *ctx,
const unsigned char *key, int key_len)
{
int i, j, k;
/* Temporaries for CALC_K. */
u32 x, y;
/* The S vector used to key the S-boxes, split up into individual bytes.
* 128-bit keys use only sa through sh; 256-bit use all of them. */
u8 sa = 0, sb = 0, sc = 0, sd = 0, se = 0, sf = 0, sg = 0, sh = 0;
u8 si = 0, sj = 0, sk = 0, sl = 0, sm = 0, sn = 0, so = 0, sp = 0;
/* Temporary for CALC_S. */
u8 tmp;
/* Check key length. */
if (key_len != 16 && key_len != 24 && key_len != 32)
return -1; /* unsupported key length */
/* Compute the first two words of the S vector. The magic numbers are
* the entries of the RS matrix, preprocessed through poly_to_exp. The
* numbers in the comments are the original (polynomial form) matrix
* entries. */
CALC_S (sa, sb, sc, sd, 0, 0x00, 0x2D, 0x01, 0x2D); /* 01 A4 02 A4 */
CALC_S (sa, sb, sc, sd, 1, 0x2D, 0xA4, 0x44, 0x8A); /* A4 56 A1 55 */
CALC_S (sa, sb, sc, sd, 2, 0x8A, 0xD5, 0xBF, 0xD1); /* 55 82 FC 87 */
CALC_S (sa, sb, sc, sd, 3, 0xD1, 0x7F, 0x3D, 0x99); /* 87 F3 C1 5A */
CALC_S (sa, sb, sc, sd, 4, 0x99, 0x46, 0x66, 0x96); /* 5A 1E 47 58 */
CALC_S (sa, sb, sc, sd, 5, 0x96, 0x3C, 0x5B, 0xED); /* 58 C6 AE DB */
CALC_S (sa, sb, sc, sd, 6, 0xED, 0x37, 0x4F, 0xE0); /* DB 68 3D 9E */
CALC_S (sa, sb, sc, sd, 7, 0xE0, 0xD0, 0x8C, 0x17); /* 9E E5 19 03 */
CALC_S (se, sf, sg, sh, 8, 0x00, 0x2D, 0x01, 0x2D); /* 01 A4 02 A4 */
CALC_S (se, sf, sg, sh, 9, 0x2D, 0xA4, 0x44, 0x8A); /* A4 56 A1 55 */
CALC_S (se, sf, sg, sh, 10, 0x8A, 0xD5, 0xBF, 0xD1); /* 55 82 FC 87 */
CALC_S (se, sf, sg, sh, 11, 0xD1, 0x7F, 0x3D, 0x99); /* 87 F3 C1 5A */
CALC_S (se, sf, sg, sh, 12, 0x99, 0x46, 0x66, 0x96); /* 5A 1E 47 58 */
CALC_S (se, sf, sg, sh, 13, 0x96, 0x3C, 0x5B, 0xED); /* 58 C6 AE DB */
CALC_S (se, sf, sg, sh, 14, 0xED, 0x37, 0x4F, 0xE0); /* DB 68 3D 9E */
CALC_S (se, sf, sg, sh, 15, 0xE0, 0xD0, 0x8C, 0x17); /* 9E E5 19 03 */
if (key_len == 24 || key_len == 32) { /* 192- or 256-bit key */
/* Calculate the third word of the S vector */
CALC_S (si, sj, sk, sl, 16, 0x00, 0x2D, 0x01, 0x2D); /* 01 A4 02 A4 */
CALC_S (si, sj, sk, sl, 17, 0x2D, 0xA4, 0x44, 0x8A); /* A4 56 A1 55 */
CALC_S (si, sj, sk, sl, 18, 0x8A, 0xD5, 0xBF, 0xD1); /* 55 82 FC 87 */
CALC_S (si, sj, sk, sl, 19, 0xD1, 0x7F, 0x3D, 0x99); /* 87 F3 C1 5A */
CALC_S (si, sj, sk, sl, 20, 0x99, 0x46, 0x66, 0x96); /* 5A 1E 47 58 */
CALC_S (si, sj, sk, sl, 21, 0x96, 0x3C, 0x5B, 0xED); /* 58 C6 AE DB */
CALC_S (si, sj, sk, sl, 22, 0xED, 0x37, 0x4F, 0xE0); /* DB 68 3D 9E */
CALC_S (si, sj, sk, sl, 23, 0xE0, 0xD0, 0x8C, 0x17); /* 9E E5 19 03 */
}
if (key_len == 32) { /* 256-bit key */
/* Calculate the fourth word of the S vector */
CALC_S (sm, sn, so, sp, 24, 0x00, 0x2D, 0x01, 0x2D); /* 01 A4 02 A4 */
CALC_S (sm, sn, so, sp, 25, 0x2D, 0xA4, 0x44, 0x8A); /* A4 56 A1 55 */
CALC_S (sm, sn, so, sp, 26, 0x8A, 0xD5, 0xBF, 0xD1); /* 55 82 FC 87 */
CALC_S (sm, sn, so, sp, 27, 0xD1, 0x7F, 0x3D, 0x99); /* 87 F3 C1 5A */
CALC_S (sm, sn, so, sp, 28, 0x99, 0x46, 0x66, 0x96); /* 5A 1E 47 58 */
CALC_S (sm, sn, so, sp, 29, 0x96, 0x3C, 0x5B, 0xED); /* 58 C6 AE DB */
CALC_S (sm, sn, so, sp, 30, 0xED, 0x37, 0x4F, 0xE0); /* DB 68 3D 9E */
CALC_S (sm, sn, so, sp, 31, 0xE0, 0xD0, 0x8C, 0x17); /* 9E E5 19 03 */
/* Compute the S-boxes. */
for ( i = j = 0, k = 1; i < 256; i++, j += 2, k += 2 ) {
CALC_SB256_2( i, calc_sb_tbl[j], calc_sb_tbl[k] );
}
/* Calculate whitening and round subkeys. The constants are
* indices of subkeys, preprocessed through q0 and q1. */
CALC_K256 (w, 0, 0xA9, 0x75, 0x67, 0xF3);
CALC_K256 (w, 2, 0xB3, 0xC6, 0xE8, 0xF4);
CALC_K256 (w, 4, 0x04, 0xDB, 0xFD, 0x7B);
CALC_K256 (w, 6, 0xA3, 0xFB, 0x76, 0xC8);
CALC_K256 (k, 0, 0x9A, 0x4A, 0x92, 0xD3);
CALC_K256 (k, 2, 0x80, 0xE6, 0x78, 0x6B);
CALC_K256 (k, 4, 0xE4, 0x45, 0xDD, 0x7D);
CALC_K256 (k, 6, 0xD1, 0xE8, 0x38, 0x4B);
CALC_K256 (k, 8, 0x0D, 0xD6, 0xC6, 0x32);
CALC_K256 (k, 10, 0x35, 0xD8, 0x98, 0xFD);
CALC_K256 (k, 12, 0x18, 0x37, 0xF7, 0x71);
CALC_K256 (k, 14, 0xEC, 0xF1, 0x6C, 0xE1);
CALC_K256 (k, 16, 0x43, 0x30, 0x75, 0x0F);
CALC_K256 (k, 18, 0x37, 0xF8, 0x26, 0x1B);
CALC_K256 (k, 20, 0xFA, 0x87, 0x13, 0xFA);
CALC_K256 (k, 22, 0x94, 0x06, 0x48, 0x3F);
CALC_K256 (k, 24, 0xF2, 0x5E, 0xD0, 0xBA);
CALC_K256 (k, 26, 0x8B, 0xAE, 0x30, 0x5B);
CALC_K256 (k, 28, 0x84, 0x8A, 0x54, 0x00);
CALC_K256 (k, 30, 0xDF, 0xBC, 0x23, 0x9D);
} else if (key_len == 24) { /* 192-bit key */
/* Compute the S-boxes. */
for ( i = j = 0, k = 1; i < 256; i++, j += 2, k += 2 ) {
CALC_SB192_2( i, calc_sb_tbl[j], calc_sb_tbl[k] );
}
/* Calculate whitening and round subkeys. The constants are
* indices of subkeys, preprocessed through q0 and q1. */
CALC_K192 (w, 0, 0xA9, 0x75, 0x67, 0xF3);
CALC_K192 (w, 2, 0xB3, 0xC6, 0xE8, 0xF4);
CALC_K192 (w, 4, 0x04, 0xDB, 0xFD, 0x7B);
CALC_K192 (w, 6, 0xA3, 0xFB, 0x76, 0xC8);
CALC_K192 (k, 0, 0x9A, 0x4A, 0x92, 0xD3);
CALC_K192 (k, 2, 0x80, 0xE6, 0x78, 0x6B);
CALC_K192 (k, 4, 0xE4, 0x45, 0xDD, 0x7D);
CALC_K192 (k, 6, 0xD1, 0xE8, 0x38, 0x4B);
CALC_K192 (k, 8, 0x0D, 0xD6, 0xC6, 0x32);
CALC_K192 (k, 10, 0x35, 0xD8, 0x98, 0xFD);
CALC_K192 (k, 12, 0x18, 0x37, 0xF7, 0x71);
CALC_K192 (k, 14, 0xEC, 0xF1, 0x6C, 0xE1);
CALC_K192 (k, 16, 0x43, 0x30, 0x75, 0x0F);
CALC_K192 (k, 18, 0x37, 0xF8, 0x26, 0x1B);
CALC_K192 (k, 20, 0xFA, 0x87, 0x13, 0xFA);
CALC_K192 (k, 22, 0x94, 0x06, 0x48, 0x3F);
CALC_K192 (k, 24, 0xF2, 0x5E, 0xD0, 0xBA);
CALC_K192 (k, 26, 0x8B, 0xAE, 0x30, 0x5B);
CALC_K192 (k, 28, 0x84, 0x8A, 0x54, 0x00);
CALC_K192 (k, 30, 0xDF, 0xBC, 0x23, 0x9D);
} else { /* 128-bit key */
/* Compute the S-boxes. */
for ( i = j = 0, k = 1; i < 256; i++, j += 2, k += 2 ) {
CALC_SB_2( i, calc_sb_tbl[j], calc_sb_tbl[k] );
}
/* Calculate whitening and round subkeys. The constants are
* indices of subkeys, preprocessed through q0 and q1. */
CALC_K (w, 0, 0xA9, 0x75, 0x67, 0xF3);
CALC_K (w, 2, 0xB3, 0xC6, 0xE8, 0xF4);
CALC_K (w, 4, 0x04, 0xDB, 0xFD, 0x7B);
CALC_K (w, 6, 0xA3, 0xFB, 0x76, 0xC8);
CALC_K (k, 0, 0x9A, 0x4A, 0x92, 0xD3);
CALC_K (k, 2, 0x80, 0xE6, 0x78, 0x6B);
CALC_K (k, 4, 0xE4, 0x45, 0xDD, 0x7D);
CALC_K (k, 6, 0xD1, 0xE8, 0x38, 0x4B);
CALC_K (k, 8, 0x0D, 0xD6, 0xC6, 0x32);
CALC_K (k, 10, 0x35, 0xD8, 0x98, 0xFD);
CALC_K (k, 12, 0x18, 0x37, 0xF7, 0x71);
CALC_K (k, 14, 0xEC, 0xF1, 0x6C, 0xE1);
CALC_K (k, 16, 0x43, 0x30, 0x75, 0x0F);
CALC_K (k, 18, 0x37, 0xF8, 0x26, 0x1B);
CALC_K (k, 20, 0xFA, 0x87, 0x13, 0xFA);
CALC_K (k, 22, 0x94, 0x06, 0x48, 0x3F);
CALC_K (k, 24, 0xF2, 0x5E, 0xD0, 0xBA);
CALC_K (k, 26, 0x8B, 0xAE, 0x30, 0x5B);
CALC_K (k, 28, 0x84, 0x8A, 0x54, 0x00);
CALC_K (k, 30, 0xDF, 0xBC, 0x23, 0x9D);
}
return 0;
}
/* Macros to compute the g() function in the encryption and decryption
* rounds. G1 is the straight g() function; G2 includes the 8-bit
* rotation for the high 32-bit word. */
#define G1(a) \
(ctx->s[0][(a) & 0xFF]) ^ (ctx->s[1][((a) >> 8) & 0xFF]) \
^ (ctx->s[2][((a) >> 16) & 0xFF]) ^ (ctx->s[3][(a) >> 24])
#define G2(b) \
(ctx->s[1][(b) & 0xFF]) ^ (ctx->s[2][((b) >> 8) & 0xFF]) \
^ (ctx->s[3][((b) >> 16) & 0xFF]) ^ (ctx->s[0][(b) >> 24])
/* Encryption and decryption Feistel rounds. Each one calls the two g()
* macros, does the PHT, and performs the XOR and the appropriate bit
* rotations. The parameters are the round number (used to select subkeys),
* and the four 32-bit chunks of the text. */
#define ENCROUND(n, a, b, c, d) \
x = G1 (a); y = G2 (b); \
x += y; y += x + ctx->k[2 * (n) + 1]; \
(c) ^= x + ctx->k[2 * (n)]; \
(c) = ((c) >> 1) + ((c) << 31); \
(d) = (((d) << 1)+((d) >> 31)) ^ y
#define DECROUND(n, a, b, c, d) \
x = G1 (a); y = G2 (b); \
x += y; y += x; \
(d) ^= y + ctx->k[2 * (n) + 1]; \
(d) = ((d) >> 1) + ((d) << 31); \
(c) = (((c) << 1)+((c) >> 31)); \
(c) ^= (x + ctx->k[2 * (n)])
/* Encryption and decryption cycles; each one is simply two Feistel rounds
* with the 32-bit chunks re-ordered to simulate the "swap" */
#define ENCCYCLE(n) \
ENCROUND (2 * (n), a, b, c, d); \
ENCROUND (2 * (n) + 1, c, d, a, b)
#define DECCYCLE(n) \
DECROUND (2 * (n) + 1, c, d, a, b); \
DECROUND (2 * (n), a, b, c, d)
/* Macros to convert the input and output bytes into 32-bit words,
* and simultaneously perform the whitening step. INPACK packs word
* number n into the variable named by x, using whitening subkey number m.
* OUTUNPACK unpacks word number n from the variable named by x, using
* whitening subkey number m. */
#define INPACK(n, x, m) \
x = in[4 * (n)] ^ (in[4 * (n) + 1] << 8) \
^ (in[4 * (n) + 2] << 16) ^ (in[4 * (n) + 3] << 24) ^ ctx->w[m]
#define OUTUNPACK(n, x, m) \
x ^= ctx->w[m]; \
out[4 * (n)] = x; out[4 * (n) + 1] = x >> 8; \
out[4 * (n) + 2] = x >> 16; out[4 * (n) + 3] = x >> 24
/* Encrypt one block. in and out may be the same. */
int twofish_encrypt (TWOFISH_context *ctx,
const u8 *in, u8 *out)
{
/* The four 32-bit chunks of the text. */
u32 a, b, c, d;
/* Temporaries used by the round function. */
u32 x, y;
/* Input whitening and packing. */
INPACK (0, a, 0);
INPACK (1, b, 1);
INPACK (2, c, 2);
INPACK (3, d, 3);
/* Encryption Feistel cycles. */
ENCCYCLE (0);
ENCCYCLE (1);
ENCCYCLE (2);
ENCCYCLE (3);
ENCCYCLE (4);
ENCCYCLE (5);
ENCCYCLE (6);
ENCCYCLE (7);
/* Output whitening and unpacking. */
OUTUNPACK (0, c, 4);
OUTUNPACK (1, d, 5);
OUTUNPACK (2, a, 6);
OUTUNPACK (3, b, 7);
return 0;
}
/* Decrypt one block. in and out may be the same. */
int twofish_decrypt (TWOFISH_context *ctx,
const u8 *in, u8 *out)
{
/* The four 32-bit chunks of the text. */
u32 a, b, c, d;
/* Temporaries used by the round function. */
u32 x, y;
/* Input whitening and packing. */
INPACK (0, c, 4);
INPACK (1, d, 5);
INPACK (2, a, 6);
INPACK (3, b, 7);
/* Encryption Feistel cycles. */
DECCYCLE (7);
DECCYCLE (6);
DECCYCLE (5);
DECCYCLE (4);
DECCYCLE (3);
DECCYCLE (2);
DECCYCLE (1);
DECCYCLE (0);
/* Output whitening and unpacking. */
OUTUNPACK (0, a, 0);
OUTUNPACK (1, b, 1);
OUTUNPACK (2, c, 2);
OUTUNPACK (3, d, 3);
return 0;
}
/* eof */
+20
View File
@@ -0,0 +1,20 @@
#ifndef TWOFISH_H
#define TWOFISH_H
#ifdef __KERNEL__
#include <linux/types.h>
#else
#include <sys/types.h>
#endif
/* Structure for an expanded Twofish key. s contains the key-dependent
* S-boxes composed with the MDS matrix; w contains the eight "whitening"
* subkeys, K[0] through K[7]. k holds the remaining, "round" subkeys. Note
* that k[i] corresponds to what the Twofish paper calls K[i+8]. */
typedef struct {
u_int32_t s[4][256], w[8], k[32];
} TWOFISH_context;
typedef TWOFISH_context twofish_context;
int twofish_set_key(twofish_context *tf_ctx, const u_int8_t * in_key, int key_len);
int twofish_encrypt(twofish_context *tf_ctx, const u_int8_t * in, u_int8_t * out);
int twofish_decrypt(twofish_context * tf_ctx, const u_int8_t * in, u_int8_t * out);
#endif /* TWOFISH_H */
+8
View File
@@ -0,0 +1,8 @@
#ifdef __KERNEL__
#include <linux/types.h>
#else
#include <sys/types.h>
#endif
#include "twofish_cbc.h"
#include "cbc_generic.h"
CBC_IMPL_BLK16(twofish_cbc_encrypt, twofish_context, u_int8_t *, twofish_encrypt, twofish_decrypt);
+3
View File
@@ -0,0 +1,3 @@
/* Glue header */
#include "twofish.h"
int twofish_cbc_encrypt(twofish_context *ctx, const u_int8_t * in, u_int8_t * out, int ilen, const u_int8_t* iv, int encrypt);
+127
View File
@@ -0,0 +1,127 @@
LICENSE ISSUES
==============
The OpenSSL toolkit stays under a dual license, i.e. both the conditions of
the OpenSSL License and the original SSLeay license apply to the toolkit.
See below for the actual license texts. Actually both licenses are BSD-style
Open Source licenses. In case of any license issues related to OpenSSL
please contact [email protected].
OpenSSL License
---------------
/* ====================================================================
* Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* [email protected].
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* ([email protected]). This product includes software written by Tim
* Hudson ([email protected]).
*
*/
Original SSLeay License
-----------------------
/* Copyright (C) 1995-1998 Eric Young ([email protected])
* All rights reserved.
*
* This package is an SSL implementation written
* by Eric Young ([email protected]).
* The implementation was written so as to conform with Netscapes SSL.
*
* This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson ([email protected]).
*
* Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* "This product includes cryptographic software written by
* Eric Young ([email protected])"
* The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson ([email protected])"
*
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence
* [including the GNU Public Licence.]
*/
+434
View File
@@ -0,0 +1,434 @@
#!/usr/bin/perl
package alpha;
use Carp qw(croak cluck);
$label="100";
$n_debug=0;
$smear_regs=1;
$reg_alloc=1;
$align="3";
$com_start="#";
sub main'asm_init_output { @out=(); }
sub main'asm_get_output { return(@out); }
sub main'get_labels { return(@labels); }
sub main'external_label { push(@labels,@_); }
# General registers
%regs=( 'r0', '$0',
'r1', '$1',
'r2', '$2',
'r3', '$3',
'r4', '$4',
'r5', '$5',
'r6', '$6',
'r7', '$7',
'r8', '$8',
'r9', '$22',
'r10', '$23',
'r11', '$24',
'r12', '$25',
'r13', '$27',
'r14', '$28',
'r15', '$21', # argc == 5
'r16', '$20', # argc == 4
'r17', '$19', # argc == 3
'r18', '$18', # argc == 2
'r19', '$17', # argc == 1
'r20', '$16', # argc == 0
'r21', '$9', # save 0
'r22', '$10', # save 1
'r23', '$11', # save 2
'r24', '$12', # save 3
'r25', '$13', # save 4
'r26', '$14', # save 5
'a0', '$16',
'a1', '$17',
'a2', '$18',
'a3', '$19',
'a4', '$20',
'a5', '$21',
's0', '$9',
's1', '$10',
's2', '$11',
's3', '$12',
's4', '$13',
's5', '$14',
'zero', '$31',
'sp', '$30',
);
$main'reg_s0="r21";
$main'reg_s1="r22";
$main'reg_s2="r23";
$main'reg_s3="r24";
$main'reg_s4="r25";
$main'reg_s5="r26";
@reg=( '$0', '$1' ,'$2' ,'$3' ,'$4' ,'$5' ,'$6' ,'$7' ,'$8',
'$22','$23','$24','$25','$20','$21','$27','$28');
sub main'sub { &out3("subq",@_); }
sub main'add { &out3("addq",@_); }
sub main'mov { &out3("bis",$_[0],$_[0],$_[1]); }
sub main'or { &out3("bis",@_); }
sub main'bis { &out3("bis",@_); }
sub main'br { &out1("br",@_); }
sub main'ld { &out2("ldq",@_); }
sub main'st { &out2("stq",@_); }
sub main'cmpult { &out3("cmpult",@_); }
sub main'cmplt { &out3("cmplt",@_); }
sub main'bgt { &out2("bgt",@_); }
sub main'ble { &out2("ble",@_); }
sub main'blt { &out2("blt",@_); }
sub main'mul { &out3("mulq",@_); }
sub main'muh { &out3("umulh",@_); }
$main'QWS=8;
sub main'asm_add
{
push(@out,@_);
}
sub main'asm_finish
{
&main'file_end();
print &main'asm_get_output();
}
sub main'asm_init
{
($type,$fn)=@_;
$filename=$fn;
&main'asm_init_output();
&main'comment("Don't even think of reading this code");
&main'comment("It was automatically generated by $filename");
&main'comment("Which is a perl program used to generate the alpha assember.");
&main'comment("eric <eay\@cryptsoft.com>");
&main'comment("");
$filename =~ s/\.pl$//;
&main'file($filename);
}
sub conv
{
local($r)=@_;
local($v);
return($regs{$r}) if defined($regs{$r});
return($r);
}
sub main'QWPw
{
local($off,$reg)=@_;
return(&main'QWP($off*8,$reg));
}
sub main'QWP
{
local($off,$reg)=@_;
$ret="$off(".&conv($reg).")";
return($ret);
}
sub out3
{
local($name,$p1,$p2,$p3)=@_;
$p1=&conv($p1);
$p2=&conv($p2);
$p3=&conv($p3);
push(@out,"\t$name\t");
$l=length($p1)+1;
push(@out,$p1.",");
$ll=3-($l+9)/8;
$tmp1=sprintf("\t" x $ll);
push(@out,$tmp1);
$l=length($p2)+1;
push(@out,$p2.",");
$ll=3-($l+9)/8;
$tmp1=sprintf("\t" x $ll);
push(@out,$tmp1);
push(@out,&conv($p3)."\n");
}
sub out2
{
local($name,$p1,$p2,$p3)=@_;
$p1=&conv($p1);
$p2=&conv($p2);
push(@out,"\t$name\t");
$l=length($p1)+1;
push(@out,$p1.",");
$ll=3-($l+9)/8;
$tmp1=sprintf("\t" x $ll);
push(@out,$tmp1);
push(@out,&conv($p2)."\n");
}
sub out1
{
local($name,$p1)=@_;
$p1=&conv($p1);
push(@out,"\t$name\t".$p1."\n");
}
sub out0
{
push(@out,"\t$_[0]\n");
}
sub main'file
{
local($file)=@_;
local($tmp)=<<"EOF";
# DEC Alpha assember
# Generated from perl scripts contains in SSLeay
.file 1 "$file.s"
.set noat
EOF
push(@out,$tmp);
}
sub main'function_begin
{
local($func)=@_;
print STDERR "$func\n";
local($tmp)=<<"EOF";
.text
.align $align
.globl $func
.ent $func
${func}:
${func}..ng:
.frame \$30,0,\$26,0
.prologue 0
EOF
push(@out,$tmp);
$stack=0;
}
sub main'function_end
{
local($func)=@_;
local($tmp)=<<"EOF";
ret \$31,(\$26),1
.end $func
EOF
push(@out,$tmp);
$stack=0;
%label=();
}
sub main'function_end_A
{
local($func)=@_;
local($tmp)=<<"EOF";
ret \$31,(\$26),1
EOF
push(@out,$tmp);
}
sub main'function_end_B
{
local($func)=@_;
$func=$under.$func;
push(@out,"\t.end $func\n");
$stack=0;
%label=();
}
sub main'wparam
{
local($num)=@_;
if ($num < 6)
{
$num=20-$num;
return("r$num");
}
else
{ return(&main'QWP($stack+$num*8,"sp")); }
}
sub main'stack_push
{
local($num)=@_;
$stack+=$num*8;
&main'sub("sp",$num*8,"sp");
}
sub main'stack_pop
{
local($num)=@_;
$stack-=$num*8;
&main'add("sp",$num*8,"sp");
}
sub main'swtmp
{
return(&main'QWP(($_[0])*8,"sp"));
}
# Should use swtmp, which is above sp. Linix can trash the stack above esp
#sub main'wtmp
# {
# local($num)=@_;
#
# return(&main'QWP(-($num+1)*4,"esp","",0));
# }
sub main'comment
{
foreach (@_)
{
if (/^\s*$/)
{ push(@out,"\n"); }
else
{ push(@out,"\t$com_start $_ $com_end\n"); }
}
}
sub main'label
{
if (!defined($label{$_[0]}))
{
$label{$_[0]}=$label;
$label++;
}
return('$'.$label{$_[0]});
}
sub main'set_label
{
if (!defined($label{$_[0]}))
{
$label{$_[0]}=$label;
$label++;
}
# push(@out,".align $align\n") if ($_[1] != 0);
push(@out,'$'."$label{$_[0]}:\n");
}
sub main'file_end
{
}
sub main'data_word
{
push(@out,"\t.long $_[0]\n");
}
@pool_free=();
@pool_taken=();
$curr_num=0;
$max=0;
sub main'init_pool
{
local($args)=@_;
local($i);
@pool_free=();
for ($i=(14+(6-$args)); $i >= 0; $i--)
{
push(@pool_free,"r$i");
}
print STDERR "START :register pool:@pool_free\n";
$curr_num=$max=0;
}
sub main'fin_pool
{
printf STDERR "END %2d:register pool:@pool_free\n",$max;
}
sub main'GR
{
local($r)=@_;
local($i,@n,$_);
foreach (@pool_free)
{
if ($r ne $_)
{ push(@n,$_); }
else
{
$curr_num++;
$max=$curr_num if ($curr_num > $max);
}
}
@pool_free=@n;
print STDERR "GR:@pool_free\n" if $reg_alloc;
return(@_);
}
sub main'NR
{
local($num)=@_;
local(@ret);
$num=1 if $num == 0;
($#pool_free >= ($num-1)) || croak "out of registers: want $num, have @pool_free";
while ($num > 0)
{
push(@ret,pop @pool_free);
$curr_num++;
$max=$curr_num if ($curr_num > $max);
$num--
}
print STDERR "nr @ret\n" if $n_debug;
print STDERR "NR:@pool_free\n" if $reg_alloc;
return(@ret);
}
sub main'FR
{
local(@r)=@_;
local(@a,$v,$w);
print STDERR "fr @r\n" if $n_debug;
# cluck "fr @r";
for $w (@pool_free)
{
foreach $v (@r)
{
croak "double register free of $v (@pool_free)" if $w eq $v;
}
}
foreach $v (@r)
{
croak "bad argument to FR" if ($v !~ /^r\d+$/);
if ($smear_regs)
{ unshift(@pool_free,$v); }
else { push(@pool_free,$v); }
$curr_num--;
}
print STDERR "FR:@pool_free\n" if $reg_alloc;
}
1;
+342
View File
@@ -0,0 +1,342 @@
#!/usr/bin/perl
# void des_ncbc_encrypt(input, output, length, schedule, ivec, enc)
# des_cblock (*input);
# des_cblock (*output);
# long length;
# des_key_schedule schedule;
# des_cblock (*ivec);
# int enc;
#
# calls
# des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
#
#&cbc("des_ncbc_encrypt","des_encrypt",0);
#&cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt",
# 1,4,5,3,5,-1);
#&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",
# 0,4,5,3,5,-1);
#&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",
# 0,6,7,3,4,5);
#
# When doing a cipher that needs bigendian order,
# for encrypt, the iv is kept in bigendian form,
# while for decrypt, it is kept in little endian.
sub cbc
{
local($name,$enc_func,$dec_func,$swap,$iv_off,$enc_off,$p1,$p2,$p3)=@_;
# name is the function name
# enc_func and dec_func and the functions to call for encrypt/decrypt
# swap is true if byte order needs to be reversed
# iv_off is parameter number for the iv
# enc_off is parameter number for the encrypt/decrypt flag
# p1,p2,p3 are the offsets for parameters to be passed to the
# underlying calls.
&function_begin_B($name,"");
&comment("");
$in="esi";
$out="edi";
$count="ebp";
&push("ebp");
&push("ebx");
&push("esi");
&push("edi");
$data_off=4;
$data_off+=4 if ($p1 > 0);
$data_off+=4 if ($p2 > 0);
$data_off+=4 if ($p3 > 0);
&mov($count, &wparam(2)); # length
&comment("getting iv ptr from parameter $iv_off");
&mov("ebx", &wparam($iv_off)); # Get iv ptr
&mov($in, &DWP(0,"ebx","",0));# iv[0]
&mov($out, &DWP(4,"ebx","",0));# iv[1]
&push($out);
&push($in);
&push($out); # used in decrypt for iv[1]
&push($in); # used in decrypt for iv[0]
&mov("ebx", "esp"); # This is the address of tin[2]
&mov($in, &wparam(0)); # in
&mov($out, &wparam(1)); # out
# We have loaded them all, how lets push things
&comment("getting encrypt flag from parameter $enc_off");
&mov("ecx", &wparam($enc_off)); # Get enc flag
if ($p3 > 0)
{
&comment("get and push parameter $p3");
if ($enc_off != $p3)
{ &mov("eax", &wparam($p3)); &push("eax"); }
else { &push("ecx"); }
}
if ($p2 > 0)
{
&comment("get and push parameter $p2");
if ($enc_off != $p2)
{ &mov("eax", &wparam($p2)); &push("eax"); }
else { &push("ecx"); }
}
if ($p1 > 0)
{
&comment("get and push parameter $p1");
if ($enc_off != $p1)
{ &mov("eax", &wparam($p1)); &push("eax"); }
else { &push("ecx"); }
}
&push("ebx"); # push data/iv
&cmp("ecx",0);
&jz(&label("decrypt"));
&and($count,0xfffffff8);
&mov("eax", &DWP($data_off,"esp","",0)); # load iv[0]
&mov("ebx", &DWP($data_off+4,"esp","",0)); # load iv[1]
&jz(&label("encrypt_finish"));
#############################################################
&set_label("encrypt_loop");
# encrypt start
# "eax" and "ebx" hold iv (or the last cipher text)
&mov("ecx", &DWP(0,$in,"",0)); # load first 4 bytes
&mov("edx", &DWP(4,$in,"",0)); # second 4 bytes
&xor("eax", "ecx");
&xor("ebx", "edx");
&bswap("eax") if $swap;
&bswap("ebx") if $swap;
&mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call
&mov(&DWP($data_off+4,"esp","",0), "ebx"); #
&call($enc_func);
&mov("eax", &DWP($data_off,"esp","",0));
&mov("ebx", &DWP($data_off+4,"esp","",0));
&bswap("eax") if $swap;
&bswap("ebx") if $swap;
&mov(&DWP(0,$out,"",0),"eax");
&mov(&DWP(4,$out,"",0),"ebx");
# eax and ebx are the next iv.
&add($in, 8);
&add($out, 8);
&sub($count, 8);
&jnz(&label("encrypt_loop"));
###################################################################3
&set_label("encrypt_finish");
&mov($count, &wparam(2)); # length
&and($count, 7);
&jz(&label("finish"));
&xor("ecx","ecx");
&xor("edx","edx");
&mov($count,&DWP(&label("cbc_enc_jmp_table"),"",$count,4));
&jmp_ptr($count);
&set_label("ej7");
&xor("edx", "edx") if $ppro; # ppro friendly
&movb(&HB("edx"), &BP(6,$in,"",0));
&shl("edx",8);
&set_label("ej6");
&movb(&HB("edx"), &BP(5,$in,"",0));
&set_label("ej5");
&movb(&LB("edx"), &BP(4,$in,"",0));
&set_label("ej4");
&mov("ecx", &DWP(0,$in,"",0));
&jmp(&label("ejend"));
&set_label("ej3");
&movb(&HB("ecx"), &BP(2,$in,"",0));
&xor("ecx", "ecx") if $ppro; # ppro friendly
&shl("ecx",8);
&set_label("ej2");
&movb(&HB("ecx"), &BP(1,$in,"",0));
&set_label("ej1");
&movb(&LB("ecx"), &BP(0,$in,"",0));
&set_label("ejend");
&xor("eax", "ecx");
&xor("ebx", "edx");
&bswap("eax") if $swap;
&bswap("ebx") if $swap;
&mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call
&mov(&DWP($data_off+4,"esp","",0), "ebx"); #
&call($enc_func);
&mov("eax", &DWP($data_off,"esp","",0));
&mov("ebx", &DWP($data_off+4,"esp","",0));
&bswap("eax") if $swap;
&bswap("ebx") if $swap;
&mov(&DWP(0,$out,"",0),"eax");
&mov(&DWP(4,$out,"",0),"ebx");
&jmp(&label("finish"));
#############################################################
#############################################################
&set_label("decrypt",1);
# decrypt start
&and($count,0xfffffff8);
# The next 2 instructions are only for if the jz is taken
&mov("eax", &DWP($data_off+8,"esp","",0)); # get iv[0]
&mov("ebx", &DWP($data_off+12,"esp","",0)); # get iv[1]
&jz(&label("decrypt_finish"));
&set_label("decrypt_loop");
&mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes
&mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes
&bswap("eax") if $swap;
&bswap("ebx") if $swap;
&mov(&DWP($data_off,"esp","",0), "eax"); # put back
&mov(&DWP($data_off+4,"esp","",0), "ebx"); #
&call($dec_func);
&mov("eax", &DWP($data_off,"esp","",0)); # get return
&mov("ebx", &DWP($data_off+4,"esp","",0)); #
&bswap("eax") if $swap;
&bswap("ebx") if $swap;
&mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0]
&mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1]
&xor("ecx", "eax");
&xor("edx", "ebx");
&mov("eax", &DWP(0,$in,"",0)); # get old cipher text,
&mov("ebx", &DWP(4,$in,"",0)); # next iv actually
&mov(&DWP(0,$out,"",0),"ecx");
&mov(&DWP(4,$out,"",0),"edx");
&mov(&DWP($data_off+8,"esp","",0), "eax"); # save iv
&mov(&DWP($data_off+12,"esp","",0), "ebx"); #
&add($in, 8);
&add($out, 8);
&sub($count, 8);
&jnz(&label("decrypt_loop"));
############################ ENDIT #######################3
&set_label("decrypt_finish");
&mov($count, &wparam(2)); # length
&and($count, 7);
&jz(&label("finish"));
&mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes
&mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes
&bswap("eax") if $swap;
&bswap("ebx") if $swap;
&mov(&DWP($data_off,"esp","",0), "eax"); # put back
&mov(&DWP($data_off+4,"esp","",0), "ebx"); #
&call($dec_func);
&mov("eax", &DWP($data_off,"esp","",0)); # get return
&mov("ebx", &DWP($data_off+4,"esp","",0)); #
&bswap("eax") if $swap;
&bswap("ebx") if $swap;
&mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0]
&mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1]
&xor("ecx", "eax");
&xor("edx", "ebx");
# this is for when we exit
&mov("eax", &DWP(0,$in,"",0)); # get old cipher text,
&mov("ebx", &DWP(4,$in,"",0)); # next iv actually
&set_label("dj7");
&rotr("edx", 16);
&movb(&BP(6,$out,"",0), &LB("edx"));
&shr("edx",16);
&set_label("dj6");
&movb(&BP(5,$out,"",0), &HB("edx"));
&set_label("dj5");
&movb(&BP(4,$out,"",0), &LB("edx"));
&set_label("dj4");
&mov(&DWP(0,$out,"",0), "ecx");
&jmp(&label("djend"));
&set_label("dj3");
&rotr("ecx", 16);
&movb(&BP(2,$out,"",0), &LB("ecx"));
&shl("ecx",16);
&set_label("dj2");
&movb(&BP(1,$in,"",0), &HB("ecx"));
&set_label("dj1");
&movb(&BP(0,$in,"",0), &LB("ecx"));
&set_label("djend");
# final iv is still in eax:ebx
&jmp(&label("finish"));
############################ FINISH #######################3
&set_label("finish",1);
&mov("ecx", &wparam($iv_off)); # Get iv ptr
#################################################
$total=16+4;
$total+=4 if ($p1 > 0);
$total+=4 if ($p2 > 0);
$total+=4 if ($p3 > 0);
&add("esp",$total);
&mov(&DWP(0,"ecx","",0), "eax"); # save iv
&mov(&DWP(4,"ecx","",0), "ebx"); # save iv
&function_end_A($name);
&set_label("cbc_enc_jmp_table",1);
&data_word("0");
&data_word(&label("ej1"));
&data_word(&label("ej2"));
&data_word(&label("ej3"));
&data_word(&label("ej4"));
&data_word(&label("ej5"));
&data_word(&label("ej6"));
&data_word(&label("ej7"));
&set_label("cbc_dec_jmp_table",1);
&data_word("0");
&data_word(&label("dj1"));
&data_word(&label("dj2"));
&data_word(&label("dj3"));
&data_word(&label("dj4"));
&data_word(&label("dj5"));
&data_word(&label("dj6"));
&data_word(&label("dj7"));
&function_end_B($name);
}
1;
+124
View File
@@ -0,0 +1,124 @@
The perl scripts in this directory are my 'hack' to generate
multiple different assembler formats via the one origional script.
The way to use this library is to start with adding the path to this directory
and then include it.
push(@INC,"perlasm","../../perlasm");
require "x86asm.pl";
The first thing we do is setup the file and type of assember
&asm_init($ARGV[0],$0);
The first argument is the 'type'. Currently
'cpp', 'sol', 'a.out', 'elf' or 'win32'.
Argument 2 is the file name.
The reciprocal function is
&asm_finish() which should be called at the end.
There are 2 main 'packages'. x86ms.pl, which is the microsoft assembler,
and x86unix.pl which is the unix (gas) version.
Functions of interest are:
&external_label("des_SPtrans"); declare and external variable
&LB(reg); Low byte for a register
&HB(reg); High byte for a register
&BP(off,base,index,scale) Byte pointer addressing
&DWP(off,base,index,scale) Word pointer addressing
&stack_push(num) Basically a 'sub esp, num*4' with extra
&stack_pop(num) inverse of stack_push
&function_begin(name,extra) Start a function with pushing of
edi, esi, ebx and ebp. extra is extra win32
external info that may be required.
&function_begin_B(name,extra) Same as norma function_begin but no pushing.
&function_end(name) Call at end of function.
&function_end_A(name) Standard pop and ret, for use inside functions
&function_end_B(name) Call at end but with poping or 'ret'.
&swtmp(num) Address on stack temp word.
&wparam(num) Parameter number num, that was push
in C convention. This all works over pushes
and pops.
&comment("hello there") Put in a comment.
&label("loop") Refer to a label, normally a jmp target.
&set_label("loop") Set a label at this point.
&data_word(word) Put in a word of data.
So how does this all hold together? Given
int calc(int len, int *data)
{
int i,j=0;
for (i=0; i<len; i++)
{
j+=other(data[i]);
}
}
So a very simple version of this function could be coded as
push(@INC,"perlasm","../../perlasm");
require "x86asm.pl";
&asm_init($ARGV[0],"cacl.pl");
&external_label("other");
$tmp1= "eax";
$j= "edi";
$data= "esi";
$i= "ebp";
&comment("a simple function");
&function_begin("calc");
&mov( $data, &wparam(1)); # data
&xor( $j, $j);
&xor( $i, $i);
&set_label("loop");
&cmp( $i, &wparam(0));
&jge( &label("end"));
&mov( $tmp1, &DWP(0,$data,$i,4));
&push( $tmp1);
&call( "other");
&add( $j, "eax");
&pop( $tmp1);
&inc( $i);
&jmp( &label("loop"));
&set_label("end");
&mov( "eax", $j);
&function_end("calc");
&asm_finish();
The above example is very very unoptimised but gives an idea of how
things work.
There is also a cbc mode function generator in cbc.pl
&cbc( $name,
$encrypt_function_name,
$decrypt_function_name,
$true_if_byte_swap_needed,
$parameter_number_for_iv,
$parameter_number_for_encrypt_flag,
$first_parameter_to_pass,
$second_parameter_to_pass,
$third_parameter_to_pass);
So for example, given
void BF_encrypt(BF_LONG *data,BF_KEY *key);
void BF_decrypt(BF_LONG *data,BF_KEY *key);
void BF_cbc_encrypt(unsigned char *in, unsigned char *out, long length,
BF_KEY *ks, unsigned char *iv, int enc);
&cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt",1,4,5,3,-1,-1);
&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",0,4,5,3,5,-1);
&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",0,6,7,3,4,5);
+5
View File
@@ -0,0 +1,5 @@
version,v 1.1.2.1 2003/11/21 18:12:23 jjo Exp
This version of perlasm was copied from the openssl 0.9.6c distribution
The license applying to it is enclose in the LICENSE file
+118
View File
@@ -0,0 +1,118 @@
#!/usr/bin/perl
# require 'x86asm.pl';
# &asm_init("cpp","des-586.pl");
# XXX
# XXX
# main'asm_finish
sub main'asm_finish
{
&file_end();
&asm_finish_cpp() if $cpp;
print &asm_get_output();
}
sub main'asm_init
{
($type,$fn,$i386)=@_;
$filename=$fn;
$cpp=$sol=$aout=$win32=$gaswin=0;
if ( ($type eq "elf"))
{ require "x86unix.pl"; }
elsif ( ($type eq "a.out"))
{ $aout=1; require "x86unix.pl"; }
elsif ( ($type eq "gaswin"))
{ $gaswin=1; $aout=1; require "x86unix.pl"; }
elsif ( ($type eq "sol"))
{ $sol=1; require "x86unix.pl"; }
elsif ( ($type eq "cpp"))
{ $cpp=1; require "x86unix.pl"; }
elsif ( ($type eq "win32"))
{ $win32=1; require "x86ms.pl"; }
elsif ( ($type eq "win32n"))
{ $win32=1; require "x86nasm.pl"; }
else
{
print STDERR <<"EOF";
Pick one target type from
elf - linux, FreeBSD etc
a.out - old linux
sol - x86 solaris
cpp - format so x86unix.cpp can be used
win32 - Windows 95/Windows NT
win32n - Windows 95/Windows NT NASM format
EOF
exit(1);
}
&asm_init_output();
&comment("Don't even think of reading this code");
&comment("It was automatically generated by $filename");
&comment("Which is a perl program used to generate the x86 assember for");
&comment("any of elf, a.out, BSDI, Win32, gaswin (for GNU as on Win32) or Solaris");
&comment("eric <eay\@cryptsoft.com>");
&comment("");
$filename =~ s/\.pl$//;
&file($filename);
}
sub asm_finish_cpp
{
return unless $cpp;
local($tmp,$i);
foreach $i (&get_labels())
{
$tmp.="#define $i _$i\n";
}
print <<"EOF";
/* Run the C pre-processor over this file with one of the following defined
* ELF - elf object files,
* OUT - a.out object files,
* BSDI - BSDI style a.out object files
* SOL - Solaris style elf
*/
#define TYPE(a,b) .type a,b
#define SIZE(a,b) .size a,b
#if defined(OUT) || (defined(BSDI) && !defined(ELF))
$tmp
#endif
#ifdef OUT
#define OK 1
#define ALIGN 4
#endif
#if defined(BSDI) && !defined(ELF)
#define OK 1
#define ALIGN 4
#undef SIZE
#undef TYPE
#define SIZE(a,b)
#define TYPE(a,b)
#endif
#if defined(ELF) || defined(SOL)
#define OK 1
#define ALIGN 16
#endif
#ifndef OK
You need to define one of
ELF - elf systems - linux-elf, NetBSD and DG-UX
OUT - a.out systems - linux-a.out and FreeBSD
SOL - solaris systems, which are elf with strange comment lines
BSDI - a.out with a very primative version of as.
#endif
/* Let the Assembler begin :-) */
EOF
}
1;
+365
View File
@@ -0,0 +1,365 @@
#!/usr/bin/perl
package x86ms;
$label="L000";
%lb=( 'eax', 'al',
'ebx', 'bl',
'ecx', 'cl',
'edx', 'dl',
'ax', 'al',
'bx', 'bl',
'cx', 'cl',
'dx', 'dl',
);
%hb=( 'eax', 'ah',
'ebx', 'bh',
'ecx', 'ch',
'edx', 'dh',
'ax', 'ah',
'bx', 'bh',
'cx', 'ch',
'dx', 'dh',
);
sub main'asm_init_output { @out=(); }
sub main'asm_get_output { return(@out); }
sub main'get_labels { return(@labels); }
sub main'external_label { push(@labels,@_); }
sub main'LB
{
(defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
return($lb{$_[0]});
}
sub main'HB
{
(defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
return($hb{$_[0]});
}
sub main'BP
{
&get_mem("BYTE",@_);
}
sub main'DWP
{
&get_mem("DWORD",@_);
}
sub main'BC
{
return @_;
}
sub main'DWC
{
return @_;
}
sub main'stack_push
{
local($num)=@_;
$stack+=$num*4;
&main'sub("esp",$num*4);
}
sub main'stack_pop
{
local($num)=@_;
$stack-=$num*4;
&main'add("esp",$num*4);
}
sub get_mem
{
local($size,$addr,$reg1,$reg2,$idx)=@_;
local($t,$post);
local($ret)="$size PTR ";
$addr =~ s/^\s+//;
if ($addr =~ /^(.+)\+(.+)$/)
{
$reg2=&conv($1);
$addr="_$2";
}
elsif ($addr =~ /^[_a-zA-Z]/)
{
$addr="_$addr";
}
$reg1="$regs{$reg1}" if defined($regs{$reg1});
$reg2="$regs{$reg2}" if defined($regs{$reg2});
if (($addr ne "") && ($addr ne 0))
{
if ($addr !~ /^-/)
{ $ret.=$addr; }
else { $post=$addr; }
}
if ($reg2 ne "")
{
$t="";
$t="*$idx" if ($idx != 0);
$reg1="+".$reg1 if ("$reg1$post" ne "");
$ret.="[$reg2$t$reg1$post]";
}
else
{
$ret.="[$reg1$post]"
}
return($ret);
}
sub main'mov { &out2("mov",@_); }
sub main'movb { &out2("mov",@_); }
sub main'and { &out2("and",@_); }
sub main'or { &out2("or",@_); }
sub main'shl { &out2("shl",@_); }
sub main'shr { &out2("shr",@_); }
sub main'xor { &out2("xor",@_); }
sub main'xorb { &out2("xor",@_); }
sub main'add { &out2("add",@_); }
sub main'adc { &out2("adc",@_); }
sub main'sub { &out2("sub",@_); }
sub main'rotl { &out2("rol",@_); }
sub main'rotr { &out2("ror",@_); }
sub main'exch { &out2("xchg",@_); }
sub main'cmp { &out2("cmp",@_); }
sub main'lea { &out2("lea",@_); }
sub main'mul { &out1("mul",@_); }
sub main'div { &out1("div",@_); }
sub main'dec { &out1("dec",@_); }
sub main'inc { &out1("inc",@_); }
sub main'jmp { &out1("jmp",@_); }
sub main'jmp_ptr { &out1p("jmp",@_); }
sub main'je { &out1("je",@_); }
sub main'jle { &out1("jle",@_); }
sub main'jz { &out1("jz",@_); }
sub main'jge { &out1("jge",@_); }
sub main'jl { &out1("jl",@_); }
sub main'jb { &out1("jb",@_); }
sub main'jc { &out1("jc",@_); }
sub main'jnc { &out1("jnc",@_); }
sub main'jnz { &out1("jnz",@_); }
sub main'jne { &out1("jne",@_); }
sub main'jno { &out1("jno",@_); }
sub main'push { &out1("push",@_); $stack+=4; }
sub main'pop { &out1("pop",@_); $stack-=4; }
sub main'bswap { &out1("bswap",@_); &using486(); }
sub main'not { &out1("not",@_); }
sub main'call { &out1("call",'_'.$_[0]); }
sub main'ret { &out0("ret"); }
sub main'nop { &out0("nop"); }
sub out2
{
local($name,$p1,$p2)=@_;
local($l,$t);
push(@out,"\t$name\t");
$t=&conv($p1).",";
$l=length($t);
push(@out,$t);
$l=4-($l+9)/8;
push(@out,"\t" x $l);
push(@out,&conv($p2));
push(@out,"\n");
}
sub out0
{
local($name)=@_;
push(@out,"\t$name\n");
}
sub out1
{
local($name,$p1)=@_;
local($l,$t);
push(@out,"\t$name\t".&conv($p1)."\n");
}
sub conv
{
local($p)=@_;
$p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
return $p;
}
sub using486
{
return if $using486;
$using486++;
grep(s/\.386/\.486/,@out);
}
sub main'file
{
local($file)=@_;
local($tmp)=<<"EOF";
TITLE $file.asm
.386
.model FLAT
EOF
push(@out,$tmp);
}
sub main'function_begin
{
local($func,$extra)=@_;
push(@labels,$func);
local($tmp)=<<"EOF";
_TEXT SEGMENT
PUBLIC _$func
$extra
_$func PROC NEAR
push ebp
push ebx
push esi
push edi
EOF
push(@out,$tmp);
$stack=20;
}
sub main'function_begin_B
{
local($func,$extra)=@_;
local($tmp)=<<"EOF";
_TEXT SEGMENT
PUBLIC _$func
$extra
_$func PROC NEAR
EOF
push(@out,$tmp);
$stack=4;
}
sub main'function_end
{
local($func)=@_;
local($tmp)=<<"EOF";
pop edi
pop esi
pop ebx
pop ebp
ret
_$func ENDP
_TEXT ENDS
EOF
push(@out,$tmp);
$stack=0;
%label=();
}
sub main'function_end_B
{
local($func)=@_;
local($tmp)=<<"EOF";
_$func ENDP
_TEXT ENDS
EOF
push(@out,$tmp);
$stack=0;
%label=();
}
sub main'function_end_A
{
local($func)=@_;
local($tmp)=<<"EOF";
pop edi
pop esi
pop ebx
pop ebp
ret
EOF
push(@out,$tmp);
}
sub main'file_end
{
push(@out,"END\n");
}
sub main'wparam
{
local($num)=@_;
return(&main'DWP($stack+$num*4,"esp","",0));
}
sub main'swtmp
{
return(&main'DWP($_[0]*4,"esp","",0));
}
# Should use swtmp, which is above esp. Linix can trash the stack above esp
#sub main'wtmp
# {
# local($num)=@_;
#
# return(&main'DWP(-(($num+1)*4),"esp","",0));
# }
sub main'comment
{
foreach (@_)
{
push(@out,"\t; $_\n");
}
}
sub main'label
{
if (!defined($label{$_[0]}))
{
$label{$_[0]}="\$${label}${_[0]}";
$label++;
}
return($label{$_[0]});
}
sub main'set_label
{
if (!defined($label{$_[0]}))
{
$label{$_[0]}="${label}${_[0]}";
$label++;
}
if((defined $_[2]) && ($_[2] == 1))
{
push(@out,"$label{$_[0]}::\n");
}
else
{
push(@out,"$label{$_[0]}:\n");
}
}
sub main'data_word
{
push(@out,"\tDD\t$_[0]\n");
}
sub out1p
{
local($name,$p1)=@_;
local($l,$t);
push(@out,"\t$name\t ".&conv($p1)."\n");
}
+366
View File
@@ -0,0 +1,366 @@
#!/usr/bin/perl
package x86nasm;
$label="L000";
%lb=( 'eax', 'al',
'ebx', 'bl',
'ecx', 'cl',
'edx', 'dl',
'ax', 'al',
'bx', 'bl',
'cx', 'cl',
'dx', 'dl',
);
%hb=( 'eax', 'ah',
'ebx', 'bh',
'ecx', 'ch',
'edx', 'dh',
'ax', 'ah',
'bx', 'bh',
'cx', 'ch',
'dx', 'dh',
);
%regs=( 'eax', 'eax',
'ebx', 'ebx',
'ecx', 'ecx',
'edx', 'edx',
'esi', 'esi',
'edi', 'edi',
'ebp', 'ebp',
'esp', 'esp',
'mm0', 'mm0',
'mm1', 'mm1',
);
sub main::asm_init_output { @out=(); }
sub main::asm_get_output { return(@out); }
sub main::get_labels { return(@labels); }
sub main::external_label
{
push(@labels,@_);
foreach (@_) {
push(@out, "extern\t_$_\n");
}
}
sub main::LB
{
(defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
return($lb{$_[0]});
}
sub main::HB
{
(defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
return($hb{$_[0]});
}
sub main::BP
{
&get_mem("BYTE",@_);
}
sub main::DWP
{
&get_mem("DWORD",@_);
}
sub main::BC
{
return "BYTE @_";
}
sub main::DWC
{
return "DWORD @_";
}
sub main::stack_push
{
my($num)=@_;
$stack+=$num*4;
&main::sub("esp",$num*4);
}
sub main::stack_pop
{
my($num)=@_;
$stack-=$num*4;
&main::add("esp",$num*4);
}
sub get_mem
{
my($size,$addr,$reg1,$reg2,$idx)=@_;
my($t,$post);
my($ret)="[";
$addr =~ s/^\s+//;
if ($addr =~ /^(.+)\+(.+)$/)
{
if (defined($regs{$reg2})) {
$addr=join('+', &conv($1), "_$2");
} else {
$reg2=&conv($1);
$addr="_$2";
}
}
elsif ($addr =~ /^[_a-zA-Z]/)
{
$addr="_$addr";
}
$reg1="$regs{$reg1}" if defined($regs{$reg1});
$reg2="$regs{$reg2}" if defined($regs{$reg2});
if (($addr ne "") && ($addr ne 0))
{
if ($addr !~ /^-/)
{ $ret.="${addr}+"; }
else { $post=$addr; }
}
if ($reg2 ne "")
{
$t="";
$t="*$idx" if ($idx != 0);
$reg1="+".$reg1 if ("$reg1$post" ne "");
$ret.="$reg2$t$reg1$post]";
}
else
{
$ret.="$reg1$post]"
}
return($ret);
}
sub main::mov { &out2("mov",@_); }
sub main::movb { &out2("mov",@_); }
sub main::and { &out2("and",@_); }
sub main::or { &out2("or",@_); }
sub main::shl { &out2("shl",@_); }
sub main::shr { &out2("shr",@_); }
sub main::xor { &out2("xor",@_); }
sub main::xorb { &out2("xor",@_); }
sub main::add { &out2("add",@_); }
sub main::adc { &out2("adc",@_); }
sub main::sub { &out2("sub",@_); }
sub main::rotl { &out2("rol",@_); }
sub main::rotr { &out2("ror",@_); }
sub main::exch { &out2("xchg",@_); }
sub main::cmp { &out2("cmp",@_); }
sub main::lea { &out2("lea",@_); }
sub main::mul { &out1("mul",@_); }
sub main::div { &out1("div",@_); }
sub main::dec { &out1("dec",@_); }
sub main::inc { &out1("inc",@_); }
sub main::jmp { &out1("jmp",@_); }
sub main::jmp_ptr { &out1p("jmp",@_); }
# This is a bit of a kludge: declare all branches as NEAR.
sub main::je { &out1("je NEAR",@_); }
sub main::jle { &out1("jle NEAR",@_); }
sub main::jz { &out1("jz NEAR",@_); }
sub main::jge { &out1("jge NEAR",@_); }
sub main::jl { &out1("jl NEAR",@_); }
sub main::jb { &out1("jb NEAR",@_); }
sub main::jc { &out1("jc NEAR",@_); }
sub main::jnc { &out1("jnc NEAR",@_); }
sub main::jnz { &out1("jnz NEAR",@_); }
sub main::jne { &out1("jne NEAR",@_); }
sub main::jno { &out1("jno NEAR",@_); }
sub main::push { &out1("push",@_); $stack+=4; }
sub main::pop { &out1("pop",@_); $stack-=4; }
sub main::bswap { &out1("bswap",@_); &using486(); }
sub main::not { &out1("not",@_); }
sub main::call { &out1("call",'_'.$_[0]); }
sub main::ret { &out0("ret"); }
sub main::nop { &out0("nop"); }
sub out2
{
my($name,$p1,$p2)=@_;
my($l,$t);
push(@out,"\t$name\t");
$t=&conv($p1).",";
$l=length($t);
push(@out,$t);
$l=4-($l+9)/8;
push(@out,"\t" x $l);
push(@out,&conv($p2));
push(@out,"\n");
}
sub out0
{
my($name)=@_;
push(@out,"\t$name\n");
}
sub out1
{
my($name,$p1)=@_;
my($l,$t);
push(@out,"\t$name\t".&conv($p1)."\n");
}
sub conv
{
my($p)=@_;
$p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
return $p;
}
sub using486
{
return if $using486;
$using486++;
grep(s/\.386/\.486/,@out);
}
sub main::file
{
push(@out, "segment .text\n");
}
sub main::function_begin
{
my($func,$extra)=@_;
push(@labels,$func);
my($tmp)=<<"EOF";
global _$func
_$func:
push ebp
push ebx
push esi
push edi
EOF
push(@out,$tmp);
$stack=20;
}
sub main::function_begin_B
{
my($func,$extra)=@_;
my($tmp)=<<"EOF";
global _$func
_$func:
EOF
push(@out,$tmp);
$stack=4;
}
sub main::function_end
{
my($func)=@_;
my($tmp)=<<"EOF";
pop edi
pop esi
pop ebx
pop ebp
ret
EOF
push(@out,$tmp);
$stack=0;
%label=();
}
sub main::function_end_B
{
$stack=0;
%label=();
}
sub main::function_end_A
{
my($func)=@_;
my($tmp)=<<"EOF";
pop edi
pop esi
pop ebx
pop ebp
ret
EOF
push(@out,$tmp);
}
sub main::file_end
{
}
sub main::wparam
{
my($num)=@_;
return(&main::DWP($stack+$num*4,"esp","",0));
}
sub main::swtmp
{
return(&main::DWP($_[0]*4,"esp","",0));
}
# Should use swtmp, which is above esp. Linix can trash the stack above esp
#sub main::wtmp
# {
# my($num)=@_;
#
# return(&main::DWP(-(($num+1)*4),"esp","",0));
# }
sub main::comment
{
foreach (@_)
{
push(@out,"\t; $_\n");
}
}
sub main::label
{
if (!defined($label{$_[0]}))
{
$label{$_[0]}="\$${label}${_[0]}";
$label++;
}
return($label{$_[0]});
}
sub main::set_label
{
if (!defined($label{$_[0]}))
{
$label{$_[0]}="${label}${_[0]}";
$label++;
}
push(@out,"$label{$_[0]}:\n");
}
sub main::data_word
{
push(@out,"\tDD\t$_[0]\n");
}
sub out1p
{
my($name,$p1)=@_;
my($l,$t);
push(@out,"\t$name\t ".&conv($p1)."\n");
}
##
## Additional functions required for MMX and other ops
##
sub main::testb { &out2('test', @_) }
sub main::movzx { &out2('movzx', @_) }
sub main::movd { &out2('movd', @_) }
sub main::emms { &out0('emms', @_) }
+472
View File
@@ -0,0 +1,472 @@
#!/usr/bin/perl
package x86unix;
$label="L000";
$align=($main::aout)?"4":"16";
$under=($main::aout)?"_":"";
$com_start=($main::sol)?"/":"#";
sub main::asm_init_output { @out=(); }
sub main::asm_get_output { return(@out); }
sub main::get_labels { return(@labels); }
sub main::external_label { push(@labels,@_); }
if ($main::cpp)
{
$align="ALIGN";
$under="";
$com_start='/*';
$com_end='*/';
}
%lb=( 'eax', '%al',
'ebx', '%bl',
'ecx', '%cl',
'edx', '%dl',
'ax', '%al',
'bx', '%bl',
'cx', '%cl',
'dx', '%dl',
);
%hb=( 'eax', '%ah',
'ebx', '%bh',
'ecx', '%ch',
'edx', '%dh',
'ax', '%ah',
'bx', '%bh',
'cx', '%ch',
'dx', '%dh',
);
%regs=( 'eax', '%eax',
'ebx', '%ebx',
'ecx', '%ecx',
'edx', '%edx',
'esi', '%esi',
'edi', '%edi',
'ebp', '%ebp',
'esp', '%esp',
'mm0', '%mm0',
'mm1', '%mm1',
);
%reg_val=(
'eax', 0x00,
'ebx', 0x03,
'ecx', 0x01,
'edx', 0x02,
'esi', 0x06,
'edi', 0x07,
'ebp', 0x05,
'esp', 0x04,
);
sub main::LB
{
(defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
return($lb{$_[0]});
}
sub main::HB
{
(defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
return($hb{$_[0]});
}
sub main::DWP
{
local($addr,$reg1,$reg2,$idx)=@_;
$ret="";
$addr =~ s/(^|[+ \t])([A-Za-z_]+[A-Za-z0-9_]+)($|[+ \t])/$1$under$2$3/;
$reg1="$regs{$reg1}" if defined($regs{$reg1});
$reg2="$regs{$reg2}" if defined($regs{$reg2});
$ret.=$addr if ($addr ne "") && ($addr ne 0);
if ($reg2 ne "")
{
if($idx ne "")
{ $ret.="($reg1,$reg2,$idx)"; }
else
{ $ret.="($reg1,$reg2)"; }
}
else
{ $ret.="($reg1)" }
return($ret);
}
sub main::BP
{
return(&main::DWP(@_));
}
sub main::BC
{
return @_;
}
sub main::DWC
{
return @_;
}
#sub main::BP
# {
# local($addr,$reg1,$reg2,$idx)=@_;
#
# $ret="";
#
# $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
# $reg1="$regs{$reg1}" if defined($regs{$reg1});
# $reg2="$regs{$reg2}" if defined($regs{$reg2});
# $ret.=$addr if ($addr ne "") && ($addr ne 0);
# if ($reg2 ne "")
# { $ret.="($reg1,$reg2,$idx)"; }
# else
# { $ret.="($reg1)" }
# return($ret);
# }
sub main::mov { &out2("movl",@_); }
sub main::movb { &out2("movb",@_); }
sub main::and { &out2("andl",@_); }
sub main::or { &out2("orl",@_); }
sub main::shl { &out2("sall",@_); }
sub main::shr { &out2("shrl",@_); }
sub main::xor { &out2("xorl",@_); }
sub main::xorb { &out2("xorb",@_); }
sub main::add { &out2("addl",@_); }
sub main::adc { &out2("adcl",@_); }
sub main::sub { &out2("subl",@_); }
sub main::rotl { &out2("roll",@_); }
sub main::rotr { &out2("rorl",@_); }
sub main::exch { &out2("xchg",@_); }
sub main::cmp { &out2("cmpl",@_); }
sub main::lea { &out2("leal",@_); }
sub main::mul { &out1("mull",@_); }
sub main::div { &out1("divl",@_); }
sub main::jmp { &out1("jmp",@_); }
sub main::jmp_ptr { &out1p("jmp",@_); }
sub main::je { &out1("je",@_); }
sub main::jle { &out1("jle",@_); }
sub main::jne { &out1("jne",@_); }
sub main::jnz { &out1("jnz",@_); }
sub main::jz { &out1("jz",@_); }
sub main::jge { &out1("jge",@_); }
sub main::jl { &out1("jl",@_); }
sub main::jb { &out1("jb",@_); }
sub main::jc { &out1("jc",@_); }
sub main::jnc { &out1("jnc",@_); }
sub main::jno { &out1("jno",@_); }
sub main::dec { &out1("decl",@_); }
sub main::inc { &out1("incl",@_); }
sub main::push { &out1("pushl",@_); $stack+=4; }
sub main::pop { &out1("popl",@_); $stack-=4; }
sub main::not { &out1("notl",@_); }
sub main::call { &out1("call",$under.$_[0]); }
sub main::ret { &out0("ret"); }
sub main::nop { &out0("nop"); }
# The bswapl instruction is new for the 486. Emulate if i386.
sub main::bswap
{
if ($main::i386)
{
&main::comment("bswapl @_");
&main::exch(main::HB(@_),main::LB(@_));
&main::rotr(@_,16);
&main::exch(main::HB(@_),main::LB(@_));
}
else
{
&out1("bswapl",@_);
}
}
sub out2
{
local($name,$p1,$p2)=@_;
local($l,$ll,$t);
local(%special)=( "roll",0xD1C0,"rorl",0xD1C8,
"rcll",0xD1D0,"rcrl",0xD1D8,
"shll",0xD1E0,"shrl",0xD1E8,
"sarl",0xD1F8);
if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
{
$op=$special{$name}|$reg_val{$p1};
$tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
$tmp2=sprintf(".byte %d\t",$op &0xff);
push(@out,$tmp1);
push(@out,$tmp2);
$p2=&conv($p2);
$p1=&conv($p1);
&main::comment("$name $p2 $p1");
return;
}
push(@out,"\t$name\t");
$t=&conv($p2).",";
$l=length($t);
push(@out,$t);
$ll=4-($l+9)/8;
$tmp1=sprintf("\t" x $ll);
push(@out,$tmp1);
push(@out,&conv($p1)."\n");
}
sub out1
{
local($name,$p1)=@_;
local($l,$t);
local(%special)=("bswapl",0x0FC8);
if ((defined($special{$name})) && defined($regs{$p1}))
{
$op=$special{$name}|$reg_val{$p1};
$tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
$tmp2=sprintf(".byte %d\t",$op &0xff);
push(@out,$tmp1);
push(@out,$tmp2);
$p2=&conv($p2);
$p1=&conv($p1);
&main::comment("$name $p2 $p1");
return;
}
push(@out,"\t$name\t".&conv($p1)."\n");
}
sub out1p
{
local($name,$p1)=@_;
local($l,$t);
push(@out,"\t$name\t*".&conv($p1)."\n");
}
sub out0
{
push(@out,"\t$_[0]\n");
}
sub conv
{
local($p)=@_;
# $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
$p=$regs{$p} if (defined($regs{$p}));
$p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
$p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
return $p;
}
sub main::file
{
local($file)=@_;
local($tmp)=<<"EOF";
.file "$file.s"
.version "01.01"
EOF
# Removed the next line from previous infile
#gcc2_compiled.:
push(@out,$tmp);
}
sub main::function_begin
{
local($func)=@_;
&main::external_label($func);
$func=$under.$func;
local($tmp)=<<"EOF";
.text
.align $align
.globl $func
EOF
push(@out,$tmp);
if ($main::cpp)
{ $tmp=push(@out,"\tTYPE($func,\@function)\n"); }
elsif ($main::gaswin)
{ $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
else { $tmp=push(@out,"\t.type\t$func,\@function\n"); }
push(@out,"$func:\n");
$tmp=<<"EOF";
pushl %ebp
pushl %ebx
pushl %esi
pushl %edi
EOF
push(@out,$tmp);
$stack=20;
}
sub main::function_begin_B
{
local($func,$extra)=@_;
&main::external_label($func);
$func=$under.$func;
local($tmp)=<<"EOF";
.text
.align $align
.globl $func
EOF
push(@out,$tmp);
if ($main::cpp)
{ push(@out,"\tTYPE($func,\@function)\n"); }
elsif ($main::gaswin)
{ $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
else { push(@out,"\t.type $func,\@function\n"); }
push(@out,"$func:\n");
$stack=4;
}
sub main::function_end
{
local($func)=@_;
$func=$under.$func;
local($tmp)=<<"EOF";
popl %edi
popl %esi
popl %ebx
popl %ebp
ret
.${func}_end:
EOF
push(@out,$tmp);
if ($main::cpp)
{ push(@out,"\tSIZE($func,.${func}_end-$func)\n"); }
elsif ($main::gaswin)
{ $tmp=push(@out,"\t.align 4\n"); }
else { push(@out,"\t.size\t$func,.${func}_end-$func\n"); }
push(@out,".ident \"$func\"\n");
$stack=0;
%label=();
}
sub main::function_end_A
{
local($func)=@_;
local($tmp)=<<"EOF";
popl %edi
popl %esi
popl %ebx
popl %ebp
ret
EOF
push(@out,$tmp);
}
sub main::function_end_B
{
local($func)=@_;
$func=$under.$func;
push(@out,".L_${func}_end:\n");
if ($main::cpp)
{ push(@out,"\tSIZE($func,.L_${func}_end-$func)\n"); }
elsif ($main::gaswin)
{ push(@out,"\t.align 4\n"); }
else { push(@out,"\t.size\t$func,.L_${func}_end-$func\n"); }
push(@out,".ident \"desasm.pl\"\n");
$stack=0;
%label=();
}
sub main::wparam
{
local($num)=@_;
return(&main::DWP($stack+$num*4,"esp","",0));
}
sub main::stack_push
{
local($num)=@_;
$stack+=$num*4;
&main::sub("esp",$num*4);
}
sub main::stack_pop
{
local($num)=@_;
$stack-=$num*4;
&main::add("esp",$num*4);
}
sub main::swtmp
{
return(&main::DWP($_[0]*4,"esp","",0));
}
# Should use swtmp, which is above esp. Linix can trash the stack above esp
#sub main::wtmp
# {
# local($num)=@_;
#
# return(&main::DWP(-($num+1)*4,"esp","",0));
# }
sub main::comment
{
foreach (@_)
{
if (/^\s*$/)
{ push(@out,"\n"); }
else
{ push(@out,"\t$com_start $_ $com_end\n"); }
}
}
sub main::label
{
if (!defined($label{$_[0]}))
{
$label{$_[0]}=".${label}${_[0]}";
$label++;
}
return($label{$_[0]});
}
sub main::set_label
{
if (!defined($label{$_[0]}))
{
$label{$_[0]}=".${label}${_[0]}";
$label++;
}
push(@out,".align $align\n") if ($_[1] != 0);
push(@out,"$label{$_[0]}:\n");
}
sub main::file_end
{
}
sub main::data_word
{
push(@out,"\t.long $_[0]\n");
}
##
## Additional functions required for MMX and other ops
##
sub main::testb { &out2('testb', @_) }
sub main::movzx { &out2('movzx', @_) }
sub main::movd { &out2('movd', @_) }
sub main::emms { &out0('emms', @_) }
+3
View File
@@ -0,0 +1,3 @@
des_opts
destest
speed
+245
View File
@@ -0,0 +1,245 @@
ifndef FREESWANSRCDIR
FREESWANSRCDIR=../..
endif
include ${FREESWANSRCDIR}/Makefile.inc
KLIPSD=${FREESWANSRCDIR}/linux
SRCDIR=${KLIPSD}/crypto/ciphers/des
VPATH =${SRCDIR}
# You must select the correct terminal control system to be used to
# turn character echo off when reading passwords. There a 5 systems
# SGTTY - the old BSD system
# TERMIO - most system V boxes
# TERMIOS - SGI (ala IRIX).
# VMS - the DEC operating system
# MSDOS - we all know what it is :-)
# read_pwd.c makes a reasonable guess at what is correct.
# Targets
# make - twidle the options yourself :-)
# make cc - standard cc options
# make gcc - standard gcc options
# make x86-elf - linux-elf etc
# make x86-out - linux-a.out, FreeBSD etc
# make x86-solaris
# make x86-bdsi
# If you are on a DEC Alpha, edit des.h and change the DES_LONG
# define to 'unsigned int'. I have seen this give a %20 speedup.
OPTS0= -DLIBDES_LIT -DRAND -DTERMIO #-DNOCONST
# Version 1.94 has changed the strings_to_key function so that it is
# now compatible with MITs when the string is longer than 8 characters.
# If you wish to keep the old version, uncomment the following line.
# This will affect the -E/-D options on des(1).
#OPTS1= -DOLD_STR_TO_KEY
# There are 4 possible performance options
# -DDES_PTR
# -DDES_RISC1
# -DDES_RISC2 (only one of DES_RISC1 and DES_RISC2)
# -DDES_UNROLL
# after the initial build, run 'des_opts' to see which options are best
# for your platform. There are some listed in options.txt
#OPTS2= -DDES_PTR
#OPTS3= -DDES_RISC1 # or DES_RISC2
#OPTS4= -DDES_UNROLL
OPTS= $(OPTS0) $(OPTS1) $(OPTS2) $(OPTS3) $(OPTS4)
MAKE=make -f Makefile
#CC=cc
#CFLAG= -O
#CC=gcc
#CFLAG= -O4 -funroll-loops -fomit-frame-pointer
# normally overridden by FreeS/WAN Makefiles anyway
CFLAG= -O3 -fomit-frame-pointer -I${KLIPSD}/include -I${SRCDIR}
CFLAGS=$(OPTS) $(CFLAG) $(USERCOMPILE)
CPP=$(CC) -E
# Assember version of des_encrypt*().
DES_ENC=des_enc.o fcrypt_b.o # normal C version
#DES_ENC=asm/dx86-elf.o asm/yx86-elf.o # elf format x86
#DES_ENC=asm/dx86-out.o asm/yx86-out.o # a.out format x86
#DES_ENC=asm/dx86-sol.o asm/yx86-sol.o # solaris format x86
#DES_ENC=asm/dx86bsdi.o asm/yx86basi.o # bsdi format x86
LIBDIR=$(DESTDIR)$(INC_USRLOCAL)/lib
INCDIR=$(DESTDIR)$(INC_USRLOCAL)/include
MANDIR=$(MANTREE)
MAN1=1
MAN3=3
SHELL=/bin/sh
MAN1=1
MAN3=3
SHELL=/bin/sh
OBJ_LIT=cbc_enc.o ecb_enc.o $(DES_ENC) fcrypt.o set_key.o
OBJ_FULL=cbc_cksm.o $(OBJ_LIT) pcbc_enc.o \
xcbc_enc.o qud_cksm.o \
cfb64ede.o cfb64enc.o cfb_enc.o ecb3_enc.o \
enc_read.o enc_writ.o ofb64ede.o ofb64enc.o ofb_enc.o \
rand_key.o read_pwd.o read2pwd.o rpc_enc.o str2key.o supp.o
GENERAL_LIT=COPYRIGHT INSTALL README VERSION Makefile des_crypt.man \
des.doc options.txt asm
GENERAL_FULL=$(GENERAL_LIT) FILES Imakefile times vms.com KERBEROS MODES.DES \
des.man DES.pm DES.pod DES.xs Makefile.PL dess.S des3s.S \
Makefile.uni typemap t Makefile.ssl makefile.bc Makefile.lit \
des.org des_locl.org
TESTING_LIT= destest speed des_opts
TESTING_FULL= rpw $(TESTING_LIT)
TESTING_SRC_LIT=destest.c speed.c des_opts.c
TESTING_SRC_FULL=rpw.c $(TESTING_SRC_LIT)
HEADERS_LIT=des_ver.h des.h des_locl.h podd.h sk.h spr.h
HEADERS_FULL= $(HEADERS_LIT) rpc_des.h
LIBDES_LIT=cbc_enc.c ecb_enc.c fcrypt.c set_key.c des_enc.c fcrypt_b.c
LIBDES_FULL= cbc_cksm.c pcbc_enc.c qud_cksm.c \
cfb64ede.c cfb64enc.c cfb_enc.c ecb3_enc.c \
enc_read.c enc_writ.c ofb64ede.c ofb64enc.c ofb_enc.c \
rand_key.c rpc_enc.c str2key.c supp.c \
xcbc_enc.c $(LIBDES_LIT) read_pwd.c read2pwd.c
PERL= des.pl testdes.pl doIP doPC1 doPC2 PC1 PC2 shifts.pl
OBJ= $(OBJ_LIT)
GENERAL=$(GENERAL_LIT)
TESTING=$(TESTING_LIT)
TESTING_SRC=$(TESTING_SRC_LIT)
HEADERS=$(HEADERS_LIT)
LIBDES= $(LIBDES_LIT)
ALL= $(GENERAL) $(TESTING_SRC) $(LIBDES) $(PERL) $(HEADERS)
DLIB= libdes.a
.PHONY: all cc gcc x86-elf x86-out x86-solaris x86-bsdi test tar_lit \
tar shar depend clean dclean install check checkprograms
all: $(DLIB) $(TESTING)
programs: $(DLIB)
cc:
$(MAKE) CC=cc CFLAGS="-O $(OPTS) $(CFLAG)" all
gcc:
$(MAKE) CC=gcc CFLAGS="-O3 -fomit-frame-pointer $(OPTS) $(CFLAG)" all
x86-elf:
$(MAKE) DES_ENC='asm/dx86-elf.o asm/yx86-elf.o' CC='$(CC)' CFLAGS="-DELF $(OPTS) $(CFLAG)" all
x86-out:
$(MAKE) DES_ENC='asm/dx86-out.o asm/yx86-out.o' CC='$(CC)' CFLAGS="-DOUT $(OPTS) $(CFLAG)" all
x86-solaris:
$(MAKE) DES_ENC='asm/dx86-sol.o asm/yx86-sol.o' CC='$(CC)' CFLAGS="-DSOL $(OPTS) $(CFLAG)" all
x86-bsdi:
$(MAKE) DES_ENC='asm/dx86bsdi.o asm/yx86bsdi.o' CC='$(CC)' CFLAGS="-DBSDI $(OPTS) $(CFLAG)" all
# elf
asm/dx86-elf.o: asm/dx86unix.S
$(CPP) -DELF asm/dx86unix.S | $(AS) -o asm/dx86-elf.o
asm/yx86-elf.o: asm/yx86unix.S
$(CPP) -DELF asm/yx86unix.S | $(AS) -o asm/yx86-elf.o
# solaris
asm/dx86-sol.o: asm/dx86unix.S
$(CC) -E -DSOL asm/dx86unix.S | sed 's/^#.*//' > asm/dx86-sol.s
as -o asm/dx86-sol.o asm/dx86-sol.s
rm -f asm/dx86-sol.s
asm/yx86-sol.o: asm/yx86unix.S
$(CC) -E -DSOL asm/yx86unix.S | sed 's/^#.*//' > asm/yx86-sol.s
as -o asm/yx86-sol.o asm/yx86-sol.s
rm -f asm/yx86-sol.s
# a.out
asm/dx86-out.o: asm/dx86unix.S
$(CPP) -DOUT asm/dx86unix.S | $(AS) -o asm/dx86-out.o
asm/yx86-out.o: asm/yx86unix.S
$(CPP) -DOUT asm/yx86unix.S | $(AS) -o asm/yx86-out.o
# bsdi
asm/dx86bsdi.o: asm/dx86unix.S
$(CPP) -DBSDI asm/dx86unix.S | $(AS) -o asm/dx86bsdi.o
asm/yx86bsdi.o: asm/yx86unix.S
$(CPP) -DBSDI asm/yx86unix.S | $(AS) -o asm/yx86bsdi.o
asm/dx86unix.S:
(cd asm; perl des-586.pl cpp >dx86unix.S)
asm/yx86unix.S:
(cd asm; perl crypt586.pl cpp >yx86unix.S)
test: all
./destest
$(DLIB): $(OBJ)
rm -f $(DLIB)
$(AR) crs $(DLIB) $(OBJ)
des_opts: des_opts.o $(DLIB)
$(CC) $(CFLAGS) -o des_opts des_opts.o $(DLIB)
destest: destest.o $(DLIB)
$(CC) $(CFLAGS) -o destest destest.o $(DLIB)
rpw: rpw.o $(DLIB)
$(CC) $(CFLAGS) -o rpw rpw.o $(DLIB)
speed: speed.o $(DLIB)
$(CC) $(CFLAGS) -o speed speed.o $(DLIB)
des: des.o $(DLIB)
$(CC) $(CFLAGS) -o des des.o $(DLIB)
tags:
ctags $(TESTING_SRC) $(LIBDES)
tar_lit:
/bin/mv Makefile Makefile.tmp
/bin/cp Makefile.lit Makefile
tar chf libdes-l.tar $(LIBDES_LIT) $(HEADERS_LIT) \
$(GENERAL_LIT) $(TESTING_SRC_LIT)
/bin/rm -f Makefile
/bin/mv Makefile.tmp Makefile
tar:
tar chf libdes.tar $(ALL)
shar:
shar $(ALL) >libdes.shar
depend:
makedepend $(LIBDES) $(TESTING_SRC)
clean:
/bin/rm -f *.o tags core $(TESTING) $(DLIB) .nfs* *.old *.bak asm/*.o \
asm/*.S
dclean:
sed -e '/^# DO NOT DELETE THIS LINE/ q' Makefile >Makefile.new
mv -f Makefile.new Makefile
install install_file_list:
@true
check:
echo no checks in lib right now.
checkprograms:
# DO NOT DELETE THIS LINE -- make depend depends on it.
+9
View File
@@ -0,0 +1,9 @@
try
try1a
try2
try3
try4
try4a
try6
try7
version.c
+174
View File
@@ -0,0 +1,174 @@
# FreeS/WAN library
# Copyright (C) 1998-2001 Henry Spencer.
#
# 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. See <http://www.fsf.org/copyleft/gpl.txt>.
#
# 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.
#
# RCSID $Id: Makefile,v 1.2 2004/03/22 21:53:17 as Exp $
FREESWANSRCDIR=../..
include ${FREESWANSRCDIR}/Makefile.inc
include ${FREESWANSRCDIR}/Makefile.ver
MANDIR=$(MANTREE)/man3
SRCS=addrtoa.c addrtot.c addrtypeof.c anyaddr.c atoaddr.c \
atoasr.c atosa.c atosubnet.c atoul.c copyright.c datatot.c \
goodmask.c initaddr.c initsaid.c initsubnet.c keyblobtoid.c \
optionsfrom.c pfkey_v2_build.c pfkey_v2_ext_bits.c pfkey_v2_parse.c \
pfkey_v2_debug.c prng.c \
portof.c rangetoa.c rangetosubnet.c sameaddr.c \
satoa.c satot.c subnetof.c subnettoa.c subnettot.c \
subnettypeof.c ttoaddr.c ttodata.c ttoprotoport.c \
ttosa.c ttosubnet.c ttoul.c ultoa.c ultot.c
OBJS=${SRCS:.c=.o} version.o
KLIPSD=${FREESWANSRCDIR}/linux/include
SRCDIR=${FREESWANSRCDIR}/linux/lib/libfreeswan
VPATH = ${SRCDIR}
HDRS=${KLIPSD}/freeswan.h ${SRCDIR}/internal.h
LIB=libfreeswan.a
# Original flags
CFLAGS=-I. -I${SRCDIR} -I${KLIPSD} -I${FREESWANSRCDIR} $(USERCOMPILE)
CFLAGS+= -Wall
#CFLAGS+= -Wconversion
#CFLAGS+= -Wmissing-prototypes
CFLAGS+= -Wpointer-arith
CFLAGS+= -Wcast-qual
#CFLAGS+= -Wmissing-declarations
CFLAGS+= -Wstrict-prototypes
#CFLAGS+= -pedantic
#CFLAGS+= -W
#CFLAGS+= -Wwrite-strings
CFLAGS+= -Wbad-function-cast
CFLAGS+= -DNAT_TRAVERSAL
ARFLAGS=crvs
EXTHDRS=des.h
EXTLIBS=libdes.a
MANS=anyaddr.3 atoaddr.3 atoasr.3 atosa.3 atoul.3 goodmask.3 \
initaddr.3 initsubnet.3 optionsfrom.3 portof.3 rangetosubnet.3 \
sameaddr.3 subnetof.3 ttoaddr.3 ttodata.3 ttosa.3 ttoul.3 version.3
.PHONY: all install clean l t lt tar check depend checkprograms
all: $(LIB)
programs: $(LIB)
install:
@mkdir -p $(MANDIR)
@for f in $(MANS) ; \
do \
$(INSTALL) $(INSTMANFLAGS) $(SRCDIR)/$$f $(MANDIR)/ipsec_$$f || exit 1 ; \
done
@$(FREESWANSRCDIR)/packaging/utils/manlink $(foreach man, $(MANS), ${SRCDIR}/$(man)) | \
while read from to; \
do \
ln -s -f ipsec_$$from $(MANDIR)/$$to; \
done
install_file_list:
@for f in $(MANS) ; \
do \
echo $(MANDIR)/ipsec_$$f;\
done;
@$(FREESWANSRCDIR)/packaging/utils/manlink $(foreach man, $(MANS), ${SRCDIR}/$(man)) | \
while read from to; \
do \
echo $(MANDIR)/$$to; \
done
$(LIB): $(OBJS)
$(AR) $(ARFLAGS) $(LIB) $(OBJS)
$(OBJS): $(HDRS)
# build version.c using version number from Makefile.ver
version.c: ${SRCDIR}/version.in.c ${FREESWANSRCDIR}/Makefile.ver
sed '/"/s/xxx/$(IPSECVERSION)/' ${SRCDIR}/version.in.c >$@
#libdes.a: ../libdes/libdes.a
# ln -f -s ../libdes/libdes.a
#
# yes, that's CFLAG=, not CFLAGS=
#../libdes/libdes.a:
# cd ../libdes ; \
# if test " `arch | sed 's/^i[3456]/x/'`" = " x86" ; \
# then $(MAKE) CC='$(CC)' CFLAG='$(CFLAGS)' TESTING='' x86-elf ; \
# else $(MAKE) CC='$(CC)' CFLAG='$(CFLAGS)' libdes.a ; \
# fi
clean:
rm -f $(LIB) *.o try* core *.core $(EXTHDRS) $(EXTLIBS) version.c
# developer-only stuff
l:
$(MAKE) $(LIB) ARFLAGS=crv CFLAGS=-O
$(RANLIB) $(LIB)
t: $(LIB)
ln -f -s ${SRCDIR}/atosubnet.c try.c
${CC} ${CFLAGS} -DATOSUBNET_MAIN try.c $(LIB) -o try
./try -r
ln -f -s ${SRCDIR}/ttosubnet.c try1a.c
${CC} ${CFLAGS} -DTTOSUBNET_MAIN try1a.c $(LIB) -o try1a
./try1a -r
ln -f -s ${SRCDIR}/ttodata.c try2.c
${CC} ${CFLAGS} -DTTODATA_MAIN try2.c $(LIB) -o try2
./try2 -r
ln -f -s ${SRCDIR}/atoasr.c try3.c
${CC} ${CFLAGS} -DATOASR_MAIN try3.c $(LIB) -o try3
./try3 -r
ln -f -s ${SRCDIR}/atosa.c try4.c
${CC} ${CFLAGS} -DATOSA_MAIN try4.c $(LIB) -o try4
./try4 -r
ln -f -s ${SRCDIR}/ttosa.c try4a.c
${CC} ${CFLAGS} -DTTOSA_MAIN try4a.c $(LIB) -o try4a
./try4a -r
ln -f -s ${SRCDIR}/rangetosubnet.c try6.c
${CC} ${CFLAGS} -DRANGETOSUBNET_MAIN try6.c $(LIB) -o try6
./try6 -r
ln -f -s ${SRCDIR}/addrtot.c try7.c
${CC} ${CFLAGS} -DADDRTOT_MAIN try7.c $(LIB) -o try7
./try7 -r
lt: $(LIB)
$(MAKE) t
cp optionsfrom.c try5.c
cc -DTEST try5.c $(LIB) -o try5
echo --foo --bar >try5in1
echo --optionsfrom >>try5in1
echo try5in2 >>try5in1
echo --foo >try5in2
./try5 --foo --bar --optionsfrom try5in1 --bar something
tar: clean
tar -cvf /tmp/lib.tar Makefile [a-z]*
check:
echo no checks in lib right now.
depend:
makedepend -Y -- $(CFLAGS) -- $(SRCS)
checkprograms:
# DO NOT DELETE
+1
View File
@@ -0,0 +1 @@
version.c
+96
View File
@@ -0,0 +1,96 @@
# FreeS/WAN library
# Copyright (C) 2003 Michael Richardson <[email protected]>
#
# 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. See <http://www.fsf.org/copyleft/gpl.txt>.
#
# 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.
#
# RCSID $Id: Makefile,v 1.1 2004/03/15 20:35:24 as Exp $
FREESWANSRCDIR=../..
include ${FREESWANSRCDIR}/Makefile.inc
include ${FREESWANSRCDIR}/Makefile.ver
MANDIR=$(MANTREE)/man3
SRCS=policyquery.c cgipolicy.c
OBJS=${SRCS:.c=.o} version.o
KLIPSD=${FREESWANSRCDIR}/linux/include
LIB=libipsecpolicy.a
# Original flags
CFLAGS=-I. -I${KLIPSD} -I${FREESWANSRCDIR} $(USERCOMPILE)
CFLAGS+= -Wall
CFLAGS+= -Wpointer-arith
CFLAGS+= -Wcast-qual
CFLAGS+= -Wstrict-prototypes
CFLAGS+= -Wbad-function-cast
MANS=
.PHONY: all install clean l t lt tar check depend checkprograms
all: $(LIB)
programs: $(LIB)
install:
@mkdir -p $(MANDIR)
@for f in $(MANS) ; \
do \
$(INSTALL) $(INSTMANFLAGS) $(SRCDIR)/$$f $(MANDIR)/ipsec_$$f || exit 1 ; \
done
@$(FREESWANSRCDIR)/packaging/utils/manlink $(foreach man, $(MANS), ${SRCDIR}/$(man)) | \
while read from to; \
do \
ln -s -f ipsec_$$from $(MANDIR)/$$to; \
done
install_file_list:
@for f in $(MANS) ; \
do \
echo $(MANDIR)/ipsec_$$f;\
done;
@$(FREESWANSRCDIR)/packaging/utils/manlink $(foreach man, $(MANS), ${SRCDIR}/$(man)) | \
while read from to; \
do \
echo $(MANDIR)/$$to; \
done
$(LIB): $(OBJS)
$(AR) $(ARFLAGS) $(LIB) $(OBJS)
$(OBJS): $(HDRS)
# build version.c using version number from Makefile.ver
version.c: version.in.c ${FREESWANSRCDIR}/Makefile.ver
sed '/"/s/xxx/$(IPSECVERSION)/' version.in.c >$@
clean:
rm -f $(LIB) *.o try* core *.core $(EXTHDRS) $(EXTLIBS) version.c
tar: clean
tar -cvf /tmp/lib.tar Makefile [a-z]*
check:
echo no checks in lib right now.
depend:
makedepend -Y -- $(CFLAGS) -- $(SRCS)
checkprograms:
# DO NOT DELETE
+77
View File
@@ -0,0 +1,77 @@
/* routines that interface with pluto to get policy information
* Copyright (C) 2003 Michael Richardson <[email protected]>
*
* 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. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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.
*
* RCSID $Id: cgipolicy.c,v 1.1 2004/03/15 20:35:24 as Exp $
*/
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <freeswan.h>
#include <freeswan/ipsec_policy.h>
#include "libipsecpolicy.h"
/*
* this version is appropriate for when one is called from a perl CGI,
* running under Apache. It extracts the appropriate things out of standard
* CGI environment variables, namely:
* $SERVER_ADDR us
* $REMOTE_ADDR them
*/
err_t ipsec_policy_cgilookup(struct ipsec_policy_cmd_query *result)
{
err_t ret;
char *us, *them;
/* clear it all out */
memset(result, 0, sizeof(*result));
/* setup it up */
result->head.ipm_version = IPSEC_POLICY_MSG_REVISION;
result->head.ipm_msg_len = sizeof(*result);
result->head.ipm_msg_type = IPSEC_CMD_QUERY_HOSTPAIR;
result->head.ipm_msg_seq = ipsec_policy_seq();
us = getenv("SERVER_ADDR");
them = getenv("REMOTE_ADDR");
if(!us || !them) {
return "$SERVER_ADDR and $REMOTE_ADDR must be set";
}
ret = ttoaddr(us, 0, AF_INET, &result->query_local);
if(ret != NULL) {
return ret;
}
ret = ttoaddr(them, 0, AF_INET, &result->query_remote);
if(ret != NULL) {
return ret;
}
return ipsec_policy_sendrecv((unsigned char *)result, sizeof(*result));
}
+4
View File
@@ -0,0 +1,4 @@
extern u_int32_t ipsec_policy_seq(void);
+167
View File
@@ -0,0 +1,167 @@
/* routines that interface with pluto to get policy information
* Copyright (C) 2003 Michael Richardson <[email protected]>
*
* 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. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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.
*
* RCSID $Id: policyquery.c,v 1.1 2004/03/15 20:35:25 as Exp $
*/
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <freeswan.h>
#include <freeswan/ipsec_policy.h>
#include "libipsecpolicy.h"
static int policy_query_socket = -1;
static u_int32_t policy_seq = 1;
u_int32_t ipsec_policy_seq(void)
{
return ++policy_seq;
}
err_t ipsec_policy_init(void)
{
struct sockaddr_un sn;
if(policy_query_socket != -1) {
return NULL;
}
policy_query_socket = socket(PF_UNIX, SOCK_STREAM, 0);
if(policy_query_socket == -1) {
return "failed to open policy socket";
}
/* now connect it */
sn.sun_family = AF_UNIX;
strcpy(sn.sun_path, IPSEC_POLICY_SOCKET);
if(connect(policy_query_socket, (struct sockaddr *)&sn, sizeof(sn)) != 0) {
int saveerrno = errno;
close(policy_query_socket);
policy_query_socket=-1;
errno = saveerrno;
return "failed to connect policy socket";
}
/* okay, I think we are done */
return NULL;
}
err_t ipsec_policy_final(void)
{
if(policy_query_socket != -1) {
close(policy_query_socket);
policy_query_socket = -1;
}
return NULL;
}
err_t ipsec_policy_readmsg(int policysock,
unsigned char *buf,
size_t buflen)
{
struct ipsec_policy_msg_head ipmh;
if(read(policysock, &ipmh, sizeof(ipmh))
!= sizeof(ipmh)) {
return "read failed";
}
/* got the header, sanitize it, and find out how much more to read */
switch(ipmh.ipm_version) {
case IPSEC_POLICY_MSG_REVISION:
break;
default:
/* XXX go deal with older versions, error for now */
fprintf(stderr, "Bad magic header: %u\n", ipmh.ipm_version);
return "bad policy msg version magic";
}
if(ipmh.ipm_msg_len > buflen) {
return "buffer too small for this message";
}
buflen = ipmh.ipm_msg_len;
memcpy(buf, &ipmh, sizeof(ipmh));
buf += sizeof(ipmh);
buflen -= sizeof(ipmh);
if(read(policysock, buf, buflen) != buflen) {
return "short read from socket";
}
return NULL;
}
err_t ipsec_policy_sendrecv(unsigned char *buf,
size_t buflen)
{
err_t ret;
ipsec_policy_init();
if(write(policy_query_socket, buf, buflen)
!= buflen) {
return "write failed";
}
ret = ipsec_policy_readmsg(policy_query_socket,
buf, buflen);
ipsec_policy_final();
return ret;
}
err_t ipsec_policy_lookup(int fd, struct ipsec_policy_cmd_query *result)
{
int len;
/* clear it out */
memset(result, 0, sizeof(*result));
/* setup it up */
result->head.ipm_version = IPSEC_POLICY_MSG_REVISION;
result->head.ipm_msg_len = sizeof(*result);
result->head.ipm_msg_type = IPSEC_CMD_QUERY_HOSTPAIR;
result->head.ipm_msg_seq = ipsec_policy_seq();
/* suck out the data on the sockets */
len = sizeof(result->query_local);
if(getsockname(fd, (struct sockaddr *)&result->query_local, &len) != 0) {
return "getsockname failed";
}
len = sizeof(result->query_remote);
if(getpeername(fd, (struct sockaddr *)&result->query_remote, &len) != 0) {
return "getpeername failed";
}
return ipsec_policy_sendrecv((unsigned char *)result, sizeof(*result));
}
+38
View File
@@ -0,0 +1,38 @@
/*
* libipsecpolicy version information
* Copyright (C) 2003 Michael Richardson <[email protected]>
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
*
* This library 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 Library General Public
* License for more details.
*
* RCSID $Id: version.in.c,v 1.1 2004/03/15 20:35:25 as Exp $
*/
#define V "xxx" /* substituted in by Makefile */
static const char ipsecpolicy_number[] = V;
static const char ipsecpolicy_string[] = "Linux FreeS/WAN policylib " V;
/*
- ipsec_version_code - return IPsec version number/code, as string
*/
const char *
ipsec_version_code(void)
{
return ipsecpolicy_number;
}
/*
- ipsec_version_string - return full version string
*/
const char *
ipsec_version_string(void)
{
return ipsecpolicy_string;
}
+73
View File
@@ -0,0 +1,73 @@
# Copyright (C) 2000, 2001 Internet Software Consortium.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# $Id: Makefile,v 1.1 2004/03/15 20:35:25 as Exp $
srcdir = .
VPATH = .
top_srcdir = .
CINCLUDES = -I${srcdir}/unix/include \
-I. -I./include -I${srcdir}/include
CDEFINES = -g
CWARNINGS = -Werror
CFLAGS=${CINCLUDES} ${CDEFINES} ${CWARNINGS}
VERSION="@(\#) freeswan-hacking-9.2.1-for-fs2"
LIBINTERFACE=2
LIBREVISION=0
LIBAGE=1
RANLIB=ranlib
# Alphabetically
OBJS = async.o context.o gai_strerror.o getaddrinfo.o gethost.o \
getipnode.o getnameinfo.o getrrset.o getrrset2.o herror.o \
lwbuffer.o lwconfig.o lwpacket.o lwresutil.o \
lwres_gabn.o lwres_gnba.o lwres_grbn.o lwres_noop.o \
lwinetaton.o lwinetpton.o lwinetntop.o
# Alphabetically
SRCS = async.c context.c gai_strerror.c getaddrinfo.c gethost.c \
getipnode.c getnameinfo.c getrrset.c getrrset2.c herror.c \
lwbuffer.c lwconfig.c lwpacket.c lwresutil.c \
lwres_gabn.c lwres_gnba.c lwres_grbn.c lwres_noop.c \
lwinetaton.c lwinetpton.c lwinetntop.c
programs all: liblwres.a
version.o: version.c
${LIBTOOL} ${CC} ${ALL_CFLAGS} \
-DVERSION=\"${VERSION}\" \
-DLIBINTERFACE=${LIBINTERFACE} \
-DLIBREVISION=${LIBREVISION} \
-DLIBAGE=${LIBAGE} \
-c ${srcdir}/version.c
liblwres.a: ${OBJS} version.o
${AR} ${ARFLAGS} $@ ${OBJS} version.o
${RANLIB} $@
timestamp: liblwres.a
touch timestamp
clean distclean mostlyclean realclean cleanall spotless::
rm -f liblwres.a liblwres.la timestamp $(OBJS)
install checkprograms check install_file_list:
@true
TAGS: ${SRCS}
etags ${SRCS}
+3
View File
@@ -0,0 +1,3 @@
LIBINTERFACE = 2
LIBREVISION = 0
LIBAGE = 1
+33
View File
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: assert_p.h,v 1.1 2004/03/15 20:35:25 as Exp $ */
#ifndef LWRES_ASSERT_P_H
#define LWRES_ASSERT_P_H 1
#include <assert.h> /* Required for assert() prototype. */
#define REQUIRE(x) assert(x)
#define INSIST(x) assert(x)
#define UNUSED(x) ((void)(x))
#define SPACE_OK(b, s) (LWRES_BUFFER_AVAILABLECOUNT(b) >= (s))
#define SPACE_REMAINING(b, s) (LWRES_BUFFER_REMAINING(b) >= (s))
#endif /* LWRES_ASSERT_P_H */
+361
View File
@@ -0,0 +1,361 @@
/*
* Copyright (C) 2003, Michael Richardson <[email protected]>
* Derived from code: Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: async.c,v 1.1 2004/03/15 20:35:25 as Exp $ */
#include <config.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <lwres/lwres.h>
#include <lwres/net.h>
#include <lwres/netdb.h> /* XXX #include <netdb.h> */
#include <lwres/async.h>
#include "assert_p.h"
#include "context_p.h"
/*
* malloc / calloc functions that guarantee to only
* return NULL if there is an error, like they used
* to before the ANSI C committee broke them.
*/
static void *
sane_malloc(size_t size) {
if (size == 0)
size = 1;
return (malloc(size));
}
static void *
sane_calloc(size_t number, size_t size) {
size_t len = number * size;
void *mem = sane_malloc(len);
if (mem != NULL)
memset(mem, 0, len);
return (mem);
}
int
lwres_async_init(lwres_context_t **pctx)
{
lwres_result_t lwresult;
lwres_context_t *ctx = NULL;
int result;
lwresult = lwres_context_create(&ctx, NULL, NULL, NULL, 0);
if (lwresult != LWRES_R_SUCCESS) {
result = lwresult_to_result(lwresult);
return(result);
}
(void) lwres_conf_parse(ctx, lwres_resolv_conf);
*pctx = ctx;
return (ERRSET_SUCCESS);
}
int
lwres_getrrsetbyname_init(const char *hostname, unsigned int rdclass,
unsigned int rdtype, unsigned int flags,
lwres_context_t *ctx,
struct lwres_async_state *las)
{
lwres_result_t lwresult;
unsigned int i;
unsigned int lwflags;
unsigned int result;
int ret;
lwres_lwpacket_t pkt;
lwres_grbnrequest_t request;
char target_name[1024];
unsigned int target_length;
int ret2;
if (rdclass > 0xffff || rdtype > 0xffff) {
result = ERRSET_INVAL;
return result;
}
/*
* Don't allow queries of class or type ANY
*/
if (rdclass == 0xff || rdtype == 0xff) {
result = ERRSET_INVAL;
return result;
}
/*
* If any input flags were defined, lwflags would be set here
* based on them
*/
UNUSED(flags);
lwflags = 0;
las->b_in.base = NULL;
las->b_out.base = NULL;
las->serial = lwres_context_nextserial(ctx);
las->opcode = LWRES_OPCODE_GETRDATABYNAME;
target_length = strlen(hostname);
if (target_length >= sizeof(target_name))
return (LWRES_R_FAILURE);
strcpy(target_name, hostname); /* strcpy is safe */
/*
* Set up our request and render it to a buffer.
*/
request.rdclass = rdclass;
request.rdtype = rdtype;
request.flags = lwflags;
request.name = target_name;
request.namelen = target_length;
pkt.pktflags = 0;
pkt.serial = las->serial;
pkt.result = 0;
pkt.recvlength = LWRES_RECVLENGTH;
/* set up async system */
las->next = ctx->pending;
ctx->pending = las;
ret = lwres_grbnrequest_render(ctx, &request, &pkt, &las->b_out);
return ret;
}
int
lwres_getrrsetbyname_xmit(lwres_context_t *ctx,
struct lwres_async_state *las)
{
lwres_result_t lwresult;
int ret;
lwresult = lwres_context_send(ctx, las->b_out.base, las->b_out.length);
return(lwresult_to_result(lwresult));
}
unsigned long
lwres_async_timeout(lwres_context_t *ctx)
{
unsigned long tv_sec;
/*
* Type of tv_sec is long, so make sure the unsigned long timeout
* does not overflow it.
*/
if (ctx->timeout <= LONG_MAX)
tv_sec = (long)ctx->timeout;
else
tv_sec = LONG_MAX;
return tv_sec;
}
int
lwres_async_fd(lwres_context_t *ctx)
{
return (ctx->sock);
}
/*
const char *hostname, unsigned int rdclass,
unsigned int rdtype, unsigned int flags,
*/
int
lwres_getrrsetbyname_read(struct lwres_async_state **plas,
lwres_context_t *ctx,
struct rrsetinfo **res)
{
lwres_result_t lwresult;
lwres_grbnresponse_t *response = NULL;
char *buffer;
struct rrsetinfo *rrset = NULL;
int recvlen;
int ret, result, i;
lwres_buffer_t b_in;
struct lwres_async_state *las;
struct lwres_async_state **las_prev;
lwres_lwpacket_t pkt;
buffer = NULL;
buffer = CTXMALLOC(LWRES_RECVLENGTH);
if (buffer == NULL) {
return ERRSET_NOMEMORY;
}
ret = LWRES_R_SUCCESS;
lwresult = lwres_context_recv(ctx, buffer, LWRES_RECVLENGTH, &recvlen);
if (lwresult == LWRES_R_RETRY) {
ret = LWRES_R_RETRY;
goto out;
}
if (ret != LWRES_R_SUCCESS)
goto out;
lwres_buffer_init(&b_in, buffer, recvlen);
b_in.used = recvlen;
/*
* Parse the packet header.
*/
ret = lwres_lwpacket_parseheader(&b_in, &pkt);
if (ret != LWRES_R_SUCCESS)
goto out;
/*
* find an appropriate waiting las entry. This is a linear search.
* we can do MUCH better, since we control the serial number!
* do that later.
*/
las_prev = &ctx->pending;
las = ctx->pending;
while(las && las->serial != pkt.serial) {
las_prev=&las->next;
las=las->next;
}
if(las == NULL) {
/* no matching serial number! */
return(LWRES_R_RETRY);
}
/* okay, remove it from the receive queue */
*las_prev = las->next;
las->next = NULL;
*plas = las;
/*
* Free what we've transmitted, long ago.
*/
CTXFREE(las->b_out.base, las->b_out.length);
las->b_out.base = NULL;
las->b_out.length = 0;
if (pkt.result != LWRES_R_SUCCESS) {
ret = pkt.result;
goto out;
}
/*
* Parse the response.
*/
ret = lwres_grbnresponse_parse(ctx, &b_in, &pkt, &response);
if (ret != LWRES_R_SUCCESS) {
out:
if (buffer != NULL)
CTXFREE(buffer, LWRES_RECVLENGTH);
if (response != NULL)
lwres_grbnresponse_free(ctx, &response);
result = lwresult_to_result(ret);
goto fail;
}
response->base = buffer;
response->baselen = LWRES_RECVLENGTH;
buffer = NULL; /* don't free this below */
lwresult = LWRES_R_SUCCESS;
rrset = sane_malloc(sizeof(struct rrsetinfo));
if (rrset == NULL) {
result = ERRSET_NOMEMORY;
goto fail;
}
rrset->rri_name = NULL;
rrset->rri_rdclass = response->rdclass;
rrset->rri_rdtype = response->rdtype;
rrset->rri_ttl = response->ttl;
rrset->rri_flags = 0;
rrset->rri_nrdatas = 0;
rrset->rri_rdatas = NULL;
rrset->rri_nsigs = 0;
rrset->rri_sigs = NULL;
rrset->rri_name = sane_malloc(response->realnamelen + 1);
if (rrset->rri_name == NULL) {
result = ERRSET_NOMEMORY;
goto fail;
}
strncpy(rrset->rri_name, response->realname, response->realnamelen);
rrset->rri_name[response->realnamelen] = 0;
if ((response->flags & LWRDATA_VALIDATED) != 0)
rrset->rri_flags |= RRSET_VALIDATED;
rrset->rri_nrdatas = response->nrdatas;
rrset->rri_rdatas = sane_calloc(rrset->rri_nrdatas,
sizeof(struct rdatainfo));
if (rrset->rri_rdatas == NULL) {
result = ERRSET_NOMEMORY;
goto fail;
}
for (i = 0; i < rrset->rri_nrdatas; i++) {
rrset->rri_rdatas[i].rdi_length = response->rdatalen[i];
rrset->rri_rdatas[i].rdi_data =
sane_malloc(rrset->rri_rdatas[i].rdi_length);
if (rrset->rri_rdatas[i].rdi_data == NULL) {
result = ERRSET_NOMEMORY;
goto fail;
}
memcpy(rrset->rri_rdatas[i].rdi_data, response->rdatas[i],
rrset->rri_rdatas[i].rdi_length);
}
rrset->rri_nsigs = response->nsigs;
rrset->rri_sigs = sane_calloc(rrset->rri_nsigs,
sizeof(struct rdatainfo));
if (rrset->rri_sigs == NULL) {
result = ERRSET_NOMEMORY;
goto fail;
}
for (i = 0; i < rrset->rri_nsigs; i++) {
rrset->rri_sigs[i].rdi_length = response->siglen[i];
rrset->rri_sigs[i].rdi_data =
sane_malloc(rrset->rri_sigs[i].rdi_length);
if (rrset->rri_sigs[i].rdi_data == NULL) {
result = ERRSET_NOMEMORY;
goto fail;
}
memcpy(rrset->rri_sigs[i].rdi_data, response->sigs[i],
rrset->rri_sigs[i].rdi_length);
}
lwres_grbnresponse_free(ctx, &response);
*res = rrset;
return (ERRSET_SUCCESS);
fail:
if (rrset != NULL)
lwres_freerrset(rrset);
if (response != NULL)
lwres_grbnresponse_free(ctx, &response);
return (result);
}
View File
+380
View File
@@ -0,0 +1,380 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: context.c,v 1.1 2004/03/15 20:35:25 as Exp $ */
#include <config.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <lwres/lwres.h>
#include <lwres/net.h>
#include <lwres/platform.h>
#ifdef LWRES_PLATFORM_NEEDSYSSELECTH
#include <sys/select.h>
#endif
#include "context_p.h"
#include "assert_p.h"
/*
* Some systems define the socket length argument as an int, some as size_t,
* some as socklen_t. The last is what the current POSIX standard mandates.
* This definition is here so it can be portable but easily changed if needed.
*/
#ifndef LWRES_SOCKADDR_LEN_T
#define LWRES_SOCKADDR_LEN_T unsigned int
#endif
/*
* Make a socket nonblocking.
*/
#ifndef MAKE_NONBLOCKING
#define MAKE_NONBLOCKING(sd, retval) \
do { \
retval = fcntl(sd, F_GETFL, 0); \
if (retval != -1) { \
retval |= O_NONBLOCK; \
retval = fcntl(sd, F_SETFL, retval); \
} \
} while (0)
#endif
lwres_uint16_t lwres_udp_port = LWRES_UDP_PORT;
const char *lwres_resolv_conf = LWRES_RESOLV_CONF;
static void *
lwres_malloc(void *, size_t);
static void
lwres_free(void *, void *, size_t);
static lwres_result_t
context_connect(lwres_context_t *);
lwres_result_t
lwres_context_create(lwres_context_t **contextp, void *arg,
lwres_malloc_t malloc_function,
lwres_free_t free_function,
unsigned int flags)
{
lwres_context_t *ctx;
REQUIRE(contextp != NULL && *contextp == NULL);
UNUSED(flags);
/*
* If we were not given anything special to use, use our own
* functions. These are just wrappers around malloc() and free().
*/
if (malloc_function == NULL || free_function == NULL) {
REQUIRE(malloc_function == NULL);
REQUIRE(free_function == NULL);
malloc_function = lwres_malloc;
free_function = lwres_free;
}
ctx = malloc_function(arg, sizeof(lwres_context_t));
if (ctx == NULL)
return (LWRES_R_NOMEMORY);
/*
* Set up the context.
*/
ctx->malloc = malloc_function;
ctx->free = free_function;
ctx->arg = arg;
ctx->sock = -1;
ctx->timeout = LWRES_DEFAULT_TIMEOUT;
ctx->serial = time(NULL); /* XXXMLG or BEW */
/*
* Init resolv.conf bits.
*/
lwres_conf_init(ctx);
*contextp = ctx;
return (LWRES_R_SUCCESS);
}
void
lwres_context_destroy(lwres_context_t **contextp) {
lwres_context_t *ctx;
REQUIRE(contextp != NULL && *contextp != NULL);
ctx = *contextp;
*contextp = NULL;
if (ctx->sock != -1) {
close(ctx->sock);
ctx->sock = -1;
}
CTXFREE(ctx, sizeof(lwres_context_t));
}
lwres_uint32_t
lwres_context_nextserial(lwres_context_t *ctx) {
REQUIRE(ctx != NULL);
return (ctx->serial++);
}
void
lwres_context_initserial(lwres_context_t *ctx, lwres_uint32_t serial) {
REQUIRE(ctx != NULL);
ctx->serial = serial;
}
void
lwres_context_freemem(lwres_context_t *ctx, void *mem, size_t len) {
REQUIRE(mem != NULL);
REQUIRE(len != 0);
CTXFREE(mem, len);
}
void *
lwres_context_allocmem(lwres_context_t *ctx, size_t len) {
REQUIRE(len != 0);
return (CTXMALLOC(len));
}
static void *
lwres_malloc(void *arg, size_t len) {
void *mem;
UNUSED(arg);
mem = malloc(len);
if (mem == NULL)
return (NULL);
memset(mem, 0xe5, len);
return (mem);
}
static void
lwres_free(void *arg, void *mem, size_t len) {
UNUSED(arg);
memset(mem, 0xa9, len);
free(mem);
}
static lwres_result_t
context_connect(lwres_context_t *ctx) {
int s;
int ret;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
struct sockaddr *sa;
LWRES_SOCKADDR_LEN_T salen;
int domain;
if (ctx->confdata.lwnext != 0) {
memcpy(&ctx->address, &ctx->confdata.lwservers[0],
sizeof(lwres_addr_t));
LWRES_LINK_INIT(&ctx->address, link);
} else {
/* The default is the IPv4 loopback address 127.0.0.1. */
memset(&ctx->address, 0, sizeof(ctx->address));
ctx->address.family = LWRES_ADDRTYPE_V4;
ctx->address.length = 4;
ctx->address.address[0] = 127;
ctx->address.address[1] = 0;
ctx->address.address[2] = 0;
ctx->address.address[3] = 1;
}
if (ctx->address.family == LWRES_ADDRTYPE_V4) {
memcpy(&sin.sin_addr, ctx->address.address,
sizeof(sin.sin_addr));
sin.sin_port = htons(lwres_udp_port);
sin.sin_family = AF_INET;
sa = (struct sockaddr *)&sin;
salen = sizeof(sin);
domain = PF_INET;
} else if (ctx->address.family == LWRES_ADDRTYPE_V6) {
memcpy(&sin6.sin6_addr, ctx->address.address,
sizeof(sin6.sin6_addr));
sin6.sin6_port = htons(lwres_udp_port);
sin6.sin6_family = AF_INET6;
sa = (struct sockaddr *)&sin6;
salen = sizeof(sin6);
domain = PF_INET6;
} else
return (LWRES_R_IOERROR);
s = socket(domain, SOCK_DGRAM, IPPROTO_UDP);
if (s < 0)
return (LWRES_R_IOERROR);
ret = connect(s, sa, salen);
if (ret != 0) {
close(s);
return (LWRES_R_IOERROR);
}
MAKE_NONBLOCKING(s, ret);
if (ret < 0)
return (LWRES_R_IOERROR);
ctx->sock = s;
return (LWRES_R_SUCCESS);
}
int
lwres_context_getsocket(lwres_context_t *ctx) {
return (ctx->sock);
}
lwres_result_t
lwres_context_send(lwres_context_t *ctx,
void *sendbase, int sendlen) {
int ret;
lwres_result_t lwresult;
if (ctx->sock == -1) {
lwresult = context_connect(ctx);
if (lwresult != LWRES_R_SUCCESS)
return (lwresult);
}
ret = sendto(ctx->sock, sendbase, sendlen, 0, NULL, 0);
if (ret < 0)
return (LWRES_R_IOERROR);
if (ret != sendlen)
return (LWRES_R_IOERROR);
return (LWRES_R_SUCCESS);
}
lwres_result_t
lwres_context_recv(lwres_context_t *ctx,
void *recvbase, int recvlen,
int *recvd_len)
{
LWRES_SOCKADDR_LEN_T fromlen;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
struct sockaddr *sa;
int ret;
if (ctx->address.family == LWRES_ADDRTYPE_V4) {
sa = (struct sockaddr *)&sin;
fromlen = sizeof(sin);
} else {
sa = (struct sockaddr *)&sin6;
fromlen = sizeof(sin6);
}
/*
* The address of fromlen is cast to void * to shut up compiler
* warnings, namely on systems that have the sixth parameter
* prototyped as a signed int when LWRES_SOCKADDR_LEN_T is
* defined as unsigned.
*/
ret = recvfrom(ctx->sock, recvbase, recvlen, 0, sa, (void *)&fromlen);
if (ret < 0)
return (LWRES_R_IOERROR);
if (ret == recvlen)
return (LWRES_R_TOOLARGE);
/*
* If we got something other than what we expect, have the caller
* wait for another packet. This can happen if an old result
* comes in, or if someone is sending us random stuff.
*/
if (ctx->address.family == LWRES_ADDRTYPE_V4) {
if (fromlen != sizeof(sin)
|| memcmp(&sin.sin_addr, ctx->address.address,
sizeof(sin.sin_addr)) != 0
|| sin.sin_port != htons(lwres_udp_port))
return (LWRES_R_RETRY);
} else {
if (fromlen != sizeof(sin6)
|| memcmp(&sin6.sin6_addr, ctx->address.address,
sizeof(sin6.sin6_addr)) != 0
|| sin6.sin6_port != htons(lwres_udp_port))
return (LWRES_R_RETRY);
}
if (recvd_len != NULL)
*recvd_len = ret;
return (LWRES_R_SUCCESS);
}
lwres_result_t
lwres_context_sendrecv(lwres_context_t *ctx,
void *sendbase, int sendlen,
void *recvbase, int recvlen,
int *recvd_len)
{
lwres_result_t result;
int ret2;
fd_set readfds;
struct timeval timeout;
/*
* Type of tv_sec is long, so make sure the unsigned long timeout
* does not overflow it.
*/
if (ctx->timeout <= LONG_MAX)
timeout.tv_sec = (long)ctx->timeout;
else
timeout.tv_sec = LONG_MAX;
timeout.tv_usec = 0;
result = lwres_context_send(ctx, sendbase, sendlen);
if (result != LWRES_R_SUCCESS)
return (result);
again:
FD_ZERO(&readfds);
FD_SET(ctx->sock, &readfds);
ret2 = select(ctx->sock + 1, &readfds, NULL, NULL, &timeout);
/*
* What happened with select?
*/
if (ret2 < 0)
return (LWRES_R_IOERROR);
if (ret2 == 0)
return (LWRES_R_TIMEOUT);
result = lwres_context_recv(ctx, recvbase, recvlen, recvd_len);
if (result == LWRES_R_RETRY)
goto again;
return (result);
}
+68
View File
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: context_p.h,v 1.1 2004/03/15 20:35:25 as Exp $ */
#ifndef LWRES_CONTEXT_P_H
#define LWRES_CONTEXT_P_H 1
/*
* Helper functions, assuming the context is always called "ctx" in
* the scope these functions are called from.
*/
#define CTXMALLOC(len) ctx->malloc(ctx->arg, (len))
#define CTXFREE(addr, len) ctx->free(ctx->arg, (addr), (len))
#define LWRES_DEFAULT_TIMEOUT 120 /* 120 seconds for a reply */
/*
* Not all the attributes here are actually settable by the application at
* this time.
*/
struct lwres_context {
unsigned int timeout; /* time to wait for reply */
lwres_uint32_t serial; /* serial number state */
/*
* For network I/O.
*/
int sock; /* socket to send on */
lwres_addr_t address; /* address to send to */
/*
* Function pointers for allocating memory.
*/
lwres_malloc_t malloc;
lwres_free_t free;
void *arg;
/*
* resolv.conf-like data
*/
lwres_conf_t confdata;
/* linked list of outstanding DNS requests */
struct lwres_async_state *pending;
};
#endif /* LWRES_CONTEXT_P_H */
/*
* Local Variables:
* c-basic-offset: 8
* End Variables:
*/
+52
View File
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: gai_strerror.c,v 1.1 2004/03/15 20:35:25 as Exp $ */
#include <lwres/netdb.h>
static const char *gai_messages[] = {
"no error",
"address family for hostname not supported",
"temporary failure in name resolution",
"invalid value for ai_flags",
"non-recoverable failure in name resolution",
"ai_family not supported",
"memory allocation failure",
"no address associated with hostname",
"hostname nor servname provided, or not known",
"servname not supported for ai_socktype",
"ai_socktype not supported",
"system error returned in errno",
"bad hints",
"bad protocol"
};
char *
lwres_gai_strerror(int ecode) {
union {
const char *const_ptr;
char *deconst_ptr;
} ptr;
if ((ecode < 0) ||
(ecode >= (int)(sizeof(gai_messages)/sizeof(*gai_messages))))
ptr.const_ptr = "invalid error code";
else
ptr.const_ptr = gai_messages[ecode];
return (ptr.deconst_ptr);
}
+692
View File
@@ -0,0 +1,692 @@
/*
* Copyright (C) 1999-2001 Internet Software Consortium.
*
* This code is derived from software contributed to Internet Software
* Consortium by Berkeley Software Design, Inc.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM AND
* BERKELEY SOFTWARE DESIGN, INC DISCLAIM ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE CONSORTIUM OR BERKELEY
* SOFTWARE DESIGN, INC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: getaddrinfo.c,v 1.1 2004/03/15 20:35:25 as Exp $ */
#include <config.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <lwres/lwres.h>
#include <lwres/net.h>
#include <lwres/netdb.h>
#define SA(addr) ((struct sockaddr *)(addr))
#define SIN(addr) ((struct sockaddr_in *)(addr))
#define SIN6(addr) ((struct sockaddr_in6 *)(addr))
#define SUN(addr) ((struct sockaddr_un *)(addr))
static struct addrinfo
*ai_reverse(struct addrinfo *oai),
*ai_clone(struct addrinfo *oai, int family),
*ai_alloc(int family, int addrlen);
#ifdef AF_LOCAL
static int get_local(const char *name, int socktype, struct addrinfo **res);
#endif
static int add_ipv4(const char *hostname, int flags, struct addrinfo **aip,
int socktype, int port);
static int add_ipv6(const char *hostname, int flags, struct addrinfo **aip,
int socktype, int port);
static void set_order(int, int (**)(const char *, int, struct addrinfo **,
int, int));
#define FOUND_IPV4 0x1
#define FOUND_IPV6 0x2
#define FOUND_MAX 2
#define ISC_AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
int
lwres_getaddrinfo(const char *hostname, const char *servname,
const struct addrinfo *hints, struct addrinfo **res)
{
struct servent *sp;
const char *proto;
int family, socktype, flags, protocol;
struct addrinfo *ai, *ai_list;
int port, err, i;
int (*net_order[FOUND_MAX+1])(const char *, int, struct addrinfo **,
int, int);
if (hostname == NULL && servname == NULL)
return (EAI_NONAME);
proto = NULL;
if (hints != NULL) {
if ((hints->ai_flags & ~(ISC_AI_MASK)) != 0)
return (EAI_BADFLAGS);
if (hints->ai_addrlen || hints->ai_canonname ||
hints->ai_addr || hints->ai_next) {
errno = EINVAL;
return (EAI_SYSTEM);
}
family = hints->ai_family;
socktype = hints->ai_socktype;
protocol = hints->ai_protocol;
flags = hints->ai_flags;
switch (family) {
case AF_UNSPEC:
switch (hints->ai_socktype) {
case SOCK_STREAM:
proto = "tcp";
break;
case SOCK_DGRAM:
proto = "udp";
break;
}
break;
case AF_INET:
case AF_INET6:
switch (hints->ai_socktype) {
case 0:
break;
case SOCK_STREAM:
proto = "tcp";
break;
case SOCK_DGRAM:
proto = "udp";
break;
case SOCK_RAW:
break;
default:
return (EAI_SOCKTYPE);
}
break;
#ifdef AF_LOCAL
case AF_LOCAL:
switch (hints->ai_socktype) {
case 0:
break;
case SOCK_STREAM:
break;
case SOCK_DGRAM:
break;
default:
return (EAI_SOCKTYPE);
}
break;
#endif
default:
return (EAI_FAMILY);
}
} else {
protocol = 0;
family = 0;
socktype = 0;
flags = 0;
}
#ifdef AF_LOCAL
/*
* First, deal with AF_LOCAL. If the family was not set,
* then assume AF_LOCAL if the first character of the
* hostname/servname is '/'.
*/
if (hostname != NULL &&
(family == AF_LOCAL || (family == 0 && *hostname == '/')))
return (get_local(hostname, socktype, res));
if (servname != NULL &&
(family == AF_LOCAL || (family == 0 && *servname == '/')))
return (get_local(servname, socktype, res));
#endif
/*
* Ok, only AF_INET and AF_INET6 left.
*/
ai_list = NULL;
/*
* First, look up the service name (port) if it was
* requested. If the socket type wasn't specified, then
* try and figure it out.
*/
if (servname != NULL) {
char *e;
port = strtol(servname, &e, 10);
if (*e == '\0') {
if (socktype == 0)
return (EAI_SOCKTYPE);
if (port < 0 || port > 65535)
return (EAI_SERVICE);
port = htons((unsigned short) port);
} else {
sp = getservbyname(servname, proto);
if (sp == NULL)
return (EAI_SERVICE);
port = sp->s_port;
if (socktype == 0) {
if (strcmp(sp->s_proto, "tcp") == 0)
socktype = SOCK_STREAM;
else if (strcmp(sp->s_proto, "udp") == 0)
socktype = SOCK_DGRAM;
}
}
} else
port = 0;
/*
* Next, deal with just a service name, and no hostname.
* (we verified that one of them was non-null up above).
*/
if (hostname == NULL && (flags & AI_PASSIVE) != 0) {
if (family == AF_INET || family == 0) {
ai = ai_alloc(AF_INET, sizeof(struct sockaddr_in));
if (ai == NULL)
return (EAI_MEMORY);
ai->ai_socktype = socktype;
ai->ai_protocol = protocol;
SIN(ai->ai_addr)->sin_port = port;
ai->ai_next = ai_list;
ai_list = ai;
}
if (family == AF_INET6 || family == 0) {
ai = ai_alloc(AF_INET6, sizeof(struct sockaddr_in6));
if (ai == NULL) {
lwres_freeaddrinfo(ai_list);
return (EAI_MEMORY);
}
ai->ai_socktype = socktype;
ai->ai_protocol = protocol;
SIN6(ai->ai_addr)->sin6_port = port;
ai->ai_next = ai_list;
ai_list = ai;
}
*res = ai_list;
return (0);
}
/*
* If the family isn't specified or AI_NUMERICHOST specified,
* check first to see if it is a numeric address.
* Though the gethostbyname2() routine
* will recognize numeric addresses, it will only recognize
* the format that it is being called for. Thus, a numeric
* AF_INET address will be treated by the AF_INET6 call as
* a domain name, and vice versa. Checking for both numerics
* here avoids that.
*/
if (hostname != NULL &&
(family == 0 || (flags & AI_NUMERICHOST) != 0)) {
char abuf[sizeof(struct in6_addr)];
char nbuf[NI_MAXHOST];
int addrsize, addroff;
#ifdef LWRES_HAVE_SIN6_SCOPE_ID
char *p, *ep;
char ntmp[NI_MAXHOST];
lwres_uint32_t scopeid;
#endif
#ifdef LWRES_HAVE_SIN6_SCOPE_ID
/*
* Scope identifier portion.
*/
ntmp[0] = '\0';
if (strchr(hostname, '%') != NULL) {
strncpy(ntmp, hostname, sizeof(ntmp) - 1);
ntmp[sizeof(ntmp) - 1] = '\0';
p = strchr(ntmp, '%');
ep = NULL;
/*
* Vendors may want to support non-numeric
* scopeid around here.
*/
if (p != NULL)
scopeid = (lwres_uint32_t)strtoul(p + 1,
&ep, 10);
if (p != NULL && ep != NULL && ep[0] == '\0')
*p = '\0';
else {
ntmp[0] = '\0';
scopeid = 0;
}
} else
scopeid = 0;
#endif
if (lwres_net_pton(AF_INET, hostname, (struct in_addr *)abuf)
== 1)
{
if (family == AF_INET6) {
/*
* Convert to a V4 mapped address.
*/
struct in6_addr *a6 = (struct in6_addr *)abuf;
memcpy(&a6->s6_addr[12], &a6->s6_addr[0], 4);
memset(&a6->s6_addr[10], 0xff, 2);
memset(&a6->s6_addr[0], 0, 10);
goto inet6_addr;
}
addrsize = sizeof(struct in_addr);
addroff = (char *)(&SIN(0)->sin_addr) - (char *)0;
family = AF_INET;
goto common;
#ifdef LWRES_HAVE_SIN6_SCOPE_ID
} else if (ntmp[0] != '\0' &&
lwres_net_pton(AF_INET6, ntmp, abuf) == 1)
{
if (family && family != AF_INET6)
return (EAI_NONAME);
addrsize = sizeof(struct in6_addr);
addroff = (char *)(&SIN6(0)->sin6_addr) - (char *)0;
family = AF_INET6;
goto common;
#endif
} else if (lwres_net_pton(AF_INET6, hostname, abuf) == 1) {
if (family != 0 && family != AF_INET6)
return (EAI_NONAME);
inet6_addr:
addrsize = sizeof(struct in6_addr);
addroff = (char *)(&SIN6(0)->sin6_addr) - (char *)0;
family = AF_INET6;
common:
ai = ai_clone(ai_list, family);
if (ai == NULL)
return (EAI_MEMORY);
ai_list = ai;
ai->ai_socktype = socktype;
SIN(ai->ai_addr)->sin_port = port;
memcpy((char *)ai->ai_addr + addroff, abuf, addrsize);
if (flags & AI_CANONNAME) {
#if defined(LWRES_HAVE_SIN6_SCOPE_ID)
if (ai->ai_family == AF_INET6)
SIN6(ai->ai_addr)->sin6_scope_id =
scopeid;
#endif
if (lwres_getnameinfo(ai->ai_addr,
ai->ai_addrlen, nbuf, sizeof(nbuf),
NULL, 0,
NI_NUMERICHOST) == 0) {
ai->ai_canonname = strdup(nbuf);
if (ai->ai_canonname == NULL)
return (EAI_MEMORY);
} else {
/* XXX raise error? */
ai->ai_canonname = NULL;
}
}
goto done;
} else if ((flags & AI_NUMERICHOST) != 0) {
return (EAI_NONAME);
}
}
set_order(family, net_order);
for (i = 0; i < FOUND_MAX; i++) {
if (net_order[i] == NULL)
break;
err = (net_order[i])(hostname, flags, &ai_list,
socktype, port);
if (err != 0)
return (err);
}
if (ai_list == NULL)
return (EAI_NODATA);
done:
ai_list = ai_reverse(ai_list);
*res = ai_list;
return (0);
}
static char *
lwres_strsep(char **stringp, const char *delim) {
char *string = *stringp;
char *s;
const char *d;
char sc, dc;
if (string == NULL)
return (NULL);
for (s = string; *s != '\0'; s++) {
sc = *s;
for (d = delim; (dc = *d) != '\0'; d++)
if (sc == dc) {
*s++ = '\0';
*stringp = s;
return (string);
}
}
*stringp = NULL;
return (string);
}
static void
set_order(int family, int (**net_order)(const char *, int, struct addrinfo **,
int, int))
{
char *order, *tok;
int found;
if (family) {
switch (family) {
case AF_INET:
*net_order++ = add_ipv4;
break;
case AF_INET6:
*net_order++ = add_ipv6;
break;
}
} else {
order = getenv("NET_ORDER");
found = 0;
while (order != NULL) {
/*
* We ignore any unknown names.
*/
tok = lwres_strsep(&order, ":");
if (strcasecmp(tok, "inet6") == 0) {
if ((found & FOUND_IPV6) == 0)
*net_order++ = add_ipv6;
found |= FOUND_IPV6;
} else if (strcasecmp(tok, "inet") == 0 ||
strcasecmp(tok, "inet4") == 0) {
if ((found & FOUND_IPV4) == 0)
*net_order++ = add_ipv4;
found |= FOUND_IPV4;
}
}
/*
* Add in anything that we didn't find.
*/
if ((found & FOUND_IPV4) == 0)
*net_order++ = add_ipv4;
if ((found & FOUND_IPV6) == 0)
*net_order++ = add_ipv6;
}
*net_order = NULL;
return;
}
static char v4_loop[4] = { 127, 0, 0, 1 };
/*
* The test against 0 is there to keep the Solaris compiler
* from complaining about "end-of-loop code not reached".
*/
#define ERR(code) \
do { result = (code); \
if (result != 0) goto cleanup; \
} while (0)
static int
add_ipv4(const char *hostname, int flags, struct addrinfo **aip,
int socktype, int port)
{
struct addrinfo *ai;
lwres_context_t *lwrctx = NULL;
lwres_gabnresponse_t *by = NULL;
lwres_addr_t *addr;
lwres_result_t lwres;
int result = 0;
lwres = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
if (lwres != LWRES_R_SUCCESS)
ERR(EAI_FAIL);
(void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
if (hostname == NULL && (flags & AI_PASSIVE) == 0) {
ai = ai_clone(*aip, AF_INET);
if (ai == NULL) {
lwres_freeaddrinfo(*aip);
ERR(EAI_MEMORY);
}
*aip = ai;
ai->ai_socktype = socktype;
SIN(ai->ai_addr)->sin_port = port;
memcpy(&SIN(ai->ai_addr)->sin_addr, v4_loop, 4);
} else {
lwres = lwres_getaddrsbyname(lwrctx, hostname,
LWRES_ADDRTYPE_V4, &by);
if (lwres != LWRES_R_SUCCESS) {
if (lwres == LWRES_R_NOTFOUND)
goto cleanup;
else
ERR(EAI_FAIL);
}
addr = LWRES_LIST_HEAD(by->addrs);
while (addr != NULL) {
ai = ai_clone(*aip, AF_INET);
if (ai == NULL) {
lwres_freeaddrinfo(*aip);
ERR(EAI_MEMORY);
}
*aip = ai;
ai->ai_socktype = socktype;
SIN(ai->ai_addr)->sin_port = port;
memcpy(&SIN(ai->ai_addr)->sin_addr,
addr->address, 4);
if (flags & AI_CANONNAME) {
ai->ai_canonname = strdup(by->realname);
if (ai->ai_canonname == NULL)
ERR(EAI_MEMORY);
}
addr = LWRES_LIST_NEXT(addr, link);
}
}
cleanup:
if (by != NULL)
lwres_gabnresponse_free(lwrctx, &by);
if (lwrctx != NULL) {
lwres_conf_clear(lwrctx);
lwres_context_destroy(&lwrctx);
}
return (result);
}
static char v6_loop[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
static int
add_ipv6(const char *hostname, int flags, struct addrinfo **aip,
int socktype, int port)
{
struct addrinfo *ai;
lwres_context_t *lwrctx = NULL;
lwres_gabnresponse_t *by = NULL;
lwres_addr_t *addr;
lwres_result_t lwres;
int result = 0;
lwres = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
if (lwres != LWRES_R_SUCCESS)
ERR(EAI_FAIL);
(void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
if (hostname == NULL && (flags & AI_PASSIVE) == 0) {
ai = ai_clone(*aip, AF_INET6);
if (ai == NULL) {
lwres_freeaddrinfo(*aip);
ERR(EAI_MEMORY);
}
*aip = ai;
ai->ai_socktype = socktype;
SIN6(ai->ai_addr)->sin6_port = port;
memcpy(&SIN6(ai->ai_addr)->sin6_addr, v6_loop, 16);
} else {
lwres = lwres_getaddrsbyname(lwrctx, hostname,
LWRES_ADDRTYPE_V6, &by);
if (lwres != LWRES_R_SUCCESS) {
if (lwres == LWRES_R_NOTFOUND)
goto cleanup;
else
ERR(EAI_FAIL);
}
addr = LWRES_LIST_HEAD(by->addrs);
while (addr != NULL) {
ai = ai_clone(*aip, AF_INET6);
if (ai == NULL) {
lwres_freeaddrinfo(*aip);
ERR(EAI_MEMORY);
}
*aip = ai;
ai->ai_socktype = socktype;
SIN6(ai->ai_addr)->sin6_port = port;
memcpy(&SIN6(ai->ai_addr)->sin6_addr,
addr->address, 16);
if (flags & AI_CANONNAME) {
ai->ai_canonname = strdup(by->realname);
if (ai->ai_canonname == NULL)
ERR(EAI_MEMORY);
}
addr = LWRES_LIST_NEXT(addr, link);
}
}
cleanup:
if (by != NULL)
lwres_gabnresponse_free(lwrctx, &by);
if (lwrctx != NULL) {
lwres_conf_clear(lwrctx);
lwres_context_destroy(&lwrctx);
}
return (result);
}
void
lwres_freeaddrinfo(struct addrinfo *ai) {
struct addrinfo *ai_next;
while (ai != NULL) {
ai_next = ai->ai_next;
if (ai->ai_addr != NULL)
free(ai->ai_addr);
if (ai->ai_canonname)
free(ai->ai_canonname);
free(ai);
ai = ai_next;
}
}
#ifdef AF_LOCAL
static int
get_local(const char *name, int socktype, struct addrinfo **res) {
struct addrinfo *ai;
struct sockaddr_un *sun;
if (socktype == 0)
return (EAI_SOCKTYPE);
ai = ai_alloc(AF_LOCAL, sizeof(*sun));
if (ai == NULL)
return (EAI_MEMORY);
sun = SUN(ai->ai_addr);
strncpy(sun->sun_path, name, sizeof(sun->sun_path));
ai->ai_socktype = socktype;
/*
* ai->ai_flags, ai->ai_protocol, ai->ai_canonname,
* and ai->ai_next were initialized to zero.
*/
*res = ai;
return (0);
}
#endif
/*
* Allocate an addrinfo structure, and a sockaddr structure
* of the specificed length. We initialize:
* ai_addrlen
* ai_family
* ai_addr
* ai_addr->sa_family
* ai_addr->sa_len (LWRES_PLATFORM_HAVESALEN)
* and everything else is initialized to zero.
*/
static struct addrinfo *
ai_alloc(int family, int addrlen) {
struct addrinfo *ai;
ai = (struct addrinfo *)calloc(1, sizeof(*ai));
if (ai == NULL)
return (NULL);
ai->ai_addr = SA(calloc(1, addrlen));
if (ai->ai_addr == NULL) {
free(ai);
return (NULL);
}
ai->ai_addrlen = addrlen;
ai->ai_family = family;
ai->ai_addr->sa_family = family;
#ifdef LWRES_PLATFORM_HAVESALEN
ai->ai_addr->sa_len = addrlen;
#endif
return (ai);
}
static struct addrinfo *
ai_clone(struct addrinfo *oai, int family) {
struct addrinfo *ai;
ai = ai_alloc(family, ((family == AF_INET6) ?
sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)));
if (ai == NULL) {
lwres_freeaddrinfo(oai);
return (NULL);
}
if (oai == NULL)
return (ai);
ai->ai_flags = oai->ai_flags;
ai->ai_socktype = oai->ai_socktype;
ai->ai_protocol = oai->ai_protocol;
ai->ai_canonname = NULL;
ai->ai_next = oai;
return (ai);
}
static struct addrinfo *
ai_reverse(struct addrinfo *oai) {
struct addrinfo *nai, *tai;
nai = NULL;
while (oai != NULL) {
/*
* Grab one off the old list.
*/
tai = oai;
oai = oai->ai_next;
/*
* Put it on the front of the new list.
*/
tai->ai_next = nai;
nai = tai;
}
return (nai);
}
+219
View File
@@ -0,0 +1,219 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: gethost.c,v 1.1 2004/03/15 20:35:25 as Exp $ */
#include <config.h>
#include <errno.h>
#include <string.h>
#include <lwres/net.h>
#include <lwres/netdb.h>
#include "assert_p.h"
#define LWRES_ALIGNBYTES (sizeof(char *) - 1)
#define LWRES_ALIGN(p) \
(((unsigned long)(p) + LWRES_ALIGNBYTES) &~ LWRES_ALIGNBYTES)
static struct hostent *he = NULL;
static int copytobuf(struct hostent *, struct hostent *, char *, int);
struct hostent *
lwres_gethostbyname(const char *name) {
if (he != NULL)
lwres_freehostent(he);
he = lwres_getipnodebyname(name, AF_INET, 0, &lwres_h_errno);
return (he);
}
struct hostent *
lwres_gethostbyname2(const char *name, int af) {
if (he != NULL)
lwres_freehostent(he);
he = lwres_getipnodebyname(name, af, 0, &lwres_h_errno);
return (he);
}
struct hostent *
lwres_gethostbyaddr(const char *addr, int len, int type) {
if (he != NULL)
lwres_freehostent(he);
he = lwres_getipnodebyaddr(addr, len, type, &lwres_h_errno);
return (he);
}
struct hostent *
lwres_gethostent(void) {
if (he != NULL)
lwres_freehostent(he);
return (NULL);
}
void
lwres_sethostent(int stayopen) {
/*
* Empty.
*/
UNUSED(stayopen);
}
void
lwres_endhostent(void) {
/*
* Empty.
*/
}
struct hostent *
lwres_gethostbyname_r(const char *name, struct hostent *resbuf,
char *buf, int buflen, int *error)
{
struct hostent *he;
int res;
he = lwres_getipnodebyname(name, AF_INET, 0, error);
if (he == NULL)
return (NULL);
res = copytobuf(he, resbuf, buf, buflen);
lwres_freehostent(he);
if (res != 0) {
errno = ERANGE;
return (NULL);
}
return (resbuf);
}
struct hostent *
lwres_gethostbyaddr_r(const char *addr, int len, int type,
struct hostent *resbuf, char *buf, int buflen,
int *error)
{
struct hostent *he;
int res;
he = lwres_getipnodebyaddr(addr, len, type, error);
if (he == NULL)
return (NULL);
res = copytobuf(he, resbuf, buf, buflen);
lwres_freehostent(he);
if (res != 0) {
errno = ERANGE;
return (NULL);
}
return (resbuf);
}
struct hostent *
lwres_gethostent_r(struct hostent *resbuf, char *buf, int buflen, int *error) {
UNUSED(resbuf);
UNUSED(buf);
UNUSED(buflen);
*error = 0;
return (NULL);
}
void
lwres_sethostent_r(int stayopen) {
/*
* Empty.
*/
UNUSED(stayopen);
}
void
lwres_endhostent_r(void) {
/*
* Empty.
*/
}
static int
copytobuf(struct hostent *he, struct hostent *hptr, char *buf, int buflen) {
char *cp;
char **ptr;
int i, n;
int nptr, len;
/*
* Find out the amount of space required to store the answer.
*/
nptr = 2; /* NULL ptrs */
len = (char *)LWRES_ALIGN(buf) - buf;
for (i = 0; he->h_addr_list[i]; i++, nptr++) {
len += he->h_length;
}
for (i = 0; he->h_aliases[i]; i++, nptr++) {
len += strlen(he->h_aliases[i]) + 1;
}
len += strlen(he->h_name) + 1;
len += nptr * sizeof(char*);
if (len > buflen) {
return (-1);
}
/*
* Copy address size and type.
*/
hptr->h_addrtype = he->h_addrtype;
n = hptr->h_length = he->h_length;
ptr = (char **)LWRES_ALIGN(buf);
cp = (char *)LWRES_ALIGN(buf) + nptr * sizeof(char *);
/*
* Copy address list.
*/
hptr->h_addr_list = ptr;
for (i = 0; he->h_addr_list[i]; i++, ptr++) {
memcpy(cp, he->h_addr_list[i], n);
hptr->h_addr_list[i] = cp;
cp += n;
}
hptr->h_addr_list[i] = NULL;
ptr++;
/*
* Copy official name.
*/
n = strlen(he->h_name) + 1;
strcpy(cp, he->h_name);
hptr->h_name = cp;
cp += n;
/*
* Copy aliases.
*/
hptr->h_aliases = ptr;
for (i = 0; he->h_aliases[i]; i++) {
n = strlen(he->h_aliases[i]) + 1;
strcpy(cp, he->h_aliases[i]);
hptr->h_aliases[i] = cp;
cp += n;
}
hptr->h_aliases[i] = NULL;
return (0);
}
+839
View File
@@ -0,0 +1,839 @@
/*
* Copyright (C) 1999-2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: getipnode.c,v 1.1 2004/03/15 20:35:25 as Exp $ */
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lwres/lwres.h>
#include <lwres/net.h>
#include <lwres/netdb.h> /* XXX #include <netdb.h> */
#include "assert_p.h"
#ifndef INADDRSZ
#define INADDRSZ 4
#endif
#ifndef IN6ADDRSZ
#define IN6ADDRSZ 16
#endif
#ifdef LWRES_PLATFORM_NEEDIN6ADDRANY
LIBLWRES_EXTERNAL_DATA const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
#endif
#ifndef IN6_IS_ADDR_V4COMPAT
static const unsigned char in6addr_compat[12] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
#define IN6_IS_ADDR_V4COMPAT(x) (!memcmp((x)->s6_addr, in6addr_compat, 12) && \
((x)->s6_addr[12] != 0 || \
(x)->s6_addr[13] != 0 || \
(x)->s6_addr[14] != 0 || \
((x)->s6_addr[15] != 0 && \
(x)->s6_addr[15] != 1)))
#endif
#ifndef IN6_IS_ADDR_V4MAPPED
#define IN6_IS_ADDR_V4MAPPED(x) (!memcmp((x)->s6_addr, in6addr_mapped, 12))
#endif
static const unsigned char in6addr_mapped[12] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff
};
/***
*** Forward declarations.
***/
static int
scan_interfaces(int *, int *);
static struct hostent *
copyandmerge(struct hostent *, struct hostent *, int, int *);
static struct hostent *
hostfromaddr(lwres_gnbaresponse_t *addr, int af, const void *src);
static struct hostent *
hostfromname(lwres_gabnresponse_t *name, int af);
/***
*** Public functions.
***/
/*
* AI_V4MAPPED + AF_INET6
* If no IPv6 address then a query for IPv4 and map returned values.
*
* AI_ALL + AI_V4MAPPED + AF_INET6
* Return IPv6 and IPv4 mapped.
*
* AI_ADDRCONFIG
* Only return IPv6 / IPv4 address if there is an interface of that
* type active.
*/
struct hostent *
lwres_getipnodebyname(const char *name, int af, int flags, int *error_num) {
int have_v4 = 1, have_v6 = 1;
struct in_addr in4;
struct in6_addr in6;
struct hostent he, *he1 = NULL, *he2 = NULL, *he3 = NULL;
int v4 = 0, v6 = 0;
int tmp_err;
lwres_context_t *lwrctx = NULL;
lwres_gabnresponse_t *by = NULL;
int n;
/*
* If we care about active interfaces then check.
*/
if ((flags & AI_ADDRCONFIG) != 0)
if (scan_interfaces(&have_v4, &have_v6) == -1) {
*error_num = NO_RECOVERY;
return (NULL);
}
/* Check for literal address. */
if ((v4 = lwres_net_pton(AF_INET, name, &in4)) != 1)
v6 = lwres_net_pton(AF_INET6, name, &in6);
/*
* Impossible combination?
*/
if ((af == AF_INET6 && (flags & AI_V4MAPPED) == 0 && v4 == 1) ||
(af == AF_INET && v6 == 1) ||
(have_v4 == 0 && v4 == 1) ||
(have_v6 == 0 && v6 == 1) ||
(have_v4 == 0 && af == AF_INET) ||
(have_v6 == 0 && af == AF_INET6 &&
(((flags & AI_V4MAPPED) != 0 && have_v4) ||
(flags & AI_V4MAPPED) == 0))) {
*error_num = HOST_NOT_FOUND;
return (NULL);
}
/*
* Literal address?
*/
if (v4 == 1 || v6 == 1) {
char *addr_list[2];
char *aliases[1];
union {
const char *const_name;
char *deconst_name;
} u;
u.const_name = name;
he.h_name = u.deconst_name;
he.h_addr_list = addr_list;
he.h_addr_list[0] = (v4 == 1) ? (char *)&in4 : (char *)&in6;
he.h_addr_list[1] = NULL;
he.h_aliases = aliases;
he.h_aliases[0] = NULL;
he.h_length = (v4 == 1) ? INADDRSZ : IN6ADDRSZ;
he.h_addrtype = (v4 == 1) ? AF_INET : AF_INET6;
return (copyandmerge(&he, NULL, af, error_num));
}
n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
if (n != 0) {
*error_num = NO_RECOVERY;
goto cleanup;
}
(void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
tmp_err = NO_RECOVERY;
if (have_v6 && af == AF_INET6) {
n = lwres_getaddrsbyname(lwrctx, name, LWRES_ADDRTYPE_V6, &by);
if (n == 0) {
he1 = hostfromname(by, AF_INET6);
lwres_gabnresponse_free(lwrctx, &by);
if (he1 == NULL) {
*error_num = NO_RECOVERY;
goto cleanup;
}
} else {
tmp_err = HOST_NOT_FOUND;
}
}
if (have_v4 &&
((af == AF_INET) ||
(af == AF_INET6 && (flags & AI_V4MAPPED) != 0 &&
(he1 == NULL || (flags & AI_ALL) != 0)))) {
n = lwres_getaddrsbyname(lwrctx, name, LWRES_ADDRTYPE_V4, &by);
if (n == 0) {
he2 = hostfromname(by, AF_INET);
lwres_gabnresponse_free(lwrctx, &by);
if (he2 == NULL) {
*error_num = NO_RECOVERY;
goto cleanup;
}
} else if (he1 == NULL) {
if (n == LWRES_R_NOTFOUND)
*error_num = HOST_NOT_FOUND;
else
*error_num = NO_RECOVERY;
goto cleanup;
}
} else
*error_num = tmp_err;
he3 = copyandmerge(he1, he2, af, error_num);
cleanup:
if (he1 != NULL)
lwres_freehostent(he1);
if (he2 != NULL)
lwres_freehostent(he2);
if (lwrctx != NULL) {
lwres_conf_clear(lwrctx);
lwres_context_destroy(&lwrctx);
}
return (he3);
}
struct hostent *
lwres_getipnodebyaddr(const void *src, size_t len, int af, int *error_num) {
struct hostent *he1, *he2;
lwres_context_t *lwrctx = NULL;
lwres_gnbaresponse_t *by = NULL;
lwres_result_t n;
union {
const void *konst;
struct in6_addr *in6;
} u;
/*
* Sanity checks.
*/
if (src == NULL) {
*error_num = NO_RECOVERY;
return (NULL);
}
switch (af) {
case AF_INET:
if (len != INADDRSZ) {
*error_num = NO_RECOVERY;
return (NULL);
}
break;
case AF_INET6:
if (len != IN6ADDRSZ) {
*error_num = NO_RECOVERY;
return (NULL);
}
break;
default:
*error_num = NO_RECOVERY;
return (NULL);
}
/*
* The de-"const"-ing game is done because at least one
* vendor's system (RedHat 6.0) defines the IN6_IS_ADDR_*
* macros in such a way that they discard the const with
* internal casting, and gcc ends up complaining. Rather
* than replacing their own (possibly optimized) definitions
* with our own, cleanly discarding the const is the easiest
* thing to do.
*/
u.konst = src;
/*
* Look up IPv4 and IPv4 mapped/compatible addresses.
*/
if ((af == AF_INET6 && IN6_IS_ADDR_V4COMPAT(u.in6)) ||
(af == AF_INET6 && IN6_IS_ADDR_V4MAPPED(u.in6)) ||
(af == AF_INET)) {
const unsigned char *cp = src;
if (af == AF_INET6)
cp += 12;
n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
if (n == LWRES_R_SUCCESS)
(void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
if (n == LWRES_R_SUCCESS)
n = lwres_getnamebyaddr(lwrctx, LWRES_ADDRTYPE_V4,
INADDRSZ, cp, &by);
if (n != LWRES_R_SUCCESS) {
lwres_conf_clear(lwrctx);
lwres_context_destroy(&lwrctx);
if (n == LWRES_R_NOTFOUND)
*error_num = HOST_NOT_FOUND;
else
*error_num = NO_RECOVERY;
return (NULL);
}
he1 = hostfromaddr(by, AF_INET, cp);
lwres_gnbaresponse_free(lwrctx, &by);
lwres_conf_clear(lwrctx);
lwres_context_destroy(&lwrctx);
if (af != AF_INET6)
return (he1);
/*
* Convert from AF_INET to AF_INET6.
*/
he2 = copyandmerge(he1, NULL, af, error_num);
lwres_freehostent(he1);
if (he2 == NULL)
return (NULL);
/*
* Restore original address.
*/
memcpy(he2->h_addr, src, len);
return (he2);
}
/*
* Lookup IPv6 address.
*/
if (memcmp(src, &in6addr_any, IN6ADDRSZ) == 0) {
*error_num = HOST_NOT_FOUND;
return (NULL);
}
n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
if (n == LWRES_R_SUCCESS)
(void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
if (n == LWRES_R_SUCCESS)
n = lwres_getnamebyaddr(lwrctx, LWRES_ADDRTYPE_V6, IN6ADDRSZ,
src, &by);
if (n != 0) {
*error_num = HOST_NOT_FOUND;
return (NULL);
}
he1 = hostfromaddr(by, AF_INET6, src);
lwres_gnbaresponse_free(lwrctx, &by);
if (he1 == NULL)
*error_num = NO_RECOVERY;
lwres_context_destroy(&lwrctx);
return (he1);
}
void
lwres_freehostent(struct hostent *he) {
char **cpp;
int names = 1;
int addresses = 1;
free(he->h_name);
cpp = he->h_addr_list;
while (*cpp != NULL) {
free(*cpp);
*cpp = NULL;
cpp++;
addresses++;
}
cpp = he->h_aliases;
while (*cpp != NULL) {
free(*cpp);
cpp++;
names++;
}
free(he->h_aliases);
free(he->h_addr_list);
free(he);
}
/*
* Private
*/
/*
* Scan the interface table and set have_v4 and have_v6 depending
* upon whether there are IPv4 and IPv6 interface addresses.
*
* Returns:
* 0 on success
* -1 on failure.
*/
static int
scan_interfaces(int *have_v4, int *have_v6) {
#if 1
*have_v4 = *have_v6 = 1;
return (0);
#else
struct ifconf ifc;
struct ifreq ifreq;
struct in_addr in4;
struct in6_addr in6;
char *buf = NULL, *cp, *cplim;
static int bufsiz = 4095;
int s, cpsize, n;
/*
* Set to zero. Used as loop terminators below.
*/
*have_v4 = *have_v6 = 0;
/*
* Get interface list from system.
*/
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
goto err_ret;
/*
* Grow buffer until large enough to contain all interface
* descriptions.
*/
for (;;) {
buf = malloc(bufsiz);
if (buf == NULL)
goto err_ret;
ifc.ifc_len = bufsiz;
ifc.ifc_buf = buf;
#ifdef IRIX_EMUL_IOCTL_SIOCGIFCONF
/*
* This is a fix for IRIX OS in which the call to ioctl with
* the flag SIOCGIFCONF may not return an entry for all the
* interfaces like most flavors of Unix.
*/
if (emul_ioctl(&ifc) >= 0)
break;
#else
if ((n = ioctl(s, SIOCGIFCONF, (char *)&ifc)) != -1) {
/*
* Some OS's just return what will fit rather
* than set EINVAL if the buffer is too small
* to fit all the interfaces in. If
* ifc.ifc_len is too near to the end of the
* buffer we will grow it just in case and
* retry.
*/
if (ifc.ifc_len + 2 * sizeof(ifreq) < bufsiz)
break;
}
#endif
if ((n == -1) && errno != EINVAL)
goto err_ret;
if (bufsiz > 1000000)
goto err_ret;
free(buf);
bufsiz += 4096;
}
/*
* Parse system's interface list.
*/
cplim = buf + ifc.ifc_len; /* skip over if's with big ifr_addr's */
for (cp = buf;
(*have_v4 == 0 || *have_v6 == 0) && cp < cplim;
cp += cpsize) {
memcpy(&ifreq, cp, sizeof ifreq);
#ifdef LWRES_PLATFORM_HAVESALEN
#ifdef FIX_ZERO_SA_LEN
if (ifreq.ifr_addr.sa_len == 0)
ifreq.ifr_addr.sa_len = IN6ADDRSZ;
#endif
#ifdef HAVE_MINIMUM_IFREQ
cpsize = sizeof ifreq;
if (ifreq.ifr_addr.sa_len > sizeof (struct sockaddr))
cpsize += (int)ifreq.ifr_addr.sa_len -
(int)(sizeof(struct sockaddr));
#else
cpsize = sizeof ifreq.ifr_name + ifreq.ifr_addr.sa_len;
#endif /* HAVE_MINIMUM_IFREQ */
#elif defined SIOCGIFCONF_ADDR
cpsize = sizeof ifreq;
#else
cpsize = sizeof ifreq.ifr_name;
/* XXX maybe this should be a hard error? */
if (ioctl(s, SIOCGIFADDR, (char *)&ifreq) < 0)
continue;
#endif /* LWRES_PLATFORM_HAVESALEN */
switch (ifreq.ifr_addr.sa_family) {
case AF_INET:
if (*have_v4 == 0) {
memcpy(&in4,
&((struct sockaddr_in *)
&ifreq.ifr_addr)->sin_addr,
sizeof(in4));
if (in4.s_addr == INADDR_ANY)
break;
n = ioctl(s, SIOCGIFFLAGS, (char *)&ifreq);
if (n < 0)
break;
if ((ifreq.ifr_flags & IFF_UP) == 0)
break;
*have_v4 = 1;
}
break;
case AF_INET6:
if (*have_v6 == 0) {
memcpy(&in6,
&((struct sockaddr_in6 *)
&ifreq.ifr_addr)->sin6_addr,
sizeof(in6));
if (memcmp(&in6, &in6addr_any,
sizeof(in6)) == 0)
break;
n = ioctl(s, SIOCGIFFLAGS, (char *)&ifreq);
if (n < 0)
break;
if ((ifreq.ifr_flags & IFF_UP) == 0)
break;
*have_v6 = 1;
}
break;
}
}
if (buf != NULL)
free(buf);
close(s);
return (0);
err_ret:
if (buf != NULL)
free(buf);
if (s != -1)
close(s);
return (-1);
#endif
}
static struct hostent *
copyandmerge(struct hostent *he1, struct hostent *he2, int af, int *error_num)
{
struct hostent *he = NULL;
int addresses = 1; /* NULL terminator */
int names = 1; /* NULL terminator */
int len = 0;
char **cpp, **npp;
/*
* Work out array sizes.
*/
if (he1 != NULL) {
cpp = he1->h_addr_list;
while (*cpp != NULL) {
addresses++;
cpp++;
}
cpp = he1->h_aliases;
while (*cpp != NULL) {
names++;
cpp++;
}
}
if (he2 != NULL) {
cpp = he2->h_addr_list;
while (*cpp != NULL) {
addresses++;
cpp++;
}
if (he1 == NULL) {
cpp = he2->h_aliases;
while (*cpp != NULL) {
names++;
cpp++;
}
}
}
if (addresses == 1) {
*error_num = NO_ADDRESS;
return (NULL);
}
he = malloc(sizeof *he);
if (he == NULL)
goto no_recovery;
he->h_addr_list = malloc(sizeof(char *) * (addresses));
if (he->h_addr_list == NULL)
goto cleanup0;
memset(he->h_addr_list, 0, sizeof(char *) * (addresses));
/*
* Copy addresses.
*/
npp = he->h_addr_list;
if (he1 != NULL) {
cpp = he1->h_addr_list;
while (*cpp != NULL) {
*npp = malloc((af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
if (*npp == NULL)
goto cleanup1;
/*
* Convert to mapped if required.
*/
if (af == AF_INET6 && he1->h_addrtype == AF_INET) {
memcpy(*npp, in6addr_mapped,
sizeof in6addr_mapped);
memcpy(*npp + sizeof in6addr_mapped, *cpp,
INADDRSZ);
} else {
memcpy(*npp, *cpp,
(af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
}
cpp++;
npp++;
}
}
if (he2 != NULL) {
cpp = he2->h_addr_list;
while (*cpp != NULL) {
*npp = malloc((af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
if (*npp == NULL)
goto cleanup1;
/*
* Convert to mapped if required.
*/
if (af == AF_INET6 && he2->h_addrtype == AF_INET) {
memcpy(*npp, in6addr_mapped,
sizeof in6addr_mapped);
memcpy(*npp + sizeof in6addr_mapped, *cpp,
INADDRSZ);
} else {
memcpy(*npp, *cpp,
(af == AF_INET) ? INADDRSZ : IN6ADDRSZ);
}
cpp++;
npp++;
}
}
he->h_aliases = malloc(sizeof(char *) * (names));
if (he->h_aliases == NULL)
goto cleanup1;
memset(he->h_aliases, 0, sizeof(char *) * (names));
/*
* Copy aliases.
*/
npp = he->h_aliases;
cpp = (he1 != NULL) ? he1->h_aliases : he2->h_aliases;
while (*cpp != NULL) {
len = strlen (*cpp) + 1;
*npp = malloc(len);
if (*npp == NULL)
goto cleanup2;
strcpy(*npp, *cpp);
npp++;
cpp++;
}
/*
* Copy hostname.
*/
he->h_name = malloc(strlen((he1 != NULL) ?
he1->h_name : he2->h_name) + 1);
if (he->h_name == NULL)
goto cleanup2;
strcpy(he->h_name, (he1 != NULL) ? he1->h_name : he2->h_name);
/*
* Set address type and length.
*/
he->h_addrtype = af;
he->h_length = (af == AF_INET) ? INADDRSZ : IN6ADDRSZ;
return (he);
cleanup2:
cpp = he->h_aliases;
while (*cpp != NULL) {
free(*cpp);
cpp++;
}
free(he->h_aliases);
cleanup1:
cpp = he->h_addr_list;
while (*cpp != NULL) {
free(*cpp);
*cpp = NULL;
cpp++;
}
free(he->h_addr_list);
cleanup0:
free(he);
no_recovery:
*error_num = NO_RECOVERY;
return (NULL);
}
static struct hostent *
hostfromaddr(lwres_gnbaresponse_t *addr, int af, const void *src) {
struct hostent *he;
int i;
he = malloc(sizeof *he);
if (he == NULL)
goto cleanup;
memset(he, 0, sizeof(*he));
/*
* Set family and length.
*/
he->h_addrtype = af;
switch (af) {
case AF_INET:
he->h_length = INADDRSZ;
break;
case AF_INET6:
he->h_length = IN6ADDRSZ;
break;
default:
INSIST(0);
}
/*
* Copy name.
*/
he->h_name = strdup(addr->realname);
if (he->h_name == NULL)
goto cleanup;
/*
* Copy aliases.
*/
he->h_aliases = malloc(sizeof(char *) * (addr->naliases + 1));
if (he->h_aliases == NULL)
goto cleanup;
for (i = 0 ; i < addr->naliases; i++) {
he->h_aliases[i] = strdup(addr->aliases[i]);
if (he->h_aliases[i] == NULL)
goto cleanup;
}
he->h_aliases[i] = NULL;
/*
* Copy address.
*/
he->h_addr_list = malloc(sizeof(char *) * 2);
if (he->h_addr_list == NULL)
goto cleanup;
he->h_addr_list[0] = malloc(he->h_length);
if (he->h_addr_list[0] == NULL)
goto cleanup;
memcpy(he->h_addr_list[0], src, he->h_length);
he->h_addr_list[1] = NULL;
return (he);
cleanup:
if (he != NULL && he->h_addr_list != NULL) {
for (i = 0; he->h_addr_list[i] != NULL; i++)
free(he->h_addr_list[i]);
free(he->h_addr_list);
}
if (he != NULL && he->h_aliases != NULL) {
for (i = 0; he->h_aliases[i] != NULL; i++)
free(he->h_aliases[i]);
free(he->h_aliases);
}
if (he != NULL && he->h_name != NULL)
free(he->h_name);
if (he != NULL)
free(he);
return (NULL);
}
static struct hostent *
hostfromname(lwres_gabnresponse_t *name, int af) {
struct hostent *he;
int i;
lwres_addr_t *addr;
he = malloc(sizeof *he);
if (he == NULL)
goto cleanup;
memset(he, 0, sizeof(*he));
/*
* Set family and length.
*/
he->h_addrtype = af;
switch (af) {
case AF_INET:
he->h_length = INADDRSZ;
break;
case AF_INET6:
he->h_length = IN6ADDRSZ;
break;
default:
INSIST(0);
}
/*
* Copy name.
*/
he->h_name = strdup(name->realname);
if (he->h_name == NULL)
goto cleanup;
/*
* Copy aliases.
*/
he->h_aliases = malloc(sizeof(char *) * (name->naliases + 1));
for (i = 0 ; i < name->naliases; i++) {
he->h_aliases[i] = strdup(name->aliases[i]);
if (he->h_aliases[i] == NULL)
goto cleanup;
}
he->h_aliases[i] = NULL;
/*
* Copy addresses.
*/
he->h_addr_list = malloc(sizeof(char *) * (name->naddrs + 1));
addr = LWRES_LIST_HEAD(name->addrs);
i = 0;
while (addr != NULL) {
he->h_addr_list[i] = malloc(he->h_length);
if (he->h_addr_list[i] == NULL)
goto cleanup;
memcpy(he->h_addr_list[i], addr->address, he->h_length);
addr = LWRES_LIST_NEXT(addr, link);
i++;
}
he->h_addr_list[i] = NULL;
return (he);
cleanup:
if (he != NULL && he->h_addr_list != NULL) {
for (i = 0; he->h_addr_list[i] != NULL; i++)
free(he->h_addr_list[i]);
free(he->h_addr_list);
}
if (he != NULL && he->h_aliases != NULL) {
for (i = 0; he->h_aliases[i] != NULL; i++)
free(he->h_aliases[i]);
free(he->h_aliases);
}
if (he != NULL && he->h_name != NULL)
free(he->h_name);
if (he != NULL)
free(he);
return (NULL);
}
+289
View File
@@ -0,0 +1,289 @@
/*
* Portions Copyright (C) 1999-2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: getnameinfo.c,v 1.1 2004/03/15 20:35:25 as Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by WIDE Project and
* its contributors.
* 4. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* XXX
* Issues to be discussed:
* - Return values. There seems to be no standard for return value (RFC2553)
* but INRIA implementation returns EAI_xxx defined for getaddrinfo().
*/
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <lwres/lwres.h>
#include <lwres/net.h>
#include <lwres/netdb.h>
#include "assert_p.h"
#define SUCCESS 0
static struct afd {
int a_af;
size_t a_addrlen;
size_t a_socklen;
} afdl [] = {
/*
* First entry is linked last...
*/
{ AF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in) },
{ AF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6) },
{0, 0, 0},
};
#define ENI_NOSERVNAME 1
#define ENI_NOHOSTNAME 2
#define ENI_MEMORY 3
#define ENI_SYSTEM 4
#define ENI_FAMILY 5
#define ENI_SALEN 6
#define ENI_NOSOCKET 7
/*
* The test against 0 is there to keep the Solaris compiler
* from complaining about "end-of-loop code not reached".
*/
#define ERR(code) \
do { result = (code); \
if (result != 0) goto cleanup; \
} while (0)
int
lwres_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
size_t hostlen, char *serv, size_t servlen, int flags)
{
struct afd *afd;
struct servent *sp;
unsigned short port;
#ifdef LWRES_PLATFORM_HAVESALEN
size_t len;
#endif
int family, i;
const void *addr;
char *p;
#if 0
unsigned long v4a;
unsigned char pfx;
#endif
char numserv[sizeof("65000")];
char numaddr[sizeof("abcd:abcd:abcd:abcd:abcd:abcd:255.255.255.255")
+ 1 + sizeof("4294967295")];
const char *proto;
lwres_uint32_t lwf = 0;
lwres_context_t *lwrctx = NULL;
lwres_gnbaresponse_t *by = NULL;
int result = SUCCESS;
int n;
if (sa == NULL)
ERR(ENI_NOSOCKET);
#ifdef LWRES_PLATFORM_HAVESALEN
len = sa->sa_len;
if (len != salen)
ERR(ENI_SALEN);
#endif
family = sa->sa_family;
for (i = 0; afdl[i].a_af; i++)
if (afdl[i].a_af == family) {
afd = &afdl[i];
goto found;
}
ERR(ENI_FAMILY);
found:
if (salen != afd->a_socklen)
ERR(ENI_SALEN);
switch (family) {
case AF_INET:
port = ((const struct sockaddr_in *)sa)->sin_port;
addr = &((const struct sockaddr_in *)sa)->sin_addr.s_addr;
break;
case AF_INET6:
port = ((const struct sockaddr_in6 *)sa)->sin6_port;
addr = ((const struct sockaddr_in6 *)sa)->sin6_addr.s6_addr;
break;
default:
port = 0;
addr = NULL;
INSIST(0);
}
proto = (flags & NI_DGRAM) ? "udp" : "tcp";
if (serv == NULL || servlen == 0) {
/*
* Caller does not want service.
*/
} else if ((flags & NI_NUMERICSERV) != 0 ||
(sp = getservbyport(port, proto)) == NULL) {
sprintf(numserv, "%d", ntohs(port));
if ((strlen(numserv) + 1) > servlen)
ERR(ENI_MEMORY);
strcpy(serv, numserv);
} else {
if ((strlen(sp->s_name) + 1) > servlen)
ERR(ENI_MEMORY);
strcpy(serv, sp->s_name);
}
#if 0
switch (sa->sa_family) {
case AF_INET:
v4a = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
flags |= NI_NUMERICHOST;
v4a >>= IN_CLASSA_NSHIFT;
if (v4a == 0 || v4a == IN_LOOPBACKNET)
flags |= NI_NUMERICHOST;
break;
case AF_INET6:
pfx = ((struct sockaddr_in6 *)sa)->sin6_addr.s6_addr[0];
if (pfx == 0 || pfx == 0xfe || pfx == 0xff)
flags |= NI_NUMERICHOST;
break;
}
#endif
if (host == NULL || hostlen == 0) {
/*
* What should we do?
*/
} else if (flags & NI_NUMERICHOST) {
if (lwres_net_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
== NULL)
ERR(ENI_SYSTEM);
#if defined(LWRES_HAVE_SIN6_SCOPE_ID)
if (afd->a_af == AF_INET6 &&
((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
char *p = numaddr + strlen(numaddr);
const char *stringscope = NULL;
#if 0
if ((flags & NI_NUMERICSCOPE) == 0) {
/*
* Vendors may want to add support for
* non-numeric scope identifier.
*/
stringscope = foo;
}
#endif
if (stringscope == NULL) {
snprintf(p, sizeof(numaddr) - (p - numaddr),
"%%%u",
((const struct sockaddr_in6 *)sa)->sin6_scope_id);
} else {
snprintf(p, sizeof(numaddr) - (p - numaddr),
"%%%s", stringscope);
}
}
#endif
if (strlen(numaddr) + 1 > hostlen)
ERR(ENI_MEMORY);
strcpy(host, numaddr);
} else {
switch (family) {
case AF_INET:
lwf = LWRES_ADDRTYPE_V4;
break;
case AF_INET6:
lwf = LWRES_ADDRTYPE_V6;
break;
default:
INSIST(0);
}
n = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
if (n == 0)
(void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
if (n == 0)
n = lwres_getnamebyaddr(lwrctx, lwf,
(lwres_uint16_t)afd->a_addrlen,
addr, &by);
if (n == 0) {
if (flags & NI_NOFQDN) {
p = strchr(by->realname, '.');
if (p)
*p = '\0';
}
if ((strlen(by->realname) + 1) > hostlen)
ERR(ENI_MEMORY);
strcpy(host, by->realname);
} else {
if (flags & NI_NAMEREQD)
ERR(ENI_NOHOSTNAME);
if (lwres_net_ntop(afd->a_af, addr, numaddr,
sizeof(numaddr))
== NULL)
ERR(ENI_NOHOSTNAME);
if ((strlen(numaddr) + 1) > hostlen)
ERR(ENI_MEMORY);
strcpy(host, numaddr);
}
}
result = SUCCESS;
cleanup:
if (by != NULL)
lwres_gnbaresponse_free(lwrctx, &by);
if (lwrctx != NULL) {
lwres_conf_clear(lwrctx);
lwres_context_destroy(&lwrctx);
}
return (result);
}
+211
View File
@@ -0,0 +1,211 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: getrrset.c,v 1.1 2004/03/15 20:35:25 as Exp $ */
#include <config.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <lwres/lwres.h>
#include <lwres/net.h>
#include <lwres/netdb.h> /* XXX #include <netdb.h> */
#include "assert_p.h"
unsigned int
lwresult_to_result(lwres_result_t lwresult) {
switch (lwresult) {
case LWRES_R_SUCCESS: return (ERRSET_SUCCESS);
case LWRES_R_NOMEMORY: return (ERRSET_NOMEMORY);
case LWRES_R_NOTFOUND: return (ERRSET_NONAME);
case LWRES_R_TYPENOTFOUND: return (ERRSET_NODATA);
case LWRES_R_RETRY: return (ERRSET_RETRY);
default: return (ERRSET_FAIL);
}
}
/*
* malloc / calloc functions that guarantee to only
* return NULL if there is an error, like they used
* to before the ANSI C committee broke them.
*/
static void *
sane_malloc(size_t size) {
if (size == 0)
size = 1;
return (malloc(size));
}
static void *
sane_calloc(size_t number, size_t size) {
size_t len = number * size;
void *mem = sane_malloc(len);
if (mem != NULL)
memset(mem, 0, len);
return (mem);
}
int
lwres_getrrsetbyname(const char *hostname, unsigned int rdclass,
unsigned int rdtype, unsigned int flags,
struct rrsetinfo **res)
{
lwres_context_t *lwrctx = NULL;
lwres_result_t lwresult;
lwres_grbnresponse_t *response = NULL;
struct rrsetinfo *rrset = NULL;
unsigned int i;
unsigned int lwflags;
unsigned int result;
if (rdclass > 0xffff || rdtype > 0xffff) {
result = ERRSET_INVAL;
goto fail;
}
/*
* Don't allow queries of class or type ANY
*/
if (rdclass == 0xff || rdtype == 0xff) {
result = ERRSET_INVAL;
goto fail;
}
lwresult = lwres_context_create(&lwrctx, NULL, NULL, NULL, 0);
if (lwresult != LWRES_R_SUCCESS) {
result = lwresult_to_result(lwresult);
goto fail;
}
(void) lwres_conf_parse(lwrctx, lwres_resolv_conf);
/*
* If any input flags were defined, lwflags would be set here
* based on them
*/
UNUSED(flags);
lwflags = 0;
lwresult = lwres_getrdatabyname(lwrctx, hostname,
(lwres_uint16_t)rdclass,
(lwres_uint16_t)rdtype,
lwflags, &response);
if (lwresult != LWRES_R_SUCCESS) {
result = lwresult_to_result(lwresult);
goto fail;
}
rrset = sane_malloc(sizeof(struct rrsetinfo));
if (rrset == NULL) {
result = ERRSET_NOMEMORY;
goto fail;
}
rrset->rri_name = NULL;
rrset->rri_rdclass = response->rdclass;
rrset->rri_rdtype = response->rdtype;
rrset->rri_ttl = response->ttl;
rrset->rri_flags = 0;
rrset->rri_nrdatas = 0;
rrset->rri_rdatas = NULL;
rrset->rri_nsigs = 0;
rrset->rri_sigs = NULL;
rrset->rri_name = sane_malloc(response->realnamelen + 1);
if (rrset->rri_name == NULL) {
result = ERRSET_NOMEMORY;
goto fail;
}
strncpy(rrset->rri_name, response->realname, response->realnamelen);
rrset->rri_name[response->realnamelen] = 0;
if ((response->flags & LWRDATA_VALIDATED) != 0)
rrset->rri_flags |= RRSET_VALIDATED;
rrset->rri_nrdatas = response->nrdatas;
rrset->rri_rdatas = sane_calloc(rrset->rri_nrdatas,
sizeof(struct rdatainfo));
if (rrset->rri_rdatas == NULL) {
result = ERRSET_NOMEMORY;
goto fail;
}
for (i = 0; i < rrset->rri_nrdatas; i++) {
rrset->rri_rdatas[i].rdi_length = response->rdatalen[i];
rrset->rri_rdatas[i].rdi_data =
sane_malloc(rrset->rri_rdatas[i].rdi_length);
if (rrset->rri_rdatas[i].rdi_data == NULL) {
result = ERRSET_NOMEMORY;
goto fail;
}
memcpy(rrset->rri_rdatas[i].rdi_data, response->rdatas[i],
rrset->rri_rdatas[i].rdi_length);
}
rrset->rri_nsigs = response->nsigs;
rrset->rri_sigs = sane_calloc(rrset->rri_nsigs,
sizeof(struct rdatainfo));
if (rrset->rri_sigs == NULL) {
result = ERRSET_NOMEMORY;
goto fail;
}
for (i = 0; i < rrset->rri_nsigs; i++) {
rrset->rri_sigs[i].rdi_length = response->siglen[i];
rrset->rri_sigs[i].rdi_data =
sane_malloc(rrset->rri_sigs[i].rdi_length);
if (rrset->rri_sigs[i].rdi_data == NULL) {
result = ERRSET_NOMEMORY;
goto fail;
}
memcpy(rrset->rri_sigs[i].rdi_data, response->sigs[i],
rrset->rri_sigs[i].rdi_length);
}
lwres_grbnresponse_free(lwrctx, &response);
lwres_context_destroy(&lwrctx);
*res = rrset;
return (ERRSET_SUCCESS);
fail:
if (rrset != NULL)
lwres_freerrset(rrset);
if (response != NULL)
lwres_grbnresponse_free(lwrctx, &response);
if (lwrctx != NULL)
lwres_context_destroy(&lwrctx);
return (result);
}
void
lwres_freerrset(struct rrsetinfo *rrset) {
unsigned int i;
for (i = 0; i < rrset->rri_nrdatas; i++) {
if (rrset->rri_rdatas[i].rdi_data == NULL)
break;
free(rrset->rri_rdatas[i].rdi_data);
}
free(rrset->rri_rdatas);
for (i = 0; i < rrset->rri_nsigs; i++) {
if (rrset->rri_sigs[i].rdi_data == NULL)
break;
free(rrset->rri_sigs[i].rdi_data);
}
free(rrset->rri_sigs);
free(rrset->rri_name);
free(rrset);
}
+97
View File
@@ -0,0 +1,97 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: getrrset2.c,v 1.1 2004/03/15 20:35:25 as Exp $ */
#include <config.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <lwres/lwres.h>
#include <lwres/net.h>
#include <lwres/netdb.h> /* XXX #include <netdb.h> */
#include <lwres/async.h>
#include "assert_p.h"
int
lwres_getrrsetbyname_async(const char *hostname, unsigned int rdclass,
unsigned int rdtype, unsigned int flags,
struct rrsetinfo **res)
{
int ret, ret2;
lwres_context_t *ctx = NULL;
struct lwres_async_state las;
struct lwres_async_state *plas;
struct timeval timeout;
fd_set readfds;
int sock;
ret = lwres_async_init(&ctx);
if(ret != ERRSET_SUCCESS) {
return(ret);
}
ret = lwres_getrrsetbyname_init(hostname, rdclass,
rdtype, flags,
ctx, &las);
if(ret != ERRSET_SUCCESS) {
return ret;
}
again:
lwres_getrrsetbyname_xmit(ctx, &las);
timeout.tv_sec = lwres_async_timeout(ctx);
sock = lwres_async_fd(ctx);
FD_ZERO(&readfds);
FD_SET(sock, &readfds);
ret2 = select(sock + 1, &readfds, NULL, NULL, &timeout);
/*
* What happened with select?
*/
if (ret2 < 0) {
ret = LWRES_R_IOERROR;
goto out3;
}
if (ret2 == 0) {
ret = LWRES_R_TIMEOUT;
goto out3;
}
ret = lwres_getrrsetbyname_read(&plas, ctx, res);
if(ret == LWRES_R_RETRY) {
/* XXX retransmit */
goto again;
}
out3:
/* clean stuff up */
out:
if (ctx != NULL)
lwres_context_destroy(&ctx);
return ret;
}
+101
View File
@@ -0,0 +1,101 @@
/*
* Portions Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Copyright (c) 1987, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static const char sccsid[] = "@(#)herror.c 8.1 (Berkeley) 6/4/93";
static const char rcsid[] =
"$Id: herror.c,v 1.1 2004/03/15 20:35:25 as Exp $";
#endif /* LIBC_SCCS and not lint */
#include <config.h>
#include <stdio.h>
#include <lwres/netdb.h>
#include <lwres/platform.h>
LIBLWRES_EXTERNAL_DATA int lwres_h_errno;
/*
* these have never been declared in any header file so make them static
*/
static const char *h_errlist[] = {
"Resolver Error 0 (no error)",
"Unknown host", /* 1 HOST_NOT_FOUND */
"Host name lookup failure", /* 2 TRY_AGAIN */
"Unknown server error", /* 3 NO_RECOVERY */
"No address associated with name", /* 4 NO_ADDRESS */
};
static int h_nerr = { sizeof h_errlist / sizeof h_errlist[0] };
/*
* herror --
* print the error indicated by the h_errno value.
*/
void
lwres_herror(const char *s) {
fprintf(stderr, "%s: %s\n", s, lwres_hstrerror(lwres_h_errno));
}
/*
* hstrerror --
* return the string associated with a given "host" errno value.
*/
const char *
lwres_hstrerror(int err) {
if (err < 0)
return ("Resolver internal error");
else if (err < h_nerr)
return (h_errlist[err]);
return ("Unknown resolver error");
}
+78
View File
@@ -0,0 +1,78 @@
/*
* Copyright (C) 2003 Michael Richardson
* Contributed by Michael Richardson <mcr@freeswan.org> while working
* on the Linux FreeS/WAN project in 2003.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: async.h,v 1.1 2004/03/15 20:35:25 as Exp $ */
#ifndef LWRES_ASYNC_H
#define LWRES_ASYNC_H 1
#include <lwres/lwres.h>
/*
* support for asynchronous requests to lwres port
*/
struct lwres_async_state {
struct lwres_async_state *next;
lwres_buffer_t b_in, b_out;
lwres_uint32_t serial;
int opcode;
int (*callback)(void *uctx, struct rrsetinfo *res);
void *uctx;
};
/*
* The calls for asynchronous requests.
*/
int lwres_async_init(lwres_context_t **pctx);
int lwres_getrrsetbyname_init(const char *hostname, unsigned int rdclass,
unsigned int rdtype, unsigned int flags,
lwres_context_t *ctx,
struct lwres_async_state *las);
int lwres_getrrsetbyname_xmit(lwres_context_t *ctx,
struct lwres_async_state *las);
unsigned long lwres_async_timeout(lwres_context_t *ctx);
int lwres_async_fd(lwres_context_t *ctx);
int lwres_getrrsetbyname_read(struct lwres_async_state **plas,
lwres_context_t *ctx,
struct rrsetinfo **res);
int lwres_getrrsetbyname_async(const char *hostname, unsigned int rdclass,
unsigned int rdtype, unsigned int flags,
struct rrsetinfo **res);
#endif /* LWRES_ASYNC_H */
+133
View File
@@ -0,0 +1,133 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: context.h,v 1.1 2004/03/15 20:35:25 as Exp $ */
#ifndef LWRES_CONTEXT_H
#define LWRES_CONTEXT_H 1
#include <stddef.h>
#include <lwres/lang.h>
#include <lwres/int.h>
#include <lwres/result.h>
/*
* Used to set various options such as timeout, authentication, etc
*/
typedef struct lwres_context lwres_context_t;
LWRES_LANG_BEGINDECLS
typedef void *(*lwres_malloc_t)(void *arg, size_t length);
typedef void (*lwres_free_t)(void *arg, void *mem, size_t length);
/*
* XXXMLG
*
* Make the server reload /etc/resolv.conf periodically.
*
* Make the server do sortlist/searchlist.
*
* Client side can disable the search/sortlist processing.
*
* Use an array of addresses/masks and searchlist for client-side, and
* if added to the client disable the processing on the server.
*
* Share /etc/resolv.conf data between contexts.
*/
/*
* _SERVERMODE
* Don't allocate and connect a socket to the server, since the
* caller _is_ a server.
*/
#define LWRES_CONTEXT_SERVERMODE 0x00000001U
lwres_result_t
lwres_context_create(lwres_context_t **contextp, void *arg,
lwres_malloc_t malloc_function,
lwres_free_t free_function,
unsigned int flags);
/*
* Allocate a lwres context. This is used in all lwres calls.
*
* Memory management can be replaced here by passing in two functions.
* If one is non-NULL, they must both be non-NULL. "arg" is passed to
* these functions.
*
* Contexts are not thread safe. Document at the top of the file.
* XXXMLG
*
* If they are NULL, the standard malloc() and free() will be used.
*
* Requires:
*
* contextp != NULL && contextp == NULL.
*
* Returns:
*
* Returns 0 on success, non-zero on failure.
*/
void
lwres_context_destroy(lwres_context_t **contextp);
/*
* Frees all memory associated with a lwres context.
*
* Requires:
*
* contextp != NULL && contextp == NULL.
*/
lwres_uint32_t
lwres_context_nextserial(lwres_context_t *ctx);
/*
* XXXMLG Document
*/
void
lwres_context_initserial(lwres_context_t *ctx, lwres_uint32_t serial);
void
lwres_context_freemem(lwres_context_t *ctx, void *mem, size_t len);
void *
lwres_context_allocmem(lwres_context_t *ctx, size_t len);
int
lwres_context_getsocket(lwres_context_t *ctx);
lwres_result_t
lwres_context_send(lwres_context_t *ctx,
void *sendbase, int sendlen);
lwres_result_t
lwres_context_recv(lwres_context_t *ctx,
void *recvbase, int recvlen,
int *recvd_len);
lwres_result_t
lwres_context_sendrecv(lwres_context_t *ctx,
void *sendbase, int sendlen,
void *recvbase, int recvlen,
int *recvd_len);
LWRES_LANG_ENDDECLS
#endif /* LWRES_CONTEXT_H */
+32
View File
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: int.h,v 1.1 2004/03/15 20:35:25 as Exp $ */
#ifndef LWRES_INT_H
#define LWRES_INT_H 1
typedef char lwres_int8_t;
typedef unsigned char lwres_uint8_t;
typedef short lwres_int16_t;
typedef unsigned short lwres_uint16_t;
typedef int lwres_int32_t;
typedef unsigned int lwres_uint32_t;
typedef long long lwres_int64_t;
typedef unsigned long long lwres_uint64_t;
#endif /* LWRES_INT_H */
+118
View File
@@ -0,0 +1,118 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: ipv6.h,v 1.1 2004/03/15 20:35:25 as Exp $ */
#ifndef LWRES_IPV6_H
#define LWRES_IPV6_H 1
/*****
***** Module Info
*****/
/*
* IPv6 definitions for systems which do not support IPv6.
*/
/***
*** Imports.
***/
#include <lwres/int.h>
#include <lwres/platform.h>
/***
*** Types.
***/
struct in6_addr {
union {
lwres_uint8_t _S6_u8[16];
lwres_uint16_t _S6_u16[8];
lwres_uint32_t _S6_u32[4];
} _S6_un;
};
#define s6_addr _S6_un._S6_u8
#define s6_addr8 _S6_un._S6_u8
#define s6_addr16 _S6_un._S6_u16
#define s6_addr32 _S6_un._S6_u32
#define IN6ADDR_ANY_INIT {{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }}}
#define IN6ADDR_LOOPBACK_INIT {{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }}}
LIBLWRES_EXTERNAL_DATA extern const struct in6_addr in6addr_any;
LIBLWRES_EXTERNAL_DATA extern const struct in6_addr in6addr_loopback;
struct sockaddr_in6 {
#ifdef LWRES_PLATFORM_HAVESALEN
lwres_uint8_t sin6_len;
lwres_uint8_t sin6_family;
#else
lwres_uint16_t sin6_family;
#endif
lwres_uint16_t sin6_port;
lwres_uint32_t sin6_flowinfo;
struct in6_addr sin6_addr;
lwres_uint32_t sin6_scope_id;
};
#ifdef LWRES_PLATFORM_HAVESALEN
#define SIN6_LEN 1
#endif
struct in6_pktinfo {
struct in6_addr ipi6_addr; /* src/dst IPv6 address */
unsigned int ipi6_ifindex; /* send/recv interface index */
};
/*
* Unspecified
*/
#define IN6_IS_ADDR_UNSPECIFIED(a) \
(((a)->s6_addr32[0] == 0) && \
((a)->s6_addr32[1] == 0) && \
((a)->s6_addr32[2] == 0) && \
((a)->s6_addr32[3] == 0))
/*
* Loopback
*/
#define IN6_IS_ADDR_LOOPBACK(a) \
(((a)->s6_addr32[0] == 0) && \
((a)->s6_addr32[1] == 0) && \
((a)->s6_addr32[2] == 0) && \
((a)->s6_addr32[3] == htonl(1)))
/*
* IPv4 compatible
*/
#define IN6_IS_ADDR_V4COMPAT(a) \
(((a)->s6_addr32[0] == 0) && \
((a)->s6_addr32[1] == 0) && \
((a)->s6_addr32[2] == 0) && \
((a)->s6_addr32[3] != 0) && \
((a)->s6_addr32[3] != htonl(1)))
/*
* Mapped
*/
#define IN6_IS_ADDR_V4MAPPED(a) \
(((a)->s6_addr32[0] == 0) && \
((a)->s6_addr32[1] == 0) && \
((a)->s6_addr32[2] == htonl(0x0000ffff)))
#endif /* LWRES_IPV6_H */
+31
View File
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: lang.h,v 1.1 2004/03/15 20:35:25 as Exp $ */
#ifndef LWRES_LANG_H
#define LWRES_LANG_H 1
#ifdef __cplusplus
#define LWRES_LANG_BEGINDECLS extern "C" {
#define LWRES_LANG_ENDDECLS }
#else
#define LWRES_LANG_BEGINDECLS
#define LWRES_LANG_ENDDECLS
#endif
#endif /* LWRES_LANG_H */
+119
View File
@@ -0,0 +1,119 @@
/*
* Copyright (C) 1997-2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: list.h,v 1.1 2004/03/15 20:35:25 as Exp $ */
#ifndef LWRES_LIST_H
#define LWRES_LIST_H 1
#define LWRES_LIST(type) struct { type *head, *tail; }
#define LWRES_LIST_INIT(list) \
do { (list).head = NULL; (list).tail = NULL; } while (0)
#define LWRES_LINK(type) struct { type *prev, *next; }
#define LWRES_LINK_INIT(elt, link) \
do { \
(elt)->link.prev = (void *)(-1); \
(elt)->link.next = (void *)(-1); \
} while (0)
#define LWRES_LINK_LINKED(elt, link) \
((void *)((elt)->link.prev) != (void *)(-1))
#define LWRES_LIST_HEAD(list) ((list).head)
#define LWRES_LIST_TAIL(list) ((list).tail)
#define LWRES_LIST_EMPTY(list) LWRES_TF((list).head == NULL)
#define LWRES_LIST_PREPEND(list, elt, link) \
do { \
if ((list).head != NULL) \
(list).head->link.prev = (elt); \
else \
(list).tail = (elt); \
(elt)->link.prev = NULL; \
(elt)->link.next = (list).head; \
(list).head = (elt); \
} while (0)
#define LWRES_LIST_APPEND(list, elt, link) \
do { \
if ((list).tail != NULL) \
(list).tail->link.next = (elt); \
else \
(list).head = (elt); \
(elt)->link.prev = (list).tail; \
(elt)->link.next = NULL; \
(list).tail = (elt); \
} while (0)
#define LWRES_LIST_UNLINK(list, elt, link) \
do { \
if ((elt)->link.next != NULL) \
(elt)->link.next->link.prev = (elt)->link.prev; \
else \
(list).tail = (elt)->link.prev; \
if ((elt)->link.prev != NULL) \
(elt)->link.prev->link.next = (elt)->link.next; \
else \
(list).head = (elt)->link.next; \
(elt)->link.prev = (void *)(-1); \
(elt)->link.next = (void *)(-1); \
} while (0)
#define LWRES_LIST_PREV(elt, link) ((elt)->link.prev)
#define LWRES_LIST_NEXT(elt, link) ((elt)->link.next)
#define LWRES_LIST_INSERTBEFORE(list, before, elt, link) \
do { \
if ((before)->link.prev == NULL) \
LWRES_LIST_PREPEND(list, elt, link); \
else { \
(elt)->link.prev = (before)->link.prev; \
(before)->link.prev = (elt); \
(elt)->link.prev->link.next = (elt); \
(elt)->link.next = (before); \
} \
} while (0)
#define LWRES_LIST_INSERTAFTER(list, after, elt, link) \
do { \
if ((after)->link.next == NULL) \
LWRES_LIST_APPEND(list, elt, link); \
else { \
(elt)->link.next = (after)->link.next; \
(after)->link.next = (elt); \
(elt)->link.next->link.prev = (elt); \
(elt)->link.prev = (after); \
} \
} while (0)
#define LWRES_LIST_APPENDLIST(list1, list2, link) \
do { \
if (LWRES_LIST_EMPTY(list1)) \
(list1) = (list2); \
else if (!LWRES_LIST_EMPTY(list2)) { \
(list1).tail->link.next = (list2).head; \
(list2).head->link.prev = (list1).tail; \
(list1).tail = (list2).tail; \
} \
(list2).head = NULL; \
(list2).tail = NULL; \
} while (0)
#define LWRES_LIST_ENQUEUE(list, elt, link) LWRES_LIST_APPEND(list, elt, link)
#define LWRES_LIST_DEQUEUE(list, elt, link) LWRES_LIST_UNLINK(list, elt, link)
#endif /* LWRES_LIST_H */
+402
View File
@@ -0,0 +1,402 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: lwbuffer.h,v 1.1 2004/03/15 20:35:25 as Exp $ */
#ifndef LWRES_LWBUFFER_H
#define LWRES_LWBUFFER_H 1
/*****
***** Module Info
*****/
/*
* Buffers
*
* A buffer is a region of memory, together with a set of related subregions.
* Buffers are used for parsing and I/O operations.
*
* The 'used region' and the 'available' region are disjoint, and their
* union is the buffer's region. The used region extends from the beginning
* of the buffer region to the last used byte. The available region
* extends from one byte greater than the last used byte to the end of the
* buffer's region. The size of the used region can be changed using various
* buffer commands. Initially, the used region is empty.
*
* The used region is further subdivided into two disjoint regions: the
* 'consumed region' and the 'remaining region'. The union of these two
* regions is the used region. The consumed region extends from the beginning
* of the used region to the byte before the 'current' offset (if any). The
* 'remaining' region the current pointer to the end of the used
* region. The size of the consumed region can be changed using various
* buffer commands. Initially, the consumed region is empty.
*
* The 'active region' is an (optional) subregion of the remaining region.
* It extends from the current offset to an offset in the remaining region
* that is selected with lwres_buffer_setactive(). Initially, the active
* region is empty. If the current offset advances beyond the chosen offset,
* the active region will also be empty.
*
* /----- used region -----\/-- available --\
* +----------------------------------------+
* | consumed | remaining | |
* +----------------------------------------+
* a b c d e
*
* a == base of buffer.
* b == current pointer. Can be anywhere between a and d.
* c == active pointer. Meaningful between b and d.
* d == used pointer.
* e == length of buffer.
*
* a-e == entire (length) of buffer.
* a-d == used region.
* a-b == consumed region.
* b-d == remaining region.
* b-c == optional active region.
*
* The following invariants are maintained by all routines:
*
* length > 0
*
* base is a valid pointer to length bytes of memory
*
* 0 <= used <= length
*
* 0 <= current <= used
*
* 0 <= active <= used
* (although active < current implies empty active region)
*
* MP:
* Buffers have no synchronization. Clients must ensure exclusive
* access.
*
* Reliability:
* No anticipated impact.
*
* Resources:
* Memory: 1 pointer + 6 unsigned integers per buffer.
*
* Security:
* No anticipated impact.
*
* Standards:
* None.
*/
/***
*** Imports
***/
#include <lwres/lang.h>
#include <lwres/int.h>
LWRES_LANG_BEGINDECLS
/***
*** Magic numbers
***/
#define LWRES_BUFFER_MAGIC 0x4275663fU /* Buf?. */
#define LWRES_BUFFER_VALID(b) ((b) != NULL && \
(b)->magic == LWRES_BUFFER_MAGIC)
/*
* The following macros MUST be used only on valid buffers. It is the
* caller's responsibility to ensure this by using the LWRES_BUFFER_VALID
* check above, or by calling another lwres_buffer_*() function (rather than
* another macro.)
*/
/*
* Get the length of the used region of buffer "b"
*/
#define LWRES_BUFFER_USEDCOUNT(b) ((b)->used)
/*
* Get the length of the available region of buffer "b"
*/
#define LWRES_BUFFER_AVAILABLECOUNT(b) ((b)->length - (b)->used)
#define LWRES_BUFFER_REMAINING(b) ((b)->used - (b)->current)
/*
* Note that the buffer structure is public. This is principally so buffer
* operations can be implemented using macros. Applications are strongly
* discouraged from directly manipulating the structure.
*/
typedef struct lwres_buffer lwres_buffer_t;
struct lwres_buffer {
unsigned int magic;
unsigned char *base;
/* The following integers are byte offsets from 'base'. */
unsigned int length;
unsigned int used;
unsigned int current;
unsigned int active;
};
/***
*** Functions
***/
void
lwres_buffer_init(lwres_buffer_t *b, void *base, unsigned int length);
/*
* Make 'b' refer to the 'length'-byte region starting at base.
*
* Requires:
*
* 'length' > 0
*
* 'base' is a pointer to a sequence of 'length' bytes.
*
*/
void
lwres_buffer_invalidate(lwres_buffer_t *b);
/*
* Make 'b' an invalid buffer.
*
* Requires:
* 'b' is a valid buffer.
*
* Ensures:
* If assertion checking is enabled, future attempts to use 'b' without
* calling lwres_buffer_init() on it will cause an assertion failure.
*/
void
lwres_buffer_add(lwres_buffer_t *b, unsigned int n);
/*
* Increase the 'used' region of 'b' by 'n' bytes.
*
* Requires:
*
* 'b' is a valid buffer
*
* used + n <= length
*
*/
void
lwres_buffer_subtract(lwres_buffer_t *b, unsigned int n);
/*
* Decrease the 'used' region of 'b' by 'n' bytes.
*
* Requires:
*
* 'b' is a valid buffer
*
* used >= n
*
*/
void
lwres_buffer_clear(lwres_buffer_t *b);
/*
* Make the used region empty.
*
* Requires:
*
* 'b' is a valid buffer
*
* Ensures:
*
* used = 0
*
*/
void
lwres_buffer_first(lwres_buffer_t *b);
/*
* Make the consumed region empty.
*
* Requires:
*
* 'b' is a valid buffer
*
* Ensures:
*
* current == 0
*
*/
void
lwres_buffer_forward(lwres_buffer_t *b, unsigned int n);
/*
* Increase the 'consumed' region of 'b' by 'n' bytes.
*
* Requires:
*
* 'b' is a valid buffer
*
* current + n <= used
*
*/
void
lwres_buffer_back(lwres_buffer_t *b, unsigned int n);
/*
* Decrease the 'consumed' region of 'b' by 'n' bytes.
*
* Requires:
*
* 'b' is a valid buffer
*
* n <= current
*
*/
lwres_uint8_t
lwres_buffer_getuint8(lwres_buffer_t *b);
/*
* Read an unsigned 8-bit integer from 'b' and return it.
*
* Requires:
*
* 'b' is a valid buffer.
*
* The length of the available region of 'b' is at least 1.
*
* Ensures:
*
* The current pointer in 'b' is advanced by 1.
*
* Returns:
*
* A 8-bit unsigned integer.
*/
void
lwres_buffer_putuint8(lwres_buffer_t *b, lwres_uint8_t val);
/*
* Store an unsigned 8-bit integer from 'val' into 'b'.
*
* Requires:
* 'b' is a valid buffer.
*
* The length of the unused region of 'b' is at least 1.
*
* Ensures:
* The used pointer in 'b' is advanced by 1.
*/
lwres_uint16_t
lwres_buffer_getuint16(lwres_buffer_t *b);
/*
* Read an unsigned 16-bit integer in network byte order from 'b', convert
* it to host byte order, and return it.
*
* Requires:
*
* 'b' is a valid buffer.
*
* The length of the available region of 'b' is at least 2.
*
* Ensures:
*
* The current pointer in 'b' is advanced by 2.
*
* Returns:
*
* A 16-bit unsigned integer.
*/
void
lwres_buffer_putuint16(lwres_buffer_t *b, lwres_uint16_t val);
/*
* Store an unsigned 16-bit integer in host byte order from 'val'
* into 'b' in network byte order.
*
* Requires:
* 'b' is a valid buffer.
*
* The length of the unused region of 'b' is at least 2.
*
* Ensures:
* The used pointer in 'b' is advanced by 2.
*/
lwres_uint32_t
lwres_buffer_getuint32(lwres_buffer_t *b);
/*
* Read an unsigned 32-bit integer in network byte order from 'b', convert
* it to host byte order, and return it.
*
* Requires:
*
* 'b' is a valid buffer.
*
* The length of the available region of 'b' is at least 2.
*
* Ensures:
*
* The current pointer in 'b' is advanced by 2.
*
* Returns:
*
* A 32-bit unsigned integer.
*/
void
lwres_buffer_putuint32(lwres_buffer_t *b, lwres_uint32_t val);
/*
* Store an unsigned 32-bit integer in host byte order from 'val'
* into 'b' in network byte order.
*
* Requires:
* 'b' is a valid buffer.
*
* The length of the unused region of 'b' is at least 4.
*
* Ensures:
* The used pointer in 'b' is advanced by 4.
*/
void
lwres_buffer_putmem(lwres_buffer_t *b, const unsigned char *base,
unsigned int length);
/*
* Copy 'length' bytes of memory at 'base' into 'b'.
*
* Requires:
* 'b' is a valid buffer.
*
* 'base' points to 'length' bytes of valid memory.
*
*/
void
lwres_buffer_getmem(lwres_buffer_t *b, unsigned char *base,
unsigned int length);
/*
* Copy 'length' bytes of memory from 'b' into 'base'.
*
* Requires:
* 'b' is a valid buffer.
*
* 'base' points to at least 'length' bytes of valid memory.
*
* 'b' have at least 'length' bytes remaining.
*/
LWRES_LANG_ENDDECLS
#endif /* LWRES_LWBUFFER_H */
+124
View File
@@ -0,0 +1,124 @@
/*
* Copyright (C) 1999-2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: lwpacket.h,v 1.1 2004/03/15 20:35:25 as Exp $ */
#ifndef LWRES_LWPACKET_H
#define LWRES_LWPACKET_H 1
#include <lwres/lang.h>
#include <lwres/lwbuffer.h>
#include <lwres/result.h>
typedef struct lwres_lwpacket lwres_lwpacket_t;
struct lwres_lwpacket {
lwres_uint32_t length;
lwres_uint16_t version;
lwres_uint16_t pktflags;
lwres_uint32_t serial;
lwres_uint32_t opcode;
lwres_uint32_t result;
lwres_uint32_t recvlength;
lwres_uint16_t authtype;
lwres_uint16_t authlength;
};
#define LWRES_LWPACKET_LENGTH (4 * 5 + 2 * 4)
#define LWRES_LWPACKETFLAG_RESPONSE 0x0001U /* if set, pkt is a response */
#define LWRES_LWPACKETVERSION_0 0
/*
* "length" is the overall packet length, including the entire packet header.
*
* "version" specifies the header format. Currently, there is only one
* format, LWRES_LWPACKETVERSION_0.
*
* "flags" specifies library-defined flags for this packet. None of these
* are definable by the caller, but library-defined values can be set by
* the caller. For example, one bit in this field indicates if the packet
* is a request or a response.
*
* "serial" is set by the requestor and is returned in all replies. If two
* packets from the same source have the same serial number and are from
* the same source, they are assumed to be duplicates and the latter ones
* may be dropped. (The library does not do this by default on replies, but
* does so on requests.)
*
* "opcode" is application defined. Opcodes between 0x04000000 and 0xffffffff
* are application defined. Opcodes between 0x00000000 and 0x03ffffff are
* reserved for library use.
*
* "result" is application defined, and valid only on replies.
* Results between 0x04000000 and 0xffffffff are application defined.
* Results between 0x00000000 and 0x03ffffff are reserved for library use.
* (This is the same reserved range defined in <isc/resultclass.h>, so it
* would be trivial to map ISC_R_* result codes into packet result codes
* when appropriate.)
*
* "recvlength" is set to the maximum buffer size that the receiver can
* handle on requests, and the size of the buffer needed to satisfy a request
* when the buffer is too large for replies.
*
* "authtype" is the packet level auth type used.
* Authtypes between 0x1000 and 0xffff are application defined. Authtypes
* between 0x0000 and 0x0fff are reserved for library use. This is currently
* unused and MUST be set to zero.
*
* "authlen" is the length of the authentication data. See the specific
* authtypes for more information on what is contained in this field. This
* is currently unused, and MUST be set to zero.
*
* The remainder of the packet consists of two regions, one described by
* "authlen" and one of "length - authlen - sizeof(lwres_lwpacket_t)".
*
* That is:
*
* pkt header
* authlen bytes of auth information
* data bytes
*/
/*
* Currently defined opcodes:
*
* NOOP. Success is always returned, with the packet contents echoed.
*
* GETADDRSBYNAME. Return all known addresses for a given name.
* This may return NIS or /etc/hosts info as well as DNS
* information. Flags will be provided to indicate ip4/ip6
* addresses are desired.
*
* GETNAMEBYADDR. Return the hostname for the given address. Once
* again, it will return data from multiple sources.
*/
LWRES_LANG_BEGINDECLS
/* XXXMLG document */
lwres_result_t
lwres_lwpacket_renderheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt);
lwres_result_t
lwres_lwpacket_parseheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt);
LWRES_LANG_ENDDECLS
#endif /* LWRES_LWPACKET_H */
+584
View File
@@ -0,0 +1,584 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: lwres.h,v 1.1 2004/03/15 20:35:25 as Exp $ */
#ifndef LWRES_LWRES_H
#define LWRES_LWRES_H 1
#include <stdio.h>
#include <lwres/context.h>
#include <lwres/lang.h>
#include <lwres/list.h>
#include <lwres/lwpacket.h>
/*
* Design notes:
*
* Each opcode has two structures and three functions which operate on each
* structure. For example, using the "no operation/ping" opcode as an
* example:
*
* lwres_nooprequest_t:
*
* lwres_nooprequest_render() takes a lwres_nooprequest_t and
* and renders it into wire format, storing the allocated
* buffer information in a passed-in buffer. When this buffer
* is no longer needed, it must be freed by
* lwres_context_freemem(). All other memory used by the
* caller must be freed manually, including the
* lwres_nooprequest_t passed in.
*
* lwres_nooprequest_parse() takes a wire format message and
* breaks it out into a lwres_nooprequest_t. The structure
* must be freed via lwres_nooprequest_free() when it is no longer
* needed.
*
* lwres_nooprequest_free() releases into the lwres_context_t
* any space allocated during parsing.
*
* lwres_noopresponse_t:
*
* The functions used are similar to the three used for
* requests, just with different names.
*
* Typically, the client will use request_render, response_parse, and
* response_free, while the daemon will use request_parse, response_render,
* and request_free.
*
* The basic flow of a typical client is:
*
* fill in a request_t, and call the render function.
*
* Transmit the buffer returned to the daemon.
*
* Wait for a response.
*
* When a response is received, parse it into a response_t.
*
* free the request buffer using lwres_context_freemem().
*
* free the response structure and its associated buffer using
* response_free().
*/
#define LWRES_UDP_PORT 921
#define LWRES_RECVLENGTH 16384
#define LWRES_ADDR_MAXLEN 16 /* changing this breaks ABI */
#define LWRES_RESOLV_CONF "/etc/resolv.conf"
/*
* Flags.
*
* These flags are only relevant to rrset queries.
*
* TRUSTNOTREQUIRED: DNSSEC is not required (input)
* SECUREDATA: The data was crypto-verified with DNSSEC (output)
*
*/
#define LWRES_FLAG_TRUSTNOTREQUIRED 0x00000001U
#define LWRES_FLAG_SECUREDATA 0x00000002U
/*
* no-op
*/
#define LWRES_OPCODE_NOOP 0x00000000U
typedef struct {
/* public */
lwres_uint16_t datalength;
unsigned char *data;
} lwres_nooprequest_t;
typedef struct {
/* public */
lwres_uint16_t datalength;
unsigned char *data;
} lwres_noopresponse_t;
/*
* get addresses by name
*/
#define LWRES_OPCODE_GETADDRSBYNAME 0x00010001U
typedef struct lwres_addr lwres_addr_t;
typedef LWRES_LIST(lwres_addr_t) lwres_addrlist_t;
struct lwres_addr {
lwres_uint32_t family;
lwres_uint16_t length;
unsigned char address[LWRES_ADDR_MAXLEN];
LWRES_LINK(lwres_addr_t) link;
};
typedef struct {
/* public */
lwres_uint32_t flags;
lwres_uint32_t addrtypes;
lwres_uint16_t namelen;
char *name;
} lwres_gabnrequest_t;
typedef struct {
/* public */
lwres_uint32_t flags;
lwres_uint16_t naliases;
lwres_uint16_t naddrs;
char *realname;
char **aliases;
lwres_uint16_t realnamelen;
lwres_uint16_t *aliaslen;
lwres_addrlist_t addrs;
/* if base != NULL, it will be freed when this structure is freed. */
void *base;
size_t baselen;
} lwres_gabnresponse_t;
/*
* get name by address
*/
#define LWRES_OPCODE_GETNAMEBYADDR 0x00010002U
typedef struct {
/* public */
lwres_uint32_t flags;
lwres_addr_t addr;
} lwres_gnbarequest_t;
typedef struct {
/* public */
lwres_uint32_t flags;
lwres_uint16_t naliases;
char *realname;
char **aliases;
lwres_uint16_t realnamelen;
lwres_uint16_t *aliaslen;
/* if base != NULL, it will be freed when this structure is freed. */
void *base;
size_t baselen;
} lwres_gnbaresponse_t;
/*
* get rdata by name
*/
#define LWRES_OPCODE_GETRDATABYNAME 0x00010003U
typedef struct {
/* public */
lwres_uint32_t flags;
lwres_uint16_t rdclass;
lwres_uint16_t rdtype;
lwres_uint16_t namelen;
char *name;
} lwres_grbnrequest_t;
typedef struct {
/* public */
lwres_uint32_t flags;
lwres_uint16_t rdclass;
lwres_uint16_t rdtype;
lwres_uint32_t ttl;
lwres_uint16_t nrdatas;
lwres_uint16_t nsigs;
char *realname;
lwres_uint16_t realnamelen;
unsigned char **rdatas;
lwres_uint16_t *rdatalen;
unsigned char **sigs;
lwres_uint16_t *siglen;
/* if base != NULL, it will be freed when this structure is freed. */
void *base;
size_t baselen;
} lwres_grbnresponse_t;
#define LWRDATA_VALIDATED 0x00000001
/*
* resolv.conf data
*/
#define LWRES_CONFMAXNAMESERVERS 3 /* max 3 "nameserver" entries */
#define LWRES_CONFMAXLWSERVERS 1 /* max 1 "lwserver" entry */
#define LWRES_CONFMAXSEARCH 8 /* max 8 domains in "search" entry */
#define LWRES_CONFMAXLINELEN 256 /* max size of a line */
#define LWRES_CONFMAXSORTLIST 10
typedef struct {
lwres_context_t *lwctx;
lwres_addr_t nameservers[LWRES_CONFMAXNAMESERVERS];
lwres_uint8_t nsnext; /* index for next free slot */
lwres_addr_t lwservers[LWRES_CONFMAXLWSERVERS];
lwres_uint8_t lwnext; /* index for next free slot */
char *domainname;
char *search[LWRES_CONFMAXSEARCH];
lwres_uint8_t searchnxt; /* index for next free slot */
struct {
lwres_addr_t addr;
/* mask has a non-zero 'family' and 'length' if set */
lwres_addr_t mask;
} sortlist[LWRES_CONFMAXSORTLIST];
lwres_uint8_t sortlistnxt;
lwres_uint8_t resdebug; /* non-zero if 'options debug' set */
lwres_uint8_t ndots; /* set to n in 'options ndots:n' */
lwres_uint8_t no_tld_query; /* non-zero if 'options no_tld_query' */
} lwres_conf_t;
#define LWRES_ADDRTYPE_V4 0x00000001U /* ipv4 */
#define LWRES_ADDRTYPE_V6 0x00000002U /* ipv6 */
#define LWRES_MAX_ALIASES 16 /* max # of aliases */
#define LWRES_MAX_ADDRS 64 /* max # of addrs */
LWRES_LANG_BEGINDECLS
/*
* This is in host byte order.
*/
extern lwres_uint16_t lwres_udp_port;
extern const char *lwres_resolv_conf;
lwres_result_t
lwres_gabnrequest_render(lwres_context_t *ctx, lwres_gabnrequest_t *req,
lwres_lwpacket_t *pkt, lwres_buffer_t *b);
lwres_result_t
lwres_gabnresponse_render(lwres_context_t *ctx, lwres_gabnresponse_t *req,
lwres_lwpacket_t *pkt, lwres_buffer_t *b);
lwres_result_t
lwres_gabnrequest_parse(lwres_context_t *ctx, lwres_buffer_t *b,
lwres_lwpacket_t *pkt, lwres_gabnrequest_t **structp);
lwres_result_t
lwres_gabnresponse_parse(lwres_context_t *ctx, lwres_buffer_t *b,
lwres_lwpacket_t *pkt,
lwres_gabnresponse_t **structp);
void
lwres_gabnrequest_free(lwres_context_t *ctx, lwres_gabnrequest_t **structp);
/*
* Frees any dynamically allocated memory for this structure.
*
* Requires:
*
* ctx != NULL, and be a context returned via lwres_contextcreate().
*
* structp != NULL && *structp != NULL.
*
* Ensures:
*
* *structp == NULL.
*
* All memory allocated by this structure will be returned to the
* system via the context's free function.
*/
void
lwres_gabnresponse_free(lwres_context_t *ctx, lwres_gabnresponse_t **structp);
/*
* Frees any dynamically allocated memory for this structure.
*
* Requires:
*
* ctx != NULL, and be a context returned via lwres_contextcreate().
*
* structp != NULL && *structp != NULL.
*
* Ensures:
*
* *structp == NULL.
*
* All memory allocated by this structure will be returned to the
* system via the context's free function.
*/
lwres_result_t
lwres_gnbarequest_render(lwres_context_t *ctx, lwres_gnbarequest_t *req,
lwres_lwpacket_t *pkt, lwres_buffer_t *b);
lwres_result_t
lwres_gnbaresponse_render(lwres_context_t *ctx, lwres_gnbaresponse_t *req,
lwres_lwpacket_t *pkt, lwres_buffer_t *b);
lwres_result_t
lwres_gnbarequest_parse(lwres_context_t *ctx, lwres_buffer_t *b,
lwres_lwpacket_t *pkt, lwres_gnbarequest_t **structp);
lwres_result_t
lwres_gnbaresponse_parse(lwres_context_t *ctx, lwres_buffer_t *b,
lwres_lwpacket_t *pkt,
lwres_gnbaresponse_t **structp);
void
lwres_gnbarequest_free(lwres_context_t *ctx, lwres_gnbarequest_t **structp);
/*
* Frees any dynamically allocated memory for this structure.
*
* Requires:
*
* ctx != NULL, and be a context returned via lwres_contextcreate().
*
* structp != NULL && *structp != NULL.
*
* Ensures:
*
* *structp == NULL.
*
* All memory allocated by this structure will be returned to the
* system via the context's free function.
*/
void
lwres_gnbaresponse_free(lwres_context_t *ctx, lwres_gnbaresponse_t **structp);
/*
* Frees any dynamically allocated memory for this structure.
*
* Requires:
*
* ctx != NULL, and be a context returned via lwres_contextcreate().
*
* structp != NULL && *structp != NULL.
*
* Ensures:
*
* *structp == NULL.
*
* All memory allocated by this structure will be returned to the
* system via the context's free function.
*/
lwres_result_t
lwres_grbnrequest_render(lwres_context_t *ctx, lwres_grbnrequest_t *req,
lwres_lwpacket_t *pkt, lwres_buffer_t *b);
lwres_result_t
lwres_grbnresponse_render(lwres_context_t *ctx, lwres_grbnresponse_t *req,
lwres_lwpacket_t *pkt, lwres_buffer_t *b);
lwres_result_t
lwres_grbnrequest_parse(lwres_context_t *ctx, lwres_buffer_t *b,
lwres_lwpacket_t *pkt, lwres_grbnrequest_t **structp);
lwres_result_t
lwres_grbnresponse_parse(lwres_context_t *ctx, lwres_buffer_t *b,
lwres_lwpacket_t *pkt,
lwres_grbnresponse_t **structp);
void
lwres_grbnrequest_free(lwres_context_t *ctx, lwres_grbnrequest_t **structp);
/*
* Frees any dynamically allocated memory for this structure.
*
* Requires:
*
* ctx != NULL, and be a context returned via lwres_contextcreate().
*
* structp != NULL && *structp != NULL.
*
* Ensures:
*
* *structp == NULL.
*
* All memory allocated by this structure will be returned to the
* system via the context's free function.
*/
void
lwres_grbnresponse_free(lwres_context_t *ctx, lwres_grbnresponse_t **structp);
/*
* Frees any dynamically allocated memory for this structure.
*
* Requires:
*
* ctx != NULL, and be a context returned via lwres_contextcreate().
*
* structp != NULL && *structp != NULL.
*
* Ensures:
*
* *structp == NULL.
*
* All memory allocated by this structure will be returned to the
* system via the context's free function.
*/
lwres_result_t
lwres_nooprequest_render(lwres_context_t *ctx, lwres_nooprequest_t *req,
lwres_lwpacket_t *pkt, lwres_buffer_t *b);
/*
* Allocate space and render into wire format a noop request packet.
*
* Requires:
*
* ctx != NULL, and be a context returned via lwres_contextcreate().
*
* b != NULL, and points to a lwres_buffer_t. The contents of the
* buffer structure will be initialized to contain the wire-format
* noop request packet.
*
* Caller needs to fill in parts of "pkt" before calling:
* serial, maxrecv, result.
*
* Returns:
*
* Returns 0 on success, non-zero on failure.
*
* On successful return, *b will contain data about the wire-format
* packet. It can be transmitted in any way, including lwres_sendblock().
*/
lwres_result_t
lwres_noopresponse_render(lwres_context_t *ctx, lwres_noopresponse_t *req,
lwres_lwpacket_t *pkt, lwres_buffer_t *b);
lwres_result_t
lwres_nooprequest_parse(lwres_context_t *ctx, lwres_buffer_t *b,
lwres_lwpacket_t *pkt, lwres_nooprequest_t **structp);
/*
* Parse a noop request. Note that to get here, the lwpacket must have
* already been parsed and removed by the caller, otherwise it would be
* pretty hard for it to know this is the right function to call.
*
* The function verifies bits of the header, but does not modify it.
*/
lwres_result_t
lwres_noopresponse_parse(lwres_context_t *ctx, lwres_buffer_t *b,
lwres_lwpacket_t *pkt,
lwres_noopresponse_t **structp);
void
lwres_nooprequest_free(lwres_context_t *ctx, lwres_nooprequest_t **structp);
void
lwres_noopresponse_free(lwres_context_t *ctx, lwres_noopresponse_t **structp);
/*
* Frees any dynamically allocated memory for this structure.
*
* Requires:
*
* ctx != NULL, and be a context returned via lwres_contextcreate().
*
* structp != NULL && *structp != NULL.
*
* Ensures:
*
* *structp == NULL.
*
* All memory allocated by this structure will be returned to the
* system via the context's free function.
*/
lwres_result_t
lwres_conf_parse(lwres_context_t *ctx, const char *filename);
/*
* parses a resolv.conf-format file and stores the results in the structure
* pointed to by *ctx.
*
* Requires:
* ctx != NULL
* filename != NULL && strlen(filename) > 0
*
* Returns:
* LWRES_R_SUCCESS on a successful parse.
* Anything else on error, although the structure may be partially filled
* in.
*/
lwres_result_t
lwres_conf_print(lwres_context_t *ctx, FILE *fp);
/*
* Prints a resolv.conf-format of confdata output to fp.
*
* Requires:
* ctx != NULL
*/
void
lwres_conf_init(lwres_context_t *ctx);
/*
* sets all internal fields to a default state. Used to initialize a new
* lwres_conf_t structure (not reset a used on).
*
* Requires:
* ctx != NULL
*/
void
lwres_conf_clear(lwres_context_t *ctx);
/*
* frees all internally allocated memory in confdata. Uses the memory
* routines supplied by ctx.
*
* Requires:
* ctx != NULL
*/
lwres_conf_t *
lwres_conf_get(lwres_context_t *ctx);
/*
* returns a pointer to the current config structure.
* Be extremely cautions in modifying the contents of this structure; it
* needs an API to return the various bits of data, walk lists, etc.
*
* Requires:
* ctx != NULL
*/
/*
* Helper functions
*/
lwres_result_t
lwres_data_parse(lwres_buffer_t *b, unsigned char **p, lwres_uint16_t *len);
lwres_result_t
lwres_string_parse(lwres_buffer_t *b, char **c, lwres_uint16_t *len);
lwres_result_t
lwres_addr_parse(lwres_buffer_t *b, lwres_addr_t *addr);
lwres_result_t
lwres_getaddrsbyname(lwres_context_t *ctx, const char *name,
lwres_uint32_t addrtypes, lwres_gabnresponse_t **structp);
lwres_result_t
lwres_getnamebyaddr(lwres_context_t *ctx, lwres_uint32_t addrtype,
lwres_uint16_t addrlen, const unsigned char *addr,
lwres_gnbaresponse_t **structp);
lwres_result_t
lwres_getrdatabyname(lwres_context_t *ctx, const char *name,
lwres_uint16_t rdclass, lwres_uint16_t rdtype,
lwres_uint32_t flags, lwres_grbnresponse_t **structp);
lwres_result_t
lwres_getaddrsbyname_setup(lwres_context_t *ctx, const char *name,
lwres_uint32_t addrtypes, lwres_gabnresponse_t **structp);
LWRES_LANG_ENDDECLS
#endif /* LWRES_LWRES_H */
+522
View File
@@ -0,0 +1,522 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: netdb.h,v 1.1 2004/03/15 20:35:25 as Exp $ */
#ifndef LWRES_NETDB_H
#define LWRES_NETDB_H 1
#include <stddef.h> /* Required on FreeBSD (and others?) for size_t. */
#include <netdb.h> /* Contractual provision. */
#include <lwres/lang.h>
/*
* Define if <netdb.h> does not declare struct addrinfo.
*/
#undef ISC_LWRES_NEEDADDRINFO
#ifdef ISC_LWRES_NEEDADDRINFO
struct addrinfo {
int ai_flags; /* AI_PASSIVE, AI_CANONNAME */
int ai_family; /* PF_xxx */
int ai_socktype; /* SOCK_xxx */
int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
size_t ai_addrlen; /* Length of ai_addr */
char *ai_canonname; /* Canonical name for hostname */
struct sockaddr *ai_addr; /* Binary address */
struct addrinfo *ai_next; /* Next structure in linked list */
};
#endif
/*
* Undefine all #defines we are interested in as <netdb.h> may or may not have
* defined them.
*/
/*
* Error return codes from gethostbyname() and gethostbyaddr()
* (left in extern int h_errno).
*/
#undef NETDB_INTERNAL
#undef NETDB_SUCCESS
#undef HOST_NOT_FOUND
#undef TRY_AGAIN
#undef NO_RECOVERY
#undef NO_DATA
#undef NO_ADDRESS
#define NETDB_INTERNAL -1 /* see errno */
#define NETDB_SUCCESS 0 /* no problem */
#define HOST_NOT_FOUND 1 /* Authoritative Answer Host not found */
#define TRY_AGAIN 2 /* Non-Authoritive Host not found, or SERVERFAIL */
#define NO_RECOVERY 3 /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
#define NO_DATA 4 /* Valid name, no data record of requested type */
#define NO_ADDRESS NO_DATA /* no address, look for MX record */
/*
* Error return codes from getaddrinfo()
*/
#undef EAI_ADDRFAMILY
#undef EAI_AGAIN
#undef EAI_BADFLAGS
#undef EAI_FAIL
#undef EAI_FAMILY
#undef EAI_MEMORY
#undef EAI_NODATA
#undef EAI_NONAME
#undef EAI_SERVICE
#undef EAI_SOCKTYPE
#undef EAI_SYSTEM
#undef EAI_BADHINTS
#undef EAI_PROTOCOL
#undef EAI_MAX
#define EAI_ADDRFAMILY 1 /* address family for hostname not supported */
#define EAI_AGAIN 2 /* temporary failure in name resolution */
#define EAI_BADFLAGS 3 /* invalid value for ai_flags */
#define EAI_FAIL 4 /* non-recoverable failure in name resolution */
#define EAI_FAMILY 5 /* ai_family not supported */
#define EAI_MEMORY 6 /* memory allocation failure */
#define EAI_NODATA 7 /* no address associated with hostname */
#define EAI_NONAME 8 /* hostname nor servname provided, or not known */
#define EAI_SERVICE 9 /* servname not supported for ai_socktype */
#define EAI_SOCKTYPE 10 /* ai_socktype not supported */
#define EAI_SYSTEM 11 /* system error returned in errno */
#define EAI_BADHINTS 12
#define EAI_PROTOCOL 13
#define EAI_MAX 14
/*
* Flag values for getaddrinfo()
*/
#undef AI_PASSIVE
#undef AI_CANONNAME
#undef AI_NUMERICHOST
#define AI_PASSIVE 0x00000001
#define AI_CANONNAME 0x00000002
#define AI_NUMERICHOST 0x00000004
/*
* Flag values for getipnodebyname()
*/
#undef AI_V4MAPPED
#undef AI_ALL
#undef AI_ADDRCONFIG
#undef AI_DEFAULT
#define AI_V4MAPPED 0x00000008
#define AI_ALL 0x00000010
#define AI_ADDRCONFIG 0x00000020
#define AI_DEFAULT (AI_V4MAPPED|AI_ADDRCONFIG)
/*
* Constants for lwres_getnameinfo()
*/
#undef NI_MAXHOST
#undef NI_MAXSERV
#define NI_MAXHOST 1025
#define NI_MAXSERV 32
/*
* Flag values for lwres_getnameinfo()
*/
#undef NI_NOFQDN
#undef NI_NUMERICHOST
#undef NI_NAMEREQD
#undef NI_NUMERICSERV
#undef NI_DGRAM
#undef NI_NUMERICSCOPE
#define NI_NOFQDN 0x00000001
#define NI_NUMERICHOST 0x00000002
#define NI_NAMEREQD 0x00000004
#define NI_NUMERICSERV 0x00000008
#define NI_DGRAM 0x00000010
#define NI_NUMERICSCOPE 0x00000020 /*2553bis-00*/
/*
* Define if <netdb.h> does not declare struct rrsetinfo.
*/
#define ISC_LWRES_NEEDRRSETINFO 1
#ifdef ISC_LWRES_NEEDRRSETINFO
/*
* Structures for getrrsetbyname()
*/
struct rdatainfo {
unsigned int rdi_length;
unsigned char *rdi_data;
};
struct rrsetinfo {
unsigned int rri_flags;
int rri_rdclass;
int rri_rdtype;
unsigned int rri_ttl;
unsigned int rri_nrdatas;
unsigned int rri_nsigs;
char *rri_name;
struct rdatainfo *rri_rdatas;
struct rdatainfo *rri_sigs;
};
/*
* Flags for getrrsetbyname()
*/
#define RRSET_VALIDATED 0x00000001
/* Set was dnssec validated */
/*
* Return codes for getrrsetbyname()
*/
#define ERRSET_SUCCESS 0
#define ERRSET_NOMEMORY 1
#define ERRSET_FAIL 2
#define ERRSET_INVAL 3
#define ERRSET_NONAME 4
#define ERRSET_NODATA 5
#define ERRSET_RETRY 6
#endif
/*
* Define to map into lwres_ namespace.
*/
#define LWRES_NAMESPACE
#ifdef LWRES_NAMESPACE
/*
* Use our versions not the ones from the C library.
*/
#ifdef getnameinfo
#undef getnameinfo
#endif
#define getnameinfo lwres_getnameinfo
#ifdef getaddrinfo
#undef getaddrinfo
#endif
#define getaddrinfo lwres_getaddrinfo
#ifdef freeaddrinfo
#undef freeaddrinfo
#endif
#define freeaddrinfo lwres_freeaddrinfo
#ifdef gai_strerror
#undef gai_strerror
#endif
#define gai_strerror lwres_gai_strerror
#ifdef herror
#undef herror
#endif
#define herror lwres_herror
#ifdef hstrerror
#undef hstrerror
#endif
#define hstrerror lwres_hstrerror
#ifdef getipnodebyname
#undef getipnodebyname
#endif
#define getipnodebyname lwres_getipnodebyname
#ifdef getipnodebyaddr
#undef getipnodebyaddr
#endif
#define getipnodebyaddr lwres_getipnodebyaddr
#ifdef freehostent
#undef freehostent
#endif
#define freehostent lwres_freehostent
#ifdef gethostbyname
#undef gethostbyname
#endif
#define gethostbyname lwres_gethostbyname
#ifdef gethostbyname2
#undef gethostbyname2
#endif
#define gethostbyname2 lwres_gethostbyname2
#ifdef gethostbyaddr
#undef gethostbyaddr
#endif
#define gethostbyaddr lwres_gethostbyaddr
#ifdef gethostent
#undef gethostent
#endif
#define gethostent lwres_gethostent
#ifdef sethostent
#undef sethostent
#endif
#define sethostent lwres_sethostent
#ifdef endhostent
#undef endhostent
#endif
#define endhostent lwres_endhostent
/* #define sethostfile lwres_sethostfile */
#ifdef gethostbyname_r
#undef gethostbyname_r
#endif
#define gethostbyname_r lwres_gethostbyname_r
#ifdef gethostbyaddr_r
#undef gethostbyaddr_r
#endif
#define gethostbyaddr_r lwres_gethostbyaddr_r
#ifdef gethostent_r
#undef gethostent_r
#endif
#define gethostent_r lwres_gethostent_r
#ifdef sethostent_r
#undef sethostent_r
#endif
#define sethostent_r lwres_sethostent_r
#ifdef endhostent_r
#undef endhostent_r
#endif
#define endhostent_r lwres_endhostent_r
#ifdef getrrsetbyname
#undef getrrsetbyname
#endif
#define getrrsetbyname lwres_getrrsetbyname
#ifdef freerrset
#undef freerrset
#endif
#define freerrset lwres_freerrset
#ifdef notyet
#define getservbyname lwres_getservbyname
#define getservbyport lwres_getservbyport
#define getservent lwres_getservent
#define setservent lwres_setservent
#define endservent lwres_endservent
#define getservbyname_r lwres_getservbyname_r
#define getservbyport_r lwres_getservbyport_r
#define getservent_r lwres_getservent_r
#define setservent_r lwres_setservent_r
#define endservent_r lwres_endservent_r
#define getprotobyname lwres_getprotobyname
#define getprotobynumber lwres_getprotobynumber
#define getprotoent lwres_getprotoent
#define setprotoent lwres_setprotoent
#define endprotoent lwres_endprotoent
#define getprotobyname_r lwres_getprotobyname_r
#define getprotobynumber_r lwres_getprotobynumber_r
#define getprotoent_r lwres_getprotoent_r
#define setprotoent_r lwres_setprotoent_r
#define endprotoent_r lwres_endprotoent_r
#ifdef getnetbyname
#undef getnetbyname
#endif
#define getnetbyname lwres_getnetbyname
#ifdef getnetbyaddr
#undef getnetbyaddr
#endif
#define getnetbyaddr lwres_getnetbyaddr
#ifdef getnetent
#undef getnetent
#endif
#define getnetent lwres_getnetent
#ifdef setnetent
#undef setnetent
#endif
#define setnetent lwres_setnetent
#ifdef endnetent
#undef endnetent
#endif
#define endnetent lwres_endnetent
#ifdef getnetbyname_r
#undef getnetbyname_r
#endif
#define getnetbyname_r lwres_getnetbyname_r
#ifdef getnetbyaddr_r
#undef getnetbyaddr_r
#endif
#define getnetbyaddr_r lwres_getnetbyaddr_r
#ifdef getnetent_r
#undef getnetent_r
#endif
#define getnetent_r lwres_getnetent_r
#ifdef setnetent_r
#undef setnetent_r
#endif
#define setnetent_r lwres_setnetent_r
#ifdef endnetent_r
#undef endnetent_r
#endif
#define endnetent_r lwres_endnetent_r
#endif /* notyet */
#ifdef h_errno
#undef h_errno
#endif
#define h_errno lwres_h_errno
#endif /* LWRES_NAMESPACE */
LWRES_LANG_BEGINDECLS
extern int lwres_h_errno;
int lwres_getaddrinfo(const char *, const char *,
const struct addrinfo *, struct addrinfo **);
int lwres_getnameinfo(const struct sockaddr *, size_t, char *,
size_t, char *, size_t, int);
void lwres_freeaddrinfo(struct addrinfo *);
char *lwres_gai_strerror(int);
struct hostent *lwres_gethostbyaddr(const char *, int, int);
struct hostent *lwres_gethostbyname(const char *);
struct hostent *lwres_gethostbyname2(const char *, int);
struct hostent *lwres_gethostent(void);
struct hostent *lwres_getipnodebyname(const char *, int, int, int *);
struct hostent *lwres_getipnodebyaddr(const void *, size_t, int, int *);
void lwres_endhostent(void);
void lwres_sethostent(int);
/* void lwres_sethostfile(const char *); */
void lwres_freehostent(struct hostent *);
int lwres_getrrsetbyname(const char *, unsigned int, unsigned int,
unsigned int, struct rrsetinfo **);
int lwres_getrrsetbyname_all(const char *, unsigned int,
unsigned int,
unsigned int, struct rrsetinfo **);
void lwres_freerrset(struct rrsetinfo *);
#ifdef notyet
struct netent *lwres_getnetbyaddr(unsigned long, int);
struct netent *lwres_getnetbyname(const char *);
struct netent *lwres_getnetent(void);
void lwres_endnetent(void);
void lwres_setnetent(int);
struct protoent *lwres_getprotobyname(const char *);
struct protoent *lwres_getprotobynumber(int);
struct protoent *lwres_getprotoent(void);
void lwres_endprotoent(void);
void lwres_setprotoent(int);
struct servent *lwres_getservbyname(const char *, const char *);
struct servent *lwres_getservbyport(int, const char *);
struct servent *lwres_getservent(void);
void lwres_endservent(void);
void lwres_setservent(int);
#endif /* notyet */
void lwres_herror(const char *);
const char *lwres_hstrerror(int);
struct hostent *lwres_gethostbyaddr_r(const char *, int, int, struct hostent *,
char *, int, int *);
struct hostent *lwres_gethostbyname_r(const char *, struct hostent *,
char *, int, int *);
struct hostent *lwres_gethostent_r(struct hostent *, char *, int, int *);
void lwres_sethostent_r(int);
void lwres_endhostent_r(void);
#ifdef notyet
struct netent *lwres_getnetbyname_r(const char *, struct netent *,
char *, int);
struct netent *lwres_getnetbyaddr_r(long, int, struct netent *,
char *, int);
struct netent *lwres_getnetent_r(struct netent *, char *, int);
void lwres_setnetent_r(int);
void lwres_endnetent_r(void);
struct protoent *lwres_getprotobyname_r(const char *,
struct protoent *, char *, int);
struct protoent *lwres_getprotobynumber_r(int,
struct protoent *, char *, int);
struct protoent *lwres_getprotoent_r(struct protoent *, char *, int);
void lwres_setprotoent_r(int);
void lwres_endprotoent_r(void);
struct servent *lwres_getservbyname_r(const char *name, const char *,
struct servent *, char *, int);
struct servent *lwres_getservbyport_r(int port, const char *,
struct servent *, char *, int);
struct servent *lwres_getservent_r(struct servent *, char *, int);
void lwres_setservent_r(int);
void lwres_endservent_r(void);
#endif /* notyet */
LWRES_LANG_ENDDECLS
#ifdef notyet
/* This is nec'y to make this include file properly replace the sun version. */
#ifdef sun
#ifdef __GNU_LIBRARY__
#include <rpc/netdb.h> /* Required. */
#else /* !__GNU_LIBRARY__ */
struct rpcent {
char *r_name; /* name of server for this rpc program */
char **r_aliases; /* alias list */
int r_number; /* rpc program number */
};
struct rpcent *lwres_getrpcbyname();
struct rpcent *lwres_getrpcbynumber(),
struct rpcent *lwres_getrpcent();
#endif /* __GNU_LIBRARY__ */
#endif /* sun */
#endif /* notyet */
/*
* Tell Emacs to use C mode on this file.
* Local variables:
* mode: c
* End:
*/
#endif /* LWRES_NETDB_H */
+518
View File
@@ -0,0 +1,518 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: netdb.h.in,v 1.1 2004/03/15 20:35:25 as Exp $ */
#ifndef LWRES_NETDB_H
#define LWRES_NETDB_H 1
#include <stddef.h> /* Required on FreeBSD (and others?) for size_t. */
#include <netdb.h> /* Contractual provision. */
#include <lwres/lang.h>
/*
* Define if <netdb.h> does not declare struct addrinfo.
*/
@ISC_LWRES_NEEDADDRINFO@
#ifdef ISC_LWRES_NEEDADDRINFO
struct addrinfo {
int ai_flags; /* AI_PASSIVE, AI_CANONNAME */
int ai_family; /* PF_xxx */
int ai_socktype; /* SOCK_xxx */
int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
size_t ai_addrlen; /* Length of ai_addr */
char *ai_canonname; /* Canonical name for hostname */
struct sockaddr *ai_addr; /* Binary address */
struct addrinfo *ai_next; /* Next structure in linked list */
};
#endif
/*
* Undefine all #defines we are interested in as <netdb.h> may or may not have
* defined them.
*/
/*
* Error return codes from gethostbyname() and gethostbyaddr()
* (left in extern int h_errno).
*/
#undef NETDB_INTERNAL
#undef NETDB_SUCCESS
#undef HOST_NOT_FOUND
#undef TRY_AGAIN
#undef NO_RECOVERY
#undef NO_DATA
#undef NO_ADDRESS
#define NETDB_INTERNAL -1 /* see errno */
#define NETDB_SUCCESS 0 /* no problem */
#define HOST_NOT_FOUND 1 /* Authoritative Answer Host not found */
#define TRY_AGAIN 2 /* Non-Authoritive Host not found, or SERVERFAIL */
#define NO_RECOVERY 3 /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
#define NO_DATA 4 /* Valid name, no data record of requested type */
#define NO_ADDRESS NO_DATA /* no address, look for MX record */
/*
* Error return codes from getaddrinfo()
*/
#undef EAI_ADDRFAMILY
#undef EAI_AGAIN
#undef EAI_BADFLAGS
#undef EAI_FAIL
#undef EAI_FAMILY
#undef EAI_MEMORY
#undef EAI_NODATA
#undef EAI_NONAME
#undef EAI_SERVICE
#undef EAI_SOCKTYPE
#undef EAI_SYSTEM
#undef EAI_BADHINTS
#undef EAI_PROTOCOL
#undef EAI_MAX
#define EAI_ADDRFAMILY 1 /* address family for hostname not supported */
#define EAI_AGAIN 2 /* temporary failure in name resolution */
#define EAI_BADFLAGS 3 /* invalid value for ai_flags */
#define EAI_FAIL 4 /* non-recoverable failure in name resolution */
#define EAI_FAMILY 5 /* ai_family not supported */
#define EAI_MEMORY 6 /* memory allocation failure */
#define EAI_NODATA 7 /* no address associated with hostname */
#define EAI_NONAME 8 /* hostname nor servname provided, or not known */
#define EAI_SERVICE 9 /* servname not supported for ai_socktype */
#define EAI_SOCKTYPE 10 /* ai_socktype not supported */
#define EAI_SYSTEM 11 /* system error returned in errno */
#define EAI_BADHINTS 12
#define EAI_PROTOCOL 13
#define EAI_MAX 14
/*
* Flag values for getaddrinfo()
*/
#undef AI_PASSIVE
#undef AI_CANONNAME
#undef AI_NUMERICHOST
#define AI_PASSIVE 0x00000001
#define AI_CANONNAME 0x00000002
#define AI_NUMERICHOST 0x00000004
/*
* Flag values for getipnodebyname()
*/
#undef AI_V4MAPPED
#undef AI_ALL
#undef AI_ADDRCONFIG
#undef AI_DEFAULT
#define AI_V4MAPPED 0x00000008
#define AI_ALL 0x00000010
#define AI_ADDRCONFIG 0x00000020
#define AI_DEFAULT (AI_V4MAPPED|AI_ADDRCONFIG)
/*
* Constants for lwres_getnameinfo()
*/
#undef NI_MAXHOST
#undef NI_MAXSERV
#define NI_MAXHOST 1025
#define NI_MAXSERV 32
/*
* Flag values for lwres_getnameinfo()
*/
#undef NI_NOFQDN
#undef NI_NUMERICHOST
#undef NI_NAMEREQD
#undef NI_NUMERICSERV
#undef NI_DGRAM
#undef NI_NUMERICSCOPE
#define NI_NOFQDN 0x00000001
#define NI_NUMERICHOST 0x00000002
#define NI_NAMEREQD 0x00000004
#define NI_NUMERICSERV 0x00000008
#define NI_DGRAM 0x00000010
#define NI_NUMERICSCOPE 0x00000020 /*2553bis-00*/
/*
* Define if <netdb.h> does not declare struct rrsetinfo.
*/
@ISC_LWRES_NEEDRRSETINFO@
#ifdef ISC_LWRES_NEEDRRSETINFO
/*
* Structures for getrrsetbyname()
*/
struct rdatainfo {
unsigned int rdi_length;
unsigned char *rdi_data;
};
struct rrsetinfo {
unsigned int rri_flags;
int rri_rdclass;
int rri_rdtype;
unsigned int rri_ttl;
unsigned int rri_nrdatas;
unsigned int rri_nsigs;
char *rri_name;
struct rdatainfo *rri_rdatas;
struct rdatainfo *rri_sigs;
};
/*
* Flags for getrrsetbyname()
*/
#define RRSET_VALIDATED 0x00000001
/* Set was dnssec validated */
/*
* Return codes for getrrsetbyname()
*/
#define ERRSET_SUCCESS 0
#define ERRSET_NOMEMORY 1
#define ERRSET_FAIL 2
#define ERRSET_INVAL 3
#define ERRSET_NONAME 4
#define ERRSET_NODATA 5
#endif
/*
* Define to map into lwres_ namespace.
*/
#define LWRES_NAMESPACE
#ifdef LWRES_NAMESPACE
/*
* Use our versions not the ones from the C library.
*/
#ifdef getnameinfo
#undef getnameinfo
#endif
#define getnameinfo lwres_getnameinfo
#ifdef getaddrinfo
#undef getaddrinfo
#endif
#define getaddrinfo lwres_getaddrinfo
#ifdef freeaddrinfo
#undef freeaddrinfo
#endif
#define freeaddrinfo lwres_freeaddrinfo
#ifdef gai_strerror
#undef gai_strerror
#endif
#define gai_strerror lwres_gai_strerror
#ifdef herror
#undef herror
#endif
#define herror lwres_herror
#ifdef hstrerror
#undef hstrerror
#endif
#define hstrerror lwres_hstrerror
#ifdef getipnodebyname
#undef getipnodebyname
#endif
#define getipnodebyname lwres_getipnodebyname
#ifdef getipnodebyaddr
#undef getipnodebyaddr
#endif
#define getipnodebyaddr lwres_getipnodebyaddr
#ifdef freehostent
#undef freehostent
#endif
#define freehostent lwres_freehostent
#ifdef gethostbyname
#undef gethostbyname
#endif
#define gethostbyname lwres_gethostbyname
#ifdef gethostbyname2
#undef gethostbyname2
#endif
#define gethostbyname2 lwres_gethostbyname2
#ifdef gethostbyaddr
#undef gethostbyaddr
#endif
#define gethostbyaddr lwres_gethostbyaddr
#ifdef gethostent
#undef gethostent
#endif
#define gethostent lwres_gethostent
#ifdef sethostent
#undef sethostent
#endif
#define sethostent lwres_sethostent
#ifdef endhostent
#undef endhostent
#endif
#define endhostent lwres_endhostent
/* #define sethostfile lwres_sethostfile */
#ifdef gethostbyname_r
#undef gethostbyname_r
#endif
#define gethostbyname_r lwres_gethostbyname_r
#ifdef gethostbyaddr_r
#undef gethostbyaddr_r
#endif
#define gethostbyaddr_r lwres_gethostbyaddr_r
#ifdef gethostent_r
#undef gethostent_r
#endif
#define gethostent_r lwres_gethostent_r
#ifdef sethostent_r
#undef sethostent_r
#endif
#define sethostent_r lwres_sethostent_r
#ifdef endhostent_r
#undef endhostent_r
#endif
#define endhostent_r lwres_endhostent_r
#ifdef getrrsetbyname
#undef getrrsetbyname
#endif
#define getrrsetbyname lwres_getrrsetbyname
#ifdef freerrset
#undef freerrset
#endif
#define freerrset lwres_freerrset
#ifdef notyet
#define getservbyname lwres_getservbyname
#define getservbyport lwres_getservbyport
#define getservent lwres_getservent
#define setservent lwres_setservent
#define endservent lwres_endservent
#define getservbyname_r lwres_getservbyname_r
#define getservbyport_r lwres_getservbyport_r
#define getservent_r lwres_getservent_r
#define setservent_r lwres_setservent_r
#define endservent_r lwres_endservent_r
#define getprotobyname lwres_getprotobyname
#define getprotobynumber lwres_getprotobynumber
#define getprotoent lwres_getprotoent
#define setprotoent lwres_setprotoent
#define endprotoent lwres_endprotoent
#define getprotobyname_r lwres_getprotobyname_r
#define getprotobynumber_r lwres_getprotobynumber_r
#define getprotoent_r lwres_getprotoent_r
#define setprotoent_r lwres_setprotoent_r
#define endprotoent_r lwres_endprotoent_r
#ifdef getnetbyname
#undef getnetbyname
#endif
#define getnetbyname lwres_getnetbyname
#ifdef getnetbyaddr
#undef getnetbyaddr
#endif
#define getnetbyaddr lwres_getnetbyaddr
#ifdef getnetent
#undef getnetent
#endif
#define getnetent lwres_getnetent
#ifdef setnetent
#undef setnetent
#endif
#define setnetent lwres_setnetent
#ifdef endnetent
#undef endnetent
#endif
#define endnetent lwres_endnetent
#ifdef getnetbyname_r
#undef getnetbyname_r
#endif
#define getnetbyname_r lwres_getnetbyname_r
#ifdef getnetbyaddr_r
#undef getnetbyaddr_r
#endif
#define getnetbyaddr_r lwres_getnetbyaddr_r
#ifdef getnetent_r
#undef getnetent_r
#endif
#define getnetent_r lwres_getnetent_r
#ifdef setnetent_r
#undef setnetent_r
#endif
#define setnetent_r lwres_setnetent_r
#ifdef endnetent_r
#undef endnetent_r
#endif
#define endnetent_r lwres_endnetent_r
#endif /* notyet */
#ifdef h_errno
#undef h_errno
#endif
#define h_errno lwres_h_errno
#endif /* LWRES_NAMESPACE */
LWRES_LANG_BEGINDECLS
extern int lwres_h_errno;
int lwres_getaddrinfo(const char *, const char *,
const struct addrinfo *, struct addrinfo **);
int lwres_getnameinfo(const struct sockaddr *, size_t, char *,
size_t, char *, size_t, int);
void lwres_freeaddrinfo(struct addrinfo *);
char *lwres_gai_strerror(int);
struct hostent *lwres_gethostbyaddr(const char *, int, int);
struct hostent *lwres_gethostbyname(const char *);
struct hostent *lwres_gethostbyname2(const char *, int);
struct hostent *lwres_gethostent(void);
struct hostent *lwres_getipnodebyname(const char *, int, int, int *);
struct hostent *lwres_getipnodebyaddr(const void *, size_t, int, int *);
void lwres_endhostent(void);
void lwres_sethostent(int);
/* void lwres_sethostfile(const char *); */
void lwres_freehostent(struct hostent *);
int lwres_getrrsetbyname(const char *, unsigned int, unsigned int,
unsigned int, struct rrsetinfo **);
void lwres_freerrset(struct rrsetinfo *);
#ifdef notyet
struct netent *lwres_getnetbyaddr(unsigned long, int);
struct netent *lwres_getnetbyname(const char *);
struct netent *lwres_getnetent(void);
void lwres_endnetent(void);
void lwres_setnetent(int);
struct protoent *lwres_getprotobyname(const char *);
struct protoent *lwres_getprotobynumber(int);
struct protoent *lwres_getprotoent(void);
void lwres_endprotoent(void);
void lwres_setprotoent(int);
struct servent *lwres_getservbyname(const char *, const char *);
struct servent *lwres_getservbyport(int, const char *);
struct servent *lwres_getservent(void);
void lwres_endservent(void);
void lwres_setservent(int);
#endif /* notyet */
void lwres_herror(const char *);
const char *lwres_hstrerror(int);
struct hostent *lwres_gethostbyaddr_r(const char *, int, int, struct hostent *,
char *, int, int *);
struct hostent *lwres_gethostbyname_r(const char *, struct hostent *,
char *, int, int *);
struct hostent *lwres_gethostent_r(struct hostent *, char *, int, int *);
void lwres_sethostent_r(int);
void lwres_endhostent_r(void);
#ifdef notyet
struct netent *lwres_getnetbyname_r(const char *, struct netent *,
char *, int);
struct netent *lwres_getnetbyaddr_r(long, int, struct netent *,
char *, int);
struct netent *lwres_getnetent_r(struct netent *, char *, int);
void lwres_setnetent_r(int);
void lwres_endnetent_r(void);
struct protoent *lwres_getprotobyname_r(const char *,
struct protoent *, char *, int);
struct protoent *lwres_getprotobynumber_r(int,
struct protoent *, char *, int);
struct protoent *lwres_getprotoent_r(struct protoent *, char *, int);
void lwres_setprotoent_r(int);
void lwres_endprotoent_r(void);
struct servent *lwres_getservbyname_r(const char *name, const char *,
struct servent *, char *, int);
struct servent *lwres_getservbyport_r(int port, const char *,
struct servent *, char *, int);
struct servent *lwres_getservent_r(struct servent *, char *, int);
void lwres_setservent_r(int);
void lwres_endservent_r(void);
#endif /* notyet */
LWRES_LANG_ENDDECLS
#ifdef notyet
/* This is nec'y to make this include file properly replace the sun version. */
#ifdef sun
#ifdef __GNU_LIBRARY__
#include <rpc/netdb.h> /* Required. */
#else /* !__GNU_LIBRARY__ */
struct rpcent {
char *r_name; /* name of server for this rpc program */
char **r_aliases; /* alias list */
int r_number; /* rpc program number */
};
struct rpcent *lwres_getrpcbyname();
struct rpcent *lwres_getrpcbynumber(),
struct rpcent *lwres_getrpcent();
#endif /* __GNU_LIBRARY__ */
#endif /* sun */
#endif /* notyet */
/*
* Tell Emacs to use C mode on this file.
* Local variables:
* mode: c
* End:
*/
#endif /* LWRES_NETDB_H */
+91
View File
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: platform.h,v 1.1 2004/03/15 20:35:25 as Exp $ */
#ifndef LWRES_PLATFORM_H
#define LWRES_PLATFORM_H 1
/*****
***** Platform-dependent defines.
*****/
/***
*** Network.
***/
/*
* Define if this system needs the <netinet/in6.h> header file for IPv6.
*/
#undef LWRES_PLATFORM_NEEDNETINETIN6H
/*
* Define if this system needs the <netinet6/in6.h> header file for IPv6.
*/
#undef LWRES_PLATFORM_NEEDNETINET6IN6H
/*
* If sockaddrs on this system have an sa_len field, LWRES_PLATFORM_HAVESALEN
* will be defined.
*/
#undef LWRES_PLATFORM_HAVESALEN
/*
* If this system has the IPv6 structure definitions, LWRES_PLATFORM_HAVEIPV6
* will be defined.
*/
#define LWRES_PLATFORM_HAVEIPV6 1
/*
* If this system is missing in6addr_any, LWRES_PLATFORM_NEEDIN6ADDRANY will
* be defined.
*/
#undef LWRES_PLATFORM_NEEDIN6ADDRANY
/*
* If this system is missing in6addr_loopback,
* LWRES_PLATFORM_NEEDIN6ADDRLOOPBACK will be defined.
*/
#undef LWRES_PLATFORM_NEEDIN6ADDRLOOPBACK
/*
* If this system has in_addr6, rather than in6_addr,
* LWRES_PLATFORM_HAVEINADDR6 will be defined.
*/
#undef LWRES_PLATFORM_HAVEINADDR6
/*
* Defined if unistd.h does not cause fd_set to be delared.
*/
#define LWRES_PLATFORM_NEEDSYSSELECTH 1
/*
* Used to control how extern data is linked; needed for Win32 platforms.
*/
#undef LWRES_PLATFORM_USEDECLSPEC
#ifndef LWRES_PLATFORM_USEDECLSPEC
#define LIBLWRES_EXTERNAL_DATA
#else
#ifdef LIBLWRES_EXPORTS
#define LIBLWRES_EXTERNAL_DATA __declspec(dllexport)
#else
#define LIBLWRES_EXTERNAL_DATA __declspec(dllimport)
#endif
#endif
#endif /* LWRES_PLATFORM_H */
+91
View File
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: platform.h.in,v 1.1 2004/03/15 20:35:25 as Exp $ */
#ifndef LWRES_PLATFORM_H
#define LWRES_PLATFORM_H 1
/*****
***** Platform-dependent defines.
*****/
/***
*** Network.
***/
/*
* Define if this system needs the <netinet/in6.h> header file for IPv6.
*/
@LWRES_PLATFORM_NEEDNETINETIN6H@
/*
* Define if this system needs the <netinet6/in6.h> header file for IPv6.
*/
@LWRES_PLATFORM_NEEDNETINET6IN6H@
/*
* If sockaddrs on this system have an sa_len field, LWRES_PLATFORM_HAVESALEN
* will be defined.
*/
@LWRES_PLATFORM_HAVESALEN@
/*
* If this system has the IPv6 structure definitions, LWRES_PLATFORM_HAVEIPV6
* will be defined.
*/
@LWRES_PLATFORM_HAVEIPV6@
/*
* If this system is missing in6addr_any, LWRES_PLATFORM_NEEDIN6ADDRANY will
* be defined.
*/
@LWRES_PLATFORM_NEEDIN6ADDRANY@
/*
* If this system is missing in6addr_loopback,
* LWRES_PLATFORM_NEEDIN6ADDRLOOPBACK will be defined.
*/
@LWRES_PLATFORM_NEEDIN6ADDRLOOPBACK@
/*
* If this system has in_addr6, rather than in6_addr,
* LWRES_PLATFORM_HAVEINADDR6 will be defined.
*/
@LWRES_PLATFORM_HAVEINADDR6@
/*
* Defined if unistd.h does not cause fd_set to be delared.
*/
@LWRES_PLATFORM_NEEDSYSSELECTH@
/*
* Used to control how extern data is linked; needed for Win32 platforms.
*/
@LWRES_PLATFORM_USEDECLSPEC@
#ifndef LWRES_PLATFORM_USEDECLSPEC
#define LIBLWRES_EXTERNAL_DATA
#else
#ifdef LIBLWRES_EXPORTS
#define LIBLWRES_EXTERNAL_DATA __declspec(dllexport)
#else
#define LIBLWRES_EXTERNAL_DATA __declspec(dllimport)
#endif
#endif
#endif /* LWRES_PLATFORM_H */
+40
View File
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: result.h,v 1.1 2004/03/15 20:35:25 as Exp $ */
#ifndef LWRES_RESULT_H
#define LWRES_RESULT_H 1
typedef unsigned int lwres_result_t;
#define LWRES_R_SUCCESS 0
#define LWRES_R_NOMEMORY 1
#define LWRES_R_TIMEOUT 2
#define LWRES_R_NOTFOUND 3
#define LWRES_R_UNEXPECTEDEND 4 /* unexpected end of input */
#define LWRES_R_FAILURE 5 /* generic failure */
#define LWRES_R_IOERROR 6
#define LWRES_R_NOTIMPLEMENTED 7
#define LWRES_R_UNEXPECTED 8
#define LWRES_R_TRAILINGDATA 9
#define LWRES_R_INCOMPLETE 10
#define LWRES_R_RETRY 11
#define LWRES_R_TYPENOTFOUND 12
#define LWRES_R_TOOLARGE 13
#endif /* LWRES_RESULT_H */
+287
View File
@@ -0,0 +1,287 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: lwbuffer.c,v 1.1 2004/03/15 20:35:25 as Exp $ */
#include <config.h>
#include <string.h>
#include <lwres/lwbuffer.h>
#include "assert_p.h"
void
lwres_buffer_init(lwres_buffer_t *b, void *base, unsigned int length)
{
/*
* Make 'b' refer to the 'length'-byte region starting at base.
*/
REQUIRE(b != NULL);
b->magic = LWRES_BUFFER_MAGIC;
b->base = base;
b->length = length;
b->used = 0;
b->current = 0;
b->active = 0;
}
void
lwres_buffer_invalidate(lwres_buffer_t *b)
{
/*
* Make 'b' an invalid buffer.
*/
REQUIRE(LWRES_BUFFER_VALID(b));
b->magic = 0;
b->base = NULL;
b->length = 0;
b->used = 0;
b->current = 0;
b->active = 0;
}
void
lwres_buffer_add(lwres_buffer_t *b, unsigned int n)
{
/*
* Increase the 'used' region of 'b' by 'n' bytes.
*/
REQUIRE(LWRES_BUFFER_VALID(b));
REQUIRE(b->used + n <= b->length);
b->used += n;
}
void
lwres_buffer_subtract(lwres_buffer_t *b, unsigned int n)
{
/*
* Decrease the 'used' region of 'b' by 'n' bytes.
*/
REQUIRE(LWRES_BUFFER_VALID(b));
REQUIRE(b->used >= n);
b->used -= n;
if (b->current > b->used)
b->current = b->used;
if (b->active > b->used)
b->active = b->used;
}
void
lwres_buffer_clear(lwres_buffer_t *b)
{
/*
* Make the used region empty.
*/
REQUIRE(LWRES_BUFFER_VALID(b));
b->used = 0;
b->current = 0;
b->active = 0;
}
void
lwres_buffer_first(lwres_buffer_t *b)
{
/*
* Make the consumed region empty.
*/
REQUIRE(LWRES_BUFFER_VALID(b));
b->current = 0;
}
void
lwres_buffer_forward(lwres_buffer_t *b, unsigned int n)
{
/*
* Increase the 'consumed' region of 'b' by 'n' bytes.
*/
REQUIRE(LWRES_BUFFER_VALID(b));
REQUIRE(b->current + n <= b->used);
b->current += n;
}
void
lwres_buffer_back(lwres_buffer_t *b, unsigned int n)
{
/*
* Decrease the 'consumed' region of 'b' by 'n' bytes.
*/
REQUIRE(LWRES_BUFFER_VALID(b));
REQUIRE(n <= b->current);
b->current -= n;
}
lwres_uint8_t
lwres_buffer_getuint8(lwres_buffer_t *b)
{
unsigned char *cp;
lwres_uint8_t result;
/*
* Read an unsigned 8-bit integer from 'b' and return it.
*/
REQUIRE(LWRES_BUFFER_VALID(b));
REQUIRE(b->used - b->current >= 1);
cp = b->base;
cp += b->current;
b->current += 1;
result = ((unsigned int)(cp[0]));
return (result);
}
void
lwres_buffer_putuint8(lwres_buffer_t *b, lwres_uint8_t val)
{
unsigned char *cp;
REQUIRE(LWRES_BUFFER_VALID(b));
REQUIRE(b->used + 1 <= b->length);
cp = b->base;
cp += b->used;
b->used += 1;
cp[0] = (val & 0x00ff);
}
lwres_uint16_t
lwres_buffer_getuint16(lwres_buffer_t *b)
{
unsigned char *cp;
lwres_uint16_t result;
/*
* Read an unsigned 16-bit integer in network byte order from 'b',
* convert it to host byte order, and return it.
*/
REQUIRE(LWRES_BUFFER_VALID(b));
REQUIRE(b->used - b->current >= 2);
cp = b->base;
cp += b->current;
b->current += 2;
result = ((unsigned int)(cp[0])) << 8;
result |= ((unsigned int)(cp[1]));
return (result);
}
void
lwres_buffer_putuint16(lwres_buffer_t *b, lwres_uint16_t val)
{
unsigned char *cp;
REQUIRE(LWRES_BUFFER_VALID(b));
REQUIRE(b->used + 2 <= b->length);
cp = b->base;
cp += b->used;
b->used += 2;
cp[0] = (val & 0xff00) >> 8;
cp[1] = (val & 0x00ff);
}
lwres_uint32_t
lwres_buffer_getuint32(lwres_buffer_t *b)
{
unsigned char *cp;
lwres_uint32_t result;
/*
* Read an unsigned 32-bit integer in network byte order from 'b',
* convert it to host byte order, and return it.
*/
REQUIRE(LWRES_BUFFER_VALID(b));
REQUIRE(b->used - b->current >= 4);
cp = b->base;
cp += b->current;
b->current += 4;
result = ((unsigned int)(cp[0])) << 24;
result |= ((unsigned int)(cp[1])) << 16;
result |= ((unsigned int)(cp[2])) << 8;
result |= ((unsigned int)(cp[3]));
return (result);
}
void
lwres_buffer_putuint32(lwres_buffer_t *b, lwres_uint32_t val)
{
unsigned char *cp;
REQUIRE(LWRES_BUFFER_VALID(b));
REQUIRE(b->used + 4 <= b->length);
cp = b->base;
cp += b->used;
b->used += 4;
cp[0] = (unsigned char)((val & 0xff000000) >> 24);
cp[1] = (unsigned char)((val & 0x00ff0000) >> 16);
cp[2] = (unsigned char)((val & 0x0000ff00) >> 8);
cp[3] = (unsigned char)(val & 0x000000ff);
}
void
lwres_buffer_putmem(lwres_buffer_t *b, const unsigned char *base,
unsigned int length)
{
unsigned char *cp;
REQUIRE(LWRES_BUFFER_VALID(b));
REQUIRE(b->used + length <= b->length);
cp = (unsigned char *)b->base + b->used;
memcpy(cp, base, length);
b->used += length;
}
void
lwres_buffer_getmem(lwres_buffer_t *b, unsigned char *base,
unsigned int length)
{
unsigned char *cp;
REQUIRE(LWRES_BUFFER_VALID(b));
REQUIRE(b->used - b->current >= length);
cp = b->base;
cp += b->current;
b->current += length;
memcpy(base, cp, length);
}
+703
View File
@@ -0,0 +1,703 @@
/*
* Copyright (C) 2000, 2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: lwconfig.c,v 1.1 2004/03/15 20:35:25 as Exp $ */
/***
*** Module for parsing resolv.conf files.
***
*** entry points are:
*** lwres_conf_init(lwres_context_t *ctx)
*** intializes data structure for subsequent config parsing.
***
*** lwres_conf_parse(lwres_context_t *ctx, const char *filename)
*** parses a file and fills in the data structure.
***
*** lwres_conf_print(lwres_context_t *ctx, FILE *fp)
*** prints the config data structure to the FILE.
***
*** lwres_conf_clear(lwres_context_t *ctx)
*** frees up all the internal memory used by the config data
*** structure, returning it to the lwres_context_t.
***
***/
#include <config.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <lwres/lwbuffer.h>
#include <lwres/lwres.h>
#include <lwres/net.h>
#include <lwres/result.h>
#include "assert_p.h"
#include "context_p.h"
#if ! defined(NS_INADDRSZ)
#define NS_INADDRSZ 4
#endif
#if ! defined(NS_IN6ADDRSZ)
#define NS_IN6ADDRSZ 16
#endif
static lwres_result_t
lwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp);
static lwres_result_t
lwres_conf_parselwserver(lwres_context_t *ctx, FILE *fp);
static lwres_result_t
lwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp);
static lwres_result_t
lwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp);
static lwres_result_t
lwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp);
static lwres_result_t
lwres_conf_parseoption(lwres_context_t *ctx, FILE *fp);
static void
lwres_resetaddr(lwres_addr_t *addr);
static lwres_result_t
lwres_create_addr(const char *buff, lwres_addr_t *addr, int convert_zero);
static int lwresaddr2af(int lwresaddrtype);
static int
lwresaddr2af(int lwresaddrtype)
{
int af = 0;
switch (lwresaddrtype) {
case LWRES_ADDRTYPE_V4:
af = AF_INET;
break;
case LWRES_ADDRTYPE_V6:
af = AF_INET6;
break;
}
return (af);
}
/*
* Eat characters from FP until EOL or EOF. Returns EOF or '\n'
*/
static int
eatline(FILE *fp) {
int ch;
ch = fgetc(fp);
while (ch != '\n' && ch != EOF)
ch = fgetc(fp);
return (ch);
}
/*
* Eats white space up to next newline or non-whitespace character (of
* EOF). Returns the last character read. Comments are considered white
* space.
*/
static int
eatwhite(FILE *fp) {
int ch;
ch = fgetc(fp);
while (ch != '\n' && ch != EOF && isspace((unsigned char)ch))
ch = fgetc(fp);
if (ch == ';' || ch == '#')
ch = eatline(fp);
return (ch);
}
/*
* Skip over any leading whitespace and then read in the next sequence of
* non-whitespace characters. In this context newline is not considered
* whitespace. Returns EOF on end-of-file, or the character
* that caused the reading to stop.
*/
static int
getword(FILE *fp, char *buffer, size_t size) {
int ch;
char *p = buffer;
REQUIRE(buffer != NULL);
REQUIRE(size > 0);
*p = '\0';
ch = eatwhite(fp);
if (ch == EOF)
return (EOF);
do {
*p = '\0';
if (ch == EOF || isspace((unsigned char)ch))
break;
else if ((size_t) (p - buffer) == size - 1)
return (EOF); /* Not enough space. */
*p++ = (char)ch;
ch = fgetc(fp);
} while (1);
return (ch);
}
static void
lwres_resetaddr(lwres_addr_t *addr) {
REQUIRE(addr != NULL);
memset(addr->address, 0, LWRES_ADDR_MAXLEN);
addr->family = 0;
addr->length = 0;
}
static char *
lwres_strdup(lwres_context_t *ctx, const char *str) {
char *p;
REQUIRE(str != NULL);
REQUIRE(strlen(str) > 0);
p = CTXMALLOC(strlen(str) + 1);
if (p != NULL)
strcpy(p, str);
return (p);
}
void
lwres_conf_init(lwres_context_t *ctx) {
int i;
lwres_conf_t *confdata;
REQUIRE(ctx != NULL);
confdata = &ctx->confdata;
confdata->nsnext = 0;
confdata->lwnext = 0;
confdata->domainname = NULL;
confdata->searchnxt = 0;
confdata->sortlistnxt = 0;
confdata->resdebug = 0;
confdata->ndots = 1;
confdata->no_tld_query = 0;
for (i = 0 ; i < LWRES_CONFMAXNAMESERVERS ; i++)
lwres_resetaddr(&confdata->nameservers[i]);
for (i = 0 ; i < LWRES_CONFMAXSEARCH ; i++)
confdata->search[i] = NULL;
for (i = 0 ; i < LWRES_CONFMAXSORTLIST ; i++) {
lwres_resetaddr(&confdata->sortlist[i].addr);
lwres_resetaddr(&confdata->sortlist[i].mask);
}
}
void
lwres_conf_clear(lwres_context_t *ctx) {
int i;
lwres_conf_t *confdata;
REQUIRE(ctx != NULL);
confdata = &ctx->confdata;
for (i = 0 ; i < confdata->nsnext ; i++)
lwres_resetaddr(&confdata->nameservers[i]);
if (confdata->domainname != NULL) {
CTXFREE(confdata->domainname,
strlen(confdata->domainname) + 1);
confdata->domainname = NULL;
}
for (i = 0 ; i < confdata->searchnxt ; i++) {
if (confdata->search[i] != NULL) {
CTXFREE(confdata->search[i],
strlen(confdata->search[i]) + 1);
confdata->search[i] = NULL;
}
}
for (i = 0 ; i < LWRES_CONFMAXSORTLIST ; i++) {
lwres_resetaddr(&confdata->sortlist[i].addr);
lwres_resetaddr(&confdata->sortlist[i].mask);
}
confdata->nsnext = 0;
confdata->lwnext = 0;
confdata->domainname = NULL;
confdata->searchnxt = 0;
confdata->sortlistnxt = 0;
confdata->resdebug = 0;
confdata->ndots = 1;
confdata->no_tld_query = 0;
}
static lwres_result_t
lwres_conf_parsenameserver(lwres_context_t *ctx, FILE *fp) {
char word[LWRES_CONFMAXLINELEN];
int res;
lwres_conf_t *confdata;
confdata = &ctx->confdata;
if (confdata->nsnext == LWRES_CONFMAXNAMESERVERS)
return (LWRES_R_SUCCESS);
res = getword(fp, word, sizeof(word));
if (strlen(word) == 0)
return (LWRES_R_FAILURE); /* Nothing on line. */
else if (res == ' ' || res == '\t')
res = eatwhite(fp);
if (res != EOF && res != '\n')
return (LWRES_R_FAILURE); /* Extra junk on line. */
res = lwres_create_addr(word,
&confdata->nameservers[confdata->nsnext++], 1);
if (res != LWRES_R_SUCCESS)
return (res);
return (LWRES_R_SUCCESS);
}
static lwres_result_t
lwres_conf_parselwserver(lwres_context_t *ctx, FILE *fp) {
char word[LWRES_CONFMAXLINELEN];
int res;
lwres_conf_t *confdata;
confdata = &ctx->confdata;
if (confdata->lwnext == LWRES_CONFMAXLWSERVERS)
return (LWRES_R_SUCCESS);
res = getword(fp, word, sizeof(word));
if (strlen(word) == 0)
return (LWRES_R_FAILURE); /* Nothing on line. */
else if (res == ' ' || res == '\t')
res = eatwhite(fp);
if (res != EOF && res != '\n')
return (LWRES_R_FAILURE); /* Extra junk on line. */
res = lwres_create_addr(word,
&confdata->lwservers[confdata->lwnext++], 1);
if (res != LWRES_R_SUCCESS)
return (res);
return (LWRES_R_SUCCESS);
}
static lwres_result_t
lwres_conf_parsedomain(lwres_context_t *ctx, FILE *fp) {
char word[LWRES_CONFMAXLINELEN];
int res, i;
lwres_conf_t *confdata;
confdata = &ctx->confdata;
res = getword(fp, word, sizeof(word));
if (strlen(word) == 0)
return (LWRES_R_FAILURE); /* Nothing else on line. */
else if (res == ' ' || res == '\t')
res = eatwhite(fp);
if (res != EOF && res != '\n')
return (LWRES_R_FAILURE); /* Extra junk on line. */
if (confdata->domainname != NULL)
CTXFREE(confdata->domainname,
strlen(confdata->domainname) + 1); /* */
/*
* Search and domain are mutually exclusive.
*/
for (i = 0 ; i < LWRES_CONFMAXSEARCH ; i++) {
if (confdata->search[i] != NULL) {
CTXFREE(confdata->search[i],
strlen(confdata->search[i])+1);
confdata->search[i] = NULL;
}
}
confdata->searchnxt = 0;
confdata->domainname = lwres_strdup(ctx, word);
if (confdata->domainname == NULL)
return (LWRES_R_FAILURE);
return (LWRES_R_SUCCESS);
}
static lwres_result_t
lwres_conf_parsesearch(lwres_context_t *ctx, FILE *fp) {
int idx, delim;
char word[LWRES_CONFMAXLINELEN];
lwres_conf_t *confdata;
confdata = &ctx->confdata;
if (confdata->domainname != NULL) {
/*
* Search and domain are mutually exclusive.
*/
CTXFREE(confdata->domainname,
strlen(confdata->domainname) + 1);
confdata->domainname = NULL;
}
/*
* Remove any previous search definitions.
*/
for (idx = 0 ; idx < LWRES_CONFMAXSEARCH ; idx++) {
if (confdata->search[idx] != NULL) {
CTXFREE(confdata->search[idx],
strlen(confdata->search[idx])+1);
confdata->search[idx] = NULL;
}
}
confdata->searchnxt = 0;
delim = getword(fp, word, sizeof(word));
if (strlen(word) == 0)
return (LWRES_R_FAILURE); /* Nothing else on line. */
idx = 0;
while (strlen(word) > 0) {
if (confdata->searchnxt == LWRES_CONFMAXSEARCH)
goto ignore; /* Too many domains. */
confdata->search[idx] = lwres_strdup(ctx, word);
if (confdata->search[idx] == NULL)
return (LWRES_R_FAILURE);
idx++;
confdata->searchnxt++;
ignore:
if (delim == EOF || delim == '\n')
break;
else
delim = getword(fp, word, sizeof(word));
}
return (LWRES_R_SUCCESS);
}
static lwres_result_t
lwres_create_addr(const char *buffer, lwres_addr_t *addr, int convert_zero) {
struct in_addr v4;
struct in6_addr v6;
if (lwres_net_aton(buffer, &v4) == 1) {
if (convert_zero) {
unsigned char zeroaddress[] = {0, 0, 0, 0};
unsigned char loopaddress[] = {127, 0, 0, 1};
if (memcmp(&v4, zeroaddress, 4) == 0)
memcpy(&v4, loopaddress, 4);
}
addr->family = LWRES_ADDRTYPE_V4;
addr->length = NS_INADDRSZ;
memcpy((void *)addr->address, &v4, NS_INADDRSZ);
} else if (lwres_net_pton(AF_INET6, buffer, &v6) == 1) {
addr->family = LWRES_ADDRTYPE_V6;
addr->length = NS_IN6ADDRSZ;
memcpy((void *)addr->address, &v6, NS_IN6ADDRSZ);
} else {
return (LWRES_R_FAILURE); /* Unrecognised format. */
}
return (LWRES_R_SUCCESS);
}
static lwres_result_t
lwres_conf_parsesortlist(lwres_context_t *ctx, FILE *fp) {
int delim, res, idx;
char word[LWRES_CONFMAXLINELEN];
char *p;
lwres_conf_t *confdata;
confdata = &ctx->confdata;
delim = getword(fp, word, sizeof(word));
if (strlen(word) == 0)
return (LWRES_R_FAILURE); /* Empty line after keyword. */
while (strlen(word) > 0) {
if (confdata->sortlistnxt == LWRES_CONFMAXSORTLIST)
return (LWRES_R_FAILURE); /* Too many values. */
p = strchr(word, '/');
if (p != NULL)
*p++ = '\0';
idx = confdata->sortlistnxt;
res = lwres_create_addr(word, &confdata->sortlist[idx].addr, 1);
if (res != LWRES_R_SUCCESS)
return (res);
if (p != NULL) {
res = lwres_create_addr(p,
&confdata->sortlist[idx].mask,
0);
if (res != LWRES_R_SUCCESS)
return (res);
} else {
/*
* Make up a mask.
*/
confdata->sortlist[idx].mask =
confdata->sortlist[idx].addr;
memset(&confdata->sortlist[idx].mask.address, 0xff,
confdata->sortlist[idx].addr.length);
}
confdata->sortlistnxt++;
if (delim == EOF || delim == '\n')
break;
else
delim = getword(fp, word, sizeof(word));
}
return (LWRES_R_SUCCESS);
}
static lwres_result_t
lwres_conf_parseoption(lwres_context_t *ctx, FILE *fp) {
int delim;
long ndots;
char *p;
char word[LWRES_CONFMAXLINELEN];
lwres_conf_t *confdata;
REQUIRE(ctx != NULL);
confdata = &ctx->confdata;
delim = getword(fp, word, sizeof(word));
if (strlen(word) == 0)
return (LWRES_R_FAILURE); /* Empty line after keyword. */
while (strlen(word) > 0) {
if (strcmp("debug", word) == 0) {
confdata->resdebug = 1;
} else if (strcmp("no_tld_query", word) == 0) {
confdata->no_tld_query = 1;
} else if (strncmp("ndots:", word, 6) == 0) {
ndots = strtol(word + 6, &p, 10);
if (*p != '\0') /* Bad string. */
return (LWRES_R_FAILURE);
if (ndots < 0 || ndots > 0xff) /* Out of range. */
return (LWRES_R_FAILURE);
confdata->ndots = (lwres_uint8_t)ndots;
}
if (delim == EOF || delim == '\n')
break;
else
delim = getword(fp, word, sizeof(word));
}
return (LWRES_R_SUCCESS);
}
lwres_result_t
lwres_conf_parse(lwres_context_t *ctx, const char *filename) {
FILE *fp = NULL;
char word[256];
lwres_result_t rval, ret;
lwres_conf_t *confdata;
int stopchar;
REQUIRE(ctx != NULL);
confdata = &ctx->confdata;
REQUIRE(filename != NULL);
REQUIRE(strlen(filename) > 0);
REQUIRE(confdata != NULL);
errno = 0;
if ((fp = fopen(filename, "r")) == NULL)
return (LWRES_R_FAILURE);
ret = LWRES_R_SUCCESS;
do {
stopchar = getword(fp, word, sizeof(word));
if (stopchar == EOF) {
rval = LWRES_R_SUCCESS;
break;
}
if (strlen(word) == 0)
rval = LWRES_R_SUCCESS;
else if (strcmp(word, "nameserver") == 0)
rval = lwres_conf_parsenameserver(ctx, fp);
else if (strcmp(word, "lwserver") == 0)
rval = lwres_conf_parselwserver(ctx, fp);
else if (strcmp(word, "domain") == 0)
rval = lwres_conf_parsedomain(ctx, fp);
else if (strcmp(word, "search") == 0)
rval = lwres_conf_parsesearch(ctx, fp);
else if (strcmp(word, "sortlist") == 0)
rval = lwres_conf_parsesortlist(ctx, fp);
else if (strcmp(word, "option") == 0)
rval = lwres_conf_parseoption(ctx, fp);
else {
/* unrecognised word. Ignore entire line */
rval = LWRES_R_SUCCESS;
stopchar = eatline(fp);
if (stopchar == EOF) {
break;
}
}
if (ret == LWRES_R_SUCCESS && rval != LWRES_R_SUCCESS)
ret = rval;
} while (1);
fclose(fp);
return (ret);
}
lwres_result_t
lwres_conf_print(lwres_context_t *ctx, FILE *fp) {
int i;
int af;
char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
const char *p;
lwres_conf_t *confdata;
lwres_addr_t tmpaddr;
REQUIRE(ctx != NULL);
confdata = &ctx->confdata;
REQUIRE(confdata->nsnext <= LWRES_CONFMAXNAMESERVERS);
for (i = 0 ; i < confdata->nsnext ; i++) {
af = lwresaddr2af(confdata->nameservers[i].family);
p = lwres_net_ntop(af, confdata->nameservers[i].address,
tmp, sizeof(tmp));
if (p != tmp)
return (LWRES_R_FAILURE);
fprintf(fp, "nameserver %s\n", tmp);
}
for (i = 0 ; i < confdata->lwnext ; i++) {
af = lwresaddr2af(confdata->lwservers[i].family);
p = lwres_net_ntop(af, confdata->lwservers[i].address,
tmp, sizeof(tmp));
if (p != tmp)
return (LWRES_R_FAILURE);
fprintf(fp, "lwserver %s\n", tmp);
}
if (confdata->domainname != NULL) {
fprintf(fp, "domain %s\n", confdata->domainname);
} else if (confdata->searchnxt > 0) {
REQUIRE(confdata->searchnxt <= LWRES_CONFMAXSEARCH);
fprintf(fp, "search");
for (i = 0 ; i < confdata->searchnxt ; i++)
fprintf(fp, " %s", confdata->search[i]);
fputc('\n', fp);
}
REQUIRE(confdata->sortlistnxt <= LWRES_CONFMAXSORTLIST);
if (confdata->sortlistnxt > 0) {
fputs("sortlist", fp);
for (i = 0 ; i < confdata->sortlistnxt ; i++) {
af = lwresaddr2af(confdata->sortlist[i].addr.family);
p = lwres_net_ntop(af,
confdata->sortlist[i].addr.address,
tmp, sizeof(tmp));
if (p != tmp)
return (LWRES_R_FAILURE);
fprintf(fp, " %s", tmp);
tmpaddr = confdata->sortlist[i].mask;
memset(&tmpaddr.address, 0xff, tmpaddr.length);
if (memcmp(&tmpaddr.address,
confdata->sortlist[i].mask.address,
confdata->sortlist[i].mask.length) != 0) {
af = lwresaddr2af(
confdata->sortlist[i].mask.family);
p = lwres_net_ntop
(af,
confdata->sortlist[i].mask.address,
tmp, sizeof(tmp));
if (p != tmp)
return (LWRES_R_FAILURE);
fprintf(fp, "/%s", tmp);
}
}
fputc('\n', fp);
}
if (confdata->resdebug)
fprintf(fp, "options debug\n");
if (confdata->ndots > 0)
fprintf(fp, "options ndots:%d\n", confdata->ndots);
if (confdata->no_tld_query)
fprintf(fp, "options no_tld_query\n");
return (LWRES_R_SUCCESS);
}
lwres_conf_t *
lwres_conf_get(lwres_context_t *ctx) {
REQUIRE(ctx != NULL);
return (&ctx->confdata);
}
+203
View File
@@ -0,0 +1,203 @@
/*
* Portions Copyright (C) 1996-2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Copyright (c) 1983, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Portions Copyright (c) 1993 by Digital Equipment Corporation.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies, and that
* the name of Digital Equipment Corporation not be used in advertising or
* publicity pertaining to distribution of the document or software without
* specific, written prior permission.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
* CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93";
static char rcsid[] = "$Id: lwinetaton.c,v 1.1 2004/03/15 20:35:25 as Exp $";
#endif /* LIBC_SCCS and not lint */
#include <config.h>
#include <ctype.h>
#include <stddef.h>
#include <lwres/int.h>
#include <lwres/net.h>
#include "assert_p.h"
/*
* Check whether "cp" is a valid ascii representation
* of an Internet address and convert to a binary address.
* Returns 1 if the address is valid, 0 if not.
* This replaces inet_addr, the return value from which
* cannot distinguish between failure and a local broadcast address.
*/
int
lwres_net_aton(const char *cp, struct in_addr *addr) {
unsigned long val;
int base, n;
unsigned char c;
lwres_uint8_t parts[4];
lwres_uint8_t *pp = parts;
int digit;
REQUIRE(cp != NULL);
c = *cp;
for (;;) {
/*
* Collect number up to ``.''.
* Values are specified as for C:
* 0x=hex, 0=octal, isdigit=decimal.
*/
if (!isdigit(c & 0xff))
return (0);
val = 0;
base = 10;
digit = 0;
if (c == '0') {
c = *++cp;
if (c == 'x' || c == 'X') {
base = 16;
c = *++cp;
} else {
base = 8;
digit = 1;
}
}
for (;;) {
/*
* isascii() is valid for all integer values, and
* when it is true, c is known to be in scope
* for isdigit(). No cast necessary. Similar
* comment applies for later ctype uses.
*/
if (isascii(c) && isdigit(c)) {
if (base == 8 && (c == '8' || c == '9'))
return (0);
val = (val * base) + (c - '0');
c = *++cp;
digit = 1;
} else if (base == 16 && isascii(c) && isxdigit(c)) {
val = (val << 4) |
(c + 10 - (islower(c) ? 'a' : 'A'));
c = *++cp;
digit = 1;
} else
break;
}
if (c == '.') {
/*
* Internet format:
* a.b.c.d
* a.b.c (with c treated as 16 bits)
* a.b (with b treated as 24 bits)
*/
if (pp >= parts + 3 || val > 0xff)
return (0);
*pp++ = (lwres_uint8_t)val;
c = *++cp;
} else
break;
}
/*
* Check for trailing characters.
*/
if (c != '\0' && (!isascii(c) || !isspace(c)))
return (0);
/*
* Did we get a valid digit?
*/
if (!digit)
return (0);
/*
* Concoct the address according to
* the number of parts specified.
*/
n = pp - parts + 1;
switch (n) {
case 1: /* a -- 32 bits */
break;
case 2: /* a.b -- 8.24 bits */
if (val > 0xffffff)
return (0);
val |= parts[0] << 24;
break;
case 3: /* a.b.c -- 8.8.16 bits */
if (val > 0xffff)
return (0);
val |= (parts[0] << 24) | (parts[1] << 16);
break;
case 4: /* a.b.c.d -- 8.8.8.8 bits */
if (val > 0xff)
return (0);
val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
break;
}
if (addr != NULL)
addr->s_addr = htonl(val);
return (1);
}

Some files were not shown because too many files have changed in this diff Show More